gallium.py revision 848b8605
1848b8605Smrg"""gallium
2848b8605Smrg
3848b8605SmrgFrontend-tool for Gallium3D architecture.
4848b8605Smrg
5848b8605Smrg"""
6848b8605Smrg
7848b8605Smrg#
8848b8605Smrg# Copyright 2008 VMware, Inc.
9848b8605Smrg# All Rights Reserved.
10848b8605Smrg#
11848b8605Smrg# Permission is hereby granted, free of charge, to any person obtaining a
12848b8605Smrg# copy of this software and associated documentation files (the
13848b8605Smrg# "Software"), to deal in the Software without restriction, including
14848b8605Smrg# without limitation the rights to use, copy, modify, merge, publish,
15848b8605Smrg# distribute, sub license, and/or sell copies of the Software, and to
16848b8605Smrg# permit persons to whom the Software is furnished to do so, subject to
17848b8605Smrg# the following conditions:
18848b8605Smrg#
19848b8605Smrg# The above copyright notice and this permission notice (including the
20848b8605Smrg# next paragraph) shall be included in all copies or substantial portions
21848b8605Smrg# of the Software.
22848b8605Smrg#
23848b8605Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24848b8605Smrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25848b8605Smrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
26848b8605Smrg# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
27848b8605Smrg# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28848b8605Smrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29848b8605Smrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30848b8605Smrg#
31848b8605Smrg
32848b8605Smrg
33848b8605Smrgimport distutils.version
34848b8605Smrgimport os
35848b8605Smrgimport os.path
36848b8605Smrgimport re
37848b8605Smrgimport subprocess
38848b8605Smrgimport platform as _platform
39848b8605Smrgimport sys
40848b8605Smrgimport tempfile
41848b8605Smrg
42848b8605Smrgimport SCons.Action
43848b8605Smrgimport SCons.Builder
44848b8605Smrgimport SCons.Scanner
45848b8605Smrg
46848b8605Smrg
47848b8605Smrgdef symlink(target, source, env):
48848b8605Smrg    target = str(target[0])
49848b8605Smrg    source = str(source[0])
50848b8605Smrg    if os.path.islink(target) or os.path.exists(target):
51848b8605Smrg        os.remove(target)
52848b8605Smrg    os.symlink(os.path.basename(source), target)
53848b8605Smrg
54848b8605Smrgdef install(env, source, subdir):
55848b8605Smrg    target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'], subdir)
56848b8605Smrg    return env.Install(target_dir, source)
57848b8605Smrg
58848b8605Smrgdef install_program(env, source):
59848b8605Smrg    return install(env, source, 'bin')
60848b8605Smrg
61848b8605Smrgdef install_shared_library(env, sources, version = ()):
62848b8605Smrg    targets = []
63848b8605Smrg    install_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'])
64848b8605Smrg    version = tuple(map(str, version))
65848b8605Smrg    if env['SHLIBSUFFIX'] == '.dll':
66848b8605Smrg        dlls = env.FindIxes(sources, 'SHLIBPREFIX', 'SHLIBSUFFIX')
67848b8605Smrg        targets += install(env, dlls, 'bin')
68848b8605Smrg        libs = env.FindIxes(sources, 'LIBPREFIX', 'LIBSUFFIX')
69848b8605Smrg        targets += install(env, libs, 'lib')
70848b8605Smrg    else:
71848b8605Smrg        for source in sources:
72848b8605Smrg            target_dir =  os.path.join(install_dir, 'lib')
73848b8605Smrg            target_name = '.'.join((str(source),) + version)
74848b8605Smrg            last = env.InstallAs(os.path.join(target_dir, target_name), source)
75848b8605Smrg            targets += last
76848b8605Smrg            while len(version):
77848b8605Smrg                version = version[:-1]
78848b8605Smrg                target_name = '.'.join((str(source),) + version)
79848b8605Smrg                action = SCons.Action.Action(symlink, "  Symlinking $TARGET ...")
80848b8605Smrg                last = env.Command(os.path.join(target_dir, target_name), last, action)
81848b8605Smrg                targets += last
82848b8605Smrg    return targets
83848b8605Smrg
84848b8605Smrg
85848b8605Smrgdef createInstallMethods(env):
86848b8605Smrg    env.AddMethod(install_program, 'InstallProgram')
87848b8605Smrg    env.AddMethod(install_shared_library, 'InstallSharedLibrary')
88848b8605Smrg
89848b8605Smrg
90848b8605Smrgdef num_jobs():
91848b8605Smrg    try:
92848b8605Smrg        return int(os.environ['NUMBER_OF_PROCESSORS'])
93848b8605Smrg    except (ValueError, KeyError):
94848b8605Smrg        pass
95848b8605Smrg
96848b8605Smrg    try:
97848b8605Smrg        return os.sysconf('SC_NPROCESSORS_ONLN')
98848b8605Smrg    except (ValueError, OSError, AttributeError):
99848b8605Smrg        pass
100848b8605Smrg
101848b8605Smrg    try:
102848b8605Smrg        return int(os.popen2("sysctl -n hw.ncpu")[1].read())
103848b8605Smrg    except ValueError:
104848b8605Smrg        pass
105848b8605Smrg
106848b8605Smrg    return 1
107848b8605Smrg
108848b8605Smrg
109848b8605Smrgdef check_cc(env, cc, expr, cpp_opt = '-E'):
110848b8605Smrg    # Invoke C-preprocessor to determine whether the specified expression is
111848b8605Smrg    # true or not.
112848b8605Smrg
113848b8605Smrg    sys.stdout.write('Checking for %s ... ' % cc)
114848b8605Smrg
115848b8605Smrg    source = tempfile.NamedTemporaryFile(suffix='.c', delete=False)
116848b8605Smrg    source.write('#if !(%s)\n#error\n#endif\n' % expr)
117848b8605Smrg    source.close()
118848b8605Smrg
119848b8605Smrg    pipe = SCons.Action._subproc(env, [env['CC'], cpp_opt, source.name],
120848b8605Smrg                                 stdin = 'devnull',
121848b8605Smrg                                 stderr = 'devnull',
122848b8605Smrg                                 stdout = 'devnull')
123848b8605Smrg    result = pipe.wait() == 0
124848b8605Smrg
125848b8605Smrg    os.unlink(source.name)
126848b8605Smrg
127848b8605Smrg    sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
128848b8605Smrg    return result
129848b8605Smrg
130848b8605Smrg
131848b8605Smrgdef generate(env):
132848b8605Smrg    """Common environment generation code"""
133848b8605Smrg
134848b8605Smrg    # Tell tools which machine to compile for
135848b8605Smrg    env['TARGET_ARCH'] = env['machine']
136848b8605Smrg    env['MSVS_ARCH'] = env['machine']
137848b8605Smrg
138848b8605Smrg    # Toolchain
139848b8605Smrg    platform = env['platform']
140848b8605Smrg    env.Tool(env['toolchain'])
141848b8605Smrg
142848b8605Smrg    # Allow override compiler and specify additional flags from environment
143848b8605Smrg    if os.environ.has_key('CC'):
144848b8605Smrg        env['CC'] = os.environ['CC']
145848b8605Smrg        # Update CCVERSION to match
146848b8605Smrg        pipe = SCons.Action._subproc(env, [env['CC'], '--version'],
147848b8605Smrg                                     stdin = 'devnull',
148848b8605Smrg                                     stderr = 'devnull',
149848b8605Smrg                                     stdout = subprocess.PIPE)
150848b8605Smrg        if pipe.wait() == 0:
151848b8605Smrg            line = pipe.stdout.readline()
152848b8605Smrg            match = re.search(r'[0-9]+(\.[0-9]+)+', line)
153848b8605Smrg            if match:
154848b8605Smrg                env['CCVERSION'] = match.group(0)
155848b8605Smrg    if os.environ.has_key('CFLAGS'):
156848b8605Smrg        env['CCFLAGS'] += SCons.Util.CLVar(os.environ['CFLAGS'])
157848b8605Smrg    if os.environ.has_key('CXX'):
158848b8605Smrg        env['CXX'] = os.environ['CXX']
159848b8605Smrg    if os.environ.has_key('CXXFLAGS'):
160848b8605Smrg        env['CXXFLAGS'] += SCons.Util.CLVar(os.environ['CXXFLAGS'])
161848b8605Smrg    if os.environ.has_key('LDFLAGS'):
162848b8605Smrg        env['LINKFLAGS'] += SCons.Util.CLVar(os.environ['LDFLAGS'])
163848b8605Smrg
164848b8605Smrg    # Detect gcc/clang not by executable name, but through pre-defined macros
165848b8605Smrg    # as autoconf does, to avoid drawing wrong conclusions when using tools
166848b8605Smrg    # that overrice CC/CXX like scan-build.
167848b8605Smrg    env['gcc'] = 0
168848b8605Smrg    env['clang'] = 0
169848b8605Smrg    env['msvc'] = 0
170848b8605Smrg    if _platform.system() == 'Windows':
171848b8605Smrg        env['msvc'] = check_cc(env, 'MSVC', 'defined(_MSC_VER)', '/E')
172848b8605Smrg    if not env['msvc']:
173848b8605Smrg        env['gcc'] = check_cc(env, 'GCC', 'defined(__GNUC__) && !defined(__clang__)')
174848b8605Smrg        env['clang'] = check_cc(env, 'Clang', '__clang__')
175848b8605Smrg    env['suncc'] = env['platform'] == 'sunos' and os.path.basename(env['CC']) == 'cc'
176848b8605Smrg    env['icc'] = 'icc' == os.path.basename(env['CC'])
177848b8605Smrg
178848b8605Smrg    if env['msvc'] and env['toolchain'] == 'default' and env['machine'] == 'x86_64':
179848b8605Smrg        # MSVC x64 support is broken in earlier versions of scons
180848b8605Smrg        env.EnsurePythonVersion(2, 0)
181848b8605Smrg
182848b8605Smrg    # shortcuts
183848b8605Smrg    machine = env['machine']
184848b8605Smrg    platform = env['platform']
185848b8605Smrg    x86 = env['machine'] == 'x86'
186848b8605Smrg    ppc = env['machine'] == 'ppc'
187848b8605Smrg    gcc_compat = env['gcc'] or env['clang']
188848b8605Smrg    msvc = env['msvc']
189848b8605Smrg    suncc = env['suncc']
190848b8605Smrg    icc = env['icc']
191848b8605Smrg
192848b8605Smrg    # Determine whether we are cross compiling; in particular, whether we need
193848b8605Smrg    # to compile code generators with a different compiler as the target code.
194848b8605Smrg    host_platform = _platform.system().lower()
195848b8605Smrg    if host_platform.startswith('cygwin'):
196848b8605Smrg        host_platform = 'cygwin'
197848b8605Smrg    host_machine = os.environ.get('PROCESSOR_ARCHITEW6432', os.environ.get('PROCESSOR_ARCHITECTURE', _platform.machine()))
198848b8605Smrg    host_machine = {
199848b8605Smrg        'x86': 'x86',
200848b8605Smrg        'i386': 'x86',
201848b8605Smrg        'i486': 'x86',
202848b8605Smrg        'i586': 'x86',
203848b8605Smrg        'i686': 'x86',
204848b8605Smrg        'ppc' : 'ppc',
205848b8605Smrg        'AMD64': 'x86_64',
206848b8605Smrg        'x86_64': 'x86_64',
207848b8605Smrg    }.get(host_machine, 'generic')
208848b8605Smrg    env['crosscompile'] = platform != host_platform
209848b8605Smrg    if machine == 'x86_64' and host_machine != 'x86_64':
210848b8605Smrg        env['crosscompile'] = True
211848b8605Smrg    env['hostonly'] = False
212848b8605Smrg
213848b8605Smrg    # Backwards compatability with the debug= profile= options
214848b8605Smrg    if env['build'] == 'debug':
215848b8605Smrg        if not env['debug']:
216848b8605Smrg            print 'scons: warning: debug option is deprecated and will be removed eventually; use instead'
217848b8605Smrg            print
218848b8605Smrg            print ' scons build=release'
219848b8605Smrg            print
220848b8605Smrg            env['build'] = 'release'
221848b8605Smrg        if env['profile']:
222848b8605Smrg            print 'scons: warning: profile option is deprecated and will be removed eventually; use instead'
223848b8605Smrg            print
224848b8605Smrg            print ' scons build=profile'
225848b8605Smrg            print
226848b8605Smrg            env['build'] = 'profile'
227848b8605Smrg    if False:
228848b8605Smrg        # Enforce SConscripts to use the new build variable
229848b8605Smrg        env.popitem('debug')
230848b8605Smrg        env.popitem('profile')
231848b8605Smrg    else:
232848b8605Smrg        # Backwards portability with older sconscripts
233848b8605Smrg        if env['build'] in ('debug', 'checked'):
234848b8605Smrg            env['debug'] = True
235848b8605Smrg            env['profile'] = False
236848b8605Smrg        if env['build'] == 'profile':
237848b8605Smrg            env['debug'] = False
238848b8605Smrg            env['profile'] = True
239848b8605Smrg        if env['build'] == 'release':
240848b8605Smrg            env['debug'] = False
241848b8605Smrg            env['profile'] = False
242848b8605Smrg
243848b8605Smrg    # Put build output in a separate dir, which depends on the current
244848b8605Smrg    # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
245848b8605Smrg    build_topdir = 'build'
246848b8605Smrg    build_subdir = env['platform']
247848b8605Smrg    if env['embedded']:
248848b8605Smrg        build_subdir =  'embedded-' + build_subdir
249848b8605Smrg    if env['machine'] != 'generic':
250848b8605Smrg        build_subdir += '-' + env['machine']
251848b8605Smrg    if env['build'] != 'release':
252848b8605Smrg        build_subdir += '-' +  env['build']
253848b8605Smrg    build_dir = os.path.join(build_topdir, build_subdir)
254848b8605Smrg    # Place the .sconsign file in the build dir too, to avoid issues with
255848b8605Smrg    # different scons versions building the same source file
256848b8605Smrg    env['build_dir'] = build_dir
257848b8605Smrg    env.SConsignFile(os.path.join(build_dir, '.sconsign'))
258848b8605Smrg    if 'SCONS_CACHE_DIR' in os.environ:
259848b8605Smrg        print 'scons: Using build cache in %s.' % (os.environ['SCONS_CACHE_DIR'],)
260848b8605Smrg        env.CacheDir(os.environ['SCONS_CACHE_DIR'])
261848b8605Smrg    env['CONFIGUREDIR'] = os.path.join(build_dir, 'conf')
262848b8605Smrg    env['CONFIGURELOG'] = os.path.join(os.path.abspath(build_dir), 'config.log')
263848b8605Smrg
264848b8605Smrg    # Parallel build
265848b8605Smrg    if env.GetOption('num_jobs') <= 1:
266848b8605Smrg        env.SetOption('num_jobs', num_jobs())
267848b8605Smrg
268848b8605Smrg    env.Decider('MD5-timestamp')
269848b8605Smrg    env.SetOption('max_drift', 60)
270848b8605Smrg
271848b8605Smrg    # C preprocessor options
272848b8605Smrg    cppdefines = []
273848b8605Smrg    if env['build'] in ('debug', 'checked'):
274848b8605Smrg        cppdefines += ['DEBUG']
275848b8605Smrg    else:
276848b8605Smrg        cppdefines += ['NDEBUG']
277848b8605Smrg    if env['build'] == 'profile':
278848b8605Smrg        cppdefines += ['PROFILE']
279848b8605Smrg    if env['platform'] in ('posix', 'linux', 'freebsd', 'darwin'):
280848b8605Smrg        cppdefines += [
281848b8605Smrg            '_POSIX_SOURCE',
282848b8605Smrg            ('_POSIX_C_SOURCE', '199309L'),
283848b8605Smrg            '_SVID_SOURCE',
284848b8605Smrg            '_BSD_SOURCE',
285848b8605Smrg            '_GNU_SOURCE',
286848b8605Smrg            'HAVE_PTHREAD',
287848b8605Smrg            'HAVE_POSIX_MEMALIGN',
288848b8605Smrg        ]
289848b8605Smrg        if env['platform'] == 'darwin':
290848b8605Smrg            cppdefines += [
291848b8605Smrg                '_DARWIN_C_SOURCE',
292848b8605Smrg                'GLX_USE_APPLEGL',
293848b8605Smrg                'GLX_DIRECT_RENDERING',
294848b8605Smrg            ]
295848b8605Smrg        else:
296848b8605Smrg            cppdefines += [
297848b8605Smrg                'GLX_DIRECT_RENDERING',
298848b8605Smrg                'GLX_INDIRECT_RENDERING',
299848b8605Smrg            ]
300848b8605Smrg        if env['platform'] in ('linux', 'freebsd'):
301848b8605Smrg            cppdefines += ['HAVE_ALIAS']
302848b8605Smrg        else:
303848b8605Smrg            cppdefines += ['GLX_ALIAS_UNSUPPORTED']
304848b8605Smrg    if env['platform'] == 'haiku':
305848b8605Smrg        cppdefines += [
306848b8605Smrg            'HAVE_PTHREAD',
307848b8605Smrg            'HAVE_POSIX_MEMALIGN'
308848b8605Smrg        ]
309848b8605Smrg    if platform == 'windows':
310848b8605Smrg        cppdefines += [
311848b8605Smrg            'WIN32',
312848b8605Smrg            '_WINDOWS',
313848b8605Smrg            #'_UNICODE',
314848b8605Smrg            #'UNICODE',
315848b8605Smrg            # http://msdn.microsoft.com/en-us/library/aa383745.aspx
316848b8605Smrg            ('_WIN32_WINNT', '0x0601'),
317848b8605Smrg            ('WINVER', '0x0601'),
318848b8605Smrg        ]
319848b8605Smrg        if gcc_compat:
320848b8605Smrg            cppdefines += [('__MSVCRT_VERSION__', '0x0700')]
321848b8605Smrg        if msvc:
322848b8605Smrg            cppdefines += [
323848b8605Smrg                'VC_EXTRALEAN',
324848b8605Smrg                '_USE_MATH_DEFINES',
325848b8605Smrg                '_CRT_SECURE_NO_WARNINGS',
326848b8605Smrg                '_CRT_SECURE_NO_DEPRECATE',
327848b8605Smrg                '_SCL_SECURE_NO_WARNINGS',
328848b8605Smrg                '_SCL_SECURE_NO_DEPRECATE',
329848b8605Smrg                '_ALLOW_KEYWORD_MACROS',
330848b8605Smrg            ]
331848b8605Smrg        if env['build'] in ('debug', 'checked'):
332848b8605Smrg            cppdefines += ['_DEBUG']
333848b8605Smrg    if platform == 'windows':
334848b8605Smrg        cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
335848b8605Smrg    if env['embedded']:
336848b8605Smrg        cppdefines += ['PIPE_SUBSYSTEM_EMBEDDED']
337848b8605Smrg    if env['texture_float']:
338848b8605Smrg        print 'warning: Floating-point textures enabled.'
339848b8605Smrg        print 'warning: Please consult docs/patents.txt with your lawyer before building Mesa.'
340848b8605Smrg        cppdefines += ['TEXTURE_FLOAT_ENABLED']
341848b8605Smrg    env.Append(CPPDEFINES = cppdefines)
342848b8605Smrg
343848b8605Smrg    # C compiler options
344848b8605Smrg    cflags = [] # C
345848b8605Smrg    cxxflags = [] # C++
346848b8605Smrg    ccflags = [] # C & C++
347848b8605Smrg    if gcc_compat:
348848b8605Smrg        ccversion = env['CCVERSION']
349848b8605Smrg        if env['build'] == 'debug':
350848b8605Smrg            ccflags += ['-O0']
351848b8605Smrg        elif env['gcc'] and ccversion.startswith('4.2.'):
352848b8605Smrg            # gcc 4.2.x optimizer is broken
353848b8605Smrg            print "warning: gcc 4.2.x optimizer is broken -- disabling optimizations"
354848b8605Smrg            ccflags += ['-O0']
355848b8605Smrg        else:
356848b8605Smrg            ccflags += ['-O3']
357848b8605Smrg        if env['gcc']:
358848b8605Smrg            # gcc's builtin memcmp is slower than glibc's
359848b8605Smrg            # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
360848b8605Smrg            ccflags += ['-fno-builtin-memcmp']
361848b8605Smrg        # Work around aliasing bugs - developers should comment this out
362848b8605Smrg        ccflags += ['-fno-strict-aliasing']
363848b8605Smrg        ccflags += ['-g']
364848b8605Smrg        if env['build'] in ('checked', 'profile'):
365848b8605Smrg            # See http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Which_options_should_I_pass_to_gcc_when_compiling_for_profiling?
366848b8605Smrg            ccflags += [
367848b8605Smrg                '-fno-omit-frame-pointer',
368848b8605Smrg            ]
369848b8605Smrg            if env['gcc']:
370848b8605Smrg                ccflags += ['-fno-optimize-sibling-calls']
371848b8605Smrg        if env['machine'] == 'x86':
372848b8605Smrg            ccflags += [
373848b8605Smrg                '-m32',
374848b8605Smrg                #'-march=pentium4',
375848b8605Smrg            ]
376848b8605Smrg            if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2') \
377848b8605Smrg               and (platform != 'windows' or env['build'] == 'debug' or True) \
378848b8605Smrg               and platform != 'haiku':
379848b8605Smrg                # NOTE: We need to ensure stack is realigned given that we
380848b8605Smrg                # produce shared objects, and have no control over the stack
381848b8605Smrg                # alignment policy of the application. Therefore we need
382848b8605Smrg                # -mstackrealign ore -mincoming-stack-boundary=2.
383848b8605Smrg                #
384848b8605Smrg                # XXX: -O and -mstackrealign causes stack corruption on MinGW
385848b8605Smrg                #
386848b8605Smrg                # XXX: We could have SSE without -mstackrealign if we always used
387848b8605Smrg                # __attribute__((force_align_arg_pointer)), but that's not
388848b8605Smrg                # always the case.
389848b8605Smrg                ccflags += [
390848b8605Smrg                    '-mstackrealign', # ensure stack is aligned
391848b8605Smrg                    '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
392848b8605Smrg                    #'-mfpmath=sse',
393848b8605Smrg                ]
394848b8605Smrg            if platform in ['windows', 'darwin']:
395848b8605Smrg                # Workaround http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37216
396848b8605Smrg                ccflags += ['-fno-common']
397848b8605Smrg            if platform in ['haiku']:
398848b8605Smrg                # Make optimizations compatible with Pentium or higher on Haiku
399848b8605Smrg                ccflags += [
400848b8605Smrg                    '-mstackrealign', # ensure stack is aligned
401848b8605Smrg                    '-march=i586', # Haiku target is Pentium
402848b8605Smrg                    '-mtune=i686' # use i686 where we can
403848b8605Smrg                ]
404848b8605Smrg        if env['machine'] == 'x86_64':
405848b8605Smrg            ccflags += ['-m64']
406848b8605Smrg            if platform == 'darwin':
407848b8605Smrg                ccflags += ['-fno-common']
408848b8605Smrg        if env['platform'] not in ('cygwin', 'haiku', 'windows'):
409848b8605Smrg            ccflags += ['-fvisibility=hidden']
410848b8605Smrg        # See also:
411848b8605Smrg        # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
412848b8605Smrg        ccflags += [
413848b8605Smrg            '-Wall',
414848b8605Smrg            '-Wno-long-long',
415848b8605Smrg            '-fmessage-length=0', # be nice to Eclipse
416848b8605Smrg        ]
417848b8605Smrg        cflags += [
418848b8605Smrg            '-Wmissing-prototypes',
419848b8605Smrg            '-std=gnu99',
420848b8605Smrg        ]
421848b8605Smrg        if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2'):
422848b8605Smrg            ccflags += [
423848b8605Smrg                '-Wpointer-arith',
424848b8605Smrg            ]
425848b8605Smrg            cflags += [
426848b8605Smrg                '-Wdeclaration-after-statement',
427848b8605Smrg            ]
428848b8605Smrg    if icc:
429848b8605Smrg        cflags += [
430848b8605Smrg            '-std=gnu99',
431848b8605Smrg        ]
432848b8605Smrg    if msvc:
433848b8605Smrg        # See also:
434848b8605Smrg        # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
435848b8605Smrg        # - cl /?
436848b8605Smrg        if 'MSVC_VERSION' not in env or distutils.version.LooseVersion(env['MSVC_VERSION']) < distutils.version.LooseVersion('12.0'):
437848b8605Smrg            # Use bundled stdbool.h and stdint.h headers for older MSVC
438848b8605Smrg            # versions.  stdint.h was introduced in MSVC 2010, but stdbool.h
439848b8605Smrg            # was only introduced in MSVC 2013.
440848b8605Smrg            top_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
441848b8605Smrg            env.Append(CPPPATH = [os.path.join(top_dir, 'include/c99')])
442848b8605Smrg        if env['build'] == 'debug':
443848b8605Smrg            ccflags += [
444848b8605Smrg              '/Od', # disable optimizations
445848b8605Smrg              '/Oi', # enable intrinsic functions
446848b8605Smrg            ]
447848b8605Smrg        else:
448848b8605Smrg            if 'MSVC_VERSION' in env and distutils.version.LooseVersion(env['MSVC_VERSION']) < distutils.version.LooseVersion('11.0'):
449848b8605Smrg                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 )'
450848b8605Smrg            ccflags += [
451848b8605Smrg                '/O2', # optimize for speed
452848b8605Smrg            ]
453848b8605Smrg        if env['build'] == 'release':
454848b8605Smrg            ccflags += [
455848b8605Smrg                '/GL', # enable whole program optimization
456848b8605Smrg            ]
457848b8605Smrg        else:
458848b8605Smrg            ccflags += [
459848b8605Smrg                '/Oy-', # disable frame pointer omission
460848b8605Smrg                '/GL-', # disable whole program optimization
461848b8605Smrg            ]
462848b8605Smrg        ccflags += [
463848b8605Smrg            '/W3', # warning level
464848b8605Smrg            '/wd4244', # conversion from 'type1' to 'type2', possible loss of data
465848b8605Smrg            '/wd4305', # truncation from 'type1' to 'type2'
466848b8605Smrg            '/wd4800', # forcing value to bool 'true' or 'false' (performance warning)
467848b8605Smrg            '/wd4996', # disable deprecated POSIX name warnings
468848b8605Smrg        ]
469848b8605Smrg        if env['machine'] == 'x86':
470848b8605Smrg            ccflags += [
471848b8605Smrg                #'/arch:SSE2', # use the SSE2 instructions
472848b8605Smrg            ]
473848b8605Smrg        if platform == 'windows':
474848b8605Smrg            ccflags += [
475848b8605Smrg                # TODO
476848b8605Smrg            ]
477848b8605Smrg        # Automatic pdb generation
478848b8605Smrg        # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
479848b8605Smrg        env.EnsureSConsVersion(0, 98, 0)
480848b8605Smrg        env['PDB'] = '${TARGET.base}.pdb'
481848b8605Smrg    env.Append(CCFLAGS = ccflags)
482848b8605Smrg    env.Append(CFLAGS = cflags)
483848b8605Smrg    env.Append(CXXFLAGS = cxxflags)
484848b8605Smrg
485848b8605Smrg    if env['platform'] == 'windows' and msvc:
486848b8605Smrg        # Choose the appropriate MSVC CRT
487848b8605Smrg        # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
488848b8605Smrg        if env['build'] in ('debug', 'checked'):
489848b8605Smrg            env.Append(CCFLAGS = ['/MTd'])
490848b8605Smrg            env.Append(SHCCFLAGS = ['/LDd'])
491848b8605Smrg        else:
492848b8605Smrg            env.Append(CCFLAGS = ['/MT'])
493848b8605Smrg            env.Append(SHCCFLAGS = ['/LD'])
494848b8605Smrg
495848b8605Smrg    # Static code analysis
496848b8605Smrg    if env['analyze']:
497848b8605Smrg        if env['msvc']:
498848b8605Smrg            # http://msdn.microsoft.com/en-us/library/ms173498.aspx
499848b8605Smrg            env.Append(CCFLAGS = [
500848b8605Smrg                '/analyze',
501848b8605Smrg                #'/analyze:log', '${TARGET.base}.xml',
502848b8605Smrg            ])
503848b8605Smrg        if env['clang']:
504848b8605Smrg            # scan-build will produce more comprehensive output
505848b8605Smrg            env.Append(CCFLAGS = ['--analyze'])
506848b8605Smrg
507848b8605Smrg    # Assembler options
508848b8605Smrg    if gcc_compat:
509848b8605Smrg        if env['machine'] == 'x86':
510848b8605Smrg            env.Append(ASFLAGS = ['-m32'])
511848b8605Smrg        if env['machine'] == 'x86_64':
512848b8605Smrg            env.Append(ASFLAGS = ['-m64'])
513848b8605Smrg
514848b8605Smrg    # Linker options
515848b8605Smrg    linkflags = []
516848b8605Smrg    shlinkflags = []
517848b8605Smrg    if gcc_compat:
518848b8605Smrg        if env['machine'] == 'x86':
519848b8605Smrg            linkflags += ['-m32']
520848b8605Smrg        if env['machine'] == 'x86_64':
521848b8605Smrg            linkflags += ['-m64']
522848b8605Smrg        if env['platform'] not in ('darwin'):
523848b8605Smrg            shlinkflags += [
524848b8605Smrg                '-Wl,-Bsymbolic',
525848b8605Smrg            ]
526848b8605Smrg        # Handle circular dependencies in the libraries
527848b8605Smrg        if env['platform'] in ('darwin'):
528848b8605Smrg            pass
529848b8605Smrg        else:
530848b8605Smrg            env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
531848b8605Smrg        if env['platform'] == 'windows':
532848b8605Smrg            # Avoid depending on gcc runtime DLLs
533848b8605Smrg            linkflags += ['-static-libgcc']
534848b8605Smrg            if 'w64' in env['CC'].split('-'):
535848b8605Smrg                linkflags += ['-static-libstdc++']
536848b8605Smrg            # Handle the @xx symbol munging of DLL exports
537848b8605Smrg            shlinkflags += ['-Wl,--enable-stdcall-fixup']
538848b8605Smrg            #shlinkflags += ['-Wl,--kill-at']
539848b8605Smrg    if msvc:
540848b8605Smrg        if env['build'] == 'release':
541848b8605Smrg            # enable Link-time Code Generation
542848b8605Smrg            linkflags += ['/LTCG']
543848b8605Smrg            env.Append(ARFLAGS = ['/LTCG'])
544848b8605Smrg    if platform == 'windows' and msvc:
545848b8605Smrg        # See also:
546848b8605Smrg        # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
547848b8605Smrg        linkflags += [
548848b8605Smrg            '/fixed:no',
549848b8605Smrg            '/incremental:no',
550848b8605Smrg        ]
551848b8605Smrg    env.Append(LINKFLAGS = linkflags)
552848b8605Smrg    env.Append(SHLINKFLAGS = shlinkflags)
553848b8605Smrg
554848b8605Smrg    # We have C++ in several libraries, so always link with the C++ compiler
555848b8605Smrg    if gcc_compat:
556848b8605Smrg        env['LINK'] = env['CXX']
557848b8605Smrg
558848b8605Smrg    # Default libs
559848b8605Smrg    libs = []
560848b8605Smrg    if env['platform'] in ('darwin', 'freebsd', 'linux', 'posix', 'sunos'):
561848b8605Smrg        libs += ['m', 'pthread', 'dl']
562848b8605Smrg    if env['platform'] in ('linux',):
563848b8605Smrg        libs += ['rt']
564848b8605Smrg    if env['platform'] in ('haiku'):
565848b8605Smrg        libs += ['root', 'be', 'network', 'translation']
566848b8605Smrg    env.Append(LIBS = libs)
567848b8605Smrg
568848b8605Smrg    # OpenMP
569848b8605Smrg    if env['openmp']:
570848b8605Smrg        if env['msvc']:
571848b8605Smrg            env.Append(CCFLAGS = ['/openmp'])
572848b8605Smrg            # When building openmp release VS2008 link.exe crashes with LNK1103 error.
573848b8605Smrg            # Workaround: overwrite PDB flags with empty value as it isn't required anyways
574848b8605Smrg            if env['build'] == 'release':
575848b8605Smrg                env['PDB'] = ''
576848b8605Smrg        if env['gcc']:
577848b8605Smrg            env.Append(CCFLAGS = ['-fopenmp'])
578848b8605Smrg            env.Append(LIBS = ['gomp'])
579848b8605Smrg
580848b8605Smrg    # Load tools
581848b8605Smrg    env.Tool('lex')
582848b8605Smrg    env.Tool('yacc')
583848b8605Smrg    if env['llvm']:
584848b8605Smrg        env.Tool('llvm')
585848b8605Smrg
586848b8605Smrg    # Custom builders and methods
587848b8605Smrg    env.Tool('custom')
588848b8605Smrg    createInstallMethods(env)
589848b8605Smrg
590848b8605Smrg    env.PkgCheckModules('X11', ['x11', 'xext', 'xdamage', 'xfixes'])
591848b8605Smrg    env.PkgCheckModules('XCB', ['x11-xcb', 'xcb-glx >= 1.8.1', 'xcb-dri2 >= 1.8'])
592848b8605Smrg    env.PkgCheckModules('XF86VIDMODE', ['xxf86vm'])
593848b8605Smrg    env.PkgCheckModules('DRM', ['libdrm >= 2.4.38'])
594848b8605Smrg    env.PkgCheckModules('UDEV', ['libudev >= 151'])
595848b8605Smrg
596848b8605Smrg    env['dri'] = env['x11'] and env['drm']
597848b8605Smrg
598848b8605Smrg    # for debugging
599848b8605Smrg    #print env.Dump()
600848b8605Smrg
601848b8605Smrg
602848b8605Smrgdef exists(env):
603848b8605Smrg    return 1
604