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
24 # some systems have broken threading in python
25 if os.environ.get('WAF_NOTHREADS') == '1':
30 os.putenv('PYTHONUNBUFFERED', '1')
33 def SAMBA_BUILD_ENV(conf):
34 '''create the samba build environment'''
35 conf.env['BUILD_DIRECTORY'] = conf.blddir
36 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
37 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
38 # this allows all of the bin/shared and bin/python targets
39 # to be expressed in terms of build directory paths
40 for p in ['python','shared']:
41 link_target = os.path.join(conf.blddir, 'default/' + p)
42 if not os.path.lexists(link_target):
43 os.symlink('../' + p, link_target)
45 # get perl to put the blib files in the build directory
46 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
47 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
48 mkdir_p(blib_bld + '/man1')
49 mkdir_p(blib_bld + '/man3')
50 if os.path.islink(blib_src):
52 elif os.path.exists(blib_src):
53 shutil.rmtree(blib_src)
56 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
57 '''add an init_function to the list for a subsystem'''
58 if init_function is None:
60 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
61 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
62 if not subsystem in cache:
64 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
65 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
69 #################################################################
70 def SAMBA_LIBRARY(bld, libname, source,
79 external_library=False,
89 target_type='LIBRARY',
90 bundled_extension=True,
93 '''define a Samba library'''
96 SET_TARGET_TYPE(bld, libname, 'DISABLED')
99 source = bld.EXPAND_VARIABLES(source, vars=vars)
101 # remember empty libraries, so we can strip the dependencies
102 if (source == '') or (source == []):
103 SET_TARGET_TYPE(bld, libname, 'EMPTY')
106 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
109 obj_target = libname + '.objlist'
111 # first create a target for building the object files for this library
112 # by separating in this way, we avoid recompiling the C files
113 # separately for the install library and the build library
114 bld.SAMBA_SUBSYSTEM(obj_target,
117 public_deps = public_deps,
119 public_headers = public_headers,
120 header_path = header_path,
123 autoproto = autoproto,
124 depends_on = depends_on,
125 needs_python = needs_python,
126 local_include = local_include)
128 if libname == obj_target:
131 if not SET_TARGET_TYPE(bld, libname, target_type):
134 # the library itself will depend on that object target
135 deps += ' ' + public_deps
137 deps.append(obj_target)
139 if target_type == 'PYTHON':
140 bundled_name = libname
142 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
144 features = 'cc cshlib symlink_lib install_lib'
145 if target_type == 'PYTHON':
148 features += ' pyembed'
150 bld.SET_BUILD_GROUP(group)
154 target = bundled_name,
155 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
156 depends_on = depends_on,
158 samba_includes = includes,
159 local_include = local_include,
162 samba_inst_path = install_path,
164 samba_realname = realname,
165 samba_install = install
169 t.link_name = link_name
171 if autoproto is not None:
172 bld.SAMBA_AUTOPROTO(autoproto, source)
174 if public_headers is not None:
175 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
177 if pc_files is not None:
178 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
180 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
183 #################################################################
184 def SAMBA_BINARY(bld, binname, source,
204 '''define a Samba binary'''
206 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
209 features = 'cc cprogram symlink_bin install_bin'
211 features += ' pyembed'
213 obj_target = binname + '.objlist'
215 source = bld.EXPAND_VARIABLES(source, vars=vars)
217 # first create a target for building the object files for this binary
218 # by separating in this way, we avoid recompiling the C files
219 # separately for the install binary and the build binary
220 bld.SAMBA_SUBSYSTEM(obj_target,
226 autoproto = autoproto,
227 subsystem_name = subsystem_name,
228 needs_python = needs_python,
229 local_include = local_include)
231 bld.SET_BUILD_GROUP(group)
233 # the binary itself will depend on that object target
235 deps.append(obj_target)
241 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
243 samba_includes = includes,
244 local_include = local_include,
245 samba_modules = modules,
247 samba_subsystem= subsystem_name,
249 samba_inst_path= install_path,
250 samba_install = install
253 # setup the subsystem_name as an alias for the real
254 # binary name, so it can be found when expanding
255 # subsystem dependencies
256 if subsystem_name is not None:
257 bld.TARGET_ALIAS(subsystem_name, binname)
259 if autoproto is not None:
260 bld.SAMBA_AUTOPROTO(autoproto, source)
261 if public_headers is not None:
262 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
263 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
266 #################################################################
267 def SAMBA_MODULE(bld, modname, source,
273 autoproto_extra_source='',
276 internal_module=True,
280 '''define a Samba module.'''
282 # we add the init function regardless of whether the module
283 # is enabled or not, as we need to generate a null list if
285 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
287 if internal_module or BUILTIN_LIBRARY(bld, modname):
288 # treat internal modules as subsystems for now
289 SAMBA_SUBSYSTEM(bld, modname, source,
293 autoproto_extra_source=autoproto_extra_source,
295 local_include=local_include,
300 SET_TARGET_TYPE(bld, modname, 'DISABLED')
303 source = bld.EXPAND_VARIABLES(source, vars=vars)
305 # remember empty modules, so we can strip the dependencies
306 if (source == '') or (source == []):
307 SET_TARGET_TYPE(bld, modname, 'EMPTY')
310 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
313 if subsystem is not None:
314 deps += ' ' + subsystem
316 bld.SET_BUILD_GROUP('main')
321 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
322 samba_includes = includes,
323 local_include = local_include,
324 samba_deps = TO_LIST(deps)
327 if autoproto is not None:
328 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
330 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
333 #################################################################
334 def SAMBA_SUBSYSTEM(bld, modname, source,
343 init_function_sentinal=None,
344 heimdal_autoproto=None,
345 heimdal_autoproto_options=None,
346 heimdal_autoproto_private=None,
348 autoproto_extra_source='',
351 local_include_first=True,
356 '''define a Samba subsystem'''
359 SET_TARGET_TYPE(bld, modname, 'DISABLED')
362 # remember empty subsystems, so we can strip the dependencies
363 if (source == '') or (source == []):
364 SET_TARGET_TYPE(bld, modname, 'EMPTY')
367 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
370 source = bld.EXPAND_VARIABLES(source, vars=vars)
372 deps += ' ' + public_deps
374 bld.SET_BUILD_GROUP(group)
384 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
385 depends_on = depends_on,
386 samba_deps = TO_LIST(deps),
387 samba_includes = includes,
388 local_include = local_include,
389 local_include_first = local_include_first,
390 samba_subsystem= subsystem_name
393 if cflags_end is not None:
394 t.samba_cflags.extend(TO_LIST(cflags_end))
396 if heimdal_autoproto is not None:
397 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
398 if heimdal_autoproto_private is not None:
399 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
400 if autoproto is not None:
401 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
402 if public_headers is not None:
403 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
407 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
410 def SAMBA_GENERATOR(bld, name, rule, source, target,
411 group='generators', enabled=True,
415 '''A generic source generator target'''
417 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
423 bld.SET_BUILD_GROUP(group)
426 source=bld.EXPAND_VARIABLES(source, vars=vars),
428 shell=isinstance(rule, str),
434 if public_headers is not None:
435 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
437 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
441 def BUILD_SUBDIR(bld, dir):
442 '''add a new set of build rules from a subdirectory'''
443 path = os.path.normpath(bld.curdir + '/' + dir)
444 cache = LOCAL_CACHE(bld, 'SUBDIR_LIST')
445 if path in cache: return
447 debug("build: Processing subdirectory %s" % dir)
449 Build.BuildContext.BUILD_SUBDIR = BUILD_SUBDIR
454 def SETUP_BUILD_GROUPS(bld):
455 '''setup build groups used to ensure that the different build
456 phases happen consecutively'''
457 bld.p_ln = bld.srcnode # we do want to see all targets!
458 bld.env['USING_BUILD_GROUPS'] = True
459 bld.add_group('setup')
460 bld.add_group('build_compiler_source')
461 bld.add_group('base_libraries')
462 bld.add_group('generators')
463 bld.add_group('compiler_prototypes')
464 bld.add_group('compiler_libraries')
465 bld.add_group('build_compilers')
466 bld.add_group('build_source')
467 bld.add_group('prototypes')
468 bld.add_group('main')
469 bld.add_group('binaries')
470 bld.add_group('final')
471 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
474 def SET_BUILD_GROUP(bld, group):
475 '''set the current build group'''
476 if not 'USING_BUILD_GROUPS' in bld.env:
479 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
484 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
485 """use timestamps instead of file contents for deps
486 this currently doesn't work"""
487 def h_file(filename):
489 st = os.stat(filename)
490 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
492 m.update(str(st.st_mtime))
493 m.update(str(st.st_size))
496 Utils.h_file = h_file
500 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
501 shell=True, color='PINK', ext_in='.bin')
504 @feature('copy_script')
505 @before('apply_link')
506 def copy_script(self):
507 tsk = self.create_task('copy_script', self.allnodes[0])
508 tsk.env.TARGET = self.target
510 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
511 '''used to copy scripts from the source tree into the build directory
512 for use by selftest'''
514 source = bld.path.ant_glob(pattern)
516 bld.SET_BUILD_GROUP('build_source')
517 for s in TO_LIST(source):
519 if installname != None:
521 target = os.path.join(installdir, iname)
522 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
524 t = bld(features='copy_script',
529 t.env.LINK_TARGET = target
531 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
534 def install_file(bld, destdir, file, chmod=0644, flat=False,
535 python_fixup=False, destname=None, base_name=None):
537 destdir = bld.EXPAND_VARIABLES(destdir)
541 destname = os.path.basename(destname)
542 dest = os.path.join(destdir, destname)
544 # fixup the python path it will use to find Samba modules
545 inst_file = file + '.inst'
546 bld.SAMBA_GENERATOR('python_%s' % destname,
547 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
552 file = os.path.join(base_name, file)
553 bld.install_as(dest, file, chmod=chmod)
556 def INSTALL_FILES(bld, destdir, files, chmod=0644, flat=False,
557 python_fixup=False, destname=None, base_name=None):
558 '''install a set of files'''
559 for f in TO_LIST(files):
560 install_file(bld, destdir, f, chmod=chmod, flat=flat,
561 python_fixup=python_fixup, destname=destname,
563 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
566 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=0644, flat=False,
567 python_fixup=False, exclude=None, trim_path=None):
568 '''install a set of files matching a wildcard pattern'''
569 files=TO_LIST(bld.path.ant_glob(pattern))
573 files2.append(os_path_relpath(f, trim_path))
578 if fnmatch.fnmatch(f, exclude):
580 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
581 python_fixup=python_fixup, base_name=trim_path)
582 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
585 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
586 '''install some headers
588 header_path may either be a string that is added to the INCLUDEDIR,
589 or it can be a dictionary of wildcard patterns which map to destination
590 directories relative to INCLUDEDIR
592 dest = '${INCLUDEDIR}'
593 if isinstance(header_path, str):
594 dest += '/' + header_path
595 for h in TO_LIST(public_headers):
597 if isinstance(header_path, list):
598 for (p1, dir) in header_path:
602 if fnmatch.fnmatch(h, p2):
604 hdest = os.path.join(hdest, dir)
607 if found_match: break
608 if h.find(':') != -1:
610 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
612 INSTALL_FILES(bld, hdest, h, flat=True)
613 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
616 def subst_at_vars(task):
617 '''substiture @VAR@ style variables in a file'''
618 src = task.inputs[0].srcpath(task.env)
619 tgt = task.outputs[0].bldpath(task.env)
625 a = re.split('(@\w+@)', s)
628 if re.match('@\w+@', v):
630 if not vname in task.env and vname.upper() in task.env:
631 vname = vname.upper()
632 if not vname in task.env:
633 print "Unknown substitution %s in %s" % (v, task.name)
637 contents = ''.join(out)
639 s = f.write(contents)
645 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
646 '''install some pkg_config pc files'''
647 dest = '${PKGCONFIGDIR}'
648 dest = bld.EXPAND_VARIABLES(dest)
649 for f in TO_LIST(pc_files):
650 base=os.path.basename(f)
651 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
656 t.env.PACKAGE_VERSION = vnum
657 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
658 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
662 #############################################################
663 # give a nicer display when building different types of files
664 def progress_display(self, msg, fname):
665 col1 = Logs.colors(self.color)
666 col2 = Logs.colors.NORMAL
667 total = self.position[1]
669 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
670 return fs % (self.position[0], self.position[1], col1, fname, col2)
672 def link_display(self):
673 if Options.options.progress_bar != 0:
674 return Task.Task.old_display(self)
675 fname = self.outputs[0].bldpath(self.env)
676 return progress_display(self, 'Linking', fname)
677 Task.TaskBase.classes['cc_link'].display = link_display
679 def samba_display(self):
680 if Options.options.progress_bar != 0:
681 return Task.Task.old_display(self)
683 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
684 if self.name in targets:
685 target_type = targets[self.name]
686 type_map = { 'GENERATOR' : 'Generating',
687 'PROTOTYPE' : 'Generating'
689 if target_type in type_map:
690 return progress_display(self, type_map[target_type], self.name)
692 fname = self.inputs[0].bldpath(self.env)
693 if fname[0:3] == '../':
695 ext_loc = fname.rfind('.')
697 return Task.Task.old_display(self)
698 ext = fname[ext_loc:]
700 ext_map = { '.idl' : 'Compiling IDL',
701 '.et' : 'Compiling ERRTABLE',
702 '.asn1': 'Compiling ASN1',
705 return progress_display(self, ext_map[ext], fname)
706 return Task.Task.old_display(self)
708 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
709 Task.TaskBase.classes['Task'].display = samba_display