From 68251ab226b837cd5ca66faaa6464cbdd2f84128 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Sun, 9 Apr 2017 09:15:31 -0600 Subject: [PATCH] Improve the frustrating gtest SConscript Define gtest version in a single variable, for future-proofing Fix to "python style" (except long lines and constant names) - indents, etc. Fix redefinition of builtin name 'var' (windows section) Quit right away if target does not need gtest, and remove extra checks for this elsewhere Add checks for help and clean targets: should not unpack/config/build in these cases, and actually clean if clean is called Split configure and build targets for non-windows: may still need to build even if already configured (due to clean target) Change-Id: Ic9818fdd221eb2d3d7fead8c9585c41db80b3d5a Signed-off-by: Mats Wichmann Reviewed-on: https://gerrit.iotivity.org/gerrit/18633 Reviewed-by: Phil Coval Tested-by: jenkins-iotivity Reviewed-by: Dan Mihai Origin: master Reviewed-on: https://gerrit.iotivity.org/gerrit/20905 Reviewed-by: Peter Hatina Reviewed-by: Ziran Sun --- extlibs/gtest/SConscript | 193 +++++++++++++++++++++------------------ 1 file changed, 104 insertions(+), 89 deletions(-) diff --git a/extlibs/gtest/SConscript b/extlibs/gtest/SConscript index 73b332921..ca7ddd078 100644 --- a/extlibs/gtest/SConscript +++ b/extlibs/gtest/SConscript @@ -1,108 +1,123 @@ # -*- mode: python; python-indent-offset: 4; indent-tabs-mode: nil -*- ## -# 'googletest' script to check if Google Unit Test library is installed. +# script to check if Google Unit Test library is installed. # If not, get it and install it # ## import os -import shutil - Import('env') gtest_env = env.Clone() target_os = gtest_env.get('TARGET_OS') src_dir = gtest_env.get('SRC_DIR') -targets_need_gtest = ['darwin','linux', 'msys_nt', 'windows'] -gtest_dir = os.path.join(src_dir, 'extlibs', 'gtest', 'googletest-release-1.7.0') -gtest_lib_dir = os.path.join(gtest_dir,'lib') -gtest_dotlib_dir = os.path.join(gtest_lib_dir, '.libs') -gtest_zip_file = os.path.join(src_dir, 'extlibs', 'gtest', 'release-1.7.0.zip') -gtest_url = 'https://github.com/google/googletest/archive/release-1.7.0.zip' - -if target_os in targets_need_gtest: - print '*** Checking for installation of google unit test 1.7.0 ***' - if not os.path.exists(os.path.join(gtest_dir, 'CMakeLists.txt')): - # If the gtest zip file is not already present, download it - if not os.path.exists(gtest_zip_file): - gtest_zip = gtest_env.Download(gtest_zip_file, gtest_url) - else: - gtest_zip = gtest_zip_file - print 'Unzipping to : ' + gtest_dir - gtest_env.UnpackAll(gtest_dir, gtest_zip) +targets_need_gtest = ['darwin', 'linux', 'msys_nt', 'windows'] + +GTEST_VERSION = '1.7.0' +gtest_dir = os.path.join(src_dir, 'extlibs', 'gtest', 'googletest-release-' + GTEST_VERSION) +gtest_lib_dir = os.path.join(gtest_dir, 'lib') +gtest_configured_sentinel = os.path.join(gtest_dir, 'Makefile') +gtest_unpacked_sentinel = os.path.join(gtest_dir, 'CMakeLists.txt') +gtest_dotlib_dir = os.path.join(gtest_lib_dir, '.libs') +gtest_zip_file = 'release-' + GTEST_VERSION + '.zip' +gtest_url = 'https://github.com/google/googletest/archive/' + gtest_zip_file +gtest_zip_path = os.path.join(src_dir, 'extlibs', 'gtest', gtest_zip_file) + +# nothing to do if this target doesn't use gtest +if target_os not in targets_need_gtest: + Return("gtest_env") + +# nothing to do if asked for help +if gtest_env.GetOption('help'): + Return("gtest_env") + +# Clean up gtest if 'clean' is specified, and it looks like has been built. +# Implicity skips the targets which don't "configure" (e.g. windows) +if gtest_env.GetOption('clean'): + print 'Cleaning google unit test' + if os.path.exists(gtest_configured_sentinel): + clean = "cd %s && make clean" % gtest_dir + Execute(clean) + Return("gtest_env") + +print '*** Checking for installation of google unit test %s ***', GTEST_VERSION +if not os.path.exists(gtest_unpacked_sentinel): + # If the gtest zip file is not already present, download it + if not os.path.exists(gtest_zip_path): + gtest_zip = gtest_env.Download(gtest_zip_path, gtest_url) + else: + gtest_zip = gtest_zip_path + print 'Unzipping to : ' + gtest_dir + gtest_env.UnpackAll(gtest_dir, gtest_zip) if target_os in ['linux']: - if os.path.exists(gtest_dir): - if not os.path.exists(gtest_lib_dir): - # Run configure on gtest - print 'Configuring google unit test' - reconfigure_ran = gtest_env.Configure(gtest_dir, 'autoreconf -i') - if env.get('CROSS_COMPILE'): - configure_ran = env.Configure(gtest_dir, './configure --disable-shared --host=' + env['CROSS_COMPILE']) - gtest_env.Depends(configure_ran, reconfigure_ran) - else: - configure_ran = env.Configure(gtest_dir, './configure --disable-shared') - gtest_env.Depends(configure_ran, reconfigure_ran) - # Run make on gtest - print 'Making google unit test' - make_ran = gtest_env.Configure(gtest_dir, 'make') - gtest_env.Depends(make_ran, configure_ran) - -elif target_os in ['darwin', 'msys_nt']: - if os.path.exists(gtest_dir): - if not os.path.exists(gtest_lib_dir): - # Create lib dir - os.mkdir(gtest_lib_dir) - os.mkdir(gtest_dotlib_dir) - - # Run configure on gtest - print 'Configuring google unit test for compilation' - gtest_env.Configure(gtest_dir, 'cmake -G "Unix Makefiles" .') - - # Run make on gtest - print 'Making google unit test' - gtest_env.Configure(gtest_dir, 'make') - - print 'Moving libraries to lib folder' - gtest_env.Configure(gtest_dir, 'cp libgtest.a lib') - gtest_env.Configure(gtest_dir, 'mv libgtest.a lib/.libs') - gtest_env.Configure(gtest_dir, 'cp libgtest_main.a lib') - gtest_env.Configure(gtest_dir, 'mv libgtest_main.a lib/.libs') - -elif target_os in ['windows']: - # Avoid building the same StaticLibrary in more than one environment, by using the - # IOTIVITY_GTEST_HAS_BEEN_BUILT environment variable - if 'IOTIVITY_GTEST_HAS_BEEN_BUILT' not in env: - gtest_env.AppendUnique(CPPPATH = [ gtest_dir ]) - gtest_build_env = gtest_env.Clone() - gtest_build_env.AppendUnique(CPPPATH = [ os.path.join(gtest_dir, 'include') ]) - gtest_build_env.AppendUnique(CXXFLAGS = ['/EHsc']) - gtest = gtest_build_env.StaticLibrary(target = 'gtest', source = [ '%s/src/gtest-all.cc' % gtest_dir ]) - gtest_main = gtest_build_env.StaticLibrary(target = 'gtest_main', source = [ '%s/src/gtest_main.cc' % gtest_dir ]) - gtest_env.InstallTarget(gtest, 'gtest') - gtest_env.InstallTarget(gtest_main, 'gtest_main') - - vars = Variables(); - vars.AddVariables(('IOTIVITY_GTEST_HAS_BEEN_BUILT', '', '1')) - vars.Update(env) + if not os.path.exists(gtest_configured_sentinel): + # Run configure on gtest + print 'Configuring google unit test' + reconfigure_ran = gtest_env.Configure(gtest_dir, 'autoreconf -i') + if env.get('CROSS_COMPILE'): + configure_ran = env.Configure(gtest_dir, './configure --disable-shared --host=' + env['CROSS_COMPILE']) + else: + configure_ran = env.Configure(gtest_dir, './configure --disable-shared') + gtest_env.Depends(configure_ran, reconfigure_ran) -# Export flags once for all -if target_os in targets_need_gtest: - gtest_env.AppendUnique(LIBPATH = [gtest_dotlib_dir]) - gtest_env.PrependUnique(CPPPATH = [os.path.join(gtest_dir, 'include')]) - gtest_env.AppendENVPath('LD_LIBRARY_PATH', gtest_dotlib_dir) - if 'g++' in gtest_env.get('CXX'): - gtest_env.AppendUnique(CXXFLAGS = ['-std=c++0x']) - gtest_env.AppendUnique(CXXFLAGS = ['-Wall']) -# Note: 'pthread' for android is in bionic -# On other platform, if use new gcc(>4.9?) it isn't required, otherwise, it's required - if target_os not in ['android']: - gtest_env.AppendUnique(CXXFLAGS = ['-pthread']) - gtest_env.PrependUnique(LIBS = ['pthread']) - gtest_env.PrependUnique(LIBS = ['gtest', 'gtest_main']) - if target_os in ['windows']: - gtest_env.AppendUnique(LINKFLAGS = ['/subsystem:CONSOLE']) + # Run make on gtest + print 'Making google unit test' + make_ran = gtest_env.Configure(gtest_dir, 'make') +elif target_os in ['darwin', 'msys_nt'] and os.path.exists(gtest_dir): + if not os.path.exists(gtest_configured_sentinel): + # Create lib dir + os.mkdir(gtest_lib_dir) + os.mkdir(gtest_dotlib_dir) + + # Run configure on gtest + print 'Configuring google unit test for compilation' + gtest_env.Configure(gtest_dir, 'cmake -G "Unix Makefiles" .') + + # Run make on gtest + print 'Making google unit test' + gtest_env.Configure(gtest_dir, 'make') + + print 'Moving libraries to lib folder' + gtest_env.Configure(gtest_dir, 'cp libgtest.a lib') + gtest_env.Configure(gtest_dir, 'mv libgtest.a lib/.libs') + gtest_env.Configure(gtest_dir, 'cp libgtest_main.a lib') + gtest_env.Configure(gtest_dir, 'mv libgtest_main.a lib/.libs') + +elif target_os in ['windows'] and 'IOTIVITY_GTEST_HAS_BEEN_BUILT' not in env: + # Avoid building the same StaticLibrary in more than one environment, + # by using the IOTIVITY_GTEST_HAS_BEEN_BUILT environment variable + gtest_env.AppendUnique(CPPPATH=[gtest_dir]) + gtest_build_env = gtest_env.Clone() + gtest_build_env.AppendUnique(CPPPATH=[os.path.join(gtest_dir, 'include')]) + gtest_build_env.AppendUnique(CXXFLAGS=['/EHsc']) + gtest_build_env.AppendUnique(CCFLAGS=['/W4', '/WX']) + gtest = gtest_build_env.StaticLibrary(target='gtest', source=['%s/src/gtest-all.cc' % gtest_dir]) + gtest_main = gtest_build_env.StaticLibrary(target='gtest_main', source=['%s/src/gtest_main.cc' % gtest_dir]) + gtest_env.InstallTarget(gtest, 'gtest') + gtest_env.InstallTarget(gtest_main, 'gtest_main') + + gtest_vars = Variables() + gtest_vars.AddVariables(('IOTIVITY_GTEST_HAS_BEEN_BUILT', '', '1')) + gtest_vars.Update(env) + +# Export flags once for all +gtest_env.AppendUnique(LIBPATH=[gtest_dotlib_dir]) +gtest_env.PrependUnique(CPPPATH=[os.path.join(gtest_dir, 'include')]) +gtest_env.AppendENVPath('LD_LIBRARY_PATH', gtest_dotlib_dir) +if 'g++' in gtest_env.get('CXX'): + gtest_env.AppendUnique(CXXFLAGS=['-std=c++0x']) + gtest_env.AppendUnique(CXXFLAGS=['-Wall']) + # Note: 'pthread' for android is in bionic + # On other platforms, if use new gcc(>4.9?) it isn't required, + # otherwise, it's required + if target_os not in ['android']: + gtest_env.AppendUnique(CXXFLAGS=['-pthread']) + gtest_env.PrependUnique(LIBS=['pthread']) +gtest_env.PrependUnique(LIBS=['gtest', 'gtest_main']) +if target_os in ['windows']: + gtest_env.AppendUnique(LINKFLAGS=['/subsystem:CONSOLE']) Return('gtest_env') -- 2.34.1