Commit d4ea93cb authored by André Anjos's avatar André Anjos 💬
Browse files

Merge branch 'improve-build-setup' into 'master'

Improve build setup



See merge request !15
parents e14d03b6 5907c415
Pipeline #3712 canceled with stage
......@@ -23,11 +23,18 @@ class EnvironmentWrapper(object):
settings from initialization.
"""
DEBUG_FLAGS = '-O0 -g -DBOB_DEBUG'
RELEASE_FLAGS = '-O3 -g0 -DNDEBUG -mtune=native'
DEBUG_CFLAGS = '-O0 -g -DBOB_DEBUG'
RELEASE_CFLAGS = '-O3 -g0 -DNDEBUG -mtune=generic'
LDFLAGS = ''
MACOSX_DEPLOYMENT_TARGET = '10.9'
if platform.system() == 'Darwin':
DEBUG_CFLAGS += ' -pthread'
LDFLAGS = '-lpthread'
# Note: CLang does not work well with BZ_DEBUG\n
if platform.system() != 'Darwin':
DEBUG_FLAGS += " -DBZ_DEBUG"
DEBUG_CFLAGS += ' -DBZ_DEBUG'
def __init__(self, logger, debug=None, prefixes=None, environ=None):
......@@ -45,32 +52,76 @@ class EnvironmentWrapper(object):
# set the pkg-config paths to look at, environment settings in front
prefixes = prefixes if prefixes else []
if 'CMAKE_PREFIX_PATH' in self.environ:
prefixes = self.environ['CMAKE_PREFIX_PATH'].split(os.pathsep) + prefixes
if 'CMAKE_PREFIX_PATH' in os.environ:
prefixes = os.environ['CMAKE_PREFIX_PATH'].split(os.pathsep) + prefixes
if 'BOB_PREFIX_PATH' in self.environ:
prefixes = self.environ['BOB_PREFIX_PATH'].split(os.pathsep) + prefixes
if 'BOB_PREFIX_PATH' in os.environ:
prefixes = os.environ['BOB_PREFIX_PATH'].split(os.pathsep) + prefixes
pkgcfg += [os.path.join(k, 'lib', 'pkgconfig') for k in prefixes]
pkgcfg += [os.path.join(k, 'lib64', 'pkgconfig') for k in prefixes]
pkgcfg += [os.path.join(k, 'lib32', 'pkgconfig') for k in prefixes]
# joins all paths
if prefixes:
self.environ['BOB_PREFIX_PATH'] = os.pathsep.join(prefixes)
if pkgcfg:
self.environ['PKG_CONFIG_PATH'] = os.pathsep.join(pkgcfg)
def __remove_environ(key):
if key in self.environ: del self.environ[key]
def __append_to_environ(key, value, sep=' '):
if self.environ.get(key):
if value:
self.environ[key] += sep + value.strip(sep)
else:
if value:
self.environ[key] = value.strip(sep)
# joins all paths, respecting potential environment variables set by the
# user, with priority
__remove_environ('BOB_PREFIX_PATH')
__append_to_environ('BOB_PREFIX_PATH', os.pathsep.join(prefixes),
os.pathsep)
__remove_environ('CMAKE_PREFIX_PATH')
__append_to_environ('CMAKE_PREFIX_PATH', os.pathsep.join(prefixes),
os.pathsep)
__remove_environ('PKG_CONFIG_PATH')
__append_to_environ('PKG_CONFIG_PATH', os.environ.get('PKG_CONFIG_PATH'),
os.pathsep)
__append_to_environ('PKG_CONFIG_PATH', os.pathsep.join(pkgcfg), os.pathsep)
# reset the CFLAGS and CXXFLAGS depending on the user input
cflags = None
if self.debug is True: cflags = str(EnvironmentWrapper.DEBUG_FLAGS)
elif self.debug is False: cflags = str(EnvironmentWrapper.RELEASE_FLAGS)
if self.debug is True: cflags = str(EnvironmentWrapper.DEBUG_CFLAGS)
elif self.debug is False: cflags = str(EnvironmentWrapper.RELEASE_CFLAGS)
# else: pass
if cflags:
self.environ['CFLAGS'] = cflags + ' ' + \
self.environ.get('CFLAGS', '') + os.environ.get('CFLAGS', '')
self.environ['CFLAGS'] = self.environ['CFLAGS'].strip() #clean-up
self.environ['CXXFLAGS'] = cflags + ' ' + \
self.environ.get('CXXFLAGS', '') + os.environ.get('CXXFLAGS', '')
self.environ['CXXFLAGS'] = self.environ['CXXFLAGS'].strip() #clean-up
def _order_flags(key, internal=None):
if internal:
# prepend internal
saved = self.environ.get(key)
__remove_environ(key)
__append_to_environ(key, internal)
__append_to_environ(key, saved)
__append_to_environ(key, os.environ.get(key))
# for these environment variables, values set on the environment come last
# so they can override, values set on the buildout recipe or our internal
# settings
if cflags is not None:
_order_flags('CFLAGS', cflags)
_order_flags('CXXFLAGS', cflags)
_order_flags('LDFLAGS', EnvironmentWrapper.LDFLAGS)
# sets the MacOSX deployment target, if the user has not yet set it on
# their environment
if platform.system() == 'Darwin':
if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
self.environ['MACOSX_DEPLOYMENT_TARGET'] = \
os.environ['MACOSX_DEPLOYMENT_TARGET']
else:
self.environ['MACOSX_DEPLOYMENT_TARGET'] = \
EnvironmentWrapper.MACOSX_DEPLOYMENT_TARGET
def set(self):
"""Sets the current environment for variables needed for the setup of the
......
......@@ -8,6 +8,7 @@
import os
import logging
import nose.tools
import platform
from .envwrapper import EnvironmentWrapper
......@@ -34,7 +35,8 @@ def test_default():
def cleanup():
'''Removes weird variables from the user environment just for the tests'''
remove = ['CFLAGS', 'CXXFLAGS', 'BOB_PREFIX_PATH', 'PKG_CONFIG_PATH']
remove = ['CFLAGS', 'CXXFLAGS', 'LDFLAGS', 'BOB_PREFIX_PATH',
'PKG_CONFIG_PATH', 'CMAKE_PREFIX_PATH', 'MACOSX_DEPLOYMENT_TARGET']
for key in remove:
if key in os.environ: del os.environ[key]
......@@ -50,13 +52,28 @@ def test_set_debug_true():
before = dict(os.environ)
e.set()
nose.tools.eq_(len(os.environ) - len(before), 2)
if platform.system() == 'Darwin':
nose.tools.eq_(len(os.environ) - len(before), 4)
else:
# Linux
nose.tools.eq_(len(os.environ) - len(before), 2)
assert 'CFLAGS' in os.environ
assert os.environ['CFLAGS'].find(EnvironmentWrapper.DEBUG_FLAGS) >= 0
assert os.environ['CFLAGS'].find(EnvironmentWrapper.RELEASE_FLAGS) < 0
assert os.environ['CFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) >= 0
assert os.environ['CFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) < 0
assert 'CXXFLAGS' in os.environ
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.DEBUG_FLAGS) >= 0
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.RELEASE_FLAGS) < 0
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) >= 0
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) < 0
if platform.system() == 'Darwin':
assert 'LDFLAGS' in os.environ
assert os.environ['LDFLAGS'].find(EnvironmentWrapper.LDFLAGS) >= 0
assert 'MACOSX_DEPLOYMENT_TARGET' in os.environ
assert os.environ['MACOSX_DEPLOYMENT_TARGET'] == \
EnvironmentWrapper.MACOSX_DEPLOYMENT_TARGET
e.unset()
for key in before:
......@@ -77,15 +94,30 @@ def test_set_debug_false():
before = dict(os.environ)
e.set()
nose.tools.eq_(len(os.environ) - len(before), 2)
if platform.system() == 'Darwin':
nose.tools.eq_(len(os.environ) - len(before), 4)
else:
# Linux
nose.tools.eq_(len(os.environ) - len(before), 2)
assert 'CFLAGS' in os.environ
assert 'CXXFLAGS' in os.environ
nose.tools.eq_(os.environ['CFLAGS'], e.environ['CFLAGS'])
assert os.environ['CFLAGS'].find(EnvironmentWrapper.DEBUG_FLAGS) < 0
assert os.environ['CFLAGS'].find(EnvironmentWrapper.RELEASE_FLAGS) >= 0
assert os.environ['CFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) < 0
assert os.environ['CFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) >= 0
nose.tools.eq_(os.environ['CXXFLAGS'], e.environ['CXXFLAGS'])
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.DEBUG_FLAGS) < 0
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.RELEASE_FLAGS) >= 0
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.DEBUG_CFLAGS) < 0
assert os.environ['CXXFLAGS'].find(EnvironmentWrapper.RELEASE_CFLAGS) >= 0
if platform.system() == 'Darwin':
assert 'LDFLAGS' in os.environ
assert os.environ['LDFLAGS'].find(EnvironmentWrapper.LDFLAGS) >= 0
assert 'MACOSX_DEPLOYMENT_TARGET' in os.environ
assert os.environ['MACOSX_DEPLOYMENT_TARGET'] == \
EnvironmentWrapper.MACOSX_DEPLOYMENT_TARGET
e.unset()
for key in before:
......@@ -111,6 +143,8 @@ def test_set_prefixes():
nose.tools.eq_(os.environ['PKG_CONFIG_PATH'], e.environ['PKG_CONFIG_PATH'])
assert 'BOB_PREFIX_PATH' in os.environ
nose.tools.eq_(os.environ['BOB_PREFIX_PATH'], os.pathsep.join(prefixes))
assert 'CMAKE_PREFIX_PATH' in os.environ
nose.tools.eq_(os.environ['CMAKE_PREFIX_PATH'], os.pathsep.join(prefixes))
e.unset()
for key in before:
......@@ -132,6 +166,7 @@ def test_set_environment():
before = dict(os.environ)
e.set()
nose.tools.eq_(len(os.environ) - len(before), 1)
assert varname in os.environ
nose.tools.eq_(os.environ[varname], varvalue)
......@@ -179,7 +214,8 @@ def test_set_multiple():
CFLAGS='-DNDEBUG',
CXXFLAGS='${CFLAGS}',
PKG_CONFIG_PATH='/a/b/lib/pkgconfig',
BOB_PREFIX_PATH='/c/d'
BOB_PREFIX_PATH='/c/d',
CMAKE_PREFIX_PATH='/d/f',
)
e = EnvironmentWrapper(logging.getLogger(), debug=True, environ=environ)
......@@ -187,10 +223,18 @@ def test_set_multiple():
before = dict(os.environ)
e.set()
nose.tools.eq_(len(os.environ) - len(before), 4)
nose.tools.eq_(os.environ['CFLAGS'], EnvironmentWrapper.DEBUG_FLAGS + ' ' + environ['CFLAGS'])
if platform.system() == 'Darwin':
nose.tools.eq_(len(os.environ) - len(before), 7)
else:
# Linux
nose.tools.eq_(len(os.environ) - len(before), 5)
nose.tools.eq_(os.environ['CFLAGS'], EnvironmentWrapper.DEBUG_CFLAGS + ' ' + environ['CFLAGS'])
nose.tools.eq_(os.environ['CXXFLAGS'], os.environ['CFLAGS'])
nose.tools.eq_(os.environ['BOB_PREFIX_PATH'], environ['BOB_PREFIX_PATH'])
assert os.environ['BOB_PREFIX_PATH'].startswith(environ['BOB_PREFIX_PATH'])
assert os.environ['BOB_PREFIX_PATH'].endswith(environ['CMAKE_PREFIX_PATH'])
assert os.environ['CMAKE_PREFIX_PATH'] == os.environ['BOB_PREFIX_PATH']
assert os.environ['PKG_CONFIG_PATH'].startswith(environ['PKG_CONFIG_PATH'])
assert os.environ['PKG_CONFIG_PATH'].find(environ['BOB_PREFIX_PATH']) >= 0
......@@ -217,7 +261,13 @@ def test_preserve_user():
before = dict(os.environ)
e.set()
nose.tools.eq_(len(os.environ) - len(before), 1)
if platform.system() == 'Darwin':
nose.tools.eq_(len(os.environ) - len(before), 3)
else:
# Linux
nose.tools.eq_(len(os.environ) - len(before), 1)
assert os.environ['CFLAGS'].endswith('-BUILDOUT-TEST-STRING')
e.unset()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment