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, sys, 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_autoproto import *
18 from samba_python import *
19 from samba_deps import *
20 from samba_bundled import *
22 import samba_conftests
35 # some systems have broken threading in python
36 if os.environ.get('WAF_NOTHREADS') == '1':
41 os.environ['PYTHONUNBUFFERED'] = '1'
44 if Constants.HEXVERSION < 0x105019:
46 Please use the version of waf that comes with Samba, not
47 a system installed version. See http://wiki.samba.org/index.php/Waf
50 Alternatively, please run ./configure and make as usual. That will
51 call the right version of waf.''')
56 def SAMBA_BUILD_ENV(conf):
57 '''create the samba build environment'''
58 conf.env.BUILD_DIRECTORY = conf.blddir
59 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
60 mkdir_p(os.path.join(conf.blddir, LIB_PATH, "private"))
61 mkdir_p(os.path.join(conf.blddir, "modules"))
62 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
63 # this allows all of the bin/shared and bin/python targets
64 # to be expressed in terms of build directory paths
65 mkdir_p(os.path.join(conf.blddir, 'default'))
66 for p in ['python','shared', 'modules']:
67 link_target = os.path.join(conf.blddir, 'default/' + p)
68 if not os.path.lexists(link_target):
69 os.symlink('../' + p, link_target)
71 # get perl to put the blib files in the build directory
72 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
73 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
74 mkdir_p(blib_bld + '/man1')
75 mkdir_p(blib_bld + '/man3')
76 if os.path.islink(blib_src):
78 elif os.path.exists(blib_src):
79 shutil.rmtree(blib_src)
82 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
83 '''add an init_function to the list for a subsystem'''
84 if init_function is None:
86 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
87 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
88 if not subsystem in cache:
90 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
91 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
95 #################################################################
96 def SAMBA_LIBRARY(bld, libname, source,
107 external_library=False,
120 target_type='LIBRARY',
121 bundled_extension=True,
127 private_library=False,
128 grouping_library=False,
129 allow_undefined_symbols=False,
131 '''define a Samba library'''
134 SET_TARGET_TYPE(bld, libname, 'DISABLED')
137 source = bld.EXPAND_VARIABLES(source, vars=vars)
139 source = bld.SUBDIR(subdir, source)
141 # remember empty libraries, so we can strip the dependencies
142 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
143 SET_TARGET_TYPE(bld, libname, 'EMPTY')
146 if BUILTIN_LIBRARY(bld, libname):
149 obj_target = libname + '.objlist'
151 if group == 'libraries':
152 subsystem_group = 'main'
154 subsystem_group = group
156 # first create a target for building the object files for this library
157 # by separating in this way, we avoid recompiling the C files
158 # separately for the install library and the build library
159 bld.SAMBA_SUBSYSTEM(obj_target,
162 public_deps = public_deps,
164 public_headers = public_headers,
165 header_path = header_path,
167 group = subsystem_group,
168 autoproto = autoproto,
169 depends_on = depends_on,
170 hide_symbols = hide_symbols,
171 pyext = pyext or (target_type == "PYTHON"),
172 local_include = local_include,
173 global_include = global_include)
175 if BUILTIN_LIBRARY(bld, libname):
178 if not SET_TARGET_TYPE(bld, libname, target_type):
181 # the library itself will depend on that object target
182 deps += ' ' + public_deps
184 deps.append(obj_target)
186 realname = bld.map_shlib_extension(realname, python=(target_type=='PYTHON'))
187 link_name = bld.map_shlib_extension(link_name, python=(target_type=='PYTHON'))
189 # we don't want any public libraries without version numbers
190 if not private_library and vnum is None and soname is None and target_type != 'PYTHON' and not realname:
191 raise Utils.WafError("public library '%s' must have a vnum" % libname)
193 if target_type == 'PYTHON' or realname or not private_library:
194 bundled_name = libname.replace('_', '-')
196 bundled_name = PRIVATE_NAME(bld, libname, bundled_extension, private_library)
198 ldflags = TO_LIST(ldflags)
200 features = 'cc cshlib symlink_lib install_lib'
201 if target_type == 'PYTHON':
204 # this is quite strange. we should add pyext feature for pyext
205 # but that breaks the build. This may be a bug in the waf python tool
206 features += ' pyembed'
209 features += ' abi_check'
212 if bld.env.HAVE_LD_VERSION_SCRIPT:
214 version = "%s_%s" % (Utils.g_module.APPNAME, Utils.g_module.VERSION)
216 version = "%s_%s" % (libname, vnum)
220 vscript = "%s.vscript" % libname
221 bld.ABI_VSCRIPT(libname, abi_directory, version, vscript,
223 fullname = apply_pattern(bundled_name, bld.env.shlib_PATTERN)
224 fullpath = bld.path.find_or_declare(fullname)
225 vscriptpath = bld.path.find_or_declare(vscript)
227 raise Utils.WafError("unable to find fullpath for %s" % fullname)
229 raise Utils.WafError("unable to find vscript path for %s" % vscript)
230 bld.add_manual_dependency(fullpath, vscriptpath)
231 if Options.is_install:
232 # also make the .inst file depend on the vscript
233 instname = apply_pattern(bundled_name + '.inst', bld.env.shlib_PATTERN)
234 bld.add_manual_dependency(bld.path.find_or_declare(instname), bld.path.find_or_declare(vscript))
235 vscript = os.path.join(bld.path.abspath(bld.env), vscript)
237 bld.SET_BUILD_GROUP(group)
241 target = bundled_name,
242 depends_on = depends_on,
243 samba_ldflags = ldflags,
245 samba_includes = includes,
246 version_script = vscript,
247 local_include = local_include,
248 global_include = global_include,
252 samba_inst_path = install_path,
254 samba_realname = realname,
255 samba_install = install,
256 abi_directory = "%s/%s" % (bld.path.abspath(), abi_directory),
257 abi_match = abi_match,
258 private_library = private_library,
259 grouping_library=grouping_library,
260 allow_undefined_symbols=allow_undefined_symbols
263 if realname and not link_name:
264 link_name = 'shared/%s' % realname
267 t.link_name = link_name
269 if pc_files is not None:
270 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
272 if manpages is not None and 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
273 bld.MANPAGES(manpages)
276 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
279 #################################################################
280 def SAMBA_BINARY(bld, binname, source,
290 use_global_deps=True,
303 '''define a Samba binary'''
306 SET_TARGET_TYPE(bld, binname, 'DISABLED')
309 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
312 features = 'cc cprogram symlink_bin install_bin'
314 features += ' pyembed'
316 obj_target = binname + '.objlist'
318 source = bld.EXPAND_VARIABLES(source, vars=vars)
320 source = bld.SUBDIR(subdir, source)
321 source = unique_list(TO_LIST(source))
323 if group == 'binaries':
324 subsystem_group = 'main'
326 subsystem_group = group
328 # first create a target for building the object files for this binary
329 # by separating in this way, we avoid recompiling the C files
330 # separately for the install binary and the build binary
331 bld.SAMBA_SUBSYSTEM(obj_target,
336 group = subsystem_group,
337 autoproto = autoproto,
338 subsystem_name = subsystem_name,
339 local_include = local_include,
340 global_include = global_include,
341 use_hostcc = use_hostcc,
343 use_global_deps= use_global_deps)
345 bld.SET_BUILD_GROUP(group)
347 # the binary itself will depend on that object target
349 deps.append(obj_target)
356 samba_includes = includes,
357 local_include = local_include,
358 global_include = global_include,
359 samba_modules = modules,
361 samba_subsystem= subsystem_name,
363 samba_inst_path= install_path,
364 samba_install = install,
365 samba_ldflags = TO_LIST(ldflags)
368 if manpages is not None and 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
369 bld.MANPAGES(manpages)
371 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
374 #################################################################
375 def SAMBA_MODULE(bld, modname, source,
380 module_init_name='samba_init_module',
382 autoproto_extra_source='',
384 internal_module=True,
391 allow_undefined_symbols=False
393 '''define a Samba module.'''
395 source = bld.EXPAND_VARIABLES(source, vars=vars)
397 source = bld.SUBDIR(subdir, source)
399 if internal_module or BUILTIN_LIBRARY(bld, modname):
400 bld.SAMBA_SUBSYSTEM(modname, source,
404 autoproto_extra_source=autoproto_extra_source,
406 local_include=local_include,
407 global_include=global_include,
410 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
414 SET_TARGET_TYPE(bld, modname, 'DISABLED')
417 obj_target = modname + '.objlist'
420 if subsystem is not None:
421 deps += ' ' + subsystem
422 while realname.startswith("lib"+subsystem+"_"):
423 realname = realname[len("lib"+subsystem+"_"):]
424 while realname.startswith(subsystem+"_"):
425 realname = realname[len(subsystem+"_"):]
427 realname = bld.make_libname(realname)
428 while realname.startswith("lib"):
429 realname = realname[len("lib"):]
431 build_link_name = "modules/%s/%s" % (subsystem, realname)
434 cflags += " -D%s=%s" % (init_function, module_init_name)
436 bld.SAMBA_LIBRARY(modname,
442 autoproto = autoproto,
443 local_include=local_include,
444 global_include=global_include,
446 link_name=build_link_name,
447 install_path="${MODULESDIR}/%s" % subsystem,
449 allow_undefined_symbols=allow_undefined_symbols
453 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
456 #################################################################
457 def SAMBA_SUBSYSTEM(bld, modname, source,
466 init_function_sentinal=None,
468 autoproto_extra_source='',
471 local_include_first=True,
476 use_global_deps=True,
481 '''define a Samba subsystem'''
484 SET_TARGET_TYPE(bld, modname, 'DISABLED')
487 # remember empty subsystems, so we can strip the dependencies
488 if ((source == '') or (source == [])) and deps == '' and public_deps == '':
489 SET_TARGET_TYPE(bld, modname, 'EMPTY')
492 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
495 source = bld.EXPAND_VARIABLES(source, vars=vars)
497 source = bld.SUBDIR(subdir, source)
498 source = unique_list(TO_LIST(source))
500 deps += ' ' + public_deps
502 bld.SET_BUILD_GROUP(group)
512 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags, hide_symbols=hide_symbols),
513 depends_on = depends_on,
514 samba_deps = TO_LIST(deps),
515 samba_includes = includes,
516 local_include = local_include,
517 local_include_first = local_include_first,
518 global_include = global_include,
519 samba_subsystem= subsystem_name,
520 samba_use_hostcc = use_hostcc,
521 samba_use_global_deps = use_global_deps
524 if cflags_end is not None:
525 t.samba_cflags.extend(TO_LIST(cflags_end))
527 if autoproto is not None:
528 bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
529 if public_headers is not None:
530 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
534 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
537 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
538 group='generators', enabled=True,
540 public_headers_install=True,
544 '''A generic source generator target'''
546 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
552 bld.SET_BUILD_GROUP(group)
555 source=bld.EXPAND_VARIABLES(source, vars=vars),
557 shell=isinstance(rule, str),
561 samba_type='GENERATOR',
562 dep_vars = [rule] + (vars or []),
568 if public_headers is not None:
569 bld.PUBLIC_HEADERS(public_headers, header_path=header_path,
570 public_headers_install=public_headers_install)
572 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
577 def SETUP_BUILD_GROUPS(bld):
578 '''setup build groups used to ensure that the different build
579 phases happen consecutively'''
580 bld.p_ln = bld.srcnode # we do want to see all targets!
581 bld.env['USING_BUILD_GROUPS'] = True
582 bld.add_group('setup')
583 bld.add_group('build_compiler_source')
584 bld.add_group('vscripts')
585 bld.add_group('base_libraries')
586 bld.add_group('generators')
587 bld.add_group('compiler_prototypes')
588 bld.add_group('compiler_libraries')
589 bld.add_group('build_compilers')
590 bld.add_group('build_source')
591 bld.add_group('prototypes')
592 bld.add_group('main')
593 bld.add_group('symbolcheck')
594 bld.add_group('libraries')
595 bld.add_group('binaries')
596 bld.add_group('syslibcheck')
597 bld.add_group('final')
598 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
601 def SET_BUILD_GROUP(bld, group):
602 '''set the current build group'''
603 if not 'USING_BUILD_GROUPS' in bld.env:
606 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
611 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
612 """use timestamps instead of file contents for deps
613 this currently doesn't work"""
614 def h_file(filename):
616 st = os.stat(filename)
617 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
619 m.update(str(st.st_mtime))
620 m.update(str(st.st_size))
623 Utils.h_file = h_file
627 t = Task.simple_task_type('copy_script', 'rm -f "${LINK_TARGET}" && ln -s "${SRC[0].abspath(env)}" ${LINK_TARGET}',
628 shell=True, color='PINK', ext_in='.bin')
631 @feature('copy_script')
632 @before('apply_link')
633 def copy_script(self):
634 tsk = self.create_task('copy_script', self.allnodes[0])
635 tsk.env.TARGET = self.target
637 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
638 '''used to copy scripts from the source tree into the build directory
639 for use by selftest'''
641 source = bld.path.ant_glob(pattern)
643 bld.SET_BUILD_GROUP('build_source')
644 for s in TO_LIST(source):
646 if installname != None:
648 target = os.path.join(installdir, iname)
649 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
651 t = bld(features='copy_script',
656 t.env.LINK_TARGET = target
658 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
660 def copy_and_fix_python_path(task):
661 pattern='sys.path.insert(0, "bin/python")'
662 if task.env["PYTHONARCHDIR"] in sys.path and task.env["PYTHONDIR"] in sys.path:
664 elif task.env["PYTHONARCHDIR"] == task.env["PYTHONDIR"]:
665 replacement="""sys.path.insert(0, "%s")""" % task.env["PYTHONDIR"]
667 replacement="""sys.path.insert(0, "%s")
668 sys.path.insert(1, "%s")""" % (task.env["PYTHONARCHDIR"], task.env["PYTHONDIR"])
670 installed_location=task.outputs[0].bldpath(task.env)
671 source_file = open(task.inputs[0].srcpath(task.env))
672 installed_file = open(installed_location, 'w')
673 for line in source_file:
676 newline = line.replace(pattern, replacement)
677 installed_file.write(newline)
678 installed_file.close()
679 os.chmod(installed_location, 0755)
683 def install_file(bld, destdir, file, chmod=MODE_644, flat=False,
684 python_fixup=False, destname=None, base_name=None):
686 destdir = bld.EXPAND_VARIABLES(destdir)
690 destname = os.path.basename(destname)
691 dest = os.path.join(destdir, destname)
693 # fixup the python path it will use to find Samba modules
694 inst_file = file + '.inst'
695 bld.SAMBA_GENERATOR('python_%s' % destname,
696 rule=copy_and_fix_python_path,
701 file = os.path.join(base_name, file)
702 bld.install_as(dest, file, chmod=chmod)
705 def INSTALL_FILES(bld, destdir, files, chmod=MODE_644, flat=False,
706 python_fixup=False, destname=None, base_name=None):
707 '''install a set of files'''
708 for f in TO_LIST(files):
709 install_file(bld, destdir, f, chmod=chmod, flat=flat,
710 python_fixup=python_fixup, destname=destname,
712 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
715 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=MODE_644, flat=False,
716 python_fixup=False, exclude=None, trim_path=None):
717 '''install a set of files matching a wildcard pattern'''
718 files=TO_LIST(bld.path.ant_glob(pattern))
722 files2.append(os_path_relpath(f, trim_path))
727 if fnmatch.fnmatch(f, exclude):
729 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
730 python_fixup=python_fixup, base_name=trim_path)
731 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
734 def INSTALL_DIRS(bld, destdir, dirs):
735 '''install a set of directories'''
736 destdir = bld.EXPAND_VARIABLES(destdir)
737 dirs = bld.EXPAND_VARIABLES(dirs)
738 for d in TO_LIST(dirs):
739 bld.install_dir(os.path.join(destdir, d))
740 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
743 def MANPAGES(bld, manpages):
744 '''build and install manual pages'''
745 bld.env.MAN_XSL = 'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
746 for m in manpages.split():
748 bld.SAMBA_GENERATOR(m,
752 rule='${XSLTPROC} -o ${TGT} --nonet ${MAN_XSL} ${SRC}'
754 bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
755 Build.BuildContext.MANPAGES = MANPAGES
758 #############################################################
759 # give a nicer display when building different types of files
760 def progress_display(self, msg, fname):
761 col1 = Logs.colors(self.color)
762 col2 = Logs.colors.NORMAL
763 total = self.position[1]
765 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
766 return fs % (self.position[0], self.position[1], col1, fname, col2)
768 def link_display(self):
769 if Options.options.progress_bar != 0:
770 return Task.Task.old_display(self)
771 fname = self.outputs[0].bldpath(self.env)
772 return progress_display(self, 'Linking', fname)
773 Task.TaskBase.classes['cc_link'].display = link_display
775 def samba_display(self):
776 if Options.options.progress_bar != 0:
777 return Task.Task.old_display(self)
779 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
780 if self.name in targets:
781 target_type = targets[self.name]
782 type_map = { 'GENERATOR' : 'Generating',
783 'PROTOTYPE' : 'Generating'
785 if target_type in type_map:
786 return progress_display(self, type_map[target_type], self.name)
788 if len(self.inputs) == 0:
789 return Task.Task.old_display(self)
791 fname = self.inputs[0].bldpath(self.env)
792 if fname[0:3] == '../':
794 ext_loc = fname.rfind('.')
796 return Task.Task.old_display(self)
797 ext = fname[ext_loc:]
799 ext_map = { '.idl' : 'Compiling IDL',
800 '.et' : 'Compiling ERRTABLE',
801 '.asn1': 'Compiling ASN1',
804 return progress_display(self, ext_map[ext], fname)
805 return Task.Task.old_display(self)
807 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
808 Task.TaskBase.classes['Task'].display = samba_display
813 def apply_bundle_remove_dynamiclib_patch(self):
814 if self.env['MACBUNDLE'] or getattr(self,'mac_bundle',False):
815 if not getattr(self,'vnum',None):
817 self.env['LINKFLAGS'].remove('-dynamiclib')
818 self.env['LINKFLAGS'].remove('-single_module')