blob: ff7a7a935de7e43f8f259a633e4dd403760525b7 [file] [log] [blame]
José Fonseca97e2c5a2009-12-31 17:58:56 +00001"""custom
2
3Custom builders and methods.
4
5"""
6
7#
José Fonseca87712852014-01-17 16:27:50 +00008# Copyright 2008 VMware, Inc.
José Fonseca97e2c5a2009-12-31 17:58:56 +00009# 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.
José Fonseca87712852014-01-17 16:27:50 +000026# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
José Fonseca97e2c5a2009-12-31 17:58:56 +000027# 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
José Fonseca97e2c5a2009-12-31 17:58:56 +000033import os.path
José Fonseca235225e2011-06-30 17:36:37 +010034import sys
35import subprocess
Jose Fonsecac521f2d2016-05-06 14:03:05 +010036import modulefinder
José Fonseca97e2c5a2009-12-31 17:58:56 +000037
38import SCons.Action
39import SCons.Builder
40import SCons.Scanner
41
42import fixes
43
Chia-I Wu582b5d82011-08-18 17:12:29 +080044import source_list
José Fonseca97e2c5a2009-12-31 17:58:56 +000045
46def quietCommandLines(env):
47 # Quiet command lines
48 # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
49 env['ASCOMSTR'] = " Assembling $SOURCE ..."
50 env['ASPPCOMSTR'] = " Assembling $SOURCE ..."
51 env['CCCOMSTR'] = " Compiling $SOURCE ..."
52 env['SHCCCOMSTR'] = " Compiling $SOURCE ..."
53 env['CXXCOMSTR'] = " Compiling $SOURCE ..."
54 env['SHCXXCOMSTR'] = " Compiling $SOURCE ..."
55 env['ARCOMSTR'] = " Archiving $TARGET ..."
56 env['RANLIBCOMSTR'] = " Indexing $TARGET ..."
57 env['LINKCOMSTR'] = " Linking $TARGET ..."
58 env['SHLINKCOMSTR'] = " Linking $TARGET ..."
59 env['LDMODULECOMSTR'] = " Linking $TARGET ..."
60 env['SWIGCOMSTR'] = " Generating $TARGET ..."
José Fonseca54d8c5e2011-03-03 15:42:58 +000061 env['LEXCOMSTR'] = " Generating $TARGET ..."
62 env['YACCCOMSTR'] = " Generating $TARGET ..."
José Fonseca2311e2a2010-02-09 23:16:26 +000063 env['CODEGENCOMSTR'] = " Generating $TARGET ..."
José Fonseca37058c32011-05-04 14:10:24 +010064 env['INSTALLSTR'] = " Installing $TARGET ..."
José Fonseca97e2c5a2009-12-31 17:58:56 +000065
66
67def createConvenienceLibBuilder(env):
68 """This is a utility function that creates the ConvenienceLibrary
69 Builder in an Environment if it is not there already.
70
71 If it is already there, we return the existing one.
72
73 Based on the stock StaticLibrary and SharedLibrary builders.
74 """
75
76 try:
77 convenience_lib = env['BUILDERS']['ConvenienceLibrary']
78 except KeyError:
79 action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
80 if env.Detect('ranlib'):
81 ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
82 action_list.append(ranlib_action)
83
84 convenience_lib = SCons.Builder.Builder(action = action_list,
85 emitter = '$LIBEMITTER',
86 prefix = '$LIBPREFIX',
87 suffix = '$LIBSUFFIX',
88 src_suffix = '$SHOBJSUFFIX',
89 src_builder = 'SharedObject')
90 env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
91
92 return convenience_lib
93
94
José Fonseca97e2c5a2009-12-31 17:58:56 +000095def python_scan(node, env, path):
96 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
Jose Fonsecac521f2d2016-05-06 14:03:05 +010097 # https://docs.python.org/2/library/modulefinder.html
José Fonseca97e2c5a2009-12-31 17:58:56 +000098 contents = node.get_contents()
99 source_dir = node.get_dir()
Jose Fonsecac521f2d2016-05-06 14:03:05 +0100100 finder = modulefinder.ModuleFinder()
101 finder.run_script(node.abspath)
José Fonseca97e2c5a2009-12-31 17:58:56 +0000102 results = []
Jose Fonsecac521f2d2016-05-06 14:03:05 +0100103 for name, mod in finder.modules.iteritems():
104 if mod.__file__ is None:
105 continue
106 assert os.path.exists(mod.__file__)
107 results.append(env.File(mod.__file__))
José Fonseca97e2c5a2009-12-31 17:58:56 +0000108 return results
109
110python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
111
112
113def code_generate(env, script, target, source, command):
114 """Method to simplify code generation via python scripts.
115
116 http://www.scons.org/wiki/UsingCodeGenerators
117 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
118 """
119
120 # We're generating code using Python scripts, so we have to be
121 # careful with our scons elements. This entry represents
122 # the generator file *in the source directory*.
123 script_src = env.File(script).srcnode()
124
125 # This command creates generated code *in the build directory*.
126 command = command.replace('$SCRIPT', script_src.path)
José Fonseca2311e2a2010-02-09 23:16:26 +0000127 action = SCons.Action.Action(command, "$CODEGENCOMSTR")
128 code = env.Command(target, source, action)
José Fonseca97e2c5a2009-12-31 17:58:56 +0000129
130 # Explicitly mark that the generated code depends on the generator,
131 # and on implicitly imported python modules
132 path = (script_src.get_dir(),)
133 deps = [script_src]
134 deps += script_src.get_implicit_deps(env, python_scanner, path)
135 env.Depends(code, deps)
136
137 # Running the Python script causes .pyc files to be generated in the
138 # source directory. When we clean up, they should go too. So add side
139 # effects for .pyc files
140 for dep in deps:
141 pyc = env.File(str(dep) + 'c')
142 env.SideEffect(pyc, code)
143
144 return code
145
146
147def createCodeGenerateMethod(env):
148 env.Append(SCANNERS = python_scanner)
149 env.AddMethod(code_generate, 'CodeGenerate')
150
151
José Fonseca235225e2011-06-30 17:36:37 +0100152def _pkg_check_modules(env, name, modules):
153 '''Simple wrapper for pkg-config.'''
154
155 env['HAVE_' + name] = False
156
157 # For backwards compatability
158 env[name.lower()] = False
159
160 if env['platform'] == 'windows':
161 return
162
163 if not env.Detect('pkg-config'):
164 return
165
166 if subprocess.call(["pkg-config", "--exists", ' '.join(modules)]) != 0:
167 return
168
José Fonseca2470e912012-02-07 11:17:35 +0000169 # Strip version expressions from modules
170 modules = [module.split(' ', 1)[0] for module in modules]
171
José Fonseca235225e2011-06-30 17:36:37 +0100172 # Other flags may affect the compilation of unrelated targets, so store
173 # them with a prefix, (e.g., XXX_CFLAGS, XXX_LIBS, etc)
174 try:
175 flags = env.ParseFlags('!pkg-config --cflags --libs ' + ' '.join(modules))
176 except OSError:
177 return
178 prefix = name + '_'
179 for flag_name, flag_value in flags.iteritems():
180 assert '_' not in flag_name
181 env[prefix + flag_name] = flag_value
182
183 env['HAVE_' + name] = True
184
185def pkg_check_modules(env, name, modules):
186
José Fonseca2470e912012-02-07 11:17:35 +0000187 sys.stdout.write('Checking for %s (%s)...' % (name, ' '.join(modules)))
José Fonseca235225e2011-06-30 17:36:37 +0100188 _pkg_check_modules(env, name, modules)
189 result = env['HAVE_' + name]
190 sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
191
192 # XXX: For backwards compatability
193 env[name.lower()] = result
194
195
196def pkg_use_modules(env, names):
197 '''Search for all environment flags that match NAME_FOO and append them to
198 the FOO environment variable.'''
199
200 names = env.Flatten(names)
201
202 for name in names:
203 prefix = name + '_'
204
205 if not 'HAVE_' + name in env:
José Fonsecaf8aeb1c2011-09-20 20:40:05 +0100206 raise Exception('Attempt to use unknown module %s' % name)
José Fonseca235225e2011-06-30 17:36:37 +0100207
208 if not env['HAVE_' + name]:
José Fonsecaf8aeb1c2011-09-20 20:40:05 +0100209 raise Exception('Attempt to use unavailable module %s' % name)
José Fonseca235225e2011-06-30 17:36:37 +0100210
211 flags = {}
212 for flag_name, flag_value in env.Dictionary().iteritems():
213 if flag_name.startswith(prefix):
214 flag_name = flag_name[len(prefix):]
215 if '_' not in flag_name:
216 flags[flag_name] = flag_value
217 if flags:
218 env.MergeFlags(flags)
219
220
221def createPkgConfigMethods(env):
222 env.AddMethod(pkg_check_modules, 'PkgCheckModules')
223 env.AddMethod(pkg_use_modules, 'PkgUseModules')
224
225
Chia-I Wu582b5d82011-08-18 17:12:29 +0800226def parse_source_list(env, filename, names=None):
227 # parse the source list file
228 parser = source_list.SourceListParser()
229 src = env.File(filename).srcnode()
José Fonsecaea8dcfc2012-08-14 12:18:45 +0100230
José Fonseca50dec632012-08-15 19:24:58 +0100231 cur_srcdir = env.Dir('.').srcnode().abspath
232 top_srcdir = env.Dir('#').abspath
233 top_builddir = os.path.join(top_srcdir, env['build_dir'])
234
José Fonseca06424372013-01-22 20:54:17 +0000235 # Normalize everything to / slashes
236 cur_srcdir = cur_srcdir.replace('\\', '/')
237 top_srcdir = top_srcdir.replace('\\', '/')
238 top_builddir = top_builddir.replace('\\', '/')
239
José Fonseca50dec632012-08-15 19:24:58 +0100240 # Populate the symbol table of the Makefile parser.
241 parser.add_symbol('top_srcdir', top_srcdir)
242 parser.add_symbol('top_builddir', top_builddir)
José Fonsecaea8dcfc2012-08-14 12:18:45 +0100243
Chia-I Wu582b5d82011-08-18 17:12:29 +0800244 sym_table = parser.parse(src.abspath)
245
246 if names:
247 if isinstance(names, basestring):
248 names = [names]
249
250 symbols = names
251 else:
252 symbols = sym_table.keys()
253
254 # convert the symbol table to source lists
255 src_lists = {}
256 for sym in symbols:
257 val = sym_table[sym]
José Fonseca50dec632012-08-15 19:24:58 +0100258 srcs = []
259 for f in val.split():
260 if f:
261 # Process source paths
262 if f.startswith(top_builddir + '/src'):
José Fonseca06424372013-01-22 20:54:17 +0000263 # Automake puts build output on a `src` subdirectory, but
264 # SCons does not, so strip it here.
José Fonseca50dec632012-08-15 19:24:58 +0100265 f = top_builddir + f[len(top_builddir + '/src'):]
266 if f.startswith(cur_srcdir + '/'):
267 # Prefer relative source paths, as absolute files tend to
268 # cause duplicate actions.
269 f = f[len(cur_srcdir + '/'):]
Jose Fonsecad4a1f3f2014-08-13 20:33:35 +0100270 # do not include any headers
271 if f.endswith('.h'):
272 continue
José Fonseca50dec632012-08-15 19:24:58 +0100273 srcs.append(f)
274
275 src_lists[sym] = srcs
Chia-I Wu582b5d82011-08-18 17:12:29 +0800276
277 # if names are given, concatenate the lists
278 if names:
279 srcs = []
280 for name in names:
281 srcs.extend(src_lists[name])
282
283 return srcs
284 else:
285 return src_lists
286
287def createParseSourceListMethod(env):
288 env.AddMethod(parse_source_list, 'ParseSourceList')
289
290
José Fonseca97e2c5a2009-12-31 17:58:56 +0000291def generate(env):
292 """Common environment generation code"""
293
José Fonsecac7bd0fa2011-06-17 18:42:39 +0100294 verbose = env.get('verbose', False) or not env.get('quiet', True)
295 if not verbose:
José Fonseca97e2c5a2009-12-31 17:58:56 +0000296 quietCommandLines(env)
297
298 # Custom builders and methods
299 createConvenienceLibBuilder(env)
300 createCodeGenerateMethod(env)
José Fonseca235225e2011-06-30 17:36:37 +0100301 createPkgConfigMethods(env)
Chia-I Wu582b5d82011-08-18 17:12:29 +0800302 createParseSourceListMethod(env)
José Fonseca97e2c5a2009-12-31 17:58:56 +0000303
304 # for debugging
305 #print env.Dump()
306
307
308def exists(env):
309 return 1