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
8 TaskGen.task_gen.apply_verif = Utils.nada
10 # bring in the other samba modules
11 from samba_optimisation import *
12 from samba_utils import *
13 from samba_version import *
14 from samba_autoconf import *
15 from samba_patterns import *
16 from samba_pidl import *
17 from samba_errtable import *
18 from samba_asn1 import *
19 from samba_autoproto import *
20 from samba_python import *
21 from samba_deps import *
22 from samba_bundled import *
24 import samba_conftests
32 # some systems have broken threading in python
33 if os.environ.get('WAF_NOTHREADS') == '1':
38 os.putenv('PYTHONUNBUFFERED', '1')
41 if Constants.HEXVERSION < 0x105016:
43 Please use the version of waf that comes with Samba, not
44 a system installed version. See http://wiki.samba.org/index.php/Waf
47 Alternatively, please use ./autogen-waf.sh, and then
48 run ./configure and make as usual. That will call the right version of waf.
54 def SAMBA_BUILD_ENV(conf):
55 '''create the samba build environment'''
56 conf.env.BUILD_DIRECTORY = conf.blddir
57 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
58 mkdir_p(os.path.join(conf.blddir, "modules"))
59 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
60 # this allows all of the bin/shared and bin/python targets
61 # to be expressed in terms of build directory paths
62 mkdir_p(os.path.join(conf.blddir, 'default'))
63 for p in ['python','shared', 'modules']:
64 link_target = os.path.join(conf.blddir, 'default/' + p)
65 if not os.path.lexists(link_target):
66 os.symlink('../' + p, link_target)
68 # get perl to put the blib files in the build directory
69 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
70 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
71 mkdir_p(blib_bld + '/man1')
72 mkdir_p(blib_bld + '/man3')
73 if os.path.islink(blib_src):
75 elif os.path.exists(blib_src):
76 shutil.rmtree(blib_src)
79 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
80 '''add an init_function to the list for a subsystem'''
81 if init_function is None:
83 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
84 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
85 if not subsystem in cache:
87 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
88 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
92 #################################################################
93 def SAMBA_LIBRARY(bld, libname, source,
102 external_library=False,
112 target_type='LIBRARY',
113 bundled_extension=True,
121 '''define a Samba library'''
124 SET_TARGET_TYPE(bld, libname, 'DISABLED')
127 source = bld.EXPAND_VARIABLES(source, vars=vars)
129 # remember empty libraries, so we can strip the dependencies
130 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
131 SET_TARGET_TYPE(bld, libname, 'EMPTY')
134 if BUILTIN_LIBRARY(bld, libname):
137 obj_target = libname + '.objlist'
139 # first create a target for building the object files for this library
140 # by separating in this way, we avoid recompiling the C files
141 # separately for the install library and the build library
142 bld.SAMBA_SUBSYSTEM(obj_target,
145 public_deps = public_deps,
147 public_headers = public_headers,
148 header_path = header_path,
151 autoproto = autoproto,
152 depends_on = depends_on,
153 needs_python = needs_python,
154 hide_symbols = hide_symbols,
155 local_include = local_include)
157 if BUILTIN_LIBRARY(bld, libname):
160 if not SET_TARGET_TYPE(bld, libname, target_type):
163 # the library itself will depend on that object target
164 deps += ' ' + public_deps
166 deps.append(obj_target)
168 if target_type == 'PYTHON' or realname or not is_bundled:
169 # Sanitize the library name
170 bundled_name = libname.lower().replace('_', '-')
171 while bundled_name.startswith("lib"):
172 bundled_name = bundled_name[3:]
174 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
176 features = 'cc cshlib symlink_lib install_lib'
177 if target_type == 'PYTHON':
180 features += ' pyembed'
182 features += ' abi_check'
185 abi_file = os.path.join(bld.curdir, abi_file)
187 bld.SET_BUILD_GROUP(group)
191 target = bundled_name,
192 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
193 depends_on = depends_on,
195 samba_includes = includes,
196 local_include = local_include,
199 samba_inst_path = install_path,
201 samba_realname = realname,
202 samba_install = install,
204 abi_match = abi_match,
205 is_bundled = is_bundled
208 if realname and not link_name:
209 link_name = 'shared/%s' % realname
212 t.link_name = link_name
214 if pc_files is not None:
215 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
217 if manpages is not None and 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
218 bld.MANPAGES(manpages)
221 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
224 #################################################################
225 def SAMBA_BINARY(bld, binname, source,
235 use_global_deps=True,
246 '''define a Samba binary'''
249 SET_TARGET_TYPE(bld, binname, 'DISABLED')
252 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
255 features = 'cc cprogram symlink_bin install_bin'
257 features += ' pyembed'
259 obj_target = binname + '.objlist'
261 source = bld.EXPAND_VARIABLES(source, vars=vars)
262 source = unique_list(TO_LIST(source))
264 # first create a target for building the object files for this binary
265 # by separating in this way, we avoid recompiling the C files
266 # separately for the install binary and the build binary
267 bld.SAMBA_SUBSYSTEM(obj_target,
273 autoproto = autoproto,
274 subsystem_name = subsystem_name,
275 needs_python = needs_python,
276 local_include = local_include,
277 use_hostcc = use_hostcc,
278 use_global_deps= use_global_deps)
280 bld.SET_BUILD_GROUP(group)
282 # the binary itself will depend on that object target
284 deps.append(obj_target)
290 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
292 samba_includes = includes,
293 local_include = local_include,
294 samba_modules = modules,
296 samba_subsystem= subsystem_name,
298 samba_inst_path= install_path,
299 samba_install = install
302 # setup the subsystem_name as an alias for the real
303 # binary name, so it can be found when expanding
304 # subsystem dependencies
305 if subsystem_name is not None:
306 bld.TARGET_ALIAS(subsystem_name, binname)
308 if manpages is not None and 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
309 bld.MANPAGES(manpages)
311 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
314 #################################################################
315 def SAMBA_MODULE(bld, modname, source,
321 autoproto_extra_source='',
324 internal_module=True,
328 '''define a Samba module.'''
330 source = bld.EXPAND_VARIABLES(source, vars=vars)
332 if internal_module or BUILTIN_LIBRARY(bld, modname):
333 # treat internal modules as subsystems for now
334 if subsystem is not None:
335 deps += ' ' + subsystem
337 bld.SAMBA_SUBSYSTEM(modname, source,
341 autoproto_extra_source=autoproto_extra_source,
343 local_include=local_include,
346 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
350 SET_TARGET_TYPE(bld, modname, 'DISABLED')
353 modnames = [modname] + TO_LIST(aliases)
354 for modname in modnames:
355 obj_target = modname + '.objlist'
358 if subsystem is not None:
359 deps += ' ' + subsystem
360 while realname.startswith("lib"+subsystem+"_"):
361 realname = realname[len("lib"+subsystem+"_"):]
362 while realname.startswith(subsystem+"_"):
363 realname = realname[len(subsystem+"_"):]
365 realname = bld.env.shlib_PATTERN % realname
366 while realname.startswith("lib"):
367 realname = realname[len("lib"):]
369 build_link_name = "modules/%s/%s" % (subsystem, realname)
371 bld.SAMBA_LIBRARY(modname,
376 autoproto = autoproto,
377 local_include=local_include,
379 link_name=build_link_name,
380 install_path="${MODULESDIR}/%s" % subsystem
383 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
386 #################################################################
387 def SAMBA_SUBSYSTEM(bld, modname, source,
396 init_function_sentinal=None,
397 heimdal_autoproto=None,
398 heimdal_autoproto_options=None,
399 heimdal_autoproto_private=None,
401 autoproto_extra_source='',
404 local_include_first=True,
408 use_global_deps=True,
412 '''define a Samba subsystem'''
415 SET_TARGET_TYPE(bld, modname, 'DISABLED')
418 # remember empty subsystems, so we can strip the dependencies
419 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
420 SET_TARGET_TYPE(bld, modname, 'EMPTY')
423 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
426 source = bld.EXPAND_VARIABLES(source, vars=vars)
427 source = unique_list(TO_LIST(source))
429 deps += ' ' + public_deps
431 bld.SET_BUILD_GROUP(group)
441 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags, hide_symbols=hide_symbols),
442 depends_on = depends_on,
443 samba_deps = TO_LIST(deps),
444 samba_includes = includes,
445 local_include = local_include,
446 local_include_first = local_include_first,
447 samba_subsystem= subsystem_name,
448 samba_use_hostcc = use_hostcc,
449 samba_use_global_deps = use_global_deps
452 if cflags_end is not None:
453 t.samba_cflags.extend(TO_LIST(cflags_end))
455 if heimdal_autoproto is not None:
456 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
457 if heimdal_autoproto_private is not None:
458 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
459 if autoproto is not None:
460 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
461 if public_headers is not None:
462 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
466 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
469 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
470 group='generators', enabled=True,
475 '''A generic source generator target'''
477 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
483 bld.SET_BUILD_GROUP(group)
486 source=bld.EXPAND_VARIABLES(source, vars=vars),
488 shell=isinstance(rule, str),
497 if public_headers is not None:
498 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
500 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
505 def SETUP_BUILD_GROUPS(bld):
506 '''setup build groups used to ensure that the different build
507 phases happen consecutively'''
508 bld.p_ln = bld.srcnode # we do want to see all targets!
509 bld.env['USING_BUILD_GROUPS'] = True
510 bld.add_group('setup')
511 bld.add_group('build_compiler_source')
512 bld.add_group('base_libraries')
513 bld.add_group('generators')
514 bld.add_group('compiler_prototypes')
515 bld.add_group('compiler_libraries')
516 bld.add_group('build_compilers')
517 bld.add_group('build_source')
518 bld.add_group('prototypes')
519 bld.add_group('main')
520 bld.add_group('binaries')
521 bld.add_group('final')
522 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
525 def SET_BUILD_GROUP(bld, group):
526 '''set the current build group'''
527 if not 'USING_BUILD_GROUPS' in bld.env:
530 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
535 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
536 """use timestamps instead of file contents for deps
537 this currently doesn't work"""
538 def h_file(filename):
540 st = os.stat(filename)
541 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
543 m.update(str(st.st_mtime))
544 m.update(str(st.st_size))
547 Utils.h_file = h_file
551 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
552 shell=True, color='PINK', ext_in='.bin')
555 @feature('copy_script')
556 @before('apply_link')
557 def copy_script(self):
558 tsk = self.create_task('copy_script', self.allnodes[0])
559 tsk.env.TARGET = self.target
561 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
562 '''used to copy scripts from the source tree into the build directory
563 for use by selftest'''
565 source = bld.path.ant_glob(pattern)
567 bld.SET_BUILD_GROUP('build_source')
568 for s in TO_LIST(source):
570 if installname != None:
572 target = os.path.join(installdir, iname)
573 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
575 t = bld(features='copy_script',
580 t.env.LINK_TARGET = target
582 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
585 def install_file(bld, destdir, file, chmod=0644, flat=False,
586 python_fixup=False, destname=None, base_name=None):
588 destdir = bld.EXPAND_VARIABLES(destdir)
592 destname = os.path.basename(destname)
593 dest = os.path.join(destdir, destname)
595 # fixup the python path it will use to find Samba modules
596 inst_file = file + '.inst'
597 if bld.env["PYTHONDIR"] not in sys.path:
598 regex = "s|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g"
600 # Eliminate updating sys.path if the target python dir is already
602 regex = "s|sys.path.insert.*bin/python.*$||g"
603 bld.SAMBA_GENERATOR('python_%s' % destname,
604 rule="sed '%s' < ${SRC} > ${TGT}" % regex,
609 file = os.path.join(base_name, file)
610 bld.install_as(dest, file, chmod=chmod)
613 def INSTALL_FILES(bld, destdir, files, chmod=0644, flat=False,
614 python_fixup=False, destname=None, base_name=None):
615 '''install a set of files'''
616 for f in TO_LIST(files):
617 install_file(bld, destdir, f, chmod=chmod, flat=flat,
618 python_fixup=python_fixup, destname=destname,
620 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
623 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=0644, flat=False,
624 python_fixup=False, exclude=None, trim_path=None):
625 '''install a set of files matching a wildcard pattern'''
626 files=TO_LIST(bld.path.ant_glob(pattern))
630 files2.append(os_path_relpath(f, trim_path))
635 if fnmatch.fnmatch(f, exclude):
637 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
638 python_fixup=python_fixup, base_name=trim_path)
639 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
642 def INSTALL_DIRS(bld, destdir, dirs):
643 '''install a set of directories'''
644 destdir = bld.EXPAND_VARIABLES(destdir)
645 dirs = bld.EXPAND_VARIABLES(dirs)
646 for d in TO_LIST(dirs):
647 bld.install_dir(os.path.join(destdir, d))
648 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
651 re_header = re.compile('#include[ \t]*"([^"]+)"', re.I | re.M)
652 class header_task(Task.Task):
654 The public headers (the one installed on the system) have both
655 different paths and contents, so the rename is not enough.
657 Intermediate .inst.h files are created because path manipulation
658 may be slow. The substitution is thus performed only once.
663 vars = ['INCLUDEDIR', 'HEADER_DEPS']
666 txt = self.inputs[0].read(self.env)
668 # hard-coded string, but only present in samba4 (I promise, you won't feel a thing)
669 txt = txt.replace('#if _SAMBA_BUILD_ == 4', '#if 1\n')
671 # use a regexp to substitute the #include lines in the files
672 map = self.generator.bld.hnodemap
673 dirnodes = self.generator.bld.hnodedirs
678 # pokemon headers: gotta catch'em all!
680 if s.startswith('bin/default'):
681 node = self.generator.bld.srcnode.find_resource(s.replace('bin/default/', ''))
683 Logs.warn('could not find the public header for %r' % s)
687 Logs.warn('could not find the public header replacement for build header %r' % s)
689 # this part is more difficult since the path may be relative to anything
690 for dirnode in dirnodes:
691 node = dirnode.find_resource(s)
697 Logs.warn('could not find the public header replacement for source header %r %r' % (s, node))
699 Logs.warn('-> could not find the public header for %r' % s)
701 return "#include <%s>" % fin
704 txt = re_header.sub(repl, txt)
706 # and write the output file
709 f = open(self.outputs[0].abspath(self.env), 'w')
715 @TaskGen.feature('pubh')
716 def make_public_headers(self):
718 collect the public headers to process and to install, then
719 create the substitutions (name and contents)
722 if not self.bld.is_install:
723 # install time only (lazy)
727 # hnodedirs: list of folders for searching the headers
728 # hnodemap: node ids and replacement string (node objects are unique)
730 self.bld.hnodedirs.append(self.path)
731 except AttributeError:
732 self.bld.hnodemap = {}
733 self.bld.hnodedirs = [self.bld.srcnode, self.path]
735 for k in 'source4 source4/include lib/talloc lib/tevent/ source4/lib/ldb/include/'.split():
736 node = self.bld.srcnode.find_dir(k)
738 self.bld.hnodedirs.append(node)
740 header_path = getattr(self, 'header_path', None) or ''
742 for x in self.to_list(self.headers):
744 # too complicated, but what was the original idea?
745 if isinstance(header_path, list):
747 for (p1, dir) in header_path:
748 lst = self.to_list(p1)
750 if fnmatch.fnmatch(x, p2):
758 inst_path = header_path
762 if x.find(':') != -1:
767 inn = self.path.find_resource(name)
770 raise ValueError("could not find the public header %r in %r" % (name, self.path))
771 out = inn.change_ext('.inst.h')
772 self.create_task('header', inn, out)
778 inst_path = inst_path + '/'
779 inst_path = inst_path + dest
781 self.bld.install_as('${INCLUDEDIR}/%s' % inst_path, out, self.env)
783 self.bld.hnodemap[inn.id] = inst_path
785 # create a hash (not md5) to make sure the headers are re-created if something changes
787 lst = list(self.bld.hnodemap.keys())
790 val = hash((val, k, self.bld.hnodemap[k]))
791 self.bld.env.HEADER_DEPS = val
793 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
794 '''install some headers
796 header_path may either be a string that is added to the INCLUDEDIR,
797 or it can be a dictionary of wildcard patterns which map to destination
798 directories relative to INCLUDEDIR
800 bld.SET_BUILD_GROUP('final')
801 ret = bld(features=['pubh'], headers=public_headers, header_path=header_path)
803 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
806 def subst_at_vars(task):
807 '''substiture @VAR@ style variables in a file'''
808 src = task.inputs[0].srcpath(task.env)
809 tgt = task.outputs[0].bldpath(task.env)
815 a = re.split('(@\w+@)', s)
818 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
820 if re.match('@\w+@', v):
822 if not vname in task.env and vname.upper() in task.env:
823 vname = vname.upper()
824 if not vname in task.env:
825 Logs.error("Unknown substitution %s in %s" % (v, task.name))
827 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
828 # now we back substitute the allowed pc vars
829 for (b, m) in back_sub:
832 if not b in done_var:
833 # we don't want to substitute the first usage
839 contents = ''.join(out)
841 s = f.write(contents)
847 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
848 '''install some pkg_config pc files'''
849 dest = '${PKGCONFIGDIR}'
850 dest = bld.EXPAND_VARIABLES(dest)
851 for f in TO_LIST(pc_files):
852 base=os.path.basename(f)
853 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
858 t.env.PACKAGE_VERSION = vnum
859 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
860 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
863 def MANPAGES(bld, manpages):
864 '''build and install manual pages'''
865 bld.env.MAN_XSL = 'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
866 for m in manpages.split():
868 bld.SAMBA_GENERATOR(m,
872 rule='${XSLTPROC} -o ${TGT} --nonet ${MAN_XSL} ${SRC}'
874 bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
875 Build.BuildContext.MANPAGES = MANPAGES
878 #############################################################
879 # give a nicer display when building different types of files
880 def progress_display(self, msg, fname):
881 col1 = Logs.colors(self.color)
882 col2 = Logs.colors.NORMAL
883 total = self.position[1]
885 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
886 return fs % (self.position[0], self.position[1], col1, fname, col2)
888 def link_display(self):
889 if Options.options.progress_bar != 0:
890 return Task.Task.old_display(self)
891 fname = self.outputs[0].bldpath(self.env)
892 return progress_display(self, 'Linking', fname)
893 Task.TaskBase.classes['cc_link'].display = link_display
895 def samba_display(self):
896 if Options.options.progress_bar != 0:
897 return Task.Task.old_display(self)
899 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
900 if self.name in targets:
901 target_type = targets[self.name]
902 type_map = { 'GENERATOR' : 'Generating',
903 'PROTOTYPE' : 'Generating'
905 if target_type in type_map:
906 return progress_display(self, type_map[target_type], self.name)
908 fname = self.inputs[0].bldpath(self.env)
909 if fname[0:3] == '../':
911 ext_loc = fname.rfind('.')
913 return Task.Task.old_display(self)
914 ext = fname[ext_loc:]
916 ext_map = { '.idl' : 'Compiling IDL',
917 '.et' : 'Compiling ERRTABLE',
918 '.asn1': 'Compiling ASN1',
921 return progress_display(self, ext_map[ext], fname)
922 return Task.Task.old_display(self)
924 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
925 Task.TaskBase.classes['Task'].display = samba_display