diff --git a/site_scons/binaries.py b/site_scons/binaries.py new file mode 100644 index 0000000..b858376 --- /dev/null +++ b/site_scons/binaries.py @@ -0,0 +1,42 @@ +# files.py +# +# Utilities for working with files and paths in SCons. +# +# Eryn Wells + +import os + + +def which(program): + ''' + Given a program name, search the system environment's $PATH for a binary of + that name. If one exists, return its name. If not, return None. This + function will also use the system environment's $PATHEXT to find binaries + with appropriate extensions (i.e., .exe on Windows). + ''' + is_executable = lambda path: ( os.path.exists(path) + and os.access(path, os.X_OK)) + + path, name = os.path.split(program) + if path: + if is_executable(program): + return program + else: + pathext = [''] + os.environ.get('PATHEXT', '').split(os.pathsep) + for path in os.environ.get('PATH', '').split(os.pathsep): + exe = os.path.join(path, program) + for ext in pathext: + candidate = exe + ext + if is_executable(candidate): + return candidate + return None + + +def first(binaries): + ''' + Given a list of binaries, return the first one found. + ''' + for binary in binaries: + if which(binary): + return binary + return None diff --git a/site_scons/site_init.py b/site_scons/site_init.py new file mode 100644 index 0000000..874833e --- /dev/null +++ b/site_scons/site_init.py @@ -0,0 +1,51 @@ +# site_init.py +# +# This file is read before every SConstruct and SConscript. So, anything that +# should be available to every build script should go here. +# +# Eryn Wells + +import os.path +import SCons.Defaults +import binaries + + +BUILD_DIR = Dir('#build') +LIB_DIR = Dir('#lib') +SRC_DIR = Dir('#src') +TEST_DIR = Dir('#test') + +# +# SCons Command Line Options +# + +AddOption('--show-build-cmds', + dest='build_cmds', + action='store_true', + help='Show build commands instead of friendly build messages') +AddOption('--mode', + dest='mode', + metavar='MODE', + help='The build mode') + +# +# Environment Configuration +# + +def has_clang(env): + _, cc_tail = os.path.split(env['CC']) + _, cxx_tail = os.path.split(env['CXX']) + return any([cc_tail.startswith('clang'), cxx_tail.startswith('clang')]) + +default_env = SCons.Defaults.DefaultEnvironment() +default_env['CC'] = binaries.first(['clang', 'gcc']) +default_env['CXX'] = binaries.first(['clang++', 'g++']) + +default_env.Append(CCFLAGS=['-Wall', '-Wextra', '-pedantic']) +if has_clang(default_env): + # Only clang supports color. + default_env.Append(CCFLAGS=['-fcolor-diagnostics']) +default_env.Append(CFLAGS=['-std=c99']) +default_env.Append(CXXFLAGS=['-std=c++11']) + +