wafsamba: Add missing cflags_end argument to SAMBA_MODULE
[metze/samba/wip.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                  autoproto=None,
357                  use_hostcc=False,
358                  use_global_deps=True,
359                  compiler=None,
360                  group='main',
361                  manpages=None,
362                  local_include=True,
363                  global_include=True,
364                  subsystem_name=None,
365                  pyembed=False,
366                  vars=None,
367                  subdir=None,
368                  install=True,
369                  install_path=None,
370                  enabled=True):
371     '''define a Samba binary'''
372
373     if not enabled:
374         SET_TARGET_TYPE(bld, binname, 'DISABLED')
375         return
376
377     if not SET_TARGET_TYPE(bld, binname, 'BINARY'):
378         return
379
380     features = 'c cprogram symlink_bin install_bin'
381     if pyembed:
382         features += ' pyembed'
383
384     obj_target = binname + '.objlist'
385
386     source = bld.EXPAND_VARIABLES(source, vars=vars)
387     if subdir:
388         source = bld.SUBDIR(subdir, source)
389     source = unique_list(TO_LIST(source))
390
391     if group == 'binaries':
392         subsystem_group = 'main'
393     else:
394         subsystem_group = group
395
396     # only specify PIE flags for binaries
397     pie_cflags = cflags
398     pie_ldflags = TO_LIST(ldflags)
399     if bld.env['ENABLE_PIE'] is True:
400         pie_cflags += ' -fPIE'
401         pie_ldflags.extend(TO_LIST('-pie'))
402     if bld.env['ENABLE_RELRO'] is True:
403         pie_ldflags.extend(TO_LIST('-Wl,-z,relro,-z,now'))
404
405     # first create a target for building the object files for this binary
406     # by separating in this way, we avoid recompiling the C files
407     # separately for the install binary and the build binary
408     bld.SAMBA_SUBSYSTEM(obj_target,
409                         source         = source,
410                         deps           = deps,
411                         includes       = includes,
412                         cflags         = pie_cflags,
413                         group          = subsystem_group,
414                         autoproto      = autoproto,
415                         subsystem_name = subsystem_name,
416                         local_include  = local_include,
417                         global_include = global_include,
418                         use_hostcc     = use_hostcc,
419                         pyext          = pyembed,
420                         use_global_deps= use_global_deps)
421
422     bld.SET_BUILD_GROUP(group)
423
424     # the binary itself will depend on that object target
425     deps = TO_LIST(deps)
426     deps.append(obj_target)
427
428     t = bld(
429         features       = features,
430         source         = [],
431         target         = binname,
432         samba_deps     = deps,
433         samba_includes = includes,
434         local_include  = local_include,
435         global_include = global_include,
436         samba_modules  = modules,
437         top            = True,
438         samba_subsystem= subsystem_name,
439         install_path   = None,
440         samba_inst_path= install_path,
441         samba_install  = install,
442         samba_ldflags  = pie_ldflags
443         )
444
445     if manpages is not None and 'XSLTPROC_MANPAGES' in bld.env and bld.env['XSLTPROC_MANPAGES']:
446         bld.MANPAGES(manpages, install)
447
448 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
449
450
451 #################################################################
452 def SAMBA_MODULE(bld, modname, source,
453                  deps='',
454                  includes='',
455                  subsystem=None,
456                  init_function=None,
457                  module_init_name='samba_init_module',
458                  autoproto=None,
459                  autoproto_extra_source='',
460                  cflags='',
461                  cflags_end=None,
462                  internal_module=True,
463                  local_include=True,
464                  global_include=True,
465                  vars=None,
466                  subdir=None,
467                  enabled=True,
468                  pyembed=False,
469                  manpages=None,
470                  allow_undefined_symbols=False,
471                  allow_warnings=False,
472                  install=True
473                  ):
474     '''define a Samba module.'''
475
476     bld.ASSERT(subsystem, "You must specify a subsystem for SAMBA_MODULE(%s)" % modname)
477
478     source = bld.EXPAND_VARIABLES(source, vars=vars)
479     if subdir:
480         source = bld.SUBDIR(subdir, source)
481
482     if internal_module or BUILTIN_LIBRARY(bld, modname):
483         # Do not create modules for disabled subsystems
484         if GET_TARGET_TYPE(bld, subsystem) == 'DISABLED':
485             return
486         bld.SAMBA_SUBSYSTEM(modname, source,
487                     deps=deps,
488                     includes=includes,
489                     autoproto=autoproto,
490                     autoproto_extra_source=autoproto_extra_source,
491                     cflags=cflags,
492                     cflags_end=cflags_end,
493                     local_include=local_include,
494                     global_include=global_include,
495                     allow_warnings=allow_warnings,
496                     enabled=enabled)
497
498         bld.ADD_INIT_FUNCTION(subsystem, modname, init_function)
499         return
500
501     if not enabled:
502         SET_TARGET_TYPE(bld, modname, 'DISABLED')
503         return
504
505     # Do not create modules for disabled subsystems
506     if GET_TARGET_TYPE(bld, subsystem) == 'DISABLED':
507         return
508
509     realname = modname
510     deps += ' ' + subsystem
511     while realname.startswith("lib"+subsystem+"_"):
512         realname = realname[len("lib"+subsystem+"_"):]
513     while realname.startswith(subsystem+"_"):
514         realname = realname[len(subsystem+"_"):]
515
516     build_name = "%s_module_%s" % (subsystem, realname)
517
518     realname = bld.make_libname(realname)
519     while realname.startswith("lib"):
520         realname = realname[len("lib"):]
521
522     build_link_name = "modules/%s/%s" % (subsystem, realname)
523
524     if init_function:
525         cflags += " -D%s=%s" % (init_function, module_init_name)
526
527     bld.SAMBA_LIBRARY(modname,
528                       source,
529                       deps=deps,
530                       includes=includes,
531                       cflags=cflags,
532                       cflags_end=cflags_end,
533                       realname = realname,
534                       autoproto = autoproto,
535                       local_include=local_include,
536                       global_include=global_include,
537                       vars=vars,
538                       bundled_name=build_name,
539                       link_name=build_link_name,
540                       install_path="${MODULESDIR}/%s" % subsystem,
541                       pyembed=pyembed,
542                       manpages=manpages,
543                       allow_undefined_symbols=allow_undefined_symbols,
544                       allow_warnings=allow_warnings,
545                       install=install
546                       )
547
548
549 Build.BuildContext.SAMBA_MODULE = SAMBA_MODULE
550
551
552 #################################################################
553 def SAMBA_SUBSYSTEM(bld, modname, source,
554                     deps='',
555                     public_deps='',
556                     includes='',
557                     public_headers=None,
558                     public_headers_install=True,
559                     private_headers=None,
560                     header_path=None,
561                     cflags='',
562                     cflags_end=None,
563                     group='main',
564                     init_function_sentinel=None,
565                     autoproto=None,
566                     autoproto_extra_source='',
567                     depends_on='',
568                     local_include=True,
569                     local_include_first=True,
570                     global_include=True,
571                     subsystem_name=None,
572                     enabled=True,
573                     use_hostcc=False,
574                     use_global_deps=True,
575                     vars=None,
576                     subdir=None,
577                     hide_symbols=False,
578                     allow_warnings=False,
579                     pyext=False,
580                     pyembed=False):
581     '''define a Samba subsystem'''
582
583     if not enabled:
584         SET_TARGET_TYPE(bld, modname, 'DISABLED')
585         return
586
587     # remember empty subsystems, so we can strip the dependencies
588     if ((source == '') or (source == [])):
589         if deps == '' and public_deps == '':
590             SET_TARGET_TYPE(bld, modname, 'EMPTY')
591             return
592         empty_c = modname + '.empty.c'
593         bld.SAMBA_GENERATOR('%s_empty_c' % modname,
594                             rule=generate_empty_file,
595                             target=empty_c)
596         source=empty_c
597
598     if not SET_TARGET_TYPE(bld, modname, 'SUBSYSTEM'):
599         return
600
601     source = bld.EXPAND_VARIABLES(source, vars=vars)
602     if subdir:
603         source = bld.SUBDIR(subdir, source)
604     source = unique_list(TO_LIST(source))
605
606     deps += ' ' + public_deps
607
608     bld.SET_BUILD_GROUP(group)
609
610     features = 'c'
611     if pyext:
612         features += ' pyext'
613     if pyembed:
614         features += ' pyembed'
615
616     t = bld(
617         features       = features,
618         source         = source,
619         target         = modname,
620         samba_cflags   = CURRENT_CFLAGS(bld, modname, cflags,
621                                         allow_warnings=allow_warnings,
622                                         hide_symbols=hide_symbols),
623         depends_on     = depends_on,
624         samba_deps     = TO_LIST(deps),
625         samba_includes = includes,
626         local_include  = local_include,
627         local_include_first  = local_include_first,
628         global_include = global_include,
629         samba_subsystem= subsystem_name,
630         samba_use_hostcc = use_hostcc,
631         samba_use_global_deps = use_global_deps,
632         )
633
634     if cflags_end is not None:
635         t.samba_cflags.extend(TO_LIST(cflags_end))
636
637     if autoproto is not None:
638         bld.SAMBA_AUTOPROTO(autoproto, source + TO_LIST(autoproto_extra_source))
639     if public_headers is not None:
640         bld.PUBLIC_HEADERS(public_headers, header_path=header_path,
641                            public_headers_install=public_headers_install)
642     return t
643
644
645 Build.BuildContext.SAMBA_SUBSYSTEM = SAMBA_SUBSYSTEM
646
647
648 def SAMBA_GENERATOR(bld, name, rule, source='', target='',
649                     group='generators', enabled=True,
650                     public_headers=None,
651                     public_headers_install=True,
652                     private_headers=None,
653                     header_path=None,
654                     vars=None,
655                     dep_vars=[],
656                     always=False):
657     '''A generic source generator target'''
658
659     if not SET_TARGET_TYPE(bld, name, 'GENERATOR'):
660         return
661
662     if not enabled:
663         return
664
665     dep_vars.append('ruledeps')
666     dep_vars.append('SAMBA_GENERATOR_VARS')
667
668     bld.SET_BUILD_GROUP(group)
669     t = bld(
670         rule=rule,
671         source=bld.EXPAND_VARIABLES(source, vars=vars),
672         target=target,
673         shell=isinstance(rule, str),
674         update_outputs=True,
675         before='cc',
676         ext_out='.c',
677         samba_type='GENERATOR',
678         dep_vars = dep_vars,
679         name=name)
680
681     if vars is None:
682         vars = {}
683     t.env.SAMBA_GENERATOR_VARS = vars
684
685     if always:
686         t.always = True
687
688     if public_headers is not None:
689         bld.PUBLIC_HEADERS(public_headers, header_path=header_path,
690                            public_headers_install=public_headers_install)
691     return t
692 Build.BuildContext.SAMBA_GENERATOR = SAMBA_GENERATOR
693
694
695
696 @Utils.run_once
697 def SETUP_BUILD_GROUPS(bld):
698     '''setup build groups used to ensure that the different build
699     phases happen consecutively'''
700     bld.p_ln = bld.srcnode # we do want to see all targets!
701     bld.env['USING_BUILD_GROUPS'] = True
702     bld.add_group('setup')
703     bld.add_group('build_compiler_source')
704     bld.add_group('vscripts')
705     bld.add_group('base_libraries')
706     bld.add_group('generators')
707     bld.add_group('compiler_prototypes')
708     bld.add_group('compiler_libraries')
709     bld.add_group('build_compilers')
710     bld.add_group('build_source')
711     bld.add_group('prototypes')
712     bld.add_group('headers')
713     bld.add_group('main')
714     bld.add_group('symbolcheck')
715     bld.add_group('syslibcheck')
716     bld.add_group('final')
717 Build.BuildContext.SETUP_BUILD_GROUPS = SETUP_BUILD_GROUPS
718
719
720 def SET_BUILD_GROUP(bld, group):
721     '''set the current build group'''
722     if not 'USING_BUILD_GROUPS' in bld.env:
723         return
724     bld.set_group(group)
725 Build.BuildContext.SET_BUILD_GROUP = SET_BUILD_GROUP
726
727
728
729 @conf
730 def ENABLE_TIMESTAMP_DEPENDENCIES(conf):
731     """use timestamps instead of file contents for deps
732     this currently doesn't work"""
733     def h_file(filename):
734         import stat
735         st = os.stat(filename)
736         if stat.S_ISDIR(st[stat.ST_MODE]): raise IOError('not a file')
737         m = Utils.md5()
738         m.update(str(st.st_mtime))
739         m.update(str(st.st_size))
740         m.update(filename)
741         return m.digest()
742     Utils.h_file = h_file
743
744
745 def SAMBA_SCRIPT(bld, name, pattern, installdir, installname=None):
746     '''used to copy scripts from the source tree into the build directory
747        for use by selftest'''
748
749     source = bld.path.ant_glob(pattern, flat=True)
750
751     bld.SET_BUILD_GROUP('build_source')
752     for s in TO_LIST(source):
753         iname = s
754         if installname is not None:
755             iname = installname
756         target = os.path.join(installdir, iname)
757         tgtdir = os.path.dirname(os.path.join(bld.srcnode.abspath(bld.env), '..', target))
758         mkdir_p(tgtdir)
759         link_src = os.path.normpath(os.path.join(bld.curdir, s))
760         link_dst = os.path.join(tgtdir, os.path.basename(iname))
761         if os.path.islink(link_dst) and os.readlink(link_dst) == link_src:
762             continue
763         if os.path.exists(link_dst):
764             os.unlink(link_dst)
765         Logs.info("symlink: %s -> %s/%s" % (s, installdir, iname))
766         os.symlink(link_src, link_dst)
767 Build.BuildContext.SAMBA_SCRIPT = SAMBA_SCRIPT
768
769
770 def copy_and_fix_python_path(task):
771     pattern='sys.path.insert(0, "bin/python")'
772     if task.env["PYTHONARCHDIR"] in sys.path and task.env["PYTHONDIR"] in sys.path:
773         replacement = ""
774     elif task.env["PYTHONARCHDIR"] == task.env["PYTHONDIR"]:
775         replacement="""sys.path.insert(0, "%s")""" % task.env["PYTHONDIR"]
776     else:
777         replacement="""sys.path.insert(0, "%s")
778 sys.path.insert(1, "%s")""" % (task.env["PYTHONARCHDIR"], task.env["PYTHONDIR"])
779
780     if task.env["PYTHON"][0] == "/":
781         replacement_shebang = "#!%s\n" % task.env["PYTHON"]
782     else:
783         replacement_shebang = "#!/usr/bin/env %s\n" % task.env["PYTHON"]
784
785     installed_location=task.outputs[0].bldpath(task.env)
786     source_file = open(task.inputs[0].srcpath(task.env))
787     installed_file = open(installed_location, 'w')
788     lineno = 0
789     for line in source_file:
790         newline = line
791         if (lineno == 0 and task.env["PYTHON_SPECIFIED"] is True and
792                 line[:2] == "#!"):
793             newline = replacement_shebang
794         elif pattern in line:
795             newline = line.replace(pattern, replacement)
796         installed_file.write(newline)
797         lineno = lineno + 1
798     installed_file.close()
799     os.chmod(installed_location, 0755)
800     return 0
801
802 def copy_and_fix_perl_path(task):
803     pattern='use lib "$RealBin/lib";'
804
805     replacement = ""
806     if not task.env["PERL_LIB_INSTALL_DIR"] in task.env["PERL_INC"]:
807          replacement = 'use lib "%s";' % task.env["PERL_LIB_INSTALL_DIR"]
808
809     if task.env["PERL"][0] == "/":
810         replacement_shebang = "#!%s\n" % task.env["PERL"]
811     else:
812         replacement_shebang = "#!/usr/bin/env %s\n" % task.env["PERL"]
813
814     installed_location=task.outputs[0].bldpath(task.env)
815     source_file = open(task.inputs[0].srcpath(task.env))
816     installed_file = open(installed_location, 'w')
817     lineno = 0
818     for line in source_file:
819         newline = line
820         if lineno == 0 and task.env["PERL_SPECIFIED"] == True and line[:2] == "#!":
821             newline = replacement_shebang
822         elif pattern in line:
823             newline = line.replace(pattern, replacement)
824         installed_file.write(newline)
825         lineno = lineno + 1
826     installed_file.close()
827     os.chmod(installed_location, 0755)
828     return 0
829
830
831 def install_file(bld, destdir, file, chmod=MODE_644, flat=False,
832                  python_fixup=False, perl_fixup=False,
833                  destname=None, base_name=None):
834     '''install a file'''
835     destdir = bld.EXPAND_VARIABLES(destdir)
836     if not destname:
837         destname = file
838         if flat:
839             destname = os.path.basename(destname)
840     dest = os.path.join(destdir, destname)
841     if python_fixup:
842         # fix the path python will use to find Samba modules
843         inst_file = file + '.inst'
844         bld.SAMBA_GENERATOR('python_%s' % destname,
845                             rule=copy_and_fix_python_path,
846                             dep_vars=["PYTHON","PYTHON_SPECIFIED","PYTHONDIR","PYTHONARCHDIR"],
847                             source=file,
848                             target=inst_file)
849         file = inst_file
850     if perl_fixup:
851         # fix the path perl will use to find Samba modules
852         inst_file = file + '.inst'
853         bld.SAMBA_GENERATOR('perl_%s' % destname,
854                             rule=copy_and_fix_perl_path,
855                             dep_vars=["PERL","PERL_SPECIFIED","PERL_LIB_INSTALL_DIR"],
856                             source=file,
857                             target=inst_file)
858         file = inst_file
859     if base_name:
860         file = os.path.join(base_name, file)
861     bld.install_as(dest, file, chmod=chmod)
862
863
864 def INSTALL_FILES(bld, destdir, files, chmod=MODE_644, flat=False,
865                   python_fixup=False, perl_fixup=False,
866                   destname=None, base_name=None):
867     '''install a set of files'''
868     for f in TO_LIST(files):
869         install_file(bld, destdir, f, chmod=chmod, flat=flat,
870                      python_fixup=python_fixup, perl_fixup=perl_fixup,
871                      destname=destname, base_name=base_name)
872 Build.BuildContext.INSTALL_FILES = INSTALL_FILES
873
874
875 def INSTALL_WILDCARD(bld, destdir, pattern, chmod=MODE_644, flat=False,
876                      python_fixup=False, exclude=None, trim_path=None):
877     '''install a set of files matching a wildcard pattern'''
878     files=TO_LIST(bld.path.ant_glob(pattern, flat=True))
879     if trim_path:
880         files2 = []
881         for f in files:
882             files2.append(os_path_relpath(f, trim_path))
883         files = files2
884
885     if exclude:
886         for f in files[:]:
887             if fnmatch.fnmatch(f, exclude):
888                 files.remove(f)
889     INSTALL_FILES(bld, destdir, files, chmod=chmod, flat=flat,
890                   python_fixup=python_fixup, base_name=trim_path)
891 Build.BuildContext.INSTALL_WILDCARD = INSTALL_WILDCARD
892
893 def INSTALL_DIR(bld, path, chmod=0o755, env=None):
894     """Install a directory if it doesn't exist, always set permissions."""
895
896     if not path:
897         return []
898
899     destpath = bld.get_install_path(path, env)
900
901     if bld.is_install > 0:
902         if not os.path.isdir(destpath):
903             try:
904                 os.makedirs(destpath)
905                 os.chmod(destpath, chmod)
906             except OSError as e:
907                 if not os.path.isdir(destpath):
908                     raise Utils.WafError("Cannot create the folder '%s' (error: %s)" % (path, e))
909 Build.BuildContext.INSTALL_DIR = INSTALL_DIR
910
911 def INSTALL_DIRS(bld, destdir, dirs, chmod=0o755, env=None):
912     '''install a set of directories'''
913     destdir = bld.EXPAND_VARIABLES(destdir)
914     dirs = bld.EXPAND_VARIABLES(dirs)
915     for d in TO_LIST(dirs):
916         INSTALL_DIR(bld, os.path.join(destdir, d), chmod, env)
917 Build.BuildContext.INSTALL_DIRS = INSTALL_DIRS
918
919
920 def MANPAGES(bld, manpages, install):
921     '''build and install manual pages'''
922     bld.env.MAN_XSL = 'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
923     for m in manpages.split():
924         source = m + '.xml'
925         bld.SAMBA_GENERATOR(m,
926                             source=source,
927                             target=m,
928                             group='final',
929                             rule='${XSLTPROC} --xinclude -o ${TGT} --nonet ${MAN_XSL} ${SRC}'
930                             )
931         if install:
932             bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
933 Build.BuildContext.MANPAGES = MANPAGES
934
935 def SAMBAMANPAGES(bld, manpages, extra_source=None):
936     '''build and install manual pages'''
937     bld.env.SAMBA_EXPAND_XSL = bld.srcnode.abspath() + '/docs-xml/xslt/expand-sambadoc.xsl'
938     bld.env.SAMBA_MAN_XSL = bld.srcnode.abspath() + '/docs-xml/xslt/man.xsl'
939     bld.env.SAMBA_CATALOG = bld.srcnode.abspath() + '/bin/default/docs-xml/build/catalog.xml'
940     bld.env.SAMBA_CATALOGS = 'file:///etc/xml/catalog file:///usr/local/share/xml/catalog file://' + bld.env.SAMBA_CATALOG
941
942     for m in manpages.split():
943         source = m + '.xml'
944         if extra_source is not None:
945             source = [source, extra_source]
946         bld.SAMBA_GENERATOR(m,
947                             source=source,
948                             target=m,
949                             group='final',
950                             dep_vars=['SAMBA_MAN_XSL', 'SAMBA_EXPAND_XSL', 'SAMBA_CATALOG'],
951                             rule='''XML_CATALOG_FILES="${SAMBA_CATALOGS}"
952                                     export XML_CATALOG_FILES
953                                     ${XSLTPROC} --xinclude --stringparam noreference 0 -o ${TGT}.xml --nonet ${SAMBA_EXPAND_XSL} ${SRC[0].abspath(env)}
954                                     ${XSLTPROC} --nonet -o ${TGT} ${SAMBA_MAN_XSL} ${TGT}.xml'''
955                             )
956         bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m, flat=True)
957 Build.BuildContext.SAMBAMANPAGES = SAMBAMANPAGES
958
959 #############################################################
960 # give a nicer display when building different types of files
961 def progress_display(self, msg, fname):
962     col1 = Logs.colors(self.color)
963     col2 = Logs.colors.NORMAL
964     total = self.position[1]
965     n = len(str(total))
966     fs = '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n, n, msg)
967     return fs % (self.position[0], self.position[1], col1, fname, col2)
968
969 def link_display(self):
970     if Options.options.progress_bar != 0:
971         return Task.Task.old_display(self)
972     fname = self.outputs[0].bldpath(self.env)
973     return progress_display(self, 'Linking', fname)
974 Task.TaskBase.classes['cc_link'].display = link_display
975
976 def samba_display(self):
977     if Options.options.progress_bar != 0:
978         return Task.Task.old_display(self)
979
980     targets    = LOCAL_CACHE(self, 'TARGET_TYPE')
981     if self.name in targets:
982         target_type = targets[self.name]
983         type_map = { 'GENERATOR' : 'Generating',
984                      'PROTOTYPE' : 'Generating'
985                      }
986         if target_type in type_map:
987             return progress_display(self, type_map[target_type], self.name)
988
989     if len(self.inputs) == 0:
990         return Task.Task.old_display(self)
991
992     fname = self.inputs[0].bldpath(self.env)
993     if fname[0:3] == '../':
994         fname = fname[3:]
995     ext_loc = fname.rfind('.')
996     if ext_loc == -1:
997         return Task.Task.old_display(self)
998     ext = fname[ext_loc:]
999
1000     ext_map = { '.idl' : 'Compiling IDL',
1001                 '.et'  : 'Compiling ERRTABLE',
1002                 '.asn1': 'Compiling ASN1',
1003                 '.c'   : 'Compiling' }
1004     if ext in ext_map:
1005         return progress_display(self, ext_map[ext], fname)
1006     return Task.Task.old_display(self)
1007
1008 Task.TaskBase.classes['Task'].old_display = Task.TaskBase.classes['Task'].display
1009 Task.TaskBase.classes['Task'].display = samba_display
1010
1011
1012 @after('apply_link')
1013 @feature('cshlib')
1014 def apply_bundle_remove_dynamiclib_patch(self):
1015     if self.env['MACBUNDLE'] or getattr(self,'mac_bundle',False):
1016         if not getattr(self,'vnum',None):
1017             try:
1018                 self.env['LINKFLAGS'].remove('-dynamiclib')
1019                 self.env['LINKFLAGS'].remove('-single_module')
1020             except ValueError:
1021                 pass