scons: Disable version symlinking for DLLs.
[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 symlink(target, source, env):
43 target = str(target[0])
44 source = str(source[0])
45 if os.path.islink(target) or os.path.exists(target):
46 os.remove(target)
47 os.symlink(os.path.basename(source), target)
48
49 def install_program(env, source):
50 source = str(source[0])
51 target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build'], 'bin')
52 target_name = str(source)
53 env.InstallAs(os.path.join(target_dir, target_name), source)
54
55 def install_shared_library(env, source, version = ()):
56 source = str(source[0])
57 version = tuple(map(str, version))
58 target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build'], 'lib')
59 if env['SHLIBSUFFIX'] == '.so':
60 target_name = '.'.join((str(source),) + version)
61 last = env.InstallAs(os.path.join(target_dir, target_name), source)
62 while len(version):
63 version = version[:-1]
64 target_name = '.'.join((str(source),) + version)
65 action = SCons.Action.Action(symlink, "$TARGET -> $SOURCE")
66 last = env.Command(os.path.join(target_dir, target_name), last, action)
67 else:
68 target_name = str(source)
69 env.InstallAs(os.path.join(target_dir, target_name), source)
70
71 def createInstallMethods(env):
72 env.AddMethod(install_program, 'InstallProgram')
73 env.AddMethod(install_shared_library, 'InstallSharedLibrary')
74
75
76 def num_jobs():
77 try:
78 return int(os.environ['NUMBER_OF_PROCESSORS'])
79 except (ValueError, KeyError):
80 pass
81
82 try:
83 return os.sysconf('SC_NPROCESSORS_ONLN')
84 except (ValueError, OSError, AttributeError):
85 pass
86
87 try:
88 return int(os.popen2("sysctl -n hw.ncpu")[1].read())
89 except ValueError:
90 pass
91
92 return 1
93
94
95 def generate(env):
96 """Common environment generation code"""
97
98 # Toolchain
99 platform = env['platform']
100 if env['toolchain'] == 'default':
101 if platform == 'winddk':
102 env['toolchain'] = 'winddk'
103 elif platform == 'wince':
104 env['toolchain'] = 'wcesdk'
105 env.Tool(env['toolchain'])
106
107 env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
108 env['msvc'] = env['CC'] == 'cl'
109
110 # shortcuts
111 debug = env['debug']
112 machine = env['machine']
113 platform = env['platform']
114 x86 = env['machine'] == 'x86'
115 ppc = env['machine'] == 'ppc'
116 gcc = env['gcc']
117 msvc = env['msvc']
118
119 # Put build output in a separate dir, which depends on the current
120 # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
121 build_topdir = 'build'
122 build_subdir = env['platform']
123 if env['llvm']:
124 build_subdir += "-llvm"
125 if env['machine'] != 'generic':
126 build_subdir += '-' + env['machine']
127 if env['debug']:
128 build_subdir += "-debug"
129 if env['profile']:
130 build_subdir += "-profile"
131 build_dir = os.path.join(build_topdir, build_subdir)
132 # Place the .sconsign file in the build dir too, to avoid issues with
133 # different scons versions building the same source file
134 env['build'] = build_dir
135 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
136 env.CacheDir('build/cache')
137 env['CONFIGUREDIR'] = os.path.join(build_dir, 'conf')
138 env['CONFIGURELOG'] = os.path.join(os.path.abspath(build_dir), 'config.log')
139
140 # Parallel build
141 if env.GetOption('num_jobs') <= 1:
142 env.SetOption('num_jobs', num_jobs())
143
144 # C preprocessor options
145 cppdefines = []
146 if debug:
147 cppdefines += ['DEBUG']
148 else:
149 cppdefines += ['NDEBUG']
150 if env['profile']:
151 cppdefines += ['PROFILE']
152 if platform == 'windows':
153 cppdefines += [
154 'WIN32',
155 '_WINDOWS',
156 #'_UNICODE',
157 #'UNICODE',
158 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
159 ('WINVER', '0x0501'),
160 ]
161 if msvc and env['toolchain'] != 'winddk':
162 cppdefines += [
163 'VC_EXTRALEAN',
164 '_USE_MATH_DEFINES',
165 '_CRT_SECURE_NO_WARNINGS',
166 '_CRT_SECURE_NO_DEPRECATE',
167 '_SCL_SECURE_NO_WARNINGS',
168 '_SCL_SECURE_NO_DEPRECATE',
169 ]
170 if debug:
171 cppdefines += ['_DEBUG']
172 if env['toolchain'] == 'winddk':
173 # Mimic WINDDK's builtin flags. See also:
174 # - WINDDK's bin/makefile.new i386mk.inc for more info.
175 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
176 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
177 if machine == 'x86':
178 cppdefines += ['_X86_', 'i386']
179 if machine == 'x86_64':
180 cppdefines += ['_AMD64_', 'AMD64']
181 if platform == 'winddk':
182 cppdefines += [
183 'STD_CALL',
184 ('CONDITION_HANDLING', '1'),
185 ('NT_INST', '0'),
186 ('WIN32', '100'),
187 ('_NT1X_', '100'),
188 ('WINNT', '1'),
189 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
190 ('WINVER', '0x0501'),
191 ('_WIN32_IE', '0x0603'),
192 ('WIN32_LEAN_AND_MEAN', '1'),
193 ('DEVL', '1'),
194 ('__BUILDMACHINE__', 'WinDDK'),
195 ('FPO', '0'),
196 ]
197 if debug:
198 cppdefines += [('DBG', 1)]
199 if platform == 'wince':
200 cppdefines += [
201 '_CRT_SECURE_NO_DEPRECATE',
202 '_USE_32BIT_TIME_T',
203 'UNICODE',
204 '_UNICODE',
205 ('UNDER_CE', '600'),
206 ('_WIN32_WCE', '0x600'),
207 'WINCEOEM',
208 'WINCEINTERNAL',
209 'WIN32',
210 'STRICT',
211 'x86',
212 '_X86_',
213 'INTERNATIONAL',
214 ('INTLMSG_CODEPAGE', '1252'),
215 ]
216 if platform == 'windows':
217 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
218 if platform == 'winddk':
219 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
220 if platform == 'wince':
221 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
222 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE_OGL']
223 env.Append(CPPDEFINES = cppdefines)
224
225 # C compiler options
226 cflags = [] # C
227 cxxflags = [] # C++
228 ccflags = [] # C & C++
229 if gcc:
230 if debug:
231 ccflags += ['-O0', '-g3']
232 elif env['CCVERSION'].startswith('4.2.'):
233 # gcc 4.2.x optimizer is broken
234 print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
235 ccflags += ['-O0', '-g3']
236 else:
237 ccflags += ['-O3', '-g3']
238 if env['profile']:
239 # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
240 ccflags += [
241 '-fno-omit-frame-pointer',
242 '-fno-optimize-sibling-calls',
243 ]
244 if env['machine'] == 'x86':
245 ccflags += [
246 '-m32',
247 #'-march=pentium4',
248 #'-mfpmath=sse',
249 ]
250 if platform != 'windows':
251 # XXX: -mstackrealign causes stack corruption on MinGW. Ditto
252 # for -mincoming-stack-boundary=2. Still enable it on other
253 # platforms for now, but we can't rely on it for cross platform
254 # code. We have to use __attribute__((force_align_arg_pointer))
255 # instead.
256 ccflags += [
257 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
258 '-mstackrealign', # ensure stack is aligned
259 ]
260 if env['machine'] == 'x86_64':
261 ccflags += ['-m64']
262 # See also:
263 # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
264 ccflags += [
265 '-Wall',
266 '-Wmissing-field-initializers',
267 '-Werror=pointer-arith',
268 '-Wno-long-long',
269 '-ffast-math',
270 '-fmessage-length=0', # be nice to Eclipse
271 ]
272 cflags += [
273 '-Werror=declaration-after-statement',
274 '-Wmissing-prototypes',
275 '-std=gnu99',
276 ]
277 if msvc:
278 # See also:
279 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
280 # - cl /?
281 if debug:
282 ccflags += [
283 '/Od', # disable optimizations
284 '/Oi', # enable intrinsic functions
285 '/Oy-', # disable frame pointer omission
286 '/GL-', # disable whole program optimization
287 ]
288 else:
289 ccflags += [
290 '/O2', # optimize for speed
291 '/GL', # enable whole program optimization
292 ]
293 ccflags += [
294 '/fp:fast', # fast floating point
295 '/W3', # warning level
296 #'/Wp64', # enable 64 bit porting warnings
297 ]
298 if env['machine'] == 'x86':
299 ccflags += [
300 #'/arch:SSE2', # use the SSE2 instructions
301 ]
302 if platform == 'windows':
303 ccflags += [
304 # TODO
305 ]
306 if platform == 'winddk':
307 ccflags += [
308 '/Zl', # omit default library name in .OBJ
309 '/Zp8', # 8bytes struct member alignment
310 '/Gy', # separate functions for linker
311 '/Gm-', # disable minimal rebuild
312 '/WX', # treat warnings as errors
313 '/Gz', # __stdcall Calling convention
314 '/GX-', # disable C++ EH
315 '/GR-', # disable C++ RTTI
316 '/GF', # enable read-only string pooling
317 '/G6', # optimize for PPro, P-II, P-III
318 '/Ze', # enable extensions
319 '/Gi-', # disable incremental compilation
320 '/QIfdiv-', # disable Pentium FDIV fix
321 '/hotpatch', # prepares an image for hotpatching.
322 #'/Z7', #enable old-style debug info
323 ]
324 if platform == 'wince':
325 # See also C:\WINCE600\public\common\oak\misc\makefile.def
326 ccflags += [
327 '/Zl', # omit default library name in .OBJ
328 '/GF', # enable read-only string pooling
329 '/GR-', # disable C++ RTTI
330 '/GS', # enable security checks
331 # Allow disabling language conformance to maintain backward compat
332 #'/Zc:wchar_t-', # don't force wchar_t as native type, instead of typedef
333 #'/Zc:forScope-', # don't enforce Standard C++ for scoping rules
334 #'/wd4867',
335 #'/wd4430',
336 #'/MT',
337 #'/U_MT',
338 ]
339 # Automatic pdb generation
340 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
341 env.EnsureSConsVersion(0, 98, 0)
342 env['PDB'] = '${TARGET.base}.pdb'
343 env.Append(CCFLAGS = ccflags)
344 env.Append(CFLAGS = cflags)
345 env.Append(CXXFLAGS = cxxflags)
346
347 if env['platform'] == 'windows' and msvc:
348 # Choose the appropriate MSVC CRT
349 # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
350 if env['debug']:
351 env.Append(CCFLAGS = ['/MTd'])
352 env.Append(SHCCFLAGS = ['/LDd'])
353 else:
354 env.Append(CCFLAGS = ['/MT'])
355 env.Append(SHCCFLAGS = ['/LD'])
356
357 # Assembler options
358 if gcc:
359 if env['machine'] == 'x86':
360 env.Append(ASFLAGS = ['-m32'])
361 if env['machine'] == 'x86_64':
362 env.Append(ASFLAGS = ['-m64'])
363
364 # Linker options
365 linkflags = []
366 shlinkflags = []
367 if gcc:
368 if env['machine'] == 'x86':
369 linkflags += ['-m32']
370 if env['machine'] == 'x86_64':
371 linkflags += ['-m64']
372 shlinkflags += [
373 '-Wl,-Bsymbolic',
374 ]
375 # Handle circular dependencies in the libraries
376 env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
377 if msvc:
378 if not env['debug']:
379 # enable Link-time Code Generation
380 linkflags += ['/LTCG']
381 env.Append(ARFLAGS = ['/LTCG'])
382 if platform == 'windows' and msvc:
383 # See also:
384 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
385 linkflags += [
386 '/fixed:no',
387 '/incremental:no',
388 ]
389 if platform == 'winddk':
390 linkflags += [
391 '/merge:_PAGE=PAGE',
392 '/merge:_TEXT=.text',
393 '/section:INIT,d',
394 '/opt:ref',
395 '/opt:icf',
396 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
397 '/incremental:no',
398 '/fullbuild',
399 '/release',
400 '/nodefaultlib',
401 '/wx',
402 '/debug',
403 '/debugtype:cv',
404 '/version:5.1',
405 '/osversion:5.1',
406 '/functionpadmin:5',
407 '/safeseh',
408 '/pdbcompress',
409 '/stack:0x40000,0x1000',
410 '/driver',
411 '/align:0x80',
412 '/subsystem:native,5.01',
413 '/base:0x10000',
414
415 '/entry:DrvEnableDriver',
416 ]
417 if env['debug'] or env['profile']:
418 linkflags += [
419 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
420 ]
421 if platform == 'wince':
422 linkflags += [
423 '/nodefaultlib',
424 #'/incremental:no',
425 #'/fullbuild',
426 '/entry:_DllMainCRTStartup',
427 ]
428 env.Append(LINKFLAGS = linkflags)
429 env.Append(SHLINKFLAGS = shlinkflags)
430
431 # Default libs
432 env.Append(LIBS = [])
433
434 # Custom builders and methods
435 env.Tool('custom')
436 createInstallMethods(env)
437
438 # for debugging
439 #print env.Dump()
440
441
442 def exists(env):
443 return 1