I have both Visual Studio 2022(Having MSVC 19) and Visual Studio 2008 (having MSVC 9) installed on my computer. VS 2008 is needed for supporting legacy codebase.
Our codebase is mainly in C++. Some modules are common and need to be built for both VS2022 & VS2008. I was thinking of maintaining a CMakeLists.txt for each module and then inject a varying toolchain.cmake (either targeting MSVC19 or MSVC9) for compilation. I plan to create a msvc9toolchain.cmake for target MSVC9 and msvc19toolchain.cmake for targeting MSVC19 and then use the -DCMAKE_TOOLCHAIN_FILE flag to point to correct.
Example: Lets say we have a HelloWorld module with below cmake recipe:
cmake_minimum_required(VERSION 3.0)
# CMakeLists.txt for Hello World project
project(HelloWorld)
# Create an executable target named "HelloWorld" from the source file "main.cpp"
add_executable(HelloWorld main.cpp)
Contents of main.cpp
#include <iostream>
int main()
{
int var = 0;
std::cout<<" Hello World!";
std::cin>>var;
return 0;
}
For targeting MSVC9 as build environment, I have created msvc9toolchain.cmake with below contents:
SET(CMAKE_SYSTEM_NAME Windows)
# which compilers to use for C and C++
unset(CMAKE_C_COMPILER CACHE)
unset(CMAKE_CXX_COMPILER CACHE)
SET(CMAKE_C_COMPILER C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin/cl.exe)
SET(CMAKE_CXX_COMPILER C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin/cl.exe)
# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/lib; C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin")
# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Now the problem is when I trigger cmake with -DCMAKE_TOOLCHAIN_FILE=msvc9toolchain.cmake file, I expect it to take MSVC9 compiler but somehow cmake takes MSVC19 as the default compiler for C & C++.
Output for command "cmake -DCMAKE_TOOLCHAIN_FILE=msvc9toolchain.exe .":
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22000.0 to target Windows .
-- The C compiler identification is MSVC 19.35.32215.0
-- The CXX compiler identification is MSVC 19.35.32215.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (9.5s)
-- Generating done (0.0s)
-- Build files have been written to: D:/Eaton/Tasks/EDODSO-728/smp-devsecops-cmake-support/build
What I have tried till now:
- I tried to remove VS 2022 & MSVC19 from PATH in system variables but still the same.
- I tried to force generator by invoking cmake -G "Visual Studio 9 2008" but that gave me the warning that "Visual Studio 9 2008" has been deprecated for cmake. The default compiler picked up was still MSVC19.
- I tried re-installing cmake but still facing the same issue.
Kindly help me on how I can target MSVC9 in cmake for software build.