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, Constants
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
32 # some systems have broken threading in python
33 if os.environ.get('WAF_NOTHREADS') == '1':
38 os.putenv('PYTHONUNBUFFERED', '1')
41 if Constants.HEXVERSION < 0x105016:
43 Please use the version of waf that comes with Samba, not
44 a system installed version. See http://wiki.samba.org/index.php/Waf
47 Alternatively, please use ./autogen-waf.sh, and then
48 run ./configure and make as usual. That will call the right version of waf.
54 def SAMBA_BUILD_ENV(conf):
55 '''create the samba build environment'''
56 conf.env.BUILD_DIRECTORY = conf.blddir
57 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
58 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
59 # this allows all of the bin/shared and bin/python targets
60 # to be expressed in terms of build directory paths
61 mkdir_p(os.path.join(conf.blddir, 'default'))
62 for p in ['python','shared']:
63 link_target = os.path.join(conf.blddir, 'default/' + p)
64 if not os.path.lexists(link_target):
65 os.symlink('../' + p, link_target)
67 # get perl to put the blib files in the build directory
68 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
69 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
70 mkdir_p(blib_bld + '/man1')
71 mkdir_p(blib_bld + '/man3')
72 if os.path.islink(blib_src):
74 elif os.path.exists(blib_src):
75 shutil.rmtree(blib_src)
78 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
79 '''add an init_function to the list for a subsystem'''
80 if init_function is None:
82 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
83 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
84 if not subsystem in cache:
86 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
87 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
91 #################################################################
92 def SAMBA_LIBRARY(bld, libname, source,
101 external_library=False,
111 target_type='LIBRARY',
112 bundled_extension=True,
118 '''define a Samba library'''
121 SET_TARGET_TYPE(bld, libname, 'DISABLED')
124 source = bld.EXPAND_VARIABLES(source, vars=vars)
126 # remember empty libraries, so we can strip the dependencies
127 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
128 SET_TARGET_TYPE(bld, libname, 'EMPTY')
131 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
134 obj_target = libname + '.objlist'
136 # first create a target for building the object files for this library
137 # by separating in this way, we avoid recompiling the C files
138 # separately for the install library and the build library
139 bld.SAMBA_SUBSYSTEM(obj_target,
142 public_deps = public_deps,
144 public_headers = public_headers,
145 header_path = header_path,
148 autoproto = autoproto,
149 depends_on = depends_on,
150 needs_python = needs_python,
151 hide_symbols = hide_symbols,
152 local_include = local_include)
154 if libname == obj_target:
157 if not SET_TARGET_TYPE(bld, libname, target_type):
160 # the library itself will depend on that object target
161 deps += ' ' + public_deps
163 deps.append(obj_target)
165 if target_type == 'PYTHON' or realname:
166 bundled_name = libname
168 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
170 features = 'cc cshlib symlink_lib install_lib'
171 if target_type == 'PYTHON':
174 features += ' pyembed'
176 features += ' abi_check'
179 abi_file = os.path.join(bld.curdir, abi_file)
181 bld.SET_BUILD_GROUP(group)
185 target = bundled_name,
186 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
187 depends_on = depends_on,
189 samba_includes = includes,
190 local_include = local_include,
193 samba_inst_path = install_path,
195 samba_realname = realname,
196 samba_install = install,
198 abi_match = abi_match
201 if realname and not link_name:
202 link_name = 'shared/%s' % realname
205 t.link_name = link_name
207 if pc_files is not None:
208 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
210 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
213 #################################################################
214 def SAMBA_BINARY(bld, binname, source,
224 use_global_deps=True,
235 '''define a Samba binary'''
238 SET_TARGET_TYPE(bld, binname, 'DISABLED')
241 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
244 features = 'cc cprogram symlink_bin install_bin'
246 features += ' pyembed'
248 obj_target = binname + '.objlist'
250 source = bld.EXPAND_VARIABLES(source, vars=vars)
251 source = unique_list(TO_LIST(source))
253 # first create a target for building the object files for this binary
254 # by separating in this way, we avoid recompiling the C files
255 # separately for the install binary and the build binary
256 bld.SAMBA_SUBSYSTEM(obj_target,
262 autoproto = autoproto,
263 subsystem_name = subsystem_name,
264 needs_python = needs_python,
265 local_include = local_include,
266 use_hostcc = use_hostcc,
267 use_global_deps= use_global_deps)
269 bld.SET_BUILD_GROUP(group)
271 # the binary itself will depend on that object target
273 deps.append(obj_target)
279 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
281 samba_includes = includes,
282 local_include = local_include,
283 samba_modules = modules,
285 samba_subsystem= subsystem_name,
287 samba_inst_path= install_path,
288 samba_install = install
291 # setup the subsystem_name as an alias for the real
292 # binary name, so it can be found when expanding
293 # subsystem dependencies
294 if subsystem_name is not None:
295 bld.TARGET_ALIAS(subsystem_name, binname)
297 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
300 #################################################################
301 def SAMBA_MODULE(bld, modname, source,
307 autoproto_extra_source='',
310 internal_module=True,
314 '''define a Samba module.'''
316 # we add the init function regardless of whether the module
317 # is enabled or not, as we need to generate a null list if
319 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
321 if internal_module or BUILTIN_LIBRARY(bld, modname):
322 # treat internal modules as subsystems for now
323 SAMBA_SUBSYSTEM(bld, modname, source,
327 autoproto_extra_source=autoproto_extra_source,
329 local_include=local_include,
334 SET_TARGET_TYPE(bld, modname, 'DISABLED')
337 source = bld.EXPAND_VARIABLES(source, vars=vars)
338 source = unique_list(TO_LIST(source))
340 # remember empty modules, so we can strip the dependencies
341 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
342 SET_TARGET_TYPE(bld, modname, 'EMPTY')
345 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
348 if subsystem is not None:
349 deps += ' ' + subsystem
351 bld.SET_BUILD_GROUP('main')
356 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
357 samba_includes = includes,
358 local_include = local_include,
359 samba_deps = TO_LIST(deps)
362 if autoproto is not None:
363 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
365 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
368 #################################################################
369 def SAMBA_SUBSYSTEM(bld, modname, source,
378 init_function_sentinal=None,
379 heimdal_autoproto=None,
380 heimdal_autoproto_options=None,
381 heimdal_autoproto_private=None,
383 autoproto_extra_source='',
386 local_include_first=True,
390 use_global_deps=True,
394 '''define a Samba subsystem'''
397 SET_TARGET_TYPE(bld, modname, 'DISABLED')
400 # remember empty subsystems, so we can strip the dependencies
401 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
402 SET_TARGET_TYPE(bld, modname, 'EMPTY')
405 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
408 source = bld.EXPAND_VARIABLES(source, vars=vars)
409 source = unique_list(TO_LIST(source))
411 deps += ' ' + public_deps
413 bld.SET_BUILD_GROUP(group)
423 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags, hide_symbols=hide_symbols),
424 depends_on = depends_on,
425 samba_deps = TO_LIST(deps),
426 samba_includes = includes,
427 local_include = local_include,
428 local_include_first = local_include_first,
429 samba_subsystem= subsystem_name,
430 samba_use_hostcc = use_hostcc,
431 samba_use_global_deps = use_global_deps
434 if cflags_end is not None:
435 t.samba_cflags.extend(TO_LIST(cflags_end))
437 if heimdal_autoproto is not None:
438 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
439 if heimdal_autoproto_private is not None:
440 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
441 if autoproto is not None:
442 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
443 if public_headers is not None:
444 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
448 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
451 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
452 group='generators', enabled=True,
456 '''A generic source generator target'''
458 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
464 bld.SET_BUILD_GROUP(group)
467 source=bld.EXPAND_VARIABLES(source, vars=vars),
469 shell=isinstance(rule, str),
475 if public_headers is not None:
476 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
478 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
483 def SETUP_BUILD_GROUPS(bld):
484 '''setup build groups used to ensure that the different build
485 phases happen consecutively'''
486 bld.p_ln = bld.srcnode # we do want to see all targets!
487 bld.env['USING_BUILD_GROUPS'] = True
488 bld.add_group('setup')
489 bld.add_group('build_compiler_source')
490 bld.add_group('base_libraries')
491 bld.add_group('generators')
492 bld.add_group('compiler_prototypes')
493 bld.add_group('compiler_libraries')
494 bld.add_group('build_compilers')
495 bld.add_group('build_source')
496 bld.add_group('prototypes')
497 bld.add_group('main')
498 bld.add_group('binaries')
499 bld.add_group('final')
500 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
503 def SET_BUILD_GROUP(bld, group):
504 '''set the current build group'''
505 if not 'USING_BUILD_GROUPS' in bld.env:
508 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
513 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
514 """use timestamps instead of file contents for deps
515 this currently doesn't work"""
516 def h_file(filename):
518 st = os.stat(filename)
519 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
521 m.update(str(st.st_mtime))
522 m.update(str(st.st_size))
525 Utils.h_file = h_file
529 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
530 shell=True, color='PINK', ext_in='.bin')
533 @feature('copy_script')
534 @before('apply_link')
535 def copy_script(self):
536 tsk = self.create_task('copy_script', self.allnodes[0])
537 tsk.env.TARGET = self.target
539 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
540 '''used to copy scripts from the source tree into the build directory
541 for use by selftest'''
543 source = bld.path.ant_glob(pattern)
545 bld.SET_BUILD_GROUP('build_source')
546 for s in TO_LIST(source):
548 if installname != None:
550 target = os.path.join(installdir, iname)
551 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
553 t = bld(features='copy_script',
558 t.env.LINK_TARGET = target
560 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
563 def install_file(bld, destdir, file, chmod=O644, flat=False,
564 python_fixup=False, destname=None, base_name=None):
566 destdir = bld.EXPAND_VARIABLES(destdir)
570 destname = os.path.basename(destname)
571 dest = os.path.join(destdir, destname)
573 # fixup the python path it will use to find Samba modules
574 inst_file = file + '.inst'
575 bld.SAMBA_GENERATOR('python_%s' % destname,
576 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
581 file = os.path.join(base_name, file)
582 bld.install_as(dest, file, chmod=chmod)
585 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
586 python_fixup=False, destname=None, base_name=None):
587 '''install a set of files'''
588 for f in TO_LIST(files):
589 install_file(bld, destdir, f, chmod=chmod, flat=flat,
590 python_fixup=python_fixup, destname=destname,
592 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
595 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
596 python_fixup=False, exclude=None, trim_path=None):
597 '''install a set of files matching a wildcard pattern'''
598 files=TO_LIST(bld.path.ant_glob(pattern))
602 files2.append(os_path_relpath(f, trim_path))
607 if fnmatch.fnmatch(f, exclude):
609 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
610 python_fixup=python_fixup, base_name=trim_path)
611 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
614 def INSTALL_DIRS(bld, destdir, dirs):
615 '''install a set of directories'''
616 destdir = bld.EXPAND_VARIABLES(destdir)
617 dirs = bld.EXPAND_VARIABLES(dirs)
618 for d in TO_LIST(dirs):
619 bld.install_dir(os.path.join(destdir, d))
620 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
623 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
624 '''install some headers
626 header_path may either be a string that is added to the INCLUDEDIR,
627 or it can be a dictionary of wildcard patterns which map to destination
628 directories relative to INCLUDEDIR
630 dest = '${INCLUDEDIR}'
631 if isinstance(header_path, str):
632 dest += '/' + header_path
633 for h in TO_LIST(public_headers):
635 if isinstance(header_path, list):
636 for (p1, dir) in header_path:
640 if fnmatch.fnmatch(h, p2):
642 hdest = os.path.join(hdest, dir)
645 if found_match: break
646 if h.find(':') != -1:
648 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
650 INSTALL_FILES(bld, hdest, h, flat=True)
651 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
654 def subst_at_vars(task):
655 '''substiture @VAR@ style variables in a file'''
656 src = task.inputs[0].srcpath(task.env)
657 tgt = task.outputs[0].bldpath(task.env)
663 a = re.split('(@\w+@)', s)
665 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
667 if re.match('@\w+@', v):
669 if not vname in task.env and vname.upper() in task.env:
670 vname = vname.upper()
671 if not vname in task.env:
672 Logs.error("Unknown substitution %s in %s" % (v, task.name))
674 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
675 # now we back substitute the allowed pc vars
676 for (b, m) in back_sub:
681 contents = ''.join(out)
683 s = f.write(contents)
689 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
690 '''install some pkg_config pc files'''
691 dest = '${PKGCONFIGDIR}'
692 dest = bld.EXPAND_VARIABLES(dest)
693 for f in TO_LIST(pc_files):
694 base=os.path.basename(f)
695 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
700 t.env.PACKAGE_VERSION = vnum
701 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
702 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
706 #############################################################
707 # give a nicer display when building different types of files
708 def progress_display(self, msg, fname):
709 col1 = Logs.colors(self.color)
710 col2 = Logs.colors.NORMAL
711 total = self.position[1]
713 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
714 return fs % (self.position[0], self.position[1], col1, fname, col2)
716 def link_display(self):
717 if Options.options.progress_bar != 0:
718 return Task.Task.old_display(self)
719 fname = self.outputs[0].bldpath(self.env)
720 return progress_display(self, 'Linking', fname)
721 Task.TaskBase.classes['cc_link'].display = link_display
723 def samba_display(self):
724 if Options.options.progress_bar != 0:
725 return Task.Task.old_display(self)
727 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
728 if self.name in targets:
729 target_type = targets[self.name]
730 type_map = { 'GENERATOR' : 'Generating',
731 'PROTOTYPE' : 'Generating'
733 if target_type in type_map:
734 return progress_display(self, type_map[target_type], self.name)
736 fname = self.inputs[0].bldpath(self.env)
737 if fname[0:3] == '../':
739 ext_loc = fname.rfind('.')
741 return Task.Task.old_display(self)
742 ext = fname[ext_loc:]
744 ext_map = { '.idl' : 'Compiling IDL',
745 '.et' : 'Compiling ERRTABLE',
746 '.asn1': 'Compiling ASN1',
749 return progress_display(self, ext_map[ext], fname)
750 return Task.Task.old_display(self)
752 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
753 Task.TaskBase.classes['Task'].display = samba_display