Merge branch 'glsl-to-tgsi'
[mesa.git] / scons / custom.py
1 """custom
2
3 Custom 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
33 import os
34 import os.path
35 import re
36 import sys
37 import subprocess
38
39 import SCons.Action
40 import SCons.Builder
41 import SCons.Scanner
42
43 import fixes
44
45
46 def 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 ..."
61 env['LEXCOMSTR'] = " Generating $TARGET ..."
62 env['YACCCOMSTR'] = " Generating $TARGET ..."
63 env['CODEGENCOMSTR'] = " Generating $TARGET ..."
64 env['INSTALLSTR'] = " Installing $TARGET ..."
65
66
67 def 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
95 # TODO: handle import statements with multiple modules
96 # TODO: handle from import statements
97 import_re = re.compile(r'^import\s+(\S+)$', re.M)
98
99 def python_scan(node, env, path):
100 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
101 contents = node.get_contents()
102 source_dir = node.get_dir()
103 imports = import_re.findall(contents)
104 results = []
105 for imp in imports:
106 for dir in path:
107 file = os.path.join(str(dir), imp.replace('.', os.sep) + '.py')
108 if os.path.exists(file):
109 results.append(env.File(file))
110 break
111 file = os.path.join(str(dir), imp.replace('.', os.sep), '__init__.py')
112 if os.path.exists(file):
113 results.append(env.File(file))
114 break
115 return results
116
117 python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
118
119
120 def code_generate(env, script, target, source, command):
121 """Method to simplify code generation via python scripts.
122
123 http://www.scons.org/wiki/UsingCodeGenerators
124 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
125 """
126
127 # We're generating code using Python scripts, so we have to be
128 # careful with our scons elements. This entry represents
129 # the generator file *in the source directory*.
130 script_src = env.File(script).srcnode()
131
132 # This command creates generated code *in the build directory*.
133 command = command.replace('$SCRIPT', script_src.path)
134 action = SCons.Action.Action(command, "$CODEGENCOMSTR")
135 code = env.Command(target, source, action)
136
137 # Explicitly mark that the generated code depends on the generator,
138 # and on implicitly imported python modules
139 path = (script_src.get_dir(),)
140 deps = [script_src]
141 deps += script_src.get_implicit_deps(env, python_scanner, path)
142 env.Depends(code, deps)
143
144 # Running the Python script causes .pyc files to be generated in the
145 # source directory. When we clean up, they should go too. So add side
146 # effects for .pyc files
147 for dep in deps:
148 pyc = env.File(str(dep) + 'c')
149 env.SideEffect(pyc, code)
150
151 return code
152
153
154 def createCodeGenerateMethod(env):
155 env.Append(SCANNERS = python_scanner)
156 env.AddMethod(code_generate, 'CodeGenerate')
157
158
159 def _pkg_check_modules(env, name, modules):
160 '''Simple wrapper for pkg-config.'''
161
162 env['HAVE_' + name] = False
163
164 # For backwards compatability
165 env[name.lower()] = False
166
167 if env['platform'] == 'windows':
168 return
169
170 if not env.Detect('pkg-config'):
171 return
172
173 if subprocess.call(["pkg-config", "--exists", ' '.join(modules)]) != 0:
174 return
175
176 # Other flags may affect the compilation of unrelated targets, so store
177 # them with a prefix, (e.g., XXX_CFLAGS, XXX_LIBS, etc)
178 try:
179 flags = env.ParseFlags('!pkg-config --cflags --libs ' + ' '.join(modules))
180 except OSError:
181 return
182 prefix = name + '_'
183 for flag_name, flag_value in flags.iteritems():
184 assert '_' not in flag_name
185 env[prefix + flag_name] = flag_value
186
187 env['HAVE_' + name] = True
188
189 def pkg_check_modules(env, name, modules):
190
191 sys.stdout.write('Checking for %s...' % name)
192 _pkg_check_modules(env, name, modules)
193 result = env['HAVE_' + name]
194 sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
195
196 # XXX: For backwards compatability
197 env[name.lower()] = result
198
199
200 def pkg_use_modules(env, names):
201 '''Search for all environment flags that match NAME_FOO and append them to
202 the FOO environment variable.'''
203
204 names = env.Flatten(names)
205
206 for name in names:
207 prefix = name + '_'
208
209 if not 'HAVE_' + name in env:
210 print 'Attempt to use unknown module %s' % name
211 env.Exit(1)
212
213 if not env['HAVE_' + name]:
214 print 'Attempt to use unavailable module %s' % name
215 env.Exit(1)
216
217 flags = {}
218 for flag_name, flag_value in env.Dictionary().iteritems():
219 if flag_name.startswith(prefix):
220 flag_name = flag_name[len(prefix):]
221 if '_' not in flag_name:
222 flags[flag_name] = flag_value
223 if flags:
224 env.MergeFlags(flags)
225
226
227 def createPkgConfigMethods(env):
228 env.AddMethod(pkg_check_modules, 'PkgCheckModules')
229 env.AddMethod(pkg_use_modules, 'PkgUseModules')
230
231
232 def generate(env):
233 """Common environment generation code"""
234
235 verbose = env.get('verbose', False) or not env.get('quiet', True)
236 if not verbose:
237 quietCommandLines(env)
238
239 # Custom builders and methods
240 createConvenienceLibBuilder(env)
241 createCodeGenerateMethod(env)
242 createPkgConfigMethods(env)
243
244 # for debugging
245 #print env.Dump()
246
247
248 def exists(env):
249 return 1