triclosan
triclosan

Reputation: 5724

Variables are empty in toolchain file

I try to use MSVC and MSVC_VERSION in toolchain file -

cmake-toolchain-file.cmake

message (STATUS "Toolchain MSVC=${MSVC} MSVC_VERSION=${MSVC_VERSION}")

CMakeLists.txt

message (STATUS "Project MSVC=${MSVC} MSVC_VERSION=${MSVC_VERSION}")

Output is

> cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/cmake-toolchain-file.cmake
-- Toolchain MSVC= MSVC_VERSION=
-- Project MSVC=1 MSVC_VERSION=1800
-- Bulding target for: windows-x86
-- Configuring done
-- Generating done
-- Build files have been written to: D:/test/build

Is it normal behavior that I get MSVC and MSVC_VERSION from toolchain file empty?

Upvotes: 1

Views: 623

Answers (1)

user2288008
user2288008

Reputation:

Is it normal behavior that I get MSVC and MSVC_VERSION from toolchain file empty?

Yes. Variables initialized after project command. This is where toolchain read also. Try to add some extra messages:

cmake_minimum_required(VERSION 3.0)
message(STATUS "Before project: MSVC(${MSVC})")
project(Foo)
message(STATUS "After project: MSVC(${MSVC})")

Result:

-- Before project: MSVC()
-- Toolchain MSVC()
-- Toolchain MSVC()
...
-- After project: MSVC(1)
-- Configuring done
-- Generating done

So I guess you have one hidden question in mind:

Is it confusing?

Yes, for the Visual Studio generators. Since usually you can set some critical stuff like path to compiler in toolchain it doesn't make sense to read compiler-related variables like CMAKE_CXX_COMPILER_ID before toolchain processed, i.e. before project command. But when you set generator to Visual Studio your compiler is always MSVC. This gives you a hint for the workaround. Just check CMAKE_GENERATOR variable in toolchain instead of MSVC_VERSION (though this will not work for NMake generator of course):

if("${CMAKE_GENERATOR}" STREQUAL "Visual Studio 12 2013")
  message("Toolchain: MSVC 1800")
endif()

if("${CMAKE_GENERATOR}" STREQUAL "Visual Studio 9 2008")
  message("Toolchain: MSVC 1500")
endif()

Upvotes: 2

Related Questions