blob: cc953bd999a0167aa728436f0b4d4b71670f8e34 [file] [log] [blame]
José Fonseca97e2c5a2009-12-31 17:58:56 +00001"""custom
2
3Custom builders and methods.
4
5"""
6
7#
8# Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
9# All Rights Reserved.
10#
11# Permission is hereby granted, free of charge, to any person obtaining a
12# copy of this software and associated documentation files (the
13# "Software"), to deal in the Software without restriction, including
14# without limitation the rights to use, copy, modify, merge, publish,
15# distribute, sub license, and/or sell copies of the Software, and to
16# permit persons to whom the Software is furnished to do so, subject to
17# the following conditions:
18#
19# The above copyright notice and this permission notice (including the
20# next paragraph) shall be included in all copies or substantial portions
21# of the Software.
22#
23# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
26# IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
27# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30#
31
32
33import os
34import os.path
35import re
José Fonseca235225e2011-06-30 17:36:37 +010036import sys
37import subprocess
José Fonseca97e2c5a2009-12-31 17:58:56 +000038
39import SCons.Action
40import SCons.Builder
41import SCons.Scanner
42
43import fixes
44
Chia-I Wu582b5d82011-08-18 17:12:29 +080045import source_list
José Fonseca97e2c5a2009-12-31 17:58:56 +000046
47def quietCommandLines(env):
48 # Quiet command lines
49 # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
50 env['ASCOMSTR'] = " Assembling $SOURCE ..."
51 env['ASPPCOMSTR'] = " Assembling $SOURCE ..."
52 env['CCCOMSTR'] = " Compiling $SOURCE ..."
53 env['SHCCCOMSTR'] = " Compiling $SOURCE ..."
54 env['CXXCOMSTR'] = " Compiling $SOURCE ..."
55 env['SHCXXCOMSTR'] = " Compiling $SOURCE ..."
56 env['ARCOMSTR'] = " Archiving $TARGET ..."
57 env['RANLIBCOMSTR'] = " Indexing $TARGET ..."
58 env['LINKCOMSTR'] = " Linking $TARGET ..."
59 env['SHLINKCOMSTR'] = " Linking $TARGET ..."
60 env['LDMODULECOMSTR'] = " Linking $TARGET ..."
61 env['SWIGCOMSTR'] = " Generating $TARGET ..."
José Fonseca54d8c5e2011-03-03 15:42:58 +000062 env['LEXCOMSTR'] = " Generating $TARGET ..."
63 env['YACCCOMSTR'] = " Generating $TARGET ..."
José Fonseca2311e2a2010-02-09 23:16:26 +000064 env['CODEGENCOMSTR'] = " Generating $TARGET ..."
José Fonseca37058c32011-05-04 14:10:24 +010065 env['INSTALLSTR'] = " Installing $TARGET ..."
José Fonseca97e2c5a2009-12-31 17:58:56 +000066
67
68def createConvenienceLibBuilder(env):
69 """This is a utility function that creates the ConvenienceLibrary
70 Builder in an Environment if it is not there already.
71
72 If it is already there, we return the existing one.
73
74 Based on the stock StaticLibrary and SharedLibrary builders.
75 """
76
77 try:
78 convenience_lib = env['BUILDERS']['ConvenienceLibrary']
79 except KeyError:
80 action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
81 if env.Detect('ranlib'):
82 ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
83 action_list.append(ranlib_action)
84
85 convenience_lib = SCons.Builder.Builder(action = action_list,
86 emitter = '$LIBEMITTER',
87 prefix = '$LIBPREFIX',
88 suffix = '$LIBSUFFIX',
89 src_suffix = '$SHOBJSUFFIX',
90 src_builder = 'SharedObject')
91 env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
92
93 return convenience_lib
94
95
96# TODO: handle import statements with multiple modules
97# TODO: handle from import statements
98import_re = re.compile(r'^import\s+(\S+)$', re.M)
99
100def python_scan(node, env, path):
101 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
102 contents = node.get_contents()
103 source_dir = node.get_dir()
104 imports = import_re.findall(contents)
105 results = []
106 for imp in imports:
107 for dir in path:
108 file = os.path.join(str(dir), imp.replace('.', os.sep) + '.py')
109 if os.path.exists(file):
110 results.append(env.File(file))
111 break
112 file = os.path.join(str(dir), imp.replace('.', os.sep), '__init__.py')
113 if os.path.exists(file):
114 results.append(env.File(file))
115 break
116 return results
117
118python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
119
120
121def code_generate(env, script, target, source, command):
122 """Method to simplify code generation via python scripts.
123
124 http://www.scons.org/wiki/UsingCodeGenerators
125 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
126 """
127
128 # We're generating code using Python scripts, so we have to be
129 # careful with our scons elements. This entry represents
130 # the generator file *in the source directory*.
131 script_src = env.File(script).srcnode()
132
133 # This command creates generated code *in the build directory*.
134 command = command.replace('$SCRIPT', script_src.path)
José Fonseca2311e2a2010-02-09 23:16:26 +0000135 action = SCons.Action.Action(command, "$CODEGENCOMSTR")
136 code = env.Command(target, source, action)
José Fonseca97e2c5a2009-12-31 17:58:56 +0000137
138 # Explicitly mark that the generated code depends on the generator,
139 # and on implicitly imported python modules
140 path = (script_src.get_dir(),)
141 deps = [script_src]
142 deps += script_src.get_implicit_deps(env, python_scanner, path)
143 env.Depends(code, deps)
144
145 # Running the Python script causes .pyc files to be generated in the
146 # source directory. When we clean up, they should go too. So add side
147 # effects for .pyc files
148 for dep in deps:
149 pyc = env.File(str(dep) + 'c')
150 env.SideEffect(pyc, code)
151
152 return code
153
154
155def createCodeGenerateMethod(env):
156 env.Append(SCANNERS = python_scanner)
157 env.AddMethod(code_generate, 'CodeGenerate')
158
159
José Fonseca235225e2011-06-30 17:36:37 +0100160def _pkg_check_modules(env, name, modules):
161 '''Simple wrapper for pkg-config.'''
162
163 env['HAVE_' + name] = False
164
165 # For backwards compatability
166 env[name.lower()] = False
167
168 if env['platform'] == 'windows':
169 return
170
171 if not env.Detect('pkg-config'):
172 return
173
174 if subprocess.call(["pkg-config", "--exists", ' '.join(modules)]) != 0:
175 return
176
José Fonseca2470e912012-02-07 11:17:35 +0000177 # Strip version expressions from modules
178 modules = [module.split(' ', 1)[0] for module in modules]
179
José Fonseca235225e2011-06-30 17:36:37 +0100180 # Other flags may affect the compilation of unrelated targets, so store
181 # them with a prefix, (e.g., XXX_CFLAGS, XXX_LIBS, etc)
182 try:
183 flags = env.ParseFlags('!pkg-config --cflags --libs ' + ' '.join(modules))
184 except OSError:
185 return
186 prefix = name + '_'
187 for flag_name, flag_value in flags.iteritems():
188 assert '_' not in flag_name
189 env[prefix + flag_name] = flag_value
190
191 env['HAVE_' + name] = True
192
193def pkg_check_modules(env, name, modules):
194
José Fonseca2470e912012-02-07 11:17:35 +0000195 sys.stdout.write('Checking for %s (%s)...' % (name, ' '.join(modules)))
José Fonseca235225e2011-06-30 17:36:37 +0100196 _pkg_check_modules(env, name, modules)
197 result = env['HAVE_' + name]
198 sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
199
200 # XXX: For backwards compatability
201 env[name.lower()] = result
202
203
204def pkg_use_modules(env, names):
205 '''Search for all environment flags that match NAME_FOO and append them to
206 the FOO environment variable.'''
207
208 names = env.Flatten(names)
209
210 for name in names:
211 prefix = name + '_'
212
213 if not 'HAVE_' + name in env:
José Fonsecaf8aeb1c2011-09-20 20:40:05 +0100214 raise Exception('Attempt to use unknown module %s' % name)
José Fonseca235225e2011-06-30 17:36:37 +0100215
216 if not env['HAVE_' + name]:
José Fonsecaf8aeb1c2011-09-20 20:40:05 +0100217 raise Exception('Attempt to use unavailable module %s' % name)
José Fonseca235225e2011-06-30 17:36:37 +0100218
219 flags = {}
220 for flag_name, flag_value in env.Dictionary().iteritems():
221 if flag_name.startswith(prefix):
222 flag_name = flag_name[len(prefix):]
223 if '_' not in flag_name:
224 flags[flag_name] = flag_value
225 if flags:
226 env.MergeFlags(flags)
227
228
229def createPkgConfigMethods(env):
230 env.AddMethod(pkg_check_modules, 'PkgCheckModules')
231 env.AddMethod(pkg_use_modules, 'PkgUseModules')
232
233
Chia-I Wu582b5d82011-08-18 17:12:29 +0800234def parse_source_list(env, filename, names=None):
235 # parse the source list file
236 parser = source_list.SourceListParser()
237 src = env.File(filename).srcnode()
238 sym_table = parser.parse(src.abspath)
239
240 if names:
241 if isinstance(names, basestring):
242 names = [names]
243
244 symbols = names
245 else:
246 symbols = sym_table.keys()
247
248 # convert the symbol table to source lists
249 src_lists = {}
250 for sym in symbols:
251 val = sym_table[sym]
252 src_lists[sym] = [f for f in val.split(' ') if f]
253
254 # if names are given, concatenate the lists
255 if names:
256 srcs = []
257 for name in names:
258 srcs.extend(src_lists[name])
259
260 return srcs
261 else:
262 return src_lists
263
264def createParseSourceListMethod(env):
265 env.AddMethod(parse_source_list, 'ParseSourceList')
266
267
José Fonseca97e2c5a2009-12-31 17:58:56 +0000268def generate(env):
269 """Common environment generation code"""
270
José Fonsecac7bd0fa2011-06-17 18:42:39 +0100271 verbose = env.get('verbose', False) or not env.get('quiet', True)
272 if not verbose:
José Fonseca97e2c5a2009-12-31 17:58:56 +0000273 quietCommandLines(env)
274
275 # Custom builders and methods
276 createConvenienceLibBuilder(env)
277 createCodeGenerateMethod(env)
José Fonseca235225e2011-06-30 17:36:37 +0100278 createPkgConfigMethods(env)
Chia-I Wu582b5d82011-08-18 17:12:29 +0800279 createParseSourceListMethod(env)
José Fonseca97e2c5a2009-12-31 17:58:56 +0000280
281 # for debugging
282 #print env.Dump()
283
284
285def exists(env):
286 return 1