From 3b1ab7210c88c09a512752710caa6c0a31fc9d24 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 1 Oct 2009 23:02:59 +0000 Subject: [PATCH] Refactors the scons script (by Vlad Losev). --- scons/SConscript | 13 ++- scons/SConstruct.common | 212 ++++++++++++++++++++-------------------- 2 files changed, 112 insertions(+), 113 deletions(-) diff --git a/scons/SConscript b/scons/SConscript index 11a105dd..26fa5fbf 100644 --- a/scons/SConscript +++ b/scons/SConscript @@ -95,9 +95,8 @@ import os ############################################################ # Environments for building the targets, sorted by name. -Import('env') +Import('env', 'EnvCreator') -EnvCreator = SConscript('SConstruct.common').EnvCreator env = EnvCreator.Create(env) # Note: The relative paths in SConscript files are relative to the location @@ -114,11 +113,15 @@ env = EnvCreator.Create(env) env.Prepend(CPPPATH = ['..', '../include']) env_use_own_tuple = EnvCreator.Create(env, EnvCreator.UseOwnTuple) -env_warning_ok = EnvCreator.Create(env, EnvCreator.WarningOk) -env_with_exceptions = EnvCreator.Create(env, EnvCreator.WithExceptions) env_less_optimized = EnvCreator.Create(env, EnvCreator.LessOptimized) env_with_threads = EnvCreator.Create(env, EnvCreator.WithThreads) -env_without_rtti = EnvCreator.Create(env, EnvCreator.NoRtti) +# The following environments are used to compile gtest_unittest.cc, which +# triggers a warning in all but the most recent GCC versions when compiling +# the EXPECT_EQ(NULL, ptr) statement. +env_warning_ok = EnvCreator.Create(env, EnvCreator.WarningOk) +env_with_exceptions = EnvCreator.Create(env_warning_ok, + EnvCreator.WithExceptions) +env_without_rtti = EnvCreator.Create(env_warning_ok, EnvCreator.NoRtti) ############################################################ # Helpers for creating build targets. diff --git a/scons/SConstruct.common b/scons/SConstruct.common index 65c80535..2445bebd 100644 --- a/scons/SConstruct.common +++ b/scons/SConstruct.common @@ -82,6 +82,108 @@ class SConstructHelper: # Enable scons -h Help(vars.GenerateHelpText(self.env_base)) + class EnvCreator: + """Creates new customized environments from a base one.""" + + def _Remove(cls, env, attribute, value): + """Removes the given attribute value from the environment.""" + + attribute_values = env[attribute] + if value in attribute_values: + attribute_values.remove(value) + _Remove = classmethod(_Remove) + + def Create(cls, base_env, modifier=None): + # User should NOT create more than one environment with the same + # modifier (including None). + env = base_env.Clone() + if modifier: + modifier(env) + else: + env['OBJ_SUFFIX'] = '' # Default suffix for unchanged environment. + return env; + Create = classmethod(Create) + + # Each of the following methods modifies the environment for a particular + # purpose and can be used by clients for creating new environments. Each + # one needs to set the OBJ_SUFFIX variable to a unique suffix to + # differentiate targets built with that environment. Otherwise, SCons may + # complain about same target built with different settings. + + def UseOwnTuple(cls, env): + """Instructs Google Test to use its internal implementation of tuple.""" + + env['OBJ_SUFFIX'] = '_use_own_tuple' + env.Append(CPPDEFINES = 'GTEST_USE_OWN_TR1_TUPLE=1') + UseOwnTuple = classmethod(UseOwnTuple) + + def WarningOk(cls, env): + """Does not treat warnings as errors. + + Necessary for compiling gtest_unittest.cc, which triggers a gcc + warning when testing EXPECT_EQ(NULL, ptr).""" + + env['OBJ_SUFFIX'] = '_warning_ok' + if env['PLATFORM'] == 'win32': + cls._Remove(env, 'CCFLAGS', '-WX') + else: + cls._Remove(env, 'CCFLAGS', '-Werror') + WarningOk = classmethod(WarningOk) + + def WithExceptions(cls, env): + """Re-enables exceptions.""" + + env['OBJ_SUFFIX'] = '_ex' + if env['PLATFORM'] == 'win32': + env.Append(CCFLAGS=['/EHsc']) + env.Append(CPPDEFINES='_HAS_EXCEPTIONS=1') + # Undoes the _TYPEINFO_ hack, which is unnecessary and only creates + # trouble when exceptions are enabled. + cls._Remove(env, 'CPPDEFINES', '_TYPEINFO_') + cls._Remove(env, 'CPPDEFINES', '_HAS_EXCEPTIONS=0') + else: + env.Append(CCFLAGS='-fexceptions') + cls._Remove(env, 'CCFLAGS', '-fno-exceptions') + WithExceptions = classmethod(WithExceptions) + + def LessOptimized(cls, env): + """Disables certain optimizations on Windows. + + We need to disable some optimization flags for some tests on + Windows; otherwise the redirection of stdout does not work + (apparently because of a compiler bug).""" + + env['OBJ_SUFFIX'] = '_less_optimized' + if env['PLATFORM'] == 'win32': + for flag in ['/O1', '/Os', '/Og', '/Oy']: + cls._Remove(env, 'LINKFLAGS', flag) + LessOptimized = classmethod(LessOptimized) + + def WithThreads(cls, env): + """Allows use of threads. + + Currently only enables pthreads under GCC.""" + + env['OBJ_SUFFIX'] = '_with_threads' + if env['PLATFORM'] != 'win32': + # Assuming POSIX-like environment with GCC. + # TODO(vladl@google.com): sniff presence of pthread_atfork instead of + # selecting on a platform. + env.Append(CCFLAGS=['-pthread']) + env.Append(LINKFLAGS=['-pthread']) + WithThreads = classmethod(WithThreads) + + def NoRtti(cls, env): + """Disables RTTI support.""" + + env['OBJ_SUFFIX'] = '_no_rtti' + if env['PLATFORM'] == 'win32': + env.Append(CCFLAGS=['/GR-']) + else: + env.Append(CCFLAGS=['-fno-rtti']) + env.Append(CPPDEFINES='GTEST_HAS_RTTI=0') + NoRtti = classmethod(NoRtti) + def AllowVc71StlWithoutExceptions(self, env): env.Append( CPPDEFINES = [# needed for using some parts of STL with exception @@ -219,6 +321,8 @@ class SConstructHelper: self.SetBuildNameAndDir(gcc_opt, 'opt') def BuildSelectedEnvironments(self): + EnvCreator = SConstructHelper.EnvCreator + Export('EnvCreator') # Build using whichever environments the 'BUILD' option selected for build_name in self.env_base['BUILD']: print 'BUILDING %s' % build_name @@ -247,114 +351,6 @@ class SConstructHelper: variant_dir=env['BUILD_DIR'], duplicate=0) - class EnvCreator: - """Creates new customized environments from a base one.""" - - def _Remove(cls, env, attribute, value): - """Removes the given attribute value from the environment.""" - - attribute_values = env[attribute] - if value in attribute_values: - attribute_values.remove(value) - _Remove = classmethod(_Remove) - - def Create(cls, base_env, modifier=None): - # User should NOT create more than one environment with the same - # modifier (including None). - env = base_env.Clone() - if modifier: - modifier(env) - else: - env['OBJ_SUFFIX'] = '' # Default suffix for unchanged environment. - return env; - Create = classmethod(Create) - - # Each of the following methods modifies the environment for a particular - # purpose and can be used by clients for creating new environments. Each - # one needs to set the OBJ_SUFFIX variable to a unique suffix to - # differentiate targets built with that environment. Otherwise, SCons may - # complain about same target built with different settings. - - def UseOwnTuple(cls, env): - """Instructs Google Test to use its internal implementation of tuple.""" - - env['OBJ_SUFFIX'] = '_use_own_tuple' - env.Append(CPPDEFINES = 'GTEST_USE_OWN_TR1_TUPLE=1') - UseOwnTuple = classmethod(UseOwnTuple) - - def WarningOk(cls, env): - """Does not treat warnings as errors. - - Necessary for compiling gtest_unittest.cc, which triggers a gcc - warning when testing EXPECT_EQ(NULL, ptr).""" - - env['OBJ_SUFFIX'] = '_warning_ok' - if env['PLATFORM'] == 'win32': - cls._Remove(env, 'CCFLAGS', '-WX') - else: - cls._Remove(env, 'CCFLAGS', '-Werror') - WarningOk = classmethod(WarningOk) - - def WithExceptions(cls, env): - """Re-enables exceptions.""" - - # We compile gtest_unittest in this environment which means we need to - # allow warnings here as well. - cls.WarningOk(env) - env['OBJ_SUFFIX'] = '_ex' # Overrides the suffix supplied by WarningOK. - if env['PLATFORM'] == 'win32': - env.Append(CCFLAGS=['/EHsc']) - env.Append(CPPDEFINES='_HAS_EXCEPTIONS=1') - # Undoes the _TYPEINFO_ hack, which is unnecessary and only creates - # trouble when exceptions are enabled. - cls._Remove(env, 'CPPDEFINES', '_TYPEINFO_') - cls._Remove(env, 'CPPDEFINES', '_HAS_EXCEPTIONS=0') - else: - env.Append(CCFLAGS='-fexceptions') - cls._Remove(env, 'CCFLAGS', '-fno-exceptions') - WithExceptions = classmethod(WithExceptions) - - def LessOptimized(cls, env): - """Disables certain optimizations on Windows. - - We need to disable some optimization flags for some tests on - Windows; otherwise the redirection of stdout does not work - (apparently because of a compiler bug).""" - - env['OBJ_SUFFIX'] = '_less_optimized' - if env['PLATFORM'] == 'win32': - for flag in ['/O1', '/Os', '/Og', '/Oy']: - cls._Remove(env, 'LINKFLAGS', flag) - LessOptimized = classmethod(LessOptimized) - - def WithThreads(cls, env): - """Allows use of threads. - - Currently only enables pthreads under GCC.""" - - env['OBJ_SUFFIX'] = '_with_threads' - if env['PLATFORM'] != 'win32': - # Assuming POSIX-like environment with GCC. - # TODO(vladl@google.com): sniff presence of pthread_atfork instead of - # selecting on a platform. - env.Append(CCFLAGS=['-pthread']) - env.Append(LINKFLAGS=['-pthread']) - WithThreads = classmethod(WithThreads) - - def NoRtti(cls, env): - """Disables RTTI support.""" - - # We compile gtest_unittest in this environment which means we need to - # allow warnings here as well. - cls.WarningOk(env) - env['OBJ_SUFFIX'] = '_no_rtti' # Overrides suffix supplied by WarningOK. - if env['PLATFORM'] == 'win32': - env.Append(CCFLAGS=['/GR-']) - else: - env.Append(CCFLAGS=['-fno-rtti']) - env.Append(CPPDEFINES='GTEST_HAS_RTTI=0') - NoRtti = classmethod(NoRtti) - sconstruct_helper = SConstructHelper() Return('sconstruct_helper')