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,
543 '''A generic source generator target'''
545 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
551 bld.SET_BUILD_GROUP(group)
554 source=bld.EXPAND_VARIABLES(source, vars=vars),
556 shell=isinstance(rule, str),
560 samba_type='GENERATOR',
561 dep_vars = [rule] + (vars or []),
567 if public_headers is not None:
568 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
570 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
575 def SETUP_BUILD_GROUPS(bld):
576 '''setup build groups used to ensure that the different build
577 phases happen consecutively'''
578 bld.p_ln = bld.srcnode # we do want to see all targets!
579 bld.env['USING_BUILD_GROUPS'] = True
580 bld.add_group('setup')
581 bld.add_group('build_compiler_source')
582 bld.add_group('vscripts')
583 bld.add_group('base_libraries')
584 bld.add_group('generators')
585 bld.add_group('compiler_prototypes')
586 bld.add_group('compiler_libraries')
587 bld.add_group('build_compilers')
588 bld.add_group('build_source')
589 bld.add_group('prototypes')
590 bld.add_group('main')
591 bld.add_group('symbolcheck')
592 bld.add_group('libraries')
593 bld.add_group('binaries')
594 bld.add_group('syslibcheck')
595 bld.add_group('final')
596 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
599 def SET_BUILD_GROUP(bld, group):
600 '''set the current build group'''
601 if not 'USING_BUILD_GROUPS' in bld.env:
604 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
609 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
610 """use timestamps instead of file contents for deps
611 this currently doesn't work"""
612 def h_file(filename):
614 st = os.stat(filename)
615 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
617 m.update(str(st.st_mtime))
618 m.update(str(st.st_size))
621 Utils.h_file = h_file
625 t = Task.simple_task_type('copy_script', 'rm -f "${LINK_TARGET}" && ln -s "${SRC[0].abspath(env)}" ${LINK_TARGET}',
626 shell=True, color='PINK', ext_in='.bin')
629 @feature('copy_script')
630 @before('apply_link')
631 def copy_script(self):
632 tsk = self.create_task('copy_script', self.allnodes[0])
633 tsk.env.TARGET = self.target
635 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
636 '''used to copy scripts from the source tree into the build directory
637 for use by selftest'''
639 source = bld.path.ant_glob(pattern)
641 bld.SET_BUILD_GROUP('build_source')
642 for s in TO_LIST(source):
644 if installname != None:
646 target = os.path.join(installdir, iname)
647 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
649 t = bld(features='copy_script',
654 t.env.LINK_TARGET = target
656 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
658 def copy_and_fix_python_path(task):
659 pattern='sys.path.insert(0, "bin/python")'
660 if task.env["PYTHONARCHDIR"] in sys.path and task.env["PYTHONDIR"] in sys.path:
662 elif task.env["PYTHONARCHDIR"] == task.env["PYTHONDIR"]:
663 replacement="""sys.path.insert(0, "%s")""" % task.env["PYTHONDIR"]
665 replacement="""sys.path.insert(0, "%s")
666 sys.path.insert(1, "%s")""" % (task.env["PYTHONARCHDIR"], task.env["PYTHONDIR"])
668 installed_location=task.outputs[0].bldpath(task.env)
669 source_file = open(task.inputs[0].srcpath(task.env))
670 installed_file = open(installed_location, 'w')
671 for line in source_file:
674 newline = line.replace(pattern, replacement)
675 installed_file.write(newline)
676 installed_file.close()
677 os.chmod(installed_location, 0755)
681 def install_file(bld, destdir, file, chmod=MODE_644, flat=False,
682 python_fixup=False, destname=None, base_name=None):
684 destdir = bld.EXPAND_VARIABLES(destdir)
688 destname = os.path.basename(destname)
689 dest = os.path.join(destdir, destname)
691 # fixup the python path it will use to find Samba modules
692 inst_file = file + '.inst'
693 bld.SAMBA_GENERATOR('python_%s' % destname,
694 rule=copy_and_fix_python_path,
699 file = os.path.join(base_name, file)
700 bld.install_as(dest, file, chmod=chmod)
703 def INSTALL_FILES(bld, destdir, files, chmod=MODE_644, flat=False,
704 python_fixup=False, destname=None, base_name=None):
705 '''install a set of files'''
706 for f in TO_LIST(files):
707 install_file(bld, destdir, f, chmod=chmod, flat=flat,
708 python_fixup=python_fixup, destname=destname,
710 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
713 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=MODE_644, flat=False,
714 python_fixup=False, exclude=None, trim_path=None):
715 '''install a set of files matching a wildcard pattern'''
716 files=TO_LIST(bld.path.ant_glob(pattern))
720 files2.append(os_path_relpath(f, trim_path))
725 if fnmatch.fnmatch(f, exclude):
727 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
728 python_fixup=python_fixup, base_name=trim_path)
729 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
732 def INSTALL_DIRS(bld, destdir, dirs):
733 '''install a set of directories'''
734 destdir = bld.EXPAND_VARIABLES(destdir)
735 dirs = bld.EXPAND_VARIABLES(dirs)
736 for d in TO_LIST(dirs):
737 bld.install_dir(os.path.join(destdir, d))
738 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
741 def MANPAGES(bld, manpages):
742 '''build and install manual pages'''
743 bld.env.MAN_XSL = 'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
744 for m in manpages.split():
746 bld.SAMBA_GENERATOR(m,
750 rule='${XSLTPROC} -o ${TGT} --nonet ${MAN_XSL} ${SRC}'
752 bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
753 Build.BuildContext.MANPAGES = MANPAGES
756 #############################################################
757 # give a nicer display when building different types of files
758 def progress_display(self, msg, fname):
759 col1 = Logs.colors(self.color)
760 col2 = Logs.colors.NORMAL
761 total = self.position[1]
763 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
764 return fs % (self.position[0], self.position[1], col1, fname, col2)
766 def link_display(self):
767 if Options.options.progress_bar != 0:
768 return Task.Task.old_display(self)
769 fname = self.outputs[0].bldpath(self.env)
770 return progress_display(self, 'Linking', fname)
771 Task.TaskBase.classes['cc_link'].display = link_display
773 def samba_display(self):
774 if Options.options.progress_bar != 0:
775 return Task.Task.old_display(self)
777 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
778 if self.name in targets:
779 target_type = targets[self.name]
780 type_map = { 'GENERATOR' : 'Generating',
781 'PROTOTYPE' : 'Generating'
783 if target_type in type_map:
784 return progress_display(self, type_map[target_type], self.name)
786 if len(self.inputs) == 0:
787 return Task.Task.old_display(self)
789 fname = self.inputs[0].bldpath(self.env)
790 if fname[0:3] == '../':
792 ext_loc = fname.rfind('.')
794 return Task.Task.old_display(self)
795 ext = fname[ext_loc:]
797 ext_map = { '.idl' : 'Compiling IDL',
798 '.et' : 'Compiling ERRTABLE',
799 '.asn1': 'Compiling ASN1',
802 return progress_display(self, ext_map[ext], fname)
803 return Task.Task.old_display(self)
805 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
806 Task.TaskBase.classes['Task'].display = samba_display
811 def apply_bundle_remove_dynamiclib_patch(self):
812 if self.env['MACBUNDLE'] or getattr(self,'mac_bundle',False):
813 if not getattr(self,'vnum',None):
815 self.env['LINKFLAGS'].remove('-dynamiclib')
816 self.env['LINKFLAGS'].remove('-single_module')