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
31 # some systems have broken threading in python
32 if os.environ.get('WAF_NOTHREADS') == '1':
37 os.putenv('PYTHONUNBUFFERED', '1')
40 if Constants.HEXVERSION < 0x105016:
42 Please use the version of waf that comes with Samba, not
43 a system installed version. See http://wiki.samba.org/index.php/Waf
46 Alternatively, please use ./autogen-waf.sh, and then
47 run ./configure and make as usual. That will call the right version of waf.
53 def SAMBA_BUILD_ENV(conf):
54 '''create the samba build environment'''
55 conf.env.BUILD_DIRECTORY = conf.blddir
56 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
57 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
58 # this allows all of the bin/shared and bin/python targets
59 # to be expressed in terms of build directory paths
60 mkdir_p(os.path.join(conf.blddir, 'default'))
61 for p in ['python','shared']:
62 link_target = os.path.join(conf.blddir, 'default/' + p)
63 if not os.path.lexists(link_target):
64 os.symlink('../' + p, link_target)
66 # get perl to put the blib files in the build directory
67 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
68 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
69 mkdir_p(blib_bld + '/man1')
70 mkdir_p(blib_bld + '/man3')
71 if os.path.islink(blib_src):
73 elif os.path.exists(blib_src):
74 shutil.rmtree(blib_src)
77 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
78 '''add an init_function to the list for a subsystem'''
79 if init_function is None:
81 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
82 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
83 if not subsystem in cache:
85 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
86 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
90 #################################################################
91 def SAMBA_LIBRARY(bld, libname, source,
100 external_library=False,
110 target_type='LIBRARY',
111 bundled_extension=True,
117 '''define a Samba library'''
120 SET_TARGET_TYPE(bld, libname, 'DISABLED')
123 source = bld.EXPAND_VARIABLES(source, vars=vars)
125 # remember empty libraries, so we can strip the dependencies
126 if (source == '') or (source == []):
127 SET_TARGET_TYPE(bld, libname, 'EMPTY')
130 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
133 obj_target = libname + '.objlist'
135 # first create a target for building the object files for this library
136 # by separating in this way, we avoid recompiling the C files
137 # separately for the install library and the build library
138 bld.SAMBA_SUBSYSTEM(obj_target,
141 public_deps = public_deps,
143 public_headers = public_headers,
144 header_path = header_path,
147 autoproto = autoproto,
148 depends_on = depends_on,
149 needs_python = needs_python,
150 hide_symbols = hide_symbols,
151 local_include = local_include)
153 if libname == obj_target:
156 if not SET_TARGET_TYPE(bld, libname, target_type):
159 # the library itself will depend on that object target
160 deps += ' ' + public_deps
162 deps.append(obj_target)
164 if target_type == 'PYTHON':
165 bundled_name = libname
167 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
169 features = 'cc cshlib symlink_lib install_lib'
170 if target_type == 'PYTHON':
173 features += ' pyembed'
175 features += ' abi_check'
178 abi_file = os.path.join(bld.curdir, abi_file)
180 bld.SET_BUILD_GROUP(group)
184 target = bundled_name,
185 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
186 depends_on = depends_on,
188 samba_includes = includes,
189 local_include = local_include,
192 samba_inst_path = install_path,
194 samba_realname = realname,
195 samba_install = install,
197 abi_match = abi_match
201 t.link_name = link_name
203 if pc_files is not None:
204 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
206 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
209 #################################################################
210 def SAMBA_BINARY(bld, binname, source,
220 use_global_deps=True,
230 '''define a Samba binary'''
232 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
235 features = 'cc cprogram symlink_bin install_bin'
237 features += ' pyembed'
239 obj_target = binname + '.objlist'
241 source = bld.EXPAND_VARIABLES(source, vars=vars)
243 # first create a target for building the object files for this binary
244 # by separating in this way, we avoid recompiling the C files
245 # separately for the install binary and the build binary
246 bld.SAMBA_SUBSYSTEM(obj_target,
252 autoproto = autoproto,
253 subsystem_name = subsystem_name,
254 needs_python = needs_python,
255 local_include = local_include,
256 use_hostcc = use_hostcc,
257 use_global_deps= use_global_deps)
259 bld.SET_BUILD_GROUP(group)
261 # the binary itself will depend on that object target
263 deps.append(obj_target)
269 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
271 samba_includes = includes,
272 local_include = local_include,
273 samba_modules = modules,
275 samba_subsystem= subsystem_name,
277 samba_inst_path= install_path,
278 samba_install = install
281 # setup the subsystem_name as an alias for the real
282 # binary name, so it can be found when expanding
283 # subsystem dependencies
284 if subsystem_name is not None:
285 bld.TARGET_ALIAS(subsystem_name, binname)
287 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
290 #################################################################
291 def SAMBA_MODULE(bld, modname, source,
297 autoproto_extra_source='',
300 internal_module=True,
304 '''define a Samba module.'''
306 # we add the init function regardless of whether the module
307 # is enabled or not, as we need to generate a null list if
309 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
311 if internal_module or BUILTIN_LIBRARY(bld, modname):
312 # treat internal modules as subsystems for now
313 SAMBA_SUBSYSTEM(bld, modname, source,
317 autoproto_extra_source=autoproto_extra_source,
319 local_include=local_include,
324 SET_TARGET_TYPE(bld, modname, 'DISABLED')
327 source = bld.EXPAND_VARIABLES(source, vars=vars)
329 # remember empty modules, so we can strip the dependencies
330 if (source == '') or (source == []):
331 SET_TARGET_TYPE(bld, modname, 'EMPTY')
334 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
337 if subsystem is not None:
338 deps += ' ' + subsystem
340 bld.SET_BUILD_GROUP('main')
345 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
346 samba_includes = includes,
347 local_include = local_include,
348 samba_deps = TO_LIST(deps)
351 if autoproto is not None:
352 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
354 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
357 #################################################################
358 def SAMBA_SUBSYSTEM(bld, modname, source,
367 init_function_sentinal=None,
368 heimdal_autoproto=None,
369 heimdal_autoproto_options=None,
370 heimdal_autoproto_private=None,
372 autoproto_extra_source='',
375 local_include_first=True,
379 use_global_deps=True,
383 '''define a Samba subsystem'''
386 SET_TARGET_TYPE(bld, modname, 'DISABLED')
389 # remember empty subsystems, so we can strip the dependencies
390 if (source == '') or (source == []):
391 SET_TARGET_TYPE(bld, modname, 'EMPTY')
394 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
397 source = bld.EXPAND_VARIABLES(source, vars=vars)
399 deps += ' ' + public_deps
401 bld.SET_BUILD_GROUP(group)
411 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags, hide_symbols=hide_symbols),
412 depends_on = depends_on,
413 samba_deps = TO_LIST(deps),
414 samba_includes = includes,
415 local_include = local_include,
416 local_include_first = local_include_first,
417 samba_subsystem= subsystem_name,
418 samba_use_hostcc = use_hostcc,
419 samba_use_global_deps = use_global_deps
422 if cflags_end is not None:
423 t.samba_cflags.extend(TO_LIST(cflags_end))
425 if heimdal_autoproto is not None:
426 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
427 if heimdal_autoproto_private is not None:
428 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
429 if autoproto is not None:
430 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
431 if public_headers is not None:
432 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
436 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
439 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
440 group='generators', enabled=True,
444 '''A generic source generator target'''
446 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
452 bld.SET_BUILD_GROUP(group)
455 source=bld.EXPAND_VARIABLES(source, vars=vars),
457 shell=isinstance(rule, str),
463 if public_headers is not None:
464 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
466 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
471 def SETUP_BUILD_GROUPS(bld):
472 '''setup build groups used to ensure that the different build
473 phases happen consecutively'''
474 bld.p_ln = bld.srcnode # we do want to see all targets!
475 bld.env['USING_BUILD_GROUPS'] = True
476 bld.add_group('setup')
477 bld.add_group('build_compiler_source')
478 bld.add_group('base_libraries')
479 bld.add_group('generators')
480 bld.add_group('compiler_prototypes')
481 bld.add_group('compiler_libraries')
482 bld.add_group('build_compilers')
483 bld.add_group('build_source')
484 bld.add_group('prototypes')
485 bld.add_group('main')
486 bld.add_group('binaries')
487 bld.add_group('final')
488 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
491 def SET_BUILD_GROUP(bld, group):
492 '''set the current build group'''
493 if not 'USING_BUILD_GROUPS' in bld.env:
496 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
501 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
502 """use timestamps instead of file contents for deps
503 this currently doesn't work"""
504 def h_file(filename):
506 st = os.stat(filename)
507 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
509 m.update(str(st.st_mtime))
510 m.update(str(st.st_size))
513 Utils.h_file = h_file
517 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
518 shell=True, color='PINK', ext_in='.bin')
521 @feature('copy_script')
522 @before('apply_link')
523 def copy_script(self):
524 tsk = self.create_task('copy_script', self.allnodes[0])
525 tsk.env.TARGET = self.target
527 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
528 '''used to copy scripts from the source tree into the build directory
529 for use by selftest'''
531 source = bld.path.ant_glob(pattern)
533 bld.SET_BUILD_GROUP('build_source')
534 for s in TO_LIST(source):
536 if installname != None:
538 target = os.path.join(installdir, iname)
539 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
541 t = bld(features='copy_script',
546 t.env.LINK_TARGET = target
548 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
551 def install_file(bld, destdir, file, chmod=O644, flat=False,
552 python_fixup=False, destname=None, base_name=None):
554 destdir = bld.EXPAND_VARIABLES(destdir)
558 destname = os.path.basename(destname)
559 dest = os.path.join(destdir, destname)
561 # fixup the python path it will use to find Samba modules
562 inst_file = file + '.inst'
563 bld.SAMBA_GENERATOR('python_%s' % destname,
564 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
569 file = os.path.join(base_name, file)
570 bld.install_as(dest, file, chmod=chmod)
573 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
574 python_fixup=False, destname=None, base_name=None):
575 '''install a set of files'''
576 for f in TO_LIST(files):
577 install_file(bld, destdir, f, chmod=chmod, flat=flat,
578 python_fixup=python_fixup, destname=destname,
580 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
583 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
584 python_fixup=False, exclude=None, trim_path=None):
585 '''install a set of files matching a wildcard pattern'''
586 files=TO_LIST(bld.path.ant_glob(pattern))
590 files2.append(os_path_relpath(f, trim_path))
595 if fnmatch.fnmatch(f, exclude):
597 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
598 python_fixup=python_fixup, base_name=trim_path)
599 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
602 def INSTALL_DIRS(bld, destdir, dirs):
603 '''install a set of directories'''
604 destdir = bld.EXPAND_VARIABLES(destdir)
605 dirs = bld.EXPAND_VARIABLES(dirs)
606 for d in TO_LIST(dirs):
607 bld.install_dir(os.path.join(destdir, d))
608 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
611 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
612 '''install some headers
614 header_path may either be a string that is added to the INCLUDEDIR,
615 or it can be a dictionary of wildcard patterns which map to destination
616 directories relative to INCLUDEDIR
618 dest = '${INCLUDEDIR}'
619 if isinstance(header_path, str):
620 dest += '/' + header_path
621 for h in TO_LIST(public_headers):
623 if isinstance(header_path, list):
624 for (p1, dir) in header_path:
628 if fnmatch.fnmatch(h, p2):
630 hdest = os.path.join(hdest, dir)
633 if found_match: break
634 if h.find(':') != -1:
636 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
638 INSTALL_FILES(bld, hdest, h, flat=True)
639 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
642 def subst_at_vars(task):
643 '''substiture @VAR@ style variables in a file'''
644 src = task.inputs[0].srcpath(task.env)
645 tgt = task.outputs[0].bldpath(task.env)
651 a = re.split('(@\w+@)', s)
653 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
655 if re.match('@\w+@', v):
657 if not vname in task.env and vname.upper() in task.env:
658 vname = vname.upper()
659 if not vname in task.env:
660 Logs.error("Unknown substitution %s in %s" % (v, task.name))
662 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
663 # now we back substitute the allowed pc vars
664 for (b, m) in back_sub:
669 contents = ''.join(out)
671 s = f.write(contents)
677 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
678 '''install some pkg_config pc files'''
679 dest = '${PKGCONFIGDIR}'
680 dest = bld.EXPAND_VARIABLES(dest)
681 for f in TO_LIST(pc_files):
682 base=os.path.basename(f)
683 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
688 t.env.PACKAGE_VERSION = vnum
689 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
690 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
694 #############################################################
695 # give a nicer display when building different types of files
696 def progress_display(self, msg, fname):
697 col1 = Logs.colors(self.color)
698 col2 = Logs.colors.NORMAL
699 total = self.position[1]
701 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
702 return fs % (self.position[0], self.position[1], col1, fname, col2)
704 def link_display(self):
705 if Options.options.progress_bar != 0:
706 return Task.Task.old_display(self)
707 fname = self.outputs[0].bldpath(self.env)
708 return progress_display(self, 'Linking', fname)
709 Task.TaskBase.classes['cc_link'].display = link_display
711 def samba_display(self):
712 if Options.options.progress_bar != 0:
713 return Task.Task.old_display(self)
715 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
716 if self.name in targets:
717 target_type = targets[self.name]
718 type_map = { 'GENERATOR' : 'Generating',
719 'PROTOTYPE' : 'Generating'
721 if target_type in type_map:
722 return progress_display(self, type_map[target_type], self.name)
724 fname = self.inputs[0].bldpath(self.env)
725 if fname[0:3] == '../':
727 ext_loc = fname.rfind('.')
729 return Task.Task.old_display(self)
730 ext = fname[ext_loc:]
732 ext_map = { '.idl' : 'Compiling IDL',
733 '.et' : 'Compiling ERRTABLE',
734 '.asn1': 'Compiling ASN1',
737 return progress_display(self, ext_map[ext], fname)
738 return Task.Task.old_display(self)
740 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
741 Task.TaskBase.classes['Task'].display = samba_display