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
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 os.path.islink(blib_src):
47 elif os.path.exists(blib_src):
48 shutil.rmtree(blib_src)
49 os.symlink(blib_bld, blib_src)
53 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
54 '''add an init_function to the list for a subsystem'''
55 if init_function is None:
57 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
58 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
59 if not subsystem in cache:
61 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
62 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
66 #################################################################
67 def SAMBA_LIBRARY(bld, libname, source,
76 external_library=False,
86 target_type='LIBRARY',
87 bundled_extension=True,
90 '''define a Samba library'''
93 SET_TARGET_TYPE(bld, libname, 'DISABLED')
96 source = bld.EXPAND_VARIABLES(source, vars=vars)
98 # remember empty libraries, so we can strip the dependencies
99 if (source == '') or (source == []):
100 SET_TARGET_TYPE(bld, libname, 'EMPTY')
103 if BUILTIN_LIBRARY(bld, libname):
106 obj_target = libname + '.objlist'
108 # first create a target for building the object files for this library
109 # by separating in this way, we avoid recompiling the C files
110 # separately for the install library and the build library
111 bld.SAMBA_SUBSYSTEM(obj_target,
114 public_deps = public_deps,
116 public_headers = public_headers,
117 header_path = header_path,
120 autoproto = autoproto,
121 depends_on = depends_on,
122 needs_python = needs_python,
123 local_include = local_include)
125 if BUILTIN_LIBRARY(bld, libname):
128 if not SET_TARGET_TYPE(bld, libname, target_type):
131 # the library itself will depend on that object target
132 deps += ' ' + public_deps
134 deps.append(obj_target)
137 bundled_name = libname
139 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
141 features = 'cc cshlib'
145 bld.SET_BUILD_GROUP(group)
147 features = features + ' symlink_lib',
149 target = bundled_name,
150 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
151 depends_on = depends_on,
153 samba_includes = includes,
154 local_include = local_include,
157 ldflags = build_rpath(bld),
162 t.link_name = link_name
164 if install_path is None:
165 install_path = '${LIBDIR}'
166 install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
168 # we don't need the double libraries if rpath is off
169 if (bld.env.RPATH_ON_INSTALL == False and
170 bld.env.RPATH_ON_BUILD == False):
171 install_target = bundled_name
173 install_target = bundled_name + '.inst'
175 if install and install_target != bundled_name:
176 # create a separate install library, which may have
177 # different rpath settings
178 SET_TARGET_TYPE(bld, install_target, target_type)
182 target = install_target,
183 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
184 depends_on = depends_on,
186 samba_includes = includes,
187 local_include = local_include,
189 install_as = bundled_name,
191 ldflags = install_rpath(bld)
196 install_name = realname
198 inst_name = install_target + '.so'
200 vnum_base = vnum.split('.')[0]
201 install_name = 'lib%s.so.%s' % (bundled_name, vnum)
202 install_link = 'lib%s.so.%s' % (bundled_name, vnum_base)
203 inst_name = 'lib%s.so' % install_target
205 install_name = 'lib%s.so' % bundled_name
207 inst_name = 'lib%s.so' % install_target
209 bld.install_as(os.path.join(install_path, install_name), inst_name)
211 bld.symlink_as(os.path.join(install_path, install_link), install_name)
213 if autoproto is not None:
214 bld.SAMBA_AUTOPROTO(autoproto, source)
216 if public_headers is not None:
217 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
219 if pc_files is not None:
220 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
222 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
225 #################################################################
226 def SAMBA_BINARY(bld, binname, source,
246 '''define a Samba binary'''
248 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
251 features = 'cc cprogram'
253 features += ' pyembed'
255 bld.SET_BUILD_GROUP(group)
257 obj_target = binname + '.objlist'
259 source = bld.EXPAND_VARIABLES(source, vars=vars)
261 # first create a target for building the object files for this binary
262 # by separating in this way, we avoid recompiling the C files
263 # separately for the install binary and the build binary
264 bld.SAMBA_SUBSYSTEM(obj_target,
270 autoproto = autoproto,
271 subsystem_name = subsystem_name,
272 needs_python = needs_python,
273 local_include = local_include)
275 # the library itself will depend on that object target
277 deps.append(obj_target)
280 features = features + ' symlink_bin',
283 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
285 samba_includes = includes,
286 local_include = local_include,
287 samba_modules = modules,
289 samba_subsystem= subsystem_name,
291 ldflags = build_rpath(bld)
294 if install_path is None:
295 install_path = '${BINDIR}'
296 install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
298 # we don't need the double binaries if rpath is off
299 if (bld.env.RPATH_ON_INSTALL == False and
300 bld.env.RPATH_ON_BUILD == False):
301 install_target = binname
303 install_target = binname + '.inst'
305 if install and install_target != binname:
306 # we create a separate 'install' binary, which
307 # will have different rpath settings
308 SET_TARGET_TYPE(bld, install_target, 'BINARY')
312 target = install_target,
313 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
315 samba_includes = includes,
316 local_include = local_include,
317 samba_modules = modules,
319 samba_subsystem= subsystem_name,
321 ldflags = install_rpath(bld)
325 bld.install_as(os.path.join(install_path, binname),
329 # setup the subsystem_name as an alias for the real
330 # binary name, so it can be found when expanding
331 # subsystem dependencies
332 if subsystem_name is not None:
333 bld.TARGET_ALIAS(subsystem_name, binname)
335 if autoproto is not None:
336 bld.SAMBA_AUTOPROTO(autoproto, source)
337 if public_headers is not None:
338 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
339 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
342 #################################################################
343 def SAMBA_MODULE(bld, modname, source,
349 autoproto_extra_source='',
352 internal_module=True,
356 '''define a Samba module.'''
358 # we add the init function regardless of whether the module
359 # is enabled or not, as we need to generate a null list if
361 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
363 if internal_module or BUILTIN_LIBRARY(bld, modname):
364 # treat internal modules as subsystems for now
365 SAMBA_SUBSYSTEM(bld, modname, source,
369 autoproto_extra_source=autoproto_extra_source,
371 local_include=local_include,
376 SET_TARGET_TYPE(bld, modname, 'DISABLED')
379 source = bld.EXPAND_VARIABLES(source, vars=vars)
381 # remember empty modules, so we can strip the dependencies
382 if (source == '') or (source == []):
383 SET_TARGET_TYPE(bld, modname, 'EMPTY')
386 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
389 if subsystem is not None:
390 deps += ' ' + subsystem
392 bld.SET_BUILD_GROUP('main')
397 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
398 samba_includes = includes,
399 local_include = local_include,
400 samba_deps = TO_LIST(deps)
403 if autoproto is not None:
404 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
406 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
409 #################################################################
410 def SAMBA_SUBSYSTEM(bld, modname, source,
419 init_function_sentinal=None,
420 heimdal_autoproto=None,
421 heimdal_autoproto_options=None,
422 heimdal_autoproto_private=None,
424 autoproto_extra_source='',
427 local_include_first=True,
432 '''define a Samba subsystem'''
435 SET_TARGET_TYPE(bld, modname, 'DISABLED')
438 # remember empty subsystems, so we can strip the dependencies
439 if (source == '') or (source == []):
440 SET_TARGET_TYPE(bld, modname, 'EMPTY')
443 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
446 source = bld.EXPAND_VARIABLES(source, vars=vars)
448 deps += ' ' + public_deps
450 bld.SET_BUILD_GROUP(group)
460 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
461 depends_on = depends_on,
462 samba_deps = TO_LIST(deps),
463 samba_includes = includes,
464 local_include = local_include,
465 local_include_first = local_include_first,
466 samba_subsystem= subsystem_name
469 if cflags_end is not None:
470 t.samba_cflags.extend(TO_LIST(cflags_end))
472 if heimdal_autoproto is not None:
473 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
474 if heimdal_autoproto_private is not None:
475 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
476 if autoproto is not None:
477 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
478 if public_headers is not None:
479 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
483 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
486 def SAMBA_GENERATOR(bld, name, rule, source, target,
487 group='build_source', enabled=True,
491 '''A generic source generator target'''
493 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
499 bld.SET_BUILD_GROUP(group)
502 source=bld.EXPAND_VARIABLES(source, vars=vars),
504 shell=isinstance(rule, str),
510 if public_headers is not None:
511 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
513 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
517 def BUILD_SUBDIR(bld, dir):
518 '''add a new set of build rules from a subdirectory'''
519 path = os.path.normpath(bld.curdir + '/' + dir)
520 cache = LOCAL_CACHE(bld, 'SUBDIR_LIST')
521 if path in cache: return
523 debug("build: Processing subdirectory %s" % dir)
525 Build.BuildContext.BUILD_SUBDIR = BUILD_SUBDIR
530 def SETUP_BUILD_GROUPS(bld):
531 '''setup build groups used to ensure that the different build
532 phases happen consecutively'''
533 bld.p_ln = bld.srcnode # we do want to see all targets!
534 bld.env['USING_BUILD_GROUPS'] = True
535 bld.add_group('setup')
536 bld.add_group('build_compiler_source')
537 bld.add_group('base_libraries')
538 bld.add_group('build_compilers')
539 bld.add_group('build_source')
540 bld.add_group('prototypes')
541 bld.add_group('main')
542 bld.add_group('binaries')
543 bld.add_group('final')
544 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
547 def SET_BUILD_GROUP(bld, group):
548 '''set the current build group'''
549 if not 'USING_BUILD_GROUPS' in bld.env:
552 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
557 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
558 """use timestamps instead of file contents for deps
559 this currently doesn't work"""
560 def h_file(filename):
562 st = os.stat(filename)
563 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
565 m.update(str(st.st_mtime))
566 m.update(str(st.st_size))
569 Utils.h_file = h_file
573 ##############################
574 # handle the creation of links for libraries and binaries
575 # note that we use a relative symlink path to allow the whole tree
576 # to me moved/copied elsewhere without breaking the links
577 t = Task.simple_task_type('symlink_lib', 'rm -f ${LINK_TARGET} && ln -s ${LINK_SOURCE} ${LINK_TARGET}',
578 shell=True, color='PINK', ext_in='.bin')
581 @feature('symlink_lib')
583 def symlink_lib(self):
584 '''symlink a shared lib'''
585 tsk = self.create_task('symlink_lib', self.link_task.outputs[0])
587 # calculat the link target and put it in the environment
589 vnum = getattr(self, 'vnum', None)
591 soext = '.' + vnum.split('.')[0]
593 link_target = getattr(self, 'link_name', '')
594 if link_target == '':
595 link_target = '%s/lib%s.so%s' % (LIB_PATH, self.target, soext)
598 link_source = os_path_relpath(self.link_task.outputs[0].abspath(self.env),
599 os.path.join(self.env.BUILD_DIRECTORY, link_target))
601 tsk.env.LINK_TARGET = link_target
602 tsk.env.LINK_SOURCE = link_source[3:]
603 debug('task_gen: LINK for %s is %s -> %s',
604 self.name, tsk.env.LINK_SOURCE, tsk.env.LINK_TARGET)
607 t = Task.simple_task_type('symlink_bin', 'rm -f ${BIN_TARGET} && ln -s ${SRC} ${BIN_TARGET}',
608 shell=True, color='PINK', ext_in='.bin')
611 @feature('symlink_bin')
613 def symlink_bin(self):
614 '''symlink a binary'''
615 if Options.is_install:
616 # we don't want to copy the install binary, as
617 # that has the install rpath, not the build rpath
618 # The rpath of the binaries in bin/default/foo/blah is different
619 # during the install phase, as distros insist on not using rpath in installed binaries
621 tsk = self.create_task('symlink_bin', self.link_task.outputs[0])
623 tsk.env.BIN_TARGET = self.target
624 debug('task_gen: BIN_TARGET for %s is %s', self.name, tsk.env.BIN_TARGET)
629 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
630 shell=True, color='PINK', ext_in='.bin')
633 @feature('copy_script')
634 @before('apply_link')
635 def copy_script(self):
636 tsk = self.create_task('copy_script', self.allnodes[0])
637 tsk.env.TARGET = self.target
639 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
640 '''used to copy scripts from the source tree into the build directory
641 for use by selftest'''
643 source = bld.path.ant_glob(pattern)
645 bld.SET_BUILD_GROUP('build_source')
646 for s in TO_LIST(source):
648 if installname != None:
650 target = os.path.join(installdir, iname)
651 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
653 t = bld(features='copy_script',
658 t.env.LINK_TARGET = target
660 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
663 def install_file(bld, destdir, file, chmod=0644, flat=False,
664 python_fixup=False, destname=None):
666 destdir = bld.EXPAND_VARIABLES(destdir)
670 destname = os.path.basename(destname)
671 dest = os.path.join(destdir, destname)
673 # fixup the python path it will use to find Samba modules
674 inst_file = file + '.inst'
675 bld.SAMBA_GENERATOR('python_%s' % destname,
676 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
680 bld.install_as(dest, file, chmod=chmod)
683 def INSTALL_FILES(bld, destdir, files, chmod=0644, flat=False,
684 python_fixup=False, destname=None):
685 '''install a set of files'''
686 for f in TO_LIST(files):
687 install_file(bld, destdir, f, chmod=chmod, flat=flat,
688 python_fixup=python_fixup, destname=destname)
689 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
692 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=0644, flat=False,
693 python_fixup=False, exclude=None):
694 '''install a set of files matching a wildcard pattern'''
695 files=TO_LIST(bld.path.ant_glob(pattern))
698 if fnmatch.fnmatch(f, exclude):
700 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat, python_fixup=python_fixup)
701 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
704 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
705 '''install some headers
707 header_path may either be a string that is added to the INCLUDEDIR,
708 or it can be a dictionary of wildcard patterns which map to destination
709 directories relative to INCLUDEDIR
711 dest = '${INCLUDEDIR}'
712 if isinstance(header_path, str):
713 dest += '/' + header_path
714 for h in TO_LIST(public_headers):
716 if isinstance(header_path, list):
717 for (p1, dir) in header_path:
721 if fnmatch.fnmatch(h, p2):
723 hdest = os.path.join(hdest, dir)
726 if found_match: break
727 if h.find(':') != -1:
729 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
731 INSTALL_FILES(bld, hdest, h, flat=True)
732 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
735 def subst_at_vars(task):
736 '''substiture @VAR@ style variables in a file'''
737 src = task.inputs[0].srcpath(task.env)
738 tgt = task.outputs[0].bldpath(task.env)
744 a = re.split('(@\w+@)', s)
747 if re.match('@\w+@', v):
749 if not vname in task.env and vname.upper() in task.env:
750 vname = vname.upper()
751 if not vname in task.env:
752 print "Unknown substitution %s in %s" % (v, task.name)
756 contents = ''.join(out)
758 s = f.write(contents)
764 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
765 '''install some pkg_config pc files'''
766 dest = '${PKGCONFIGDIR}'
767 dest = bld.EXPAND_VARIABLES(dest)
768 for f in TO_LIST(pc_files):
769 base=os.path.basename(f)
770 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
775 t.env.PACKAGE_VERSION = vnum
776 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
777 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
781 #############################################################
782 # give a nicer display when building different types of files
783 def progress_display(self, msg, fname):
784 col1 = Logs.colors(self.color)
785 col2 = Logs.colors.NORMAL
786 total = self.position[1]
788 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
789 return fs % (self.position[0], self.position[1], col1, fname, col2)
791 def link_display(self):
792 if Options.options.progress_bar != 0:
793 return Task.Task.old_display(self)
794 fname = self.outputs[0].bldpath(self.env)
795 return progress_display(self, 'Linking', fname)
796 Task.TaskBase.classes['cc_link'].display = link_display
798 def samba_display(self):
799 if Options.options.progress_bar != 0:
800 return Task.Task.old_display(self)
801 fname = self.inputs[0].bldpath(self.env)
802 if fname[0:3] == '../':
804 ext_loc = fname.rfind('.')
806 return Task.Task.old_display(self)
807 ext = fname[ext_loc:]
809 ext_map = { '.idl' : 'Compiling IDL',
810 '.et' : 'Compiling ERRTABLE',
811 '.asn1': 'Compiling ASN1',
814 return progress_display(self, ext_map[ext], fname)
815 return Task.Task.old_display(self)
817 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
818 Task.TaskBase.classes['Task'].display = samba_display