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