5f149f952844945a49e6441c3f7eb5c7e419c612
[mesa.git] / scons / gallium.py
1 """gallium
2
3 Frontend-tool for Gallium3D architecture.
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
37 import SCons.Action
38 import SCons.Builder
39 import SCons.Scanner
40
41 import fixes
42
43
44 def quietCommandLines(env):
45 # Quiet command lines
46 # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
47 env['ASCOMSTR'] = " Assembling $SOURCE ..."
48 env['ASPPCOMSTR'] = " Assembling $SOURCE ..."
49 env['CCCOMSTR'] = " Compiling $SOURCE ..."
50 env['SHCCCOMSTR'] = " Compiling $SOURCE ..."
51 env['CXXCOMSTR'] = " Compiling $SOURCE ..."
52 env['SHCXXCOMSTR'] = " Compiling $SOURCE ..."
53 env['ARCOMSTR'] = " Archiving $TARGET ..."
54 env['RANLIBCOMSTR'] = " Indexing $TARGET ..."
55 env['LINKCOMSTR'] = " Linking $TARGET ..."
56 env['SHLINKCOMSTR'] = " Linking $TARGET ..."
57 env['LDMODULECOMSTR'] = " Linking $TARGET ..."
58 env['SWIGCOMSTR'] = " Generating $TARGET ..."
59
60
61 def createConvenienceLibBuilder(env):
62 """This is a utility function that creates the ConvenienceLibrary
63 Builder in an Environment if it is not there already.
64
65 If it is already there, we return the existing one.
66
67 Based on the stock StaticLibrary and SharedLibrary builders.
68 """
69
70 try:
71 convenience_lib = env['BUILDERS']['ConvenienceLibrary']
72 except KeyError:
73 action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
74 if env.Detect('ranlib'):
75 ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
76 action_list.append(ranlib_action)
77
78 convenience_lib = SCons.Builder.Builder(action = action_list,
79 emitter = '$LIBEMITTER',
80 prefix = '$LIBPREFIX',
81 suffix = '$LIBSUFFIX',
82 src_suffix = '$SHOBJSUFFIX',
83 src_builder = 'SharedObject')
84 env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
85
86 return convenience_lib
87
88
89 # TODO: handle import statements with multiple modules
90 # TODO: handle from import statements
91 import_re = re.compile(r'^import\s+(\S+)$', re.M)
92
93 def python_scan(node, env, path):
94 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
95 contents = node.get_contents()
96 source_dir = node.get_dir()
97 imports = import_re.findall(contents)
98 results = []
99 for imp in imports:
100 for dir in path:
101 file = os.path.join(str(dir), imp.replace('.', os.sep) + '.py')
102 if os.path.exists(file):
103 results.append(env.File(file))
104 break
105 file = os.path.join(str(dir), imp.replace('.', os.sep), '__init__.py')
106 if os.path.exists(file):
107 results.append(env.File(file))
108 break
109 return results
110
111 python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
112
113
114 def code_generate(env, script, target, source, command):
115 """Method to simplify code generation via python scripts.
116
117 http://www.scons.org/wiki/UsingCodeGenerators
118 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
119 """
120
121 # We're generating code using Python scripts, so we have to be
122 # careful with our scons elements. This entry represents
123 # the generator file *in the source directory*.
124 script_src = env.File(script).srcnode()
125
126 # This command creates generated code *in the build directory*.
127 command = command.replace('$SCRIPT', script_src.path)
128 code = env.Command(target, source, command)
129
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
147 def createCodeGenerateMethod(env):
148 env.Append(SCANNERS = python_scanner)
149 env.AddMethod(code_generate, 'CodeGenerate')
150
151
152 def symlink(target, source, env):
153 target = str(target[0])
154 source = str(source[0])
155 if os.path.islink(target) or os.path.exists(target):
156 os.remove(target)
157 os.symlink(os.path.basename(source), target)
158
159 def install_shared_library(env, source, version = ()):
160 source = str(source[0])
161 version = tuple(map(str, version))
162 target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build'], 'lib')
163 target_name = '.'.join((str(source),) + version)
164 last = env.InstallAs(os.path.join(target_dir, target_name), source)
165 while len(version):
166 version = version[:-1]
167 target_name = '.'.join((str(source),) + version)
168 action = SCons.Action.Action(symlink, "$TARGET -> $SOURCE")
169 last = env.Command(os.path.join(target_dir, target_name), last, action)
170
171 def createInstallMethods(env):
172 env.AddMethod(install_shared_library, 'InstallSharedLibrary')
173
174
175 def num_jobs():
176 try:
177 return int(os.environ['NUMBER_OF_PROCESSORS'])
178 except (ValueError, KeyError):
179 pass
180
181 try:
182 return os.sysconf('SC_NPROCESSORS_ONLN')
183 except (ValueError, OSError, AttributeError):
184 pass
185
186 try:
187 return int(os.popen2("sysctl -n hw.ncpu")[1].read())
188 except ValueError:
189 pass
190
191 return 1
192
193
194 def generate(env):
195 """Common environment generation code"""
196
197 if env.get('quiet', True):
198 quietCommandLines(env)
199
200 # Toolchain
201 platform = env['platform']
202 if env['toolchain'] == 'default':
203 if platform == 'winddk':
204 env['toolchain'] = 'winddk'
205 elif platform == 'wince':
206 env['toolchain'] = 'wcesdk'
207 env.Tool(env['toolchain'])
208
209 env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
210 env['msvc'] = env['CC'] == 'cl'
211
212 # shortcuts
213 debug = env['debug']
214 machine = env['machine']
215 platform = env['platform']
216 x86 = env['machine'] == 'x86'
217 ppc = env['machine'] == 'ppc'
218 gcc = env['gcc']
219 msvc = env['msvc']
220
221 # Put build output in a separate dir, which depends on the current
222 # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
223 build_topdir = 'build'
224 build_subdir = env['platform']
225 if env['llvm']:
226 build_subdir += "-llvm"
227 if env['machine'] != 'generic':
228 build_subdir += '-' + env['machine']
229 if env['debug']:
230 build_subdir += "-debug"
231 if env['profile']:
232 build_subdir += "-profile"
233 build_dir = os.path.join(build_topdir, build_subdir)
234 # Place the .sconsign file in the build dir too, to avoid issues with
235 # different scons versions building the same source file
236 env['build'] = build_dir
237 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
238 env.CacheDir('build/cache')
239
240 # Parallel build
241 if env.GetOption('num_jobs') <= 1:
242 env.SetOption('num_jobs', num_jobs())
243
244 # C preprocessor options
245 cppdefines = []
246 if debug:
247 cppdefines += ['DEBUG']
248 else:
249 cppdefines += ['NDEBUG']
250 if env['profile']:
251 cppdefines += ['PROFILE']
252 if platform == 'windows':
253 cppdefines += [
254 'WIN32',
255 '_WINDOWS',
256 #'_UNICODE',
257 #'UNICODE',
258 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
259 ('WINVER', '0x0501'),
260 # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
261 'WIN32_LEAN_AND_MEAN',
262 ]
263 if msvc and env['toolchain'] != 'winddk':
264 cppdefines += [
265 'VC_EXTRALEAN',
266 '_USE_MATH_DEFINES',
267 '_CRT_SECURE_NO_WARNINGS',
268 '_CRT_SECURE_NO_DEPRECATE',
269 '_SCL_SECURE_NO_WARNINGS',
270 '_SCL_SECURE_NO_DEPRECATE',
271 ]
272 if debug:
273 cppdefines += ['_DEBUG']
274 if env['toolchain'] == 'winddk':
275 # Mimic WINDDK's builtin flags. See also:
276 # - WINDDK's bin/makefile.new i386mk.inc for more info.
277 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
278 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
279 if machine == 'x86':
280 cppdefines += ['_X86_', 'i386']
281 if machine == 'x86_64':
282 cppdefines += ['_AMD64_', 'AMD64']
283 if platform == 'winddk':
284 cppdefines += [
285 'STD_CALL',
286 ('CONDITION_HANDLING', '1'),
287 ('NT_INST', '0'),
288 ('WIN32', '100'),
289 ('_NT1X_', '100'),
290 ('WINNT', '1'),
291 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
292 ('WINVER', '0x0501'),
293 ('_WIN32_IE', '0x0603'),
294 ('WIN32_LEAN_AND_MEAN', '1'),
295 ('DEVL', '1'),
296 ('__BUILDMACHINE__', 'WinDDK'),
297 ('FPO', '0'),
298 ]
299 if debug:
300 cppdefines += [('DBG', 1)]
301 if platform == 'wince':
302 cppdefines += [
303 '_CRT_SECURE_NO_DEPRECATE',
304 '_USE_32BIT_TIME_T',
305 'UNICODE',
306 '_UNICODE',
307 ('UNDER_CE', '600'),
308 ('_WIN32_WCE', '0x600'),
309 'WINCEOEM',
310 'WINCEINTERNAL',
311 'WIN32',
312 'STRICT',
313 'x86',
314 '_X86_',
315 'INTERNATIONAL',
316 ('INTLMSG_CODEPAGE', '1252'),
317 ]
318 if platform == 'windows':
319 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
320 if platform == 'winddk':
321 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
322 if platform == 'wince':
323 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
324 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE_OGL']
325 env.Append(CPPDEFINES = cppdefines)
326
327 # C compiler options
328 cflags = [] # C
329 cxxflags = [] # C++
330 ccflags = [] # C & C++
331 if gcc:
332 if debug:
333 ccflags += ['-O0', '-g3']
334 elif env['CCVERSION'].startswith('4.2.'):
335 # gcc 4.2.x optimizer is broken
336 print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
337 ccflags += ['-O0', '-g3']
338 else:
339 ccflags += ['-O3', '-g3']
340 if env['profile']:
341 # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
342 ccflags += [
343 '-fno-omit-frame-pointer',
344 '-fno-optimize-sibling-calls',
345 ]
346 if env['machine'] == 'x86':
347 ccflags += [
348 '-m32',
349 #'-march=pentium4',
350 #'-mfpmath=sse',
351 ]
352 if platform != 'windows':
353 # XXX: -mstackrealign causes stack corruption on MinGW. Ditto
354 # for -mincoming-stack-boundary=2. Still enable it on other
355 # platforms for now, but we can't rely on it for cross platform
356 # code. We have to use __attribute__((force_align_arg_pointer))
357 # instead.
358 ccflags += [
359 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
360 '-mstackrealign', # ensure stack is aligned
361 ]
362 if env['machine'] == 'x86_64':
363 ccflags += ['-m64']
364 # See also:
365 # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
366 ccflags += [
367 '-Wall',
368 '-Wmissing-field-initializers',
369 '-Werror=pointer-arith',
370 '-Wno-long-long',
371 '-ffast-math',
372 '-fmessage-length=0', # be nice to Eclipse
373 ]
374 cflags += [
375 '-Werror=declaration-after-statement',
376 '-Wmissing-prototypes',
377 '-std=gnu99',
378 ]
379 if msvc:
380 # See also:
381 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
382 # - cl /?
383 if debug:
384 ccflags += [
385 '/Od', # disable optimizations
386 '/Oi', # enable intrinsic functions
387 '/Oy-', # disable frame pointer omission
388 '/GL-', # disable whole program optimization
389 ]
390 else:
391 ccflags += [
392 '/O2', # optimize for speed
393 '/GL', # enable whole program optimization
394 ]
395 ccflags += [
396 '/fp:fast', # fast floating point
397 '/W3', # warning level
398 #'/Wp64', # enable 64 bit porting warnings
399 ]
400 if env['machine'] == 'x86':
401 ccflags += [
402 #'/arch:SSE2', # use the SSE2 instructions
403 ]
404 if platform == 'windows':
405 ccflags += [
406 # TODO
407 ]
408 if platform == 'winddk':
409 ccflags += [
410 '/Zl', # omit default library name in .OBJ
411 '/Zp8', # 8bytes struct member alignment
412 '/Gy', # separate functions for linker
413 '/Gm-', # disable minimal rebuild
414 '/WX', # treat warnings as errors
415 '/Gz', # __stdcall Calling convention
416 '/GX-', # disable C++ EH
417 '/GR-', # disable C++ RTTI
418 '/GF', # enable read-only string pooling
419 '/G6', # optimize for PPro, P-II, P-III
420 '/Ze', # enable extensions
421 '/Gi-', # disable incremental compilation
422 '/QIfdiv-', # disable Pentium FDIV fix
423 '/hotpatch', # prepares an image for hotpatching.
424 #'/Z7', #enable old-style debug info
425 ]
426 if platform == 'wince':
427 # See also C:\WINCE600\public\common\oak\misc\makefile.def
428 ccflags += [
429 '/Zl', # omit default library name in .OBJ
430 '/GF', # enable read-only string pooling
431 '/GR-', # disable C++ RTTI
432 '/GS', # enable security checks
433 # Allow disabling language conformance to maintain backward compat
434 #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
435 #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
436 #'/wd4867',
437 #'/wd4430',
438 #'/MT',
439 #'/U_MT',
440 ]
441 # Automatic pdb generation
442 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
443 env.EnsureSConsVersion(0, 98, 0)
444 env['PDB'] = '${TARGET.base}.pdb'
445 env.Append(CCFLAGS = ccflags)
446 env.Append(CFLAGS = cflags)
447 env.Append(CXXFLAGS = cxxflags)
448
449 if env['platform'] == 'windows' and msvc:
450 # Choose the appropriate MSVC CRT
451 # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
452 if env['debug']:
453 env.Append(CCFLAGS = ['/MTd'])
454 env.Append(SHCCFLAGS = ['/LDd'])
455 else:
456 env.Append(CCFLAGS = ['/MT'])
457 env.Append(SHCCFLAGS = ['/LD'])
458
459 # Assembler options
460 if gcc:
461 if env['machine'] == 'x86':
462 env.Append(ASFLAGS = ['-m32'])
463 if env['machine'] == 'x86_64':
464 env.Append(ASFLAGS = ['-m64'])
465
466 # Linker options
467 linkflags = []
468 shlinkflags = []
469 if gcc:
470 if env['machine'] == 'x86':
471 linkflags += ['-m32']
472 if env['machine'] == 'x86_64':
473 linkflags += ['-m64']
474 shlinkflags += [
475 '-Wl,-Bsymbolic',
476 ]
477 # Handle circular dependencies in the libraries
478 env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
479 if msvc:
480 if not env['debug']:
481 # enable Link-time Code Generation
482 linkflags += ['/LTCG']
483 env.Append(ARFLAGS = ['/LTCG'])
484 if platform == 'windows' and msvc:
485 # See also:
486 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
487 linkflags += [
488 '/fixed:no',
489 '/incremental:no',
490 ]
491 if platform == 'winddk':
492 linkflags += [
493 '/merge:_PAGE=PAGE',
494 '/merge:_TEXT=.text',
495 '/section:INIT,d',
496 '/opt:ref',
497 '/opt:icf',
498 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
499 '/incremental:no',
500 '/fullbuild',
501 '/release',
502 '/nodefaultlib',
503 '/wx',
504 '/debug',
505 '/debugtype:cv',
506 '/version:5.1',
507 '/osversion:5.1',
508 '/functionpadmin:5',
509 '/safeseh',
510 '/pdbcompress',
511 '/stack:0x40000,0x1000',
512 '/driver',
513 '/align:0x80',
514 '/subsystem:native,5.01',
515 '/base:0x10000',
516
517 '/entry:DrvEnableDriver',
518 ]
519 if env['debug'] or env['profile']:
520 linkflags += [
521 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
522 ]
523 if platform == 'wince':
524 linkflags += [
525 '/nodefaultlib',
526 #'/incremental:no',
527 #'/fullbuild',
528 '/entry:_DllMainCRTStartup',
529 ]
530 env.Append(LINKFLAGS = linkflags)
531 env.Append(SHLINKFLAGS = shlinkflags)
532
533 # Default libs
534 env.Append(LIBS = [])
535
536 # Custom builders and methods
537 createConvenienceLibBuilder(env)
538 createCodeGenerateMethod(env)
539 createInstallMethods(env)
540
541 # for debugging
542 #print env.Dump()
543
544
545 def exists(env):
546 return 1