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