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