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
, Constants
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
32 # some systems have broken threading in python
33 if os
.environ
.get('WAF_NOTHREADS') == '1':
38 os
.putenv('PYTHONUNBUFFERED', '1')
41 if Constants
.HEXVERSION
< 0x105016:
43 Please use the version of waf that comes with Samba, not
44 a system installed version. See http://wiki.samba.org/index.php/Waf
47 Alternatively, please use ./autogen-waf.sh, and then
48 run ./configure and make as usual. That will call the right version of waf.
54 def SAMBA_BUILD_ENV(conf
):
55 '''create the samba build environment'''
56 conf
.env
.BUILD_DIRECTORY
= conf
.blddir
57 mkdir_p(os
.path
.join(conf
.blddir
, LIB_PATH
))
58 mkdir_p(os
.path
.join(conf
.blddir
, 'python/samba/dcerpc'))
59 # this allows all of the bin/shared and bin/python targets
60 # to be expressed in terms of build directory paths
61 mkdir_p(os
.path
.join(conf
.blddir
, 'default'))
62 for p
in ['python','shared']:
63 link_target
= os
.path
.join(conf
.blddir
, 'default/' + p
)
64 if not os
.path
.lexists(link_target
):
65 os
.symlink('../' + p
, link_target
)
67 # get perl to put the blib files in the build directory
68 blib_bld
= os
.path
.join(conf
.blddir
, 'default/pidl/blib')
69 blib_src
= os
.path
.join(conf
.srcdir
, 'pidl/blib')
70 mkdir_p(blib_bld
+ '/man1')
71 mkdir_p(blib_bld
+ '/man3')
72 if os
.path
.islink(blib_src
):
74 elif os
.path
.exists(blib_src
):
75 shutil
.rmtree(blib_src
)
78 def ADD_INIT_FUNCTION(bld
, subsystem
, target
, init_function
):
79 '''add an init_function to the list for a subsystem'''
80 if init_function
is None:
82 bld
.ASSERT(subsystem
is not None, "You must specify a subsystem for init_function '%s'" % init_function
)
83 cache
= LOCAL_CACHE(bld
, 'INIT_FUNCTIONS')
84 if not subsystem
in cache
:
86 cache
[subsystem
].append( { 'TARGET':target
, 'INIT_FUNCTION':init_function
} )
87 Build
.BuildContext
.ADD_INIT_FUNCTION
= ADD_INIT_FUNCTION
91 #################################################################
92 def SAMBA_LIBRARY(bld
, libname
, source
,
101 external_library
=False,
111 target_type
='LIBRARY',
112 bundled_extension
=True,
118 '''define a Samba library'''
121 SET_TARGET_TYPE(bld
, libname
, 'DISABLED')
124 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
126 # remember empty libraries, so we can strip the dependencies
127 if ((source
== '') or (source
== [])) and deps
== '' and public_deps
== '':
128 SET_TARGET_TYPE(bld
, libname
, 'EMPTY')
131 if target_type
!= 'PYTHON' and BUILTIN_LIBRARY(bld
, libname
):
134 obj_target
= libname
+ '.objlist'
136 # first create a target for building the object files for this library
137 # by separating in this way, we avoid recompiling the C files
138 # separately for the install library and the build library
139 bld
.SAMBA_SUBSYSTEM(obj_target
,
142 public_deps
= public_deps
,
144 public_headers
= public_headers
,
145 header_path
= header_path
,
148 autoproto
= autoproto
,
149 depends_on
= depends_on
,
150 needs_python
= needs_python
,
151 hide_symbols
= hide_symbols
,
152 local_include
= local_include
)
154 if libname
== obj_target
:
157 if not SET_TARGET_TYPE(bld
, libname
, target_type
):
160 # the library itself will depend on that object target
161 deps
+= ' ' + public_deps
163 deps
.append(obj_target
)
165 if target_type
== 'PYTHON' or realname
:
166 bundled_name
= libname
168 bundled_name
= BUNDLED_NAME(bld
, libname
, bundled_extension
)
170 features
= 'cc cshlib symlink_lib install_lib'
171 if target_type
== 'PYTHON':
174 features
+= ' pyembed'
176 features
+= ' abi_check'
179 abi_file
= os
.path
.join(bld
.curdir
, abi_file
)
181 bld
.SET_BUILD_GROUP(group
)
185 target
= bundled_name
,
186 samba_cflags
= CURRENT_CFLAGS(bld
, libname
, cflags
),
187 depends_on
= depends_on
,
189 samba_includes
= includes
,
190 local_include
= local_include
,
193 samba_inst_path
= install_path
,
195 samba_realname
= realname
,
196 samba_install
= install
,
198 abi_match
= abi_match
201 if realname
and not link_name
:
202 link_name
= 'shared/%s' % realname
205 t
.link_name
= link_name
207 if pc_files
is not None:
208 bld
.PKG_CONFIG_FILES(pc_files
, vnum
=vnum
)
210 Build
.BuildContext
.SAMBA_LIBRARY
= SAMBA_LIBRARY
213 #################################################################
214 def SAMBA_BINARY(bld
, binname
, source
,
224 use_global_deps
=True,
235 '''define a Samba binary'''
238 SET_TARGET_TYPE(bld
, binname
, 'DISABLED')
241 if not SET_TARGET_TYPE(bld
, binname
, 'BINARY'):
244 features
= 'cc cprogram symlink_bin install_bin'
246 features
+= ' pyembed'
248 obj_target
= binname
+ '.objlist'
250 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
251 source
= unique_list(TO_LIST(source
))
253 # first create a target for building the object files for this binary
254 # by separating in this way, we avoid recompiling the C files
255 # separately for the install binary and the build binary
256 bld
.SAMBA_SUBSYSTEM(obj_target
,
262 autoproto
= autoproto
,
263 subsystem_name
= subsystem_name
,
264 needs_python
= needs_python
,
265 local_include
= local_include
,
266 use_hostcc
= use_hostcc
,
267 use_global_deps
= use_global_deps
)
269 bld
.SET_BUILD_GROUP(group
)
271 # the binary itself will depend on that object target
273 deps
.append(obj_target
)
279 samba_cflags
= CURRENT_CFLAGS(bld
, binname
, cflags
),
281 samba_includes
= includes
,
282 local_include
= local_include
,
283 samba_modules
= modules
,
285 samba_subsystem
= subsystem_name
,
287 samba_inst_path
= install_path
,
288 samba_install
= install
291 # setup the subsystem_name as an alias for the real
292 # binary name, so it can be found when expanding
293 # subsystem dependencies
294 if subsystem_name
is not None:
295 bld
.TARGET_ALIAS(subsystem_name
, binname
)
297 Build
.BuildContext
.SAMBA_BINARY
= SAMBA_BINARY
300 #################################################################
301 def SAMBA_MODULE(bld
, modname
, source
,
307 autoproto_extra_source
='',
310 internal_module
=True,
314 '''define a Samba module.'''
316 # we add the init function regardless of whether the module
317 # is enabled or not, as we need to generate a null list if
319 bld
.ADD_INIT_FUNCTION(subsystem
, modname
, init_function
)
321 if internal_module
or BUILTIN_LIBRARY(bld
, modname
):
322 # treat internal modules as subsystems for now
323 SAMBA_SUBSYSTEM(bld
, modname
, source
,
327 autoproto_extra_source
=autoproto_extra_source
,
329 local_include
=local_include
,
334 SET_TARGET_TYPE(bld
, modname
, 'DISABLED')
337 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
338 source
= unique_list(TO_LIST(source
))
340 # remember empty modules, so we can strip the dependencies
341 if ((source
== '') or (source
== [])) and deps
== '' and public_deps
== '':
342 SET_TARGET_TYPE(bld
, modname
, 'EMPTY')
345 if not SET_TARGET_TYPE(bld
, modname
, 'MODULE'):
348 if subsystem
is not None:
349 deps
+= ' ' + subsystem
351 bld
.SET_BUILD_GROUP('main')
356 samba_cflags
= CURRENT_CFLAGS(bld
, modname
, cflags
),
357 samba_includes
= includes
,
358 local_include
= local_include
,
359 samba_deps
= TO_LIST(deps
)
362 if autoproto
is not None:
363 bld
.SAMBA_AUTOPROTO(autoproto
, source
+ TO_LIST(autoproto_extra_source
))
365 Build
.BuildContext
.SAMBA_MODULE
= SAMBA_MODULE
368 #################################################################
369 def SAMBA_SUBSYSTEM(bld
, modname
, source
,
378 init_function_sentinal
=None,
379 heimdal_autoproto
=None,
380 heimdal_autoproto_options
=None,
381 heimdal_autoproto_private
=None,
383 autoproto_extra_source
='',
386 local_include_first
=True,
390 use_global_deps
=True,
394 '''define a Samba subsystem'''
397 SET_TARGET_TYPE(bld
, modname
, 'DISABLED')
400 # remember empty subsystems, so we can strip the dependencies
401 if ((source
== '') or (source
== [])) and deps
== '' and public_deps
== '':
402 SET_TARGET_TYPE(bld
, modname
, 'EMPTY')
405 if not SET_TARGET_TYPE(bld
, modname
, 'SUBSYSTEM'):
408 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
409 source
= unique_list(TO_LIST(source
))
411 deps
+= ' ' + public_deps
413 bld
.SET_BUILD_GROUP(group
)
423 samba_cflags
= CURRENT_CFLAGS(bld
, modname
, cflags
, hide_symbols
=hide_symbols
),
424 depends_on
= depends_on
,
425 samba_deps
= TO_LIST(deps
),
426 samba_includes
= includes
,
427 local_include
= local_include
,
428 local_include_first
= local_include_first
,
429 samba_subsystem
= subsystem_name
,
430 samba_use_hostcc
= use_hostcc
,
431 samba_use_global_deps
= use_global_deps
434 if cflags_end
is not None:
435 t
.samba_cflags
.extend(TO_LIST(cflags_end
))
437 if heimdal_autoproto
is not None:
438 bld
.HEIMDAL_AUTOPROTO(heimdal_autoproto
, source
, options
=heimdal_autoproto_options
)
439 if heimdal_autoproto_private
is not None:
440 bld
.HEIMDAL_AUTOPROTO_PRIVATE(heimdal_autoproto_private
, source
)
441 if autoproto
is not None:
442 bld
.SAMBA_AUTOPROTO(autoproto
, source
+ TO_LIST(autoproto_extra_source
))
443 if public_headers
is not None:
444 bld
.PUBLIC_HEADERS(public_headers
, header_path
=header_path
)
448 Build
.BuildContext
.SAMBA_SUBSYSTEM
= SAMBA_SUBSYSTEM
451 def SAMBA_GENERATOR(bld
, name
, rule
, source
='', target
='',
452 group
='generators', enabled
=True,
456 '''A generic source generator target'''
458 if not SET_TARGET_TYPE(bld
, name
, 'GENERATOR'):
464 bld
.SET_BUILD_GROUP(group
)
467 source
=bld
.EXPAND_VARIABLES(source
, vars=vars),
469 shell
=isinstance(rule
, str),
475 if public_headers
is not None:
476 bld
.PUBLIC_HEADERS(public_headers
, header_path
=header_path
)
478 Build
.BuildContext
.SAMBA_GENERATOR
= SAMBA_GENERATOR
483 def SETUP_BUILD_GROUPS(bld
):
484 '''setup build groups used to ensure that the different build
485 phases happen consecutively'''
486 bld
.p_ln
= bld
.srcnode
# we do want to see all targets!
487 bld
.env
['USING_BUILD_GROUPS'] = True
488 bld
.add_group('setup')
489 bld
.add_group('build_compiler_source')
490 bld
.add_group('base_libraries')
491 bld
.add_group('generators')
492 bld
.add_group('compiler_prototypes')
493 bld
.add_group('compiler_libraries')
494 bld
.add_group('build_compilers')
495 bld
.add_group('build_source')
496 bld
.add_group('prototypes')
497 bld
.add_group('main')
498 bld
.add_group('binaries')
499 bld
.add_group('final')
500 Build
.BuildContext
.SETUP_BUILD_GROUPS
= SETUP_BUILD_GROUPS
503 def SET_BUILD_GROUP(bld
, group
):
504 '''set the current build group'''
505 if not 'USING_BUILD_GROUPS' in bld
.env
:
508 Build
.BuildContext
.SET_BUILD_GROUP
= SET_BUILD_GROUP
513 def ENABLE_TIMESTAMP_DEPENDENCIES(conf
):
514 """use timestamps instead of file contents for deps
515 this currently doesn't work"""
516 def h_file(filename
):
518 st
= os
.stat(filename
)
519 if stat
.S_ISDIR(st
[stat
.ST_MODE
]): raise IOError('not a file')
521 m
.update(str(st
.st_mtime
))
522 m
.update(str(st
.st_size
))
525 Utils
.h_file
= h_file
529 t
= Task
.simple_task_type('copy_script', 'rm -f ${LINK_TARGET} && ln -s ${SRC[0].abspath(env)} ${LINK_TARGET}',
530 shell
=True, color
='PINK', ext_in
='.bin')
533 @feature('copy_script')
534 @before('apply_link')
535 def copy_script(self
):
536 tsk
= self
.create_task('copy_script', self
.allnodes
[0])
537 tsk
.env
.TARGET
= self
.target
539 def SAMBA_SCRIPT(bld
, name
, pattern
, installdir
, installname
=None):
540 '''used to copy scripts from the source tree into the build directory
541 for use by selftest'''
543 source
= bld
.path
.ant_glob(pattern
)
545 bld
.SET_BUILD_GROUP('build_source')
546 for s
in TO_LIST(source
):
548 if installname
!= None:
550 target
= os
.path
.join(installdir
, iname
)
551 tgtdir
= os
.path
.dirname(os
.path
.join(bld
.srcnode
.abspath(bld
.env
), '..', target
))
553 t
= bld(features
='copy_script',
558 t
.env
.LINK_TARGET
= target
560 Build
.BuildContext
.SAMBA_SCRIPT
= SAMBA_SCRIPT
563 def install_file(bld
, destdir
, file, chmod
=O644
, flat
=False,
564 python_fixup
=False, destname
=None, base_name
=None):
566 destdir
= bld
.EXPAND_VARIABLES(destdir
)
570 destname
= os
.path
.basename(destname
)
571 dest
= os
.path
.join(destdir
, destname
)
573 # fixup the python path it will use to find Samba modules
574 inst_file
= file + '.inst'
575 bld
.SAMBA_GENERATOR('python_%s' % destname
,
576 rule
="sed 's|\(sys.path.insert.*\)bin/python\(.*\)$|\\1${PYTHONDIR}\\2|g' < ${SRC} > ${TGT}",
581 file = os
.path
.join(base_name
, file)
582 bld
.install_as(dest
, file, chmod
=chmod
)
585 def INSTALL_FILES(bld
, destdir
, files
, chmod
=O644
, flat
=False,
586 python_fixup
=False, destname
=None, base_name
=None):
587 '''install a set of files'''
588 for f
in TO_LIST(files
):
589 install_file(bld
, destdir
, f
, chmod
=chmod
, flat
=flat
,
590 python_fixup
=python_fixup
, destname
=destname
,
592 Build
.BuildContext
.INSTALL_FILES
= INSTALL_FILES
595 def INSTALL_WILDCARD(bld
, destdir
, pattern
, chmod
=O644
, flat
=False,
596 python_fixup
=False, exclude
=None, trim_path
=None):
597 '''install a set of files matching a wildcard pattern'''
598 files
=TO_LIST(bld
.path
.ant_glob(pattern
))
602 files2
.append(os_path_relpath(f
, trim_path
))
607 if fnmatch
.fnmatch(f
, exclude
):
609 INSTALL_FILES(bld
, destdir
, files
, chmod
=chmod
, flat
=flat
,
610 python_fixup
=python_fixup
, base_name
=trim_path
)
611 Build
.BuildContext
.INSTALL_WILDCARD
= INSTALL_WILDCARD
614 def INSTALL_DIRS(bld
, destdir
, dirs
):
615 '''install a set of directories'''
616 destdir
= bld
.EXPAND_VARIABLES(destdir
)
617 dirs
= bld
.EXPAND_VARIABLES(dirs
)
618 for d
in TO_LIST(dirs
):
619 bld
.install_dir(os
.path
.join(destdir
, d
))
620 Build
.BuildContext
.INSTALL_DIRS
= INSTALL_DIRS
623 re_header
= re
.compile('#include[ \t]*"([^"]+)"', re
.I | re
.M
)
624 class header_task(Task
.Task
):
627 vars = ['INCLUDEDIR', 'HEADER_DEPS']
629 txt
= self
.inputs
[0].read(self
.env
)
631 txt
= txt
.replace('#if _SAMBA_BUILD_ == 4', '#if 1\n')
633 themap
= self
.generator
.bld
.subst_table
637 return "#include <%s>" % themap
.get(s
, s
)
640 txt
= re_header
.sub(repl
, txt
)
644 f
= open(self
.outputs
[0].abspath(self
.env
), 'w')
652 initialize the header substitution table
653 for now use the file headermap.txt but in the future we will compute the paths properly
656 if getattr(bld
, 'subst_table', None):
657 return bld
.subst_table_h
659 node
= bld
.srcnode
.find_resource("source4/headermap.txt")
662 bld
.subst_table_h
= 0
664 lines
= node
.read(None)
666 lines
= [x
.strip().split(': ') for x
in lines
.split('\n') if x
.rfind(': ') > -1]
667 bld
.subst_table
= dict(lines
)
669 # pidl file replacement (all of this is temporary, one step at a time)
670 keyz
= list(bld
.subst_table
.keys())
672 bld
.subst_table
['bin/default/' + k
] = bld
.subst_table
[k
]
674 tp
= tuple(bld
.subst_table
.keys())
675 bld
.subst_table_h
= hash(tp
)
676 return bld
.subst_table_h
678 @TaskGen.feature('pubh')
679 def make_public_headers(self
):
680 if not self
.bld
.is_install
:
681 # install time only (lazy)
684 self
.env
['HEADER_DEPS'] = init_subst(self
.bld
)
685 # adds a dependency and trigger a rebuild if the dict changes
687 header_path
= getattr(self
, 'header_path', None) or ''
689 for x
in self
.to_list(self
.headers
):
691 # too complicated, but what was the original idea?
692 if isinstance(header_path
, list):
694 for (p1
, dir) in header_path
:
695 lst
= self
.to_list(p1
)
697 if fnmatch
.fnmatch(x
, p2
):
705 inst_path
= header_path
709 if x
.find(':') != -1:
714 inn
= self
.path
.find_resource(name
)
716 raise ValueError("could not find the public header %r in %r" % (name
, self
.path
))
717 out
= inn
.change_ext('.inst.h')
718 self
.create_task('header', inn
, out
)
724 inst_path
= inst_path
+ '/'
725 inst_path
= inst_path
+ dest
727 #print("going to install the headers", inst_path, out)
728 self
.bld
.install_as('${INCLUDEDIR}/%s' % inst_path
, out
, self
.env
)
730 def PUBLIC_HEADERS(bld
, public_headers
, header_path
=None):
731 '''install some headers
733 header_path may either be a string that is added to the INCLUDEDIR,
734 or it can be a dictionary of wildcard patterns which map to destination
735 directories relative to INCLUDEDIR
737 bld
.SET_BUILD_GROUP('final')
738 ret
= bld(features
=['pubh'], headers
=public_headers
, header_path
=header_path
)
740 Build
.BuildContext
.PUBLIC_HEADERS
= PUBLIC_HEADERS
743 def subst_at_vars(task
):
744 '''substiture @VAR@ style variables in a file'''
745 src
= task
.inputs
[0].srcpath(task
.env
)
746 tgt
= task
.outputs
[0].bldpath(task
.env
)
752 a
= re
.split('(@\w+@)', s
)
755 back_sub
= [ ('PREFIX', '${prefix}'), ('EXEC_PREFIX', '${exec_prefix}')]
757 if re
.match('@\w+@', v
):
759 if not vname
in task
.env
and vname
.upper() in task
.env
:
760 vname
= vname
.upper()
761 if not vname
in task
.env
:
762 Logs
.error("Unknown substitution %s in %s" % (v
, task
.name
))
764 v
= SUBST_VARS_RECURSIVE(task
.env
[vname
], task
.env
)
765 # now we back substitute the allowed pc vars
766 for (b
, m
) in back_sub
:
769 if not b
in done_var
:
770 # we don't want to substitute the first usage
776 contents
= ''.join(out
)
778 s
= f
.write(contents
)
784 def PKG_CONFIG_FILES(bld
, pc_files
, vnum
=None):
785 '''install some pkg_config pc files'''
786 dest
= '${PKGCONFIGDIR}'
787 dest
= bld
.EXPAND_VARIABLES(dest
)
788 for f
in TO_LIST(pc_files
):
789 base
=os
.path
.basename(f
)
790 t
= bld
.SAMBA_GENERATOR('PKGCONFIG_%s' % base
,
795 t
.env
.PACKAGE_VERSION
= vnum
796 INSTALL_FILES(bld
, dest
, f
, flat
=True, destname
=base
)
797 Build
.BuildContext
.PKG_CONFIG_FILES
= PKG_CONFIG_FILES
801 #############################################################
802 # give a nicer display when building different types of files
803 def progress_display(self
, msg
, fname
):
804 col1
= Logs
.colors(self
.color
)
805 col2
= Logs
.colors
.NORMAL
806 total
= self
.position
[1]
808 fs
= '[%%%dd/%%%dd] %s %%s%%s%%s\n' % (n
, n
, msg
)
809 return fs
% (self
.position
[0], self
.position
[1], col1
, fname
, col2
)
811 def link_display(self
):
812 if Options
.options
.progress_bar
!= 0:
813 return Task
.Task
.old_display(self
)
814 fname
= self
.outputs
[0].bldpath(self
.env
)
815 return progress_display(self
, 'Linking', fname
)
816 Task
.TaskBase
.classes
['cc_link'].display
= link_display
818 def samba_display(self
):
819 if Options
.options
.progress_bar
!= 0:
820 return Task
.Task
.old_display(self
)
822 targets
= LOCAL_CACHE(self
, 'TARGET_TYPE')
823 if self
.name
in targets
:
824 target_type
= targets
[self
.name
]
825 type_map
= { 'GENERATOR' : 'Generating',
826 'PROTOTYPE' : 'Generating'
828 if target_type
in type_map
:
829 return progress_display(self
, type_map
[target_type
], self
.name
)
831 fname
= self
.inputs
[0].bldpath(self
.env
)
832 if fname
[0:3] == '../':
834 ext_loc
= fname
.rfind('.')
836 return Task
.Task
.old_display(self
)
837 ext
= fname
[ext_loc
:]
839 ext_map
= { '.idl' : 'Compiling IDL',
840 '.et' : 'Compiling ERRTABLE',
841 '.asn1': 'Compiling ASN1',
844 return progress_display(self
, ext_map
[ext
], fname
)
845 return Task
.Task
.old_display(self
)
847 Task
.TaskBase
.classes
['Task'].old_display
= Task
.TaskBase
.classes
['Task'].display
848 Task
.TaskBase
.classes
['Task'].display
= samba_display