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,
234 '''define a Samba binary'''
237 SET_TARGET_TYPE(bld, binname, 'DISABLED')
240 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
243 features = 'cc cprogram symlink_bin install_bin'
245 features += ' pyembed'
247 obj_target = binname + '.objlist'
249 source = bld.EXPAND_VARIABLES(source, vars=vars)
250 source = unique_list(TO_LIST(source))
252 # first create a target for building the object files for this binary
253 # by separating in this way, we avoid recompiling the C files
254 # separately for the install binary and the build binary
255 bld.SAMBA_SUBSYSTEM(obj_target,
261 autoproto = autoproto,
262 subsystem_name = subsystem_name,
263 needs_python = needs_python,
264 local_include = local_include,
265 use_hostcc = use_hostcc,
266 use_global_deps= use_global_deps)
268 bld.SET_BUILD_GROUP(group)
270 # the binary itself will depend on that object target
272 deps.append(obj_target)
278 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
280 samba_includes = includes,
281 local_include = local_include,
282 samba_modules = modules,
284 samba_subsystem= subsystem_name,
286 samba_inst_path= install_path,
287 samba_install = install
290 # setup the subsystem_name as an alias for the real
291 # binary name, so it can be found when expanding
292 # subsystem dependencies
293 if subsystem_name is not None:
294 bld.TARGET_ALIAS(subsystem_name, binname)
296 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
299 #################################################################
300 def SAMBA_MODULE(bld, modname, source,
306 autoproto_extra_source='',
309 internal_module=True,
313 '''define a Samba module.'''
315 # we add the init function regardless of whether the module
316 # is enabled or not, as we need to generate a null list if
318 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
320 if internal_module or BUILTIN_LIBRARY(bld, modname):
321 # treat internal modules as subsystems for now
322 SAMBA_SUBSYSTEM(bld, modname, source,
326 autoproto_extra_source=autoproto_extra_source,
328 local_include=local_include,
333 SET_TARGET_TYPE(bld, modname, 'DISABLED')
336 source = bld.EXPAND_VARIABLES(source, vars=vars)
337 source = unique_list(TO_LIST(source))
339 # remember empty modules, so we can strip the dependencies
340 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
341 SET_TARGET_TYPE(bld, modname, 'EMPTY')
344 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
347 if subsystem is not None:
348 deps += ' ' + subsystem
350 bld.SET_BUILD_GROUP('main')
355 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
356 samba_includes = includes,
357 local_include = local_include,
358 samba_deps = TO_LIST(deps)
361 if autoproto is not None:
362 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
364 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
367 #################################################################
368 def SAMBA_SUBSYSTEM(bld, modname, source,
377 init_function_sentinal=None,
378 heimdal_autoproto=None,
379 heimdal_autoproto_options=None,
380 heimdal_autoproto_private=None,
382 autoproto_extra_source='',
385 local_include_first=True,
389 use_global_deps=True,
393 '''define a Samba subsystem'''
396 SET_TARGET_TYPE(bld, modname, 'DISABLED')
399 # remember empty subsystems, so we can strip the dependencies
400 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
401 SET_TARGET_TYPE(bld, modname, 'EMPTY')
404 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
407 source = bld.EXPAND_VARIABLES(source, vars=vars)
408 source = unique_list(TO_LIST(source))
410 deps += ' ' + public_deps
412 bld.SET_BUILD_GROUP(group)
422 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags, hide_symbols=hide_symbols),
423 depends_on = depends_on,
424 samba_deps = TO_LIST(deps),
425 samba_includes = includes,
426 local_include = local_include,
427 local_include_first = local_include_first,
428 samba_subsystem= subsystem_name,
429 samba_use_hostcc = use_hostcc,
430 samba_use_global_deps = use_global_deps
433 if cflags_end is not None:
434 t.samba_cflags.extend(TO_LIST(cflags_end))
436 if heimdal_autoproto is not None:
437 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
438 if heimdal_autoproto_private is not None:
439 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
440 if autoproto is not None:
441 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
442 if public_headers is not None:
443 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
447 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
450 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
451 group='generators', enabled=True,
455 '''A generic source generator target'''
457 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
463 bld.SET_BUILD_GROUP(group)
466 source=bld.EXPAND_VARIABLES(source, vars=vars),
468 shell=isinstance(rule, str),
474 if public_headers is not None:
475 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
477 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
482 def SETUP_BUILD_GROUPS(bld):
483 '''setup build groups used to ensure that the different build
484 phases happen consecutively'''
485 bld.p_ln = bld.srcnode # we do want to see all targets!
486 bld.env['USING_BUILD_GROUPS'] = True
487 bld.add_group('setup')
488 bld.add_group('build_compiler_source')
489 bld.add_group('base_libraries')
490 bld.add_group('generators')
491 bld.add_group('compiler_prototypes')
492 bld.add_group('compiler_libraries')
493 bld.add_group('build_compilers')
494 bld.add_group('build_source')
495 bld.add_group('prototypes')
496 bld.add_group('main')
497 bld.add_group('binaries')
498 bld.add_group('final')
499 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
502 def SET_BUILD_GROUP(bld, group):
503 '''set the current build group'''
504 if not 'USING_BUILD_GROUPS' in bld.env:
507 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
512 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
513 """use timestamps instead of file contents for deps
514 this currently doesn't work"""
515 def h_file(filename):
517 st = os.stat(filename)
518 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
520 m.update(str(st.st_mtime))
521 m.update(str(st.st_size))
524 Utils.h_file = h_file
528 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
529 shell=True, color='PINK', ext_in='.bin')
532 @feature('copy_script')
533 @before('apply_link')
534 def copy_script(self):
535 tsk = self.create_task('copy_script', self.allnodes[0])
536 tsk.env.TARGET = self.target
538 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
539 '''used to copy scripts from the source tree into the build directory
540 for use by selftest'''
542 source = bld.path.ant_glob(pattern)
544 bld.SET_BUILD_GROUP('build_source')
545 for s in TO_LIST(source):
547 if installname != None:
549 target = os.path.join(installdir, iname)
550 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
552 t = bld(features='copy_script',
557 t.env.LINK_TARGET = target
559 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
562 def install_file(bld, destdir, file, chmod=O644, flat=False,
563 python_fixup=False, destname=None, base_name=None):
565 destdir = bld.EXPAND_VARIABLES(destdir)
569 destname = os.path.basename(destname)
570 dest = os.path.join(destdir, destname)
572 # fixup the python path it will use to find Samba modules
573 inst_file = file + '.inst'
574 bld.SAMBA_GENERATOR('python_%s' % destname,
575 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
580 file = os.path.join(base_name, file)
581 bld.install_as(dest, file, chmod=chmod)
584 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
585 python_fixup=False, destname=None, base_name=None):
586 '''install a set of files'''
587 for f in TO_LIST(files):
588 install_file(bld, destdir, f, chmod=chmod, flat=flat,
589 python_fixup=python_fixup, destname=destname,
591 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
594 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
595 python_fixup=False, exclude=None, trim_path=None):
596 '''install a set of files matching a wildcard pattern'''
597 files=TO_LIST(bld.path.ant_glob(pattern))
601 files2.append(os_path_relpath(f, trim_path))
606 if fnmatch.fnmatch(f, exclude):
608 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
609 python_fixup=python_fixup, base_name=trim_path)
610 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
613 def INSTALL_DIRS(bld, destdir, dirs):
614 '''install a set of directories'''
615 destdir = bld.EXPAND_VARIABLES(destdir)
616 dirs = bld.EXPAND_VARIABLES(dirs)
617 for d in TO_LIST(dirs):
618 bld.install_dir(os.path.join(destdir, d))
619 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
622 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
623 '''install some headers
625 header_path may either be a string that is added to the INCLUDEDIR,
626 or it can be a dictionary of wildcard patterns which map to destination
627 directories relative to INCLUDEDIR
629 dest = '${INCLUDEDIR}'
630 if isinstance(header_path, str):
631 dest += '/' + header_path
632 for h in TO_LIST(public_headers):
634 if isinstance(header_path, list):
635 for (p1, dir) in header_path:
639 if fnmatch.fnmatch(h, p2):
641 hdest = os.path.join(hdest, dir)
644 if found_match: break
645 if h.find(':') != -1:
647 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
649 INSTALL_FILES(bld, hdest, h, flat=True)
650 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
653 def subst_at_vars(task):
654 '''substiture @VAR@ style variables in a file'''
655 src = task.inputs[0].srcpath(task.env)
656 tgt = task.outputs[0].bldpath(task.env)
662 a = re.split('(@\w+@)', s)
664 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
666 if re.match('@\w+@', v):
668 if not vname in task.env and vname.upper() in task.env:
669 vname = vname.upper()
670 if not vname in task.env:
671 Logs.error("Unknown substitution %s in %s" % (v, task.name))
673 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
674 # now we back substitute the allowed pc vars
675 for (b, m) in back_sub:
680 contents = ''.join(out)
682 s = f.write(contents)
688 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
689 '''install some pkg_config pc files'''
690 dest = '${PKGCONFIGDIR}'
691 dest = bld.EXPAND_VARIABLES(dest)
692 for f in TO_LIST(pc_files):
693 base=os.path.basename(f)
694 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
699 t.env.PACKAGE_VERSION = vnum
700 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
701 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
705 #############################################################
706 # give a nicer display when building different types of files
707 def progress_display(self, msg, fname):
708 col1 = Logs.colors(self.color)
709 col2 = Logs.colors.NORMAL
710 total = self.position[1]
712 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
713 return fs % (self.position[0], self.position[1], col1, fname, col2)
715 def link_display(self):
716 if Options.options.progress_bar != 0:
717 return Task.Task.old_display(self)
718 fname = self.outputs[0].bldpath(self.env)
719 return progress_display(self, 'Linking', fname)
720 Task.TaskBase.classes['cc_link'].display = link_display
722 def samba_display(self):
723 if Options.options.progress_bar != 0:
724 return Task.Task.old_display(self)
726 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
727 if self.name in targets:
728 target_type = targets[self.name]
729 type_map = { 'GENERATOR' : 'Generating',
730 'PROTOTYPE' : 'Generating'
732 if target_type in type_map:
733 return progress_display(self, type_map[target_type], self.name)
735 fname = self.inputs[0].bldpath(self.env)
736 if fname[0:3] == '../':
738 ext_loc = fname.rfind('.')
740 return Task.Task.old_display(self)
741 ext = fname[ext_loc:]
743 ext_map = { '.idl' : 'Compiling IDL',
744 '.et' : 'Compiling ERRTABLE',
745 '.asn1': 'Compiling ASN1',
748 return progress_display(self, ext_map[ext], fname)
749 return Task.Task.old_display(self)
751 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
752 Task.TaskBase.classes['Task'].display = samba_display