[SCons] Use precompiled header to speed up compilation with GCC/Clang
This commit is contained in:
parent
b25784dc09
commit
cbcce07981
5 changed files with 197 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -9,6 +9,7 @@ build/
|
|||
test/work/
|
||||
interfaces/cython/cantera/_cantera.h
|
||||
include/cantera/base/config.h
|
||||
include/cantera/base/system.h.gch
|
||||
include/cantera/ext/
|
||||
interfaces/matlab/ctpath.m
|
||||
interfaces/matlab/Contents.m
|
||||
|
|
|
|||
22
SConstruct
22
SConstruct
|
|
@ -60,6 +60,7 @@ if 'clean' in COMMAND_LINE_TARGETS:
|
|||
removeDirectory('.sconf_temp')
|
||||
removeFile('.sconsign.dblite')
|
||||
removeFile('include/cantera/base/config.h')
|
||||
removeFile('include/cantera/base/system.h.gch')
|
||||
removeDirectory('include/cantera/ext')
|
||||
removeFile('interfaces/cython/cantera/_cantera.cpp')
|
||||
removeFile('interfaces/cython/cantera/_cantera.h')
|
||||
|
|
@ -180,7 +181,7 @@ if os.name == 'nt':
|
|||
else:
|
||||
toolchain = ['default']
|
||||
|
||||
env = Environment(tools=toolchain+['textfile', 'subst', 'recursiveInstall', 'wix'],
|
||||
env = Environment(tools=toolchain+['textfile', 'subst', 'recursiveInstall', 'wix', 'gch'],
|
||||
ENV={'PATH': os.environ['PATH']},
|
||||
toolchain=toolchain,
|
||||
**extraEnvArgs)
|
||||
|
|
@ -245,6 +246,8 @@ defaults.noDebugCcFlags = ''
|
|||
defaults.debugLinkFlags = ''
|
||||
defaults.noDebugLinkFlags = ''
|
||||
defaults.warningFlags = '-Wall'
|
||||
defaults.buildPch = False
|
||||
env['pch_flags'] = []
|
||||
|
||||
if 'gcc' in env.subst('$CC'):
|
||||
defaults.optimizeCcFlags += ' -Wno-inline'
|
||||
|
|
@ -253,6 +256,8 @@ if 'gcc' in env.subst('$CC'):
|
|||
defaults.cxxFlags = '-std=gnu++0x'
|
||||
else:
|
||||
defaults.cxxFlags = '-std=c++0x'
|
||||
defaults.buildPch = True
|
||||
env['pch_flags'] = ['-include', 'include/cantera/base/system.h']
|
||||
|
||||
elif env['CC'] == 'cl': # Visual Studio
|
||||
defaults.cxxFlags = ['/EHsc']
|
||||
|
|
@ -272,6 +277,8 @@ elif 'icc' in env.subst('$CC'):
|
|||
elif 'clang' in env.subst('$CC'):
|
||||
defaults.ccFlags = '-fcolor-diagnostics'
|
||||
defaults.cxxFlags = '-std=c++11'
|
||||
defaults.buildPch = True
|
||||
env['pch_flags'] = ['-include-pch', 'include/cantera/base/system.h.gch']
|
||||
|
||||
else:
|
||||
print "WARNING: Unrecognized C compiler '%s'" % env['CC']
|
||||
|
|
@ -473,6 +480,9 @@ config_options = [
|
|||
string "all" or a comma separated list of variable names, e.g.
|
||||
'LD_LIBRARY_PATH,HOME'.""",
|
||||
defaults.env_vars),
|
||||
BoolVariable(
|
||||
'use_pch', """Use a precompiled-header to speed up compilation""",
|
||||
defaults.buildPch),
|
||||
('cxx_flags',
|
||||
"""Compiler flags passed to the C++ compiler only. Separate multiple
|
||||
options with spaces, e.g. cxx_flags='-g -Wextra -O3 --std=c++11'""",
|
||||
|
|
@ -1298,6 +1308,7 @@ config_h = env.Command('include/cantera/base/config.h',
|
|||
'include/cantera/base/config.h.in',
|
||||
ConfigBuilder(configh))
|
||||
env.AlwaysBuild(config_h)
|
||||
|
||||
env['config_h_target'] = config_h
|
||||
|
||||
# *********************
|
||||
|
|
@ -1396,6 +1407,15 @@ env['cantera_shared_libs'] = linkSharedLibs
|
|||
if not env['renamed_shared_libraries']:
|
||||
env['cantera_shared_libs'] = linkLibs
|
||||
|
||||
if env['use_pch']:
|
||||
env['precompiled_header'] = File('include/cantera/base/system.h')
|
||||
pch = build(env.GchSh('include/cantera/base/system.h.gch', env['precompiled_header']))
|
||||
# To force early compilaton of the precompiled header
|
||||
env.Depends(config_h, pch)
|
||||
else:
|
||||
removeFile('include/cantera/base/system.h.gch')
|
||||
env['pch_flags'] = []
|
||||
|
||||
# Add targets from the SConscript files in the various subdirectories
|
||||
Export('env', 'build', 'libraryTargets', 'install', 'buildSample')
|
||||
|
||||
|
|
|
|||
19
include/cantera/base/system.h
Normal file
19
include/cantera/base/system.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef CT_SYSTEM_H
|
||||
#define CT_SYSTEM_H
|
||||
|
||||
// commonly-included system headers
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#endif
|
||||
155
site_scons/site_tools/gch.py
Normal file
155
site_scons/site_tools/gch.py
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
#
|
||||
# SCons builder for gcc's precompiled headers
|
||||
# Copyright (C) 2006 Tim Blechmann (C) 2011 Pedro Larroy (C) 2015 Carl Cerecke
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; see the file COPYING. If not, write to
|
||||
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
# Boston, MA 02111-1307, USA.
|
||||
|
||||
# 1.3
|
||||
#
|
||||
# 09-11-2011 Pedro Larroy: Fixed dependency emitter not working with variant dir
|
||||
# 07-09-2012 Pedro Larroy: Create the resulting pch on the variant directory,
|
||||
# the variant dir should be added to the includes, before any of the other
|
||||
# local includes for the compiler to search the pch file on the build directory
|
||||
# that we just created. In doubt execute 'strace -f g++ ...' to check that it
|
||||
# opens the correct pch.
|
||||
# 2015-08-07 Carl Cerecke: Hack to work around corruption of .sconsign.dblite
|
||||
# caused by SCons.Scanner.C.CScanner() when using both variant dirs, and also
|
||||
# separate non-gch static libraries which depend on generated c++ source files.
|
||||
# Instead of using SCons.Scanner.C.CScanner, use our own hack.
|
||||
#
|
||||
# FIXME: the original precompiled header has to be set
|
||||
# in the environment as
|
||||
# env['precompiled_header'], this should be fixed
|
||||
#
|
||||
#
|
||||
# env['precompiled_header'] = File('src/common/includes/all.h')
|
||||
# env['Gch'] = env.Gch(target='common/includes/all.h.gch', source=env['precompiled_header'])
|
||||
|
||||
|
||||
|
||||
import SCons.Action
|
||||
import SCons.Builder
|
||||
import SCons.Scanner.C
|
||||
import SCons.Util
|
||||
import SCons.Script
|
||||
import os
|
||||
import functools
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
SCons.Script.EnsureSConsVersion(0,96,92)
|
||||
|
||||
GchAction = SCons.Action.Action('$GCHCOM', '$GCHCOMSTR')
|
||||
GchShAction = SCons.Action.Action('$GCHSHCOM', '$GCHSHCOMSTR')
|
||||
|
||||
def gen_suffix(env, sources):
|
||||
return sources[0].get_suffix() + env['GCHSUFFIX']
|
||||
|
||||
GchShBuilder = SCons.Builder.Builder(action = GchShAction,
|
||||
source_scanner = SCons.Scanner.C.CScanner(),
|
||||
suffix = gen_suffix)
|
||||
|
||||
GchBuilder = SCons.Builder.Builder(action = GchAction,
|
||||
source_scanner = SCons.Scanner.C.CScanner(),
|
||||
suffix = gen_suffix)
|
||||
|
||||
def header_path(node):
|
||||
h_path = node.abspath
|
||||
idx = h_path.rfind('.gch')
|
||||
if idx != -1:
|
||||
h_path = h_path[0:idx]
|
||||
if not os.path.isfile(h_path):
|
||||
raise SCons.Errors.StopError("can't find header file: {0}".format(h_path))
|
||||
return h_path
|
||||
|
||||
else:
|
||||
raise SCons.Errors.StopError("{0} file doesn't have .gch extension".format(h_path))
|
||||
|
||||
def dump_node(node):
|
||||
print '~Attrs for '+str(node)
|
||||
for attr_name in sorted(dir(node)):
|
||||
if attr_name[0] == '_':
|
||||
continue
|
||||
attr = getattr(node, attr_name)
|
||||
if type(attr) == list:
|
||||
attr = [str(x) for x in attr]
|
||||
if not callable(attr):
|
||||
print '~',attr_name+":", attr
|
||||
|
||||
|
||||
inc_re = re.compile(r'#include\s*"([a-zA-Z0-9._]+)"')
|
||||
def _directly_includes_header(node, header):
|
||||
'''
|
||||
Simple file search.
|
||||
Using SCons.Scanner.C.CScanner with variant_dir can cause corruption of .SConsign.dblite
|
||||
and you will get messages during build like:
|
||||
"scons: Cannot explain why `output/libGch1.o' is being rebuilt: No previous build information found"
|
||||
'''
|
||||
for line in open(str(node)):
|
||||
if line.startswith('#include'):
|
||||
match = inc_re.match(line)
|
||||
if match:
|
||||
fname = match.group(1)
|
||||
if fname == header:
|
||||
return True
|
||||
#return False # Should first #include should be precompiled header?
|
||||
return False
|
||||
|
||||
def pch_emitter(pch_env_key, target, source, env):
|
||||
src = source[0].srcnode()
|
||||
|
||||
if not os.path.exists(str(src)):
|
||||
return target, source
|
||||
|
||||
if env.get(pch_env_key):
|
||||
if _directly_includes_header(src, os.path.basename(env['precompiled_header'].path)):
|
||||
if 'explain' in env.GetOption('debug'):
|
||||
print 'Found dep. on pch: ', source[0], ' -> ', env[pch_env_key]
|
||||
env.Depends(target, env[pch_env_key])
|
||||
return (target, source)
|
||||
|
||||
def generate(env):
|
||||
"""
|
||||
Add builders and construction variables for the Gch builder.
|
||||
"""
|
||||
env.Append(BUILDERS = {
|
||||
'gch': env.Builder(
|
||||
action = GchAction,
|
||||
suffix = 'gch',
|
||||
target_factory = env.fs.File,
|
||||
),
|
||||
'gchsh': env.Builder(
|
||||
action = GchShAction,
|
||||
target_factory = env.fs.File,
|
||||
),
|
||||
})
|
||||
|
||||
try:
|
||||
bld = env['BUILDERS']['Gch']
|
||||
bldsh = env['BUILDERS']['GchSh']
|
||||
except KeyError:
|
||||
bld = GchBuilder
|
||||
bldsh = GchShBuilder
|
||||
env['BUILDERS']['Gch'] = bld
|
||||
env['BUILDERS']['GchSh'] = bldsh
|
||||
|
||||
env['GCHCOM'] = '$CXX -Wall -o $TARGET -x c++-header -c $CXXFLAGS $CCFLAGS $_CCCOMCOM $SOURCE'
|
||||
env['GCHSHCOM'] = '$CXX -o $TARGET -x c++-header -c $SHCXXFLAGS $SHCCFLAGS $_CCCOMCOM $SOURCE'
|
||||
env['GCHSUFFIX'] = '.gch'
|
||||
|
||||
|
||||
def exists(env):
|
||||
return env.Detect('g++')
|
||||
|
|
@ -3,6 +3,7 @@ from buildutils import *
|
|||
Import('env', 'build', 'install', 'libraryTargets')
|
||||
|
||||
def defaultSetup(env, subdir, extensions):
|
||||
env.Append(CCFLAGS=env['pch_flags'])
|
||||
return mglob(env, subdir, *extensions)
|
||||
|
||||
def baseSetup(env, subdir, extensions):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue