author Lars T Hansen <>
Sat, 02 Apr 2016 08:55:24 -0700
changeset 291510 859f435f2ca001acc659cf47a2068fc94287e84a
parent 290996 63338edce3ba60f6668973b60bf832560e78d7c4
permissions -rw-r--r--
Bug 1225028 - remove Atomics.fence. r=bbouvier

# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at

def die(*args):
    'Print an error and terminate configure.'

@imports(_from='mozbuild.configure', _import='ConfigureError')
def configure_error(message):
    '''Raise a programming error and terminate configure.
    Primarily for use in moz.configure templates to sanity check
    their inputs from moz.configure usage.'''
    raise ConfigureError(message)

def is_absolute_or_relative(path):
    if os.altsep and os.altsep in path:
        return True
    return os.sep in path

@imports(_import='mozpack.path', _as='mozpath')
def normsep(path):
    return mozpath.normsep(path)

# This unlocks the sandbox. Do not copy blindly.
@imports(_import='__builtin__', _as='__builtins__')
def find_program(file):
    if is_absolute_or_relative(file):
        return os.path.abspath(file) if os.path.isfile(file) else None
    # We can't use @imports here because it imports at declaration time,
    # and the declaration of find_program happens before we ensure the
    # which module is available in sys.path somehow.
    from which import which, WhichError
        return normsep(which(file))
    except WhichError:
        return None

def unique_list(l):
    result = []
    for i in l:
        if l not in result:
    return result

@imports(_from='mozbuild.configure.util', _import='Version', _as='_Version')
def Version(v):
    'A version number that can be compared usefully.'
    return _Version(v)

# Denotes a deprecated option. Combines option() and @depends:
# @deprecated_option('--option')
# def option(value):
#     ...
# @deprecated_option() takes the same arguments as option(), except `help`.
# The function may handle the option like a typical @depends function would,
# but it is recommended it emits a deprecation error message suggesting an
# alternative option to use if there is one.
def deprecated_option(*args, **kwargs):
    assert 'help' not in kwargs
    kwargs['help'] = 'Deprecated'
    opt = option(*args, **kwargs)

    def decorator(func):
        def deprecated(value):
            if value.origin != 'default':
                return func(value)
        return deprecated

    return decorator

# from mozbuild.util import ReadOnlyNamespace as namespace
@imports(_from='mozbuild.util', _import='ReadOnlyNamespace')
def namespace(**kwargs):
    return ReadOnlyNamespace(**kwargs)

# Some @depends function return namespaces, and one could want to use one
# specific attribute from such a namespace as a "value" given to functions
# such as `set_config`. But those functions do not take immediate values.
# The `delayed_getattr` function allows access to attributes from the result
# of a @depends function in a non-immediate manner.
#   @depends('--option')
#   def option(value)
#       return namespace(foo=value)
#   set_config('FOO', delayed_getattr(option, 'foo')
def delayed_getattr(func, key):
    @imports(_from='__builtin__', _import='getattr')
    def result(value):
        # The @depends function we're being passed may have returned
        # None, or an object that simply doesn't have the wanted key.
        # In that case, just return None.
        return getattr(value, key, None)
    return result

# Like @depends, but the decorated function is only called if one of the
# arguments it would be called with has a positive value (bool(value) is True)
def depends_if(*args):
    def decorator(func):
        def wrapper(*args):
            if any(arg for arg in args):
                return func(*args)
        return wrapper
    return decorator