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