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