"""SCons.Tool
SCons tool selection.
This looks for modules that define a callable object that can modify
a construction environment as appropriate for a given tool (or tool
chain).
Note that because this subsystem just *selects* a callable that can
modify a construction environment, it's possible for people to define
their own "tool specification" in an arbitrary callable function. No
one needs to use or tie in to this subsystem in order to roll their own
tool definition.
"""
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
import sys
import os
from collections.abc import Callable
import importlib.util
import SCons.Builder
import SCons.Errors
import SCons.Node.FS
import SCons.Scanner
import SCons.Scanner.C
import SCons.Scanner.D
import SCons.Scanner.LaTeX
import SCons.Scanner.Prog
import SCons.Scanner.SWIG
DefaultToolpath = []
CScanner = SCons.Scanner.C.CScanner()
DScanner = SCons.Scanner.D.DScanner()
LaTeXScanner = SCons.Scanner.LaTeX.LaTeXScanner()
PDFLaTeXScanner = SCons.Scanner.LaTeX.PDFLaTeXScanner()
ProgramScanner = SCons.Scanner.Prog.ProgramScanner()
SourceFileScanner = SCons.Scanner.Base({}, name='SourceFileScanner')
SWIGScanner = SCons.Scanner.SWIG.SWIGScanner()
CSuffixes = [".c", ".C", ".cxx", ".cpp", ".c++", ".cc",
".h", ".H", ".hxx", ".hpp", ".hh",
".F", ".fpp", ".FPP",
".m", ".mm",
".S", ".spp", ".SPP", ".sx"]
DSuffixes = ['.d']
IDLSuffixes = [".idl", ".IDL"]
LaTeXSuffixes = [".tex", ".ltx", ".latex"]
SWIGSuffixes = ['.i']
for suffix in CSuffixes:
SourceFileScanner.add_scanner(suffix, CScanner)
for suffix in DSuffixes:
SourceFileScanner.add_scanner(suffix, DScanner)
for suffix in SWIGSuffixes:
SourceFileScanner.add_scanner(suffix, SWIGScanner)
# FIXME: what should be done here? Two scanners scan the same extensions,
# but look for different files, e.g., "picture.eps" vs. "picture.pdf".
# The builders for DVI and PDF explicitly reference their scanners
# I think that means this is not needed???
for suffix in LaTeXSuffixes:
SourceFileScanner.add_scanner(suffix, LaTeXScanner)
SourceFileScanner.add_scanner(suffix, PDFLaTeXScanner)
# Tool aliases are needed for those tools whose module names also
# occur in the python standard library (This causes module shadowing and
# can break using python library functions under python3) or if the current tool/file names
# are not legal module names (violate python's identifier rules or are
# python language keywords).
TOOL_ALIASES = {
'gettext': 'gettext_tool',
'clang++': 'clangxx',
'as': 'asm',
}
##########################################################################
# Create common executable program / library / object builders
[docs]def createProgBuilder(env):
"""This is a utility function that creates the Program
Builder in an Environment if it is not there already.
If it is already there, we return the existing one.
"""
try:
program = env['BUILDERS']['Program']
except KeyError:
import SCons.Defaults
program = SCons.Builder.Builder(action=SCons.Defaults.LinkAction,
emitter='$PROGEMITTER',
prefix='$PROGPREFIX',
suffix='$PROGSUFFIX',
src_suffix='$OBJSUFFIX',
src_builder='Object',
target_scanner=ProgramScanner)
env['BUILDERS']['Program'] = program
return program
[docs]def createStaticLibBuilder(env):
"""This is a utility function that creates the StaticLibrary
Builder in an Environment if it is not there already.
If it is already there, we return the existing one.
"""
try:
static_lib = env['BUILDERS']['StaticLibrary']
except KeyError:
action_list = [SCons.Action.Action("$ARCOM", "$ARCOMSTR")]
if env.get('RANLIB', False) or env.Detect('ranlib'):
ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
action_list.append(ranlib_action)
static_lib = SCons.Builder.Builder(action=action_list,
emitter='$LIBEMITTER',
prefix='$LIBPREFIX',
suffix='$LIBSUFFIX',
src_suffix='$OBJSUFFIX',
src_builder='StaticObject')
env['BUILDERS']['StaticLibrary'] = static_lib
env['BUILDERS']['Library'] = static_lib
return static_lib
[docs]def _call_linker_cb(env, callback, args, result=None):
"""Returns the result of env['LINKCALLBACKS'][callback](*args)
if env['LINKCALLBACKS'] is a dictionary and env['LINKCALLBACKS'][callback]
is callable. If these conditions are not met, return the value provided as
the *result* argument. This function is mainly used for generating library
info such as versioned suffixes, symlink maps, sonames etc. by delegating
the core job to callbacks configured by current linker tool"""
Verbose = False
if Verbose:
print('_call_linker_cb: args=%r' % args)
print('_call_linker_cb: callback=%r' % callback)
try:
cbfun = env['LINKCALLBACKS'][callback]
except (KeyError, TypeError):
if Verbose:
print('_call_linker_cb: env["LINKCALLBACKS"][%r] not found or can not be used' % callback)
pass
else:
if Verbose:
print('_call_linker_cb: env["LINKCALLBACKS"][%r] found' % callback)
print('_call_linker_cb: env["LINKCALLBACKS"][%r]=%r' % (callback, cbfun))
if isinstance(cbfun, Callable):
if Verbose:
print('_call_linker_cb: env["LINKCALLBACKS"][%r] is callable' % callback)
result = cbfun(env, *args)
return result
[docs]def _call_env_subst(env, string, *args, **kw):
kw2 = {}
for k in ('raw', 'target', 'source', 'conv', 'executor'):
try:
kw2[k] = kw[k]
except KeyError:
pass
return env.subst(string, *args, **kw2)
[docs]class _ShLibInfoSupport:
@property
def libtype(self):
return 'ShLib'
[docs] def get_lib_prefix(self, env, *args, **kw):
return _call_env_subst(env, '$SHLIBPREFIX', *args, **kw)
[docs] def get_lib_suffix(self, env, *args, **kw):
return _call_env_subst(env, '$SHLIBSUFFIX', *args, **kw)
[docs] def get_lib_version(self, env, *args, **kw):
return _call_env_subst(env, '$SHLIBVERSION', *args, **kw)
[docs] def get_lib_noversionsymlinks(self, env, *args, **kw):
return _call_env_subst(env, '$SHLIBNOVERSIONSYMLINKS', *args, **kw)
[docs]class _LdModInfoSupport:
@property
def libtype(self):
return 'LdMod'
[docs] def get_lib_prefix(self, env, *args, **kw):
return _call_env_subst(env, '$LDMODULEPREFIX', *args, **kw)
[docs] def get_lib_suffix(self, env, *args, **kw):
return _call_env_subst(env, '$LDMODULESUFFIX', *args, **kw)
[docs] def get_lib_version(self, env, *args, **kw):
return _call_env_subst(env, '$LDMODULEVERSION', *args, **kw)
[docs] def get_lib_noversionsymlinks(self, env, *args, **kw):
return _call_env_subst(env, '$LDMODULENOVERSIONSYMLINKS', *args, **kw)
[docs]class _ImpLibInfoSupport:
@property
def libtype(self):
return 'ImpLib'
[docs] def get_lib_prefix(self, env, *args, **kw):
return _call_env_subst(env, '$IMPLIBPREFIX', *args, **kw)
[docs] def get_lib_suffix(self, env, *args, **kw):
return _call_env_subst(env, '$IMPLIBSUFFIX', *args, **kw)
[docs] def get_lib_version(self, env, *args, **kw):
version = _call_env_subst(env, '$IMPLIBVERSION', *args, **kw)
if not version:
try:
lt = kw['implib_libtype']
except KeyError:
pass
else:
if lt == 'ShLib':
version = _call_env_subst(env, '$SHLIBVERSION', *args, **kw)
elif lt == 'LdMod':
version = _call_env_subst(env, '$LDMODULEVERSION', *args, **kw)
return version
[docs] def get_lib_noversionsymlinks(self, env, *args, **kw):
disable = None
try:
env['IMPLIBNOVERSIONSYMLINKS']
except KeyError:
try:
lt = kw['implib_libtype']
except KeyError:
pass
else:
if lt == 'ShLib':
disable = _call_env_subst(env, '$SHLIBNOVERSIONSYMLINKS', *args, **kw)
elif lt == 'LdMod':
disable = _call_env_subst(env, '$LDMODULENOVERSIONSYMLINKS', *args, **kw)
else:
disable = _call_env_subst(env, '$IMPLIBNOVERSIONSYMLINKS', *args, **kw)
return disable
[docs]class _LibInfoGeneratorBase:
"""Generator base class for library-related info such as suffixes for
versioned libraries, symlink maps, sonames etc. It handles commonities
of SharedLibrary and LoadableModule
"""
_support_classes = {'ShLib': _ShLibInfoSupport,
'LdMod': _LdModInfoSupport,
'ImpLib': _ImpLibInfoSupport}
def __init__(self, libtype, infoname):
self.libtype = libtype
self.infoname = infoname
@property
def libtype(self):
return self._support.libtype
@libtype.setter
def libtype(self, libtype):
try:
support_class = self._support_classes[libtype]
except KeyError:
raise ValueError('unsupported libtype %r' % libtype)
self._support = support_class()
[docs] def get_lib_prefix(self, env, *args, **kw):
return self._support.get_lib_prefix(env, *args, **kw)
[docs] def get_lib_suffix(self, env, *args, **kw):
return self._support.get_lib_suffix(env, *args, **kw)
[docs] def get_lib_version(self, env, *args, **kw):
return self._support.get_lib_version(env, *args, **kw)
[docs] def get_lib_noversionsymlinks(self, env, *args, **kw):
return self._support.get_lib_noversionsymlinks(env, *args, **kw)
# Returns name of generator linker callback that shall be used to generate
# our info for a versioned library. For example, if our libtype is 'ShLib'
# and infoname is 'Prefix', it would return 'VersionedShLibPrefix'.
[docs] def get_versioned_lib_info_generator(self, **kw):
try:
libtype = kw['generator_libtype']
except KeyError:
libtype = self.libtype
return 'Versioned%s%s' % (libtype, self.infoname)
[docs] def generate_versioned_lib_info(self, env, args, result=None, **kw):
callback = self.get_versioned_lib_info_generator(**kw)
return _call_linker_cb(env, callback, args, result)
[docs]class _LibPrefixGenerator(_LibInfoGeneratorBase):
"""Library prefix generator, used as target_prefix in SharedLibrary and
LoadableModule builders"""
def __init__(self, libtype):
super(_LibPrefixGenerator, self).__init__(libtype, 'Prefix')
def __call__(self, env, sources=None, **kw):
Verbose = False
if sources and 'source' not in kw:
kw2 = kw.copy()
kw2['source'] = sources
else:
kw2 = kw
prefix = self.get_lib_prefix(env, **kw2)
if Verbose:
print("_LibPrefixGenerator: input prefix=%r" % prefix)
version = self.get_lib_version(env, **kw2)
if Verbose:
print("_LibPrefixGenerator: version=%r" % version)
if version:
prefix = self.generate_versioned_lib_info(env, [prefix, version], prefix, **kw2)
if Verbose:
print("_LibPrefixGenerator: return prefix=%r" % prefix)
return prefix
ShLibPrefixGenerator = _LibPrefixGenerator('ShLib')
LdModPrefixGenerator = _LibPrefixGenerator('LdMod')
ImpLibPrefixGenerator = _LibPrefixGenerator('ImpLib')
[docs]class _LibSuffixGenerator(_LibInfoGeneratorBase):
"""Library suffix generator, used as target_suffix in SharedLibrary and
LoadableModule builders"""
def __init__(self, libtype):
super(_LibSuffixGenerator, self).__init__(libtype, 'Suffix')
def __call__(self, env, sources=None, **kw):
Verbose = False
if sources and 'source' not in kw:
kw2 = kw.copy()
kw2['source'] = sources
else:
kw2 = kw
suffix = self.get_lib_suffix(env, **kw2)
if Verbose:
print("_LibSuffixGenerator: input suffix=%r" % suffix)
version = self.get_lib_version(env, **kw2)
if Verbose:
print("_LibSuffixGenerator: version=%r" % version)
if version:
suffix = self.generate_versioned_lib_info(env, [suffix, version], suffix, **kw2)
if Verbose:
print("_LibSuffixGenerator: return suffix=%r" % suffix)
return suffix
ShLibSuffixGenerator = _LibSuffixGenerator('ShLib')
LdModSuffixGenerator = _LibSuffixGenerator('LdMod')
ImpLibSuffixGenerator = _LibSuffixGenerator('ImpLib')
[docs]class _LibSymlinkGenerator(_LibInfoGeneratorBase):
"""Library symlink map generator. It generates a list of symlinks that
should be created by SharedLibrary or LoadableModule builders"""
def __init__(self, libtype):
super(_LibSymlinkGenerator, self).__init__(libtype, 'Symlinks')
def __call__(self, env, libnode, **kw):
Verbose = False
if libnode and 'target' not in kw:
kw2 = kw.copy()
kw2['target'] = libnode
else:
kw2 = kw
if Verbose:
print("_LibSymLinkGenerator: libnode=%r" % libnode.get_path())
symlinks = None
version = self.get_lib_version(env, **kw2)
disable = self.get_lib_noversionsymlinks(env, **kw2)
if Verbose:
print('_LibSymlinkGenerator: version=%r' % version)
print('_LibSymlinkGenerator: disable=%r' % disable)
if version and not disable:
prefix = self.get_lib_prefix(env, **kw2)
suffix = self.get_lib_suffix(env, **kw2)
symlinks = self.generate_versioned_lib_info(env, [libnode, version, prefix, suffix], **kw2)
if Verbose:
print('_LibSymlinkGenerator: return symlinks=%r' % StringizeLibSymlinks(symlinks))
return symlinks
ShLibSymlinkGenerator = _LibSymlinkGenerator('ShLib')
LdModSymlinkGenerator = _LibSymlinkGenerator('LdMod')
ImpLibSymlinkGenerator = _LibSymlinkGenerator('ImpLib')
[docs]class _LibNameGenerator(_LibInfoGeneratorBase):
"""Generates "unmangled" library name from a library file node.
Generally, it's thought to revert modifications done by prefix/suffix
generators (_LibPrefixGenerator/_LibSuffixGenerator) used by a library
builder. For example, on gnulink the suffix generator used by SharedLibrary
builder appends $SHLIBVERSION to $SHLIBSUFFIX producing node name which
ends with "$SHLIBSUFFIX.$SHLIBVERSION". Correspondingly, the implementation
of _LibNameGenerator replaces "$SHLIBSUFFIX.$SHLIBVERSION" with
"$SHLIBSUFFIX" in the node's basename. So that, if $SHLIBSUFFIX is ".so",
$SHLIBVERSION is "0.1.2" and the node path is "/foo/bar/libfoo.so.0.1.2",
the _LibNameGenerator shall return "libfoo.so". Other link tools may
implement it's own way of library name unmangling.
"""
def __init__(self, libtype):
super(_LibNameGenerator, self).__init__(libtype, 'Name')
def __call__(self, env, libnode, **kw):
"""Returns "demangled" library name"""
Verbose = False
if libnode and 'target' not in kw:
kw2 = kw.copy()
kw2['target'] = libnode
else:
kw2 = kw
if Verbose:
print("_LibNameGenerator: libnode=%r" % libnode.get_path())
version = self.get_lib_version(env, **kw2)
if Verbose:
print('_LibNameGenerator: version=%r' % version)
name = None
if version:
prefix = self.get_lib_prefix(env, **kw2)
suffix = self.get_lib_suffix(env, **kw2)
name = self.generate_versioned_lib_info(env, [libnode, version, prefix, suffix], **kw2)
if not name:
name = os.path.basename(libnode.get_path())
if Verbose:
print('_LibNameGenerator: return name=%r' % name)
return name
ShLibNameGenerator = _LibNameGenerator('ShLib')
LdModNameGenerator = _LibNameGenerator('LdMod')
ImpLibNameGenerator = _LibNameGenerator('ImpLib')
[docs]class _LibSonameGenerator(_LibInfoGeneratorBase):
"""Library soname generator. Returns library soname (e.g. libfoo.so.0) for
a given node (e.g. /foo/bar/libfoo.so.0.1.2)"""
def __init__(self, libtype):
super(_LibSonameGenerator, self).__init__(libtype, 'Soname')
def __call__(self, env, libnode, **kw):
"""Returns a SONAME based on a shared library's node path"""
Verbose = False
if libnode and 'target' not in kw:
kw2 = kw.copy()
kw2['target'] = libnode
else:
kw2 = kw
if Verbose:
print("_LibSonameGenerator: libnode=%r" % libnode.get_path())
soname = _call_env_subst(env, '$SONAME', **kw2)
if not soname:
version = self.get_lib_version(env, **kw2)
if Verbose:
print("_LibSonameGenerator: version=%r" % version)
if version:
prefix = self.get_lib_prefix(env, **kw2)
suffix = self.get_lib_suffix(env, **kw2)
soname = self.generate_versioned_lib_info(env, [libnode, version, prefix, suffix], **kw2)
if not soname:
# fallback to library name (as returned by appropriate _LibNameGenerator)
soname = _LibNameGenerator(self.libtype)(env, libnode)
if Verbose:
print("_LibSonameGenerator: FALLBACK: soname=%r" % soname)
if Verbose:
print("_LibSonameGenerator: return soname=%r" % soname)
return soname
ShLibSonameGenerator = _LibSonameGenerator('ShLib')
LdModSonameGenerator = _LibSonameGenerator('LdMod')
[docs]def StringizeLibSymlinks(symlinks):
"""Converts list with pairs of nodes to list with pairs of node paths
(strings). Used mainly for debugging."""
if SCons.Util.is_List(symlinks):
try:
return [(k.get_path(), v.get_path()) for k, v in symlinks]
except (TypeError, ValueError):
return symlinks
else:
return symlinks
[docs]def EmitLibSymlinks(env, symlinks, libnode, **kw):
"""Used by emitters to handle (shared/versioned) library symlinks"""
Verbose = False
# nodes involved in process... all symlinks + library
nodes = list(set([x for x, y in symlinks] + [libnode]))
clean_targets = kw.get('clean_targets', [])
if not SCons.Util.is_List(clean_targets):
clean_targets = [clean_targets]
for link, linktgt in symlinks:
env.SideEffect(link, linktgt)
if Verbose:
print("EmitLibSymlinks: SideEffect(%r,%r)" % (link.get_path(), linktgt.get_path()))
clean_list = [x for x in nodes if x != linktgt]
env.Clean(list(set([linktgt] + clean_targets)), clean_list)
if Verbose:
print("EmitLibSymlinks: Clean(%r,%r)" % (linktgt.get_path(), [x.get_path() for x in clean_list]))
[docs]def CreateLibSymlinks(env, symlinks):
"""Physically creates symlinks. The symlinks argument must be a list in
form [ (link, linktarget), ... ], where link and linktarget are SCons
nodes.
"""
Verbose = False
for link, linktgt in symlinks:
linktgt = link.get_dir().rel_path(linktgt)
link = link.get_path()
if Verbose:
print("CreateLibSymlinks: preparing to add symlink %r -> %r" % (link, linktgt))
# Delete the (previously created) symlink if exists. Let only symlinks
# to be deleted to prevent accidental deletion of source files...
if env.fs.islink(link):
env.fs.unlink(link)
if Verbose:
print("CreateLibSymlinks: removed old symlink %r" % link)
# If a file or directory exists with the same name as link, an OSError
# will be thrown, which should be enough, I think.
env.fs.symlink(linktgt, link)
if Verbose:
print("CreateLibSymlinks: add symlink %r -> %r" % (link, linktgt))
return 0
[docs]def LibSymlinksActionFunction(target, source, env):
for tgt in target:
symlinks = getattr(getattr(tgt, 'attributes', None), 'shliblinks', None)
if symlinks:
CreateLibSymlinks(env, symlinks)
return 0
[docs]def LibSymlinksStrFun(target, source, env, *args):
cmd = None
for tgt in target:
symlinks = getattr(getattr(tgt, 'attributes', None), 'shliblinks', None)
if symlinks:
if cmd is None: cmd = ""
if cmd: cmd += "\n"
cmd += "Create symlinks for: %r" % tgt.get_path()
try:
linkstr = ', '.join(["%r->%r" % (k, v) for k, v in StringizeLibSymlinks(symlinks)])
except (KeyError, ValueError):
pass
else:
cmd += ": %s" % linkstr
return cmd
LibSymlinksAction = SCons.Action.Action(LibSymlinksActionFunction, LibSymlinksStrFun)
[docs]def createSharedLibBuilder(env):
"""This is a utility function that creates the SharedLibrary
Builder in an Environment if it is not there already.
If it is already there, we return the existing one.
"""
try:
shared_lib = env['BUILDERS']['SharedLibrary']
except KeyError:
import SCons.Defaults
action_list = [SCons.Defaults.SharedCheck,
SCons.Defaults.ShLinkAction,
LibSymlinksAction]
shared_lib = SCons.Builder.Builder(action=action_list,
emitter="$SHLIBEMITTER",
prefix=ShLibPrefixGenerator,
suffix=ShLibSuffixGenerator,
target_scanner=ProgramScanner,
src_suffix='$SHOBJSUFFIX',
src_builder='SharedObject')
env['BUILDERS']['SharedLibrary'] = shared_lib
return shared_lib
[docs]def createLoadableModuleBuilder(env):
"""This is a utility function that creates the LoadableModule
Builder in an Environment if it is not there already.
If it is already there, we return the existing one.
"""
try:
ld_module = env['BUILDERS']['LoadableModule']
except KeyError:
import SCons.Defaults
action_list = [SCons.Defaults.SharedCheck,
SCons.Defaults.LdModuleLinkAction,
LibSymlinksAction]
ld_module = SCons.Builder.Builder(action=action_list,
emitter="$LDMODULEEMITTER",
prefix=LdModPrefixGenerator,
suffix=LdModSuffixGenerator,
target_scanner=ProgramScanner,
src_suffix='$SHOBJSUFFIX',
src_builder='SharedObject')
env['BUILDERS']['LoadableModule'] = ld_module
return ld_module
[docs]def createObjBuilders(env):
"""This is a utility function that creates the StaticObject
and SharedObject Builders in an Environment if they
are not there already.
If they are there already, we return the existing ones.
This is a separate function because soooo many Tools
use this functionality.
The return is a 2-tuple of (StaticObject, SharedObject)
"""
try:
static_obj = env['BUILDERS']['StaticObject']
except KeyError:
static_obj = SCons.Builder.Builder(action={},
emitter={},
prefix='$OBJPREFIX',
suffix='$OBJSUFFIX',
src_builder=['CFile', 'CXXFile'],
source_scanner=SourceFileScanner,
single_source=1)
env['BUILDERS']['StaticObject'] = static_obj
env['BUILDERS']['Object'] = static_obj
try:
shared_obj = env['BUILDERS']['SharedObject']
except KeyError:
shared_obj = SCons.Builder.Builder(action={},
emitter={},
prefix='$SHOBJPREFIX',
suffix='$SHOBJSUFFIX',
src_builder=['CFile', 'CXXFile'],
source_scanner=SourceFileScanner,
single_source=1)
env['BUILDERS']['SharedObject'] = shared_obj
return (static_obj, shared_obj)
[docs]def createCFileBuilders(env):
"""This is a utility function that creates the CFile/CXXFile
Builders in an Environment if they
are not there already.
If they are there already, we return the existing ones.
This is a separate function because soooo many Tools
use this functionality.
The return is a 2-tuple of (CFile, CXXFile)
"""
try:
c_file = env['BUILDERS']['CFile']
except KeyError:
c_file = SCons.Builder.Builder(action={},
emitter={},
suffix={None: '$CFILESUFFIX'})
env['BUILDERS']['CFile'] = c_file
env.SetDefault(CFILESUFFIX='.c')
try:
cxx_file = env['BUILDERS']['CXXFile']
except KeyError:
cxx_file = SCons.Builder.Builder(action={},
emitter={},
suffix={None: '$CXXFILESUFFIX'})
env['BUILDERS']['CXXFile'] = cxx_file
env.SetDefault(CXXFILESUFFIX='.cc')
return (c_file, cxx_file)
##########################################################################
# Create common Java builders
[docs]def CreateJarBuilder(env):
"""The Jar builder expects a list of class files
which it can package into a jar file.
The jar tool provides an interface for passing other types
of java files such as .java, directories or swig interfaces
and will build them to class files in which it can package
into the jar.
"""
try:
java_jar = env['BUILDERS']['JarFile']
except KeyError:
fs = SCons.Node.FS.get_default_fs()
jar_com = SCons.Action.Action('$JARCOM', '$JARCOMSTR')
java_jar = SCons.Builder.Builder(action=jar_com,
suffix='$JARSUFFIX',
src_suffix='$JAVACLASSSUFFIX',
src_builder='JavaClassFile',
source_factory=fs.Entry)
env['BUILDERS']['JarFile'] = java_jar
return java_jar
[docs]def CreateJavaHBuilder(env):
try:
java_javah = env['BUILDERS']['JavaH']
except KeyError:
fs = SCons.Node.FS.get_default_fs()
java_javah_com = SCons.Action.Action('$JAVAHCOM', '$JAVAHCOMSTR')
java_javah = SCons.Builder.Builder(action=java_javah_com,
src_suffix='$JAVACLASSSUFFIX',
target_factory=fs.Entry,
source_factory=fs.File,
src_builder='JavaClassFile')
env['BUILDERS']['JavaH'] = java_javah
return java_javah
[docs]def CreateJavaClassFileBuilder(env):
try:
java_class_file = env['BUILDERS']['JavaClassFile']
except KeyError:
fs = SCons.Node.FS.get_default_fs()
javac_com = SCons.Action.Action('$JAVACCOM', '$JAVACCOMSTR')
java_class_file = SCons.Builder.Builder(action=javac_com,
emitter={},
# suffix = '$JAVACLASSSUFFIX',
src_suffix='$JAVASUFFIX',
src_builder=['JavaFile'],
target_factory=fs.Entry,
source_factory=fs.File)
env['BUILDERS']['JavaClassFile'] = java_class_file
return java_class_file
[docs]def CreateJavaClassDirBuilder(env):
try:
java_class_dir = env['BUILDERS']['JavaClassDir']
except KeyError:
fs = SCons.Node.FS.get_default_fs()
javac_com = SCons.Action.Action('$JAVACCOM', '$JAVACCOMSTR')
java_class_dir = SCons.Builder.Builder(action=javac_com,
emitter={},
target_factory=fs.Dir,
source_factory=fs.Dir)
env['BUILDERS']['JavaClassDir'] = java_class_dir
return java_class_dir
[docs]def CreateJavaFileBuilder(env):
try:
java_file = env['BUILDERS']['JavaFile']
except KeyError:
java_file = SCons.Builder.Builder(action={},
emitter={},
suffix={None: '$JAVASUFFIX'})
env['BUILDERS']['JavaFile'] = java_file
env['JAVASUFFIX'] = '.java'
return java_file
# If we fall through here, there was no tool module found.
# This is where we can put an informative error message
# about the inability to find the tool. We'll start doing
# this as we cut over more pre-defined Builder+Tools to use
# the ToolInitializer class.
[docs]def Initializers(env):
ToolInitializer(env, ['install'], ['_InternalInstall', '_InternalInstallAs', '_InternalInstallVersionedLib'])
def Install(self, *args, **kw):
return self._InternalInstall(*args, **kw)
def InstallAs(self, *args, **kw):
return self._InternalInstallAs(*args, **kw)
def InstallVersionedLib(self, *args, **kw):
return self._InternalInstallVersionedLib(*args, **kw)
env.AddMethod(Install)
env.AddMethod(InstallAs)
env.AddMethod(InstallVersionedLib)
[docs]def find_program_path(env, key_program, default_paths=None):
"""
Find the location of a tool using various means.
Mainly for windows where tools aren't all installed in /usr/bin, etc.
:param env: Current Construction Environment.
:param key_program: Tool to locate.
:param default_paths: List of additional paths this tool might be found in.
"""
# First search in the SCons path
path = env.WhereIs(key_program)
if path:
return path
# Then in the OS path
path = SCons.Util.WhereIs(key_program)
if path:
return path
# Finally, add the defaults and check again. Do not change
# ['ENV']['PATH'] permananetly, the caller can do that if needed.
if default_paths is None:
return path
save_path = env['ENV']['PATH']
for p in default_paths:
env.AppendENVPath('PATH', p)
path = env.WhereIs(key_program)
env['ENV']['PATH'] = save_path
return path
# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: