blob: 6dbce62b8ed27a8134760abfab93b120743475bc [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é Fonseca52c2dd12008-09-08 07:54:15 +0900143def 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
150def install_shared_library(env, source, version = ()):
151 source = str(source[0])
152 version = tuple(map(str, version))
José Fonseca7cfc2942008-09-08 21:50:50 +0900153 target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build'], 'lib')
José Fonseca52c2dd12008-09-08 07:54:15 +0900154 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")
José Fonseca52c2dd12008-09-08 07:54:15 +0900160 last = env.Command(os.path.join(target_dir, target_name), last, action)
161
162def createInstallMethods(env):
163 env.AddMethod(install_shared_library, 'InstallSharedLibrary')
164
165
José Fonsecab04aa712008-06-06 14:48:57 +0900166def generate(env):
José Fonseca381e3482008-07-17 11:23:43 +0900167 """Common environment generation code"""
José Fonsecab04aa712008-06-06 14:48:57 +0900168
José Fonseca381e3482008-07-17 11:23:43 +0900169 # FIXME: this is already too late
170 #if env.get('quiet', False):
171 # quietCommandLines(env)
José Fonsecab04aa712008-06-06 14:48:57 +0900172
José Fonseca6cf59e12008-11-18 19:13:32 +0900173 # Toolchain
174 platform = env['platform']
175 if env['toolchain'] == 'default':
176 if platform == 'winddk':
Michal Krol4f3dcf32008-11-19 20:31:38 +0100177 env['toolchain'] = 'winddk'
José Fonseca6cf59e12008-11-18 19:13:32 +0900178 elif platform == 'wince':
Michal Krol4f3dcf32008-11-19 20:31:38 +0100179 env['toolchain'] = 'wcesdk'
José Fonseca6cf59e12008-11-18 19:13:32 +0900180 env.Tool(env['toolchain'])
181
José Fonseca381e3482008-07-17 11:23:43 +0900182 # shortcuts
183 debug = env['debug']
184 machine = env['machine']
185 platform = env['platform']
186 x86 = env['machine'] == 'x86'
Michel Dänzer6b69e3c2008-10-23 10:28:48 +0200187 ppc = env['machine'] == 'ppc'
José Fonseca6cf59e12008-11-18 19:13:32 +0900188 gcc = env['platform'] in ('linux', 'freebsd', 'darwin') or env['toolchain'] == 'crossmingw'
189 msvc = env['platform'] in ('windows', 'winddk', 'wince') and env['toolchain'] != 'crossmingw'
José Fonsecab04aa712008-06-06 14:48:57 +0900190
José Fonseca381e3482008-07-17 11:23:43 +0900191 # 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['dri']:
196 build_subdir += "-dri"
197 if env['llvm']:
198 build_subdir += "-llvm"
199 if env['machine'] != 'generic':
200 build_subdir += '-' + env['machine']
201 if env['debug']:
202 build_subdir += "-debug"
203 if env['profile']:
204 build_subdir += "-profile"
205 build_dir = os.path.join(build_topdir, build_subdir)
206 # Place the .sconsign file in the build dir too, to avoid issues with
207 # different scons versions building the same source file
208 env['build'] = build_dir
209 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
José Fonsecab04aa712008-06-06 14:48:57 +0900210
José Fonseca381e3482008-07-17 11:23:43 +0900211 # C preprocessor options
212 cppdefines = []
213 if debug:
214 cppdefines += ['DEBUG']
215 else:
216 cppdefines += ['NDEBUG']
217 if env['profile']:
218 cppdefines += ['PROFILE']
219 if platform == 'windows':
220 cppdefines += [
221 'WIN32',
222 '_WINDOWS',
223 '_UNICODE',
224 'UNICODE',
José Fonseca129c6ed2008-12-01 11:53:26 -0800225 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
226 ('WINVER', '0x0501'),
José Fonseca381e3482008-07-17 11:23:43 +0900227 # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
228 'WIN32_LEAN_AND_MEAN',
229 'VC_EXTRALEAN',
230 '_CRT_SECURE_NO_DEPRECATE',
231 ]
232 if debug:
233 cppdefines += ['_DEBUG']
234 if platform == 'winddk':
235 # Mimic WINDDK's builtin flags. See also:
236 # - WINDDK's bin/makefile.new i386mk.inc for more info.
237 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
238 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
239 cppdefines += [
240 ('_X86_', '1'),
241 ('i386', '1'),
242 'STD_CALL',
243 ('CONDITION_HANDLING', '1'),
244 ('NT_INST', '0'),
245 ('WIN32', '100'),
246 ('_NT1X_', '100'),
247 ('WINNT', '1'),
248 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
249 ('WINVER', '0x0501'),
250 ('_WIN32_IE', '0x0603'),
251 ('WIN32_LEAN_AND_MEAN', '1'),
252 ('DEVL', '1'),
253 ('__BUILDMACHINE__', 'WinDDK'),
254 ('FPO', '0'),
255 ]
256 if debug:
257 cppdefines += [('DBG', 1)]
258 if platform == 'wince':
259 cppdefines += [
260 '_CRT_SECURE_NO_DEPRECATE',
261 '_USE_32BIT_TIME_T',
262 'UNICODE',
263 '_UNICODE',
264 ('UNDER_CE', '600'),
265 ('_WIN32_WCE', '0x600'),
266 'WINCEOEM',
267 'WINCEINTERNAL',
268 'WIN32',
269 'STRICT',
270 'x86',
271 '_X86_',
272 'INTERNATIONAL',
273 ('INTLMSG_CODEPAGE', '1252'),
274 ]
275 if platform == 'windows':
276 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
277 if platform == 'winddk':
278 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
279 if platform == 'wince':
280 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
José Fonseca40b3bb02008-11-04 10:53:02 +0900281 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE_OGL']
José Fonseca381e3482008-07-17 11:23:43 +0900282 env.Append(CPPDEFINES = cppdefines)
José Fonsecab04aa712008-06-06 14:48:57 +0900283
José Fonseca381e3482008-07-17 11:23:43 +0900284 # C preprocessor includes
285 if platform == 'winddk':
286 env.Append(CPPPATH = [
287 env['SDK_INC_PATH'],
288 env['DDK_INC_PATH'],
289 env['WDM_INC_PATH'],
290 env['CRT_INC_PATH'],
291 ])
José Fonseca05cfb4c2008-06-27 13:41:23 +0900292
José Fonseca381e3482008-07-17 11:23:43 +0900293 # C compiler options
294 cflags = []
295 if gcc:
296 if debug:
297 cflags += ['-O0', '-g3']
298 else:
299 cflags += ['-O3', '-g3']
300 if env['profile']:
301 cflags += ['-pg']
302 if env['machine'] == 'x86':
303 cflags += [
304 '-m32',
305 #'-march=pentium4',
306 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
307 #'-mfpmath=sse',
308 ]
309 if env['machine'] == 'x86_64':
310 cflags += ['-m64']
311 cflags += [
312 '-Wall',
313 '-Wmissing-prototypes',
314 '-Wno-long-long',
315 '-ffast-math',
316 '-pedantic',
317 '-fmessage-length=0', # be nice to Eclipse
318 ]
319 if msvc:
320 # See also:
José Fonsecaa6c72582008-09-01 09:47:40 +0900321 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
José Fonseca381e3482008-07-17 11:23:43 +0900322 # - cl /?
323 if debug:
324 cflags += [
325 '/Od', # disable optimizations
326 '/Oi', # enable intrinsic functions
327 '/Oy-', # disable frame pointer omission
328 ]
329 else:
330 cflags += [
331 '/Ox', # maximum optimizations
332 '/Oi', # enable intrinsic functions
José Fonsecaa6c72582008-09-01 09:47:40 +0900333 '/Ot', # favor code speed
334 #'/fp:fast', # fast floating point
José Fonseca381e3482008-07-17 11:23:43 +0900335 ]
336 if env['profile']:
337 cflags += [
338 '/Gh', # enable _penter hook function
339 '/GH', # enable _pexit hook function
340 ]
341 cflags += [
342 '/W3', # warning level
343 #'/Wp64', # enable 64 bit porting warnings
344 ]
José Fonsecaa6c72582008-09-01 09:47:40 +0900345 if env['machine'] == 'x86':
346 cflags += [
347 #'/QIfist', # Suppress _ftol
348 #'/arch:SSE2', # use the SSE2 instructions
349 ]
José Fonseca381e3482008-07-17 11:23:43 +0900350 if platform == 'windows':
351 cflags += [
352 # TODO
353 ]
354 if platform == 'winddk':
355 cflags += [
356 '/Zl', # omit default library name in .OBJ
357 '/Zp8', # 8bytes struct member alignment
358 '/Gy', # separate functions for linker
359 '/Gm-', # disable minimal rebuild
360 '/WX', # treat warnings as errors
361 '/Gz', # __stdcall Calling convention
362 '/GX-', # disable C++ EH
363 '/GR-', # disable C++ RTTI
364 '/GF', # enable read-only string pooling
365 '/G6', # optimize for PPro, P-II, P-III
366 '/Ze', # enable extensions
367 '/Gi-', # disable incremental compilation
368 '/QIfdiv-', # disable Pentium FDIV fix
369 '/hotpatch', # prepares an image for hotpatching.
370 #'/Z7', #enable old-style debug info
371 ]
372 if platform == 'wince':
373 # See also C:\WINCE600\public\common\oak\misc\makefile.def
374 cflags += [
375 '/Zl', # omit default library name in .OBJ
376 '/GF', # enable read-only string pooling
377 '/GR-', # disable C++ RTTI
378 '/GS', # enable security checks
379 # Allow disabling language conformance to maintain backward compat
380 #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
381 #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
382 #'/wd4867',
383 #'/wd4430',
384 #'/MT',
385 #'/U_MT',
386 ]
387 # Automatic pdb generation
388 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
389 env.EnsureSConsVersion(0, 98, 0)
390 env['PDB'] = '${TARGET.base}.pdb'
391 env.Append(CFLAGS = cflags)
392 env.Append(CXXFLAGS = cflags)
José Fonsecab04aa712008-06-06 14:48:57 +0900393
José Fonseca381e3482008-07-17 11:23:43 +0900394 # Assembler options
395 if gcc:
396 if env['machine'] == 'x86':
397 env.Append(ASFLAGS = ['-m32'])
398 if env['machine'] == 'x86_64':
399 env.Append(ASFLAGS = ['-m64'])
José Fonseca27d8d6f2008-07-03 12:42:23 +0900400
José Fonseca381e3482008-07-17 11:23:43 +0900401 # Linker options
402 linkflags = []
403 if gcc:
404 if env['machine'] == 'x86':
405 linkflags += ['-m32']
406 if env['machine'] == 'x86_64':
407 linkflags += ['-m64']
408 if platform == 'winddk':
409 # See also:
410 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
411 linkflags += [
412 '/merge:_PAGE=PAGE',
413 '/merge:_TEXT=.text',
414 '/section:INIT,d',
415 '/opt:ref',
416 '/opt:icf',
417 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
418 '/incremental:no',
419 '/fullbuild',
420 '/release',
421 '/nodefaultlib',
422 '/wx',
423 '/debug',
424 '/debugtype:cv',
425 '/version:5.1',
426 '/osversion:5.1',
427 '/functionpadmin:5',
428 '/safeseh',
429 '/pdbcompress',
430 '/stack:0x40000,0x1000',
431 '/driver',
432 '/align:0x80',
433 '/subsystem:native,5.01',
434 '/base:0x10000',
435
436 '/entry:DrvEnableDriver',
437 ]
438 if env['profile']:
439 linkflags += [
440 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
441 ]
442 if platform == 'wince':
443 linkflags += [
444 '/nodefaultlib',
445 #'/incremental:no',
446 #'/fullbuild',
447 '/entry:_DllMainCRTStartup',
448 ]
449 env.Append(LINKFLAGS = linkflags)
450
José Fonsecac76787a2008-07-17 11:25:20 +0900451 # Default libs
452 env.Append(LIBS = [])
453
José Fonseca381e3482008-07-17 11:23:43 +0900454 # Custom builders and methods
455 createConvenienceLibBuilder(env)
456 createCodeGenerateMethod(env)
José Fonseca52c2dd12008-09-08 07:54:15 +0900457 createInstallMethods(env)
José Fonseca381e3482008-07-17 11:23:43 +0900458
459 # for debugging
460 #print env.Dump()
José Fonsecab04aa712008-06-06 14:48:57 +0900461
462
463def exists(env):
José Fonseca381e3482008-07-17 11:23:43 +0900464 return 1