3 Generic tool that provides a commmon ground for all platforms.
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.
36 import platform
as _platform
44 def quietCommandLines(env
):
46 # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
47 env
['CCCOMSTR'] = "Compiling $SOURCE ..."
48 env
['CXXCOMSTR'] = "Compiling $SOURCE ..."
49 env
['ARCOMSTR'] = "Archiving $TARGET ..."
50 env
['RANLIBCOMSTR'] = ""
51 env
['LINKCOMSTR'] = "Linking $TARGET ..."
54 def createConvenienceLibBuilder(env
):
55 """This is a utility function that creates the ConvenienceLibrary
56 Builder in an Environment if it is not there already.
58 If it is already there, we return the existing one.
60 Based on the stock StaticLibrary and SharedLibrary builders.
64 convenience_lib
= env
['BUILDERS']['ConvenienceLibrary']
66 action_list
= [ SCons
.Action
.Action("$ARCOM", "$ARCOMSTR") ]
67 if env
.Detect('ranlib'):
68 ranlib_action
= SCons
.Action
.Action("$RANLIBCOM", "$RANLIBCOMSTR")
69 action_list
.append(ranlib_action
)
71 convenience_lib
= SCons
.Builder
.Builder(action
= action_list
,
72 emitter
= '$LIBEMITTER',
73 prefix
= '$LIBPREFIX',
74 suffix
= '$LIBSUFFIX',
75 src_suffix
= '$SHOBJSUFFIX',
76 src_builder
= 'SharedObject')
77 env
['BUILDERS']['ConvenienceLibrary'] = convenience_lib
79 return convenience_lib
82 # TODO: handle import statements with multiple modules
83 # TODO: handle from import statements
84 import_re
= re
.compile(r
'^import\s+(\S+)$', re
.M
)
86 def python_scan(node
, env
, path
):
87 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
88 contents
= node
.get_contents()
89 source_dir
= node
.get_dir()
90 imports
= import_re
.findall(contents
)
94 file = os
.path
.join(str(dir), imp
.replace('.', os
.sep
) + '.py')
95 if os
.path
.exists(file):
96 results
.append(env
.File(file))
98 file = os
.path
.join(str(dir), imp
.replace('.', os
.sep
), '__init__.py')
99 if os
.path
.exists(file):
100 results
.append(env
.File(file))
104 python_scanner
= SCons
.Scanner
.Scanner(function
= python_scan
, skeys
= ['.py'])
107 def code_generate(env
, script
, target
, source
, command
):
108 """Method to simplify code generation via python scripts.
110 http://www.scons.org/wiki/UsingCodeGenerators
111 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
114 # We're generating code using Python scripts, so we have to be
115 # careful with our scons elements. This entry represents
116 # the generator file *in the source directory*.
117 script_src
= env
.File(script
).srcnode()
119 # This command creates generated code *in the build directory*.
120 command
= command
.replace('$SCRIPT', script_src
.path
)
121 code
= env
.Command(target
, source
, command
)
123 # Explicitly mark that the generated code depends on the generator,
124 # and on implicitly imported python modules
125 path
= (script_src
.get_dir(),)
127 deps
+= script_src
.get_implicit_deps(env
, python_scanner
, path
)
128 env
.Depends(code
, deps
)
130 # Running the Python script causes .pyc files to be generated in the
131 # source directory. When we clean up, they should go too. So add side
132 # effects for .pyc files
134 pyc
= env
.File(str(dep
) + 'c')
135 env
.SideEffect(pyc
, code
)
140 def createCodeGenerateMethod(env
):
141 env
.Append(SCANNERS
= python_scanner
)
142 env
.AddMethod(code_generate
, 'CodeGenerate')
145 def symlink(target
, source
, env
):
146 target
= str(target
[0])
147 source
= str(source
[0])
148 if os
.path
.islink(target
) or os
.path
.exists(target
):
150 os
.symlink(os
.path
.basename(source
), target
)
152 def install_shared_library(env
, source
, version
= ()):
153 source
= str(source
[0])
154 version
= tuple(map(str, version
))
155 target_dir
= os
.path
.join(env
.Dir('#.').srcnode().abspath
, env
['build'], 'lib')
156 target_name
= '.'.join((str(source
),) + version
)
157 last
= env
.InstallAs(os
.path
.join(target_dir
, target_name
), source
)
159 version
= version
[:-1]
160 target_name
= '.'.join((str(source
),) + version
)
161 action
= SCons
.Action
.Action(symlink
, "$TARGET -> $SOURCE")
162 last
= env
.Command(os
.path
.join(target_dir
, target_name
), last
, action
)
164 def createInstallMethods(env
):
165 env
.AddMethod(install_shared_library
, 'InstallSharedLibrary')
211 return int(os
.environ
['NUMBER_OF_PROCESSORS'])
212 except (ValueError, KeyError):
216 return os
.sysconf('SC_NPROCESSORS_ONLN')
217 except (ValueError, OSError, AttributeError):
221 return int(os
.popen2("sysctl -n hw.ncpu")[1].read())
229 """Common environment generation code"""
231 from SCons
.Script
import ARGUMENTS
233 # FIXME: this is already too late
234 #if env.get('quiet', False):
235 # quietCommandLines(env)
240 env
['platform'] = ARGUMENTS
['platform']
242 env
['platform'] = _platform_map
.get(sys
.platform
, sys
.platform
)
246 env
['machine'] = ARGUMENTS
['machine']
248 env
['machine'] = _machine_map
.get(os
.environ
.get('PROCESSOR_ARCHITECTURE', _platform
.machine()), 'generic')
252 env
['toolchain'] = ARGUMENTS
['toolchain']
254 if env
['platform'] in ('windows', 'winddk', 'wince') and sys
.platform
!= 'win32':
255 env
['toolchain'] = 'crossmingw'
257 env
['toolchain'] = _toolchain_map
.get(env
['platform'], 'default')
258 if env
['toolchain'] == 'crossmingw' and env
['machine'] not in ('generic', 'x86'):
259 env
['machine'] = 'x86'
262 env
['MSVS_VERSION'] = ARGUMENTS
['MSVS_VERSION']
267 env
['debug'] = _bool_map
[ARGUMENTS
.get('debug', 'no')]
268 env
['profile'] = _bool_map
[ARGUMENTS
.get('profile', 'no')]
270 # Put build output in a separate dir, which depends on the current
271 # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
273 env
['build'] = ARGUMENTS
['build']
275 build_topdir
= 'build'
276 build_subdir
= env
['platform']
277 if env
['machine'] != 'generic':
278 build_subdir
+= '-' + env
['machine']
280 build_subdir
+= "-debug"
282 build_subdir
+= "-profile"
283 env
['build'] = os
.path
.join(build_topdir
, build_subdir
)
284 # Place the .sconsign file in the build dir too, to avoid issues with
285 # different scons versions building the same source file
286 env
.SConsignFile(os
.path
.join(env
['build'], '.sconsign'))
289 if env
.GetOption('num_jobs') <= 1:
290 env
.SetOption('num_jobs', num_jobs())
294 print ' platform=%s' % env
['platform']
295 print ' machine=%s' % env
['machine']
296 print ' toolchain=%s' % env
['toolchain']
297 print ' debug=%s' % ['no', 'yes'][env
['debug']]
298 print ' profile=%s' % ['no', 'yes'][env
['profile']]
299 print ' build=%s' % env
['build']
300 print ' %s jobs' % env
.GetOption('num_jobs')
304 env
.Tool(env
['toolchain'])
306 env
['gcc'] = 'gcc' in os
.path
.basename(env
['CC']).split('-')
307 env
['msvc'] = env
['CC'] == 'cl'
311 machine
= env
['machine']
312 platform
= env
['platform']
313 x86
= env
['machine'] == 'x86'
314 ppc
= env
['machine'] == 'ppc'
318 # C preprocessor options
321 cppdefines
+= ['DEBUG']
323 cppdefines
+= ['NDEBUG']
325 cppdefines
+= ['PROFILE']
326 if platform
== 'windows':
332 # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
333 #'WIN32_LEAN_AND_MEAN',
339 '_CRT_SECURE_NO_WARNINGS',
340 '_CRT_SECURE_NO_DEPRECATE',
341 '_SCL_SECURE_NO_WARNINGS',
342 '_SCL_SECURE_NO_DEPRECATE',
345 cppdefines
+= ['_DEBUG']
346 if platform
== 'winddk':
347 # Mimic WINDDK's builtin flags. See also:
348 # - WINDDK's bin/makefile.new i386mk.inc for more info.
349 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
350 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
355 ('CONDITION_HANDLING', '1'),
360 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
361 ('WINVER', '0x0501'),
362 ('_WIN32_IE', '0x0603'),
363 ('WIN32_LEAN_AND_MEAN', '1'),
365 ('__BUILDMACHINE__', 'WinDDK'),
369 cppdefines
+= [('DBG', 1)]
370 if platform
== 'wince':
372 '_CRT_SECURE_NO_DEPRECATE',
377 ('_WIN32_WCE', '0x600'),
385 ('INTLMSG_CODEPAGE', '1252'),
387 env
.Append(CPPDEFINES
= cppdefines
)
389 # C preprocessor includes
390 if platform
== 'winddk':
391 env
.Append(CPPPATH
= [
401 ccflags
= [] # C & C++
404 ccflags
+= ['-O0', '-g3']
405 elif env
['toolchain'] == 'crossmingw':
406 ccflags
+= ['-O0', '-g3'] # mingw 4.2.1 optimizer is broken
408 ccflags
+= ['-O3', '-g0']
409 if env
['machine'] == 'x86':
413 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
416 if env
['machine'] == 'x86_64':
419 # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
422 '-Wmissing-field-initializers',
426 '-fmessage-length=0', # be nice to Eclipse
429 '-Werror=declaration-after-statement',
430 '-Wmissing-prototypes',
435 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
439 '/Od', # disable optimizations
440 '/Oi', # enable intrinsic functions
441 '/Oy-', # disable frame pointer omission
442 '/GL-', # disable whole program optimization
446 '/Ox', # maximum optimizations
447 '/Oi', # enable intrinsic functions
448 '/Ot', # favor code speed
449 #'/fp:fast', # fast floating point
452 '/W3', # warning level
453 #'/Wp64', # enable 64 bit porting warnings
455 if env
['machine'] == 'x86':
457 #'/QIfist', # Suppress _ftol
458 #'/arch:SSE2', # use the SSE2 instructions
460 if platform
== 'windows':
464 if platform
== 'winddk':
466 '/Zl', # omit default library name in .OBJ
467 '/Zp8', # 8bytes struct member alignment
468 '/Gy', # separate functions for linker
469 '/Gm-', # disable minimal rebuild
470 '/WX', # treat warnings as errors
471 '/Gz', # __stdcall Calling convention
472 '/GX-', # disable C++ EH
473 '/GR-', # disable C++ RTTI
474 '/GF', # enable read-only string pooling
475 '/G6', # optimize for PPro, P-II, P-III
476 '/Ze', # enable extensions
477 '/Gi-', # disable incremental compilation
478 '/QIfdiv-', # disable Pentium FDIV fix
479 '/hotpatch', # prepares an image for hotpatching.
480 #'/Z7', #enable old-style debug info
482 if platform
== 'wince':
483 # See also C:\WINCE600\public\common\oak\misc\makefile.def
485 '/Zl', # omit default library name in .OBJ
486 '/GF', # enable read-only string pooling
487 '/GR-', # disable C++ RTTI
488 '/GS', # enable security checks
489 # Allow disabling language conformance to maintain backward compat
490 #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
491 #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
497 # Automatic pdb generation
498 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
499 env
.EnsureSConsVersion(0, 98, 0)
500 env
['PDB'] = '${TARGET.base}.pdb'
501 env
.Append(CCFLAGS
= ccflags
)
502 env
.Append(CFLAGS
= cflags
)
503 env
.Append(CXXFLAGS
= cxxflags
)
505 if env
['platform'] == 'windows' and msvc
:
506 # Choose the appropriate MSVC CRT
507 # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
509 env
.Append(CCFLAGS
= ['/MTd'])
510 env
.Append(SHCCFLAGS
= ['/LDd'])
512 env
.Append(CCFLAGS
= ['/MT'])
513 env
.Append(SHCCFLAGS
= ['/LD'])
517 if env
['machine'] == 'x86':
518 env
.Append(ASFLAGS
= ['-m32'])
519 if env
['machine'] == 'x86_64':
520 env
.Append(ASFLAGS
= ['-m64'])
525 if env
['machine'] == 'x86':
526 linkflags
+= ['-m32']
527 if env
['machine'] == 'x86_64':
528 linkflags
+= ['-m64']
529 if platform
== 'windows' and msvc
:
531 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
536 if platform
== 'winddk':
539 '/merge:_TEXT=.text',
543 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
556 '/stack:0x40000,0x1000',
559 '/subsystem:native,5.01',
562 '/entry:DrvEnableDriver',
564 if env
['debug'] or env
['profile']:
566 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
568 if platform
== 'wince':
573 '/entry:_DllMainCRTStartup',
575 env
.Append(LINKFLAGS
= linkflags
)
578 env
.Append(LIBS
= [])
580 # Custom builders and methods
581 createConvenienceLibBuilder(env
)
582 createCodeGenerateMethod(env
)
583 createInstallMethods(env
)