MarkSchofield / WindowsToolchain

A repository containing a CMake toolchain for using MSVC
MIT License
106 stars 19 forks source link

MSVC_TOOLSET_VERSION unset when using EWDK #90

Closed ahogen closed 10 months ago

ahogen commented 10 months ago

Using EWDK with Visual Studio 2019 (v16.9.2), fe_release.20348.1.

Initialized my repo using "Windows.EWDK.toolchain.cmake" toolchain and "Ninja Multi-Config" generator.

I had a check in my CMakeLists.txt ensuring MSVC_TOOLSET_VERSION was at least 80 (VS 2005), but that CMake variable is unset, so my check fails. I've refactored my minimum version check to use MSVC_VERSION instead, which works.

MSVC_TOOLSET_VERSION used to be set worked when I used the "Visual Studio 16 2019" generator.

Project build scripts were otherwise generated successfully (ty! :grinning:).

MarkSchofield commented 10 months ago

Thanks for the bug! The toolchain files were setting MSVC_VERSION themselves, which ends-up by-passing CMake's built-in logic to calculate MSVC_TOOLSET_VERSION. If I remove the logic to set MSVC_VERSION in the toolchain, then MSVC_VERSION and MSVC_TOOLSET_VERSION will be correctly set by CMake. I'll try things out a bit more, and send a PR later.

ahogen commented 10 months ago

Ah okay, that makes sense. Not a huge deal for me, given that I was able to use MSVC_VERSION instead for my little minimum version check.

ahogen commented 10 months ago

This may be a different discussion topic, but have you considered using the version=<...> keys supported by CMake v3.27 CMAKE_GENERATOR_PLATFORM (or CMake 3.12 CMAKE_GENERATOR_TOOLSET) variables for the EWDK toolset? I may have come up with a rather simple CMake toolchain file using those extra keys/values.

# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2024 Engineering Design Team, Inc.
# Author: Alex Hogen <alex@edt.com>

set(CMAKE_SYSTEM_NAME        Windows)
set(CMAKE_SYSTEM_NAME       "${CMAKE_SYSTEM_NAME}" CACHE INTERNAL "")

set(CMAKE_SYSTEM_VERSION     "$ENV{Version_Number}")
set(CMAKE_SYSTEM_VERSION     "${CMAKE_SYSTEM_VERSION}" CACHE INTERNAL "")

set(CMAKE_GENERATOR_INSTANCE "$ENV{VSINSTALLDIR}")
set(CMAKE_GENERATOR_INSTANCE "${CMAKE_GENERATOR_INSTANCE}" CACHE INTERNAL "")

set(CMAKE_GENERATOR_PLATFORM "$ENV{VSCMD_ARG_TGT_ARCH},version=$ENV{Version_Number}")
set(CMAKE_GENERATOR_PLATFORM "${CMAKE_GENERATOR_PLATFORM}" CACHE INTERNAL "")

set(CMAKE_GENERATOR_TOOLSET   "host=$ENV{VSCMD_ARG_HOST_ARCH},version=$ENV{VCToolsVersion}")
set(CMAKE_GENERATOR_TOOLSET   "${CMAKE_GENERATOR_TOOLSET}" CACHE INTERNAL "")

# https://cmake.org/cmake/help/v3.15/variable/CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION.html
set(ENV{CMAKE_WINDOWS_KITS_10_DIR} "$ENV{WindowsSdkDir}")

I've only tested this with the "Visual Studio 16 2019" generator. The CMAKE_GENERATOR_PLATFORM "version=" in particular seems to help fix some previous issues I've had with CMake's ABI checks failing to find and link to kernel32.lib.

I'm wondering, has been tried and found to have some issue I haven't run in to yet?

Without "version" key in CMAKE_GENERATOR_PLATFORM Removing "version=$ENV{Version_Number}" from CMAKE_GENERATOR_PLATFORM results in this kernel32.lib link error: ```log -- The C compiler identification is MSVC 19.28.29913.0 -- The CXX compiler identification is MSVC 19.28.29913.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: E:/Program Files/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe -- Check for working C compiler: E:/Program Files/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - broken CMake Error at C:/Program Files/CMake/share/cmake-3.28/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "E:/Program Files/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe" is not able to compile a simple test program. It fails with the following output: [...] Link: E:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\build_x64\CMakeFiles\CMakeScratch\TryCompile-1y006g\Debug\cmTC_f204a.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/build_x64/CMakeFiles/CMakeScratch/TryCompile-1y006g/Debug/cmTC_f204a.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/build_x64/CMakeFiles/CMakeScratch/TryCompile-1y006g/Debug/cmTC_f204a.lib" /MACHINE:X64 /machine:x64 cmTC_f204a.dir\Debug\testCCompiler.obj LINK : fatal error LNK1104: cannot open file 'kernel32.lib' Done Building Project [...] CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:22 (project) -- Configuring incomplete, errors occurred! ```
MarkSchofield commented 10 months ago

The CMAKE_GENERATOR_PLATFORM/CMAKE_GENERATOR_TOOLSET variables only work with Visual Studio generators, which really isn't the purpose of the Toolchain files in this repo.

And from looking at your output, it looks like you're using EWDK environment variables for configuration but finding an installed Visual Studio..? I don't think the Visual Studio Generators work with EWDK build Windows.

Edit: Ah. I suppose you could probably use CMAKE_GENERATOR_INSTANCE to get the Visual Studio Generator to use the Visual Studio instance in the eWDK.

MarkSchofield commented 10 months ago

Fixed by #91.