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 import samba_conftests
24 os.putenv('PYTHONUNBUFFERED', '1')
26 #################################################################
27 # create the samba build environment
29 def SAMBA_BUILD_ENV(conf):
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 ################################################################
51 # add an init_function to the list for a subsystem
52 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
53 if init_function is None:
55 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
56 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
57 if not subsystem in cache:
59 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
60 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
63 #################################################################
64 # define a Samba library
65 def SAMBA_LIBRARY(bld, libname, source,
74 external_library=False,
86 SET_TARGET_TYPE(bld, libname, 'DISABLED')
89 source = bld.EXPAND_VARIABLES(source, vars=vars)
91 # remember empty libraries, so we can strip the dependencies
92 if (source == '') or (source == []):
93 SET_TARGET_TYPE(bld, libname, 'EMPTY')
96 if bld.env.DISABLE_SHARED:
99 obj_target = libname + '.objlist'
101 # first create a target for building the object files for this library
102 # by separating in this way, we avoid recompiling the C files
103 # separately for the install library and the build library
104 bld.SAMBA_SUBSYSTEM(obj_target,
107 public_deps = public_deps,
109 public_headers = public_headers,
110 header_path = header_path,
113 autoproto = autoproto,
114 depends_on = depends_on,
115 local_include = local_include)
117 if bld.env.DISABLE_SHARED:
120 if not SET_TARGET_TYPE(bld, libname, 'LIBRARY'):
123 # the library itself will depend on that object target
124 deps += ' ' + public_deps
126 deps.append(obj_target)
128 bld.SET_BUILD_GROUP(group)
130 features = 'cc cshlib symlink_lib',
133 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
134 depends_on = depends_on,
136 samba_includes = includes,
137 local_include = local_include,
140 ldflags = build_rpath(bld)
143 if install_path is None:
144 install_path = '${LIBDIR}'
145 install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
147 # we don't need the double libraries if rpath is off
148 if (bld.env.RPATH_ON_INSTALL == False and
149 bld.env.RPATH_ON_BUILD == False):
150 install_target = libname
152 install_target = libname + '.inst'
154 if install and install_target != libname:
155 # create a separate install library, which may have
156 # different rpath settings
157 SET_TARGET_TYPE(bld, install_target, 'LIBRARY')
159 features = 'cc cshlib',
161 target = install_target,
162 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
163 depends_on = depends_on,
165 samba_includes = includes,
166 local_include = local_include,
168 install_as = libname,
170 ldflags = install_rpath(bld)
175 vnum_base = vnum.split('.')[0]
176 install_name = 'lib%s.so.%s' % (libname, vnum)
177 install_link = 'lib%s.so.%s' % (libname, vnum_base)
179 install_name = 'lib%s.so' % libname
182 bld.install_as(os.path.join(install_path, install_name),
183 'lib%s.inst.so' % libname)
185 bld.symlink_as(os.path.join(install_path, install_link), install_name)
187 if autoproto is not None:
188 bld.SAMBA_AUTOPROTO(autoproto, source)
190 if public_headers is not None:
191 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
193 if pc_files is not None:
194 bld.PKG_CONFIG_FILES(pc_files)
196 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
199 #################################################################
200 # define a Samba binary
201 def SAMBA_BINARY(bld, binname, source,
222 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
225 features = 'cc cprogram'
227 features += ' pyembed'
229 bld.SET_BUILD_GROUP(group)
231 obj_target = binname + '.objlist'
233 source = bld.EXPAND_VARIABLES(source, vars=vars)
235 # first create a target for building the object files for this binary
236 # by separating in this way, we avoid recompiling the C files
237 # separately for the install binary and the build binary
238 bld.SAMBA_SUBSYSTEM(obj_target,
244 autoproto = autoproto,
245 subsystem_name = subsystem_name,
246 needs_python = needs_python,
247 local_include = local_include)
249 # the library itself will depend on that object target
251 deps.append(obj_target)
254 features = features + ' symlink_bin',
257 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
259 samba_includes = includes,
260 local_include = local_include,
261 samba_modules = modules,
263 samba_subsystem= subsystem_name,
265 ldflags = build_rpath(bld)
268 if install_path is None:
269 install_path = '${BINDIR}'
270 install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
272 # we don't need the double binaries if rpath is off
273 if (bld.env.RPATH_ON_INSTALL == False and
274 bld.env.RPATH_ON_BUILD == False):
275 install_target = binname
277 install_target = binname + '.inst'
279 if install and install_target != binname:
280 # we create a separate 'install' binary, which
281 # will have different rpath settings
282 SET_TARGET_TYPE(bld, install_target, 'BINARY')
286 target = install_target,
287 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
289 samba_includes = includes,
290 local_include = local_include,
291 samba_modules = modules,
293 samba_subsystem= subsystem_name,
295 ldflags = install_rpath(bld)
299 bld.install_as(os.path.join(install_path, binname),
303 # setup the subsystem_name as an alias for the real
304 # binary name, so it can be found when expanding
305 # subsystem dependencies
306 if subsystem_name is not None:
307 bld.TARGET_ALIAS(subsystem_name, binname)
309 if autoproto is not None:
310 bld.SAMBA_AUTOPROTO(autoproto, source)
311 if public_headers is not None:
312 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
313 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
316 #################################################################
317 # define a Samba module.
318 def SAMBA_MODULE(bld, modname, source,
324 autoproto_extra_source='',
327 internal_module=True,
332 # we add the init function regardless of whether the module
333 # is enabled or not, as we need to generate a null list if
335 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
337 if internal_module or bld.env.DISABLE_SHARED:
338 # treat internal modules as subsystems for now
339 SAMBA_SUBSYSTEM(bld, modname, source,
343 autoproto_extra_source=autoproto_extra_source,
345 local_include=local_include,
350 SET_TARGET_TYPE(bld, modname, 'DISABLED')
353 source = bld.EXPAND_VARIABLES(source, vars=vars)
355 # remember empty modules, so we can strip the dependencies
356 if (source == '') or (source == []):
357 SET_TARGET_TYPE(bld, modname, 'EMPTY')
360 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
363 if subsystem is not None:
364 deps += ' ' + subsystem
366 bld.SET_BUILD_GROUP('main')
371 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
372 samba_includes = includes,
373 local_include = local_include,
374 samba_deps = TO_LIST(deps)
377 if autoproto is not None:
378 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
380 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
383 #################################################################
384 # define a Samba subsystem
385 def SAMBA_SUBSYSTEM(bld, modname, source,
394 init_function_sentinal=None,
395 heimdal_autoproto=None,
396 heimdal_autoproto_options=None,
397 heimdal_autoproto_private=None,
399 autoproto_extra_source='',
402 local_include_first=True,
409 SET_TARGET_TYPE(bld, modname, 'DISABLED')
412 # remember empty subsystems, so we can strip the dependencies
413 if (source == '') or (source == []):
414 SET_TARGET_TYPE(bld, modname, 'EMPTY')
417 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
420 source = bld.EXPAND_VARIABLES(source, vars=vars)
422 deps += ' ' + public_deps
424 bld.SET_BUILD_GROUP(group)
434 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
435 depends_on = depends_on,
436 samba_deps = TO_LIST(deps),
437 samba_includes = includes,
438 local_include = local_include,
439 local_include_first = local_include_first,
440 samba_subsystem= subsystem_name
443 if cflags_end is not None:
444 t.samba_cflags.extend(TO_LIST(cflags_end))
446 if heimdal_autoproto is not None:
447 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
448 if heimdal_autoproto_private is not None:
449 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
450 if autoproto is not None:
451 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
452 if public_headers is not None:
453 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
457 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
460 def SAMBA_GENERATOR(bld, name, rule, source, target,
461 group='build_source', enabled=True,
465 '''A generic source generator target'''
467 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
473 bld.SET_BUILD_GROUP(group)
476 source=bld.EXPAND_VARIABLES(source, vars=vars),
478 shell=isinstance(rule, str),
484 if public_headers is not None:
485 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
487 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
491 ###############################################################
492 # add a new set of build rules from a subdirectory
493 # the @runonce decorator ensures we don't end up
494 # with duplicate rules
495 def BUILD_SUBDIR(bld, dir):
496 path = os.path.normpath(bld.curdir + '/' + dir)
497 cache = LOCAL_CACHE(bld, 'SUBDIR_LIST')
498 if path in cache: return
500 debug("build: Processing subdirectory %s" % dir)
503 Build.BuildContext.BUILD_SUBDIR = BUILD_SUBDIR
506 ##########################################################
507 # add a new top level command to waf
508 def ADD_COMMAND(opt, name, function):
509 Utils.g_module.__dict__[name] = function
511 Options.Handler.ADD_COMMAND = ADD_COMMAND
513 ###########################################################
514 # setup build groups used to ensure that the different build
515 # phases happen consecutively
517 def SETUP_BUILD_GROUPS(bld):
518 bld.p_ln = bld.srcnode # we do want to see all targets!
519 bld.env['USING_BUILD_GROUPS'] = True
520 bld.add_group('setup')
521 bld.add_group('build_compiler_source')
522 bld.add_group('base_libraries')
523 bld.add_group('build_compilers')
524 bld.add_group('build_source')
525 bld.add_group('prototypes')
526 bld.add_group('main')
527 bld.add_group('binaries')
528 bld.add_group('final')
529 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
532 ###########################################################
533 # set the current build group
534 def SET_BUILD_GROUP(bld, group):
535 if not 'USING_BUILD_GROUPS' in bld.env:
538 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
541 def h_file(filename):
543 st = os.stat(filename)
544 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
546 m.update(str(st.st_mtime))
547 m.update(str(st.st_size))
552 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
553 Utils.h_file = h_file
556 ##############################
557 # handle the creation of links for libraries and binaries
558 # note that we use a relative symlink path to allow the whole tree
559 # to me moved/copied elsewhere without breaking the links
560 t = Task.simple_task_type('symlink_lib', 'rm -f ${LINK_TARGET} && ln -s ${LINK_SOURCE} ${LINK_TARGET}',
561 shell=True, color='PINK', ext_in='.bin')
564 @feature('symlink_lib')
566 def symlink_lib(self):
567 tsk = self.create_task('symlink_lib', self.link_task.outputs[0])
569 # calculat the link target and put it in the environment
571 vnum = getattr(self, 'vnum', None)
573 soext = '.' + vnum.split('.')[0]
575 link_target = getattr(self, 'link_name', '')
576 if link_target == '':
577 link_target = '%s/lib%s.so%s' % (LIB_PATH, self.sname, soext)
580 link_source = os_path_relpath(self.link_task.outputs[0].abspath(self.env),
581 os.path.join(self.env.BUILD_DIRECTORY, link_target))
583 tsk.env.LINK_TARGET = link_target
584 tsk.env.LINK_SOURCE = link_source[3:]
585 debug('task_gen: LINK for %s is %s -> %s',
586 self.name, tsk.env.LINK_SOURCE, tsk.env.LINK_TARGET)
589 t = Task.simple_task_type('symlink_bin', 'rm -f ${BIN_TARGET} && ln -s ${SRC} ${BIN_TARGET}',
590 shell=True, color='PINK', ext_in='.bin')
593 @feature('symlink_bin')
595 def symlink_bin(self):
596 if Options.is_install:
597 # we don't want to copy the install binary, as
598 # that has the install rpath, not the build rpath
599 # The rpath of the binaries in bin/default/foo/blah is different
600 # during the install phase, as distros insist on not using rpath in installed binaries
602 tsk = self.create_task('symlink_bin', self.link_task.outputs[0])
604 tsk.env.BIN_TARGET = self.target
605 debug('task_gen: BIN_TARGET for %s is %s', self.name, tsk.env.BIN_TARGET)
610 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
611 shell=True, color='PINK', ext_in='.bin')
614 @feature('copy_script')
615 @before('apply_link')
616 def copy_script(self):
617 tsk = self.create_task('copy_script', self.allnodes[0])
618 tsk.env.TARGET = self.target
620 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
621 '''used to copy scripts from the source tree into the build directory
622 for use by selftest'''
624 source = bld.path.ant_glob(pattern)
626 bld.SET_BUILD_GROUP('build_source')
627 for s in TO_LIST(source):
629 if installname != None:
631 target = os.path.join(installdir, iname)
632 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
634 t = bld(features='copy_script',
639 t.env.LINK_TARGET = target
641 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
644 def install_file(bld, destdir, file, chmod=0644, flat=False,
645 python_fixup=False, destname=None):
647 destdir = bld.EXPAND_VARIABLES(destdir)
651 destname = os.path.basename(destname)
652 dest = os.path.join(destdir, destname)
654 # fixup the python path it will use to find Samba modules
655 inst_file = file + '.inst'
656 bld.SAMBA_GENERATOR('python_%s' % destname,
657 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
661 bld.install_as(dest, file, chmod=chmod)
664 def INSTALL_FILES(bld, destdir, files, chmod=0644, flat=False,
665 python_fixup=False, destname=None):
666 '''install a set of files'''
667 for f in TO_LIST(files):
668 install_file(bld, destdir, f, chmod=chmod, flat=flat,
669 python_fixup=python_fixup, destname=destname)
670 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
673 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=0644, flat=False,
674 python_fixup=False, exclude=None):
675 '''install a set of files matching a wildcard pattern'''
676 files=TO_LIST(bld.path.ant_glob(pattern))
679 if fnmatch.fnmatch(f, exclude):
681 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat, python_fixup=python_fixup)
682 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
685 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
686 '''install some headers
688 header_path may either be a string that is added to the INCLUDEDIR,
689 or it can be a dictionary of wildcard patterns which map to destination
690 directories relative to INCLUDEDIR
692 dest = '${INCLUDEDIR}'
693 if isinstance(header_path, str):
694 dest += '/' + header_path
695 for h in TO_LIST(public_headers):
697 if isinstance(header_path, list):
698 for (p1, dir) in header_path:
702 if fnmatch.fnmatch(h, p2):
704 hdest = os.path.join(hdest, dir)
707 if found_match: break
708 if h.find(':') != -1:
710 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
712 INSTALL_FILES(bld, hdest, h, flat=True)
713 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
716 def subst_at_vars(task):
717 '''substiture @VAR@ style variables in a file'''
718 src = task.inputs[0].srcpath(task.env)
719 tgt = task.outputs[0].bldpath(task.env)
725 a = re.split('(@\w+@)', s)
728 if re.match('@\w+@', v):
730 if not vname in task.env and vname.upper() in task.env:
731 vname = vname.upper()
732 if not vname in task.env:
733 print "Unknown substitution %s in %s" % (v, task.name)
737 contents = ''.join(out)
739 s = f.write(contents)
745 def PKG_CONFIG_FILES(bld, pc_files):
746 '''install some pkg_config pc files'''
747 dest = '${PKGCONFIGDIR}'
748 dest = bld.EXPAND_VARIABLES(dest)
749 for f in TO_LIST(pc_files):
750 base=os.path.basename(f)
751 bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
755 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
756 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES