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
30 # some systems have broken threading in python
31 if os.environ.get('WAF_NOTHREADS') == '1':
36 os.putenv('PYTHONUNBUFFERED', '1')
39 def SAMBA_BUILD_ENV(conf):
40 '''create the samba build environment'''
41 conf.env.BUILD_DIRECTORY = conf.blddir
42 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
43 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
44 # this allows all of the bin/shared and bin/python targets
45 # to be expressed in terms of build directory paths
46 for p in ['python','shared']:
47 link_target = os.path.join(conf.blddir, 'default/' + p)
48 if not os.path.lexists(link_target):
49 os.symlink('../' + p, link_target)
51 # get perl to put the blib files in the build directory
52 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
53 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
54 mkdir_p(blib_bld + '/man1')
55 mkdir_p(blib_bld + '/man3')
56 if os.path.islink(blib_src):
58 elif os.path.exists(blib_src):
59 shutil.rmtree(blib_src)
62 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
63 '''add an init_function to the list for a subsystem'''
64 if init_function is None:
66 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
67 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
68 if not subsystem in cache:
70 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
71 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
75 #################################################################
76 def SAMBA_LIBRARY(bld, libname, source,
85 external_library=False,
95 target_type='LIBRARY',
96 bundled_extension=True,
99 '''define a Samba library'''
102 SET_TARGET_TYPE(bld, libname, 'DISABLED')
105 source = bld.EXPAND_VARIABLES(source, vars=vars)
107 # remember empty libraries, so we can strip the dependencies
108 if (source == '') or (source == []):
109 SET_TARGET_TYPE(bld, libname, 'EMPTY')
112 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
115 obj_target = libname + '.objlist'
117 # first create a target for building the object files for this library
118 # by separating in this way, we avoid recompiling the C files
119 # separately for the install library and the build library
120 bld.SAMBA_SUBSYSTEM(obj_target,
123 public_deps = public_deps,
125 public_headers = public_headers,
126 header_path = header_path,
129 autoproto = autoproto,
130 depends_on = depends_on,
131 needs_python = needs_python,
132 local_include = local_include)
134 if libname == obj_target:
137 if not SET_TARGET_TYPE(bld, libname, target_type):
140 # the library itself will depend on that object target
141 deps += ' ' + public_deps
143 deps.append(obj_target)
145 if target_type == 'PYTHON':
146 bundled_name = libname
148 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
150 features = 'cc cshlib symlink_lib install_lib'
151 if target_type == 'PYTHON':
154 features += ' pyembed'
156 bld.SET_BUILD_GROUP(group)
160 target = bundled_name,
161 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
162 depends_on = depends_on,
164 samba_includes = includes,
165 local_include = local_include,
168 samba_inst_path = install_path,
170 samba_realname = realname,
171 samba_install = install
175 t.link_name = link_name
177 if autoproto is not None:
178 bld.SAMBA_AUTOPROTO(autoproto, source)
180 if public_headers is not None:
181 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
183 if pc_files is not None:
184 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
186 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
189 #################################################################
190 def SAMBA_BINARY(bld, binname, source,
201 use_global_deps=True,
211 '''define a Samba binary'''
213 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
216 features = 'cc cprogram symlink_bin install_bin'
218 features += ' pyembed'
220 obj_target = binname + '.objlist'
222 source = bld.EXPAND_VARIABLES(source, vars=vars)
224 # first create a target for building the object files for this binary
225 # by separating in this way, we avoid recompiling the C files
226 # separately for the install binary and the build binary
227 bld.SAMBA_SUBSYSTEM(obj_target,
233 autoproto = autoproto,
234 subsystem_name = subsystem_name,
235 needs_python = needs_python,
236 local_include = local_include,
237 use_hostcc = use_hostcc,
238 use_global_deps= use_global_deps)
240 bld.SET_BUILD_GROUP(group)
242 # the binary itself will depend on that object target
244 deps.append(obj_target)
250 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
252 samba_includes = includes,
253 local_include = local_include,
254 samba_modules = modules,
256 samba_subsystem= subsystem_name,
258 samba_inst_path= install_path,
259 samba_install = install
262 # setup the subsystem_name as an alias for the real
263 # binary name, so it can be found when expanding
264 # subsystem dependencies
265 if subsystem_name is not None:
266 bld.TARGET_ALIAS(subsystem_name, binname)
268 if autoproto is not None:
269 bld.SAMBA_AUTOPROTO(autoproto, source)
270 if public_headers is not None:
271 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
272 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
275 #################################################################
276 def SAMBA_MODULE(bld, modname, source,
282 autoproto_extra_source='',
285 internal_module=True,
289 '''define a Samba module.'''
291 # we add the init function regardless of whether the module
292 # is enabled or not, as we need to generate a null list if
294 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
296 if internal_module or BUILTIN_LIBRARY(bld, modname):
297 # treat internal modules as subsystems for now
298 SAMBA_SUBSYSTEM(bld, modname, source,
302 autoproto_extra_source=autoproto_extra_source,
304 local_include=local_include,
309 SET_TARGET_TYPE(bld, modname, 'DISABLED')
312 source = bld.EXPAND_VARIABLES(source, vars=vars)
314 # remember empty modules, so we can strip the dependencies
315 if (source == '') or (source == []):
316 SET_TARGET_TYPE(bld, modname, 'EMPTY')
319 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
322 if subsystem is not None:
323 deps += ' ' + subsystem
325 bld.SET_BUILD_GROUP('main')
330 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
331 samba_includes = includes,
332 local_include = local_include,
333 samba_deps = TO_LIST(deps)
336 if autoproto is not None:
337 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
339 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
342 #################################################################
343 def SAMBA_SUBSYSTEM(bld, modname, source,
352 init_function_sentinal=None,
353 heimdal_autoproto=None,
354 heimdal_autoproto_options=None,
355 heimdal_autoproto_private=None,
357 autoproto_extra_source='',
360 local_include_first=True,
364 use_global_deps=True,
367 '''define a Samba subsystem'''
370 SET_TARGET_TYPE(bld, modname, 'DISABLED')
373 # remember empty subsystems, so we can strip the dependencies
374 if (source == '') or (source == []):
375 SET_TARGET_TYPE(bld, modname, 'EMPTY')
378 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
381 source = bld.EXPAND_VARIABLES(source, vars=vars)
383 deps += ' ' + public_deps
385 bld.SET_BUILD_GROUP(group)
395 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
396 depends_on = depends_on,
397 samba_deps = TO_LIST(deps),
398 samba_includes = includes,
399 local_include = local_include,
400 local_include_first = local_include_first,
401 samba_subsystem= subsystem_name,
402 samba_use_hostcc = use_hostcc,
403 samba_use_global_deps = use_global_deps
406 if cflags_end is not None:
407 t.samba_cflags.extend(TO_LIST(cflags_end))
409 if heimdal_autoproto is not None:
410 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
411 if heimdal_autoproto_private is not None:
412 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
413 if autoproto is not None:
414 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
415 if public_headers is not None:
416 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
420 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
423 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
424 group='generators', enabled=True,
428 '''A generic source generator target'''
430 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
436 bld.SET_BUILD_GROUP(group)
439 source=bld.EXPAND_VARIABLES(source, vars=vars),
441 shell=isinstance(rule, str),
447 if public_headers is not None:
448 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
450 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
455 def SETUP_BUILD_GROUPS(bld):
456 '''setup build groups used to ensure that the different build
457 phases happen consecutively'''
458 bld.p_ln = bld.srcnode # we do want to see all targets!
459 bld.env['USING_BUILD_GROUPS'] = True
460 bld.add_group('setup')
461 bld.add_group('build_compiler_source')
462 bld.add_group('base_libraries')
463 bld.add_group('generators')
464 bld.add_group('compiler_prototypes')
465 bld.add_group('compiler_libraries')
466 bld.add_group('build_compilers')
467 bld.add_group('build_source')
468 bld.add_group('prototypes')
469 bld.add_group('main')
470 bld.add_group('binaries')
471 bld.add_group('final')
472 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
475 def SET_BUILD_GROUP(bld, group):
476 '''set the current build group'''
477 if not 'USING_BUILD_GROUPS' in bld.env:
480 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
485 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
486 """use timestamps instead of file contents for deps
487 this currently doesn't work"""
488 def h_file(filename):
490 st = os.stat(filename)
491 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
493 m.update(str(st.st_mtime))
494 m.update(str(st.st_size))
497 Utils.h_file = h_file
501 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
502 shell=True, color='PINK', ext_in='.bin')
505 @feature('copy_script')
506 @before('apply_link')
507 def copy_script(self):
508 tsk = self.create_task('copy_script', self.allnodes[0])
509 tsk.env.TARGET = self.target
511 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
512 '''used to copy scripts from the source tree into the build directory
513 for use by selftest'''
515 source = bld.path.ant_glob(pattern)
517 bld.SET_BUILD_GROUP('build_source')
518 for s in TO_LIST(source):
520 if installname != None:
522 target = os.path.join(installdir, iname)
523 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
525 t = bld(features='copy_script',
530 t.env.LINK_TARGET = target
532 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
535 def install_file(bld, destdir, file, chmod=O644, flat=False,
536 python_fixup=False, destname=None, base_name=None):
538 destdir = bld.EXPAND_VARIABLES(destdir)
542 destname = os.path.basename(destname)
543 dest = os.path.join(destdir, destname)
545 # fixup the python path it will use to find Samba modules
546 inst_file = file + '.inst'
547 bld.SAMBA_GENERATOR('python_%s' % destname,
548 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
553 file = os.path.join(base_name, file)
554 bld.install_as(dest, file, chmod=chmod)
557 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
558 python_fixup=False, destname=None, base_name=None):
559 '''install a set of files'''
560 for f in TO_LIST(files):
561 install_file(bld, destdir, f, chmod=chmod, flat=flat,
562 python_fixup=python_fixup, destname=destname,
564 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
567 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
568 python_fixup=False, exclude=None, trim_path=None):
569 '''install a set of files matching a wildcard pattern'''
570 files=TO_LIST(bld.path.ant_glob(pattern))
574 files2.append(os_path_relpath(f, trim_path))
579 if fnmatch.fnmatch(f, exclude):
581 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
582 python_fixup=python_fixup, base_name=trim_path)
583 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
586 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
587 '''install some headers
589 header_path may either be a string that is added to the INCLUDEDIR,
590 or it can be a dictionary of wildcard patterns which map to destination
591 directories relative to INCLUDEDIR
593 dest = '${INCLUDEDIR}'
594 if isinstance(header_path, str):
595 dest += '/' + header_path
596 for h in TO_LIST(public_headers):
598 if isinstance(header_path, list):
599 for (p1, dir) in header_path:
603 if fnmatch.fnmatch(h, p2):
605 hdest = os.path.join(hdest, dir)
608 if found_match: break
609 if h.find(':') != -1:
611 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
613 INSTALL_FILES(bld, hdest, h, flat=True)
614 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
617 def subst_at_vars(task):
618 '''substiture @VAR@ style variables in a file'''
619 src = task.inputs[0].srcpath(task.env)
620 tgt = task.outputs[0].bldpath(task.env)
626 a = re.split('(@\w+@)', s)
628 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
630 if re.match('@\w+@', v):
632 if not vname in task.env and vname.upper() in task.env:
633 vname = vname.upper()
634 if not vname in task.env:
635 Logs.error("Unknown substitution %s in %s" % (v, task.name))
637 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
638 # now we back substitute the allowed pc vars
639 for (b, m) in back_sub:
644 contents = ''.join(out)
646 s = f.write(contents)
652 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
653 '''install some pkg_config pc files'''
654 dest = '${PKGCONFIGDIR}'
655 dest = bld.EXPAND_VARIABLES(dest)
656 for f in TO_LIST(pc_files):
657 base=os.path.basename(f)
658 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
663 t.env.PACKAGE_VERSION = vnum
664 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
665 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
669 #############################################################
670 # give a nicer display when building different types of files
671 def progress_display(self, msg, fname):
672 col1 = Logs.colors(self.color)
673 col2 = Logs.colors.NORMAL
674 total = self.position[1]
676 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
677 return fs % (self.position[0], self.position[1], col1, fname, col2)
679 def link_display(self):
680 if Options.options.progress_bar != 0:
681 return Task.Task.old_display(self)
682 fname = self.outputs[0].bldpath(self.env)
683 return progress_display(self, 'Linking', fname)
684 Task.TaskBase.classes['cc_link'].display = link_display
686 def samba_display(self):
687 if Options.options.progress_bar != 0:
688 return Task.Task.old_display(self)
690 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
691 if self.name in targets:
692 target_type = targets[self.name]
693 type_map = { 'GENERATOR' : 'Generating',
694 'PROTOTYPE' : 'Generating'
696 if target_type in type_map:
697 return progress_display(self, type_map[target_type], self.name)
699 fname = self.inputs[0].bldpath(self.env)
700 if fname[0:3] == '../':
702 ext_loc = fname.rfind('.')
704 return Task.Task.old_display(self)
705 ext = fname[ext_loc:]
707 ext_map = { '.idl' : 'Compiling IDL',
708 '.et' : 'Compiling ERRTABLE',
709 '.asn1': 'Compiling ASN1',
712 return progress_display(self, ext_map[ext], fname)
713 return Task.Task.old_display(self)
715 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
716 Task.TaskBase.classes['Task'].display = samba_display