scons: Revamp how to specify targets to build.

Use scons target and dependency system instead of ad-hoc options.

Now is simply a matter of naming what to build. For example:

  scons libgl-xlib

  scons libgl-gdi

  scons graw-progs

  scons llvmpipe

and so on. And there is also the possibility of scepcified subdirs, e.g.

  scons src/gallium/drivers

If nothing is specified then everything will be build.

There might be some rough corners over the next days. Please bare with me.
diff --git a/scons/gallium.py b/scons/gallium.py
index b065b7b..194b152 100644
--- a/scons/gallium.py
+++ b/scons/gallium.py
@@ -98,6 +98,38 @@
     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
+
+    # 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"""
 
@@ -110,21 +142,27 @@
             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'
@@ -140,10 +178,16 @@
     # 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 +228,9 @@
     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'):
@@ -499,9 +546,14 @@
     # 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, 'dri', ['libdrm'])
+    pkg_config_modules(env, 'xorg', ['xorg-server'])
 
     # Custom builders and methods
     env.Tool('custom')
diff --git a/scons/llvm.py b/scons/llvm.py
index 39fbb91..1b033ac 100644
--- a/scons/llvm.py
+++ b/scons/llvm.py
@@ -38,6 +38,8 @@
 
 
 def generate(env):
+    env['llvm'] = False
+
     try:
         llvm_dir = os.environ['LLVM']
     except KeyError:
@@ -64,13 +66,13 @@
         # XXX: There is no llvm-config on Windows, so assume a standard layout
         if llvm_dir is None:
             print 'scons: LLVM environment variable must be specified when building for windows'
-            env.Exit(1)
+            return
 
         # Try to determine the LLVM version from llvm/Config/config.h
         llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h')
         if not os.path.exists(llvm_config):
             print 'scons: could not find %s' % llvm_config
-            env.Exit(1)
+            return
         llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"')
         llvm_version = None
         for line in open(llvm_config, 'rt'):
@@ -81,7 +83,7 @@
                 break
         if llvm_version is None:
             print 'scons: could not determine the LLVM version from %s' % llvm_config
-            env.Exit(1)
+            return
 
         env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
         env.AppendUnique(CPPDEFINES = [
@@ -133,7 +135,7 @@
     else:
         if not env.Detect('llvm-config'):
             print 'scons: llvm-config script not found' % llvm_version
-            env.Exit(1)
+            return
 
         llvm_version = env.backtick('llvm-config --version').rstrip()
         llvm_version = distutils.version.LooseVersion(llvm_version)
@@ -144,11 +146,12 @@
             env.ParseConfig('llvm-config --ldflags')
         except OSError:
             print 'scons: llvm-config version %s failed' % llvm_version
-            env.Exit(1)
+            return
         else:
             env['LINK'] = env['CXX']
 
     assert llvm_version is not None
+    env['llvm'] = True
 
     print 'scons: Found LLVM version %s' % llvm_version
     env['LLVM_VERSION'] = llvm_version
diff --git a/scons/udis86.py b/scons/udis86.py
index ba71d4e..bb91d3c 100644
--- a/scons/udis86.py
+++ b/scons/udis86.py
@@ -31,8 +31,10 @@
     conf = env.Configure()
 
     if conf.CheckHeader('udis86.h'): # and conf.CheckLib('udis86'):
-        env.Append(CPPDEFINES = [('HAVE_UDIS86', '1')])
+        env['UDIS86'] = True
         env.Prepend(LIBS = ['udis86'])
+    else:
+        env['UDIS86'] = False
 
     conf.Finish()
 
diff --git a/scons/x11.py b/scons/x11.py
index 99bf079..7368618 100644
--- a/scons/x11.py
+++ b/scons/x11.py
@@ -29,24 +29,12 @@
 
 
 def generate(env):
-    env.Append(CPPPATH = ['/usr/X11R6/include'])
-    env.Append(LIBPATH = ['/usr/X11R6/lib'])
-
-    env.Append(LIBS = [
-        'X11',
-        'Xext',
-        'Xxf86vm',
-        'Xdamage',
-        'Xfixes',
-    ])
+    # XXX: backwards compatability only
+    pass
 
 
 def exists(env):
-    # TODO: actually detect the presence of the headers
-    if env['platform'] in ('linux', 'freebsd', 'darwin'):
-        return True
-    else:
-        return False
+    return True
 
 
 # vim:set ts=4 sw=4 et: