diff --git a/SConstruct b/SConstruct index f1dfb04..3507e97 100644 --- a/SConstruct +++ b/SConstruct @@ -1,175 +1,186 @@ from __future__ import print_function import os from os.path import join, abspath from version import write_info_file + +def gen_print(action_string, color_string, env): + """Generic function for creating pretty compile output""" + if env['verbose']: + return None + + def print_fun(command, target, source, env): + colors = env['COLOR_DICT'] + print("{}[{}] {}{}{}".format(colors[color_string], + action_string, + colors['blue'], + target[0], + colors['end'])) + return print_fun + + # Compilation colors colors = { 'cyan': '\033[96m', 'purple': '\033[95m', 'blue': '\033[94m', 'green': '\033[92m', 'yellow': '\033[93m', 'red': '\033[91m', 'end': '\033[0m' } # Inherit all environment variables (for CXX detection, etc.) main_env = Environment(ENV=os.environ) main_env['COLOR_DICT'] = colors # Compiler detection compiler_default = 'g++' if 'CXX' in os.environ: compiler_default = os.environ['CXX'] # Build variables vars = Variables('build-setup.conf') vars.Add(EnumVariable('build_type', 'Build type', 'release', allowed_values=('release', 'profiling', 'debug'), ignorecase=2)) vars.Add('prefix', 'Prefix where to install', '/usr/local') vars.Add('CXX', 'Compiler', compiler_default) vars.Add('py_exec', 'Python executable', 'python') vars.Add(BoolVariable('timer', 'Activate the timer possibilities', False)) vars.Add(BoolVariable('verbose', 'Activate verbosity', False)) vars.Add(BoolVariable('build_doc', 'Build documentation', False)) vars.Add(BoolVariable('color', 'Color the non-verbose compilation output', False)) vars.Update(main_env) Help(vars.GenerateHelpText(main_env)) # Save all options, not just those that differ from default with open('build-setup.conf', 'w') as setup: for key in vars.keys(): setup.write("{} = '{}'\n".format(key, main_env[key])) build_type = main_env['build_type'] build_dir = 'build-' + main_env['build_type'] print("Building in " + build_dir) verbose = main_env['verbose'] # Remove colors if not set if not main_env['color']: for key in colors: colors[key] = '' if not verbose: main_env['SHCXXCOMSTR'] = u'{0}[Compiling] {1}$SOURCE{2}'.format(colors['green'], colors['blue'], colors['end']) main_env['SHLINKCOMSTR'] = u'{0}[Linking] {1}$TARGET{2}'.format(colors['purple'], colors['blue'], colors['end']) main_env['SWIGCOMSTR'] = u'{0}[Swig] {1}$SOURCE{2}'.format(colors['yellow'], colors['blue'], colors['end']) # Include paths main_env.AppendUnique(CPPPATH=['#/src', '#/src/core', '#/src/surface', '#/src/bem', '#/src/python', '#/python']) # Treating Intel compiler for OpenMP if main_env['CXX'] != 'icpc': omp_libs = ['gomp'] omp_flag = '-fopenmp' else: omp_libs = [''] omp_flag = '-qopenmp' main_env.AppendUnique(LIBS=omp_libs) main_env.AppendUnique(LINKFLAGS=[omp_flag]) # Flags and options main_env.AppendUnique(CXXFLAGS=['-std=c++11', '-Wall', omp_flag]) # Adding compile flags defined in evironment if 'CXXFLAGS' in os.environ: main_env.AppendUnique(CXXFLAGS=Split(os.environ['CXXFLAGS'])) if main_env['timer']: main_env.AppendUnique(CPPDEFINES=['USING_TIMER']) cxxflags_dict = { "debug": Split("-g -O0 -DTAMAAS_DEBUG"), "profiling": Split("-g -pg -O2"), "release": Split("-O3") } shlinkflags_dict = { "debug": [], "profiling": ['-pg'], "release": [] } main_env.AppendUnique(CXXFLAGS=cxxflags_dict[build_type]) main_env.AppendUnique(SHLINKFLAGS=shlinkflags_dict[build_type]) main_env['LIBPATH'] = [abspath(join(build_dir, 'src'))] main_env['RPATH'] = "$LIBPATH" fftw_include = "" fftw_library = "" # If FFTW is provided by module system (on clusters) if 'FFTW_ROOT' in main_env['ENV']: fftw_include = join(main_env['ENV']['FFTW_ROOT'], 'include') fftw_library = join(main_env['ENV']['FFTW_ROOT'], 'lib') main_env = main_env.Clone( tools=[fftw], FFTW_LIBRARY_WISH=['main', 'omp'], FFTW_INCLUDE_DIR=fftw_include, FFTW_LIBRARY_DIR=fftw_library, ) # Writing information file write_info_file("src/tamaas_info.cpp") -Export('main_env') - -# Compiling sources -SConscript('src/SConscript', variant_dir=join(build_dir, 'src'), duplicate=False) -# Building python wrapper -SConscript('python/SConscript', variant_dir=join(build_dir, 'python'), duplicate=False) -# Copying tests -SConscript('tests/SConscript', variant_dir=join(build_dir, 'tests'), duplicate=False) - -# Building documentation -if main_env['build_doc']: - SConscript('doc/SConscript', variant_dir=join(build_dir, 'doc'), duplicate=False) - # Saving the env file env_content = """export PYTHONPATH=$PYTHONPATH:{0}/python export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{0}/src """ def write_env_file(target, source, env): + """Builder to write content to file""" with open(str(target[0]), 'w') as env_file: env_file.write(env_content.format(abspath(build_dir))) -def print_write_env_file(command, target, source, env): - colors = env['COLOR_DICT'] - print("{}[Writing] {}{}{}".format(colors['cyan'], - colors['blue'], - target[0], - colors['end'])) - -if not main_env['verbose']: - print_function = print_write_env_file -else: - print_function = None -env_file_env = main_env.Clone(PRINT_CMD_LINE_FUNC=print_function) +main_env['gen_print'] = gen_print +env_file_env = main_env.Clone(PRINT_CMD_LINE_FUNC=gen_print("Writing", "cyan", main_env)) # Need to have a command and manage tamaas_environement.sh as target because # the build directory does not always exist env_file_env.Command(join(build_dir, 'tamaas_environement.sh'), None, write_env_file) + + +Export('main_env') + + +# Building subdirs +def subdir(dir): SConscript(join(dir, 'SConscript'), + variant_dir=join(build_dir, dir), + duplicate=False) + + +for dir in ['src', 'python', 'tests']: + subdir(dir) + +# Building documentation +if main_env['build_doc']: + subdir('doc') diff --git a/tests/SConscript b/tests/SConscript index 9222a0f..75d745d 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -1,41 +1,29 @@ from __future__ import print_function from os.path import join -def print_copy(command, target, source, env): - colors = env['COLOR_DICT'] - print("{}[Copying] {}{}{}".format(colors['red'], - colors['blue'], - source[0], - colors['end'])) - Import('main_env') -if not main_env['verbose']: - print_function = print_copy -else: - print_function = None - -test_env = main_env.Clone(PRINT_CMD_LINE_FUNC=print_function) +test_env = main_env.Clone(PRINT_CMD_LINE_FUNC=main_env['gen_print']("Copying", "red", main_env)) test_files = Split(""" run_tests.sh test_hertz_pressure.py test_westergaard.py test_surface.py test_autocorrelation.py test_hertz_disp.py test_hertz_kato.py test_hertz_adhesion.py test_saturated_pressure.py test_fftransform.py test_bem_grid.py test_flood_fill.py """) src_dir = "#/tests" build_dir = 'build-' + main_env['build_type'] + '/tests' for file in test_files: source = join(src_dir, file) test_env.Command(file, source, Copy("$TARGET", "$SOURCE"))