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 == []):
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':
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
201 t.link_name = link_name
203 if pc_files is not None:
204 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
206 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
209 #################################################################
210 def SAMBA_BINARY(bld, binname, source,
220 use_global_deps=True,
230 '''define a Samba binary'''
232 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
235 features = 'cc cprogram symlink_bin install_bin'
237 features += ' pyembed'
239 obj_target = binname + '.objlist'
241 source = bld.EXPAND_VARIABLES(source, vars=vars)
242 source = unique_list(TO_LIST(source))
244 # first create a target for building the object files for this binary
245 # by separating in this way, we avoid recompiling the C files
246 # separately for the install binary and the build binary
247 bld.SAMBA_SUBSYSTEM(obj_target,
253 autoproto = autoproto,
254 subsystem_name = subsystem_name,
255 needs_python = needs_python,
256 local_include = local_include,
257 use_hostcc = use_hostcc,
258 use_global_deps= use_global_deps)
260 bld.SET_BUILD_GROUP(group)
262 # the binary itself will depend on that object target
264 deps.append(obj_target)
270 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
272 samba_includes = includes,
273 local_include = local_include,
274 samba_modules = modules,
276 samba_subsystem= subsystem_name,
278 samba_inst_path= install_path,
279 samba_install = install
282 # setup the subsystem_name as an alias for the real
283 # binary name, so it can be found when expanding
284 # subsystem dependencies
285 if subsystem_name is not None:
286 bld.TARGET_ALIAS(subsystem_name, binname)
288 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
291 #################################################################
292 def SAMBA_MODULE(bld, modname, source,
298 autoproto_extra_source='',
301 internal_module=True,
305 '''define a Samba module.'''
307 # we add the init function regardless of whether the module
308 # is enabled or not, as we need to generate a null list if
310 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
312 if internal_module or BUILTIN_LIBRARY(bld, modname):
313 # treat internal modules as subsystems for now
314 SAMBA_SUBSYSTEM(bld, modname, source,
318 autoproto_extra_source=autoproto_extra_source,
320 local_include=local_include,
325 SET_TARGET_TYPE(bld, modname, 'DISABLED')
328 source = bld.EXPAND_VARIABLES(source, vars=vars)
329 source = unique_list(TO_LIST(source))
331 # remember empty modules, so we can strip the dependencies
332 if (source == '') or (source == []):
333 SET_TARGET_TYPE(bld, modname, 'EMPTY')
336 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
339 if subsystem is not None:
340 deps += ' ' + subsystem
342 bld.SET_BUILD_GROUP('main')
347 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
348 samba_includes = includes,
349 local_include = local_include,
350 samba_deps = TO_LIST(deps)
353 if autoproto is not None:
354 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
356 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
359 #################################################################
360 def SAMBA_SUBSYSTEM(bld, modname, source,
369 init_function_sentinal=None,
370 heimdal_autoproto=None,
371 heimdal_autoproto_options=None,
372 heimdal_autoproto_private=None,
374 autoproto_extra_source='',
377 local_include_first=True,
381 use_global_deps=True,
385 '''define a Samba subsystem'''
388 SET_TARGET_TYPE(bld, modname, 'DISABLED')
391 # remember empty subsystems, so we can strip the dependencies
392 if (source == '') or (source == []):
393 SET_TARGET_TYPE(bld, modname, 'EMPTY')
396 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
399 source = bld.EXPAND_VARIABLES(source, vars=vars)
400 source = unique_list(TO_LIST(source))
402 deps += ' ' + public_deps
404 bld.SET_BUILD_GROUP(group)
414 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags, hide_symbols=hide_symbols),
415 depends_on = depends_on,
416 samba_deps = TO_LIST(deps),
417 samba_includes = includes,
418 local_include = local_include,
419 local_include_first = local_include_first,
420 samba_subsystem= subsystem_name,
421 samba_use_hostcc = use_hostcc,
422 samba_use_global_deps = use_global_deps
425 if cflags_end is not None:
426 t.samba_cflags.extend(TO_LIST(cflags_end))
428 if heimdal_autoproto is not None:
429 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
430 if heimdal_autoproto_private is not None:
431 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
432 if autoproto is not None:
433 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
434 if public_headers is not None:
435 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
439 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
442 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
443 group='generators', enabled=True,
447 '''A generic source generator target'''
449 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
455 bld.SET_BUILD_GROUP(group)
458 source=bld.EXPAND_VARIABLES(source, vars=vars),
460 shell=isinstance(rule, str),
466 if public_headers is not None:
467 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
469 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
474 def SETUP_BUILD_GROUPS(bld):
475 '''setup build groups used to ensure that the different build
476 phases happen consecutively'''
477 bld.p_ln = bld.srcnode # we do want to see all targets!
478 bld.env['USING_BUILD_GROUPS'] = True
479 bld.add_group('setup')
480 bld.add_group('build_compiler_source')
481 bld.add_group('base_libraries')
482 bld.add_group('generators')
483 bld.add_group('compiler_prototypes')
484 bld.add_group('compiler_libraries')
485 bld.add_group('build_compilers')
486 bld.add_group('build_source')
487 bld.add_group('prototypes')
488 bld.add_group('main')
489 bld.add_group('binaries')
490 bld.add_group('final')
491 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
494 def SET_BUILD_GROUP(bld, group):
495 '''set the current build group'''
496 if not 'USING_BUILD_GROUPS' in bld.env:
499 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
504 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
505 """use timestamps instead of file contents for deps
506 this currently doesn't work"""
507 def h_file(filename):
509 st = os.stat(filename)
510 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
512 m.update(str(st.st_mtime))
513 m.update(str(st.st_size))
516 Utils.h_file = h_file
520 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
521 shell=True, color='PINK', ext_in='.bin')
524 @feature('copy_script')
525 @before('apply_link')
526 def copy_script(self):
527 tsk = self.create_task('copy_script', self.allnodes[0])
528 tsk.env.TARGET = self.target
530 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
531 '''used to copy scripts from the source tree into the build directory
532 for use by selftest'''
534 source = bld.path.ant_glob(pattern)
536 bld.SET_BUILD_GROUP('build_source')
537 for s in TO_LIST(source):
539 if installname != None:
541 target = os.path.join(installdir, iname)
542 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
544 t = bld(features='copy_script',
549 t.env.LINK_TARGET = target
551 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
554 def install_file(bld, destdir, file, chmod=O644, flat=False,
555 python_fixup=False, destname=None, base_name=None):
557 destdir = bld.EXPAND_VARIABLES(destdir)
561 destname = os.path.basename(destname)
562 dest = os.path.join(destdir, destname)
564 # fixup the python path it will use to find Samba modules
565 inst_file = file + '.inst'
566 bld.SAMBA_GENERATOR('python_%s' % destname,
567 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
572 file = os.path.join(base_name, file)
573 bld.install_as(dest, file, chmod=chmod)
576 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
577 python_fixup=False, destname=None, base_name=None):
578 '''install a set of files'''
579 for f in TO_LIST(files):
580 install_file(bld, destdir, f, chmod=chmod, flat=flat,
581 python_fixup=python_fixup, destname=destname,
583 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
586 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
587 python_fixup=False, exclude=None, trim_path=None):
588 '''install a set of files matching a wildcard pattern'''
589 files=TO_LIST(bld.path.ant_glob(pattern))
593 files2.append(os_path_relpath(f, trim_path))
598 if fnmatch.fnmatch(f, exclude):
600 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
601 python_fixup=python_fixup, base_name=trim_path)
602 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
605 def INSTALL_DIRS(bld, destdir, dirs):
606 '''install a set of directories'''
607 destdir = bld.EXPAND_VARIABLES(destdir)
608 dirs = bld.EXPAND_VARIABLES(dirs)
609 for d in TO_LIST(dirs):
610 bld.install_dir(os.path.join(destdir, d))
611 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
614 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
615 '''install some headers
617 header_path may either be a string that is added to the INCLUDEDIR,
618 or it can be a dictionary of wildcard patterns which map to destination
619 directories relative to INCLUDEDIR
621 dest = '${INCLUDEDIR}'
622 if isinstance(header_path, str):
623 dest += '/' + header_path
624 for h in TO_LIST(public_headers):
626 if isinstance(header_path, list):
627 for (p1, dir) in header_path:
631 if fnmatch.fnmatch(h, p2):
633 hdest = os.path.join(hdest, dir)
636 if found_match: break
637 if h.find(':') != -1:
639 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
641 INSTALL_FILES(bld, hdest, h, flat=True)
642 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
645 def subst_at_vars(task):
646 '''substiture @VAR@ style variables in a file'''
647 src = task.inputs[0].srcpath(task.env)
648 tgt = task.outputs[0].bldpath(task.env)
654 a = re.split('(@\w+@)', s)
656 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
658 if re.match('@\w+@', v):
660 if not vname in task.env and vname.upper() in task.env:
661 vname = vname.upper()
662 if not vname in task.env:
663 Logs.error("Unknown substitution %s in %s" % (v, task.name))
665 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
666 # now we back substitute the allowed pc vars
667 for (b, m) in back_sub:
672 contents = ''.join(out)
674 s = f.write(contents)
680 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
681 '''install some pkg_config pc files'''
682 dest = '${PKGCONFIGDIR}'
683 dest = bld.EXPAND_VARIABLES(dest)
684 for f in TO_LIST(pc_files):
685 base=os.path.basename(f)
686 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
691 t.env.PACKAGE_VERSION = vnum
692 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
693 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
697 #############################################################
698 # give a nicer display when building different types of files
699 def progress_display(self, msg, fname):
700 col1 = Logs.colors(self.color)
701 col2 = Logs.colors.NORMAL
702 total = self.position[1]
704 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
705 return fs % (self.position[0], self.position[1], col1, fname, col2)
707 def link_display(self):
708 if Options.options.progress_bar != 0:
709 return Task.Task.old_display(self)
710 fname = self.outputs[0].bldpath(self.env)
711 return progress_display(self, 'Linking', fname)
712 Task.TaskBase.classes['cc_link'].display = link_display
714 def samba_display(self):
715 if Options.options.progress_bar != 0:
716 return Task.Task.old_display(self)
718 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
719 if self.name in targets:
720 target_type = targets[self.name]
721 type_map = { 'GENERATOR' : 'Generating',
722 'PROTOTYPE' : 'Generating'
724 if target_type in type_map:
725 return progress_display(self, type_map[target_type], self.name)
727 fname = self.inputs[0].bldpath(self.env)
728 if fname[0:3] == '../':
730 ext_loc = fname.rfind('.')
732 return Task.Task.old_display(self)
733 ext = fname[ext_loc:]
735 ext_map = { '.idl' : 'Compiling IDL',
736 '.et' : 'Compiling ERRTABLE',
737 '.asn1': 'Compiling ASN1',
740 return progress_display(self, ext_map[ext], fname)
741 return Task.Task.old_display(self)
743 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
744 Task.TaskBase.classes['Task'].display = samba_display