scons: Enable building through Clang Static Analyzer.
[mesa.git] / scons / gallium.py
1 """gallium
2
3 Frontend-tool for Gallium3D architecture.
4
5 """
6
7 #
8 # Copyright 2008 VMware, Inc.
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 VMWARE 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 distutils.version
34 import os
35 import os.path
36 import re
37 import subprocess
38 import platform as _platform
39
40 import SCons.Action
41 import SCons.Builder
42 import SCons.Scanner
43
44
45 def symlink(target, source, env):
46 target = str(target[0])
47 source = str(source[0])
48 if os.path.islink(target) or os.path.exists(target):
49 os.remove(target)
50 os.symlink(os.path.basename(source), target)
51
52 def install(env, source, subdir):
53 target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'], subdir)
54 return env.Install(target_dir, source)
55
56 def install_program(env, source):
57 return install(env, source, 'bin')
58
59 def install_shared_library(env, sources, version = ()):
60 targets = []
61 install_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'])
62 version = tuple(map(str, version))
63 if env['SHLIBSUFFIX'] == '.dll':
64 dlls = env.FindIxes(sources, 'SHLIBPREFIX', 'SHLIBSUFFIX')
65 targets += install(env, dlls, 'bin')
66 libs = env.FindIxes(sources, 'LIBPREFIX', 'LIBSUFFIX')
67 targets += install(env, libs, 'lib')
68 else:
69 for source in sources:
70 target_dir = os.path.join(install_dir, 'lib')
71 target_name = '.'.join((str(source),) + version)
72 last = env.InstallAs(os.path.join(target_dir, target_name), source)
73 targets += last
74 while len(version):
75 version = version[:-1]
76 target_name = '.'.join((str(source),) + version)
77 action = SCons.Action.Action(symlink, " Symlinking $TARGET ...")
78 last = env.Command(os.path.join(target_dir, target_name), last, action)
79 targets += last
80 return targets
81
82
83 def createInstallMethods(env):
84 env.AddMethod(install_program, 'InstallProgram')
85 env.AddMethod(install_shared_library, 'InstallSharedLibrary')
86
87
88 def num_jobs():
89 try:
90 return int(os.environ['NUMBER_OF_PROCESSORS'])
91 except (ValueError, KeyError):
92 pass
93
94 try:
95 return os.sysconf('SC_NPROCESSORS_ONLN')
96 except (ValueError, OSError, AttributeError):
97 pass
98
99 try:
100 return int(os.popen2("sysctl -n hw.ncpu")[1].read())
101 except ValueError:
102 pass
103
104 return 1
105
106
107 def get_cc_version(env):
108 # Get the first line of `$CC --version`
109 pipe = SCons.Action._subproc(env, [env['CC'], '--version'],
110 stdin = 'devnull',
111 stderr = 'devnull',
112 stdout = subprocess.PIPE)
113 if pipe.wait() != 0:
114 return ''
115
116 line = pipe.stdout.readline()
117 return line
118
119
120 def generate(env):
121 """Common environment generation code"""
122
123 # Tell tools which machine to compile for
124 env['TARGET_ARCH'] = env['machine']
125 env['MSVS_ARCH'] = env['machine']
126
127 # Toolchain
128 platform = env['platform']
129 env.Tool(env['toolchain'])
130
131 # Allow override compiler and specify additional flags from environment
132 if os.environ.has_key('CC'):
133 env['CC'] = os.environ['CC']
134 # Update CCVERSION to match
135 line = get_cc_version(env)
136 if line:
137 match = re.search(r'[0-9]+(\.[0-9]+)+', line)
138 if match:
139 env['CCVERSION'] = match.group(0)
140 if os.environ.has_key('CFLAGS'):
141 env['CCFLAGS'] += SCons.Util.CLVar(os.environ['CFLAGS'])
142 if os.environ.has_key('CXX'):
143 env['CXX'] = os.environ['CXX']
144 if os.environ.has_key('CXXFLAGS'):
145 env['CXXFLAGS'] += SCons.Util.CLVar(os.environ['CXXFLAGS'])
146 if os.environ.has_key('LDFLAGS'):
147 env['LINKFLAGS'] += SCons.Util.CLVar(os.environ['LDFLAGS'])
148
149 # Detect gcc/clang not by executable name, but through `--version` option,
150 # to avoid drawing wrong conclusions when using tools that overrice CC/CXX
151 # like scan-build.
152 cc_version = get_cc_version(env)
153 cc_version_words = cc_version.split()
154
155 env['gcc'] = 'gcc' in cc_version_words
156 env['msvc'] = env['CC'] == 'cl'
157 env['suncc'] = env['platform'] == 'sunos' and os.path.basename(env['CC']) == 'cc'
158 env['clang'] = 'clang' in cc_version_words
159 env['icc'] = 'icc' == os.path.basename(env['CC'])
160
161 if env['msvc'] and env['toolchain'] == 'default' and env['machine'] == 'x86_64':
162 # MSVC x64 support is broken in earlier versions of scons
163 env.EnsurePythonVersion(2, 0)
164
165 # shortcuts
166 machine = env['machine']
167 platform = env['platform']
168 x86 = env['machine'] == 'x86'
169 ppc = env['machine'] == 'ppc'
170 gcc_compat = env['gcc'] or env['clang']
171 msvc = env['msvc']
172 suncc = env['suncc']
173 icc = env['icc']
174
175 # Determine whether we are cross compiling; in particular, whether we need
176 # to compile code generators with a different compiler as the target code.
177 host_platform = _platform.system().lower()
178 if host_platform.startswith('cygwin'):
179 host_platform = 'cygwin'
180 host_machine = os.environ.get('PROCESSOR_ARCHITEW6432', os.environ.get('PROCESSOR_ARCHITECTURE', _platform.machine()))
181 host_machine = {
182 'x86': 'x86',
183 'i386': 'x86',
184 'i486': 'x86',
185 'i586': 'x86',
186 'i686': 'x86',
187 'ppc' : 'ppc',
188 'AMD64': 'x86_64',
189 'x86_64': 'x86_64',
190 }.get(host_machine, 'generic')
191 env['crosscompile'] = platform != host_platform
192 if machine == 'x86_64' and host_machine != 'x86_64':
193 env['crosscompile'] = True
194 env['hostonly'] = False
195
196 # Backwards compatability with the debug= profile= options
197 if env['build'] == 'debug':
198 if not env['debug']:
199 print 'scons: warning: debug option is deprecated and will be removed eventually; use instead'
200 print
201 print ' scons build=release'
202 print
203 env['build'] = 'release'
204 if env['profile']:
205 print 'scons: warning: profile option is deprecated and will be removed eventually; use instead'
206 print
207 print ' scons build=profile'
208 print
209 env['build'] = 'profile'
210 if False:
211 # Enforce SConscripts to use the new build variable
212 env.popitem('debug')
213 env.popitem('profile')
214 else:
215 # Backwards portability with older sconscripts
216 if env['build'] in ('debug', 'checked'):
217 env['debug'] = True
218 env['profile'] = False
219 if env['build'] == 'profile':
220 env['debug'] = False
221 env['profile'] = True
222 if env['build'] == 'release':
223 env['debug'] = False
224 env['profile'] = False
225
226 # Put build output in a separate dir, which depends on the current
227 # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
228 build_topdir = 'build'
229 build_subdir = env['platform']
230 if env['embedded']:
231 build_subdir = 'embedded-' + build_subdir
232 if env['machine'] != 'generic':
233 build_subdir += '-' + env['machine']
234 if env['build'] != 'release':
235 build_subdir += '-' + env['build']
236 build_dir = os.path.join(build_topdir, build_subdir)
237 # Place the .sconsign file in the build dir too, to avoid issues with
238 # different scons versions building the same source file
239 env['build_dir'] = build_dir
240 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
241 if 'SCONS_CACHE_DIR' in os.environ:
242 print 'scons: Using build cache in %s.' % (os.environ['SCONS_CACHE_DIR'],)
243 env.CacheDir(os.environ['SCONS_CACHE_DIR'])
244 env['CONFIGUREDIR'] = os.path.join(build_dir, 'conf')
245 env['CONFIGURELOG'] = os.path.join(os.path.abspath(build_dir), 'config.log')
246
247 # Parallel build
248 if env.GetOption('num_jobs') <= 1:
249 env.SetOption('num_jobs', num_jobs())
250
251 env.Decider('MD5-timestamp')
252 env.SetOption('max_drift', 60)
253
254 # C preprocessor options
255 cppdefines = []
256 if env['build'] in ('debug', 'checked'):
257 cppdefines += ['DEBUG']
258 else:
259 cppdefines += ['NDEBUG']
260 if env['build'] == 'profile':
261 cppdefines += ['PROFILE']
262 if env['platform'] in ('posix', 'linux', 'freebsd', 'darwin'):
263 cppdefines += [
264 '_POSIX_SOURCE',
265 ('_POSIX_C_SOURCE', '199309L'),
266 '_SVID_SOURCE',
267 '_BSD_SOURCE',
268 '_GNU_SOURCE',
269 'HAVE_PTHREAD',
270 'HAVE_POSIX_MEMALIGN',
271 ]
272 if env['platform'] == 'darwin':
273 cppdefines += [
274 '_DARWIN_C_SOURCE',
275 'GLX_USE_APPLEGL',
276 'GLX_DIRECT_RENDERING',
277 ]
278 else:
279 cppdefines += [
280 'GLX_DIRECT_RENDERING',
281 'GLX_INDIRECT_RENDERING',
282 ]
283 if env['platform'] in ('linux', 'freebsd'):
284 cppdefines += ['HAVE_ALIAS']
285 else:
286 cppdefines += ['GLX_ALIAS_UNSUPPORTED']
287 if env['platform'] == 'haiku':
288 cppdefines += [
289 'HAVE_PTHREAD',
290 'HAVE_POSIX_MEMALIGN'
291 ]
292 if platform == 'windows':
293 cppdefines += [
294 'WIN32',
295 '_WINDOWS',
296 #'_UNICODE',
297 #'UNICODE',
298 # http://msdn.microsoft.com/en-us/library/aa383745.aspx
299 ('_WIN32_WINNT', '0x0601'),
300 ('WINVER', '0x0601'),
301 ]
302 if gcc_compat:
303 cppdefines += [('__MSVCRT_VERSION__', '0x0700')]
304 if msvc:
305 cppdefines += [
306 'VC_EXTRALEAN',
307 '_USE_MATH_DEFINES',
308 '_CRT_SECURE_NO_WARNINGS',
309 '_CRT_SECURE_NO_DEPRECATE',
310 '_SCL_SECURE_NO_WARNINGS',
311 '_SCL_SECURE_NO_DEPRECATE',
312 '_ALLOW_KEYWORD_MACROS',
313 ]
314 if env['build'] in ('debug', 'checked'):
315 cppdefines += ['_DEBUG']
316 if platform == 'windows':
317 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
318 if env['embedded']:
319 cppdefines += ['PIPE_SUBSYSTEM_EMBEDDED']
320 if env['texture_float']:
321 print 'warning: Floating-point textures enabled.'
322 print 'warning: Please consult docs/patents.txt with your lawyer before building Mesa.'
323 cppdefines += ['TEXTURE_FLOAT_ENABLED']
324 env.Append(CPPDEFINES = cppdefines)
325
326 # C compiler options
327 cflags = [] # C
328 cxxflags = [] # C++
329 ccflags = [] # C & C++
330 if gcc_compat:
331 ccversion = env['CCVERSION']
332 if env['build'] == 'debug':
333 ccflags += ['-O0']
334 elif env['gcc'] and ccversion.startswith('4.2.'):
335 # gcc 4.2.x optimizer is broken
336 print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
337 ccflags += ['-O0']
338 else:
339 ccflags += ['-O3']
340 if env['gcc']:
341 # gcc's builtin memcmp is slower than glibc's
342 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
343 ccflags += ['-fno-builtin-memcmp']
344 # Work around aliasing bugs - developers should comment this out
345 ccflags += ['-fno-strict-aliasing']
346 ccflags += ['-g']
347 if env['build'] in ('checked', 'profile'):
348 # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
349 ccflags += [
350 '-fno-omit-frame-pointer',
351 ]
352 if env['gcc']:
353 ccflags += ['-fno-optimize-sibling-calls']
354 if env['machine'] == 'x86':
355 ccflags += [
356 '-m32',
357 #'-march=pentium4',
358 ]
359 if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2') \
360 and (platform != 'windows' or env['build'] == 'debug' or True) \
361 and platform != 'haiku':
362 # NOTE: We need to ensure stack is realigned given that we
363 # produce shared objects, and have no control over the stack
364 # alignment policy of the application. Therefore we need
365 # -mstackrealign ore -mincoming-stack-boundary=2.
366 #
367 # XXX: -O and -mstackrealign causes stack corruption on MinGW
368 #
369 # XXX: We could have SSE without -mstackrealign if we always used
370 # __attribute__((force_align_arg_pointer)), but that's not
371 # always the case.
372 ccflags += [
373 '-mstackrealign', # ensure stack is aligned
374 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
375 #'-mfpmath=sse',
376 ]
377 if platform in ['windows', 'darwin']:
378 # Workaround http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37216
379 ccflags += ['-fno-common']
380 if platform in ['haiku']:
381 # Make optimizations compatible with Pentium or higher on Haiku
382 ccflags += [
383 '-mstackrealign', # ensure stack is aligned
384 '-march=i586', # Haiku target is Pentium
385 '-mtune=i686' # use i686 where we can
386 ]
387 if env['machine'] == 'x86_64':
388 ccflags += ['-m64']
389 if platform == 'darwin':
390 ccflags += ['-fno-common']
391 if env['platform'] not in ('cygwin', 'haiku', 'windows'):
392 ccflags += ['-fvisibility=hidden']
393 # See also:
394 # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
395 ccflags += [
396 '-Wall',
397 '-Wno-long-long',
398 '-fmessage-length=0', # be nice to Eclipse
399 ]
400 cflags += [
401 '-Wmissing-prototypes',
402 '-std=gnu99',
403 ]
404 if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2'):
405 ccflags += [
406 '-Wpointer-arith',
407 ]
408 cflags += [
409 '-Wdeclaration-after-statement',
410 ]
411 if icc:
412 cflags += [
413 '-std=gnu99',
414 ]
415 if msvc:
416 # See also:
417 # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
418 # - cl /?
419 if env['build'] == 'debug':
420 ccflags += [
421 '/Od', # disable optimizations
422 '/Oi', # enable intrinsic functions
423 ]
424 else:
425 if 'MSVC_VERSION' in env and distutils.version.LooseVersion(env['MSVC_VERSION']) < distutils.version.LooseVersion('11.0'):
426 print 'scons: warning: Visual Studio versions prior to 2012 are known to produce incorrect code when optimizations are enabled ( https://bugs.freedesktop.org/show_bug.cgi?id=58718 )'
427 ccflags += [
428 '/O2', # optimize for speed
429 ]
430 if env['build'] == 'release':
431 ccflags += [
432 '/GL', # enable whole program optimization
433 ]
434 else:
435 ccflags += [
436 '/Oy-', # disable frame pointer omission
437 '/GL-', # disable whole program optimization
438 ]
439 ccflags += [
440 '/W3', # warning level
441 #'/Wp64', # enable 64 bit porting warnings
442 '/wd4996', # disable deprecated POSIX name warnings
443 ]
444 if env['machine'] == 'x86':
445 ccflags += [
446 #'/arch:SSE2', # use the SSE2 instructions
447 ]
448 if platform == 'windows':
449 ccflags += [
450 # TODO
451 ]
452 # Automatic pdb generation
453 # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
454 env.EnsureSConsVersion(0, 98, 0)
455 env['PDB'] = '${TARGET.base}.pdb'
456 env.Append(CCFLAGS = ccflags)
457 env.Append(CFLAGS = cflags)
458 env.Append(CXXFLAGS = cxxflags)
459
460 if env['platform'] == 'windows' and msvc:
461 # Choose the appropriate MSVC CRT
462 # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
463 if env['build'] in ('debug', 'checked'):
464 env.Append(CCFLAGS = ['/MTd'])
465 env.Append(SHCCFLAGS = ['/LDd'])
466 else:
467 env.Append(CCFLAGS = ['/MT'])
468 env.Append(SHCCFLAGS = ['/LD'])
469
470 # Assembler options
471 if gcc_compat:
472 if env['machine'] == 'x86':
473 env.Append(ASFLAGS = ['-m32'])
474 if env['machine'] == 'x86_64':
475 env.Append(ASFLAGS = ['-m64'])
476
477 # Linker options
478 linkflags = []
479 shlinkflags = []
480 if gcc_compat:
481 if env['machine'] == 'x86':
482 linkflags += ['-m32']
483 if env['machine'] == 'x86_64':
484 linkflags += ['-m64']
485 if env['platform'] not in ('darwin'):
486 shlinkflags += [
487 '-Wl,-Bsymbolic',
488 ]
489 # Handle circular dependencies in the libraries
490 if env['platform'] in ('darwin'):
491 pass
492 else:
493 env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
494 if env['platform'] == 'windows':
495 # Avoid depending on gcc runtime DLLs
496 linkflags += ['-static-libgcc']
497 if 'w64' in env['CC'].split('-'):
498 linkflags += ['-static-libstdc++']
499 # Handle the @xx symbol munging of DLL exports
500 shlinkflags += ['-Wl,--enable-stdcall-fixup']
501 #shlinkflags += ['-Wl,--kill-at']
502 if msvc:
503 if env['build'] == 'release':
504 # enable Link-time Code Generation
505 linkflags += ['/LTCG']
506 env.Append(ARFLAGS = ['/LTCG'])
507 if platform == 'windows' and msvc:
508 # See also:
509 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
510 linkflags += [
511 '/fixed:no',
512 '/incremental:no',
513 ]
514 env.Append(LINKFLAGS = linkflags)
515 env.Append(SHLINKFLAGS = shlinkflags)
516
517 # We have C++ in several libraries, so always link with the C++ compiler
518 if gcc_compat:
519 env['LINK'] = env['CXX']
520
521 # Default libs
522 libs = []
523 if env['platform'] in ('darwin', 'freebsd', 'linux', 'posix', 'sunos'):
524 libs += ['m', 'pthread', 'dl']
525 if env['platform'] in ('linux',):
526 libs += ['rt']
527 if env['platform'] in ('haiku'):
528 libs += ['root', 'be', 'network', 'translation']
529 env.Append(LIBS = libs)
530
531 # OpenMP
532 if env['openmp']:
533 if env['msvc']:
534 env.Append(CCFLAGS = ['/openmp'])
535 # When building openmp release VS2008 link.exe crashes with LNK1103 error.
536 # Workaround: overwrite PDB flags with empty value as it isn't required anyways
537 if env['build'] == 'release':
538 env['PDB'] = ''
539 if env['gcc']:
540 env.Append(CCFLAGS = ['-fopenmp'])
541 env.Append(LIBS = ['gomp'])
542
543 # Load tools
544 env.Tool('lex')
545 env.Tool('yacc')
546 if env['llvm']:
547 env.Tool('llvm')
548
549 # Custom builders and methods
550 env.Tool('custom')
551 createInstallMethods(env)
552
553 env.PkgCheckModules('X11', ['x11', 'xext', 'xdamage', 'xfixes'])
554 env.PkgCheckModules('XCB', ['x11-xcb', 'xcb-glx >= 1.8.1'])
555 env.PkgCheckModules('XF86VIDMODE', ['xxf86vm'])
556 env.PkgCheckModules('DRM', ['libdrm >= 2.4.38'])
557 env.PkgCheckModules('DRM_INTEL', ['libdrm_intel >= 2.4.52'])
558 env.PkgCheckModules('UDEV', ['libudev >= 151'])
559
560 env['dri'] = env['x11'] and env['drm']
561
562 # for debugging
563 #print env.Dump()
564
565
566 def exists(env):
567 return 1