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
30 # some systems have broken threading in python
31 if os.environ.get('WAF_NOTHREADS') == '1':
36 os.putenv('PYTHONUNBUFFERED', '1')
39 if Constants.HEXVERSION < 0x105016:
41 Please use the version of waf that comes with Samba, not
42 a system installed version. See http://wiki.samba.org/index.php/Waf
45 Alternatively, please use ./autogen-waf.sh, and then
46 run ./configure and make as usual. That will call the right version of waf.
52 def SAMBA_BUILD_ENV(conf):
53 '''create the samba build environment'''
54 conf.env.BUILD_DIRECTORY = conf.blddir
55 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
56 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
57 # this allows all of the bin/shared and bin/python targets
58 # to be expressed in terms of build directory paths
59 for p in ['python','shared']:
60 link_target = os.path.join(conf.blddir, 'default/' + p)
61 if not os.path.lexists(link_target):
62 os.symlink('../' + p, link_target)
64 # get perl to put the blib files in the build directory
65 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
66 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
67 mkdir_p(blib_bld + '/man1')
68 mkdir_p(blib_bld + '/man3')
69 if os.path.islink(blib_src):
71 elif os.path.exists(blib_src):
72 shutil.rmtree(blib_src)
75 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
76 '''add an init_function to the list for a subsystem'''
77 if init_function is None:
79 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
80 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
81 if not subsystem in cache:
83 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
84 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
88 #################################################################
89 def SAMBA_LIBRARY(bld, libname, source,
98 external_library=False,
108 target_type='LIBRARY',
109 bundled_extension=True,
112 '''define a Samba library'''
115 SET_TARGET_TYPE(bld, libname, 'DISABLED')
118 source = bld.EXPAND_VARIABLES(source, vars=vars)
120 # remember empty libraries, so we can strip the dependencies
121 if (source == '') or (source == []):
122 SET_TARGET_TYPE(bld, libname, 'EMPTY')
125 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
128 obj_target = libname + '.objlist'
130 # first create a target for building the object files for this library
131 # by separating in this way, we avoid recompiling the C files
132 # separately for the install library and the build library
133 bld.SAMBA_SUBSYSTEM(obj_target,
136 public_deps = public_deps,
138 public_headers = public_headers,
139 header_path = header_path,
142 autoproto = autoproto,
143 depends_on = depends_on,
144 needs_python = needs_python,
145 local_include = local_include)
147 if libname == obj_target:
150 if not SET_TARGET_TYPE(bld, libname, target_type):
153 # the library itself will depend on that object target
154 deps += ' ' + public_deps
156 deps.append(obj_target)
158 if target_type == 'PYTHON':
159 bundled_name = libname
161 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
163 features = 'cc cshlib symlink_lib install_lib'
164 if target_type == 'PYTHON':
167 features += ' pyembed'
169 bld.SET_BUILD_GROUP(group)
173 target = bundled_name,
174 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
175 depends_on = depends_on,
177 samba_includes = includes,
178 local_include = local_include,
181 samba_inst_path = install_path,
183 samba_realname = realname,
184 samba_install = install
188 t.link_name = link_name
190 if pc_files is not None:
191 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
193 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
196 #################################################################
197 def SAMBA_BINARY(bld, binname, source,
208 use_global_deps=True,
218 '''define a Samba binary'''
220 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
223 features = 'cc cprogram symlink_bin install_bin'
225 features += ' pyembed'
227 obj_target = binname + '.objlist'
229 source = bld.EXPAND_VARIABLES(source, vars=vars)
231 # first create a target for building the object files for this binary
232 # by separating in this way, we avoid recompiling the C files
233 # separately for the install binary and the build binary
234 bld.SAMBA_SUBSYSTEM(obj_target,
240 autoproto = autoproto,
241 subsystem_name = subsystem_name,
242 needs_python = needs_python,
243 local_include = local_include,
244 use_hostcc = use_hostcc,
245 use_global_deps= use_global_deps)
247 bld.SET_BUILD_GROUP(group)
249 # the binary itself will depend on that object target
251 deps.append(obj_target)
257 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
259 samba_includes = includes,
260 local_include = local_include,
261 samba_modules = modules,
263 samba_subsystem= subsystem_name,
265 samba_inst_path= install_path,
266 samba_install = install
269 # setup the subsystem_name as an alias for the real
270 # binary name, so it can be found when expanding
271 # subsystem dependencies
272 if subsystem_name is not None:
273 bld.TARGET_ALIAS(subsystem_name, binname)
275 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
278 #################################################################
279 def SAMBA_MODULE(bld, modname, source,
285 autoproto_extra_source='',
288 internal_module=True,
292 '''define a Samba module.'''
294 # we add the init function regardless of whether the module
295 # is enabled or not, as we need to generate a null list if
297 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
299 if internal_module or BUILTIN_LIBRARY(bld, modname):
300 # treat internal modules as subsystems for now
301 SAMBA_SUBSYSTEM(bld, modname, source,
305 autoproto_extra_source=autoproto_extra_source,
307 local_include=local_include,
312 SET_TARGET_TYPE(bld, modname, 'DISABLED')
315 source = bld.EXPAND_VARIABLES(source, vars=vars)
317 # remember empty modules, so we can strip the dependencies
318 if (source == '') or (source == []):
319 SET_TARGET_TYPE(bld, modname, 'EMPTY')
322 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
325 if subsystem is not None:
326 deps += ' ' + subsystem
328 bld.SET_BUILD_GROUP('main')
333 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
334 samba_includes = includes,
335 local_include = local_include,
336 samba_deps = TO_LIST(deps)
339 if autoproto is not None:
340 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
342 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
345 #################################################################
346 def SAMBA_SUBSYSTEM(bld, modname, source,
355 init_function_sentinal=None,
356 heimdal_autoproto=None,
357 heimdal_autoproto_options=None,
358 heimdal_autoproto_private=None,
360 autoproto_extra_source='',
363 local_include_first=True,
367 use_global_deps=True,
370 '''define a Samba subsystem'''
373 SET_TARGET_TYPE(bld, modname, 'DISABLED')
376 # remember empty subsystems, so we can strip the dependencies
377 if (source == '') or (source == []):
378 SET_TARGET_TYPE(bld, modname, 'EMPTY')
381 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
384 source = bld.EXPAND_VARIABLES(source, vars=vars)
386 deps += ' ' + public_deps
388 bld.SET_BUILD_GROUP(group)
398 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
399 depends_on = depends_on,
400 samba_deps = TO_LIST(deps),
401 samba_includes = includes,
402 local_include = local_include,
403 local_include_first = local_include_first,
404 samba_subsystem= subsystem_name,
405 samba_use_hostcc = use_hostcc,
406 samba_use_global_deps = use_global_deps
409 if cflags_end is not None:
410 t.samba_cflags.extend(TO_LIST(cflags_end))
412 if heimdal_autoproto is not None:
413 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
414 if heimdal_autoproto_private is not None:
415 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
416 if autoproto is not None:
417 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
418 if public_headers is not None:
419 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
423 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
426 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
427 group='generators', enabled=True,
431 '''A generic source generator target'''
433 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
439 bld.SET_BUILD_GROUP(group)
442 source=bld.EXPAND_VARIABLES(source, vars=vars),
444 shell=isinstance(rule, str),
450 if public_headers is not None:
451 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
453 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
458 def SETUP_BUILD_GROUPS(bld):
459 '''setup build groups used to ensure that the different build
460 phases happen consecutively'''
461 bld.p_ln = bld.srcnode # we do want to see all targets!
462 bld.env['USING_BUILD_GROUPS'] = True
463 bld.add_group('setup')
464 bld.add_group('build_compiler_source')
465 bld.add_group('base_libraries')
466 bld.add_group('generators')
467 bld.add_group('compiler_prototypes')
468 bld.add_group('compiler_libraries')
469 bld.add_group('build_compilers')
470 bld.add_group('build_source')
471 bld.add_group('prototypes')
472 bld.add_group('main')
473 bld.add_group('binaries')
474 bld.add_group('final')
475 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
478 def SET_BUILD_GROUP(bld, group):
479 '''set the current build group'''
480 if not 'USING_BUILD_GROUPS' in bld.env:
483 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
488 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
489 """use timestamps instead of file contents for deps
490 this currently doesn't work"""
491 def h_file(filename):
493 st = os.stat(filename)
494 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
496 m.update(str(st.st_mtime))
497 m.update(str(st.st_size))
500 Utils.h_file = h_file
504 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
505 shell=True, color='PINK', ext_in='.bin')
508 @feature('copy_script')
509 @before('apply_link')
510 def copy_script(self):
511 tsk = self.create_task('copy_script', self.allnodes[0])
512 tsk.env.TARGET = self.target
514 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
515 '''used to copy scripts from the source tree into the build directory
516 for use by selftest'''
518 source = bld.path.ant_glob(pattern)
520 bld.SET_BUILD_GROUP('build_source')
521 for s in TO_LIST(source):
523 if installname != None:
525 target = os.path.join(installdir, iname)
526 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
528 t = bld(features='copy_script',
533 t.env.LINK_TARGET = target
535 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
538 def install_file(bld, destdir, file, chmod=O644, flat=False,
539 python_fixup=False, destname=None, base_name=None):
541 destdir = bld.EXPAND_VARIABLES(destdir)
545 destname = os.path.basename(destname)
546 dest = os.path.join(destdir, destname)
548 # fixup the python path it will use to find Samba modules
549 inst_file = file + '.inst'
550 bld.SAMBA_GENERATOR('python_%s' % destname,
551 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
556 file = os.path.join(base_name, file)
557 bld.install_as(dest, file, chmod=chmod)
560 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
561 python_fixup=False, destname=None, base_name=None):
562 '''install a set of files'''
563 for f in TO_LIST(files):
564 install_file(bld, destdir, f, chmod=chmod, flat=flat,
565 python_fixup=python_fixup, destname=destname,
567 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
570 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
571 python_fixup=False, exclude=None, trim_path=None):
572 '''install a set of files matching a wildcard pattern'''
573 files=TO_LIST(bld.path.ant_glob(pattern))
577 files2.append(os_path_relpath(f, trim_path))
582 if fnmatch.fnmatch(f, exclude):
584 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
585 python_fixup=python_fixup, base_name=trim_path)
586 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
589 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
590 '''install some headers
592 header_path may either be a string that is added to the INCLUDEDIR,
593 or it can be a dictionary of wildcard patterns which map to destination
594 directories relative to INCLUDEDIR
596 dest = '${INCLUDEDIR}'
597 if isinstance(header_path, str):
598 dest += '/' + header_path
599 for h in TO_LIST(public_headers):
601 if isinstance(header_path, list):
602 for (p1, dir) in header_path:
606 if fnmatch.fnmatch(h, p2):
608 hdest = os.path.join(hdest, dir)
611 if found_match: break
612 if h.find(':') != -1:
614 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
616 INSTALL_FILES(bld, hdest, h, flat=True)
617 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
620 def subst_at_vars(task):
621 '''substiture @VAR@ style variables in a file'''
622 src = task.inputs[0].srcpath(task.env)
623 tgt = task.outputs[0].bldpath(task.env)
629 a = re.split('(@\w+@)', s)
631 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
633 if re.match('@\w+@', v):
635 if not vname in task.env and vname.upper() in task.env:
636 vname = vname.upper()
637 if not vname in task.env:
638 Logs.error("Unknown substitution %s in %s" % (v, task.name))
640 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
641 # now we back substitute the allowed pc vars
642 for (b, m) in back_sub:
647 contents = ''.join(out)
649 s = f.write(contents)
655 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
656 '''install some pkg_config pc files'''
657 dest = '${PKGCONFIGDIR}'
658 dest = bld.EXPAND_VARIABLES(dest)
659 for f in TO_LIST(pc_files):
660 base=os.path.basename(f)
661 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
666 t.env.PACKAGE_VERSION = vnum
667 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
668 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
672 #############################################################
673 # give a nicer display when building different types of files
674 def progress_display(self, msg, fname):
675 col1 = Logs.colors(self.color)
676 col2 = Logs.colors.NORMAL
677 total = self.position[1]
679 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
680 return fs % (self.position[0], self.position[1], col1, fname, col2)
682 def link_display(self):
683 if Options.options.progress_bar != 0:
684 return Task.Task.old_display(self)
685 fname = self.outputs[0].bldpath(self.env)
686 return progress_display(self, 'Linking', fname)
687 Task.TaskBase.classes['cc_link'].display = link_display
689 def samba_display(self):
690 if Options.options.progress_bar != 0:
691 return Task.Task.old_display(self)
693 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
694 if self.name in targets:
695 target_type = targets[self.name]
696 type_map = { 'GENERATOR' : 'Generating',
697 'PROTOTYPE' : 'Generating'
699 if target_type in type_map:
700 return progress_display(self, type_map[target_type], self.name)
702 fname = self.inputs[0].bldpath(self.env)
703 if fname[0:3] == '../':
705 ext_loc = fname.rfind('.')
707 return Task.Task.old_display(self)
708 ext = fname[ext_loc:]
710 ext_map = { '.idl' : 'Compiling IDL',
711 '.et' : 'Compiling ERRTABLE',
712 '.asn1': 'Compiling ASN1',
715 return progress_display(self, ext_map[ext], fname)
716 return Task.Task.old_display(self)
718 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
719 Task.TaskBase.classes['Task'].display = samba_display