public
Last active

Precompiled header builder for SCons

  • Download Gist
gch.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#
# SCons builder for gcc's precompiled headers
# Copyright (C) 2006 Tim Blechmann (C) 2011 Pedro Larroy
#
# 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.2
#
# 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.
#
# FIXME: the original precompiled header has to be set
# in the environment as
# env['precompiled_header'], this should be fixed
#
#
# Example usage:
# src_variant_dir = 'build/release'
# includes = [
# Dir(src_variant_dir),
# ... other includes ...
# ]
# ... set includes in environment ...
# 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
 
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 pch_emitter(pch_env_key, target, source, env):
# print 'target', target[0].str_for_display(), 'source', str(source[0])
# print 'target'
# print map(lambda x: x.str_for_display(), target)
# print 'source'
# print map(lambda x: x.str_for_display(), source)
# print 'env:'
# print env.get(pch_env_key)
# print type(env.get(pch_env_key))
# print
SCons.Defaults.StaticObjectEmitter( target, source, env )
scanner = SCons.Scanner.C.CScanner()
path = scanner.path(env)
 
deps = scanner(source[0], env, path)
if env.get(pch_env_key):
h_path = env['precompiled_header'].abspath
#print 'h_path', h_path
#print env['Gch'].abspath
#print map(lambda x: x.str_for_display(), deps)
#if h_path in [x.abspath for x in deps]:
for i in [x.abspath for x in deps]:
#print '|{0}|{1}|'.format(i, h_path)
if i == h_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 $CCFLAGS $_CCCOMCOM $SOURCE'
env['GCHSUFFIX'] = '.gch'
 
for suffix in SCons.Util.Split('.c .C .cc .cxx .cpp .c++'):
static_pch_emitter = functools.partial(pch_emitter, 'Gch')
env['BUILDERS']['StaticObject'].add_emitter( suffix, static_pch_emitter )
shared_pch_emitter = functools.partial(pch_emitter, 'GchSh')
env['BUILDERS']['SharedObject'].add_emitter( suffix, shared_pch_emitter )
 
 
def exists(env):
return env.Detect('g++')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.