1 # a waf tool to add autoconf-like macros to the configure section
2 # and for SAMBA_ macros for building libraries, binaries etc
4 import Build
, os
, Options
, Task
, Utils
, cc
, TaskGen
, fnmatch
, re
, shutil
, Logs
5 from Configure
import conf
7 from samba_utils
import SUBST_VARS_RECURSIVE
9 # bring in the other samba modules
10 from samba_optimisation
import *
11 from samba_utils
import *
12 from samba_autoconf
import *
13 from samba_patterns
import *
14 from samba_pidl
import *
15 from samba_errtable
import *
16 from samba_asn1
import *
17 from samba_autoproto
import *
18 from samba_python
import *
19 from samba_deps
import *
20 from samba_bundled
import *
22 import samba_conftests
30 # some systems have broken threading in python
31 if os
.environ
.get('WAF_NOTHREADS') == '1':
36 os
.putenv('PYTHONUNBUFFERED', '1')
39 def SAMBA_BUILD_ENV(conf
):
40 '''create the samba build environment'''
41 conf
.env
['BUILD_DIRECTORY'] = conf
.blddir
42 mkdir_p(os
.path
.join(conf
.blddir
, LIB_PATH
))
43 mkdir_p(os
.path
.join(conf
.blddir
, 'python/samba/dcerpc'))
44 # this allows all of the bin/shared and bin/python targets
45 # to be expressed in terms of build directory paths
46 for p
in ['python','shared']:
47 link_target
= os
.path
.join(conf
.blddir
, 'default/' + p
)
48 if not os
.path
.lexists(link_target
):
49 os
.symlink('../' + p
, link_target
)
51 # get perl to put the blib files in the build directory
52 blib_bld
= os
.path
.join(conf
.blddir
, 'default/pidl/blib')
53 blib_src
= os
.path
.join(conf
.srcdir
, 'pidl/blib')
54 mkdir_p(blib_bld
+ '/man1')
55 mkdir_p(blib_bld
+ '/man3')
56 if os
.path
.islink(blib_src
):
58 elif os
.path
.exists(blib_src
):
59 shutil
.rmtree(blib_src
)
62 def ADD_INIT_FUNCTION(bld
, subsystem
, target
, init_function
):
63 '''add an init_function to the list for a subsystem'''
64 if init_function
is None:
66 bld
.ASSERT(subsystem
is not None, "You must specify a subsystem for init_function '%s'" % init_function
)
67 cache
= LOCAL_CACHE(bld
, 'INIT_FUNCTIONS')
68 if not subsystem
in cache
:
70 cache
[subsystem
].append( { 'TARGET':target
, 'INIT_FUNCTION':init_function
} )
71 Build
.BuildContext
.ADD_INIT_FUNCTION
= ADD_INIT_FUNCTION
75 #################################################################
76 def SAMBA_LIBRARY(bld
, libname
, source
,
85 external_library
=False,
95 target_type
='LIBRARY',
96 bundled_extension
=True,
99 '''define a Samba library'''
102 SET_TARGET_TYPE(bld
, libname
, 'DISABLED')
105 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
107 # remember empty libraries, so we can strip the dependencies
108 if (source
== '') or (source
== []):
109 SET_TARGET_TYPE(bld
, libname
, 'EMPTY')
112 if target_type
!= 'PYTHON' and BUILTIN_LIBRARY(bld
, libname
):
115 obj_target
= libname
+ '.objlist'
117 # first create a target for building the object files for this library
118 # by separating in this way, we avoid recompiling the C files
119 # separately for the install library and the build library
120 bld
.SAMBA_SUBSYSTEM(obj_target
,
123 public_deps
= public_deps
,
125 public_headers
= public_headers
,
126 header_path
= header_path
,
129 autoproto
= autoproto
,
130 depends_on
= depends_on
,
131 needs_python
= needs_python
,
132 local_include
= local_include
)
134 if libname
== obj_target
:
137 if not SET_TARGET_TYPE(bld
, libname
, target_type
):
140 # the library itself will depend on that object target
141 deps
+= ' ' + public_deps
143 deps
.append(obj_target
)
145 if target_type
== 'PYTHON':
146 bundled_name
= libname
148 bundled_name
= BUNDLED_NAME(bld
, libname
, bundled_extension
)
150 features
= 'cc cshlib symlink_lib install_lib'
151 if target_type
== 'PYTHON':
154 features
+= ' pyembed'
156 bld
.SET_BUILD_GROUP(group
)
160 target
= bundled_name
,
161 samba_cflags
= CURRENT_CFLAGS(bld
, libname
, cflags
),
162 depends_on
= depends_on
,
164 samba_includes
= includes
,
165 local_include
= local_include
,
168 samba_inst_path
= install_path
,
170 samba_realname
= realname
,
171 samba_install
= install
175 t
.link_name
= link_name
177 if autoproto
is not None:
178 bld
.SAMBA_AUTOPROTO(autoproto
, source
)
180 if public_headers
is not None:
181 bld
.PUBLIC_HEADERS(public_headers
, header_path
=header_path
)
183 if pc_files
is not None:
184 bld
.PKG_CONFIG_FILES(pc_files
, vnum
=vnum
)
186 Build
.BuildContext
.SAMBA_LIBRARY
= SAMBA_LIBRARY
189 #################################################################
190 def SAMBA_BINARY(bld
, binname
, source
,
210 '''define a Samba binary'''
212 if not SET_TARGET_TYPE(bld
, binname
, 'BINARY'):
215 features
= 'cc cprogram symlink_bin install_bin'
217 features
+= ' pyembed'
219 obj_target
= binname
+ '.objlist'
221 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
223 # first create a target for building the object files for this binary
224 # by separating in this way, we avoid recompiling the C files
225 # separately for the install binary and the build binary
226 bld
.SAMBA_SUBSYSTEM(obj_target
,
232 autoproto
= autoproto
,
233 subsystem_name
= subsystem_name
,
234 needs_python
= needs_python
,
235 local_include
= local_include
)
237 bld
.SET_BUILD_GROUP(group
)
239 # the binary itself will depend on that object target
241 deps
.append(obj_target
)
247 samba_cflags
= CURRENT_CFLAGS(bld
, binname
, cflags
),
249 samba_includes
= includes
,
250 local_include
= local_include
,
251 samba_modules
= modules
,
253 samba_subsystem
= subsystem_name
,
255 samba_inst_path
= install_path
,
256 samba_install
= install
259 # setup the subsystem_name as an alias for the real
260 # binary name, so it can be found when expanding
261 # subsystem dependencies
262 if subsystem_name
is not None:
263 bld
.TARGET_ALIAS(subsystem_name
, binname
)
265 if autoproto
is not None:
266 bld
.SAMBA_AUTOPROTO(autoproto
, source
)
267 if public_headers
is not None:
268 bld
.PUBLIC_HEADERS(public_headers
, header_path
=header_path
)
269 Build
.BuildContext
.SAMBA_BINARY
= SAMBA_BINARY
272 #################################################################
273 def SAMBA_MODULE(bld
, modname
, source
,
279 autoproto_extra_source
='',
282 internal_module
=True,
286 '''define a Samba module.'''
288 # we add the init function regardless of whether the module
289 # is enabled or not, as we need to generate a null list if
291 bld
.ADD_INIT_FUNCTION(subsystem
, modname
, init_function
)
293 if internal_module
or BUILTIN_LIBRARY(bld
, modname
):
294 # treat internal modules as subsystems for now
295 SAMBA_SUBSYSTEM(bld
, modname
, source
,
299 autoproto_extra_source
=autoproto_extra_source
,
301 local_include
=local_include
,
306 SET_TARGET_TYPE(bld
, modname
, 'DISABLED')
309 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
311 # remember empty modules, so we can strip the dependencies
312 if (source
== '') or (source
== []):
313 SET_TARGET_TYPE(bld
, modname
, 'EMPTY')
316 if not SET_TARGET_TYPE(bld
, modname
, 'MODULE'):
319 if subsystem
is not None:
320 deps
+= ' ' + subsystem
322 bld
.SET_BUILD_GROUP('main')
327 samba_cflags
= CURRENT_CFLAGS(bld
, modname
, cflags
),
328 samba_includes
= includes
,
329 local_include
= local_include
,
330 samba_deps
= TO_LIST(deps
)
333 if autoproto
is not None:
334 bld
.SAMBA_AUTOPROTO(autoproto
, source
+ ' ' + autoproto_extra_source
)
336 Build
.BuildContext
.SAMBA_MODULE
= SAMBA_MODULE
339 #################################################################
340 def SAMBA_SUBSYSTEM(bld
, modname
, source
,
349 init_function_sentinal
=None,
350 heimdal_autoproto
=None,
351 heimdal_autoproto_options
=None,
352 heimdal_autoproto_private
=None,
354 autoproto_extra_source
='',
357 local_include_first
=True,
362 '''define a Samba subsystem'''
365 SET_TARGET_TYPE(bld
, modname
, 'DISABLED')
368 # remember empty subsystems, so we can strip the dependencies
369 if (source
== '') or (source
== []):
370 SET_TARGET_TYPE(bld
, modname
, 'EMPTY')
373 if not SET_TARGET_TYPE(bld
, modname
, 'SUBSYSTEM'):
376 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
378 deps
+= ' ' + public_deps
380 bld
.SET_BUILD_GROUP(group
)
390 samba_cflags
= CURRENT_CFLAGS(bld
, modname
, cflags
),
391 depends_on
= depends_on
,
392 samba_deps
= TO_LIST(deps
),
393 samba_includes
= includes
,
394 local_include
= local_include
,
395 local_include_first
= local_include_first
,
396 samba_subsystem
= subsystem_name
399 if cflags_end
is not None:
400 t
.samba_cflags
.extend(TO_LIST(cflags_end
))
402 if heimdal_autoproto
is not None:
403 bld
.HEIMDAL_AUTOPROTO(heimdal_autoproto
, source
, options
=heimdal_autoproto_options
)
404 if heimdal_autoproto_private
is not None:
405 bld
.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private
, source
)
406 if autoproto
is not None:
407 bld
.SAMBA_AUTOPROTO(autoproto
, source
+ ' ' + autoproto_extra_source
)
408 if public_headers
is not None:
409 bld
.PUBLIC_HEADERS(public_headers
, header_path
=header_path
)
413 Build
.BuildContext
.SAMBA_SUBSYSTEM
= SAMBA_SUBSYSTEM
416 def SAMBA_GENERATOR(bld
, name
, rule
, source
='', target
='',
417 group
='generators', enabled
=True,
421 '''A generic source generator target'''
423 if not SET_TARGET_TYPE(bld
, name
, 'GENERATOR'):
429 bld
.SET_BUILD_GROUP(group
)
432 source
=bld
.EXPAND_VARIABLES(source
, vars=vars),
434 shell
=isinstance(rule
, str),
440 if public_headers
is not None:
441 bld
.PUBLIC_HEADERS(public_headers
, header_path
=header_path
)
443 Build
.BuildContext
.SAMBA_GENERATOR
= SAMBA_GENERATOR
448 def SETUP_BUILD_GROUPS(bld
):
449 '''setup build groups used to ensure that the different build
450 phases happen consecutively'''
451 bld
.p_ln
= bld
.srcnode
# we do want to see all targets!
452 bld
.env
['USING_BUILD_GROUPS'] = True
453 bld
.add_group('setup')
454 bld
.add_group('build_compiler_source')
455 bld
.add_group('base_libraries')
456 bld
.add_group('generators')
457 bld
.add_group('compiler_prototypes')
458 bld
.add_group('compiler_libraries')
459 bld
.add_group('build_compilers')
460 bld
.add_group('build_source')
461 bld
.add_group('prototypes')
462 bld
.add_group('main')
463 bld
.add_group('binaries')
464 bld
.add_group('final')
465 Build
.BuildContext
.SETUP_BUILD_GROUPS
= SETUP_BUILD_GROUPS
468 def SET_BUILD_GROUP(bld
, group
):
469 '''set the current build group'''
470 if not 'USING_BUILD_GROUPS' in bld
.env
:
473 Build
.BuildContext
.SET_BUILD_GROUP
= SET_BUILD_GROUP
478 def ENABLE_TIMESTAMP_DEPENDENCIES(conf
):
479 """use timestamps instead of file contents for deps
480 this currently doesn't work"""
481 def h_file(filename
):
483 st
= os
.stat(filename
)
484 if stat
.S_ISDIR(st
[stat
.ST_MODE
]): raise IOError('not a file')
486 m
.update(str(st
.st_mtime
))
487 m
.update(str(st
.st_size
))
490 Utils
.h_file
= h_file
494 t
= Task
.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
495 shell
=True, color
='PINK', ext_in
='.bin')
498 @feature('copy_script')
499 @before('apply_link')
500 def copy_script(self
):
501 tsk
= self
.create_task('copy_script', self
.allnodes
[0])
502 tsk
.env
.TARGET
= self
.target
504 def SAMBA_SCRIPT(bld
, name
, pattern
, installdir
, installname
=None):
505 '''used to copy scripts from the source tree into the build directory
506 for use by selftest'''
508 source
= bld
.path
.ant_glob(pattern
)
510 bld
.SET_BUILD_GROUP('build_source')
511 for s
in TO_LIST(source
):
513 if installname
!= None:
515 target
= os
.path
.join(installdir
, iname
)
516 tgtdir
= os
.path
.dirname(os
.path
.join(bld
.srcnode
.abspath(bld
.env
), '..', target
))
518 t
= bld(features
='copy_script',
523 t
.env
.LINK_TARGET
= target
525 Build
.BuildContext
.SAMBA_SCRIPT
= SAMBA_SCRIPT
528 def install_file(bld
, destdir
, file, chmod
=O644
, flat
=False,
529 python_fixup
=False, destname
=None, base_name
=None):
531 destdir
= bld
.EXPAND_VARIABLES(destdir
)
535 destname
= os
.path
.basename(destname
)
536 dest
= os
.path
.join(destdir
, destname
)
538 # fixup the python path it will use to find Samba modules
539 inst_file
= file + '.inst'
540 bld
.SAMBA_GENERATOR('python_%s' % destname
,
541 rule
="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
546 file = os
.path
.join(base_name
, file)
547 bld
.install_as(dest
, file, chmod
=chmod
)
550 def INSTALL_FILES(bld
, destdir
, files
, chmod
=O644
, flat
=False,
551 python_fixup
=False, destname
=None, base_name
=None):
552 '''install a set of files'''
553 for f
in TO_LIST(files
):
554 install_file(bld
, destdir
, f
, chmod
=chmod
, flat
=flat
,
555 python_fixup
=python_fixup
, destname
=destname
,
557 Build
.BuildContext
.INSTALL_FILES
= INSTALL_FILES
560 def INSTALL_WILDCARD(bld
, destdir
, pattern
, chmod
=O644
, flat
=False,
561 python_fixup
=False, exclude
=None, trim_path
=None):
562 '''install a set of files matching a wildcard pattern'''
563 files
=TO_LIST(bld
.path
.ant_glob(pattern
))
567 files2
.append(os_path_relpath(f
, trim_path
))
572 if fnmatch
.fnmatch(f
, exclude
):
574 INSTALL_FILES(bld
, destdir
, files
, chmod
=chmod
, flat
=flat
,
575 python_fixup
=python_fixup
, base_name
=trim_path
)
576 Build
.BuildContext
.INSTALL_WILDCARD
= INSTALL_WILDCARD
579 def PUBLIC_HEADERS(bld
, public_headers
, header_path
=None):
580 '''install some headers
582 header_path may either be a string that is added to the INCLUDEDIR,
583 or it can be a dictionary of wildcard patterns which map to destination
584 directories relative to INCLUDEDIR
586 dest
= '${INCLUDEDIR}'
587 if isinstance(header_path
, str):
588 dest
+= '/' + header_path
589 for h
in TO_LIST(public_headers
):
591 if isinstance(header_path
, list):
592 for (p1
, dir) in header_path
:
596 if fnmatch
.fnmatch(h
, p2
):
598 hdest
= os
.path
.join(hdest
, dir)
601 if found_match
: break
602 if h
.find(':') != -1:
604 INSTALL_FILES(bld
, hdest
, hs
[0], flat
=True, destname
=hs
[1])
606 INSTALL_FILES(bld
, hdest
, h
, flat
=True)
607 Build
.BuildContext
.PUBLIC_HEADERS
= PUBLIC_HEADERS
610 def subst_at_vars(task
):
611 '''substiture @VAR@ style variables in a file'''
612 src
= task
.inputs
[0].srcpath(task
.env
)
613 tgt
= task
.outputs
[0].bldpath(task
.env
)
619 a
= re
.split('(@\w+@)', s
)
621 back_sub
= [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
623 if re
.match('@\w+@', v
):
625 if not vname
in task
.env
and vname
.upper() in task
.env
:
626 vname
= vname
.upper()
627 if not vname
in task
.env
:
628 print("Unknown substitution %s in %s" % (v
, task
.name
))
630 v
= SUBST_VARS_RECURSIVE(task
.env
[vname
], task
.env
)
631 # now we back substitute the allowed pc vars
632 for (b
, m
) in back_sub
:
637 contents
= ''.join(out
)
639 s
= f
.write(contents
)
645 def PKG_CONFIG_FILES(bld
, pc_files
, vnum
=None):
646 '''install some pkg_config pc files'''
647 dest
= '${PKGCONFIGDIR}'
648 dest
= bld
.EXPAND_VARIABLES(dest
)
649 for f
in TO_LIST(pc_files
):
650 base
=os
.path
.basename(f
)
651 t
= bld
.SAMBA_GENERATOR('PKGCONFIG_%s' % base
,
656 t
.env
.PACKAGE_VERSION
= vnum
657 INSTALL_FILES(bld
, dest
, f
, flat
=True, destname
=base
)
658 Build
.BuildContext
.PKG_CONFIG_FILES
= PKG_CONFIG_FILES
662 #############################################################
663 # give a nicer display when building different types of files
664 def progress_display(self
, msg
, fname
):
665 col1
= Logs
.colors(self
.color
)
666 col2
= Logs
.colors
.NORMAL
667 total
= self
.position
[1]
669 fs
= '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n
, n
, msg
)
670 return fs
% (self
.position
[0], self
.position
[1], col1
, fname
, col2
)
672 def link_display(self
):
673 if Options
.options
.progress_bar
!= 0:
674 return Task
.Task
.old_display(self
)
675 fname
= self
.outputs
[0].bldpath(self
.env
)
676 return progress_display(self
, 'Linking', fname
)
677 Task
.TaskBase
.classes
['cc_link'].display
= link_display
679 def samba_display(self
):
680 if Options
.options
.progress_bar
!= 0:
681 return Task
.Task
.old_display(self
)
683 targets
= LOCAL_CACHE(self
, 'TARGET_TYPE')
684 if self
.name
in targets
:
685 target_type
= targets
[self
.name
]
686 type_map
= { 'GENERATOR' : 'Generating',
687 'PROTOTYPE' : 'Generating'
689 if target_type
in type_map
:
690 return progress_display(self
, type_map
[target_type
], self
.name
)
692 fname
= self
.inputs
[0].bldpath(self
.env
)
693 if fname
[0:3] == '../':
695 ext_loc
= fname
.rfind('.')
697 return Task
.Task
.old_display(self
)
698 ext
= fname
[ext_loc
:]
700 ext_map
= { '.idl' : 'Compiling IDL',
701 '.et' : 'Compiling ERRTABLE',
702 '.asn1': 'Compiling ASN1',
705 return progress_display(self
, ext_map
[ext
], fname
)
706 return Task
.Task
.old_display(self
)
708 Task
.TaskBase
.classes
['Task'].old_display
= Task
.TaskBase
.classes
['Task'].display
709 Task
.TaskBase
.classes
['Task'].display
= samba_display