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 autoproto is not None:
191 bld.SAMBA_AUTOPROTO(autoproto, source)
193 if public_headers is not None:
194 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
196 if pc_files is not None:
197 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
199 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
202 #################################################################
203 def SAMBA_BINARY(bld, binname, source,
214 use_global_deps=True,
224 '''define a Samba binary'''
226 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
229 features = 'cc cprogram symlink_bin install_bin'
231 features += ' pyembed'
233 obj_target = binname + '.objlist'
235 source = bld.EXPAND_VARIABLES(source, vars=vars)
237 # first create a target for building the object files for this binary
238 # by separating in this way, we avoid recompiling the C files
239 # separately for the install binary and the build binary
240 bld.SAMBA_SUBSYSTEM(obj_target,
246 autoproto = autoproto,
247 subsystem_name = subsystem_name,
248 needs_python = needs_python,
249 local_include = local_include,
250 use_hostcc = use_hostcc,
251 use_global_deps= use_global_deps)
253 bld.SET_BUILD_GROUP(group)
255 # the binary itself will depend on that object target
257 deps.append(obj_target)
263 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
265 samba_includes = includes,
266 local_include = local_include,
267 samba_modules = modules,
269 samba_subsystem= subsystem_name,
271 samba_inst_path= install_path,
272 samba_install = install
275 # setup the subsystem_name as an alias for the real
276 # binary name, so it can be found when expanding
277 # subsystem dependencies
278 if subsystem_name is not None:
279 bld.TARGET_ALIAS(subsystem_name, binname)
281 if autoproto is not None:
282 bld.SAMBA_AUTOPROTO(autoproto, source)
283 if public_headers is not None:
284 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
285 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
288 #################################################################
289 def SAMBA_MODULE(bld, modname, source,
295 autoproto_extra_source='',
298 internal_module=True,
302 '''define a Samba module.'''
304 # we add the init function regardless of whether the module
305 # is enabled or not, as we need to generate a null list if
307 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
309 if internal_module or BUILTIN_LIBRARY(bld, modname):
310 # treat internal modules as subsystems for now
311 SAMBA_SUBSYSTEM(bld, modname, source,
315 autoproto_extra_source=autoproto_extra_source,
317 local_include=local_include,
322 SET_TARGET_TYPE(bld, modname, 'DISABLED')
325 source = bld.EXPAND_VARIABLES(source, vars=vars)
327 # remember empty modules, so we can strip the dependencies
328 if (source == '') or (source == []):
329 SET_TARGET_TYPE(bld, modname, 'EMPTY')
332 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
335 if subsystem is not None:
336 deps += ' ' + subsystem
338 bld.SET_BUILD_GROUP('main')
343 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
344 samba_includes = includes,
345 local_include = local_include,
346 samba_deps = TO_LIST(deps)
349 if autoproto is not None:
350 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
352 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
355 #################################################################
356 def SAMBA_SUBSYSTEM(bld, modname, source,
365 init_function_sentinal=None,
366 heimdal_autoproto=None,
367 heimdal_autoproto_options=None,
368 heimdal_autoproto_private=None,
370 autoproto_extra_source='',
373 local_include_first=True,
377 use_global_deps=True,
380 '''define a Samba subsystem'''
383 SET_TARGET_TYPE(bld, modname, 'DISABLED')
386 # remember empty subsystems, so we can strip the dependencies
387 if (source == '') or (source == []):
388 SET_TARGET_TYPE(bld, modname, 'EMPTY')
391 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
394 source = bld.EXPAND_VARIABLES(source, vars=vars)
396 deps += ' ' + public_deps
398 bld.SET_BUILD_GROUP(group)
408 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
409 depends_on = depends_on,
410 samba_deps = TO_LIST(deps),
411 samba_includes = includes,
412 local_include = local_include,
413 local_include_first = local_include_first,
414 samba_subsystem= subsystem_name,
415 samba_use_hostcc = use_hostcc,
416 samba_use_global_deps = use_global_deps
419 if cflags_end is not None:
420 t.samba_cflags.extend(TO_LIST(cflags_end))
422 if heimdal_autoproto is not None:
423 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
424 if heimdal_autoproto_private is not None:
425 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
426 if autoproto is not None:
427 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
428 if public_headers is not None:
429 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
433 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
436 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
437 group='generators', enabled=True,
441 '''A generic source generator target'''
443 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
449 bld.SET_BUILD_GROUP(group)
452 source=bld.EXPAND_VARIABLES(source, vars=vars),
454 shell=isinstance(rule, str),
460 if public_headers is not None:
461 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
463 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
468 def SETUP_BUILD_GROUPS(bld):
469 '''setup build groups used to ensure that the different build
470 phases happen consecutively'''
471 bld.p_ln = bld.srcnode # we do want to see all targets!
472 bld.env['USING_BUILD_GROUPS'] = True
473 bld.add_group('setup')
474 bld.add_group('build_compiler_source')
475 bld.add_group('base_libraries')
476 bld.add_group('generators')
477 bld.add_group('compiler_prototypes')
478 bld.add_group('compiler_libraries')
479 bld.add_group('build_compilers')
480 bld.add_group('build_source')
481 bld.add_group('prototypes')
482 bld.add_group('main')
483 bld.add_group('binaries')
484 bld.add_group('final')
485 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
488 def SET_BUILD_GROUP(bld, group):
489 '''set the current build group'''
490 if not 'USING_BUILD_GROUPS' in bld.env:
493 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
498 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
499 """use timestamps instead of file contents for deps
500 this currently doesn't work"""
501 def h_file(filename):
503 st = os.stat(filename)
504 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
506 m.update(str(st.st_mtime))
507 m.update(str(st.st_size))
510 Utils.h_file = h_file
514 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
515 shell=True, color='PINK', ext_in='.bin')
518 @feature('copy_script')
519 @before('apply_link')
520 def copy_script(self):
521 tsk = self.create_task('copy_script', self.allnodes[0])
522 tsk.env.TARGET = self.target
524 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
525 '''used to copy scripts from the source tree into the build directory
526 for use by selftest'''
528 source = bld.path.ant_glob(pattern)
530 bld.SET_BUILD_GROUP('build_source')
531 for s in TO_LIST(source):
533 if installname != None:
535 target = os.path.join(installdir, iname)
536 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
538 t = bld(features='copy_script',
543 t.env.LINK_TARGET = target
545 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
548 def install_file(bld, destdir, file, chmod=O644, flat=False,
549 python_fixup=False, destname=None, base_name=None):
551 destdir = bld.EXPAND_VARIABLES(destdir)
555 destname = os.path.basename(destname)
556 dest = os.path.join(destdir, destname)
558 # fixup the python path it will use to find Samba modules
559 inst_file = file + '.inst'
560 bld.SAMBA_GENERATOR('python_%s' % destname,
561 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
566 file = os.path.join(base_name, file)
567 bld.install_as(dest, file, chmod=chmod)
570 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
571 python_fixup=False, destname=None, base_name=None):
572 '''install a set of files'''
573 for f in TO_LIST(files):
574 install_file(bld, destdir, f, chmod=chmod, flat=flat,
575 python_fixup=python_fixup, destname=destname,
577 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
580 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
581 python_fixup=False, exclude=None, trim_path=None):
582 '''install a set of files matching a wildcard pattern'''
583 files=TO_LIST(bld.path.ant_glob(pattern))
587 files2.append(os_path_relpath(f, trim_path))
592 if fnmatch.fnmatch(f, exclude):
594 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
595 python_fixup=python_fixup, base_name=trim_path)
596 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
599 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
600 '''install some headers
602 header_path may either be a string that is added to the INCLUDEDIR,
603 or it can be a dictionary of wildcard patterns which map to destination
604 directories relative to INCLUDEDIR
606 dest = '${INCLUDEDIR}'
607 if isinstance(header_path, str):
608 dest += '/' + header_path
609 for h in TO_LIST(public_headers):
611 if isinstance(header_path, list):
612 for (p1, dir) in header_path:
616 if fnmatch.fnmatch(h, p2):
618 hdest = os.path.join(hdest, dir)
621 if found_match: break
622 if h.find(':') != -1:
624 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
626 INSTALL_FILES(bld, hdest, h, flat=True)
627 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
630 def subst_at_vars(task):
631 '''substiture @VAR@ style variables in a file'''
632 src = task.inputs[0].srcpath(task.env)
633 tgt = task.outputs[0].bldpath(task.env)
639 a = re.split('(@\w+@)', s)
641 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
643 if re.match('@\w+@', v):
645 if not vname in task.env and vname.upper() in task.env:
646 vname = vname.upper()
647 if not vname in task.env:
648 Logs.error("Unknown substitution %s in %s" % (v, task.name))
650 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
651 # now we back substitute the allowed pc vars
652 for (b, m) in back_sub:
657 contents = ''.join(out)
659 s = f.write(contents)
665 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
666 '''install some pkg_config pc files'''
667 dest = '${PKGCONFIGDIR}'
668 dest = bld.EXPAND_VARIABLES(dest)
669 for f in TO_LIST(pc_files):
670 base=os.path.basename(f)
671 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
676 t.env.PACKAGE_VERSION = vnum
677 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
678 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
682 #############################################################
683 # give a nicer display when building different types of files
684 def progress_display(self, msg, fname):
685 col1 = Logs.colors(self.color)
686 col2 = Logs.colors.NORMAL
687 total = self.position[1]
689 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
690 return fs % (self.position[0], self.position[1], col1, fname, col2)
692 def link_display(self):
693 if Options.options.progress_bar != 0:
694 return Task.Task.old_display(self)
695 fname = self.outputs[0].bldpath(self.env)
696 return progress_display(self, 'Linking', fname)
697 Task.TaskBase.classes['cc_link'].display = link_display
699 def samba_display(self):
700 if Options.options.progress_bar != 0:
701 return Task.Task.old_display(self)
703 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
704 if self.name in targets:
705 target_type = targets[self.name]
706 type_map = { 'GENERATOR' : 'Generating',
707 'PROTOTYPE' : 'Generating'
709 if target_type in type_map:
710 return progress_display(self, type_map[target_type], self.name)
712 fname = self.inputs[0].bldpath(self.env)
713 if fname[0:3] == '../':
715 ext_loc = fname.rfind('.')
717 return Task.Task.old_display(self)
718 ext = fname[ext_loc:]
720 ext_map = { '.idl' : 'Compiling IDL',
721 '.et' : 'Compiling ERRTABLE',
722 '.asn1': 'Compiling ASN1',
725 return progress_display(self, ext_map[ext], fname)
726 return Task.Task.old_display(self)
728 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
729 Task.TaskBase.classes['Task'].display = samba_display