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