Improved support for Visual Studio
authorDan Mihai <Daniel.Mihai@microsoft.com>
Tue, 13 Sep 2016 01:48:30 +0000 (18:48 -0700)
committerDave Thaler <dthaler@microsoft.com>
Thu, 15 Sep 2016 00:53:12 +0000 (00:53 +0000)
1. Use SCons's MSVC_VERSION variable to support multiple VS
versions, instead of more fragile IoTivity-specific solutions.

2. Don't execute vsvars32.bat "manually" from run.bat. Let SCons
execute that batch script when needed, based on the value of
MSVC_VERSION.

3. Remove VS version handling from gtest/SConscript. Use SCons for
building gtest (and eliminate the need for cmake on Windows).

4. Delete duplicated lines from build_common/windows/SConscript.

Change-Id: I6490a32533624b7959b0a30992c8f10811e7735e
Signed-off-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/11727
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: David Antler <david.a.antler@intel.com>
Reviewed-by: Dave Thaler <dthaler@microsoft.com>
build_common/SConscript
build_common/windows/SConscript
extlibs/gtest/SConscript
run.bat

index b700453..6684a59 100644 (file)
@@ -116,6 +116,11 @@ help_vars.Add(PathVariable('ANDROID_NDK', 'Android NDK path', None, PathVariable
 help_vars.Add(PathVariable('ANDROID_HOME', 'Android SDK path', None, PathVariable.PathAccept))
 help_vars.Add(PathVariable('ANDROID_GRADLE', 'Gradle binary file', None, PathVariable.PathIsFile))
 
+if target_os == 'windows':
+       # For VS2013, MSVC_VERSION is '12.0'. For VS2015, MSVC_VERSION is '14.0'.
+       # Default value is None, meaning that SCons has to choose automatically a VS version.
+       help_vars.Add(EnumVariable('MSVC_VERSION', 'MSVC compiler version - Windows', None, allowed_values=('12.0', '14.0')))
+
 AddOption('--prefix',
                   dest='prefix',
                   type='string',
index 48eee71..f8a3fb5 100644 (file)
@@ -21,15 +21,10 @@ if env['CC'] == 'cl':
 
     env.AppendUnique(CCFLAGS=['/EHsc'])
 
-    vs_version = env['MSVC_VERSION']
-
     # Set release/debug flags
     if env.get('RELEASE'):
         env.AppendUnique(CCFLAGS = ['/MD', '/O2', '/GF'])
         env.AppendUnique(CPPDEFINES = ['NDEBUG'])
-    elif env.get('TARGET_ARCH') in ['x86', 'x86_64'] or "14.0" in vs_version:
-        env.AppendUnique(CCFLAGS = ['/MDd', '/Od', '/ZI', '/RTC1', '/Gm'])
-        env.AppendUnique(LINKFLAGS = ['/debug'])
     else:
         env.AppendUnique(CCFLAGS = ['/MDd', '/Od', '/Zi', '/RTC1', '/Gm'])
         env.AppendUnique(LINKFLAGS = ['/debug'])
index 9e17973..ecb392a 100644 (file)
@@ -97,71 +97,18 @@ elif target_os == 'msys_nt':
                        gtest_env.Configure(gtest_dir, 'mv libgtest_main.a lib/.libs')
 
 elif target_os == 'windows':
-       if os.path.exists(gtest_dir):
-               if gtest_env.get('RELEASE'):
-                       output_dir = os.path.join(gtest_dir, 'Release') + os.sep
-               else:
-                       output_dir = os.path.join(gtest_dir, 'Debug') + os.sep
-
-               # Three parts to the gtest config string...
-               # 1. "Visual Studio" toolchain name.
-               # 2. VS Version + Year ("14 2015", "12 2013").
-               # 3. Target Architecture ("Win64", "Win32").
-               vs_version_year = ""
-               vs_target_arch  = ""
-               vs_num = env['MSVC_VERSION']
-               if "12.0" in vs_num:
-                       vs_version_year = "Visual Studio 12 2013"
-               elif "14.0" in vs_num:
-                       vs_version_year = "Visual Studio 14 2015"
-               else:
-                       print "Error: unknown Visual Studio version %s" % vs_num
-
-               vs_arch = env['TARGET_ARCH']
-               if "amd64" in vs_arch:
-                       vs_target_arch = "Win64"
-               elif "x86" in vs_arch:
-                       vs_target_arch = ""
-               else:
-                       print "Error: unknown Visual Studio target arch %s" % vs_arch
-
-               vs_target_string = vs_version_year + " " + vs_target_arch
-
-               # Exit if we didn't get a match for one of the above.
-               if not vs_version_year or not vs_target_arch:
-                       Exit(1)
-
-               if not gtest_env.WhereIs('cmake', gtest_env.get('PATH')):
-                       print '''*********************** Error ************************
-*                                                    *
-* Please make sure that CMake is in your System PATH *
-*                                                    *
-* You can download CMake from:                       *
-*            https://cmake.org/download/             *
-*                                                    *
-******************************************************
-'''
-                       Exit(1)
-
-               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"' + vs_target_string + '" -Dgtest_force_shared_crt=ON')
-
-                       # Run make on gtest
-                       print 'Making google unit test'
-                       gtest_env.Configure(gtest_dir, 'msbuild gtest.vcxproj')
-                       gtest_env.Configure(gtest_dir, 'msbuild gtest_main.vcxproj')
-
-                       print 'Moving libraries to lib folder'
-                       gtest_env.Configure(gtest_dir, 'copy '+output_dir+'gtest.lib %s' % gtest_lib_dir)
-                       gtest_env.Configure(gtest_dir, 'move '+output_dir+'gtest.lib %s' % gtest_dotlib_dir )
-                       gtest_env.Configure(gtest_dir, 'copy '+output_dir+'gtest_main.lib %s' % gtest_lib_dir)
-                       gtest_env.Configure(gtest_dir, 'move '+output_dir+'gtest_main.lib %s' % gtest_dotlib_dir)
+       # Avoid building the same StaticLibrary in more than one environment, by using the
+       # IOTIVITY_GTEST_HAS_BEEN_BUILT environment variable
+       if not env.has_key('IOTIVITY_GTEST_HAS_BEEN_BUILT'):
+               gtest_env.Append(CPPPATH = [ gtest_dir ])
+               gtest = gtest_env.StaticLibrary(target = 'gtest', source = [ '%s/src/gtest-all.cc' % gtest_dir ])
+               gtest_main = gtest_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)
 
 # Export flags once for all
 if target_os in targets_need_gtest:
diff --git a/run.bat b/run.bat
index 861d851..7ff8b11 100644 (file)
--- a/run.bat
+++ b/run.bat
@@ -22,24 +22,6 @@ if "!SECOND_ARG!"=="debug" (
   set DEBUG="%ProgramFiles(x86)%\Windows Kits\10\Debuggers\x64\cdb.exe" -2 -c "g" 
 )
 
-IF "%CURRENT_ARG%" == "build" IF "%BUILD_MSYS%" == "" (
-  IF NOT "%VS140COMNTOOLS%" == "" (
-    CALL "%VS140COMNTOOLS%"vsvars32.bat
-  ) ELSE (
-    IF NOT "%VS120COMNTOOLS%" == "" (
-      CALL "%VS120COMNTOOLS%"vsvars32.bat
-      )
-    )
-
-  IF NOT "!VSINSTALLDIR!" == "" (
-      CALL "!VSINSTALLDIR!VC\vcvarsall.bat" amd64
-  ) ELSE (
-    @ECHO WARNING: Could not find vsvarsall.bat.
-    @ECHO WARNING: VISUAL STUDIO 2013/2015 DOES NOT APPEAR TO BE INSTALLED ON THIS MACHINE
-    GOTO :EOF
-  )
-)
-
 REM We need to append the "PATH" so the octbstack.dll can be found by executables
 IF "%BUILD_MSYS%" == "" (
   set BUILD_DIR=out\windows\amd64\debug
@@ -60,6 +42,12 @@ set ROUTING=EP
 
 set BUILD_OPTIONS= TARGET_OS=%TARGET_OS% TARGET_ARCH=%TARGET_ARCH% RELEASE=0 WITH_RA=0 TARGET_TRANSPORT=IP SECURED=%SECURED% WITH_TCP=0 BUILD_SAMPLE=ON LOGGING=%LOGGING% TEST=%TEST% WITH_RD=%WITH_RD% ROUTING=%ROUTING%
 
+REM Use MSVC_VERSION=12.0 for VS2013, or MSVC_VERSION=14.0 for VS2015.
+REM If MSVC_VERSION has not been defined here, SCons chooses automatically a VS version.
+IF NOT "%MSVC_VERSION%" == "" (
+  set BUILD_OPTIONS=%BUILD_OPTIONS% MSVC_VERSION=%MSVC_VERSION%
+)
+
 REM *** BUILD OPTIONS ***
 
 if "!CURRENT_ARG!"=="server" (
@@ -109,13 +97,14 @@ if "!CURRENT_ARG!"=="server" (
   echo   LOGGING=%LOGGING%
   echo   WITH_RD=%WITH_RD%
   echo   ROUTING=%ROUTING%
-  CL.exe | findstr "Compiler Verison"
   echo.scons VERBOSE=1 %BUILD_OPTIONS%
   scons VERBOSE=1 %BUILD_OPTIONS%
 ) else if "!CURRENT_ARG!"=="clean" (
   scons VERBOSE=1 %BUILD_OPTIONS% -c clean
   rd /s /q out
   del .sconsign.dblite
+  del extlibs\gtest\gtest*.lib
+  del extlibs\gtest\gtest-1.7.0\src\gtest*.obj
 ) else if "!CURRENT_ARG!"=="cleangtest" (
   rd /s /q extlibs\gtest\gtest-1.7.0
   del extlibs\gtest\gtest-1.7.0.zip