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
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 *
21 import samba_conftests
25 os.putenv('PYTHONUNBUFFERED', '1')
28 def SAMBA_BUILD_ENV(conf):
29 '''create the samba build environment'''
30 conf.env['BUILD_DIRECTORY'] = conf.blddir
31 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
32 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
33 # this allows all of the bin/shared and bin/python targets
34 # to be expressed in terms of build directory paths
35 for p in ['python','shared']:
36 link_target = os.path.join(conf.blddir, 'default/' + p)
37 if not os.path.lexists(link_target):
38 os.symlink('../' + p, link_target)
40 # get perl to put the blib files in the build directory
41 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
42 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
43 mkdir_p(blib_bld + '/man1')
44 mkdir_p(blib_bld + '/man3')
45 if not os.path.lexists(blib_src):
46 os.symlink(blib_bld, blib_src)
50 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
51 '''add an init_function to the list for a subsystem'''
52 if init_function is None:
54 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
55 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
56 if not subsystem in cache:
58 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
59 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
63 #################################################################
64 def SAMBA_LIBRARY(bld, libname, source,
73 external_library=False,
82 bundled_extension=True,
84 '''define a Samba library'''
87 SET_TARGET_TYPE(bld, libname, 'DISABLED')
90 source = bld.EXPAND_VARIABLES(source, vars=vars)
92 # remember empty libraries, so we can strip the dependencies
93 if (source == '') or (source == []):
94 SET_TARGET_TYPE(bld, libname, 'EMPTY')
97 if BUILTIN_LIBRARY(bld, libname):
100 obj_target = libname + '.objlist'
102 # first create a target for building the object files for this library
103 # by separating in this way, we avoid recompiling the C files
104 # separately for the install library and the build library
105 bld.SAMBA_SUBSYSTEM(obj_target,
108 public_deps = public_deps,
110 public_headers = public_headers,
111 header_path = header_path,
114 autoproto = autoproto,
115 depends_on = depends_on,
116 local_include = local_include)
118 if BUILTIN_LIBRARY(bld, libname):
121 if not SET_TARGET_TYPE(bld, libname, 'LIBRARY'):
124 # the library itself will depend on that object target
125 deps += ' ' + public_deps
127 deps.append(obj_target)
129 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
131 bld.SET_BUILD_GROUP(group)
133 features = 'cc cshlib symlink_lib',
135 target = bundled_name,
136 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
137 depends_on = depends_on,
139 samba_includes = includes,
140 local_include = local_include,
143 ldflags = build_rpath(bld),
147 if install_path is None:
148 install_path = '${LIBDIR}'
149 install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
151 # we don't need the double libraries if rpath is off
152 if (bld.env.RPATH_ON_INSTALL == False and
153 bld.env.RPATH_ON_BUILD == False):
154 install_target = bundled_name
156 install_target = bundled_name + '.inst'
158 if install and install_target != bundled_name:
159 # create a separate install library, which may have
160 # different rpath settings
161 SET_TARGET_TYPE(bld, install_target, 'LIBRARY')
163 features = 'cc cshlib',
165 target = install_target,
166 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
167 depends_on = depends_on,
169 samba_includes = includes,
170 local_include = local_include,
172 install_as = bundled_name,
174 ldflags = install_rpath(bld)
179 vnum_base = vnum.split('.')[0]
180 install_name = 'lib%s.so.%s' % (bundled_name, vnum)
181 install_link = 'lib%s.so.%s' % (bundled_name, vnum_base)
183 install_name = 'lib%s.so' % bundled_name
186 bld.install_as(os.path.join(install_path, install_name),
187 'lib%s.inst.so' % bundled_name)
189 bld.symlink_as(os.path.join(install_path, install_link), install_name)
191 if autoproto is not None:
192 bld.SAMBA_AUTOPROTO(autoproto, source)
194 if public_headers is not None:
195 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
197 if pc_files is not None:
198 bld.PKG_CONFIG_FILES(pc_files)
200 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
203 #################################################################
204 def SAMBA_BINARY(bld, binname, source,
224 '''define a Samba binary'''
226 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
229 features = 'cc cprogram'
231 features += ' pyembed'
233 bld.SET_BUILD_GROUP(group)
235 obj_target = binname + '.objlist'
237 source = bld.EXPAND_VARIABLES(source, vars=vars)
239 # first create a target for building the object files for this binary
240 # by separating in this way, we avoid recompiling the C files
241 # separately for the install binary and the build binary
242 bld.SAMBA_SUBSYSTEM(obj_target,
248 autoproto = autoproto,
249 subsystem_name = subsystem_name,
250 needs_python = needs_python,
251 local_include = local_include)
253 # the library itself will depend on that object target
255 deps.append(obj_target)
258 features = features + ' symlink_bin',
261 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
263 samba_includes = includes,
264 local_include = local_include,
265 samba_modules = modules,
267 samba_subsystem= subsystem_name,
269 ldflags = build_rpath(bld)
272 if install_path is None:
273 install_path = '${BINDIR}'
274 install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
276 # we don't need the double binaries if rpath is off
277 if (bld.env.RPATH_ON_INSTALL == False and
278 bld.env.RPATH_ON_BUILD == False):
279 install_target = binname
281 install_target = binname + '.inst'
283 if install and install_target != binname:
284 # we create a separate 'install' binary, which
285 # will have different rpath settings
286 SET_TARGET_TYPE(bld, install_target, 'BINARY')
290 target = install_target,
291 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
293 samba_includes = includes,
294 local_include = local_include,
295 samba_modules = modules,
297 samba_subsystem= subsystem_name,
299 ldflags = install_rpath(bld)
303 bld.install_as(os.path.join(install_path, binname),
307 # setup the subsystem_name as an alias for the real
308 # binary name, so it can be found when expanding
309 # subsystem dependencies
310 if subsystem_name is not None:
311 bld.TARGET_ALIAS(subsystem_name, binname)
313 if autoproto is not None:
314 bld.SAMBA_AUTOPROTO(autoproto, source)
315 if public_headers is not None:
316 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
317 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
320 #################################################################
321 def SAMBA_MODULE(bld, modname, source,
327 autoproto_extra_source='',
330 internal_module=True,
334 '''define a Samba module.'''
336 # we add the init function regardless of whether the module
337 # is enabled or not, as we need to generate a null list if
339 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
341 if internal_module or BUILTIN_LIBRARY(bld, modname):
342 # treat internal modules as subsystems for now
343 SAMBA_SUBSYSTEM(bld, modname, source,
347 autoproto_extra_source=autoproto_extra_source,
349 local_include=local_include,
354 SET_TARGET_TYPE(bld, modname, 'DISABLED')
357 source = bld.EXPAND_VARIABLES(source, vars=vars)
359 # remember empty modules, so we can strip the dependencies
360 if (source == '') or (source == []):
361 SET_TARGET_TYPE(bld, modname, 'EMPTY')
364 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
367 if subsystem is not None:
368 deps += ' ' + subsystem
370 bld.SET_BUILD_GROUP('main')
375 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
376 samba_includes = includes,
377 local_include = local_include,
378 samba_deps = TO_LIST(deps)
381 if autoproto is not None:
382 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
384 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
387 #################################################################
388 def SAMBA_SUBSYSTEM(bld, modname, source,
397 init_function_sentinal=None,
398 heimdal_autoproto=None,
399 heimdal_autoproto_options=None,
400 heimdal_autoproto_private=None,
402 autoproto_extra_source='',
405 local_include_first=True,
410 '''define a Samba subsystem'''
413 SET_TARGET_TYPE(bld, modname, 'DISABLED')
416 # remember empty subsystems, so we can strip the dependencies
417 if (source == '') or (source == []):
418 SET_TARGET_TYPE(bld, modname, 'EMPTY')
421 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
424 source = bld.EXPAND_VARIABLES(source, vars=vars)
426 deps += ' ' + public_deps
428 bld.SET_BUILD_GROUP(group)
438 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
439 depends_on = depends_on,
440 samba_deps = TO_LIST(deps),
441 samba_includes = includes,
442 local_include = local_include,
443 local_include_first = local_include_first,
444 samba_subsystem= subsystem_name
447 if cflags_end is not None:
448 t.samba_cflags.extend(TO_LIST(cflags_end))
450 if heimdal_autoproto is not None:
451 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
452 if heimdal_autoproto_private is not None:
453 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
454 if autoproto is not None:
455 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
456 if public_headers is not None:
457 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
461 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
464 def SAMBA_GENERATOR(bld, name, rule, source, target,
465 group='build_source', enabled=True,
469 '''A generic source generator target'''
471 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
477 bld.SET_BUILD_GROUP(group)
480 source=bld.EXPAND_VARIABLES(source, vars=vars),
482 shell=isinstance(rule, str),
488 if public_headers is not None:
489 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
491 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
495 def BUILD_SUBDIR(bld, dir):
496 '''add a new set of build rules from a subdirectory'''
497 path = os.path.normpath(bld.curdir + '/' + dir)
498 cache = LOCAL_CACHE(bld, 'SUBDIR_LIST')
499 if path in cache: return
501 debug("build: Processing subdirectory %s" % dir)
503 Build.BuildContext.BUILD_SUBDIR = BUILD_SUBDIR
508 def SETUP_BUILD_GROUPS(bld):
509 '''setup build groups used to ensure that the different build
510 phases happen consecutively'''
511 bld.p_ln = bld.srcnode # we do want to see all targets!
512 bld.env['USING_BUILD_GROUPS'] = True
513 bld.add_group('setup')
514 bld.add_group('build_compiler_source')
515 bld.add_group('base_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 ##############################
552 # handle the creation of links for libraries and binaries
553 # note that we use a relative symlink path to allow the whole tree
554 # to me moved/copied elsewhere without breaking the links
555 t = Task.simple_task_type('symlink_lib', 'rm -f ${LINK_TARGET} && ln -s ${LINK_SOURCE} ${LINK_TARGET}',
556 shell=True, color='PINK', ext_in='.bin')
559 @feature('symlink_lib')
561 def symlink_lib(self):
562 '''symlink a shared lib'''
563 tsk = self.create_task('symlink_lib', self.link_task.outputs[0])
565 # calculat the link target and put it in the environment
567 vnum = getattr(self, 'vnum', None)
569 soext = '.' + vnum.split('.')[0]
571 link_target = getattr(self, 'link_name', '')
572 if link_target == '':
573 link_target = '%s/lib%s.so%s' % (LIB_PATH, self.target, soext)
576 link_source = os_path_relpath(self.link_task.outputs[0].abspath(self.env),
577 os.path.join(self.env.BUILD_DIRECTORY, link_target))
579 tsk.env.LINK_TARGET = link_target
580 tsk.env.LINK_SOURCE = link_source[3:]
581 debug('task_gen: LINK for %s is %s -> %s',
582 self.name, tsk.env.LINK_SOURCE, tsk.env.LINK_TARGET)
585 t = Task.simple_task_type('symlink_bin', 'rm -f ${BIN_TARGET} && ln -s ${SRC} ${BIN_TARGET}',
586 shell=True, color='PINK', ext_in='.bin')
589 @feature('symlink_bin')
591 def symlink_bin(self):
592 '''symlink a binary'''
593 if Options.is_install:
594 # we don't want to copy the install binary, as
595 # that has the install rpath, not the build rpath
596 # The rpath of the binaries in bin/default/foo/blah is different
597 # during the install phase, as distros insist on not using rpath in installed binaries
599 tsk = self.create_task('symlink_bin', self.link_task.outputs[0])
601 tsk.env.BIN_TARGET = self.target
602 debug('task_gen: BIN_TARGET for %s is %s', self.name, tsk.env.BIN_TARGET)
607 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
608 shell=True, color='PINK', ext_in='.bin')
611 @feature('copy_script')
612 @before('apply_link')
613 def copy_script(self):
614 tsk = self.create_task('copy_script', self.allnodes[0])
615 tsk.env.TARGET = self.target
617 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
618 '''used to copy scripts from the source tree into the build directory
619 for use by selftest'''
621 source = bld.path.ant_glob(pattern)
623 bld.SET_BUILD_GROUP('build_source')
624 for s in TO_LIST(source):
626 if installname != None:
628 target = os.path.join(installdir, iname)
629 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
631 t = bld(features='copy_script',
636 t.env.LINK_TARGET = target
638 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
641 def install_file(bld, destdir, file, chmod=0644, flat=False,
642 python_fixup=False, destname=None):
644 destdir = bld.EXPAND_VARIABLES(destdir)
648 destname = os.path.basename(destname)
649 dest = os.path.join(destdir, destname)
651 # fixup the python path it will use to find Samba modules
652 inst_file = file + '.inst'
653 bld.SAMBA_GENERATOR('python_%s' % destname,
654 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
658 bld.install_as(dest, file, chmod=chmod)
661 def INSTALL_FILES(bld, destdir, files, chmod=0644, flat=False,
662 python_fixup=False, destname=None):
663 '''install a set of files'''
664 for f in TO_LIST(files):
665 install_file(bld, destdir, f, chmod=chmod, flat=flat,
666 python_fixup=python_fixup, destname=destname)
667 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
670 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=0644, flat=False,
671 python_fixup=False, exclude=None):
672 '''install a set of files matching a wildcard pattern'''
673 files=TO_LIST(bld.path.ant_glob(pattern))
676 if fnmatch.fnmatch(f, exclude):
678 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat, python_fixup=python_fixup)
679 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
682 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
683 '''install some headers
685 header_path may either be a string that is added to the INCLUDEDIR,
686 or it can be a dictionary of wildcard patterns which map to destination
687 directories relative to INCLUDEDIR
689 dest = '${INCLUDEDIR}'
690 if isinstance(header_path, str):
691 dest += '/' + header_path
692 for h in TO_LIST(public_headers):
694 if isinstance(header_path, list):
695 for (p1, dir) in header_path:
699 if fnmatch.fnmatch(h, p2):
701 hdest = os.path.join(hdest, dir)
704 if found_match: break
705 if h.find(':') != -1:
707 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
709 INSTALL_FILES(bld, hdest, h, flat=True)
710 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
713 def subst_at_vars(task):
714 '''substiture @VAR@ style variables in a file'''
715 src = task.inputs[0].srcpath(task.env)
716 tgt = task.outputs[0].bldpath(task.env)
722 a = re.split('(@\w+@)', s)
725 if re.match('@\w+@', v):
727 if not vname in task.env and vname.upper() in task.env:
728 vname = vname.upper()
729 if not vname in task.env:
730 print "Unknown substitution %s in %s" % (v, task.name)
734 contents = ''.join(out)
736 s = f.write(contents)
742 def PKG_CONFIG_FILES(bld, pc_files):
743 '''install some pkg_config pc files'''
744 dest = '${PKGCONFIGDIR}'
745 dest = bld.EXPAND_VARIABLES(dest)
746 for f in TO_LIST(pc_files):
747 base=os.path.basename(f)
748 bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
752 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
753 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
756 # override the display of the compilation and linking messages
758 return "[%d/%d]" % (self.position[0], self.position[1])
760 def cc_display(self):
761 if Options.options.progress_bar != 0:
762 return Task.Task.display(self)
763 fname = self.inputs[0].bldpath(self.env)[3:]
764 return "%s Compiling %s\n" % (progress(self), fname)
765 Task.TaskBase.classes['cc'].display = cc_display
767 def link_display(self):
768 if Options.options.progress_bar != 0:
769 return Task.Task.display(self)
770 fname = self.outputs[0].bldpath(self.env)
771 return "%s Linking %s\n" % (progress(self), fname)
772 Task.TaskBase.classes['cc_link'].display = link_display