From fd668186530d88340ecf32d2b55859a8cf6a8cb4 Mon Sep 17 00:00:00 2001 From: Calum Robinson Date: Tue, 17 Sep 2024 16:30:44 +0100 Subject: [PATCH] VS: Fix custom commands for DOTNET_SDK builds Fixes: #23723 --- Source/cmVisualStudio10TargetGenerator.cxx | 37 +++++++++---------- Source/cmVisualStudio10TargetGenerator.h | 2 - Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake | 2 +- ...etSdkCustomCommandsSource-build-stdout.txt | 1 + ...VsDotnetSdkCustomCommandsSource-result.txt | 0 ...VsDotnetSdkCustomCommandsSource-stderr.txt | 7 ---- .../VsDotnetSdkCustomCommandsSource.cmake | 5 +-- 7 files changed, 21 insertions(+), 33 deletions(-) create mode 100644 Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-build-stdout.txt delete mode 100644 Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-result.txt delete mode 100644 Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-stderr.txt diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index 883d78730c..71a140b7db 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -907,16 +907,6 @@ void cmVisualStudio10TargetGenerator::WriteSdkStyleProjectFile( return; } - if (this->HasCustomCommandsSource()) { - std::string message = cmStrCat( - "The target \"", this->GeneratorTarget->GetName(), - "\" does not currently support add_custom_command as the Visual Studio " - "generators have not yet learned how to generate custom commands in " - ".Net SDK-style projects."); - this->Makefile->IssueMessage(MessageType::FATAL_ERROR, message); - return; - } - Elem e0(BuildFileStream, "Project"); e0.Attribute("Sdk", *this->GeneratorTarget->GetProperty("DOTNET_SDK")); @@ -1016,6 +1006,7 @@ void cmVisualStudio10TargetGenerator::WriteSdkStyleProjectFile( } this->WriteDotNetDocumentationFile(e0); + this->WriteCustomCommands(e0); this->WriteAllSources(e0); this->WriteEmbeddedResourceGroup(e0); this->WriteXamlFilesGroup(e0); @@ -1077,15 +1068,6 @@ void cmVisualStudio10TargetGenerator::WriteCommonPropertyGroupGlobals(Elem& e1) } } -bool cmVisualStudio10TargetGenerator::HasCustomCommandsSource() const -{ - auto const& config_sources = this->GeneratorTarget->GetAllConfigSources(); - return std::any_of(config_sources.begin(), config_sources.end(), - [](cmGeneratorTarget::AllConfigSource const& si) { - return si.Source->GetCustomCommand(); - }); -} - void cmVisualStudio10TargetGenerator::WritePackageReferences(Elem& e0) { std::vector packageReferences = @@ -1843,6 +1825,15 @@ void cmVisualStudio10TargetGenerator::WriteCustomRule( symbolic = sf->GetPropertyAsBool("SYMBOLIC"); } } + + // Without UpToDateCheckInput VS will ignore the dependency files + // when doing it's fast up-to-date check and the command will not run + if (this->ProjectType == VsProjectType::csproj && + this->GeneratorTarget->IsDotNetSdkTarget()) { + Elem e1(e0, "ItemGroup"); + Elem e2(e1, "UpToDateCheckInput"); + e2.Attribute("Include", dep); + } } } if (this->ProjectType != VsProjectType::csproj) { @@ -1936,10 +1927,16 @@ void cmVisualStudio10TargetGenerator::WriteCustomRuleCSharp( } this->CSharpCustomCommandNames.insert(name); Elem e1(e0, "Target"); - e1.Attribute("Condition", this->CalcCondition(config)); + e1.Attribute("Condition", cmStrCat("'$(Configuration)' == '", config, '\'')); e1.S << "\n Name=\"" << name << "\""; e1.S << "\n Inputs=\"" << cmVS10EscapeAttr(inputs) << "\""; e1.S << "\n Outputs=\"" << cmVS10EscapeAttr(outputs) << "\""; + + // Run before sources are compiled... + e1.S << "\n BeforeTargets=\"CoreCompile\""; // BeforeBuild + // ...but after output directory has been created + e1.S << "\n DependsOnTargets=\"PrepareForBuild\""; + if (!comment.empty()) { Elem(e1, "Exec").Attribute("Command", cmStrCat("echo ", comment)); } diff --git a/Source/cmVisualStudio10TargetGenerator.h b/Source/cmVisualStudio10TargetGenerator.h index aae9ca3f91..9b7ae10a25 100644 --- a/Source/cmVisualStudio10TargetGenerator.h +++ b/Source/cmVisualStudio10TargetGenerator.h @@ -289,8 +289,6 @@ private: void WriteCommonPropertyGroupGlobals( cmVisualStudio10TargetGenerator::Elem& e1); - bool HasCustomCommandsSource() const; - std::unordered_map ParsedToolTargetSettings; bool PropertyIsSameInAllConfigs(const ConfigToSettings& toolSettings, const std::string& propName); diff --git a/Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake b/Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake index 68a6d5ce21..276451d6c7 100644 --- a/Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake +++ b/Tests/RunCMake/VsDotnetSdk/RunCMakeTest.cmake @@ -1,7 +1,6 @@ cmake_policy(SET CMP0053 NEW) include(RunCMake) -run_cmake(VsDotnetSdkCustomCommandsSource) run_cmake(VsDotnetSdkStartupObject) run_cmake(VsDotnetSdkDefines) run_cmake(DotnetSdkVariables) @@ -30,3 +29,4 @@ endfunction() runCmakeAndBuild(VsDotnetSdkCustomCommandsTarget) runCmakeAndBuild(VsDotnetSdkNugetRestore) +runCmakeAndBuild(VsDotnetSdkCustomCommandsSource) diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-build-stdout.txt b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-build-stdout.txt new file mode 100644 index 0000000000..9afaa2be90 --- /dev/null +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-build-stdout.txt @@ -0,0 +1 @@ +Generating bar.cs diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-result.txt b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-result.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-stderr.txt b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-stderr.txt deleted file mode 100644 index 90af627f3d..0000000000 --- a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource-stderr.txt +++ /dev/null @@ -1,7 +0,0 @@ -CMake Error in CMakeLists.txt: - The target "foo" does not currently support add_custom_command as the - Visual Studio generators have not yet learned how to generate custom - commands in .Net SDK-style projects. - - -CMake Generate step failed. Build files cannot be regenerated correctly. diff --git a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource.cmake b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource.cmake index af18946034..d6166dbcfd 100644 --- a/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource.cmake +++ b/Tests/RunCMake/VsDotnetSdk/VsDotnetSdkCustomCommandsSource.cmake @@ -7,9 +7,8 @@ endif() set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk") add_custom_command( OUTPUT bar.cs - COMMAND copy /A ${CMAKE_CURRENT_SOURCE_DIR}/lib1.cs - bar.cs - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/lib1.cs + COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/lib1.cs" bar.cs + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/lib1.cs" VERBATIM) add_library(foo SHARED bar.cs)