From 6511654164dd483b4ff0c3eb5775d5fdb2c53564 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Oct 2021 11:56:42 -0400 Subject: cmGlobalVisualStudio10Generator: Allow subclasses to reset MSBuild search While at it, convert to inline initialization. --- Source/cmGlobalVisualStudio10Generator.cxx | 1 - Source/cmGlobalVisualStudio10Generator.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Source/cmGlobalVisualStudio10Generator.cxx b/Source/cmGlobalVisualStudio10Generator.cxx index 488ff2e..499efbb 100644 --- a/Source/cmGlobalVisualStudio10Generator.cxx +++ b/Source/cmGlobalVisualStudio10Generator.cxx @@ -139,7 +139,6 @@ cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator( "ProductDir", vc10Express, cmSystemTools::KeyWOW64_32); this->CudaEnabled = false; - this->MSBuildCommandInitialized = false; { std::string envPlatformToolset; if (cmSystemTools::GetEnv("PlatformToolset", envPlatformToolset) && diff --git a/Source/cmGlobalVisualStudio10Generator.h b/Source/cmGlobalVisualStudio10Generator.h index b7ae1ee..6c8e5f7 100644 --- a/Source/cmGlobalVisualStudio10Generator.h +++ b/Source/cmGlobalVisualStudio10Generator.h @@ -222,6 +222,7 @@ protected: bool SystemIsWindowsPhone = false; bool SystemIsWindowsStore = false; bool SystemIsAndroid = false; + bool MSBuildCommandInitialized = false; private: class Factory; @@ -243,7 +244,6 @@ private: LongestSourcePath LongestSource; std::string MSBuildCommand; - bool MSBuildCommandInitialized; std::set AndroidExecutableWarnings; virtual std::string FindMSBuildCommand(); std::string FindDevEnvCommand() override; -- cgit v0.12 From 8917b8512f20147b22ac8a7bf78245ab0162acaf Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Oct 2021 11:58:39 -0400 Subject: cmGlobalVisualStudioVersionedGenerator: Allow repeating SetGeneratorInstance --- Source/cmGlobalVisualStudioVersionedGenerator.cxx | 6 ++++++ Source/cmGlobalVisualStudioVersionedGenerator.h | 3 +++ 2 files changed, 9 insertions(+) diff --git a/Source/cmGlobalVisualStudioVersionedGenerator.cxx b/Source/cmGlobalVisualStudioVersionedGenerator.cxx index 3ecf32e..b6219a6 100644 --- a/Source/cmGlobalVisualStudioVersionedGenerator.cxx +++ b/Source/cmGlobalVisualStudioVersionedGenerator.cxx @@ -436,6 +436,9 @@ bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName( bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance( std::string const& i, cmMakefile* mf) { + if (this->GeneratorInstance && i == *(this->GeneratorInstance)) { + return true; + } if (!i.empty()) { if (!this->vsSetupAPIHelper.SetVSInstance(i)) { std::ostringstream e; @@ -472,6 +475,9 @@ bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance( cmStateEnums::INTERNAL); } + // The selected instance may have a different MSBuild than previously found. + this->MSBuildCommandInitialized = false; + return true; } diff --git a/Source/cmGlobalVisualStudioVersionedGenerator.h b/Source/cmGlobalVisualStudioVersionedGenerator.h index f07492d..691651f 100644 --- a/Source/cmGlobalVisualStudioVersionedGenerator.h +++ b/Source/cmGlobalVisualStudioVersionedGenerator.h @@ -8,6 +8,8 @@ #include #include +#include + #include "cmGlobalVisualStudio14Generator.h" #include "cmVSSetupHelper.h" @@ -72,4 +74,5 @@ private: class Factory17; friend class Factory17; mutable cmVSSetupAPIHelper vsSetupAPIHelper; + cm::optional GeneratorInstance; }; -- cgit v0.12 From 7f730464be485862ba89c85248306c893117a595 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Oct 2021 11:59:44 -0400 Subject: cmGlobalGenerator: Add method to check if generator is at least VS 10 --- Source/cmGlobalGenerator.h | 2 ++ Source/cmGlobalVisualStudio10Generator.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h index 23c5700..074da4e 100644 --- a/Source/cmGlobalGenerator.h +++ b/Source/cmGlobalGenerator.h @@ -444,6 +444,8 @@ public: virtual bool IsVisualStudio() const { return false; } + virtual bool IsVisualStudioAtLeast10() const { return false; } + virtual bool IsNinja() const { return false; } /** Return true if we know the exact location of object files. diff --git a/Source/cmGlobalVisualStudio10Generator.h b/Source/cmGlobalVisualStudio10Generator.h index 6c8e5f7..a5ee284 100644 --- a/Source/cmGlobalVisualStudio10Generator.h +++ b/Source/cmGlobalVisualStudio10Generator.h @@ -20,6 +20,8 @@ class cmGlobalVisualStudio10Generator : public cmGlobalVisualStudio8Generator public: static std::unique_ptr NewFactory(); + bool IsVisualStudioAtLeast10() const override { return true; } + bool MatchesGeneratorName(const std::string& name) const override; bool SetSystemName(std::string const& s, cmMakefile* mf) override; -- cgit v0.12 From 6999b871335c26d9d39b05023559254b1437bcf5 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Oct 2021 12:01:53 -0400 Subject: cmGlobalVisualStudio10Generator: Add method to find MSBuild early Add a way to find MSBuild before the main `FindMakeProgram` code path has executed. --- Source/cmGlobalVisualStudio10Generator.cxx | 6 ++++++ Source/cmGlobalVisualStudio10Generator.h | 2 ++ Source/cmGlobalVisualStudioVersionedGenerator.cxx | 11 +++++++++++ Source/cmGlobalVisualStudioVersionedGenerator.h | 2 ++ 4 files changed, 21 insertions(+) diff --git a/Source/cmGlobalVisualStudio10Generator.cxx b/Source/cmGlobalVisualStudio10Generator.cxx index 499efbb..c11516d 100644 --- a/Source/cmGlobalVisualStudio10Generator.cxx +++ b/Source/cmGlobalVisualStudio10Generator.cxx @@ -856,6 +856,12 @@ std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand() return this->MSBuildCommand; } +cm::optional +cmGlobalVisualStudio10Generator::FindMSBuildCommandEarly(cmMakefile*) +{ + return this->GetMSBuildCommand(); +} + std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand() { std::string msbuild; diff --git a/Source/cmGlobalVisualStudio10Generator.h b/Source/cmGlobalVisualStudio10Generator.h index a5ee284..6e62390 100644 --- a/Source/cmGlobalVisualStudio10Generator.h +++ b/Source/cmGlobalVisualStudio10Generator.h @@ -134,6 +134,8 @@ public: bool GetSupportsUnityBuilds() const { return this->SupportsUnityBuilds; } + virtual cm::optional FindMSBuildCommandEarly(cmMakefile* mf); + bool FindMakeProgram(cmMakefile* mf) override; bool IsIPOSupported() const override { return true; } diff --git a/Source/cmGlobalVisualStudioVersionedGenerator.cxx b/Source/cmGlobalVisualStudioVersionedGenerator.cxx index b6219a6..ec2e74f 100644 --- a/Source/cmGlobalVisualStudioVersionedGenerator.cxx +++ b/Source/cmGlobalVisualStudioVersionedGenerator.cxx @@ -718,6 +718,17 @@ cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault( return std::string(); } +cm::optional +cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile* mf) +{ + std::string instance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE"); + if (!this->SetGeneratorInstance(instance, mf)) { + cmSystemTools::SetFatalErrorOccured(); + return {}; + } + return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf); +} + std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand() { std::string msbuild; diff --git a/Source/cmGlobalVisualStudioVersionedGenerator.h b/Source/cmGlobalVisualStudioVersionedGenerator.h index 691651f..b7760ac 100644 --- a/Source/cmGlobalVisualStudioVersionedGenerator.h +++ b/Source/cmGlobalVisualStudioVersionedGenerator.h @@ -31,6 +31,8 @@ public: bool GetVSInstance(std::string& dir) const; + cm::optional FindMSBuildCommandEarly(cmMakefile* mf) override; + cm::optional GetVSInstanceVersion() const override; AuxToolset FindAuxToolset(std::string& version, -- cgit v0.12 From 44a86d0b3890848e2df702dc87d625a19216641e Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Oct 2021 12:05:24 -0400 Subject: cmake_host_system_information: Add undocumented VS_MSBUILD_COMMAND key When using the Visual Studio generator for VS 10 or above, offer this key to get the location of the MSBuild command before the first `project()` or `enable_language()` command has finished running. This will be needed only by one of our own modules, so leave it undocumented for now. --- Source/cmCMakeHostSystemInformationCommand.cxx | 7 +++++++ Tests/RunCMake/cmake_host_system_information/RunCMakeTest.cmake | 6 ++++++ Tests/RunCMake/cmake_host_system_information/VsMSBuild.cmake | 4 ++++ .../cmake_host_system_information/VsMSBuildMissing-result.txt | 1 + .../cmake_host_system_information/VsMSBuildMissing-stderr.txt | 4 ++++ .../RunCMake/cmake_host_system_information/VsMSBuildMissing.cmake | 1 + 6 files changed, 23 insertions(+) create mode 100644 Tests/RunCMake/cmake_host_system_information/VsMSBuild.cmake create mode 100644 Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-result.txt create mode 100644 Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-stderr.txt create mode 100644 Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing.cmake diff --git a/Source/cmCMakeHostSystemInformationCommand.cxx b/Source/cmCMakeHostSystemInformationCommand.cxx index 74071ff..3922c56 100644 --- a/Source/cmCMakeHostSystemInformationCommand.cxx +++ b/Source/cmCMakeHostSystemInformationCommand.cxx @@ -27,6 +27,7 @@ #ifdef _WIN32 # include "cmAlgorithms.h" # include "cmGlobalGenerator.h" +# include "cmGlobalVisualStudio10Generator.h" # include "cmGlobalVisualStudioVersionedGenerator.h" # include "cmVSSetupHelper.h" # define HAVE_VS_SETUP_HELPER @@ -434,6 +435,12 @@ cm::optional GetValue(cmExecutionStatus& status, } } + if (key == "VS_MSBUILD_COMMAND"_s && gg->IsVisualStudioAtLeast10()) { + cmGlobalVisualStudio10Generator* vs10gen = + static_cast(gg); + return vs10gen->FindMSBuildCommandEarly(&status.GetMakefile()); + } + return {}; } #endif diff --git a/Tests/RunCMake/cmake_host_system_information/RunCMakeTest.cmake b/Tests/RunCMake/cmake_host_system_information/RunCMakeTest.cmake index 189013f..87b6944 100644 --- a/Tests/RunCMake/cmake_host_system_information/RunCMakeTest.cmake +++ b/Tests/RunCMake/cmake_host_system_information/RunCMakeTest.cmake @@ -15,3 +15,9 @@ run_cmake(CentOS6) run_cmake(Debian6) run_cmake(UserFallbackScript) + +if(RunCMake_GENERATOR MATCHES "^Visual Studio " AND NOT RunCMake_GENERATOR STREQUAL "Visual Studio 9 2008") + run_cmake(VsMSBuild) +else() + run_cmake(VsMSBuildMissing) +endif() diff --git a/Tests/RunCMake/cmake_host_system_information/VsMSBuild.cmake b/Tests/RunCMake/cmake_host_system_information/VsMSBuild.cmake new file mode 100644 index 0000000..30e616e --- /dev/null +++ b/Tests/RunCMake/cmake_host_system_information/VsMSBuild.cmake @@ -0,0 +1,4 @@ +cmake_host_system_information(RESULT msbuild QUERY VS_MSBUILD_COMMAND) +if(NOT EXISTS "${msbuild}") + message(FATAL_ERROR "VS_MSBUILD_COMMAND returned path that does not exist:\n ${msbuild}") +endif() diff --git a/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-result.txt b/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-stderr.txt b/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-stderr.txt new file mode 100644 index 0000000..3a7dfef --- /dev/null +++ b/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at VsMSBuildMissing.cmake:[0-9]+ \(cmake_host_system_information\): + cmake_host_system_information does not recognize VS_MSBUILD_COMMAND +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing.cmake b/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing.cmake new file mode 100644 index 0000000..23c0364 --- /dev/null +++ b/Tests/RunCMake/cmake_host_system_information/VsMSBuildMissing.cmake @@ -0,0 +1 @@ +cmake_host_system_information(RESULT msbuild QUERY VS_MSBUILD_COMMAND) -- cgit v0.12 From 42418b02f769858d68ce21a58dab3025d30f31a2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Oct 2021 12:07:59 -0400 Subject: Android: Refactor sysroot detection under Visual Studio Since commit 6051a49c78 (Visual Studio: Add Android support, 2020-06-12, v3.19.0-rc1~619^2) we run MSBuild to build a sample project to detect the sysroot. Previously we relied on `CMAKE_VS_MSBUILD_COMMAND` being available. That required commit d5b5c19278 (cmGlobalGenerator: FindMakeProgram() before CMakeDetermineSystem, 2020-06-15, v3.19.0-rc1~619^2~3) to make it available early enough. However, that ordering broke `CMAKE_GENERATOR_INSTANCE` so we need to prepare to revert it. Use `cmake_host_system_information` to get the location of MSBuild under a VS generator instead. --- Modules/Platform/Android-Determine.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/Platform/Android-Determine.cmake b/Modules/Platform/Android-Determine.cmake index 7b67f83..a4e9574 100644 --- a/Modules/Platform/Android-Determine.cmake +++ b/Modules/Platform/Android-Determine.cmake @@ -52,14 +52,16 @@ if(CMAKE_GENERATOR MATCHES "Visual Studio") endif() configure_file(${CMAKE_ROOT}/Modules/Platform/Android/VCXProjInspect.vcxproj.in ${CMAKE_PLATFORM_INFO_DIR}/VCXProjInspect.vcxproj @ONLY) + cmake_host_system_information(RESULT _msbuild QUERY VS_MSBUILD_COMMAND) # undocumented query execute_process( - COMMAND "${CMAKE_VS_MSBUILD_COMMAND}" "VCXProjInspect.vcxproj" + COMMAND "${_msbuild}" "VCXProjInspect.vcxproj" "/p:Configuration=Debug" "/p:Platform=${vcx_platform}" WORKING_DIRECTORY ${CMAKE_PLATFORM_INFO_DIR} OUTPUT_VARIABLE VCXPROJ_INSPECT_OUTPUT ERROR_VARIABLE VCXPROJ_INSPECT_OUTPUT RESULT_VARIABLE VCXPROJ_INSPECT_RESULT ) + unset(_msbuild) if(NOT CMAKE_SYSROOT AND VCXPROJ_INSPECT_OUTPUT MATCHES "CMAKE_SYSROOT=([^%\r\n]+)[\r\n]") # Strip VS diagnostic output from the end of the line. string(REGEX REPLACE " \\(TaskId:[0-9]*\\)$" "" _sysroot "${CMAKE_MATCH_1}") -- cgit v0.12 From 46e9ff57298b58b3a4669adcb913429b691ce5bb Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 20 Oct 2021 12:12:35 -0400 Subject: Restore honoring CMAKE_GENERATOR_INSTANCE in toolchain file Revert the changes from commit d5b5c19278 (cmGlobalGenerator: FindMakeProgram() before CMakeDetermineSystem, 2020-06-15, v3.19.0-rc1~619^2~3) and commit ef91fb02f3 (cmGlobalGenerator: FindMakeProgram() at a generator-specific time, 2020-11-23, v3.19.1~2^2). We must delay selecting the location of MSBuild until after an instance of Visual Studio has been selected. It is now safe to revert the ordering because the motivating use case (sysroot detection in Platform/Android-Determine) has been implemented another way. Fixes: #22782 --- Source/cmGlobalGenerator.cxx | 13 +------------ Source/cmGlobalGenerator.h | 11 ----------- Source/cmGlobalVisualStudioGenerator.h | 5 ----- Source/cmGlobalXCodeGenerator.h | 5 ----- 4 files changed, 1 insertion(+), 33 deletions(-) diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 6dabf637..ac283ab 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -593,16 +593,6 @@ void cmGlobalGenerator::EnableLanguage( } } - if (readCMakeSystem) { - // Find the native build tool for this generator. - // This has to be done early so that MSBuild can be used to examine the - // cross-compilation environment. - if (this->GetFindMakeProgramStage() == FindMakeProgramStage::Early && - !this->FindMakeProgram(mf)) { - return; - } - } - // Load the CMakeDetermineSystem.cmake file and find out // what platform we are running on if (!mf->GetDefinition("CMAKE_SYSTEM")) { @@ -676,8 +666,7 @@ void cmGlobalGenerator::EnableLanguage( } // Find the native build tool for this generator. - if (this->GetFindMakeProgramStage() == FindMakeProgramStage::Late && - !this->FindMakeProgram(mf)) { + if (!this->FindMakeProgram(mf)) { return; } } diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h index 074da4e..cc0ad29 100644 --- a/Source/cmGlobalGenerator.h +++ b/Source/cmGlobalGenerator.h @@ -625,17 +625,6 @@ protected: std::string GetPredefinedTargetsFolder() const; - enum class FindMakeProgramStage - { - Early, - Late, - }; - - virtual FindMakeProgramStage GetFindMakeProgramStage() const - { - return FindMakeProgramStage::Late; - } - private: using TargetMap = std::unordered_map; using GeneratorTargetMap = diff --git a/Source/cmGlobalVisualStudioGenerator.h b/Source/cmGlobalVisualStudioGenerator.h index d1bd6b1..23c8a02 100644 --- a/Source/cmGlobalVisualStudioGenerator.h +++ b/Source/cmGlobalVisualStudioGenerator.h @@ -167,11 +167,6 @@ protected: void WriteSLNHeader(std::ostream& fout); - FindMakeProgramStage GetFindMakeProgramStage() const override - { - return FindMakeProgramStage::Early; - } - bool ComputeTargetDepends() override; class VSDependSet : public std::set { diff --git a/Source/cmGlobalXCodeGenerator.h b/Source/cmGlobalXCodeGenerator.h index 72af4a4..4d7ee90 100644 --- a/Source/cmGlobalXCodeGenerator.h +++ b/Source/cmGlobalXCodeGenerator.h @@ -138,11 +138,6 @@ protected: void AddExtraIDETargets() override; void Generate() override; - FindMakeProgramStage GetFindMakeProgramStage() const override - { - return FindMakeProgramStage::Early; - } - private: enum EmbedActionFlags { -- cgit v0.12