build: support variable expansion in source= arguments to build rules
[abartlet/samba.git/.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
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
21 LIB_PATH="shared"
22
23 os.putenv('PYTHONUNBUFFERED', '1')
24
25 #################################################################
26 # create the samba build environment
27 @conf
28 def SAMBA_BUILD_ENV(conf):
29     conf.env['BUILD_DIRECTORY'] = conf.blddir
30     mkdir_p(os.path.join(conf.blddir, LIB_PATH))
31     mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
32     # this allows all of the bin/shared and bin/python targets
33     # to be expressed in terms of build directory paths
34     for p in ['python','shared']:
35         link_target = os.path.join(conf.blddir, 'default/' + p)
36         if not os.path.lexists(link_target):
37             os.symlink('../' + p, link_target)
38
39
40
41 ################################################################
42 # add an init_function to the list for a subsystem
43 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
44     if init_function is None:
45         return
46     bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
47     cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
48     if not subsystem in cache:
49         cache[subsystem] = []
50     cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
51 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
52
53
54 #################################################################
55 # define a Samba library
56 def SAMBA_LIBRARY(bld, libname, source,
57                   deps='',
58                   public_deps='',
59                   includes='',
60                   public_headers=None,
61                   vnum=None,
62                   cflags='',
63                   external_library=False,
64                   realname=None,
65                   autoproto=None,
66                   group='main',
67                   depends_on='',
68                   local_include=True,
69                   vars=None,
70                   install_path=None,
71                   install=True,
72                   enabled=True):
73
74     if not enabled:
75         SET_TARGET_TYPE(bld, libname, 'DISABLED')
76         return
77
78     source = bld.EXPAND_VARIABLES(source, vars=vars)
79
80     # remember empty libraries, so we can strip the dependencies
81     if (source == '') or (source == []):
82         SET_TARGET_TYPE(bld, libname, 'EMPTY')
83         return
84
85     if bld.env.DISABLE_SHARED:
86         obj_target = libname
87     else:
88         obj_target = libname + '.objlist'
89
90     # first create a target for building the object files for this library
91     # by separating in this way, we avoid recompiling the C files
92     # separately for the install library and the build library
93     bld.SAMBA_SUBSYSTEM(obj_target,
94                         source         = source,
95                         deps           = deps,
96                         public_deps    = public_deps,
97                         includes       = includes,
98                         public_headers = public_headers,
99                         cflags         = cflags,
100                         group          = group,
101                         autoproto      = autoproto,
102                         depends_on     = depends_on,
103                         local_include  = local_include)
104
105     if bld.env.DISABLE_SHARED:
106         return
107
108     if not SET_TARGET_TYPE(bld, libname, 'LIBRARY'):
109         return
110
111     # the library itself will depend on that object target
112     deps += ' ' + public_deps
113     deps = TO_LIST(deps)
114     deps.append(obj_target)
115
116     bld.SET_BUILD_GROUP(group)
117     t = bld(
118         features        = 'cc cshlib symlink_lib',
119         source          = [],
120         target          = libname,
121         samba_cflags    = CURRENT_CFLAGS(bld, libname, cflags),
122         depends_on      = depends_on,
123         samba_deps      = deps,
124         samba_includes  = includes,
125         local_include   = local_include,
126         vnum            = vnum,
127         install_path    = None,
128         ldflags         = build_rpath(bld)
129         )
130
131     if install_path is None:
132         install_path = '${LIBDIR}'
133     install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
134
135     # we don't need the double libraries if rpath is off
136     if (bld.env.RPATH_ON_INSTALL == False and
137         bld.env.RPATH_ON_BUILD == False):
138         install_target = libname
139     else:
140         install_target = libname + '.inst'
141
142     if install and install_target != libname:
143         # create a separate install library, which may have
144         # different rpath settings
145         SET_TARGET_TYPE(bld, install_target, 'LIBRARY')
146         t = bld(
147             features        = 'cc cshlib',
148             source          = [],
149             target          = install_target,
150             samba_cflags    = CURRENT_CFLAGS(bld, libname, cflags),
151             depends_on      = depends_on,
152             samba_deps      = deps,
153             samba_includes  = includes,
154             local_include   = local_include,
155             vnum            = vnum,
156             install_as      = libname,
157             install_path    = None,
158             ldflags         = install_rpath(bld)
159             )
160
161     if install:
162         if vnum:
163             vnum_base = vnum.split('.')[0]
164             install_name = 'lib%s.so.%s' % (libname, vnum)
165             install_link = 'lib%s.so.%s' % (libname, vnum_base)
166         else:
167             install_name = 'lib%s.so' % libname
168             install_link = None
169
170         bld.install_as(os.path.join(install_path, install_name),
171                        'lib%s.inst.so' % libname)
172         if install_link:
173             bld.symlink_as(os.path.join(install_path, install_link), install_name)
174
175     if autoproto is not None:
176         bld.SAMBA_AUTOPROTO(autoproto, source)
177
178 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
179
180
181 #################################################################
182 # define a Samba binary
183 def SAMBA_BINARY(bld, binname, source,
184                  deps='',
185                  includes='',
186                  public_headers=None,
187                  modules=None,
188                  installdir=None,
189                  ldflags=None,
190                  cflags='',
191                  autoproto=None,
192                  use_hostcc=None,
193                  compiler=None,
194                  group='binaries',
195                  manpages=None,
196                  local_include=True,
197                  subsystem_name=None,
198                  needs_python=False,
199                  vars=None,
200                  install=True,
201                  install_path=None):
202
203     if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
204         return
205
206     features = 'cc cprogram'
207     if needs_python:
208         features += ' pyembed'
209
210     bld.SET_BUILD_GROUP(group)
211
212     obj_target = binname + '.objlist'
213
214     source = bld.EXPAND_VARIABLES(source, vars=vars)
215
216     # first create a target for building the object files for this binary
217     # by separating in this way, we avoid recompiling the C files
218     # separately for the install binary and the build binary
219     bld.SAMBA_SUBSYSTEM(obj_target,
220                         source         = source,
221                         deps           = deps,
222                         includes       = includes,
223                         cflags         = cflags,
224                         group          = group,
225                         autoproto      = autoproto,
226                         subsystem_name = subsystem_name,
227                         needs_python   = needs_python,
228                         local_include  = local_include)
229
230     # the library itself will depend on that object target
231     deps = TO_LIST(deps)
232     deps.append(obj_target)
233
234     bld(
235         features       = features + ' symlink_bin',
236         source         = [],
237         target         = binname,
238         samba_cflags   = CURRENT_CFLAGS(bld, binname, cflags),
239         samba_deps     = deps,
240         samba_includes = includes,
241         local_include  = local_include,
242         samba_modules  = modules,
243         top            = True,
244         samba_subsystem= subsystem_name,
245         install_path   = None,
246         ldflags        = build_rpath(bld)
247         )
248
249     if install_path is None:
250         install_path = '${BINDIR}'
251     install_path = SUBST_VARS_RECURSIVE(install_path, bld.env)
252
253     # we don't need the double binaries if rpath is off
254     if (bld.env.RPATH_ON_INSTALL == False and
255         bld.env.RPATH_ON_BUILD == False):
256         install_target = binname
257     else:
258         install_target = binname + '.inst'
259
260     if install and install_target != binname:
261         # we create a separate 'install' binary, which
262         # will have different rpath settings
263         SET_TARGET_TYPE(bld, install_target, 'BINARY')
264         t = bld(
265             features       = features,
266             source         = [],
267             target         = install_target,
268             samba_cflags   = CURRENT_CFLAGS(bld, binname, cflags),
269             samba_deps     = deps,
270             samba_includes = includes,
271             local_include  = local_include,
272             samba_modules  = modules,
273             top            = True,
274             samba_subsystem= subsystem_name,
275             install_path   = None,
276             ldflags        = install_rpath(bld)
277             )
278
279     if install:
280         bld.install_as(os.path.join(install_path, binname),
281                        install_target,
282                        chmod=0755)
283
284     # setup the subsystem_name as an alias for the real
285     # binary name, so it can be found when expanding
286     # subsystem dependencies
287     if subsystem_name is not None:
288         bld.TARGET_ALIAS(subsystem_name, binname)
289
290     if autoproto is not None:
291         bld.SAMBA_AUTOPROTO(autoproto, source)
292 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
293
294
295 #################################################################
296 # define a Samba module.
297 def SAMBA_MODULE(bld, modname, source,
298                  deps='',
299                  includes='',
300                  subsystem=None,
301                  init_function=None,
302                  autoproto=None,
303                  autoproto_extra_source='',
304                  aliases=None,
305                  cflags='',
306                  internal_module=True,
307                  local_include=True,
308                  vars=None,
309                  enabled=True):
310
311     # we add the init function regardless of whether the module
312     # is enabled or not, as we need to generate a null list if
313     # all disabled
314     bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
315
316     if internal_module or bld.env.DISABLE_SHARED:
317         # treat internal modules as subsystems for now
318         SAMBA_SUBSYSTEM(bld, modname, source,
319                         deps=deps,
320                         includes=includes,
321                         autoproto=autoproto,
322                         autoproto_extra_source=autoproto_extra_source,
323                         cflags=cflags,
324                         local_include=local_include,
325                         enabled=enabled)
326         return
327
328     if not enabled:
329         SET_TARGET_TYPE(bld, modname, 'DISABLED')
330         return
331
332     source = bld.EXPAND_VARIABLES(source, vars=vars)
333
334     # remember empty modules, so we can strip the dependencies
335     if (source == '') or (source == []):
336         SET_TARGET_TYPE(bld, modname, 'EMPTY')
337         return
338
339     if not SET_TARGET_TYPE(bld, modname, 'MODULE'):
340         return
341
342     if subsystem is not None:
343         deps += ' ' + subsystem
344
345     bld.SET_BUILD_GROUP('main')
346     bld(
347         features       = 'cc',
348         source         = source,
349         target         = modname,
350         samba_cflags   = CURRENT_CFLAGS(bld, modname, cflags),
351         samba_includes = includes,
352         local_include  = local_include,
353         samba_deps     = TO_LIST(deps)
354         )
355
356     if autoproto is not None:
357         bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
358
359 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
360
361
362 #################################################################
363 # define a Samba subsystem
364 def SAMBA_SUBSYSTEM(bld, modname, source,
365                     deps='',
366                     public_deps='',
367                     includes='',
368                     public_headers=None,
369                     cflags='',
370                     cflags_end=None,
371                     group='main',
372                     init_function_sentinal=None,
373                     heimdal_autoproto=None,
374                     heimdal_autoproto_options=None,
375                     heimdal_autoproto_private=None,
376                     autoproto=None,
377                     autoproto_extra_source='',
378                     depends_on='',
379                     local_include=True,
380                     local_include_first=True,
381                     subsystem_name=None,
382                     enabled=True,
383                     vars=None,
384                     needs_python=False):
385
386     if not enabled:
387         SET_TARGET_TYPE(bld, modname, 'DISABLED')
388         return
389
390     # remember empty subsystems, so we can strip the dependencies
391     if (source == '') or (source == []):
392         SET_TARGET_TYPE(bld, modname, 'EMPTY')
393         return
394
395     if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
396         return
397
398     source = bld.EXPAND_VARIABLES(source, vars=vars)
399
400     deps += ' ' + public_deps
401
402     bld.SET_BUILD_GROUP(group)
403
404     features = 'cc'
405     if needs_python:
406         features += ' pyext'
407
408     t = bld(
409         features       = features,
410         source         = source,
411         target         = modname,
412         samba_cflags   = CURRENT_CFLAGS(bld, modname, cflags),
413         depends_on     = depends_on,
414         samba_deps     = TO_LIST(deps),
415         samba_includes = includes,
416         local_include  = local_include,
417         local_include_first  = local_include_first,
418         samba_subsystem= subsystem_name
419         )
420
421     if cflags_end is not None:
422         t.samba_cflags.extend(TO_LIST(cflags_end))
423
424     if heimdal_autoproto is not None:
425         bld.HEIMDAL_AUTOPROTO(heimdal_autoproto, source, options=heimdal_autoproto_options)
426     if heimdal_autoproto_private is not None:
427         bld.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private, source)
428     if autoproto is not None:
429         bld.SAMBA_AUTOPROTO(autoproto, source + ' ' + autoproto_extra_source)
430     return t
431
432 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
433
434
435 def SAMBA_GENERATOR(bld, name, rule, source, target,
436                     group='build_source', enabled=True,
437                     vars=None):
438     '''A generic source generator target'''
439
440     if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
441         return
442
443     if not enabled:
444         return
445
446     bld.SET_BUILD_GROUP(group)
447     bld(
448         rule=rule,
449         source=bld.EXPAND_VARIABLES(source, vars=vars),
450         target=target,
451         shell=True,
452         on_results=True,
453         before='cc',
454         ext_out='.c',
455         name=name)
456 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
457
458
459
460 ###############################################################
461 # add a new set of build rules from a subdirectory
462 # the @runonce decorator ensures we don't end up
463 # with duplicate rules
464 def BUILD_SUBDIR(bld, dir):
465     path = os.path.normpath(bld.curdir + '/' + dir)
466     cache = LOCAL_CACHE(bld, 'SUBDIR_LIST')
467     if path in cache: return
468     cache[path] = True
469     debug("build: Processing subdirectory %s" % dir)
470     bld.add_subdirs(dir)
471
472 Build.BuildContext.BUILD_SUBDIR = BUILD_SUBDIR
473
474
475 ##########################################################
476 # add a new top level command to waf
477 def ADD_COMMAND(opt, name, function):
478     Utils.g_module.__dict__[name] = function
479     opt.name = function
480 Options.Handler.ADD_COMMAND = ADD_COMMAND
481
482 ###########################################################
483 # setup build groups used to ensure that the different build
484 # phases happen consecutively
485 @runonce
486 def SETUP_BUILD_GROUPS(bld):
487     bld.p_ln = bld.srcnode # we do want to see all targets!
488     bld.env['USING_BUILD_GROUPS'] = True
489     bld.add_group('setup')
490     bld.add_group('build_compiler_source')
491     bld.add_group('base_libraries')
492     bld.add_group('build_compilers')
493     bld.add_group('build_source')
494     bld.add_group('prototypes')
495     bld.add_group('main')
496     bld.add_group('binaries')
497     bld.add_group('final')
498 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
499
500
501 ###########################################################
502 # set the current build group
503 def SET_BUILD_GROUP(bld, group):
504     if not 'USING_BUILD_GROUPS' in bld.env:
505         return
506     bld.set_group(group)
507 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
508
509
510 def h_file(filename):
511     import stat
512     st = os.stat(filename)
513     if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
514     m = Utils.md5()
515     m.update(str(st.st_mtime))
516     m.update(str(st.st_size))
517     m.update(filename)
518     return m.digest()
519
520 @conf
521 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
522     Utils.h_file = h_file
523
524
525 ##############################
526 # handle the creation of links for libraries and binaries
527 # note that we use a relative symlink path to allow the whole tree
528 # to me moved/copied elsewhere without breaking the links
529 t = Task.simple_task_type('symlink_lib', 'rm -f ${LINK_TARGET} && ln -s ${LINK_SOURCE} ${LINK_TARGET}',
530                           shell=True, color='PINK', ext_in='.bin')
531 t.quiet = True
532
533 @feature('symlink_lib')
534 @after('apply_link')
535 def symlink_lib(self):
536     tsk = self.create_task('symlink_lib', self.link_task.outputs[0])
537
538     # calculat the link target and put it in the environment
539     soext=""
540     vnum = getattr(self, 'vnum', None)
541     if vnum is not None:
542         soext = '.' + vnum.split('.')[0]
543
544     link_target = getattr(self, 'link_name', '')
545     if link_target == '':
546         link_target = '%s/lib%s.so%s' % (LIB_PATH, self.sname, soext)
547
548
549     link_source = os_path_relpath(self.link_task.outputs[0].abspath(self.env),
550                                   os.path.join(self.env.BUILD_DIRECTORY, link_target))
551
552     tsk.env.LINK_TARGET = link_target
553     tsk.env.LINK_SOURCE = link_source[3:]
554     debug('task_gen: LINK for %s is %s -> %s',
555           self.name, tsk.env.LINK_SOURCE, tsk.env.LINK_TARGET)
556
557
558 t = Task.simple_task_type('symlink_bin', 'rm -f ${BIN_TARGET} && ln -s ${SRC} ${BIN_TARGET}',
559                           shell=True, color='PINK', ext_in='.bin')
560 t.quiet = True
561
562 @feature('symlink_bin')
563 @after('apply_link')
564 def symlink_bin(self):
565     if Options.is_install:
566         # we don't want to copy the install binary, as
567         # that has the install rpath, not the build rpath
568         # The rpath of the binaries in bin/default/foo/blah is different
569         # during the install phase, as distros insist on not using rpath in installed binaries
570         return
571     tsk = self.create_task('symlink_bin', self.link_task.outputs[0])
572
573     tsk.env.BIN_TARGET = self.target
574     debug('task_gen: BIN_TARGET for %s is %s', self.name, tsk.env.BIN_TARGET)
575
576
577
578
579 t = Task.simple_task_type('copy_script', 'rm -f && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
580                           shell=True, color='PINK', ext_in='.bin')
581 t.quiet = True
582
583 @feature('copy_script')
584 @before('apply_link')
585 def copy_script(self):
586     tsk = self.create_task('copy_script', self.allnodes[0])
587     tsk.env.TARGET = self.target
588
589 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
590     '''used to copy scripts from the source tree into the build directory
591        for use by selftest'''
592
593     source = bld.path.ant_glob(pattern)
594
595     bld.SET_BUILD_GROUP('build_source')
596     for s in TO_LIST(source):
597         iname = s
598         if installname != None:
599             iname = installname
600         target = os.path.join(installdir, iname)
601         tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
602         mkdir_p(tgtdir)
603         t = bld(features='copy_script',
604                 source       = s,
605                 target       = target,
606                 always       = True,
607                 install_path = None)
608         t.env.LINK_TARGET = target
609
610 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
611