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