Commit cc6f7c43 authored by Karl Lindén's avatar Karl Lindén
Browse files

update to waf 1.8.9 (it is now stored in source form to ease downstream maintainence)

parent 75c27f65
No preview for this file type
This diff is collapsed.
#!/usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2005-2010 (ita)
ConfigSet: a special dict
The values put in :py:class:`ConfigSet` must be lists
import copy, re, os
from waflib import Logs, Utils
re_imp = re.compile('^(#)*?([^#=]*?)\ =\ (.*?)$', re.M)
class ConfigSet(object):
A dict that honor serialization and parent relationships. The serialization format
is human-readable (python-like) and performed by using eval() and repr().
For high performance prefer pickle. Do not store functions as they are not serializable.
The values can be accessed by attributes or by keys::
from waflib.ConfigSet import ConfigSet
env = ConfigSet()
env.FOO = 'test'
env['FOO'] = 'test'
__slots__ = ('table', 'parent')
def __init__(self, filename=None):
self.table = {}
Internal dict holding the object values
#self.parent = None
if filename:
def __contains__(self, key):
Enable the *in* syntax::
if 'foo' in env:
if key in self.table: return True
try: return self.parent.__contains__(key)
except AttributeError: return False # parent may not exist
def keys(self):
"""Dict interface (unknown purpose)"""
keys = set()
cur = self
while cur:
cur = getattr(cur, 'parent', None)
keys = list(keys)
return keys
def __str__(self):
"""Text representation of the ConfigSet (for debugging purposes)"""
return "\n".join(["%r %r" % (x, self.__getitem__(x)) for x in self.keys()])
def __getitem__(self, key):
Dictionary interface: get value from key::
def configure(conf):
conf.env['foo'] = {}
while 1:
x = self.table.get(key, None)
if not x is None:
return x
self = self.parent
except AttributeError:
return []
def __setitem__(self, key, value):
Dictionary interface: get value from key
self.table[key] = value
def __delitem__(self, key):
Dictionary interface: get value from key
self[key] = []
def __getattr__(self, name):
Attribute access provided for convenience. The following forms are equivalent::
def configure(conf):
if name in self.__slots__:
return object.__getattr__(self, name)
return self[name]
def __setattr__(self, name, value):
Attribute access provided for convenience. The following forms are equivalent::
def configure(conf):
conf.env.value = x
env['value'] = x
if name in self.__slots__:
object.__setattr__(self, name, value)
self[name] = value
def __delattr__(self, name):
Attribute access provided for convenience. The following forms are equivalent::
def configure(conf):
del env.value
del env['value']
if name in self.__slots__:
object.__delattr__(self, name)
del self[name]
def derive(self):
Returns a new ConfigSet deriving from self. The copy returned
will be a shallow copy::
from waflib.ConfigSet import ConfigSet
env = ConfigSet()
env.append_value('CFLAGS', ['-O2'])
child = env.derive()
child.CFLAGS.append('test') # warning! this will modify 'env'
child.CFLAGS = ['-O3'] # new list, ok
child.append_value('CFLAGS', ['-O3']) # ok
Use :py:func:`ConfigSet.detach` to detach the child from the parent.
newenv = ConfigSet()
newenv.parent = self
return newenv
def detach(self):
Detach self from its parent (if existing)
Modifying the parent :py:class:`ConfigSet` will not change the current object
Modifying this :py:class:`ConfigSet` will not modify the parent one.
tbl = self.get_merged_dict()
delattr(self, 'parent')
except AttributeError:
keys = tbl.keys()
for x in keys:
tbl[x] = copy.deepcopy(tbl[x])
self.table = tbl
def get_flat(self, key):
Return a value as a string. If the input is a list, the value returned is space-separated.
:param key: key to use
:type key: string
s = self[key]
if isinstance(s, str): return s
return ' '.join(s)
def _get_list_value_for_modification(self, key):
Return a list value for further modification.
The list may be modified inplace and there is no need to do this afterwards::
self.table[var] = value
value = self.table[key]
except KeyError:
try: value = self.parent[key]
except AttributeError: value = []
if isinstance(value, list):
value = value[:]
value = [value]
if not isinstance(value, list):
value = [value]
self.table[key] = value
return value
def append_value(self, var, val):
Appends a value to the specified config key::
def build(bld):
bld.env.append_value('CFLAGS', ['-O2'])
The value must be a list or a tuple
if isinstance(val, str): # if there were string everywhere we could optimize this
val = [val]
current_value = self._get_list_value_for_modification(var)
def prepend_value(self, var, val):
Prepends a value to the specified item::
def configure(conf):
conf.env.prepend_value('CFLAGS', ['-O2'])
The value must be a list or a tuple
if isinstance(val, str):
val = [val]
self.table[var] = val + self._get_list_value_for_modification(var)
def append_unique(self, var, val):
Append a value to the specified item only if it's not already present::
def build(bld):
bld.env.append_unique('CFLAGS', ['-O2', '-g'])
The value must be a list or a tuple
if isinstance(val, str):
val = [val]
current_value = self._get_list_value_for_modification(var)
for x in val:
if x not in current_value:
def get_merged_dict(self):
Compute the merged dictionary from the fusion of self and all its parent
:rtype: a ConfigSet object
table_list = []
env = self
while 1:
table_list.insert(0, env.table)
try: env = env.parent
except AttributeError: break
merged_table = {}
for table in table_list:
return merged_table
def store(self, filename):
Write the :py:class:`ConfigSet` data into a file. See :py:meth:`ConfigSet.load` for reading such files.
:param filename: file to use
:type filename: string
except OSError:
buf = []
merged_table = self.get_merged_dict()
keys = list(merged_table.keys())
fun = ascii
except NameError:
fun = repr
for k in keys:
if k != 'undo_stack':
buf.append('%s = %s\n' % (k, fun(merged_table[k])))
Utils.writef(filename, ''.join(buf))
def load(self, filename):
Retrieve the :py:class:`ConfigSet` data from a file. See :py:meth:`` for writing such files
:param filename: file to use
:type filename: string
tbl = self.table
code = Utils.readf(filename, m='rU')
for m in re_imp.finditer(code):
g =
tbl[g(2)] = eval(g(3))
Logs.debug('env: %s' % str(self.table))
def update(self, d):
Dictionary interface: replace values from another dict
:param d: object to use the value from
:type d: dict-like object
for k, v in d.items():
self[k] = v
def stash(self):
Store the object state, to provide a kind of transaction support::
env = ConfigSet()
env.append_value('CFLAGS', '-O3')
The history is kept in a stack, and is lost during the serialization by :py:meth:``
orig = self.table
tbl = self.table = self.table.copy()
for x in tbl.keys():
tbl[x] = copy.deepcopy(tbl[x])
self.undo_stack = self.undo_stack + [orig]
def revert(self):
Reverts the object to a previous state. See :py:meth:`ConfigSet.stash`
self.table = self.undo_stack.pop(-1)
#!/usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2005-2010 (ita)
Configuration system
A :py:class:`waflib.Configure.ConfigurationContext` instance is created when ``waf configure`` is called, it is used to:
* create data dictionaries (ConfigSet instances)
* store the list of modules to import
* hold configuration routines such as ``find_program``, etc
import os, shlex, sys, time, re, shutil
from waflib import ConfigSet, Utils, Options, Logs, Context, Build, Errors
BREAK = 'break'
"""In case of a configuration error, break"""
CONTINUE = 'continue'
"""In case of a configuration error, continue"""
WAF_CONFIG_LOG = 'config.log'
"""Name of the configuration log file"""
autoconfig = False
"""Execute the configuration automatically"""
conf_template = '''# project %(app)s configured on %(now)s by
# waf %(wafver)s (abi %(abi)s, python %(pyver)x on %(systype)s)
# using %(args)s
class ConfigurationContext(Context.Context):
'''configures the project'''
cmd = 'configure'
error_handlers = []
Additional functions to handle configuration errors
def __init__(self, **kw):
super(ConfigurationContext, self).__init__(**kw)
self.environ = dict(os.environ)
self.all_envs = {}
self.top_dir = None
self.out_dir = None = [] # tools loaded in the configuration, and that will be loaded when building
self.hash = 0
self.files = []
self.tool_cache = []
def setenv(self, name, env=None):
Set a new config set for conf.env. If a config set of that name already exists,
recall it without modification.
The name is the filename prefix to save to ``c4che/``, and it
is also used as *variants* by the build commands.
Though related to variants, whatever kind of data may be stored in the config set::
def configure(cfg):
cfg.env.ONE = 1
cfg.env.ONE = 2
def build(bld):
2 == bld.env_of_name('foo').ONE
:param name: name of the configuration set
:type name: string
:param env: ConfigSet to copy, or an empty ConfigSet is created
:type env: :py:class:`waflib.ConfigSet.ConfigSet`
if name not in self.all_envs or env:
if not env:
env = ConfigSet.ConfigSet()
env = env.derive()
self.all_envs[name] = env
self.variant = name
def get_env(self):
"""Getter for the env property"""
return self.all_envs[self.variant]
def set_env(self, val):
"""Setter for the env property"""
self.all_envs[self.variant] = val
env = property(get_env, set_env)
def init_dirs(self):
Initialize the project directory and the build directory
top = self.top_dir
if not top:
top =
if not top:
top = getattr(Context.g_module, Context.TOP, None)
if not top:
top = self.path.abspath()
top = os.path.abspath(top)
self.srcnode = (os.path.isabs(top) and self.root or self.path).find_dir(top)
out = self.out_dir
if not out:
out = Options.options.out
if not out:
out = getattr(Context.g_module, Context.OUT, None)
if not out:
out = Options.lockfile.replace('.lock-waf_%s_' % sys.platform, '').replace('.lock-waf', '')
# someone can be messing with symlinks
out = os.path.realpath(out)
self.bldnode = (os.path.isabs(out) and self.root or self.path).make_node(out)
if not os.path.isdir(self.bldnode.abspath()):
conf.fatal('Could not create the build directory %s' % self.bldnode.abspath())
def execute(self):
See :py:func:`waflib.Context.Context.execute`
self.cachedir = self.bldnode.make_node(Build.CACHE_DIR)
path = os.path.join(self.bldnode.abspath(), WAF_CONFIG_LOG)
self.logger = Logs.make_logger(path, 'cfg')
app = getattr(Context.g_module, 'APPNAME', '')
if app:
ver = getattr(Context.g_module, 'VERSION', '')
if ver:
app = "%s (%s)" % (app, ver)
now = time.ctime()
pyver = sys.hexversion
systype = sys.platform
args = " ".join(sys.argv)
wafver = Context.WAFVERSION
abi = Context.ABI
self.to_log(conf_template % vars())
self.msg('Setting top to', self.srcnode.abspath())
self.msg('Setting out to', self.bldnode.abspath())
if id(self.srcnode) == id(self.bldnode):
Logs.warn('Setting top == out (remember to use "update_outputs")')
elif id(self.path) != id(self.srcnode):
if self.srcnode.is_child_of(self.path):
Logs.warn('Are you certain that you do not want to set top="." ?')
super(ConfigurationContext, self).execute()
Context.top_dir = self.srcnode.abspath()
Context.out_dir = self.bldnode.abspath()
# this will write a configure lock so that subsequent builds will
# consider the current path as the root directory (see prepare_impl).
# to remove: use 'waf distclean'
env = ConfigSet.ConfigSet()
env['argv'] = sys.argv
env['options'] = Options.options.__dict__
env.run_dir = Context.run_dir
env.top_dir = Context.top_dir
env.out_dir = Context.out_dir
# conf.hash & conf.files hold wscript files paths and hash
# (used only by Configure.autoconfig)
env['hash'] = self.hash
env['files'] = self.files
env['environ'] = dict(self.environ)
if not self.env.NO_LOCK_IN_RUN:, Options.lockfile))
if not self.env.NO_LOCK_IN_TOP:, Options.lockfile))
if not self.env.NO_LOCK_IN_OUT:, Options.lockfile))
def prepare_env(self, env):
Insert *PREFIX*, *BINDIR* and *LIBDIR* values into ``env``
:type env: :py:class:`waflib.ConfigSet.ConfigSet`
:param env: a ConfigSet, usually ``conf.env``
if not env.PREFIX:
if Options.options.prefix or Utils.is_win32:
env.PREFIX = os.path.abspath(os.path.expanduser(Options.options.prefix))
env.PREFIX = ''
if not env.BINDIR:
if Options.options.bindir:
env.BINDIR = os.path.abspath(os.path.expanduser(Options.options.bindir))
env.BINDIR = Utils.subst_vars('${PREFIX}/bin', env)
if not env.LIBDIR:
if Options.options.libdir:
env.LIBDIR = os.path.abspath(os.path.expanduser(Options.options.libdir))
env.LIBDIR = Utils.subst_vars('${PREFIX}/lib%s' % Utils.lib64(), env)
def store(self):
"""Save the config results into the cache file"""
n = self.cachedir.make_node('')
n.write('version = 0x%x\ntools = %r\n' % (Context.HEXVERSION,
if not self.all_envs:
self.fatal('nothing to store in the configuration context!')
for key in self.all_envs:
tmpenv = self.all_envs[key], key + Build.CACHE_SUFFIX))
def load(self, input, tooldir=None, funs=None):
Load Waf tools, which will be imported whenever a build is started.
:param input: waf tools to import
:type input: list of string
:param tooldir: paths for the imports
:type tooldir: list of string
:param funs: functions to execute from the waf tools
:type funs: list of string
tools = Utils.to_list(input)
if tooldir: tooldir = Utils.to_list(tooldir)
for tool in tools:
# avoid loading the same tool more than once with the same functions
# used by composite projects
mag = (tool, id(self.env), funs)
if mag in self.tool_cache:
self.to_log('(tool %s is already loaded, skipping)' % tool)
module = None
module = Context.load_tool(tool, tooldir, ctx=self)
except ImportError as e:
self.fatal('Could not load the Waf tool %r from %r\n%s' % (tool, sys.path, e))
except Exception as e:
self.to_log('imp %r (%r & %r)' % (tool, tooldir, funs))
if funs is not None:
func = getattr(module, 'configure', None)
if func:
if type(func) is type(Utils.readf): func(self)