Knitschi
Knitschi

Reputation: 3112

Can I replace calling the cmake -G<generator> option and calling vcvarsall.bat by setting a toolchain file?

When using CMake to generate a Visual Studio 15 Solution for the 64 bit architecture one has to first call vcvarsall.bat amd64 and then call cmake with the generator option cmake . -Bbuild -G"Visual Studio 14 2015 Win64". CMake will then determine the value of a couple of variables when executing the project() function.

CMAKE_GENERATOR: Visual Studio 14 2015 Win64
CMAKE_BUILD_TOOL: C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
CMAKE_CXX_COMPILER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
CMAKE_C_COMPILER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
CMAKE_LINKER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/link.exe
CMAKE_CXX_COMPILER_ID: MSVC
CMAKE_CXX_COMPILER_VERSION: 19.0.24215.1
CMAKE_VS_PLATFORM_NAME: x64

I would like to get rid of the call to vcvarsall.bat and the -G"generator" option by setting the values of the variables in a toolchain file like this:

# VisualStudio2015.cmake

set(CMAKE_GENERATOR "Visual Studio 14 2015 Win64" CACHE STRING "The CMake generator" FORCE )

set(CMAKE_BUILD_TOOL "C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe" CACHE FILEPATH "The visual studio build-system" FORCE)
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_LINKER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/link.exe" CACHE FILEPATH "Microsoft linker" FORCE)

set(CMAKE_CXX_COMPILER_ID MSCV CACHE STRING "The Id string of the compiler" FORCE)
set(CMAKE_CXX_COMPILER_VERSION 19.0.24215.1 CACHE STRING "The version of the compiler" FORCE)
set(CMAKE_VS_PLATFORM_NAME x64 CACHE STRING "Target processor architecture" FORCE)

and then call cmake with the CMAKE_TOOLCHAIN_FILE option:

cmake . -Bbuild -DCMAKE_TOOLCHAIN_FILE=VisualStudio2015.cmake

The problem is that this does not seem to work. When cmake executes the project() function it overrides the values for the compiler that I have set. So do I just forget to set some variables that are required or is this simply not possible?

Thank you for your time.

Upvotes: 3

Views: 2061

Answers (1)

Florian
Florian

Reputation: 43030

Turning my comment into an answer

I think what your are looking for is not a toolchain file (since you can't change the CMAKE_GENERATOR there), but the -C <initial-cache> option to "Pre-load a script to populate the cache".

And you shouldn't put too many of the values CMake normally detects/evaluates into the script.

So I've successfully tested the following:

VS2015InitialCache.cmake

set(CMAKE_GENERATOR "Visual Studio 14 2015 Win64" CACHE STRING "The CMake generator" FORCE)

set(CMAKE_CACHE_INIT_FILE "${CMAKE_CURRENT_LIST_FILE}" CACHE STRING "The CMake cache init file used" FORCE)

set(CMAKE_MAKE_PROGRAM "C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/devenv.com" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft CXX compiler" FORCE)
set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft C compiler" FORCE)

And then you call:

> cmake -CVS2015InitialCache.cmake -H. -Bbuild
loading initial cache file VS2015InitialCache.cmake
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
...

References

Upvotes: 2

Related Questions