wafsamba: fix dependency calculation for 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                     always=False):
593     '''A generic source generator target'''
594
595     if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
596         return
597
598     if not enabled:
599         return
600
601     dep_vars = []
602     if isinstance(vars, dict):
603         dep_vars = vars.keys()
604     elif isinstance(vars, list):
605         dep_vars = vars
606     dep_vars.append('ruledeps')
607
608     bld.SET_BUILD_GROUP(group)
609     t = bld(
610         rule=rule,
611         source=bld.EXPAND_VARIABLES(source, vars=vars),
612         target=target,
613         shell=isinstance(rule, str),
614         on_results=True,
615         before='cc',
616         ext_out='.c',
617         samba_type='GENERATOR',
618         dep_vars = dep_vars,
619         name=name)
620
621     if always:
622         t.always = True
623
624     if public_headers is not None:
625         bld.PUBLIC_HEADERS(public_headers, header_path=header_path,
626                            public_headers_install=public_headers_install)
627     return t
628 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
629
630
631
632 @runonce
633 def SETUP_BUILD_GROUPS(bld):
634     '''setup build groups used to ensure that the different build
635     phases happen consecutively'''
636     bld.p_ln = bld.srcnode # we do want to see all targets!
637     bld.env['USING_BUILD_GROUPS'] = True
638     bld.add_group('setup')
639     bld.add_group('build_compiler_source')
640     bld.add_group('vscripts')
641     bld.add_group('base_libraries')
642     bld.add_group('generators')
643     bld.add_group('compiler_prototypes')
644     bld.add_group('compiler_libraries')
645     bld.add_group('build_compilers')
646     bld.add_group('build_source')
647     bld.add_group('prototypes')
648     bld.add_group('headers')
649     bld.add_group('main')
650     bld.add_group('symbolcheck')
651     bld.add_group('syslibcheck')
652     bld.add_group('final')
653 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
654
655
656 def SET_BUILD_GROUP(bld, group):
657     '''set the current build group'''
658     if not 'USING_BUILD_GROUPS' in bld.env:
659         return
660     bld.set_group(group)
661 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
662
663
664
665 @conf
666 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
667     """use timestamps instead of file contents for deps
668     this currently doesn't work"""
669     def h_file(filename):
670         import stat
671         st = os.stat(filename)
672         if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
673         m = Utils.md5()
674         m.update(str(st.st_mtime))
675         m.update(str(st.st_size))
676         m.update(filename)
677         return m.digest()
678     Utils.h_file = h_file
679
680
681 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
682     '''used to copy scripts from the source tree into the build directory
683        for use by selftest'''
684
685     source = bld.path.ant_glob(pattern)
686
687     bld.SET_BUILD_GROUP('build_source')
688     for s in TO_LIST(source):
689         iname = s
690         if installname is not None:
691             iname = installname
692         target = os.path.join(installdir, iname)
693         tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
694         mkdir_p(tgtdir)
695         link_src = os.path.normpath(os.path.join(bld.curdir, s))
696         link_dst = os.path.join(tgtdir, os.path.basename(iname))
697         if os.path.islink(link_dst) and os.readlink(link_dst) == link_src:
698             continue
699         if os.path.exists(link_dst):
700             os.unlink(link_dst)
701         Logs.info("symlink: %s -> %s/%s" % (s, installdir, iname))
702         os.symlink(link_src, link_dst)
703 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
704
705
706 def copy_and_fix_python_path(task):
707     pattern='sys.path.insert(0, "bin/python")'
708     if task.env["PYTHONARCHDIR"] in sys.path and task.env["PYTHONDIR"] in sys.path:
709         replacement = ""
710     elif task.env["PYTHONARCHDIR"] == task.env["PYTHONDIR"]:
711         replacement="""sys.path.insert(0, "%s")""" % task.env["PYTHONDIR"]
712     else:
713         replacement="""sys.path.insert(0, "%s")
714 sys.path.insert(1, "%s")""" % (task.env["PYTHONARCHDIR"], task.env["PYTHONDIR"])
715
716     if task.env["PYTHON"][0] == "/":
717         replacement_shebang = "#!%s\n" % task.env["PYTHON"]
718     else:
719         replacement_shebang = "#!/usr/bin/env %s\n" % task.env["PYTHON"]
720
721     installed_location=task.outputs[0].bldpath(task.env)
722     source_file = open(task.inputs[0].srcpath(task.env))
723     installed_file = open(installed_location, 'w')
724     lineno = 0
725     for line in source_file:
726         newline = line
727         if lineno == 0 and task.env["PYTHON_SPECIFIED"] == True and line[:2] == "#!":
728             newline = replacement_shebang
729         elif pattern in line:
730             newline = line.replace(pattern, replacement)
731         installed_file.write(newline)
732         lineno = lineno + 1
733     installed_file.close()
734     os.chmod(installed_location, 0755)
735     return 0
736
737
738 def install_file(bld, destdir, file, chmod=MODE_644, flat=False,
739                  python_fixup=False, destname=None, base_name=None):
740     '''install a file'''
741     destdir = bld.EXPAND_VARIABLES(destdir)
742     if not destname:
743         destname = file
744         if flat:
745             destname = os.path.basename(destname)
746     dest = os.path.join(destdir, destname)
747     if python_fixup:
748         # fix the path python will use to find Samba modules
749         inst_file = file + '.inst'
750         bld.SAMBA_GENERATOR('python_%s' % destname,
751                             rule=copy_and_fix_python_path,
752                             source=file,
753                             target=inst_file)
754         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), bld.env["PYTHONARCHDIR"])
755         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), bld.env["PYTHONDIR"])
756         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), str(bld.env["PYTHON_SPECIFIED"]))
757         bld.add_manual_dependency(bld.path.find_or_declare(inst_file), bld.env["PYTHON"])
758         file = inst_file
759     if base_name:
760         file = os.path.join(base_name, file)
761     bld.install_as(dest, file, chmod=chmod)
762
763
764 def INSTALL_FILES(bld, destdir, files, chmod=MODE_644, flat=False,
765                   python_fixup=False, destname=None, base_name=None):
766     '''install a set of files'''
767     for f in TO_LIST(files):
768         install_file(bld, destdir, f, chmod=chmod, flat=flat,
769                      python_fixup=python_fixup, destname=destname,
770                      base_name=base_name)
771 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
772
773
774 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=MODE_644, flat=False,
775                      python_fixup=False, exclude=None, trim_path=None):
776     '''install a set of files matching a wildcard pattern'''
777     files=TO_LIST(bld.path.ant_glob(pattern))
778     if trim_path:
779         files2 = []
780         for f in files:
781             files2.append(os_path_relpath(f, trim_path))
782         files = files2
783
784     if exclude:
785         for f in files[:]:
786             if fnmatch.fnmatch(f, exclude):
787                 files.remove(f)
788     INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
789                   python_fixup=python_fixup, base_name=trim_path)
790 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
791
792
793 def INSTALL_DIRS(bld, destdir, dirs):
794     '''install a set of directories'''
795     destdir = bld.EXPAND_VARIABLES(destdir)
796     dirs = bld.EXPAND_VARIABLES(dirs)
797     for d in TO_LIST(dirs):
798         bld.install_dir(os.path.join(destdir, d))
799 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
800
801
802 def MANPAGES(bld, manpages, install):
803     '''build and install manual pages'''
804     bld.env.MAN_XSL = 'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
805     for m in manpages.split():
806         source = m + '.xml'
807         bld.SAMBA_GENERATOR(m,
808                             source=source,
809                             target=m,
810                             group='final',
811                             rule='${XSLTPROC} --xinclude -o ${TGT} --nonet ${MAN_XSL} ${SRC}'
812                             )
813         if install:
814             bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
815 Build.BuildContext.MANPAGES = MANPAGES
816
817 def SAMBAMANPAGES(bld, manpages, extra_source=None):
818     '''build and install manual pages'''
819     bld.env.SAMBA_EXPAND_XSL = bld.srcnode.abspath() + '/docs-xml/xslt/expand-sambadoc.xsl'
820     bld.env.SAMBA_MAN_XSL = bld.srcnode.abspath() + '/docs-xml/xslt/man.xsl'
821     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'
822
823     for m in manpages.split():
824         source = m + '.xml'
825         if extra_source is not None:
826             source = [source, extra_source]
827         bld.SAMBA_GENERATOR(m,
828                             source=source,
829                             target=m,
830                             group='final',
831                             rule='''XML_CATALOG_FILES="${SAMBA_CATALOGS}"
832                                     export XML_CATALOG_FILES
833                                     ${XSLTPROC} --xinclude --stringparam noreference 0 -o ${TGT}.xml --nonet ${SAMBA_EXPAND_XSL} ${SRC[0].abspath(env)}
834                                     ${XSLTPROC} --nonet -o ${TGT} ${SAMBA_MAN_XSL} ${TGT}.xml'''
835                             )
836         bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
837 Build.BuildContext.SAMBAMANPAGES = SAMBAMANPAGES
838
839 #############################################################
840 # give a nicer display when building different types of files
841 def progress_display(self, msg, fname):
842     col1 = Logs.colors(self.color)
843     col2 = Logs.colors.NORMAL
844     total = self.position[1]
845     n = len(str(total))
846     fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
847     return fs % (self.position[0], self.position[1], col1, fname, col2)
848
849 def link_display(self):
850     if Options.options.progress_bar != 0:
851         return Task.Task.old_display(self)
852     fname = self.outputs[0].bldpath(self.env)
853     return progress_display(self, 'Linking', fname)
854 Task.TaskBase.classes['cc_link'].display = link_display
855
856 def samba_display(self):
857     if Options.options.progress_bar != 0:
858         return Task.Task.old_display(self)
859
860     targets    = LOCAL_CACHE(self, 'TARGET_TYPE')
861     if self.name in targets:
862         target_type = targets[self.name]
863         type_map = { 'GENERATOR' : 'Generating',
864                      'PROTOTYPE' : 'Generating'
865                      }
866         if target_type in type_map:
867             return progress_display(self, type_map[target_type], self.name)
868
869     if len(self.inputs) == 0:
870         return Task.Task.old_display(self)
871
872     fname = self.inputs[0].bldpath(self.env)
873     if fname[0:3] == '../':
874         fname = fname[3:]
875     ext_loc = fname.rfind('.')
876     if ext_loc == -1:
877         return Task.Task.old_display(self)
878     ext = fname[ext_loc:]
879
880     ext_map = { '.idl' : 'Compiling IDL',
881                 '.et'  : 'Compiling ERRTABLE',
882                 '.asn1': 'Compiling ASN1',
883                 '.c'   : 'Compiling' }
884     if ext in ext_map:
885         return progress_display(self, ext_map[ext], fname)
886     return Task.Task.old_display(self)
887
888 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
889 Task.TaskBase.classes['Task'].display = samba_display
890
891
892 @after('apply_link')
893 @feature('cshlib')
894 def apply_bundle_remove_dynamiclib_patch(self):
895     if self.env['MACBUNDLE'] or getattr(self,'mac_bundle',False):
896         if not getattr(self,'vnum',None):
897             try:
898                 self.env['LINKFLAGS'].remove('-dynamiclib')
899                 self.env['LINKFLAGS'].remove('-single_module')
900             except ValueError:
901                 pass