1 # a waf tool to add autoconf-like macros to the configure section
2 # and for SAMBA_ macros for building libraries, binaries etc
4 import Build, os, Options, Task, Utils, cc, TaskGen, fnmatch, re, shutil, Logs
5 from Configure import conf
7 from samba_utils import SUBST_VARS_RECURSIVE
9 # bring in the other samba modules
10 from samba_optimisation import *
11 from samba_utils import *
12 from samba_autoconf import *
13 from samba_patterns import *
14 from samba_pidl import *
15 from samba_errtable import *
16 from samba_asn1 import *
17 from samba_autoproto import *
18 from samba_python import *
19 from samba_deps import *
20 from samba_bundled import *
22 import samba_conftests
25 # some systems have broken threading in python
26 if os.environ.get('WAF_NOTHREADS') == '1':
31 os.putenv('PYTHONUNBUFFERED', '1')
34 def SAMBA_BUILD_ENV(conf):
35 '''create the samba build environment'''
36 conf.env['BUILD_DIRECTORY'] = conf.blddir
37 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
38 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
39 # this allows all of the bin/shared and bin/python targets
40 # to be expressed in terms of build directory paths
41 for p in ['python','shared']:
42 link_target = os.path.join(conf.blddir, 'default/' + p)
43 if not os.path.lexists(link_target):
44 os.symlink('../' + p, link_target)
46 # get perl to put the blib files in the build directory
47 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
48 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
49 mkdir_p(blib_bld + '/man1')
50 mkdir_p(blib_bld + '/man3')
51 if os.path.islink(blib_src):
53 elif os.path.exists(blib_src):
54 shutil.rmtree(blib_src)
57 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
58 '''add an init_function to the list for a subsystem'''
59 if init_function is None:
61 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
62 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
63 if not subsystem in cache:
65 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
66 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
70 #################################################################
71 def SAMBA_LIBRARY(bld, libname, source,
80 external_library=False,
90 target_type='LIBRARY',
91 bundled_extension=True,
94 '''define a Samba library'''
97 SET_TARGET_TYPE(bld, libname, 'DISABLED')
100 source = bld.EXPAND_VARIABLES(source, vars=vars)
102 # remember empty libraries, so we can strip the dependencies
103 if (source == '') or (source == []):
104 SET_TARGET_TYPE(bld, libname, 'EMPTY')
107 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
110 obj_target = libname + '.objlist'
112 # first create a target for building the object files for this library
113 # by separating in this way, we avoid recompiling the C files
114 # separately for the install library and the build library
115 bld.SAMBA_SUBSYSTEM(obj_target,
118 public_deps = public_deps,
120 public_headers = public_headers,
121 header_path = header_path,
124 autoproto = autoproto,
125 depends_on = depends_on,
126 needs_python = needs_python,
127 local_include = local_include)
129 if libname == obj_target:
132 if not SET_TARGET_TYPE(bld, libname, target_type):
135 # the library itself will depend on that object target
136 deps += ' ' + public_deps
138 deps.append(obj_target)
140 if target_type == 'PYTHON':
141 bundled_name = libname
143 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
145 features = 'cc cshlib symlink_lib install_lib'
146 if target_type == 'PYTHON':
149 features += ' pyembed'
151 bld.SET_BUILD_GROUP(group)
155 target = bundled_name,
156 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
157 depends_on = depends_on,
159 samba_includes = includes,
160 local_include = local_include,
163 samba_inst_path = install_path,
165 samba_realname = realname,
166 samba_install = install
170 t.link_name = link_name
172 if autoproto is not None:
173 bld.SAMBA_AUTOPROTO(autoproto, source)
175 if public_headers is not None:
176 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
178 if pc_files is not None:
179 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
181 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
184 #################################################################
185 def SAMBA_BINARY(bld, binname, source,
205 '''define a Samba binary'''
207 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
210 features = 'cc cprogram symlink_bin install_bin'
212 features += ' pyembed'
214 obj_target = binname + '.objlist'
216 source = bld.EXPAND_VARIABLES(source, vars=vars)
218 # first create a target for building the object files for this binary
219 # by separating in this way, we avoid recompiling the C files
220 # separately for the install binary and the build binary
221 bld.SAMBA_SUBSYSTEM(obj_target,
227 autoproto = autoproto,
228 subsystem_name = subsystem_name,
229 needs_python = needs_python,
230 local_include = local_include)
232 bld.SET_BUILD_GROUP(group)
234 # the binary itself will depend on that object target
236 deps.append(obj_target)
242 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
244 samba_includes = includes,
245 local_include = local_include,
246 samba_modules = modules,
248 samba_subsystem= subsystem_name,
250 samba_inst_path= install_path,
251 samba_install = install
254 # setup the subsystem_name as an alias for the real
255 # binary name, so it can be found when expanding
256 # subsystem dependencies
257 if subsystem_name is not None:
258 bld.TARGET_ALIAS(subsystem_name, binname)
260 if autoproto is not None:
261 bld.SAMBA_AUTOPROTO(autoproto, source)
262 if public_headers is not None:
263 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
264 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
267 #################################################################
268 def SAMBA_MODULE(bld, modname, source,
274 autoproto_extra_source='',
277 internal_module=True,
281 '''define a Samba module.'''
283 # we add the init function regardless of whether the module
284 # is enabled or not, as we need to generate a null list if
286 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
288 if internal_module or BUILTIN_LIBRARY(bld, modname):
289 # treat internal modules as subsystems for now
290 SAMBA_SUBSYSTEM(bld, modname, source,
294 autoproto_extra_source=autoproto_extra_source,
296 local_include=local_include,
301 SET_TARGET_TYPE(bld, modname, 'DISABLED')
304 source = bld.EXPAND_VARIABLES(source, vars=vars)
306 # remember empty modules, so we can strip the dependencies
307 if (source == '') or (source == []):
308 SET_TARGET_TYPE(bld, modname, 'EMPTY')
311 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
314 if subsystem is not None:
315 deps += ' ' + subsystem
317 bld.SET_BUILD_GROUP('main')
322 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
323 samba_includes = includes,
324 local_include = local_include,
325 samba_deps = TO_LIST(deps)
328 if autoproto is not None:
329 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
331 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
334 #################################################################
335 def SAMBA_SUBSYSTEM(bld, modname, source,
344 init_function_sentinal=None,
345 heimdal_autoproto=None,
346 heimdal_autoproto_options=None,
347 heimdal_autoproto_private=None,
349 autoproto_extra_source='',
352 local_include_first=True,
357 '''define a Samba subsystem'''
360 SET_TARGET_TYPE(bld, modname, 'DISABLED')
363 # remember empty subsystems, so we can strip the dependencies
364 if (source == '') or (source == []):
365 SET_TARGET_TYPE(bld, modname, 'EMPTY')
368 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
371 source = bld.EXPAND_VARIABLES(source, vars=vars)
373 deps += ' ' + public_deps
375 bld.SET_BUILD_GROUP(group)
385 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
386 depends_on = depends_on,
387 samba_deps = TO_LIST(deps),
388 samba_includes = includes,
389 local_include = local_include,
390 local_include_first = local_include_first,
391 samba_subsystem= subsystem_name
394 if cflags_end is not None:
395 t.samba_cflags.extend(TO_LIST(cflags_end))
397 if heimdal_autoproto is not None:
398 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
399 if heimdal_autoproto_private is not None:
400 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
401 if autoproto is not None:
402 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
403 if public_headers is not None:
404 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
408 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
411 def SAMBA_GENERATOR(bld, name, rule, source, target,
412 group='generators', enabled=True,
416 '''A generic source generator target'''
418 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
424 bld.SET_BUILD_GROUP(group)
427 source=bld.EXPAND_VARIABLES(source, vars=vars),
429 shell=isinstance(rule, str),
435 if public_headers is not None:
436 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
438 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
442 def BUILD_SUBDIR(bld, dir):
443 '''add a new set of build rules from a subdirectory'''
444 path = os.path.normpath(bld.curdir + '/' + dir)
445 cache = LOCAL_CACHE(bld, 'SUBDIR_LIST')
446 if path in cache: return
448 debug("build: Processing subdirectory %s" % dir)
450 Build.BuildContext.BUILD_SUBDIR = BUILD_SUBDIR
455 def SETUP_BUILD_GROUPS(bld):
456 '''setup build groups used to ensure that the different build
457 phases happen consecutively'''
458 bld.p_ln = bld.srcnode # we do want to see all targets!
459 bld.env['USING_BUILD_GROUPS'] = True
460 bld.add_group('setup')
461 bld.add_group('build_compiler_source')
462 bld.add_group('base_libraries')
463 bld.add_group('generators')
464 bld.add_group('compiler_prototypes')
465 bld.add_group('compiler_libraries')
466 bld.add_group('build_compilers')
467 bld.add_group('build_source')
468 bld.add_group('prototypes')
469 bld.add_group('main')
470 bld.add_group('binaries')
471 bld.add_group('final')
472 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
475 def SET_BUILD_GROUP(bld, group):
476 '''set the current build group'''
477 if not 'USING_BUILD_GROUPS' in bld.env:
480 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
485 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
486 """use timestamps instead of file contents for deps
487 this currently doesn't work"""
488 def h_file(filename):
490 st = os.stat(filename)
491 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
493 m.update(str(st.st_mtime))
494 m.update(str(st.st_size))
497 Utils.h_file = h_file
501 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
502 shell=True, color='PINK', ext_in='.bin')
505 @feature('copy_script')
506 @before('apply_link')
507 def copy_script(self):
508 tsk = self.create_task('copy_script', self.allnodes[0])
509 tsk.env.TARGET = self.target
511 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
512 '''used to copy scripts from the source tree into the build directory
513 for use by selftest'''
515 source = bld.path.ant_glob(pattern)
517 bld.SET_BUILD_GROUP('build_source')
518 for s in TO_LIST(source):
520 if installname != None:
522 target = os.path.join(installdir, iname)
523 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
525 t = bld(features='copy_script',
530 t.env.LINK_TARGET = target
532 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
535 def install_file(bld, destdir, file, chmod=0644, flat=False,
536 python_fixup=False, destname=None, base_name=None):
538 destdir = bld.EXPAND_VARIABLES(destdir)
542 destname = os.path.basename(destname)
543 dest = os.path.join(destdir, destname)
545 # fixup the python path it will use to find Samba modules
546 inst_file = file + '.inst'
547 bld.SAMBA_GENERATOR('python_%s' % destname,
548 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
553 file = os.path.join(base_name, file)
554 bld.install_as(dest, file, chmod=chmod)
557 def INSTALL_FILES(bld, destdir, files, chmod=0644, flat=False,
558 python_fixup=False, destname=None, base_name=None):
559 '''install a set of files'''
560 for f in TO_LIST(files):
561 install_file(bld, destdir, f, chmod=chmod, flat=flat,
562 python_fixup=python_fixup, destname=destname,
564 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
567 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=0644, flat=False,
568 python_fixup=False, exclude=None, trim_path=None):
569 '''install a set of files matching a wildcard pattern'''
570 files=TO_LIST(bld.path.ant_glob(pattern))
574 files2.append(os_path_relpath(f, trim_path))
579 if fnmatch.fnmatch(f, exclude):
581 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
582 python_fixup=python_fixup, base_name=trim_path)
583 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
586 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
587 '''install some headers
589 header_path may either be a string that is added to the INCLUDEDIR,
590 or it can be a dictionary of wildcard patterns which map to destination
591 directories relative to INCLUDEDIR
593 dest = '${INCLUDEDIR}'
594 if isinstance(header_path, str):
595 dest += '/' + header_path
596 for h in TO_LIST(public_headers):
598 if isinstance(header_path, list):
599 for (p1, dir) in header_path:
603 if fnmatch.fnmatch(h, p2):
605 hdest = os.path.join(hdest, dir)
608 if found_match: break
609 if h.find(':') != -1:
611 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
613 INSTALL_FILES(bld, hdest, h, flat=True)
614 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
617 def subst_at_vars(task):
618 '''substiture @VAR@ style variables in a file'''
619 src = task.inputs[0].srcpath(task.env)
620 tgt = task.outputs[0].bldpath(task.env)
626 a = re.split('(@\w+@)', s)
629 if re.match('@\w+@', v):
631 if not vname in task.env and vname.upper() in task.env:
632 vname = vname.upper()
633 if not vname in task.env:
634 print "Unknown substitution %s in %s" % (v, task.name)
638 contents = ''.join(out)
640 s = f.write(contents)
646 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
647 '''install some pkg_config pc files'''
648 dest = '${PKGCONFIGDIR}'
649 dest = bld.EXPAND_VARIABLES(dest)
650 for f in TO_LIST(pc_files):
651 base=os.path.basename(f)
652 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
657 t.env.PACKAGE_VERSION = vnum
658 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
659 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
663 #############################################################
664 # give a nicer display when building different types of files
665 def progress_display(self, msg, fname):
666 col1 = Logs.colors(self.color)
667 col2 = Logs.colors.NORMAL
668 total = self.position[1]
670 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
671 return fs % (self.position[0], self.position[1], col1, fname, col2)
673 def link_display(self):
674 if Options.options.progress_bar != 0:
675 return Task.Task.old_display(self)
676 fname = self.outputs[0].bldpath(self.env)
677 return progress_display(self, 'Linking', fname)
678 Task.TaskBase.classes['cc_link'].display = link_display
680 def samba_display(self):
681 if Options.options.progress_bar != 0:
682 return Task.Task.old_display(self)
684 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
685 if self.name in targets:
686 target_type = targets[self.name]
687 type_map = { 'GENERATOR' : 'Generating',
688 'PROTOTYPE' : 'Generating'
690 if target_type in type_map:
691 return progress_display(self, type_map[target_type], self.name)
693 fname = self.inputs[0].bldpath(self.env)
694 if fname[0:3] == '../':
696 ext_loc = fname.rfind('.')
698 return Task.Task.old_display(self)
699 ext = fname[ext_loc:]
701 ext_map = { '.idl' : 'Compiling IDL',
702 '.et' : 'Compiling ERRTABLE',
703 '.asn1': 'Compiling ASN1',
706 return progress_display(self, ext_map[ext], fname)
707 return Task.Task.old_display(self)
709 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
710 Task.TaskBase.classes['Task'].display = samba_display