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