Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

Using precompiled headers with Scons Atom Feed 0

In Brief A Scons extension for using precompiled headers with GCC, I have mirrored it here for easy editing and improvement. Maybe it can be extended to work with other compilers as well.... more
# 's
  1# $Id$
2#
3# SCons builder for gcc's precompiled headers
4# Copyright (C) 2006 Tim Blechmann
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; see the file COPYING. If not, write to
18# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19# Boston, MA 02111-1307, USA.
20
21# $Revision$
22# $LastChangedRevision$
23# $LastChangedDate$
24# $LastChangedBy$
25
26import SCons.Action
27import SCons.Builder
28import SCons.Scanner.C
29import SCons.Util
30import SCons.Script
31
32SCons.Script.EnsureSConsVersion(0,96,92)
33
34GchAction = SCons.Action.Action('$GCHCOM', '$GCHCOMSTR')
35GchShAction = SCons.Action.Action('$GCHSHCOM', '$GCHSHCOMSTR')
36
37def gen_suffix(env, sources):
38 return sources[0].get_suffix() + env['GCHSUFFIX']
39
40
41GchShBuilder = SCons.Builder.Builder(action = GchShAction,
42 source_scanner = SCons.Scanner.C.CScanner(),
43 suffix = gen_suffix)
44
45GchBuilder = SCons.Builder.Builder(action = GchAction,
46 source_scanner = SCons.Scanner.C.CScanner(),
47 suffix = gen_suffix)
48
49def static_pch_emitter(target,source,env):
50 SCons.Defaults.StaticObjectEmitter( target, source, env )
51
52 scanner = SCons.Scanner.C.CScanner()
53 path = scanner.path(env)
54 deps = scanner(source[0], env, path)
55
56 if env.get('Gch'):
57 if env['Gch'].path.strip('.gch') in [x.path for x in deps]:
58 env.Depends(target, env['Gch'])
59
60 return (target, source)
61
62def shared_pch_emitter(target,source,env):
63 SCons.Defaults.SharedObjectEmitter( target, source, env )
64
65 scanner = SCons.Scanner.C.CScanner()
66 path = scanner.path(env)
67 deps = scanner(source[0], env, path)
68
69 if env.get('GchSh'):
70 if env['GchSh'].path.strip('.gch') in [x.path for x in deps]:
71 env.Depends(target, env['GchSh'])
72 return (target, source)
73
74def generate(env):
75 """
76 Add builders and construction variables for the Gch builder.
77 """
78 env.Append(BUILDERS = {
79 'gch': env.Builder(
80 action = GchAction,
81 target_factory = env.fs.File,
82 ),
83 'gchsh': env.Builder(
84 action = GchShAction,
85 target_factory = env.fs.File,
86 ),
87 })
88
89 try:
90 bld = env['BUILDERS']['Gch']
91 bldsh = env['BUILDERS']['GchSh']
92 except KeyError:
93 bld = GchBuilder
94 bldsh = GchShBuilder
95 env['BUILDERS']['Gch'] = bld
96 env['BUILDERS']['GchSh'] = bldsh
97
98 env['GCHCOM'] = '$CXX -o $TARGET -x c++-header -c $CXXFLAGS $_CCCOMCOM $SOURCE'
99 env['GCHSHCOM'] = '$CXX -o $TARGET -x c++-header -c $SHCXXFLAGS $_CCCOMCOM $SOURCE'
100 env['GCHSUFFIX'] = '.gch'
101
102 for suffix in SCons.Util.Split('.c .C .cc .cxx .cpp .c++'):
103 env['BUILDERS']['StaticObject'].add_emitter( suffix, static_pch_emitter )
104 env['BUILDERS']['SharedObject'].add_emitter( suffix, shared_pch_emitter )
105
106
107def exists(env):
108 return env.Detect('g++')

A Scons extension for using precompiled headers with GCC, I have mirrored it here for easy editing and improvement. Maybe it can be extended to work with other compilers as well.

# 's
1env = Environment(tools = ["default", "gch"], toolpath = '.')
2
3env['Gch'] = env.Gch('myprogramheader.hpp')[0]
4program = env.Program('myprogram', 'mysource.cpp')
5
6env['GchSh'] = env.GchSh('mylibraryheader.hpp')[0]
7library = env.SharedLibrary('mylib', 'mylib.cpp')