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