build: make the 'wrong version of waf' message even clearer
[Samba/gebeck_regimport.git] / buildtools / wafsamba / wafsamba.py
blobdd9cbd604cca9f8e5b7b55e368fc671f3a67c0e0
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, Constants
5 from Configure import conf
6 from Logs import debug
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_install
22 import samba_conftests
23 import tru64cc
24 import irixcc
25 import generic_cc
26 import samba_dist
28 O644 = 420
30 # some systems have broken threading in python
31 if os.environ.get('WAF_NOTHREADS') == '1':
32 import nothreads
34 LIB_PATH="shared"
36 os.putenv('PYTHONUNBUFFERED', '1')
39 if Constants.HEXVERSION < 0x105016:
40 Logs.error('''
41 Please use the version of waf that comes with Samba, not
42 a system installed version. See http://wiki.samba.org/index.php/Waf
43 for details.
45 Alternatively, please use ./autogen-waf.sh, and then
46 run ./configure and make as usual. That will call the right version of waf.
47 ''')
48 sys.exit(1)
51 @conf
52 def SAMBA_BUILD_ENV(conf):
53 '''create the samba build environment'''
54 conf.env.BUILD_DIRECTORY = conf.blddir
55 mkdir_p(os.path.join(conf.blddir, LIB_PATH))
56 mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
57 # this allows all of the bin/shared and bin/python targets
58 # to be expressed in terms of build directory paths
59 for p in ['python','shared']:
60 link_target = os.path.join(conf.blddir, 'default/' + p)
61 if not os.path.lexists(link_target):
62 os.symlink('../' + p, link_target)
64 # get perl to put the blib files in the build directory
65 blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
66 blib_src = os.path.join(conf.srcdir, 'pidl/blib')
67 mkdir_p(blib_bld + '/man1')
68 mkdir_p(blib_bld + '/man3')
69 if os.path.islink(blib_src):
70 os.unlink(blib_src)
71 elif os.path.exists(blib_src):
72 shutil.rmtree(blib_src)
75 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
76 '''add an init_function to the list for a subsystem'''
77 if init_function is None:
78 return
79 bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
80 cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
81 if not subsystem in cache:
82 cache[subsystem] = []
83 cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
84 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
88 #################################################################
89 def SAMBA_LIBRARY(bld, libname, source,
90 deps='',
91 public_deps='',
92 includes='',
93 public_headers=None,
94 header_path=None,
95 pc_files=None,
96 vnum=None,
97 cflags='',
98 external_library=False,
99 realname=None,
100 autoproto=None,
101 group='main',
102 depends_on='',
103 local_include=True,
104 vars=None,
105 install_path=None,
106 install=True,
107 needs_python=False,
108 target_type='LIBRARY',
109 bundled_extension=True,
110 link_name=None,
111 enabled=True):
112 '''define a Samba library'''
114 if not enabled:
115 SET_TARGET_TYPE(bld, libname, 'DISABLED')
116 return
118 source = bld.EXPAND_VARIABLES(source, vars=vars)
120 # remember empty libraries, so we can strip the dependencies
121 if (source == '') or (source == []):
122 SET_TARGET_TYPE(bld, libname, 'EMPTY')
123 return
125 if target_type != 'PYTHON' and BUILTIN_LIBRARY(bld, libname):
126 obj_target = libname
127 else:
128 obj_target = libname + '.objlist'
130 # first create a target for building the object files for this library
131 # by separating in this way, we avoid recompiling the C files
132 # separately for the install library and the build library
133 bld.SAMBA_SUBSYSTEM(obj_target,
134 source = source,
135 deps = deps,
136 public_deps = public_deps,
137 includes = includes,
138 public_headers = public_headers,
139 header_path = header_path,
140 cflags = cflags,
141 group = group,
142 autoproto = autoproto,
143 depends_on = depends_on,
144 needs_python = needs_python,
145 local_include = local_include)
147 if libname == obj_target:
148 return
150 if not SET_TARGET_TYPE(bld, libname, target_type):
151 return
153 # the library itself will depend on that object target
154 deps += ' ' + public_deps
155 deps = TO_LIST(deps)
156 deps.append(obj_target)
158 if target_type == 'PYTHON':
159 bundled_name = libname
160 else:
161 bundled_name = BUNDLED_NAME(bld, libname, bundled_extension)
163 features = 'cc cshlib symlink_lib install_lib'
164 if target_type == 'PYTHON':
165 features += ' pyext'
166 elif needs_python:
167 features += ' pyembed'
169 bld.SET_BUILD_GROUP(group)
170 t = bld(
171 features = features,
172 source = [],
173 target = bundled_name,
174 samba_cflags = CURRENT_CFLAGS(bld, libname, cflags),
175 depends_on = depends_on,
176 samba_deps = deps,
177 samba_includes = includes,
178 local_include = local_include,
179 vnum = vnum,
180 install_path = None,
181 samba_inst_path = install_path,
182 name = libname,
183 samba_realname = realname,
184 samba_install = install
187 if link_name:
188 t.link_name = link_name
190 if autoproto is not None:
191 bld.SAMBA_AUTOPROTO(autoproto, source)
193 if public_headers is not None:
194 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
196 if pc_files is not None:
197 bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
199 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
202 #################################################################
203 def SAMBA_BINARY(bld, binname, source,
204 deps='',
205 includes='',
206 public_headers=None,
207 header_path=None,
208 modules=None,
209 installdir=None,
210 ldflags=None,
211 cflags='',
212 autoproto=None,
213 use_hostcc=False,
214 use_global_deps=True,
215 compiler=None,
216 group='binaries',
217 manpages=None,
218 local_include=True,
219 subsystem_name=None,
220 needs_python=False,
221 vars=None,
222 install=True,
223 install_path=None):
224 '''define a Samba binary'''
226 if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
227 return
229 features = 'cc cprogram symlink_bin install_bin'
230 if needs_python:
231 features += ' pyembed'
233 obj_target = binname + '.objlist'
235 source = bld.EXPAND_VARIABLES(source, vars=vars)
237 # first create a target for building the object files for this binary
238 # by separating in this way, we avoid recompiling the C files
239 # separately for the install binary and the build binary
240 bld.SAMBA_SUBSYSTEM(obj_target,
241 source = source,
242 deps = deps,
243 includes = includes,
244 cflags = cflags,
245 group = group,
246 autoproto = autoproto,
247 subsystem_name = subsystem_name,
248 needs_python = needs_python,
249 local_include = local_include,
250 use_hostcc = use_hostcc,
251 use_global_deps= use_global_deps)
253 bld.SET_BUILD_GROUP(group)
255 # the binary itself will depend on that object target
256 deps = TO_LIST(deps)
257 deps.append(obj_target)
259 t = bld(
260 features = features,
261 source = [],
262 target = binname,
263 samba_cflags = CURRENT_CFLAGS(bld, binname, cflags),
264 samba_deps = deps,
265 samba_includes = includes,
266 local_include = local_include,
267 samba_modules = modules,
268 top = True,
269 samba_subsystem= subsystem_name,
270 install_path = None,
271 samba_inst_path= install_path,
272 samba_install = install
275 # setup the subsystem_name as an alias for the real
276 # binary name, so it can be found when expanding
277 # subsystem dependencies
278 if subsystem_name is not None:
279 bld.TARGET_ALIAS(subsystem_name, binname)
281 if autoproto is not None:
282 bld.SAMBA_AUTOPROTO(autoproto, source)
283 if public_headers is not None:
284 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
285 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
288 #################################################################
289 def SAMBA_MODULE(bld, modname, source,
290 deps='',
291 includes='',
292 subsystem=None,
293 init_function=None,
294 autoproto=None,
295 autoproto_extra_source='',
296 aliases=None,
297 cflags='',
298 internal_module=True,
299 local_include=True,
300 vars=None,
301 enabled=True):
302 '''define a Samba module.'''
304 # we add the init function regardless of whether the module
305 # is enabled or not, as we need to generate a null list if
306 # all disabled
307 bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
309 if internal_module or BUILTIN_LIBRARY(bld, modname):
310 # treat internal modules as subsystems for now
311 SAMBA_SUBSYSTEM(bld, modname, source,
312 deps=deps,
313 includes=includes,
314 autoproto=autoproto,
315 autoproto_extra_source=autoproto_extra_source,
316 cflags=cflags,
317 local_include=local_include,
318 enabled=enabled)
319 return
321 if not enabled:
322 SET_TARGET_TYPE(bld, modname, 'DISABLED')
323 return
325 source = bld.EXPAND_VARIABLES(source, vars=vars)
327 # remember empty modules, so we can strip the dependencies
328 if (source == '') or (source == []):
329 SET_TARGET_TYPE(bld, modname, 'EMPTY')
330 return
332 if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
333 return
335 if subsystem is not None:
336 deps += ' ' + subsystem
338 bld.SET_BUILD_GROUP('main')
339 bld(
340 features = 'cc',
341 source = source,
342 target = modname,
343 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
344 samba_includes = includes,
345 local_include = local_include,
346 samba_deps = TO_LIST(deps)
349 if autoproto is not None:
350 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
352 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
355 #################################################################
356 def SAMBA_SUBSYSTEM(bld, modname, source,
357 deps='',
358 public_deps='',
359 includes='',
360 public_headers=None,
361 header_path=None,
362 cflags='',
363 cflags_end=None,
364 group='main',
365 init_function_sentinal=None,
366 heimdal_autoproto=None,
367 heimdal_autoproto_options=None,
368 heimdal_autoproto_private=None,
369 autoproto=None,
370 autoproto_extra_source='',
371 depends_on='',
372 local_include=True,
373 local_include_first=True,
374 subsystem_name=None,
375 enabled=True,
376 use_hostcc=False,
377 use_global_deps=True,
378 vars=None,
379 needs_python=False):
380 '''define a Samba subsystem'''
382 if not enabled:
383 SET_TARGET_TYPE(bld, modname, 'DISABLED')
384 return
386 # remember empty subsystems, so we can strip the dependencies
387 if (source == '') or (source == []):
388 SET_TARGET_TYPE(bld, modname, 'EMPTY')
389 return
391 if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
392 return
394 source = bld.EXPAND_VARIABLES(source, vars=vars)
396 deps += ' ' + public_deps
398 bld.SET_BUILD_GROUP(group)
400 features = 'cc'
401 if needs_python:
402 features += ' pyext'
404 t = bld(
405 features = features,
406 source = source,
407 target = modname,
408 samba_cflags = CURRENT_CFLAGS(bld, modname, cflags),
409 depends_on = depends_on,
410 samba_deps = TO_LIST(deps),
411 samba_includes = includes,
412 local_include = local_include,
413 local_include_first = local_include_first,
414 samba_subsystem= subsystem_name,
415 samba_use_hostcc = use_hostcc,
416 samba_use_global_deps = use_global_deps
419 if cflags_end is not None:
420 t.samba_cflags.extend(TO_LIST(cflags_end))
422 if heimdal_autoproto is not None:
423 bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
424 if heimdal_autoproto_private is not None:
425 bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
426 if autoproto is not None:
427 bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
428 if public_headers is not None:
429 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
430 return t
433 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
436 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
437 group='generators', enabled=True,
438 public_headers=None,
439 header_path=None,
440 vars=None):
441 '''A generic source generator target'''
443 if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
444 return
446 if not enabled:
447 return
449 bld.SET_BUILD_GROUP(group)
450 t = bld(
451 rule=rule,
452 source=bld.EXPAND_VARIABLES(source, vars=vars),
453 target=target,
454 shell=isinstance(rule, str),
455 on_results=True,
456 before='cc',
457 ext_out='.c',
458 name=name)
460 if public_headers is not None:
461 bld.PUBLIC_HEADERS(public_headers, header_path=header_path)
462 return t
463 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
467 @runonce
468 def SETUP_BUILD_GROUPS(bld):
469 '''setup build groups used to ensure that the different build
470 phases happen consecutively'''
471 bld.p_ln = bld.srcnode # we do want to see all targets!
472 bld.env['USING_BUILD_GROUPS'] = True
473 bld.add_group('setup')
474 bld.add_group('build_compiler_source')
475 bld.add_group('base_libraries')
476 bld.add_group('generators')
477 bld.add_group('compiler_prototypes')
478 bld.add_group('compiler_libraries')
479 bld.add_group('build_compilers')
480 bld.add_group('build_source')
481 bld.add_group('prototypes')
482 bld.add_group('main')
483 bld.add_group('binaries')
484 bld.add_group('final')
485 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
488 def SET_BUILD_GROUP(bld, group):
489 '''set the current build group'''
490 if not 'USING_BUILD_GROUPS' in bld.env:
491 return
492 bld.set_group(group)
493 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
497 @conf
498 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
499 """use timestamps instead of file contents for deps
500 this currently doesn't work"""
501 def h_file(filename):
502 import stat
503 st = os.stat(filename)
504 if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
505 m = Utils.md5()
506 m.update(str(st.st_mtime))
507 m.update(str(st.st_size))
508 m.update(filename)
509 return m.digest()
510 Utils.h_file = h_file
514 t = Task.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
515 shell=True, color='PINK', ext_in='.bin')
516 t.quiet = True
518 @feature('copy_script')
519 @before('apply_link')
520 def copy_script(self):
521 tsk = self.create_task('copy_script', self.allnodes[0])
522 tsk.env.TARGET = self.target
524 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
525 '''used to copy scripts from the source tree into the build directory
526 for use by selftest'''
528 source = bld.path.ant_glob(pattern)
530 bld.SET_BUILD_GROUP('build_source')
531 for s in TO_LIST(source):
532 iname = s
533 if installname != None:
534 iname = installname
535 target = os.path.join(installdir, iname)
536 tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
537 mkdir_p(tgtdir)
538 t = bld(features='copy_script',
539 source = s,
540 target = target,
541 always = True,
542 install_path = None)
543 t.env.LINK_TARGET = target
545 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
548 def install_file(bld, destdir, file, chmod=O644, flat=False,
549 python_fixup=False, destname=None, base_name=None):
550 '''install a file'''
551 destdir = bld.EXPAND_VARIABLES(destdir)
552 if not destname:
553 destname = file
554 if flat:
555 destname = os.path.basename(destname)
556 dest = os.path.join(destdir, destname)
557 if python_fixup:
558 # fixup the python path it will use to find Samba modules
559 inst_file = file + '.inst'
560 bld.SAMBA_GENERATOR('python_%s' % destname,
561 rule="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
562 source=file,
563 target=inst_file)
564 file = inst_file
565 if base_name:
566 file = os.path.join(base_name, file)
567 bld.install_as(dest, file, chmod=chmod)
570 def INSTALL_FILES(bld, destdir, files, chmod=O644, flat=False,
571 python_fixup=False, destname=None, base_name=None):
572 '''install a set of files'''
573 for f in TO_LIST(files):
574 install_file(bld, destdir, f, chmod=chmod, flat=flat,
575 python_fixup=python_fixup, destname=destname,
576 base_name=base_name)
577 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
580 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=O644, flat=False,
581 python_fixup=False, exclude=None, trim_path=None):
582 '''install a set of files matching a wildcard pattern'''
583 files=TO_LIST(bld.path.ant_glob(pattern))
584 if trim_path:
585 files2 = []
586 for f in files:
587 files2.append(os_path_relpath(f, trim_path))
588 files = files2
590 if exclude:
591 for f in files[:]:
592 if fnmatch.fnmatch(f, exclude):
593 files.remove(f)
594 INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
595 python_fixup=python_fixup, base_name=trim_path)
596 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
599 def PUBLIC_HEADERS(bld, public_headers, header_path=None):
600 '''install some headers
602 header_path may either be a string that is added to the INCLUDEDIR,
603 or it can be a dictionary of wildcard patterns which map to destination
604 directories relative to INCLUDEDIR
606 dest = '${INCLUDEDIR}'
607 if isinstance(header_path, str):
608 dest += '/' + header_path
609 for h in TO_LIST(public_headers):
610 hdest = dest
611 if isinstance(header_path, list):
612 for (p1, dir) in header_path:
613 found_match=False
614 lst = TO_LIST(p1)
615 for p2 in lst:
616 if fnmatch.fnmatch(h, p2):
617 if dir:
618 hdest = os.path.join(hdest, dir)
619 found_match=True
620 break
621 if found_match: break
622 if h.find(':') != -1:
623 hs=h.split(':')
624 INSTALL_FILES(bld, hdest, hs[0], flat=True, destname=hs[1])
625 else:
626 INSTALL_FILES(bld, hdest, h, flat=True)
627 Build.BuildContext.PUBLIC_HEADERS = PUBLIC_HEADERS
630 def subst_at_vars(task):
631 '''substiture @VAR@ style variables in a file'''
632 src = task.inputs[0].srcpath(task.env)
633 tgt = task.outputs[0].bldpath(task.env)
635 f = open(src, 'r')
636 s = f.read()
637 f.close()
638 # split on the vars
639 a = re.split('(@\w+@)', s)
640 out = []
641 back_sub = [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
642 for v in a:
643 if re.match('@\w+@', v):
644 vname = v[1:-1]
645 if not vname in task.env and vname.upper() in task.env:
646 vname = vname.upper()
647 if not vname in task.env:
648 Logs.error("Unknown substitution %s in %s" % (v, task.name))
649 sys.exit(1)
650 v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
651 # now we back substitute the allowed pc vars
652 for (b, m) in back_sub:
653 s = task.env[b]
654 if s == v[0:len(s)]:
655 v = m + v[len(s):]
656 out.append(v)
657 contents = ''.join(out)
658 f = open(tgt, 'w')
659 s = f.write(contents)
660 f.close()
661 return 0
665 def PKG_CONFIG_FILES(bld, pc_files, vnum=None):
666 '''install some pkg_config pc files'''
667 dest = '${PKGCONFIGDIR}'
668 dest = bld.EXPAND_VARIABLES(dest)
669 for f in TO_LIST(pc_files):
670 base=os.path.basename(f)
671 t = bld.SAMBA_GENERATOR('PKGCONFIG_%s' % base,
672 rule=subst_at_vars,
673 source=f+'.in',
674 target=f)
675 if vnum:
676 t.env.PACKAGE_VERSION = vnum
677 INSTALL_FILES(bld, dest, f, flat=True, destname=base)
678 Build.BuildContext.PKG_CONFIG_FILES = PKG_CONFIG_FILES
682 #############################################################
683 # give a nicer display when building different types of files
684 def progress_display(self, msg, fname):
685 col1 = Logs.colors(self.color)
686 col2 = Logs.colors.NORMAL
687 total = self.position[1]
688 n = len(str(total))
689 fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
690 return fs % (self.position[0], self.position[1], col1, fname, col2)
692 def link_display(self):
693 if Options.options.progress_bar != 0:
694 return Task.Task.old_display(self)
695 fname = self.outputs[0].bldpath(self.env)
696 return progress_display(self, 'Linking', fname)
697 Task.TaskBase.classes['cc_link'].display = link_display
699 def samba_display(self):
700 if Options.options.progress_bar != 0:
701 return Task.Task.old_display(self)
703 targets = LOCAL_CACHE(self, 'TARGET_TYPE')
704 if self.name in targets:
705 target_type = targets[self.name]
706 type_map = { 'GENERATOR' : 'Generating',
707 'PROTOTYPE' : 'Generating'
709 if target_type in type_map:
710 return progress_display(self, type_map[target_type], self.name)
712 fname = self.inputs[0].bldpath(self.env)
713 if fname[0:3] == '../':
714 fname = fname[3:]
715 ext_loc = fname.rfind('.')
716 if ext_loc == -1:
717 return Task.Task.old_display(self)
718 ext = fname[ext_loc:]
720 ext_map = { '.idl' : 'Compiling IDL',
721 '.et' : 'Compiling ERRTABLE',
722 '.asn1': 'Compiling ASN1',
723 '.c' : 'Compiling' }
724 if ext in ext_map:
725 return progress_display(self, ext_map[ext], fname)
726 return Task.Task.old_display(self)
728 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
729 Task.TaskBase.classes['Task'].display = samba_display