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