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