blob: 342a0879c30b9c57e1a937df08875da8827e983a [file] [log] [blame]
José Fonsecab04aa712008-06-06 14:48:57 +09001"""gallium
2
3Frontend-tool for Gallium3D architecture.
4
5"""
6
José Fonseca381e3482008-07-17 11:23:43 +09007#
José Fonsecab04aa712008-06-06 14:48:57 +09008# Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
9# All Rights Reserved.
José Fonseca381e3482008-07-17 11:23:43 +090010#
José Fonsecab04aa712008-06-06 14:48:57 +090011# 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:
José Fonseca381e3482008-07-17 11:23:43 +090018#
José Fonsecab04aa712008-06-06 14:48:57 +090019# 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.
José Fonseca381e3482008-07-17 11:23:43 +090022#
José Fonsecab04aa712008-06-06 14:48:57 +090023# 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.
José Fonseca381e3482008-07-17 11:23:43 +090030#
José Fonsecab04aa712008-06-06 14:48:57 +090031
32
José Fonseca27d8d6f2008-07-03 12:42:23 +090033import os
José Fonsecab04aa712008-06-06 14:48:57 +090034import os.path
José Fonseca27d8d6f2008-07-03 12:42:23 +090035import re
José Fonsecab04aa712008-06-06 14:48:57 +090036
37import SCons.Action
38import SCons.Builder
José Fonseca27d8d6f2008-07-03 12:42:23 +090039import SCons.Scanner
José Fonsecab04aa712008-06-06 14:48:57 +090040
41
42def quietCommandLines(env):
José Fonseca381e3482008-07-17 11:23:43 +090043 # 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 ..."
José Fonsecab04aa712008-06-06 14:48:57 +090050
51
52def 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.
José Fonseca381e3482008-07-17 11:23:43 +090057
José Fonsecab04aa712008-06-06 14:48:57 +090058 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
José Fonsecab04aa712008-06-06 14:48:57 +090076
77 return convenience_lib
78
79
José Fonseca27d8d6f2008-07-03 12:42:23 +090080# TODO: handle import statements with multiple modules
81# TODO: handle from import statements
82import_re = re.compile(r'^import\s+(\S+)$', re.M)
83
84def python_scan(node, env, path):
José Fonseca381e3482008-07-17 11:23:43 +090085 # 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
José Fonseca27d8d6f2008-07-03 12:42:23 +0900101
102python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
103
104
105def code_generate(env, script, target, source, command):
José Fonseca381e3482008-07-17 11:23:43 +0900106 """Method to simplify code generation via python scripts.
José Fonseca27d8d6f2008-07-03 12:42:23 +0900107
José Fonseca381e3482008-07-17 11:23:43 +0900108 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
José Fonseca27d8d6f2008-07-03 12:42:23 +0900136
137
138def createCodeGenerateMethod(env):
José Fonseca381e3482008-07-17 11:23:43 +0900139 env.Append(SCANNERS = python_scanner)
140 env.AddMethod(code_generate, 'CodeGenerate')
José Fonseca27d8d6f2008-07-03 12:42:23 +0900141
142
José Fonsecab04aa712008-06-06 14:48:57 +0900143def generate(env):
José Fonseca381e3482008-07-17 11:23:43 +0900144 """Common environment generation code"""
José Fonsecab04aa712008-06-06 14:48:57 +0900145
José Fonseca381e3482008-07-17 11:23:43 +0900146 # FIXME: this is already too late
147 #if env.get('quiet', False):
148 # quietCommandLines(env)
José Fonsecab04aa712008-06-06 14:48:57 +0900149
José Fonseca381e3482008-07-17 11:23:43 +0900150 # shortcuts
151 debug = env['debug']
152 machine = env['machine']
153 platform = env['platform']
154 x86 = env['machine'] == 'x86'
155 gcc = env['platform'] in ('linux', 'freebsd', 'darwin')
156 msvc = env['platform'] in ('windows', 'winddk', 'wince')
José Fonsecab04aa712008-06-06 14:48:57 +0900157
José Fonseca381e3482008-07-17 11:23:43 +0900158 # Tool
159 if platform == 'winddk':
160 env.Tool('winddk')
161 elif platform == 'wince':
162 env.Tool('wcesdk')
163 else:
164 env.Tool('default')
José Fonsecab04aa712008-06-06 14:48:57 +0900165
José Fonseca381e3482008-07-17 11:23:43 +0900166 # Put build output in a separate dir, which depends on the current
167 # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
168 build_topdir = 'build'
169 build_subdir = env['platform']
170 if env['dri']:
171 build_subdir += "-dri"
172 if env['llvm']:
173 build_subdir += "-llvm"
174 if env['machine'] != 'generic':
175 build_subdir += '-' + env['machine']
176 if env['debug']:
177 build_subdir += "-debug"
178 if env['profile']:
179 build_subdir += "-profile"
180 build_dir = os.path.join(build_topdir, build_subdir)
181 # Place the .sconsign file in the build dir too, to avoid issues with
182 # different scons versions building the same source file
183 env['build'] = build_dir
184 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
José Fonsecab04aa712008-06-06 14:48:57 +0900185
José Fonseca381e3482008-07-17 11:23:43 +0900186 # C preprocessor options
187 cppdefines = []
188 if debug:
189 cppdefines += ['DEBUG']
190 else:
191 cppdefines += ['NDEBUG']
192 if env['profile']:
193 cppdefines += ['PROFILE']
194 if platform == 'windows':
195 cppdefines += [
196 'WIN32',
197 '_WINDOWS',
198 '_UNICODE',
199 'UNICODE',
200 # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
201 'WIN32_LEAN_AND_MEAN',
202 'VC_EXTRALEAN',
203 '_CRT_SECURE_NO_DEPRECATE',
204 ]
205 if debug:
206 cppdefines += ['_DEBUG']
207 if platform == 'winddk':
208 # Mimic WINDDK's builtin flags. See also:
209 # - WINDDK's bin/makefile.new i386mk.inc for more info.
210 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
211 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
212 cppdefines += [
213 ('_X86_', '1'),
214 ('i386', '1'),
215 'STD_CALL',
216 ('CONDITION_HANDLING', '1'),
217 ('NT_INST', '0'),
218 ('WIN32', '100'),
219 ('_NT1X_', '100'),
220 ('WINNT', '1'),
221 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
222 ('WINVER', '0x0501'),
223 ('_WIN32_IE', '0x0603'),
224 ('WIN32_LEAN_AND_MEAN', '1'),
225 ('DEVL', '1'),
226 ('__BUILDMACHINE__', 'WinDDK'),
227 ('FPO', '0'),
228 ]
229 if debug:
230 cppdefines += [('DBG', 1)]
231 if platform == 'wince':
232 cppdefines += [
233 '_CRT_SECURE_NO_DEPRECATE',
234 '_USE_32BIT_TIME_T',
235 'UNICODE',
236 '_UNICODE',
237 ('UNDER_CE', '600'),
238 ('_WIN32_WCE', '0x600'),
239 'WINCEOEM',
240 'WINCEINTERNAL',
241 'WIN32',
242 'STRICT',
243 'x86',
244 '_X86_',
245 'INTERNATIONAL',
246 ('INTLMSG_CODEPAGE', '1252'),
247 ]
248 if platform == 'windows':
249 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
250 if platform == 'winddk':
251 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
252 if platform == 'wince':
253 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
254 env.Append(CPPDEFINES = cppdefines)
José Fonsecab04aa712008-06-06 14:48:57 +0900255
José Fonseca381e3482008-07-17 11:23:43 +0900256 # C preprocessor includes
257 if platform == 'winddk':
258 env.Append(CPPPATH = [
259 env['SDK_INC_PATH'],
260 env['DDK_INC_PATH'],
261 env['WDM_INC_PATH'],
262 env['CRT_INC_PATH'],
263 ])
José Fonseca05cfb4c2008-06-27 13:41:23 +0900264
José Fonseca381e3482008-07-17 11:23:43 +0900265 # C compiler options
266 cflags = []
267 if gcc:
268 if debug:
269 cflags += ['-O0', '-g3']
270 else:
271 cflags += ['-O3', '-g3']
272 if env['profile']:
273 cflags += ['-pg']
274 if env['machine'] == 'x86':
275 cflags += [
276 '-m32',
277 #'-march=pentium4',
278 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
279 #'-mfpmath=sse',
280 ]
281 if env['machine'] == 'x86_64':
282 cflags += ['-m64']
283 cflags += [
284 '-Wall',
285 '-Wmissing-prototypes',
286 '-Wno-long-long',
287 '-ffast-math',
288 '-pedantic',
289 '-fmessage-length=0', # be nice to Eclipse
290 ]
291 if msvc:
292 # See also:
José Fonsecaa6c72582008-09-01 09:47:40 +0900293 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
José Fonseca381e3482008-07-17 11:23:43 +0900294 # - cl /?
295 if debug:
296 cflags += [
297 '/Od', # disable optimizations
298 '/Oi', # enable intrinsic functions
299 '/Oy-', # disable frame pointer omission
300 ]
301 else:
302 cflags += [
303 '/Ox', # maximum optimizations
304 '/Oi', # enable intrinsic functions
José Fonsecaa6c72582008-09-01 09:47:40 +0900305 '/Ot', # favor code speed
306 #'/fp:fast', # fast floating point
José Fonseca381e3482008-07-17 11:23:43 +0900307 ]
308 if env['profile']:
309 cflags += [
310 '/Gh', # enable _penter hook function
311 '/GH', # enable _pexit hook function
312 ]
313 cflags += [
314 '/W3', # warning level
315 #'/Wp64', # enable 64 bit porting warnings
316 ]
José Fonsecaa6c72582008-09-01 09:47:40 +0900317 if env['machine'] == 'x86':
318 cflags += [
319 #'/QIfist', # Suppress _ftol
320 #'/arch:SSE2', # use the SSE2 instructions
321 ]
José Fonseca381e3482008-07-17 11:23:43 +0900322 if platform == 'windows':
323 cflags += [
324 # TODO
325 ]
326 if platform == 'winddk':
327 cflags += [
328 '/Zl', # omit default library name in .OBJ
329 '/Zp8', # 8bytes struct member alignment
330 '/Gy', # separate functions for linker
331 '/Gm-', # disable minimal rebuild
332 '/WX', # treat warnings as errors
333 '/Gz', # __stdcall Calling convention
334 '/GX-', # disable C++ EH
335 '/GR-', # disable C++ RTTI
336 '/GF', # enable read-only string pooling
337 '/G6', # optimize for PPro, P-II, P-III
338 '/Ze', # enable extensions
339 '/Gi-', # disable incremental compilation
340 '/QIfdiv-', # disable Pentium FDIV fix
341 '/hotpatch', # prepares an image for hotpatching.
342 #'/Z7', #enable old-style debug info
343 ]
344 if platform == 'wince':
345 # See also C:\WINCE600\public\common\oak\misc\makefile.def
346 cflags += [
347 '/Zl', # omit default library name in .OBJ
348 '/GF', # enable read-only string pooling
349 '/GR-', # disable C++ RTTI
350 '/GS', # enable security checks
351 # Allow disabling language conformance to maintain backward compat
352 #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
353 #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
354 #'/wd4867',
355 #'/wd4430',
356 #'/MT',
357 #'/U_MT',
358 ]
359 # Automatic pdb generation
360 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
361 env.EnsureSConsVersion(0, 98, 0)
362 env['PDB'] = '${TARGET.base}.pdb'
363 env.Append(CFLAGS = cflags)
364 env.Append(CXXFLAGS = cflags)
José Fonsecab04aa712008-06-06 14:48:57 +0900365
José Fonseca381e3482008-07-17 11:23:43 +0900366 # Assembler options
367 if gcc:
368 if env['machine'] == 'x86':
369 env.Append(ASFLAGS = ['-m32'])
370 if env['machine'] == 'x86_64':
371 env.Append(ASFLAGS = ['-m64'])
José Fonseca27d8d6f2008-07-03 12:42:23 +0900372
José Fonseca381e3482008-07-17 11:23:43 +0900373 # Linker options
374 linkflags = []
375 if gcc:
376 if env['machine'] == 'x86':
377 linkflags += ['-m32']
378 if env['machine'] == 'x86_64':
379 linkflags += ['-m64']
380 if platform == 'winddk':
381 # See also:
382 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
383 linkflags += [
384 '/merge:_PAGE=PAGE',
385 '/merge:_TEXT=.text',
386 '/section:INIT,d',
387 '/opt:ref',
388 '/opt:icf',
389 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
390 '/incremental:no',
391 '/fullbuild',
392 '/release',
393 '/nodefaultlib',
394 '/wx',
395 '/debug',
396 '/debugtype:cv',
397 '/version:5.1',
398 '/osversion:5.1',
399 '/functionpadmin:5',
400 '/safeseh',
401 '/pdbcompress',
402 '/stack:0x40000,0x1000',
403 '/driver',
404 '/align:0x80',
405 '/subsystem:native,5.01',
406 '/base:0x10000',
407
408 '/entry:DrvEnableDriver',
409 ]
410 if env['profile']:
411 linkflags += [
412 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
413 ]
414 if platform == 'wince':
415 linkflags += [
416 '/nodefaultlib',
417 #'/incremental:no',
418 #'/fullbuild',
419 '/entry:_DllMainCRTStartup',
420 ]
421 env.Append(LINKFLAGS = linkflags)
422
José Fonsecac76787a2008-07-17 11:25:20 +0900423 # Default libs
424 env.Append(LIBS = [])
425
José Fonseca381e3482008-07-17 11:23:43 +0900426 # Custom builders and methods
427 createConvenienceLibBuilder(env)
428 createCodeGenerateMethod(env)
429
430 # for debugging
431 #print env.Dump()
José Fonsecab04aa712008-06-06 14:48:57 +0900432
433
434def exists(env):
José Fonseca381e3482008-07-17 11:23:43 +0900435 return 1