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