only use -Werror flags with gcc 4.2.x and 4.3.x.
[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 symlink(target, source, env):
43 target = str(target[0])
44 source = str(source[0])
45 if os.path.islink(target) or os.path.exists(target):
46 os.remove(target)
47 os.symlink(os.path.basename(source), target)
48
49 def install(env, source, subdir):
50 target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build'], subdir)
51 env.Install(target_dir, source)
52
53 def install_program(env, source):
54 install(env, source, 'bin')
55
56 def install_shared_library(env, sources, version = ()):
57 install_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build'])
58 version = tuple(map(str, version))
59 if env['SHLIBSUFFIX'] == '.dll':
60 dlls = env.FindIxes(sources, 'SHLIBPREFIX', 'SHLIBSUFFIX')
61 install(env, dlls, 'bin')
62 libs = env.FindIxes(sources, 'LIBPREFIX', 'LIBSUFFIX')
63 install(env, libs, 'lib')
64 else:
65 for source in sources:
66 target_dir = os.path.join(install_dir, 'lib')
67 target_name = '.'.join((str(source),) + version)
68 last = env.InstallAs(os.path.join(target_dir, target_name), source)
69 while len(version):
70 version = version[:-1]
71 target_name = '.'.join((str(source),) + version)
72 action = SCons.Action.Action(symlink, "$TARGET -> $SOURCE")
73 last = env.Command(os.path.join(target_dir, target_name), last, action)
74
75 def createInstallMethods(env):
76 env.AddMethod(install_program, 'InstallProgram')
77 env.AddMethod(install_shared_library, 'InstallSharedLibrary')
78
79
80 def num_jobs():
81 try:
82 return int(os.environ['NUMBER_OF_PROCESSORS'])
83 except (ValueError, KeyError):
84 pass
85
86 try:
87 return os.sysconf('SC_NPROCESSORS_ONLN')
88 except (ValueError, OSError, AttributeError):
89 pass
90
91 try:
92 return int(os.popen2("sysctl -n hw.ncpu")[1].read())
93 except ValueError:
94 pass
95
96 return 1
97
98
99 def generate(env):
100 """Common environment generation code"""
101
102 # Toolchain
103 platform = env['platform']
104 if env['toolchain'] == 'default':
105 if platform == 'winddk':
106 env['toolchain'] = 'winddk'
107 elif platform == 'wince':
108 env['toolchain'] = 'wcesdk'
109 env.Tool(env['toolchain'])
110
111 env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
112 env['msvc'] = env['CC'] == 'cl'
113
114 # shortcuts
115 debug = env['debug']
116 machine = env['machine']
117 platform = env['platform']
118 x86 = env['machine'] == 'x86'
119 ppc = env['machine'] == 'ppc'
120 gcc = env['gcc']
121 msvc = env['msvc']
122
123 # Put build output in a separate dir, which depends on the current
124 # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
125 build_topdir = 'build'
126 build_subdir = env['platform']
127 if env['llvm']:
128 build_subdir += "-llvm"
129 if env['machine'] != 'generic':
130 build_subdir += '-' + env['machine']
131 if env['debug']:
132 build_subdir += "-debug"
133 if env['profile']:
134 build_subdir += "-profile"
135 build_dir = os.path.join(build_topdir, build_subdir)
136 # Place the .sconsign file in the build dir too, to avoid issues with
137 # different scons versions building the same source file
138 env['build'] = build_dir
139 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
140 env.CacheDir('build/cache')
141 env['CONFIGUREDIR'] = os.path.join(build_dir, 'conf')
142 env['CONFIGURELOG'] = os.path.join(os.path.abspath(build_dir), 'config.log')
143
144 # Parallel build
145 if env.GetOption('num_jobs') <= 1:
146 env.SetOption('num_jobs', num_jobs())
147
148 # C preprocessor options
149 cppdefines = []
150 if debug:
151 cppdefines += ['DEBUG']
152 else:
153 cppdefines += ['NDEBUG']
154 if env['profile']:
155 cppdefines += ['PROFILE']
156 if platform == 'windows':
157 cppdefines += [
158 'WIN32',
159 '_WINDOWS',
160 #'_UNICODE',
161 #'UNICODE',
162 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
163 ('WINVER', '0x0501'),
164 ]
165 if msvc and env['toolchain'] != 'winddk':
166 cppdefines += [
167 'VC_EXTRALEAN',
168 '_USE_MATH_DEFINES',
169 '_CRT_SECURE_NO_WARNINGS',
170 '_CRT_SECURE_NO_DEPRECATE',
171 '_SCL_SECURE_NO_WARNINGS',
172 '_SCL_SECURE_NO_DEPRECATE',
173 ]
174 if debug:
175 cppdefines += ['_DEBUG']
176 if env['toolchain'] == 'winddk':
177 # Mimic WINDDK's builtin flags. See also:
178 # - WINDDK's bin/makefile.new i386mk.inc for more info.
179 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
180 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
181 if machine == 'x86':
182 cppdefines += ['_X86_', 'i386']
183 if machine == 'x86_64':
184 cppdefines += ['_AMD64_', 'AMD64']
185 if platform == 'winddk':
186 cppdefines += [
187 'STD_CALL',
188 ('CONDITION_HANDLING', '1'),
189 ('NT_INST', '0'),
190 ('WIN32', '100'),
191 ('_NT1X_', '100'),
192 ('WINNT', '1'),
193 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
194 ('WINVER', '0x0501'),
195 ('_WIN32_IE', '0x0603'),
196 ('WIN32_LEAN_AND_MEAN', '1'),
197 ('DEVL', '1'),
198 ('__BUILDMACHINE__', 'WinDDK'),
199 ('FPO', '0'),
200 ]
201 if debug:
202 cppdefines += [('DBG', 1)]
203 if platform == 'wince':
204 cppdefines += [
205 '_CRT_SECURE_NO_DEPRECATE',
206 '_USE_32BIT_TIME_T',
207 'UNICODE',
208 '_UNICODE',
209 ('UNDER_CE', '600'),
210 ('_WIN32_WCE', '0x600'),
211 'WINCEOEM',
212 'WINCEINTERNAL',
213 'WIN32',
214 'STRICT',
215 'x86',
216 '_X86_',
217 'INTERNATIONAL',
218 ('INTLMSG_CODEPAGE', '1252'),
219 ]
220 if platform == 'windows':
221 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
222 if platform == 'winddk':
223 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
224 if platform == 'wince':
225 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
226 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE_OGL']
227 env.Append(CPPDEFINES = cppdefines)
228
229 # C compiler options
230 cflags = [] # C
231 cxxflags = [] # C++
232 ccflags = [] # C & C++
233 if gcc:
234 if debug:
235 ccflags += ['-O0', '-g3']
236 elif env['CCVERSION'].startswith('4.2.'):
237 # gcc 4.2.x optimizer is broken
238 print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
239 ccflags += ['-O0', '-g3']
240 else:
241 ccflags += ['-O3', '-g3']
242 if env['profile']:
243 # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
244 ccflags += [
245 '-fno-omit-frame-pointer',
246 '-fno-optimize-sibling-calls',
247 ]
248 if env['machine'] == 'x86':
249 ccflags += [
250 '-m32',
251 #'-march=pentium4',
252 #'-mfpmath=sse',
253 ]
254 if platform != 'windows':
255 # XXX: -mstackrealign causes stack corruption on MinGW. Ditto
256 # for -mincoming-stack-boundary=2. Still enable it on other
257 # platforms for now, but we can't rely on it for cross platform
258 # code. We have to use __attribute__((force_align_arg_pointer))
259 # instead.
260 ccflags += [
261 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
262 '-mstackrealign', # ensure stack is aligned
263 ]
264 if env['machine'] == 'x86_64':
265 ccflags += ['-m64']
266 # See also:
267 # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
268 ccflags += [
269 '-Wall',
270 '-Wmissing-field-initializers',
271 '-Wno-long-long',
272 '-ffast-math',
273 '-fmessage-length=0', # be nice to Eclipse
274 ]
275 cflags += [
276 '-Wmissing-prototypes',
277 '-std=gnu99',
278 ]
279 if env['CCVERSION'].startswith('4.2.') or
280 env['CCVERSION'].startswith('4.3.'):
281 ccflags += [
282 '-Werror=pointer-arith',
283 ]
284 cflags += [
285 '-Werror=declaration-after-statement',
286 ]
287 if msvc:
288 # See also:
289 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
290 # - cl /?
291 if debug:
292 ccflags += [
293 '/Od', # disable optimizations
294 '/Oi', # enable intrinsic functions
295 '/Oy-', # disable frame pointer omission
296 '/GL-', # disable whole program optimization
297 ]
298 else:
299 ccflags += [
300 '/O2', # optimize for speed
301 '/GL', # enable whole program optimization
302 ]
303 ccflags += [
304 '/fp:fast', # fast floating point
305 '/W3', # warning level
306 #'/Wp64', # enable 64 bit porting warnings
307 ]
308 if env['machine'] == 'x86':
309 ccflags += [
310 #'/arch:SSE2', # use the SSE2 instructions
311 ]
312 if platform == 'windows':
313 ccflags += [
314 # TODO
315 ]
316 if platform == 'winddk':
317 ccflags += [
318 '/Zl', # omit default library name in .OBJ
319 '/Zp8', # 8bytes struct member alignment
320 '/Gy', # separate functions for linker
321 '/Gm-', # disable minimal rebuild
322 '/WX', # treat warnings as errors
323 '/Gz', # __stdcall Calling convention
324 '/GX-', # disable C++ EH
325 '/GR-', # disable C++ RTTI
326 '/GF', # enable read-only string pooling
327 '/G6', # optimize for PPro, P-II, P-III
328 '/Ze', # enable extensions
329 '/Gi-', # disable incremental compilation
330 '/QIfdiv-', # disable Pentium FDIV fix
331 '/hotpatch', # prepares an image for hotpatching.
332 #'/Z7', #enable old-style debug info
333 ]
334 if platform == 'wince':
335 # See also C:\WINCE600\public\common\oak\misc\makefile.def
336 ccflags += [
337 '/Zl', # omit default library name in .OBJ
338 '/GF', # enable read-only string pooling
339 '/GR-', # disable C++ RTTI
340 '/GS', # enable security checks
341 # Allow disabling language conformance to maintain backward compat
342 #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
343 #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
344 #'/wd4867',
345 #'/wd4430',
346 #'/MT',
347 #'/U_MT',
348 ]
349 # Automatic pdb generation
350 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
351 env.EnsureSConsVersion(0, 98, 0)
352 env['PDB'] = '${TARGET.base}.pdb'
353 env.Append(CCFLAGS = ccflags)
354 env.Append(CFLAGS = cflags)
355 env.Append(CXXFLAGS = cxxflags)
356
357 if env['platform'] == 'windows' and msvc:
358 # Choose the appropriate MSVC CRT
359 # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
360 if env['debug']:
361 env.Append(CCFLAGS = ['/MTd'])
362 env.Append(SHCCFLAGS = ['/LDd'])
363 else:
364 env.Append(CCFLAGS = ['/MT'])
365 env.Append(SHCCFLAGS = ['/LD'])
366
367 # Assembler options
368 if gcc:
369 if env['machine'] == 'x86':
370 env.Append(ASFLAGS = ['-m32'])
371 if env['machine'] == 'x86_64':
372 env.Append(ASFLAGS = ['-m64'])
373
374 # Linker options
375 linkflags = []
376 shlinkflags = []
377 if gcc:
378 if env['machine'] == 'x86':
379 linkflags += ['-m32']
380 if env['machine'] == 'x86_64':
381 linkflags += ['-m64']
382 shlinkflags += [
383 '-Wl,-Bsymbolic',
384 ]
385 # Handle circular dependencies in the libraries
386 if env['platform'] in ('darwin'):
387 pass
388 else:
389 env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
390 if msvc:
391 if not env['debug']:
392 # enable Link-time Code Generation
393 linkflags += ['/LTCG']
394 env.Append(ARFLAGS = ['/LTCG'])
395 if platform == 'windows' and msvc:
396 # See also:
397 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
398 linkflags += [
399 '/fixed:no',
400 '/incremental:no',
401 ]
402 if platform == 'winddk':
403 linkflags += [
404 '/merge:_PAGE=PAGE',
405 '/merge:_TEXT=.text',
406 '/section:INIT,d',
407 '/opt:ref',
408 '/opt:icf',
409 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
410 '/incremental:no',
411 '/fullbuild',
412 '/release',
413 '/nodefaultlib',
414 '/wx',
415 '/debug',
416 '/debugtype:cv',
417 '/version:5.1',
418 '/osversion:5.1',
419 '/functionpadmin:5',
420 '/safeseh',
421 '/pdbcompress',
422 '/stack:0x40000,0x1000',
423 '/driver',
424 '/align:0x80',
425 '/subsystem:native,5.01',
426 '/base:0x10000',
427
428 '/entry:DrvEnableDriver',
429 ]
430 if env['debug'] or env['profile']:
431 linkflags += [
432 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
433 ]
434 if platform == 'wince':
435 linkflags += [
436 '/nodefaultlib',
437 #'/incremental:no',
438 #'/fullbuild',
439 '/entry:_DllMainCRTStartup',
440 ]
441 env.Append(LINKFLAGS = linkflags)
442 env.Append(SHLINKFLAGS = shlinkflags)
443
444 # Default libs
445 env.Append(LIBS = [])
446
447 # Custom builders and methods
448 env.Tool('custom')
449 createInstallMethods(env)
450
451 # for debugging
452 #print env.Dump()
453
454
455 def exists(env):
456 return 1