Lumiera  0.pre.03
»edit your freedom«
BuilderGCH.py
1 # -*- python -*-
2 
5 
6 # Copyright (C) scons.org/wiki/GchBuilder
7 # 2006 Tim Blechmann
8 # 2008 Hermann Vosseler <Ichthyostega@web.de>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; either version 2 of
13 # the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, see http://www.gnu.org/licenses/
22 
23 
24 # history: 8/2008 adapted for Lumiera build system
25 # changed to accept a list of precompiled header defs
26 
27 
28 from types import ListType
29 
30 import SCons.Action
31 import SCons.Builder
32 import SCons.Scanner.C
33 import SCons.Util
34 import SCons.Script
35 
36 SCons.Script.EnsureSConsVersion(0,96,92)
37 
38 GchAction = SCons.Action.Action('$GCHCOM', '$GCHCOMSTR')
39 GchShAction = SCons.Action.Action('$GCHSHCOM', '$GCHSHCOMSTR')
40 
41 def gen_suffix(env, sources):
42  return sources[0].get_suffix() + env['GCHSUFFIX']
43 
44 
45 GchShBuilder = SCons.Builder.Builder(action = GchShAction,
46  source_scanner = SCons.Scanner.C.CScanner(),
47  suffix = gen_suffix)
48 
49 GchBuilder = SCons.Builder.Builder(action = GchAction,
50  source_scanner = SCons.Scanner.C.CScanner(),
51  suffix = gen_suffix)
52 
53 def setup_dependency(target,source,env, key):
54  scanner = SCons.Scanner.C.CScanner()
55  path = scanner.path(env)
56  deps = scanner(source[0], env, path)
57 
58  if env.has_key(key) and env[key]:
59  for header in env[key]:
60  header_path = header.path.strip('.gch')
61  if header_path in [x.path for x in deps]:
62  print "Precompiled header(%s) %s \t <--- %s" % (key,header_path,source[0])
63  env.Depends(target, header)
64 
65 
66 def static_pch_emitter(target,source,env):
67  SCons.Defaults.StaticObjectEmitter( target, source, env )
68  setup_dependency(target,source,env, key='GCH')
69  return (target, source)
70 
71 def shared_pch_emitter(target,source,env):
72  SCons.Defaults.SharedObjectEmitter( target, source, env )
73  setup_dependency(target,source,env, key='GCH-sh')
74  return (target, source)
75 
76 
77 def generate(env):
78  """ Add builders and construction variables for the Gch builder.
79  """
80  env.Append(BUILDERS = {
81  'gch': env.Builder(
82  action = GchAction,
83  target_factory = env.fs.File,
84  ),
85  'gchsh': env.Builder(
86  action = GchShAction,
87  target_factory = env.fs.File,
88  ),
89  })
90 
91  try:
92  bld = env['BUILDERS']['GCH']
93  bldsh = env['BUILDERS']['GCH-sh']
94  except KeyError:
95  bld = GchBuilder
96  bldsh = GchShBuilder
97  env['BUILDERS']['PrecompiledHeader'] = bld
98  env['BUILDERS']['PrecompiledHeaderShared'] = bldsh
99 
100  env['GCHCOM'] = '$CXX -o $TARGET -x c++-header -c $CXXFLAGS $_CCCOMCOM $SOURCE'
101  env['GCHSHCOM'] = '$CXX -o $TARGET -x c++-header -c $SHCXXFLAGS $_CCCOMCOM $SOURCE'
102  env['GCHSUFFIX'] = '.gch'
103 
104  for suffix in SCons.Util.Split('.c .C .cc .cxx .cpp .c++'):
105  env['BUILDERS']['StaticObject'].add_emitter( suffix, static_pch_emitter )
106  env['BUILDERS']['SharedObject'].add_emitter( suffix, shared_pch_emitter )
107 
108 
109 def exists(env):
110  return env.Detect('g++')