3 Frontend-tool for Gallium3D architecture.
8 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
19 # The above copyright notice and this permission notice (including the
20 # next paragraph) shall be included in all copies or substantial portions
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.
44 def quietCommandLines(env
):
46 # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
47 env
['ASCOMSTR'] = " Assembling $SOURCE ..."
48 env
['ASPPCOMSTR'] = " Assembling $SOURCE ..."
49 env
['CCCOMSTR'] = " Compiling $SOURCE ..."
50 env
['SHCCCOMSTR'] = " Compiling $SOURCE ..."
51 env
['CXXCOMSTR'] = " Compiling $SOURCE ..."
52 env
['SHCXXCOMSTR'] = " Compiling $SOURCE ..."
53 env
['ARCOMSTR'] = " Archiving $TARGET ..."
54 env
['RANLIBCOMSTR'] = " Indexing $TARGET ..."
55 env
['LINKCOMSTR'] = " Linking $TARGET ..."
56 env
['SHLINKCOMSTR'] = " Linking $TARGET ..."
57 env
['LDMODULECOMSTR'] = " Linking $TARGET ..."
58 env
['SWIGCOMSTR'] = " Generating $TARGET ..."
61 def createConvenienceLibBuilder(env
):
62 """This is a utility function that creates the ConvenienceLibrary
63 Builder in an Environment if it is not there already.
65 If it is already there, we return the existing one.
67 Based on the stock StaticLibrary and SharedLibrary builders.
71 convenience_lib
= env
['BUILDERS']['ConvenienceLibrary']
73 action_list
= [ SCons
.Action
.Action("$ARCOM", "$ARCOMSTR") ]
74 if env
.Detect('ranlib'):
75 ranlib_action
= SCons
.Action
.Action("$RANLIBCOM", "$RANLIBCOMSTR")
76 action_list
.append(ranlib_action
)
78 convenience_lib
= SCons
.Builder
.Builder(action
= action_list
,
79 emitter
= '$LIBEMITTER',
80 prefix
= '$LIBPREFIX',
81 suffix
= '$LIBSUFFIX',
82 src_suffix
= '$SHOBJSUFFIX',
83 src_builder
= 'SharedObject')
84 env
['BUILDERS']['ConvenienceLibrary'] = convenience_lib
86 return convenience_lib
89 # TODO: handle import statements with multiple modules
90 # TODO: handle from import statements
91 import_re
= re
.compile(r
'^import\s+(\S+)$', re
.M
)
93 def python_scan(node
, env
, path
):
94 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
95 contents
= node
.get_contents()
96 source_dir
= node
.get_dir()
97 imports
= import_re
.findall(contents
)
101 file = os
.path
.join(str(dir), imp
.replace('.', os
.sep
) + '.py')
102 if os
.path
.exists(file):
103 results
.append(env
.File(file))
105 file = os
.path
.join(str(dir), imp
.replace('.', os
.sep
), '__init__.py')
106 if os
.path
.exists(file):
107 results
.append(env
.File(file))
111 python_scanner
= SCons
.Scanner
.Scanner(function
= python_scan
, skeys
= ['.py'])
114 def code_generate(env
, script
, target
, source
, command
):
115 """Method to simplify code generation via python scripts.
117 http://www.scons.org/wiki/UsingCodeGenerators
118 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
121 # We're generating code using Python scripts, so we have to be
122 # careful with our scons elements. This entry represents
123 # the generator file *in the source directory*.
124 script_src
= env
.File(script
).srcnode()
126 # This command creates generated code *in the build directory*.
127 command
= command
.replace('$SCRIPT', script_src
.path
)
128 code
= env
.Command(target
, source
, command
)
130 # Explicitly mark that the generated code depends on the generator,
131 # and on implicitly imported python modules
132 path
= (script_src
.get_dir(),)
134 deps
+= script_src
.get_implicit_deps(env
, python_scanner
, path
)
135 env
.Depends(code
, deps
)
137 # Running the Python script causes .pyc files to be generated in the
138 # source directory. When we clean up, they should go too. So add side
139 # effects for .pyc files
141 pyc
= env
.File(str(dep
) + 'c')
142 env
.SideEffect(pyc
, code
)
147 def createCodeGenerateMethod(env
):
148 env
.Append(SCANNERS
= python_scanner
)
149 env
.AddMethod(code_generate
, 'CodeGenerate')
152 def symlink(target
, source
, env
):
153 target
= str(target
[0])
154 source
= str(source
[0])
155 if os
.path
.islink(target
) or os
.path
.exists(target
):
157 os
.symlink(os
.path
.basename(source
), target
)
159 def install_program(env
, source
):
160 source
= str(source
[0])
161 target_dir
= os
.path
.join(env
.Dir('#.').srcnode().abspath
, env
['build'], 'bin')
162 target_name
= str(source
)
163 env
.InstallAs(os
.path
.join(target_dir
, target_name
), source
)
165 def install_shared_library(env
, source
, version
= ()):
166 source
= str(source
[0])
167 version
= tuple(map(str, version
))
168 target_dir
= os
.path
.join(env
.Dir('#.').srcnode().abspath
, env
['build'], 'lib')
169 target_name
= '.'.join((str(source
),) + version
)
170 last
= env
.InstallAs(os
.path
.join(target_dir
, target_name
), source
)
172 version
= version
[:-1]
173 target_name
= '.'.join((str(source
),) + version
)
174 action
= SCons
.Action
.Action(symlink
, "$TARGET -> $SOURCE")
175 last
= env
.Command(os
.path
.join(target_dir
, target_name
), last
, action
)
177 def createInstallMethods(env
):
178 env
.AddMethod(install_program
, 'InstallProgram')
179 env
.AddMethod(install_shared_library
, 'InstallSharedLibrary')
184 return int(os
.environ
['NUMBER_OF_PROCESSORS'])
185 except (ValueError, KeyError):
189 return os
.sysconf('SC_NPROCESSORS_ONLN')
190 except (ValueError, OSError, AttributeError):
194 return int(os
.popen2("sysctl -n hw.ncpu")[1].read())
202 """Common environment generation code"""
204 if env
.get('quiet', True):
205 quietCommandLines(env
)
208 platform
= env
['platform']
209 if env
['toolchain'] == 'default':
210 if platform
== 'winddk':
211 env
['toolchain'] = 'winddk'
212 elif platform
== 'wince':
213 env
['toolchain'] = 'wcesdk'
214 env
.Tool(env
['toolchain'])
216 env
['gcc'] = 'gcc' in os
.path
.basename(env
['CC']).split('-')
217 env
['msvc'] = env
['CC'] == 'cl'
221 machine
= env
['machine']
222 platform
= env
['platform']
223 x86
= env
['machine'] == 'x86'
224 ppc
= env
['machine'] == 'ppc'
228 # Put build output in a separate dir, which depends on the current
229 # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
230 build_topdir
= 'build'
231 build_subdir
= env
['platform']
233 build_subdir
+= "-llvm"
234 if env
['machine'] != 'generic':
235 build_subdir
+= '-' + env
['machine']
237 build_subdir
+= "-debug"
239 build_subdir
+= "-profile"
240 build_dir
= os
.path
.join(build_topdir
, build_subdir
)
241 # Place the .sconsign file in the build dir too, to avoid issues with
242 # different scons versions building the same source file
243 env
['build'] = build_dir
244 env
.SConsignFile(os
.path
.join(build_dir
, '.sconsign'))
245 env
.CacheDir('build/cache')
246 env
['CONFIGUREDIR'] = os
.path
.join(build_dir
, 'conf')
247 env
['CONFIGURELOG'] = os
.path
.join(os
.path
.abspath(build_dir
), 'config.log')
250 if env
.GetOption('num_jobs') <= 1:
251 env
.SetOption('num_jobs', num_jobs())
253 # C preprocessor options
256 cppdefines
+= ['DEBUG']
258 cppdefines
+= ['NDEBUG']
260 cppdefines
+= ['PROFILE']
261 if platform
== 'windows':
267 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
268 ('WINVER', '0x0501'),
269 # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
270 'WIN32_LEAN_AND_MEAN',
272 if msvc
and env
['toolchain'] != 'winddk':
276 '_CRT_SECURE_NO_WARNINGS',
277 '_CRT_SECURE_NO_DEPRECATE',
278 '_SCL_SECURE_NO_WARNINGS',
279 '_SCL_SECURE_NO_DEPRECATE',
282 cppdefines
+= ['_DEBUG']
283 if env
['toolchain'] == 'winddk':
284 # Mimic WINDDK's builtin flags. See also:
285 # - WINDDK's bin/makefile.new i386mk.inc for more info.
286 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
287 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
289 cppdefines
+= ['_X86_', 'i386']
290 if machine
== 'x86_64':
291 cppdefines
+= ['_AMD64_', 'AMD64']
292 if platform
== 'winddk':
295 ('CONDITION_HANDLING', '1'),
300 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
301 ('WINVER', '0x0501'),
302 ('_WIN32_IE', '0x0603'),
303 ('WIN32_LEAN_AND_MEAN', '1'),
305 ('__BUILDMACHINE__', 'WinDDK'),
309 cppdefines
+= [('DBG', 1)]
310 if platform
== 'wince':
312 '_CRT_SECURE_NO_DEPRECATE',
317 ('_WIN32_WCE', '0x600'),
325 ('INTLMSG_CODEPAGE', '1252'),
327 if platform
== 'windows':
328 cppdefines
+= ['PIPE_SUBSYSTEM_WINDOWS_USER']
329 if platform
== 'winddk':
330 cppdefines
+= ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
331 if platform
== 'wince':
332 cppdefines
+= ['PIPE_SUBSYSTEM_WINDOWS_CE']
333 cppdefines
+= ['PIPE_SUBSYSTEM_WINDOWS_CE_OGL']
334 env
.Append(CPPDEFINES
= cppdefines
)
339 ccflags
= [] # C & C++
342 ccflags
+= ['-O0', '-g3']
343 elif env
['CCVERSION'].startswith('4.2.'):
344 # gcc 4.2.x optimizer is broken
345 print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
346 ccflags
+= ['-O0', '-g3']
348 ccflags
+= ['-O3', '-g3']
350 # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
352 '-fno-omit-frame-pointer',
353 '-fno-optimize-sibling-calls',
355 if env
['machine'] == 'x86':
361 if platform
!= 'windows':
362 # XXX: -mstackrealign causes stack corruption on MinGW. Ditto
363 # for -mincoming-stack-boundary=2. Still enable it on other
364 # platforms for now, but we can't rely on it for cross platform
365 # code. We have to use __attribute__((force_align_arg_pointer))
368 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
369 '-mstackrealign', # ensure stack is aligned
371 if env
['machine'] == 'x86_64':
374 # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
377 '-Wmissing-field-initializers',
378 '-Werror=pointer-arith',
381 '-fmessage-length=0', # be nice to Eclipse
384 '-Werror=declaration-after-statement',
385 '-Wmissing-prototypes',
390 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
394 '/Od', # disable optimizations
395 '/Oi', # enable intrinsic functions
396 '/Oy-', # disable frame pointer omission
397 '/GL-', # disable whole program optimization
401 '/O2', # optimize for speed
402 '/GL', # enable whole program optimization
405 '/fp:fast', # fast floating point
406 '/W3', # warning level
407 #'/Wp64', # enable 64 bit porting warnings
409 if env
['machine'] == 'x86':
411 #'/arch:SSE2', # use the SSE2 instructions
413 if platform
== 'windows':
417 if platform
== 'winddk':
419 '/Zl', # omit default library name in .OBJ
420 '/Zp8', # 8bytes struct member alignment
421 '/Gy', # separate functions for linker
422 '/Gm-', # disable minimal rebuild
423 '/WX', # treat warnings as errors
424 '/Gz', # __stdcall Calling convention
425 '/GX-', # disable C++ EH
426 '/GR-', # disable C++ RTTI
427 '/GF', # enable read-only string pooling
428 '/G6', # optimize for PPro, P-II, P-III
429 '/Ze', # enable extensions
430 '/Gi-', # disable incremental compilation
431 '/QIfdiv-', # disable Pentium FDIV fix
432 '/hotpatch', # prepares an image for hotpatching.
433 #'/Z7', #enable old-style debug info
435 if platform
== 'wince':
436 # See also C:\WINCE600\public\common\oak\misc\makefile.def
438 '/Zl', # omit default library name in .OBJ
439 '/GF', # enable read-only string pooling
440 '/GR-', # disable C++ RTTI
441 '/GS', # enable security checks
442 # Allow disabling language conformance to maintain backward compat
443 #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
444 #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
450 # Automatic pdb generation
451 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
452 env
.EnsureSConsVersion(0, 98, 0)
453 env
['PDB'] = '${TARGET.base}.pdb'
454 env
.Append(CCFLAGS
= ccflags
)
455 env
.Append(CFLAGS
= cflags
)
456 env
.Append(CXXFLAGS
= cxxflags
)
458 if env
['platform'] == 'windows' and msvc
:
459 # Choose the appropriate MSVC CRT
460 # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
462 env
.Append(CCFLAGS
= ['/MTd'])
463 env
.Append(SHCCFLAGS
= ['/LDd'])
465 env
.Append(CCFLAGS
= ['/MT'])
466 env
.Append(SHCCFLAGS
= ['/LD'])
470 if env
['machine'] == 'x86':
471 env
.Append(ASFLAGS
= ['-m32'])
472 if env
['machine'] == 'x86_64':
473 env
.Append(ASFLAGS
= ['-m64'])
479 if env
['machine'] == 'x86':
480 linkflags
+= ['-m32']
481 if env
['machine'] == 'x86_64':
482 linkflags
+= ['-m64']
486 # Handle circular dependencies in the libraries
487 env
['_LIBFLAGS'] = '-Wl,--start-group ' + env
['_LIBFLAGS'] + ' -Wl,--end-group'
490 # enable Link-time Code Generation
491 linkflags
+= ['/LTCG']
492 env
.Append(ARFLAGS
= ['/LTCG'])
493 if platform
== 'windows' and msvc
:
495 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
500 if platform
== 'winddk':
503 '/merge:_TEXT=.text',
507 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
520 '/stack:0x40000,0x1000',
523 '/subsystem:native,5.01',
526 '/entry:DrvEnableDriver',
528 if env
['debug'] or env
['profile']:
530 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
532 if platform
== 'wince':
537 '/entry:_DllMainCRTStartup',
539 env
.Append(LINKFLAGS
= linkflags
)
540 env
.Append(SHLINKFLAGS
= shlinkflags
)
543 env
.Append(LIBS
= [])
545 # Custom builders and methods
546 createConvenienceLibBuilder(env
)
547 createCodeGenerateMethod(env
)
548 createInstallMethods(env
)