svga: Don't use more than one constant per IFC instruction.
[mesa.git] / scons / gallium.py
old mode 100644 (file)
new mode 100755 (executable)
index b065b7b..9118257
@@ -35,6 +35,7 @@ import os
 import os.path
 import re
 import subprocess
+import platform as _platform
 
 import SCons.Action
 import SCons.Builder
@@ -50,29 +51,34 @@ def symlink(target, source, env):
 
 def install(env, source, subdir):
     target_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'], subdir)
-    env.Install(target_dir, source)
+    return env.Install(target_dir, source)
 
 def install_program(env, source):
-    install(env, source, 'bin')
+    return install(env, source, 'bin')
 
 def install_shared_library(env, sources, version = ()):
+    targets = []
     install_dir = os.path.join(env.Dir('#.').srcnode().abspath, env['build_dir'])
     version = tuple(map(str, version))
     if env['SHLIBSUFFIX'] == '.dll':
         dlls = env.FindIxes(sources, 'SHLIBPREFIX', 'SHLIBSUFFIX')
-        install(env, dlls, 'bin')
+        targets += install(env, dlls, 'bin')
         libs = env.FindIxes(sources, 'LIBPREFIX', 'LIBSUFFIX')
-        install(env, libs, 'lib')
+        targets += install(env, libs, 'lib')
     else:
         for source in sources:
             target_dir =  os.path.join(install_dir, 'lib')
             target_name = '.'.join((str(source),) + version)
             last = env.InstallAs(os.path.join(target_dir, target_name), source)
+            targets += last
             while len(version):
                 version = version[:-1]
                 target_name = '.'.join((str(source),) + version)
                 action = SCons.Action.Action(symlink, "$TARGET -> $SOURCE")
                 last = env.Command(os.path.join(target_dir, target_name), last, action) 
+                targets += last
+    return targets
+
 
 def createInstallMethods(env):
     env.AddMethod(install_program, 'InstallProgram')
@@ -98,9 +104,48 @@ def num_jobs():
     return 1
 
 
+def pkg_config_modules(env, name, modules):
+    '''Simple wrapper for pkg-config.'''
+
+    env[name] = False
+
+    if env['platform'] == 'windows':
+        return
+
+    if not env.Detect('pkg-config'):
+        return
+
+    if subprocess.call(["pkg-config", "--exists", ' '.join(modules)]) != 0:
+        return
+
+    # Put -I and -L flags directly into the environment, as these don't affect
+    # the compilation of targets that do not use them
+    try:
+        env.ParseConfig('pkg-config --cflags-only-I --libs-only-L ' + ' '.join(modules))
+    except OSError:
+        return
+
+    # Other flags may affect the compilation of unrelated targets, so store
+    # them with a prefix, (e.g., XXX_CFLAGS, XXX_LIBS, etc)
+    try:
+        flags = env.ParseFlags('!pkg-config --cflags-only-other --libs-only-l --libs-only-other ' + ' '.join(modules))
+    except OSError:
+        return
+    prefix = name.upper() + '_'
+    for flag_name, flag_value in flags.iteritems():
+        env[prefix + flag_name] = flag_value
+
+    env[name] = True
+
+
+
 def generate(env):
     """Common environment generation code"""
 
+    # Tell tools which machine to compile for
+    env['TARGET_ARCH'] = env['machine']
+    env['MSVS_ARCH'] = env['machine']
+
     # Toolchain
     platform = env['platform']
     if env['toolchain'] == 'default':
@@ -110,25 +155,35 @@ def generate(env):
             env['toolchain'] = 'wcesdk'
     env.Tool(env['toolchain'])
 
-    if env['platform'] == 'embedded':
-        # Allow overriding compiler from environment
-        if os.environ.has_key('CC'):
-            env['CC'] = os.environ['CC']
-            # Update CCVERSION to match
-            pipe = SCons.Action._subproc(env, [env['CC'], '--version'],
-                                         stdin = 'devnull',
-                                         stderr = 'devnull',
-                                         stdout = subprocess.PIPE)
-            if pipe.wait() == 0:
-                line = pipe.stdout.readline()
-                match = re.search(r'[0-9]+(\.[0-9]+)+', line)
-                if match:
-                    env['CCVERSION'] = match.group(0)
-            
+    # Allow override compiler and specify additional flags from environment
+    if os.environ.has_key('CC'):
+        env['CC'] = os.environ['CC']
+        # Update CCVERSION to match
+        pipe = SCons.Action._subproc(env, [env['CC'], '--version'],
+                                     stdin = 'devnull',
+                                     stderr = 'devnull',
+                                     stdout = subprocess.PIPE)
+        if pipe.wait() == 0:
+            line = pipe.stdout.readline()
+            match = re.search(r'[0-9]+(\.[0-9]+)+', line)
+            if match:
+                env['CCVERSION'] = match.group(0)
+    if os.environ.has_key('CFLAGS'):
+        env['CCFLAGS'] += SCons.Util.CLVar(os.environ['CFLAGS'])
+    if os.environ.has_key('CXX'):
+        env['CXX'] = os.environ['CXX']
+    if os.environ.has_key('CXXFLAGS'):
+        env['CXXFLAGS'] += SCons.Util.CLVar(os.environ['CXXFLAGS'])
+    if os.environ.has_key('LDFLAGS'):
+        env['LINKFLAGS'] += SCons.Util.CLVar(os.environ['LDFLAGS'])
 
     env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
     env['msvc'] = env['CC'] == 'cl'
 
+    if env['msvc'] and env['toolchain'] == 'default' and env['machine'] == 'x86_64':
+        # MSVC x64 support is broken in earlier versions of scons
+        env.EnsurePythonVersion(2, 0)
+
     # shortcuts
     machine = env['machine']
     platform = env['platform']
@@ -137,13 +192,38 @@ def generate(env):
     gcc = env['gcc']
     msvc = env['msvc']
 
+    # Determine whether we are cross compiling; in particular, whether we need
+    # to compile code generators with a different compiler as the target code.
+    host_platform = _platform.system().lower()
+    host_machine = os.environ.get('PROCESSOR_ARCHITEW6432', os.environ.get('PROCESSOR_ARCHITECTURE', _platform.machine()))
+    host_machine = {
+        'x86': 'x86',
+        'i386': 'x86',
+        'i486': 'x86',
+        'i586': 'x86',
+        'i686': 'x86',
+        'ppc' : 'ppc',
+        'AMD64': 'x86_64',
+        'x86_64': 'x86_64',
+    }.get(host_machine, 'generic')
+    env['crosscompile'] = platform != host_platform
+    if machine == 'x86_64' and host_machine != 'x86_64':
+        env['crosscompile'] = True
+    env['hostonly'] = False
+
     # Backwards compatability with the debug= profile= options
     if env['build'] == 'debug':
         if not env['debug']:
-            print 'scons: debug option is deprecated: use instead build=release'
+            print 'scons: warning: debug option is deprecated and will be removed eventually; use instead'
+            print
+            print ' scons build=release'
+            print
             env['build'] = 'release'
         if env['profile']:
-            print 'scons: profile option is deprecated: use instead build=profile'
+            print 'scons: warning: profile option is deprecated and will be removed eventually; use instead'
+            print
+            print ' scons build=profile'
+            print
             env['build'] = 'profile'
     if False:
         # Enforce SConscripts to use the new build variable
@@ -184,6 +264,9 @@ def generate(env):
     if env.GetOption('num_jobs') <= 1:
         env.SetOption('num_jobs', num_jobs())
 
+    env.Decider('MD5-timestamp')
+    env.SetOption('max_drift', 60)
+
     # C preprocessor options
     cppdefines = []
     if env['build'] in ('debug', 'checked'):
@@ -294,12 +377,15 @@ def generate(env):
                 '-m32',
                 #'-march=pentium4',
             ]
-            if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2'):
+            if distutils.version.LooseVersion(ccversion) >= distutils.version.LooseVersion('4.2') \
+               and (platform != 'windows' or env['build'] == 'debug' or True):
                 # NOTE: We need to ensure stack is realigned given that we
                 # produce shared objects, and have no control over the stack
                 # alignment policy of the application. Therefore we need
                 # -mstackrealign ore -mincoming-stack-boundary=2.
                 #
+                # XXX: -O and -mstackrealign causes stack corruption on MinGW
+                #
                 # XXX: We could have SSE without -mstackrealign if we always used
                 # __attribute__((force_align_arg_pointer)), but that's not
                 # always the case.
@@ -347,13 +433,19 @@ def generate(env):
               '/Od', # disable optimizations
               '/Oi', # enable intrinsic functions
               '/Oy-', # disable frame pointer omission
-              '/GL-', # disable whole program optimization
             ]
         else:
             ccflags += [
                 '/O2', # optimize for speed
+            ]
+        if env['build'] == 'release':
+            ccflags += [
                 '/GL', # enable whole program optimization
             ]
+        else:
+            ccflags += [
+                '/GL-', # disable whole program optimization
+            ]
         ccflags += [
             '/fp:fast', # fast floating point 
             '/W3', # warning level
@@ -443,7 +535,7 @@ def generate(env):
         else:
             env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
     if msvc:
-        if env['build'] != 'debug':
+        if env['build'] == 'release':
             # enable Link-time Code Generation
             linkflags += ['/LTCG']
             env.Append(ARFLAGS = ['/LTCG'])
@@ -496,12 +588,26 @@ def generate(env):
     env.Append(LINKFLAGS = linkflags)
     env.Append(SHLINKFLAGS = shlinkflags)
 
+    # We have C++ in several libraries, so always link with the C++ compiler
+    if env['gcc']:
+        env['LINK'] = env['CXX']
+
     # Default libs
     env.Append(LIBS = [])
 
-    # Load LLVM
+    # Load tools
     if env['llvm']:
         env.Tool('llvm')
+        env.Tool('udis86')
+    
+    pkg_config_modules(env, 'x11', ['x11', 'xext'])
+    pkg_config_modules(env, 'drm', ['libdrm'])
+    pkg_config_modules(env, 'drm_intel', ['libdrm_intel'])
+    pkg_config_modules(env, 'drm_radeon', ['libdrm_radeon'])
+    pkg_config_modules(env, 'xorg', ['xorg-server'])
+    pkg_config_modules(env, 'kms', ['libkms'])
+
+    env['dri'] = env['x11'] and env['drm']
 
     # Custom builders and methods
     env.Tool('custom')