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
31 # some systems have broken threading in python
32 if os.environ.get('WAF_NOTHREADS') == '1':
37 os.putenv('PYTHONUNBUFFERED', '1')
40 if Constants.HEXVERSION < 0x105016:
42 Please use the version of waf that comes with Samba, not
43 a system installed version. See http://wiki.samba.org/index.php/Waf
46 Alternatively, please use ./autogen-waf.sh, and then
47 run ./configure and make as usual. That will call the right version of waf.
53 def SAMBA_BUILD_ENV(conf):
54 '''create the samba build environment'''
55 conf.env.BUILD_DIRECTORY = conf.blddir
56 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
57 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
58 # this allows all of the bin/shared and bin/python targets
59 # to be expressed in terms of build directory paths
60 mkdir_p(os.path.join(conf.blddir, 'default'))
61 for p in ['python','shared']:
62 link_target = os.path.join(conf.blddir, 'default/' + p)
63 if not os.path.lexists(link_target):
64 os.symlink('../' + p, link_target)
66 # get perl to put the blib files in the build directory
67 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
68 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
69 mkdir_p(blib_bld + '/man1')
70 mkdir_p(blib_bld + '/man3')
71 if os.path.islink(blib_src):
73 elif os.path.exists(blib_src):
74 shutil.rmtree(blib_src)
77 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
78 '''add an init_function to the list for a subsystem'''
79 if init_function is None:
81 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
82 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
83 if not subsystem in cache:
85 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
86 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
90 #################################################################
91 def SAMBA_LIBRARY(bld, libname, source,
100 external_library=False,
110 target_type='LIBRARY',
111 bundled_extension=True,
117 '''define a Samba library'''
120 SET_TARGET_TYPE(bld, libname, 'DISABLED')
123 source = bld.EXPAND_VARIABLES(source, vars=vars)
125 # remember empty libraries, so we can strip the dependencies
126 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
127 SET_TARGET_TYPE(bld, libname, 'EMPTY')
130 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
133 obj_target = libname + '.objlist'
135 # first create a target for building the object files for this library
136 # by separating in this way, we avoid recompiling the C files
137 # separately for the install library and the build library
138 bld.SAMBA_SUBSYSTEM(obj_target,
141 public_deps = public_deps,
143 public_headers = public_headers,
144 header_path = header_path,
147 autoproto = autoproto,
148 depends_on = depends_on,
149 needs_python = needs_python,
150 hide_symbols = hide_symbols,
151 local_include = local_include)
153 if libname == obj_target:
156 if not SET_TARGET_TYPE(bld, libname, target_type):
159 # the library itself will depend on that object target
160 deps += ' ' + public_deps
162 deps.append(obj_target)
164 if target_type == 'PYTHON' or realname:
165 bundled_name = libname
167 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
169 features = 'cc cshlib symlink_lib install_lib'
170 if target_type == 'PYTHON':
173 features += ' pyembed'
175 features += ' abi_check'
178 abi_file = os.path.join(bld.curdir, abi_file)
180 bld.SET_BUILD_GROUP(group)
184 target = bundled_name,
185 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
186 depends_on = depends_on,
188 samba_includes = includes,
189 local_include = local_include,
192 samba_inst_path = install_path,
194 samba_realname = realname,
195 samba_install = install,
197 abi_match = abi_match
200 if realname and not link_name:
201 link_name = 'shared/%s' % realname
204 t.link_name = link_name
206 if pc_files is not None:
207 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
209 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
212 #################################################################
213 def SAMBA_BINARY(bld, binname, source,
223 use_global_deps=True,
233 '''define a Samba binary'''
235 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
238 features = 'cc cprogram symlink_bin install_bin'
240 features += ' pyembed'
242 obj_target = binname + '.objlist'
244 source = bld.EXPAND_VARIABLES(source, vars=vars)
245 source = unique_list(TO_LIST(source))
247 # first create a target for building the object files for this binary
248 # by separating in this way, we avoid recompiling the C files
249 # separately for the install binary and the build binary
250 bld.SAMBA_SUBSYSTEM(obj_target,
256 autoproto = autoproto,
257 subsystem_name = subsystem_name,
258 needs_python = needs_python,
259 local_include = local_include,
260 use_hostcc = use_hostcc,
261 use_global_deps= use_global_deps)
263 bld.SET_BUILD_GROUP(group)
265 # the binary itself will depend on that object target
267 deps.append(obj_target)
273 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
275 samba_includes = includes,
276 local_include = local_include,
277 samba_modules = modules,
279 samba_subsystem= subsystem_name,
281 samba_inst_path= install_path,
282 samba_install = install
285 # setup the subsystem_name as an alias for the real
286 # binary name, so it can be found when expanding
287 # subsystem dependencies
288 if subsystem_name is not None:
289 bld.TARGET_ALIAS(subsystem_name, binname)
291 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
294 #################################################################
295 def SAMBA_MODULE(bld, modname, source,
301 autoproto_extra_source='',
304 internal_module=True,
308 '''define a Samba module.'''
310 # we add the init function regardless of whether the module
311 # is enabled or not, as we need to generate a null list if
313 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
315 if internal_module or BUILTIN_LIBRARY(bld, modname):
316 # treat internal modules as subsystems for now
317 SAMBA_SUBSYSTEM(bld, modname, source,
321 autoproto_extra_source=autoproto_extra_source,
323 local_include=local_include,
328 SET_TARGET_TYPE(bld, modname, 'DISABLED')
331 source = bld.EXPAND_VARIABLES(source, vars=vars)
332 source = unique_list(TO_LIST(source))
334 # remember empty modules, so we can strip the dependencies
335 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
336 SET_TARGET_TYPE(bld, modname, 'EMPTY')
339 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
342 if subsystem is not None:
343 deps += ' ' + subsystem
345 bld.SET_BUILD_GROUP('main')
350 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
351 samba_includes = includes,
352 local_include = local_include,
353 samba_deps = TO_LIST(deps)
356 if autoproto is not None:
357 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
359 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
362 #################################################################
363 def SAMBA_SUBSYSTEM(bld, modname, source,
372 init_function_sentinal=None,
373 heimdal_autoproto=None,
374 heimdal_autoproto_options=None,
375 heimdal_autoproto_private=None,
377 autoproto_extra_source='',
380 local_include_first=True,
384 use_global_deps=True,
388 '''define a Samba subsystem'''
391 SET_TARGET_TYPE(bld, modname, 'DISABLED')
394 # remember empty subsystems, so we can strip the dependencies
395 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
396 SET_TARGET_TYPE(bld, modname, 'EMPTY')
399 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
402 source = bld.EXPAND_VARIABLES(source, vars=vars)
403 source = unique_list(TO_LIST(source))
405 deps += ' ' + public_deps
407 bld.SET_BUILD_GROUP(group)
417 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags, hide_symbols=hide_symbols),
418 depends_on = depends_on,
419 samba_deps = TO_LIST(deps),
420 samba_includes = includes,
421 local_include = local_include,
422 local_include_first = local_include_first,
423 samba_subsystem= subsystem_name,
424 samba_use_hostcc = use_hostcc,
425 samba_use_global_deps = use_global_deps
428 if cflags_end is not None:
429 t.samba_cflags.extend(TO_LIST(cflags_end))
431 if heimdal_autoproto is not None:
432 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
433 if heimdal_autoproto_private is not None:
434 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
435 if autoproto is not None:
436 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
437 if public_headers is not None:
438 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
442 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
445 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
446 group='generators', enabled=True,
450 '''A generic source generator target'''
452 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
458 bld.SET_BUILD_GROUP(group)
461 source=bld.EXPAND_VARIABLES(source, vars=vars),
463 shell=isinstance(rule, str),
469 if public_headers is not None:
470 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
472 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
477 def SETUP_BUILD_GROUPS(bld):
478 '''setup build groups used to ensure that the different build
479 phases happen consecutively'''
480 bld.p_ln = bld.srcnode # we do want to see all targets!
481 bld.env['USING_BUILD_GROUPS'] = True
482 bld.add_group('setup')
483 bld.add_group('build_compiler_source')
484 bld.add_group('base_libraries')
485 bld.add_group('generators')
486 bld.add_group('compiler_prototypes')
487 bld.add_group('compiler_libraries')
488 bld.add_group('build_compilers')
489 bld.add_group('build_source')
490 bld.add_group('prototypes')
491 bld.add_group('main')
492 bld.add_group('binaries')
493 bld.add_group('final')
494 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
497 def SET_BUILD_GROUP(bld, group):
498 '''set the current build group'''
499 if not 'USING_BUILD_GROUPS' in bld.env:
502 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
507 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
508 """use timestamps instead of file contents for deps
509 this currently doesn't work"""
510 def h_file(filename):
512 st = os.stat(filename)
513 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
515 m.update(str(st.st_mtime))
516 m.update(str(st.st_size))
519 Utils.h_file = h_file
523 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
524 shell=True, color='PINK', ext_in='.bin')
527 @feature('copy_script')
528 @before('apply_link')
529 def copy_script(self):
530 tsk = self.create_task('copy_script', self.allnodes[0])
531 tsk.env.TARGET = self.target
533 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
534 '''used to copy scripts from the source tree into the build directory
535 for use by selftest'''
537 source = bld.path.ant_glob(pattern)
539 bld.SET_BUILD_GROUP('build_source')
540 for s in TO_LIST(source):
542 if installname != None:
544 target = os.path.join(installdir, iname)
545 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
547 t = bld(features='copy_script',
552 t.env.LINK_TARGET = target
554 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
557 def install_file(bld, destdir, file, chmod=O644, flat=False,
558 python_fixup=False, destname=None, base_name=None):
560 destdir = bld.EXPAND_VARIABLES(destdir)
564 destname = os.path.basename(destname)
565 dest = os.path.join(destdir, destname)
567 # fixup the python path it will use to find Samba modules
568 inst_file = file + '.inst'
569 bld.SAMBA_GENERATOR('python_%s' % destname,
570 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
575 file = os.path.join(base_name, file)
576 bld.install_as(dest, file, chmod=chmod)
579 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
580 python_fixup=False, destname=None, base_name=None):
581 '''install a set of files'''
582 for f in TO_LIST(files):
583 install_file(bld, destdir, f, chmod=chmod, flat=flat,
584 python_fixup=python_fixup, destname=destname,
586 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
589 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
590 python_fixup=False, exclude=None, trim_path=None):
591 '''install a set of files matching a wildcard pattern'''
592 files=TO_LIST(bld.path.ant_glob(pattern))
596 files2.append(os_path_relpath(f, trim_path))
601 if fnmatch.fnmatch(f, exclude):
603 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
604 python_fixup=python_fixup, base_name=trim_path)
605 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
608 def INSTALL_DIRS(bld, destdir, dirs):
609 '''install a set of directories'''
610 destdir = bld.EXPAND_VARIABLES(destdir)
611 dirs = bld.EXPAND_VARIABLES(dirs)
612 for d in TO_LIST(dirs):
613 bld.install_dir(os.path.join(destdir, d))
614 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
617 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
618 '''install some headers
620 header_path may either be a string that is added to the INCLUDEDIR,
621 or it can be a dictionary of wildcard patterns which map to destination
622 directories relative to INCLUDEDIR
624 dest = '${INCLUDEDIR}'
625 if isinstance(header_path, str):
626 dest += '/' + header_path
627 for h in TO_LIST(public_headers):
629 if isinstance(header_path, list):
630 for (p1, dir) in header_path:
634 if fnmatch.fnmatch(h, p2):
636 hdest = os.path.join(hdest, dir)
639 if found_match: break
640 if h.find(':') != -1:
642 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
644 INSTALL_FILES(bld, hdest, h, flat=True)
645 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
648 def subst_at_vars(task):
649 '''substiture @VAR@ style variables in a file'''
650 src = task.inputs[0].srcpath(task.env)
651 tgt = task.outputs[0].bldpath(task.env)
657 a = re.split('(@\w+@)', s)
659 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
661 if re.match('@\w+@', v):
663 if not vname in task.env and vname.upper() in task.env:
664 vname = vname.upper()
665 if not vname in task.env:
666 Logs.error("Unknown substitution %s in %s" % (v, task.name))
668 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
669 # now we back substitute the allowed pc vars
670 for (b, m) in back_sub:
675 contents = ''.join(out)
677 s = f.write(contents)
683 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
684 '''install some pkg_config pc files'''
685 dest = '${PKGCONFIGDIR}'
686 dest = bld.EXPAND_VARIABLES(dest)
687 for f in TO_LIST(pc_files):
688 base=os.path.basename(f)
689 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
694 t.env.PACKAGE_VERSION = vnum
695 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
696 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
700 #############################################################
701 # give a nicer display when building different types of files
702 def progress_display(self, msg, fname):
703 col1 = Logs.colors(self.color)
704 col2 = Logs.colors.NORMAL
705 total = self.position[1]
707 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
708 return fs % (self.position[0], self.position[1], col1, fname, col2)
710 def link_display(self):
711 if Options.options.progress_bar != 0:
712 return Task.Task.old_display(self)
713 fname = self.outputs[0].bldpath(self.env)
714 return progress_display(self, 'Linking', fname)
715 Task.TaskBase.classes['cc_link'].display = link_display
717 def samba_display(self):
718 if Options.options.progress_bar != 0:
719 return Task.Task.old_display(self)
721 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
722 if self.name in targets:
723 target_type = targets[self.name]
724 type_map = { 'GENERATOR' : 'Generating',
725 'PROTOTYPE' : 'Generating'
727 if target_type in type_map:
728 return progress_display(self, type_map[target_type], self.name)
730 fname = self.inputs[0].bldpath(self.env)
731 if fname[0:3] == '../':
733 ext_loc = fname.rfind('.')
735 return Task.Task.old_display(self)
736 ext = fname[ext_loc:]
738 ext_map = { '.idl' : 'Compiling IDL',
739 '.et' : 'Compiling ERRTABLE',
740 '.asn1': 'Compiling ASN1',
743 return progress_display(self, ext_map[ext], fname)
744 return Task.Task.old_display(self)
746 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
747 Task.TaskBase.classes['Task'].display = samba_display