wafsamba: allow an optional dep_vars list to be passed to SAMBA_GENERATOR()
[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, sys, 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 TaskGen.task_gen.apply_verif = Utils.nada
9
10 # bring in the other samba modules
11 from samba_optimisation import *
12 from samba_utils import *
13 from samba_version import *
14 from samba_autoconf import *
15 from samba_patterns import *
16 from samba_pidl import *
17 from samba_autoproto import *
18 from samba_python import *
19 from samba_deps import *
20 from samba_bundled import *
21 from samba_third_party import *
22 import samba_install
23 import samba_conftests
24 import samba_abi
25 import samba_headers
26 import tru64cc
27 import irixcc
28 import hpuxcc
29 import generic_cc
30 import samba_dist
31 import samba_wildcard
32 import stale_files
33 import symbols
34 import pkgconfig
35 import configure_file
36
37 # some systems have broken threading in python
38 if os.environ.get('WAF_NOTHREADS') == '1':
39     import nothreads
40
41 LIB_PATH="shared"
42
43 os.environ['PYTHONUNBUFFERED'] = '1'
44
45
46 if Constants.HEXVERSION < 0x105019:
47     Logs.error('''
48 Please use the version of waf that comes with Samba, not
49 a system installed version. See http://wiki.samba.org/index.php/Waf
50 for details.
51
52 Alternatively, please run ./configure and make as usual. That will
53 call the right version of waf.''')
54     sys.exit(1)
55
56
57 @conf
58 def SAMBA_BUILD_ENV(conf):
59     '''create the samba build environment'''
60     conf.env.BUILD_DIRECTORY = conf.blddir
61     mkdir_p(os.path.join(conf.blddir, LIB_PATH))
62     mkdir_p(os.path.join(conf.blddir, LIB_PATH, "private"))
63     mkdir_p(os.path.join(conf.blddir, "modules"))
64     mkdir_p(os.path.join(conf.blddir, 'python/samba/dcerpc'))
65     # this allows all of the bin/shared and bin/python targets
66     # to be expressed in terms of build directory paths
67     mkdir_p(os.path.join(conf.blddir, 'default'))
68     for (source, target) in [('shared', 'shared'), ('modules', 'modules'), ('python', 'python_modules')]:
69         link_target = os.path.join(conf.blddir, 'default/' + target)
70         if not os.path.lexists(link_target):
71             os.symlink('../' + source, link_target)
72
73     # get perl to put the blib files in the build directory
74     blib_bld = os.path.join(conf.blddir, 'default/pidl/blib')
75     blib_src = os.path.join(conf.srcdir, 'pidl/blib')
76     mkdir_p(blib_bld + '/man1')
77     mkdir_p(blib_bld + '/man3')
78     if os.path.islink(blib_src):
79         os.unlink(blib_src)
80     elif os.path.exists(blib_src):
81         shutil.rmtree(blib_src)
82
83
84 def ADD_INIT_FUNCTION(bld, subsystem, target, init_function):
85     '''add an init_function to the list for a subsystem'''
86     if init_function is None:
87         return
88     bld.ASSERT(subsystem is not None, "You must specify a subsystem for init_function '%s'" % init_function)
89     cache = LOCAL_CACHE(bld, 'INIT_FUNCTIONS')
90     if not subsystem in cache:
91         cache[subsystem] = []
92     cache[subsystem].append( { 'TARGET':target, 'INIT_FUNCTION':init_function } )
93 Build.BuildContext.ADD_INIT_FUNCTION = ADD_INIT_FUNCTION
94
95
96
97 #################################################################
98 def SAMBA_LIBRARY(bld, libname, source,
99                   deps='',
100                   public_deps='',
101                   includes='',
102                   public_headers=None,
103                   public_headers_install=True,
104                   header_path=None,
105                   pc_files=None,
106                   vnum=None,
107                   soname=None,
108                   cflags='',
109                   ldflags='',
110                   external_library=False,
111                   realname=None,
112                   autoproto=None,
113                   autoproto_extra_source='',
114                   group='main',
115                   depends_on='',
116                   local_include=True,
117                   global_include=True,
118                   vars=None,
119                   subdir=None,
120                   install_path=None,
121                   install=True,
122                   pyembed=False,
123                   pyext=False,
124                   target_type='LIBRARY',
125                   bundled_extension=True,
126                   link_name=None,
127                   abi_directory=None,
128                   abi_match=None,
129                   hide_symbols=False,
130                   manpages=None,
131                   private_library=False,
132                   grouping_library=False,
133                   allow_undefined_symbols=False,
134                   allow_warnings=True,
135                   enabled=True):
136     '''define a Samba library'''
137
138     if LIB_MUST_BE_PRIVATE(bld, libname):
139         private_library=True
140
141     if not enabled:
142         SET_TARGET_TYPE(bld, libname, 'DISABLED')
143         return
144
145     source = bld.EXPAND_VARIABLES(source, vars=vars)
146     if subdir:
147         source = bld.SUBDIR(subdir, source)
148
149     # remember empty libraries, so we can strip the dependencies
150     if ((source == '') or (source == [])) and deps == '' and public_deps == '':
151         SET_TARGET_TYPE(bld, libname, 'EMPTY')
152         return
153
154     if BUILTIN_LIBRARY(bld, libname):
155         obj_target = libname
156     else:
157         obj_target = libname + '.objlist'
158
159     if group == 'libraries':
160         subsystem_group = 'main'
161     else:
162         subsystem_group = group
163
164     # first create a target for building the object files for this library
165     # by separating in this way, we avoid recompiling the C files
166     # separately for the install library and the build library
167     bld.SAMBA_SUBSYSTEM(obj_target,
168                         source         = source,
169                         deps           = deps,
170                         public_deps    = public_deps,
171                         includes       = includes,
172                         public_headers = public_headers,
173                         public_headers_install = public_headers_install,
174                         header_path    = header_path,
175                         cflags         = cflags,
176                         group          = subsystem_group,
177                         autoproto      = autoproto,
178                         autoproto_extra_source=autoproto_extra_source,
179                         depends_on     = depends_on,
180                         hide_symbols   = hide_symbols,
181                         allow_warnings = allow_warnings,
182                         pyembed        = pyembed,
183                         pyext          = pyext,
184                         local_include  = local_include,
185                         global_include = global_include)
186
187     if BUILTIN_LIBRARY(bld, libname):
188         return
189
190     if not SET_TARGET_TYPE(bld, libname, target_type):
191         return
192
193     # the library itself will depend on that object target
194     deps += ' ' + public_deps
195     deps = TO_LIST(deps)
196     deps.append(obj_target)
197
198     realname = bld.map_shlib_extension(realname, python=(target_type=='PYTHON'))
199     link_name = bld.map_shlib_extension(link_name, python=(target_type=='PYTHON'))
200
201     # we don't want any public libraries without version numbers
202     if (not private_library and target_type != 'PYTHON' and not realname):
203         if vnum is None and soname is None:
204             raise Utils.WafError("public library '%s' must have a vnum" %
205                     libname)
206         if pc_files is None:
207             raise Utils.WafError("public library '%s' must have pkg-config file" %
208                        libname)
209         if public_headers is None:
210             raise Utils.WafError("public library '%s' must have header files" %
211                        libname)
212
213     if target_type == 'PYTHON' or realname or not private_library:
214         bundled_name = libname.replace('_', '-')
215     else:
216         bundled_name = PRIVATE_NAME(bld, libname, bundled_extension,
217             private_library)
218
219     ldflags = TO_LIST(ldflags)
220
221     features = 'cc cshlib symlink_lib install_lib'
222     if pyext:
223         features += ' pyext'
224     if pyembed:
225         features += ' pyembed'
226
227     if abi_directory:
228         features += ' abi_check'
229
230     vscript = None
231     if bld.env.HAVE_LD_VERSION_SCRIPT:
232         if private_library:
233             version = "%s_%s" % (Utils.g_module.APPNAME, Utils.g_module.VERSION)
234         elif vnum:
235             version = "%s_%s" % (libname, vnum)
236         else:
237             version = None
238         if version:
239             vscript = "%s.vscript" % libname
240             bld.ABI_VSCRIPT(libname, abi_directory, version, vscript,
241                             abi_match)
242             fullname = apply_pattern(bundled_name, bld.env.shlib_PATTERN)
243             fullpath = bld.path.find_or_declare(fullname)
244             vscriptpath = bld.path.find_or_declare(vscript)
245             if not fullpath:
246                 raise Utils.WafError("unable to find fullpath for %s" % fullname)
247             if not vscriptpath:
248                 raise Utils.WafError("unable to find vscript path for %s" % vscript)
249             bld.add_manual_dependency(fullpath, vscriptpath)
250             if Options.is_install:
251                 # also make the .inst file depend on the vscript
252                 instname = apply_pattern(bundled_name + '.inst', bld.env.shlib_PATTERN)
253                 bld.add_manual_dependency(bld.path.find_or_declare(instname), bld.path.find_or_declare(vscript))
254             vscript = os.path.join(bld.path.abspath(bld.env), vscript)
255
256     bld.SET_BUILD_GROUP(group)
257     t = bld(
258         features        = features,
259         source          = [],
260         target          = bundled_name,
261         depends_on      = depends_on,
262         samba_ldflags   = ldflags,
263         samba_deps      = deps,
264         samba_includes  = includes,
265         version_script  = vscript,
266         local_include   = local_include,
267         global_include  = global_include,
268         vnum            = vnum,
269         soname          = soname,
270         install_path    = None,
271         samba_inst_path = install_path,
272         name            = libname,
273         samba_realname  = realname,
274         samba_install   = install,
275         abi_directory   = "%s/%s" % (bld.path.abspath(), abi_directory),
276         abi_match       = abi_match,
277         private_library = private_library,
278         grouping_library=grouping_library,
279         allow_undefined_symbols=allow_undefined_symbols
280         )
281
282     if realname and not link_name:
283         link_name = 'shared/%s' % realname
284
285     if link_name:
286         t.link_name = link_name
287
288     if pc_files is not None and not private_library:
289         bld.PKG_CONFIG_FILES(pc_files, vnum=vnum)
290
291     if (manpages is not None and 'XSLTPROC_MANPAGES' in bld.env and
292         bld.env['XSLTPROC_MANPAGES']):
293         bld.MANPAGES(manpages, install)
294
295
296 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
297
298
299 #################################################################
300 def SAMBA_BINARY(bld, binname, source,
301                  deps='',
302                  includes='',
303                  public_headers=None,
304                  header_path=None,
305                  modules=None,
306                  ldflags=None,
307                  cflags='',
308                  autoproto=None,
309                  use_hostcc=False,
310                  use_global_deps=True,
311                  compiler=None,
312                  group='main',
313                  manpages=None,
314                  local_include=True,
315                  global_include=True,
316                  subsystem_name=None,
317                  pyembed=False,
318                  vars=None,
319                  subdir=None,
320                  install=True,
321                  install_path=None,
322                  enabled=True):
323     '''define a Samba binary'''
324
325     if not enabled:
326         SET_TARGET_TYPE(bld, binname, 'DISABLED')
327         return
328
329     if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
330         return
331
332     features = 'cc cprogram symlink_bin install_bin'
333     if pyembed:
334         features += ' pyembed'
335
336     obj_target = binname + '.objlist'
337
338     source = bld.EXPAND_VARIABLES(source, vars=vars)
339     if subdir:
340         source = bld.SUBDIR(subdir, source)
341     source = unique_list(TO_LIST(source))
342
343     if group == 'binaries':
344         subsystem_group = 'main'
345     else:
346         subsystem_group = group
347
348     # only specify PIE flags for binaries
349     pie_cflags = cflags
350     pie_ldflags = TO_LIST(ldflags)
351     if bld.env['ENABLE_PIE'] == True:
352         pie_cflags += ' -fPIE'
353         pie_ldflags.extend(TO_LIST('-pie'))
354     if bld.env['ENABLE_RELRO'] == True:
355         pie_ldflags.extend(TO_LIST('-Wl,-z,relro,-z,now'))
356
357     # first create a target for building the object files for this binary
358     # by separating in this way, we avoid recompiling the C files
359     # separately for the install binary and the build binary
360     bld.SAMBA_SUBSYSTEM(obj_target,
361                         source         = source,
362                         deps           = deps,
363                         includes       = includes,
364                         cflags         = pie_cflags,
365                         group          = subsystem_group,
366                         autoproto      = autoproto,
367                         subsystem_name = subsystem_name,
368                         local_include  = local_include,
369                         global_include = global_include,
370                         use_hostcc     = use_hostcc,
371                         pyext          = pyembed,
372                         use_global_deps= use_global_deps)
373
374     bld.SET_BUILD_GROUP(group)
375
376     # the binary itself will depend on that object target
377     deps = TO_LIST(deps)
378     deps.append(obj_target)
379
380     t = bld(
381         features       = features,
382         source         = [],
383         target         = binname,
384         samba_deps     = deps,
385         samba_includes = includes,
386         local_include  = local_include,
387         global_include = global_include,
388         samba_modules  = modules,
389         top            = True,
390         samba_subsystem= subsystem_name,
391         install_path   = None,
392         samba_inst_path= install_path,
393         samba_install  = install,
394         samba_ldflags  = pie_ldflags
395         )
396
397     if manpages is not None and 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
398         bld.MANPAGES(manpages, install)
399
400 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
401
402
403 #################################################################
404 def SAMBA_MODULE(bld, modname, source,
405                  deps='',
406                  includes='',
407                  subsystem=None,
408                  init_function=None,
409                  module_init_name='samba_init_module',
410                  autoproto=None,
411                  autoproto_extra_source='',
412                  cflags='',
413                  internal_module=True,
414                  local_include=True,
415                  global_include=True,
416                  vars=None,
417                  subdir=None,
418                  enabled=True,
419                  pyembed=False,
420                  manpages=None,
421                  allow_undefined_symbols=False,
422                  allow_warnings=True
423                  ):
424     '''define a Samba module.'''
425
426     source = bld.EXPAND_VARIABLES(source, vars=vars)
427     if subdir:
428         source = bld.SUBDIR(subdir, source)
429
430     if internal_module or BUILTIN_LIBRARY(bld, modname):
431         # Do not create modules for disabled subsystems
432         if subsystem and GET_TARGET_TYPE(bld, subsystem) == 'DISABLED':
433             return
434         bld.SAMBA_SUBSYSTEM(modname, source,
435                     deps=deps,
436                     includes=includes,
437                     autoproto=autoproto,
438                     autoproto_extra_source=autoproto_extra_source,
439                     cflags=cflags,
440                     local_include=local_include,
441                     global_include=global_include,
442                     allow_warnings=allow_warnings,
443                     enabled=enabled)
444
445         bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
446         return
447
448     if not enabled:
449         SET_TARGET_TYPE(bld, modname, 'DISABLED')
450         return
451
452     # Do not create modules for disabled subsystems
453     if subsystem and GET_TARGET_TYPE(bld, subsystem) == 'DISABLED':
454         return
455
456     obj_target = modname + '.objlist'
457
458     realname = modname
459     if subsystem is not None:
460         deps += ' ' + subsystem
461         while realname.startswith("lib"+subsystem+"_"):
462             realname = realname[len("lib"+subsystem+"_"):]
463         while realname.startswith(subsystem+"_"):
464             realname = realname[len(subsystem+"_"):]
465
466     realname = bld.make_libname(realname)
467     while realname.startswith("lib"):
468         realname = realname[len("lib"):]
469
470     build_link_name = "modules/%s/%s" % (subsystem, realname)
471
472     if init_function:
473         cflags += " -D%s=%s" % (init_function, module_init_name)
474
475     bld.SAMBA_LIBRARY(modname,
476                       source,
477                       deps=deps,
478                       includes=includes,
479                       cflags=cflags,
480                       realname = realname,
481                       autoproto = autoproto,
482                       local_include=local_include,
483                       global_include=global_include,
484                       vars=vars,
485                       link_name=build_link_name,
486                       install_path="${MODULESDIR}/%s" % subsystem,
487                       pyembed=pyembed,
488                       manpages=manpages,
489                       allow_undefined_symbols=allow_undefined_symbols,
490                       allow_warnings=allow_warnings
491                       )
492
493
494 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
495
496
497 #################################################################
498 def SAMBA_SUBSYSTEM(bld, modname, source,
499                     deps='',
500                     public_deps='',
501                     includes='',
502                     public_headers=None,
503                     public_headers_install=True,
504                     header_path=None,
505                     cflags='',
506                     cflags_end=None,
507                     group='main',
508                     init_function_sentinel=None,
509                     autoproto=None,
510                     autoproto_extra_source='',
511                     depends_on='',
512                     local_include=True,
513                     local_include_first=True,
514                     global_include=True,
515                     subsystem_name=None,
516                     enabled=True,
517                     use_hostcc=False,
518                     use_global_deps=True,
519                     vars=None,
520                     subdir=None,
521                     hide_symbols=False,
522                     allow_warnings=True,
523                     pyext=False,
524                     pyembed=False):
525     '''define a Samba subsystem'''
526
527     if not enabled:
528         SET_TARGET_TYPE(bld, modname, 'DISABLED')
529         return
530
531     # remember empty subsystems, so we can strip the dependencies
532     if ((source == '') or (source == [])) and deps == '' and public_deps == '':
533         SET_TARGET_TYPE(bld, modname, 'EMPTY')
534         return
535
536     if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
537         return
538
539     source = bld.EXPAND_VARIABLES(source, vars=vars)
540     if subdir:
541         source = bld.SUBDIR(subdir, source)
542     source = unique_list(TO_LIST(source))
543
544     deps += ' ' + public_deps
545
546     bld.SET_BUILD_GROUP(group)
547
548     features = 'cc'
549     if pyext:
550         features += ' pyext'
551     if pyembed:
552         features += ' pyembed'
553
554     t = bld(
555         features       = features,
556         source         = source,
557         target         = modname,
558         samba_cflags   = CURRENT_CFLAGS(bld, modname, cflags,
559                                         allow_warnings=allow_warnings,
560                                         hide_symbols=hide_symbols),
561         depends_on     = depends_on,
562         samba_deps     = TO_LIST(deps),
563         samba_includes = includes,
564         local_include  = local_include,
565         local_include_first  = local_include_first,
566         global_include = global_include,
567         samba_subsystem= subsystem_name,
568         samba_use_hostcc = use_hostcc,
569         samba_use_global_deps = use_global_deps,
570         )
571
572     if cflags_end is not None:
573         t.samba_cflags.extend(TO_LIST(cflags_end))
574
575     if autoproto is not None:
576         bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
577     if public_headers is not None:
578         bld.PUBLIC_HEADERS(public_headers, header_path=header_path,
579                            public_headers_install=public_headers_install)
580     return t
581
582
583 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
584
585
586 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
587                     group='generators', enabled=True,
588                     public_headers=None,
589                     public_headers_install=True,
590                     header_path=None,
591                     vars=None,
592                     dep_vars=[],
593                     always=False):
594     '''A generic source generator target'''
595
596     if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
597         return
598
599     if not enabled:
600         return
601
602     dep_vars.append('ruledeps')
603     dep_vars.append('SAMBA_GENERATOR_VARS')
604
605     bld.SET_BUILD_GROUP(group)
606     t = bld(
607         rule=rule,
608         source=bld.EXPAND_VARIABLES(source, vars=vars),
609         target=target,
610         shell=isinstance(rule, str),
611         on_results=True,
612         before='cc',
613         ext_out='.c',
614         samba_type='GENERATOR',
615         dep_vars = dep_vars,
616         name=name)
617
618     if vars is None:
619         vars = {}
620     t.env.SAMBA_GENERATOR_VARS = vars
621
622     if always:
623         t.always = True
624
625     if public_headers is not None:
626         bld.PUBLIC_HEADERS(public_headers, header_path=header_path,
627                            public_headers_install=public_headers_install)
628     return t
629 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
630
631
632
633 @runonce
634 def SETUP_BUILD_GROUPS(bld):
635     '''setup build groups used to ensure that the different build
636     phases happen consecutively'''
637     bld.p_ln = bld.srcnode # we do want to see all targets!
638     bld.env['USING_BUILD_GROUPS'] = True
639     bld.add_group('setup')
640     bld.add_group('build_compiler_source')
641     bld.add_group('vscripts')
642     bld.add_group('base_libraries')
643     bld.add_group('generators')
644     bld.add_group('compiler_prototypes')
645     bld.add_group('compiler_libraries')
646     bld.add_group('build_compilers')
647     bld.add_group('build_source')
648     bld.add_group('prototypes')
649     bld.add_group('headers')
650     bld.add_group('main')
651     bld.add_group('symbolcheck')
652     bld.add_group('syslibcheck')
653     bld.add_group('final')
654 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
655
656
657 def SET_BUILD_GROUP(bld, group):
658     '''set the current build group'''
659     if not 'USING_BUILD_GROUPS' in bld.env:
660         return
661     bld.set_group(group)
662 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
663
664
665
666 @conf
667 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
668     """use timestamps instead of file contents for deps
669     this currently doesn't work"""
670     def h_file(filename):
671         import stat
672         st = os.stat(filename)
673         if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
674         m = Utils.md5()
675         m.update(str(st.st_mtime))
676         m.update(str(st.st_size))
677         m.update(filename)
678         return m.digest()
679     Utils.h_file = h_file
680
681
682 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
683     '''used to copy scripts from the source tree into the build directory
684        for use by selftest'''
685
686     source = bld.path.ant_glob(pattern)
687
688     bld.SET_BUILD_GROUP('build_source')
689     for s in TO_LIST(source):
690         iname = s
691         if installname is not None:
692             iname = installname
693         target = os.path.join(installdir, iname)
694         tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
695         mkdir_p(tgtdir)
696         link_src = os.path.normpath(os.path.join(bld.curdir, s))
697         link_dst = os.path.join(tgtdir, os.path.basename(iname))
698         if os.path.islink(link_dst) and os.readlink(link_dst) == link_src:
699             continue
700         if os.path.exists(link_dst):
701             os.unlink(link_dst)
702         Logs.info("symlink: %s -> %s/%s" % (s, installdir, iname))
703         os.symlink(link_src, link_dst)
704 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
705
706
707 def copy_and_fix_python_path(task):
708     pattern='sys.path.insert(0, "bin/python")'
709     if task.env["PYTHONARCHDIR"] in sys.path and task.env["PYTHONDIR"] in sys.path:
710         replacement = ""
711     elif task.env["PYTHONARCHDIR"] == task.env["PYTHONDIR"]:
712         replacement="""sys.path.insert(0, "%s")""" % task.env["PYTHONDIR"]
713     else:
714         replacement="""sys.path.insert(0, "%s")
715 sys.path.insert(1, "%s")""" % (task.env["PYTHONARCHDIR"], task.env["PYTHONDIR"])
716
717     if task.env["PYTHON"][0] == "/":
718         replacement_shebang = "#!%s\n" % task.env["PYTHON"]
719     else:
720         replacement_shebang = "#!/usr/bin/env %s\n" % task.env["PYTHON"]
721
722     installed_location=task.outputs[0].bldpath(task.env)
723     source_file = open(task.inputs[0].srcpath(task.env))
724     installed_file = open(installed_location, 'w')
725     lineno = 0
726     for line in source_file:
727         newline = line
728         if lineno == 0 and task.env["PYTHON_SPECIFIED"] == True and line[:2] == "#!":
729             newline = replacement_shebang
730         elif pattern in line:
731             newline = line.replace(pattern, replacement)
732         installed_file.write(newline)
733         lineno = lineno + 1
734     installed_file.close()
735     os.chmod(installed_location, 0755)
736     return 0
737
738
739 def install_file(bld, destdir, file, chmod=MODE_644, flat=False,
740                  python_fixup=False, destname=None, base_name=None):
741     '''install a file'''
742     destdir = bld.EXPAND_VARIABLES(destdir)
743     if not destname:
744         destname = file
745         if flat:
746             destname = os.path.basename(destname)
747     dest = os.path.join(destdir, destname)
748     if python_fixup:
749         # fix the path python will use to find Samba modules
750         inst_file = file + '.inst'
751         bld.SAMBA_GENERATOR('python_%s' % destname,
752                             rule=copy_and_fix_python_path,
753                             source=file,
754                             target=inst_file)
755         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), bld.env["PYTHONARCHDIR"])
756         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), bld.env["PYTHONDIR"])
757         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), str(bld.env["PYTHON_SPECIFIED"]))
758         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), bld.env["PYTHON"])
759         file = inst_file
760     if base_name:
761         file = os.path.join(base_name, file)
762     bld.install_as(dest, file, chmod=chmod)
763
764
765 def INSTALL_FILES(bld, destdir, files, chmod=MODE_644, flat=False,
766                   python_fixup=False, destname=None, base_name=None):
767     '''install a set of files'''
768     for f in TO_LIST(files):
769         install_file(bld, destdir, f, chmod=chmod, flat=flat,
770                      python_fixup=python_fixup, destname=destname,
771                      base_name=base_name)
772 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
773
774
775 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=MODE_644, flat=False,
776                      python_fixup=False, exclude=None, trim_path=None):
777     '''install a set of files matching a wildcard pattern'''
778     files=TO_LIST(bld.path.ant_glob(pattern))
779     if trim_path:
780         files2 = []
781         for f in files:
782             files2.append(os_path_relpath(f, trim_path))
783         files = files2
784
785     if exclude:
786         for f in files[:]:
787             if fnmatch.fnmatch(f, exclude):
788                 files.remove(f)
789     INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
790                   python_fixup=python_fixup, base_name=trim_path)
791 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
792
793
794 def INSTALL_DIRS(bld, destdir, dirs):
795     '''install a set of directories'''
796     destdir = bld.EXPAND_VARIABLES(destdir)
797     dirs = bld.EXPAND_VARIABLES(dirs)
798     for d in TO_LIST(dirs):
799         bld.install_dir(os.path.join(destdir, d))
800 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
801
802
803 def MANPAGES(bld, manpages, install):
804     '''build and install manual pages'''
805     bld.env.MAN_XSL = 'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
806     for m in manpages.split():
807         source = m + '.xml'
808         bld.SAMBA_GENERATOR(m,
809                             source=source,
810                             target=m,
811                             group='final',
812                             rule='${XSLTPROC} --xinclude -o ${TGT} --nonet ${MAN_XSL} ${SRC}'
813                             )
814         if install:
815             bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
816 Build.BuildContext.MANPAGES = MANPAGES
817
818 def SAMBAMANPAGES(bld, manpages, extra_source=None):
819     '''build and install manual pages'''
820     bld.env.SAMBA_EXPAND_XSL = bld.srcnode.abspath() + '/docs-xml/xslt/expand-sambadoc.xsl'
821     bld.env.SAMBA_MAN_XSL = bld.srcnode.abspath() + '/docs-xml/xslt/man.xsl'
822     bld.env.SAMBA_CATALOGS = 'file:///etc/xml/catalog file:///usr/local/share/xml/catalog file://' + bld.srcnode.abspath() + '/bin/default/docs-xml/build/catalog.xml'
823
824     for m in manpages.split():
825         source = m + '.xml'
826         if extra_source is not None:
827             source = [source, extra_source]
828         bld.SAMBA_GENERATOR(m,
829                             source=source,
830                             target=m,
831                             group='final',
832                             rule='''XML_CATALOG_FILES="${SAMBA_CATALOGS}"
833                                     export XML_CATALOG_FILES
834                                     ${XSLTPROC} --xinclude --stringparam noreference 0 -o ${TGT}.xml --nonet ${SAMBA_EXPAND_XSL} ${SRC[0].abspath(env)}
835                                     ${XSLTPROC} --nonet -o ${TGT} ${SAMBA_MAN_XSL} ${TGT}.xml'''
836                             )
837         bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
838 Build.BuildContext.SAMBAMANPAGES = SAMBAMANPAGES
839
840 #############################################################
841 # give a nicer display when building different types of files
842 def progress_display(self, msg, fname):
843     col1 = Logs.colors(self.color)
844     col2 = Logs.colors.NORMAL
845     total = self.position[1]
846     n = len(str(total))
847     fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
848     return fs % (self.position[0], self.position[1], col1, fname, col2)
849
850 def link_display(self):
851     if Options.options.progress_bar != 0:
852         return Task.Task.old_display(self)
853     fname = self.outputs[0].bldpath(self.env)
854     return progress_display(self, 'Linking', fname)
855 Task.TaskBase.classes['cc_link'].display = link_display
856
857 def samba_display(self):
858     if Options.options.progress_bar != 0:
859         return Task.Task.old_display(self)
860
861     targets    = LOCAL_CACHE(self, 'TARGET_TYPE')
862     if self.name in targets:
863         target_type = targets[self.name]
864         type_map = { 'GENERATOR' : 'Generating',
865                      'PROTOTYPE' : 'Generating'
866                      }
867         if target_type in type_map:
868             return progress_display(self, type_map[target_type], self.name)
869
870     if len(self.inputs) == 0:
871         return Task.Task.old_display(self)
872
873     fname = self.inputs[0].bldpath(self.env)
874     if fname[0:3] == '../':
875         fname = fname[3:]
876     ext_loc = fname.rfind('.')
877     if ext_loc == -1:
878         return Task.Task.old_display(self)
879     ext = fname[ext_loc:]
880
881     ext_map = { '.idl' : 'Compiling IDL',
882                 '.et'  : 'Compiling ERRTABLE',
883                 '.asn1': 'Compiling ASN1',
884                 '.c'   : 'Compiling' }
885     if ext in ext_map:
886         return progress_display(self, ext_map[ext], fname)
887     return Task.Task.old_display(self)
888
889 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
890 Task.TaskBase.classes['Task'].display = samba_display
891
892
893 @after('apply_link')
894 @feature('cshlib')
895 def apply_bundle_remove_dynamiclib_patch(self):
896     if self.env['MACBUNDLE'] or getattr(self,'mac_bundle',False):
897         if not getattr(self,'vnum',None):
898             try:
899                 self.env['LINKFLAGS'].remove('-dynamiclib')
900                 self.env['LINKFLAGS'].remove('-single_module')
901             except ValueError:
902                 pass