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
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
28 # some systems have broken threading in python
29 if os.environ.get('WAF_NOTHREADS') == '1':
34 os.putenv('PYTHONUNBUFFERED', '1')
37 def SAMBA_BUILD_ENV(conf):
38 '''create the samba build environment'''
39 conf.env['BUILD_DIRECTORY'] = conf.blddir
40 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
41 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
42 # this allows all of the bin/shared and bin/python targets
43 # to be expressed in terms of build directory paths
44 for p in ['python','shared']:
45 link_target = os.path.join(conf.blddir, 'default/' + p)
46 if not os.path.lexists(link_target):
47 os.symlink('../' + p, link_target)
49 # get perl to put the blib files in the build directory
50 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
51 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
52 mkdir_p(blib_bld + '/man1')
53 mkdir_p(blib_bld + '/man3')
54 if os.path.islink(blib_src):
56 elif os.path.exists(blib_src):
57 shutil.rmtree(blib_src)
60 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
61 '''add an init_function to the list for a subsystem'''
62 if init_function is None:
64 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
65 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
66 if not subsystem in cache:
68 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
69 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
73 #################################################################
74 def SAMBA_LIBRARY(bld, libname, source,
83 external_library=False,
93 target_type='LIBRARY',
94 bundled_extension=True,
97 '''define a Samba library'''
100 SET_TARGET_TYPE(bld, libname, 'DISABLED')
103 source = bld.EXPAND_VARIABLES(source, vars=vars)
105 # remember empty libraries, so we can strip the dependencies
106 if (source == '') or (source == []):
107 SET_TARGET_TYPE(bld, libname, 'EMPTY')
110 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
113 obj_target = libname + '.objlist'
115 # first create a target for building the object files for this library
116 # by separating in this way, we avoid recompiling the C files
117 # separately for the install library and the build library
118 bld.SAMBA_SUBSYSTEM(obj_target,
121 public_deps = public_deps,
123 public_headers = public_headers,
124 header_path = header_path,
127 autoproto = autoproto,
128 depends_on = depends_on,
129 needs_python = needs_python,
130 local_include = local_include)
132 if libname == obj_target:
135 if not SET_TARGET_TYPE(bld, libname, target_type):
138 # the library itself will depend on that object target
139 deps += ' ' + public_deps
141 deps.append(obj_target)
143 if target_type == 'PYTHON':
144 bundled_name = libname
146 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
148 features = 'cc cshlib symlink_lib install_lib'
149 if target_type == 'PYTHON':
152 features += ' pyembed'
154 bld.SET_BUILD_GROUP(group)
158 target = bundled_name,
159 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
160 depends_on = depends_on,
162 samba_includes = includes,
163 local_include = local_include,
166 samba_inst_path = install_path,
168 samba_realname = realname,
169 samba_install = install
173 t.link_name = link_name
175 if autoproto is not None:
176 bld.SAMBA_AUTOPROTO(autoproto, source)
178 if public_headers is not None:
179 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
181 if pc_files is not None:
182 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
184 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
187 #################################################################
188 def SAMBA_BINARY(bld, binname, source,
208 '''define a Samba binary'''
210 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
213 features = 'cc cprogram symlink_bin install_bin'
215 features += ' pyembed'
217 obj_target = binname + '.objlist'
219 source = bld.EXPAND_VARIABLES(source, vars=vars)
221 # first create a target for building the object files for this binary
222 # by separating in this way, we avoid recompiling the C files
223 # separately for the install binary and the build binary
224 bld.SAMBA_SUBSYSTEM(obj_target,
230 autoproto = autoproto,
231 subsystem_name = subsystem_name,
232 needs_python = needs_python,
233 local_include = local_include)
235 bld.SET_BUILD_GROUP(group)
237 # the binary itself will depend on that object target
239 deps.append(obj_target)
245 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
247 samba_includes = includes,
248 local_include = local_include,
249 samba_modules = modules,
251 samba_subsystem= subsystem_name,
253 samba_inst_path= install_path,
254 samba_install = install
257 # setup the subsystem_name as an alias for the real
258 # binary name, so it can be found when expanding
259 # subsystem dependencies
260 if subsystem_name is not None:
261 bld.TARGET_ALIAS(subsystem_name, binname)
263 if autoproto is not None:
264 bld.SAMBA_AUTOPROTO(autoproto, source)
265 if public_headers is not None:
266 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
267 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
270 #################################################################
271 def SAMBA_MODULE(bld, modname, source,
277 autoproto_extra_source='',
280 internal_module=True,
284 '''define a Samba module.'''
286 # we add the init function regardless of whether the module
287 # is enabled or not, as we need to generate a null list if
289 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
291 if internal_module or BUILTIN_LIBRARY(bld, modname):
292 # treat internal modules as subsystems for now
293 SAMBA_SUBSYSTEM(bld, modname, source,
297 autoproto_extra_source=autoproto_extra_source,
299 local_include=local_include,
304 SET_TARGET_TYPE(bld, modname, 'DISABLED')
307 source = bld.EXPAND_VARIABLES(source, vars=vars)
309 # remember empty modules, so we can strip the dependencies
310 if (source == '') or (source == []):
311 SET_TARGET_TYPE(bld, modname, 'EMPTY')
314 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
317 if subsystem is not None:
318 deps += ' ' + subsystem
320 bld.SET_BUILD_GROUP('main')
325 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
326 samba_includes = includes,
327 local_include = local_include,
328 samba_deps = TO_LIST(deps)
331 if autoproto is not None:
332 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
334 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
337 #################################################################
338 def SAMBA_SUBSYSTEM(bld, modname, source,
347 init_function_sentinal=None,
348 heimdal_autoproto=None,
349 heimdal_autoproto_options=None,
350 heimdal_autoproto_private=None,
352 autoproto_extra_source='',
355 local_include_first=True,
360 '''define a Samba subsystem'''
363 SET_TARGET_TYPE(bld, modname, 'DISABLED')
366 # remember empty subsystems, so we can strip the dependencies
367 if (source == '') or (source == []):
368 SET_TARGET_TYPE(bld, modname, 'EMPTY')
371 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
374 source = bld.EXPAND_VARIABLES(source, vars=vars)
376 deps += ' ' + public_deps
378 bld.SET_BUILD_GROUP(group)
388 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
389 depends_on = depends_on,
390 samba_deps = TO_LIST(deps),
391 samba_includes = includes,
392 local_include = local_include,
393 local_include_first = local_include_first,
394 samba_subsystem= subsystem_name
397 if cflags_end is not None:
398 t.samba_cflags.extend(TO_LIST(cflags_end))
400 if heimdal_autoproto is not None:
401 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
402 if heimdal_autoproto_private is not None:
403 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
404 if autoproto is not None:
405 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
406 if public_headers is not None:
407 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
411 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
414 def SAMBA_GENERATOR(bld, name, rule, source, target,
415 group='generators', enabled=True,
419 '''A generic source generator target'''
421 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
427 bld.SET_BUILD_GROUP(group)
430 source=bld.EXPAND_VARIABLES(source, vars=vars),
432 shell=isinstance(rule, str),
438 if public_headers is not None:
439 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
441 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
446 def SETUP_BUILD_GROUPS(bld):
447 '''setup build groups used to ensure that the different build
448 phases happen consecutively'''
449 bld.p_ln = bld.srcnode # we do want to see all targets!
450 bld.env['USING_BUILD_GROUPS'] = True
451 bld.add_group('setup')
452 bld.add_group('build_compiler_source')
453 bld.add_group('base_libraries')
454 bld.add_group('generators')
455 bld.add_group('compiler_prototypes')
456 bld.add_group('compiler_libraries')
457 bld.add_group('build_compilers')
458 bld.add_group('build_source')
459 bld.add_group('prototypes')
460 bld.add_group('main')
461 bld.add_group('binaries')
462 bld.add_group('final')
463 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
466 def SET_BUILD_GROUP(bld, group):
467 '''set the current build group'''
468 if not 'USING_BUILD_GROUPS' in bld.env:
471 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
476 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
477 """use timestamps instead of file contents for deps
478 this currently doesn't work"""
479 def h_file(filename):
481 st = os.stat(filename)
482 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
484 m.update(str(st.st_mtime))
485 m.update(str(st.st_size))
488 Utils.h_file = h_file
492 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
493 shell=True, color='PINK', ext_in='.bin')
496 @feature('copy_script')
497 @before('apply_link')
498 def copy_script(self):
499 tsk = self.create_task('copy_script', self.allnodes[0])
500 tsk.env.TARGET = self.target
502 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
503 '''used to copy scripts from the source tree into the build directory
504 for use by selftest'''
506 source = bld.path.ant_glob(pattern)
508 bld.SET_BUILD_GROUP('build_source')
509 for s in TO_LIST(source):
511 if installname != None:
513 target = os.path.join(installdir, iname)
514 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
516 t = bld(features='copy_script',
521 t.env.LINK_TARGET = target
523 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
526 def install_file(bld, destdir, file, chmod=0644, flat=False,
527 python_fixup=False, destname=None, base_name=None):
529 destdir = bld.EXPAND_VARIABLES(destdir)
533 destname = os.path.basename(destname)
534 dest = os.path.join(destdir, destname)
536 # fixup the python path it will use to find Samba modules
537 inst_file = file + '.inst'
538 bld.SAMBA_GENERATOR('python_%s' % destname,
539 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
544 file = os.path.join(base_name, file)
545 bld.install_as(dest, file, chmod=chmod)
548 def INSTALL_FILES(bld, destdir, files, chmod=0644, flat=False,
549 python_fixup=False, destname=None, base_name=None):
550 '''install a set of files'''
551 for f in TO_LIST(files):
552 install_file(bld, destdir, f, chmod=chmod, flat=flat,
553 python_fixup=python_fixup, destname=destname,
555 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
558 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=0644, flat=False,
559 python_fixup=False, exclude=None, trim_path=None):
560 '''install a set of files matching a wildcard pattern'''
561 files=TO_LIST(bld.path.ant_glob(pattern))
565 files2.append(os_path_relpath(f, trim_path))
570 if fnmatch.fnmatch(f, exclude):
572 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
573 python_fixup=python_fixup, base_name=trim_path)
574 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
577 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
578 '''install some headers
580 header_path may either be a string that is added to the INCLUDEDIR,
581 or it can be a dictionary of wildcard patterns which map to destination
582 directories relative to INCLUDEDIR
584 dest = '${INCLUDEDIR}'
585 if isinstance(header_path, str):
586 dest += '/' + header_path
587 for h in TO_LIST(public_headers):
589 if isinstance(header_path, list):
590 for (p1, dir) in header_path:
594 if fnmatch.fnmatch(h, p2):
596 hdest = os.path.join(hdest, dir)
599 if found_match: break
600 if h.find(':') != -1:
602 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
604 INSTALL_FILES(bld, hdest, h, flat=True)
605 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
608 def subst_at_vars(task):
609 '''substiture @VAR@ style variables in a file'''
610 src = task.inputs[0].srcpath(task.env)
611 tgt = task.outputs[0].bldpath(task.env)
617 a = re.split('(@\w+@)', s)
619 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
621 if re.match('@\w+@', v):
623 if not vname in task.env and vname.upper() in task.env:
624 vname = vname.upper()
625 if not vname in task.env:
626 print "Unknown substitution %s in %s" % (v, task.name)
628 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
629 # now we back substitute the allowed pc vars
630 for (b, m) in back_sub:
635 contents = ''.join(out)
637 s = f.write(contents)
643 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
644 '''install some pkg_config pc files'''
645 dest = '${PKGCONFIGDIR}'
646 dest = bld.EXPAND_VARIABLES(dest)
647 for f in TO_LIST(pc_files):
648 base=os.path.basename(f)
649 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
654 t.env.PACKAGE_VERSION = vnum
655 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
656 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
660 #############################################################
661 # give a nicer display when building different types of files
662 def progress_display(self, msg, fname):
663 col1 = Logs.colors(self.color)
664 col2 = Logs.colors.NORMAL
665 total = self.position[1]
667 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
668 return fs % (self.position[0], self.position[1], col1, fname, col2)
670 def link_display(self):
671 if Options.options.progress_bar != 0:
672 return Task.Task.old_display(self)
673 fname = self.outputs[0].bldpath(self.env)
674 return progress_display(self, 'Linking', fname)
675 Task.TaskBase.classes['cc_link'].display = link_display
677 def samba_display(self):
678 if Options.options.progress_bar != 0:
679 return Task.Task.old_display(self)
681 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
682 if self.name in targets:
683 target_type = targets[self.name]
684 type_map = { 'GENERATOR' : 'Generating',
685 'PROTOTYPE' : 'Generating'
687 if target_type in type_map:
688 return progress_display(self, type_map[target_type], self.name)
690 fname = self.inputs[0].bldpath(self.env)
691 if fname[0:3] == '../':
693 ext_loc = fname.rfind('.')
695 return Task.Task.old_display(self)
696 ext = fname[ext_loc:]
698 ext_map = { '.idl' : 'Compiling IDL',
699 '.et' : 'Compiling ERRTABLE',
700 '.asn1': 'Compiling ASN1',
703 return progress_display(self, ext_map[ext], fname)
704 return Task.Task.old_display(self)
706 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
707 Task.TaskBase.classes['Task'].display = samba_display