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