i965g: back out unintentional changes to linux-dri
[mesa.git] / scons / generic.py
index f0bb3de9fcd43ac410b3b9b20408d6c3e21bbda0..859bf2ae64ea494683f7d0d1f688b3a982896f34 100644 (file)
@@ -206,6 +206,25 @@ _bool_map = {
 }
 
 
+def num_jobs():
+    try:
+        return int(os.environ['NUMBER_OF_PROCESSORS'])
+    except (ValueError, KeyError):
+        pass
+
+    try:
+        return os.sysconf('SC_NPROCESSORS_ONLN')
+    except (ValueError, OSError, AttributeError):
+        pass
+
+    try:
+        return int(os.popen2("sysctl -n hw.ncpu")[1].read())
+    except ValueError:
+        pass
+
+    return 1
+
+
 def generate(env):
     """Common environment generation code"""
 
@@ -239,6 +258,11 @@ def generate(env):
     if env['toolchain'] == 'crossmingw' and env['machine'] not in ('generic', 'x86'):
             env['machine'] = 'x86'
 
+    try:
+        env['MSVS_VERSION'] = ARGUMENTS['MSVS_VERSION']
+    except KeyError:
+        pass
+
     # Build type
     env['debug'] = _bool_map[ARGUMENTS.get('debug', 'no')]
     env['profile'] = _bool_map[ARGUMENTS.get('profile', 'no')]
@@ -246,7 +270,7 @@ def generate(env):
     # Put build output in a separate dir, which depends on the current
     # configuration. See also http://www.scons.org/wiki/AdvancedBuildExample
     try:
-        env['variant_dir'] = ARGUMENTS['variant_dir']
+        env['build'] = ARGUMENTS['build']
     except KeyError:
         build_topdir = 'build'
         build_subdir = env['platform']
@@ -256,11 +280,14 @@ def generate(env):
             build_subdir += "-debug"
         if env['profile']:
             build_subdir += "-profile"
-        env['variant_dir'] = os.path.join(build_topdir, build_subdir)
+        env['build'] = os.path.join(build_topdir, build_subdir)
     # Place the .sconsign file in the build dir too, to avoid issues with
     # different scons versions building the same source file
-    #env.VariantDir(env['variant_dir']
-    #env.SConsignFile(os.path.join(env['variant_dir'], '.sconsign'))
+    env.SConsignFile(os.path.join(env['build'], '.sconsign'))
+
+    # Parallel build
+    if env.GetOption('num_jobs') <= 1:
+        env.SetOption('num_jobs', num_jobs())
 
     # Summary
     print
@@ -269,20 +296,24 @@ def generate(env):
     print '  toolchain=%s' % env['toolchain']
     print '  debug=%s' % ['no', 'yes'][env['debug']]
     print '  profile=%s' % ['no', 'yes'][env['profile']]
-    #print '  variant_dir=%s' % env['variant_dir']
+    print '  build=%s' % env['build']
+    print '  %s jobs' % env.GetOption('num_jobs')
     print
 
     # Load tool chain
     env.Tool(env['toolchain'])
 
+    env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
+    env['msvc'] = env['CC'] == 'cl'
+
     # shortcuts
     debug = env['debug']
     machine = env['machine']
     platform = env['platform']
     x86 = env['machine'] == 'x86'
     ppc = env['machine'] == 'ppc'
-    gcc = env['platform'] in ('linux', 'freebsd', 'darwin') or env['toolchain'] == 'crossmingw'
-    msvc = env['platform'] in ('windows', 'winddk', 'wince') and env['toolchain'] != 'crossmingw'
+    gcc = env['gcc']
+    msvc = env['msvc']
 
     # C preprocessor options
     cppdefines = []
@@ -299,10 +330,17 @@ def generate(env):
             #'_UNICODE',
             #'UNICODE',
             # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
-            'WIN32_LEAN_AND_MEAN',
-            'VC_EXTRALEAN',
-            '_CRT_SECURE_NO_DEPRECATE',
+            #'WIN32_LEAN_AND_MEAN',
         ]
+        if msvc:
+            cppdefines += [
+                'VC_EXTRALEAN',
+                '_USE_MATH_DEFINES',
+                '_CRT_SECURE_NO_WARNINGS',
+                '_CRT_SECURE_NO_DEPRECATE',
+                '_SCL_SECURE_NO_WARNINGS',
+                '_SCL_SECURE_NO_DEPRECATE',
+            ]
         if debug:
             cppdefines += ['_DEBUG']
     if platform == 'winddk':
@@ -358,68 +396,73 @@ def generate(env):
         ])
 
     # C compiler options
-    cflags = []
+    cflags = [] # C
+    cxxflags = [] # C++
+    ccflags = [] # C & C++
     if gcc:
         if debug:
-            cflags += ['-O0', '-g3']
+            ccflags += ['-O0', '-g3']
+        elif env['toolchain'] == 'crossmingw':
+            ccflags += ['-O0', '-g3'] # mingw 4.2.1 optimizer is broken
         else:
-            cflags += ['-O3', '-g0']
-        if env['profile']:
-            cflags += ['-pg']
+            ccflags += ['-O3', '-g0']
         if env['machine'] == 'x86':
-            cflags += [
+            ccflags += [
                 '-m32',
                 #'-march=pentium4',
                 '-mmmx', '-msse', '-msse2', # enable SIMD intrinsics
                 #'-mfpmath=sse',
             ]
         if env['machine'] == 'x86_64':
-            cflags += ['-m64']
-        cflags += [
+            ccflags += ['-m64']
+        # See also:
+        # - http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
+        ccflags += [
             '-Wall',
-            '-Wmissing-prototypes',
+            '-Wmissing-field-initializers',
+            '-Wpointer-arith',
             '-Wno-long-long',
             '-ffast-math',
-            '-pedantic',
             '-fmessage-length=0', # be nice to Eclipse
         ]
+        cflags += [
+            '-Werror=declaration-after-statement',
+            '-Wmissing-prototypes',
+            '-std=gnu99',
+        ]
     if msvc:
         # See also:
         # - http://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
         # - cl /?
         if debug:
-            cflags += [
+            ccflags += [
               '/Od', # disable optimizations
               '/Oi', # enable intrinsic functions
               '/Oy-', # disable frame pointer omission
+              '/GL-', # disable whole program optimization
             ]
         else:
-            cflags += [
+            ccflags += [
               '/Ox', # maximum optimizations
               '/Oi', # enable intrinsic functions
               '/Ot', # favor code speed
               #'/fp:fast', # fast floating point 
             ]
-        if env['profile']:
-            cflags += [
-                '/Gh', # enable _penter hook function
-                '/GH', # enable _pexit hook function
-            ]
-        cflags += [
+        ccflags += [
             '/W3', # warning level
             #'/Wp64', # enable 64 bit porting warnings
         ]
         if env['machine'] == 'x86':
-            cflags += [
+            ccflags += [
                 #'/QIfist', # Suppress _ftol
                 #'/arch:SSE2', # use the SSE2 instructions
             ]
         if platform == 'windows':
-            cflags += [
+            ccflags += [
                 # TODO
             ]
         if platform == 'winddk':
-            cflags += [
+            ccflags += [
                 '/Zl', # omit default library name in .OBJ
                 '/Zp8', # 8bytes struct member alignment
                 '/Gy', # separate functions for linker
@@ -438,7 +481,7 @@ def generate(env):
             ]
         if platform == 'wince':
             # See also C:\WINCE600\public\common\oak\misc\makefile.def
-            cflags += [
+            ccflags += [
                 '/Zl', # omit default library name in .OBJ
                 '/GF', # enable read-only string pooling
                 '/GR-', # disable C++ RTTI
@@ -455,9 +498,20 @@ def generate(env):
         # See http://scons.tigris.org/issues/show_bug.cgi?id=1656
         env.EnsureSConsVersion(0, 98, 0)
         env['PDB'] = '${TARGET.base}.pdb'
+    env.Append(CCFLAGS = ccflags)
     env.Append(CFLAGS = cflags)
-    env.Append(CXXFLAGS = cflags)
+    env.Append(CXXFLAGS = cxxflags)
 
+    if env['platform'] == 'windows' and msvc:
+        # Choose the appropriate MSVC CRT
+        # http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
+        if env['debug']:
+            env.Append(CCFLAGS = ['/MTd'])
+            env.Append(SHCCFLAGS = ['/LDd'])
+        else:
+            env.Append(CCFLAGS = ['/MT'])
+            env.Append(SHCCFLAGS = ['/LD'])
+    
     # Assembler options
     if gcc:
         if env['machine'] == 'x86':
@@ -472,9 +526,14 @@ def generate(env):
             linkflags += ['-m32']
         if env['machine'] == 'x86_64':
             linkflags += ['-m64']
-    if platform == 'winddk':
+    if platform == 'windows' and msvc:
         # See also:
         # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
+        linkflags += [
+            '/fixed:no',
+            '/incremental:no',
+        ]
+    if platform == 'winddk':
         linkflags += [
             '/merge:_PAGE=PAGE',
             '/merge:_TEXT=.text',
@@ -502,7 +561,7 @@ def generate(env):
 
             '/entry:DrvEnableDriver',
         ]
-        if env['profile']:
+        if env['debug'] or env['profile']:
             linkflags += [
                 '/MAP', # http://msdn.microsoft.com/en-us/library/k7xkk3e2.aspx
             ]