1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 12:16:40 +08:00

VS: Support add_custom_command in .Net SDK-style projects

Fixes: #26048
This commit is contained in:
Calum Robinson
2024-06-14 16:19:44 +01:00
parent c1e10bad81
commit c7e5ebd0a8
7 changed files with 92 additions and 19 deletions

View File

@@ -914,7 +914,7 @@ void cmVisualStudio10TargetGenerator::WriteSdkStyleProjectFile(
return;
}
if (this->HasCustomCommands()) {
if (this->HasCustomCommandsSource()) {
std::string message = cmStrCat(
"The target \"", this->GeneratorTarget->GetName(),
"\" does not currently support add_custom_command as the Visual Studio "
@@ -1005,7 +1005,6 @@ void cmVisualStudio10TargetGenerator::WriteSdkStyleProjectFile(
e1.Attribute("Condition",
cmStrCat("'$(Configuration)' == '", config, '\''));
e1.SetHasElements();
this->WriteEvents(e1, config);
std::string outDir =
cmStrCat(this->GeneratorTarget->GetDirectory(config), '/');
@@ -1017,6 +1016,10 @@ void cmVisualStudio10TargetGenerator::WriteSdkStyleProjectFile(
oh.OutputFlagMap();
}
for (const std::string& config : this->Configurations) {
this->WriteSdkStyleEvents(e0, config);
}
this->WriteDotNetDocumentationFile(e0);
this->WriteAllSources(e0);
this->WriteEmbeddedResourceGroup(e0);
@@ -1079,14 +1082,8 @@ void cmVisualStudio10TargetGenerator::WriteCommonPropertyGroupGlobals(Elem& e1)
}
}
bool cmVisualStudio10TargetGenerator::HasCustomCommands() const
bool cmVisualStudio10TargetGenerator::HasCustomCommandsSource() const
{
if (!this->GeneratorTarget->GetPreBuildCommands().empty() ||
!this->GeneratorTarget->GetPreLinkCommands().empty() ||
!this->GeneratorTarget->GetPostBuildCommands().empty()) {
return true;
}
auto const& config_sources = this->GeneratorTarget->GetAllConfigSources();
return std::any_of(config_sources.begin(), config_sources.end(),
[](cmGeneratorTarget::AllConfigSource const& si) {
@@ -4871,6 +4868,71 @@ void cmVisualStudio10TargetGenerator::WriteEvent(
}
}
void cmVisualStudio10TargetGenerator::WriteSdkStyleEvents(
Elem& e0, std::string const& configName)
{
this->WriteSdkStyleEvent(e0, "PreLink", "BeforeTargets", "Link",
this->GeneratorTarget->GetPreLinkCommands(),
configName);
this->WriteSdkStyleEvent(e0, "PreBuild", "BeforeTargets", "PreBuildEvent",
this->GeneratorTarget->GetPreBuildCommands(),
configName);
this->WriteSdkStyleEvent(e0, "PostBuild", "AfterTargets", "PostBuildEvent",
this->GeneratorTarget->GetPostBuildCommands(),
configName);
}
void cmVisualStudio10TargetGenerator::WriteSdkStyleEvent(
Elem& e0, const std::string& name, const std::string& when,
const std::string& target, std::vector<cmCustomCommand> const& commands,
std::string const& configName)
{
if (commands.empty()) {
return;
}
Elem e1(e0, "Target");
e1.Attribute("Condition",
cmStrCat("'$(Configuration)' == '", configName, '\''));
e1.Attribute("Name", name + configName);
e1.Attribute(when.c_str(), target);
e1.SetHasElements();
cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
std::string script;
const char* pre = "";
std::string comment;
bool stdPipesUTF8 = false;
for (cmCustomCommand const& cc : commands) {
cmCustomCommandGenerator ccg(cc, configName, lg);
if (!ccg.HasOnlyEmptyCommandLines()) {
comment += pre;
comment += lg->ConstructComment(ccg);
script += pre;
pre = "\n";
script += lg->ConstructScript(ccg);
stdPipesUTF8 = stdPipesUTF8 || cc.GetStdPipesUTF8();
}
}
if (!script.empty()) {
script += lg->FinishConstructScript(this->ProjectType);
}
comment = cmVS10EscapeComment(comment);
std::string strippedComment = comment;
strippedComment.erase(
std::remove(strippedComment.begin(), strippedComment.end(), '\t'),
strippedComment.end());
std::ostringstream oss;
if (!comment.empty() && !strippedComment.empty()) {
oss << "echo " << comment << "\n";
}
oss << script << "\n";
Elem e2(e1, "Exec");
e2.Attribute("Command", oss.str());
}
void cmVisualStudio10TargetGenerator::WriteProjectReferences(Elem& e0)
{
cmGlobalGenerator::TargetDependSet const& unordered =

View File

@@ -193,6 +193,11 @@ private:
void WriteEvent(Elem& e1, std::string const& name,
std::vector<cmCustomCommand> const& commands,
std::string const& configName);
void WriteSdkStyleEvents(Elem& e0, std::string const& configName);
void WriteSdkStyleEvent(Elem& e0, const std::string& name,
const std::string& when, const std::string& target,
std::vector<cmCustomCommand> const& commands,
std::string const& configName);
void WriteGroupSources(Elem& e0, std::string const& name,
ToolSources const& sources,
std::vector<cmSourceGroup>&);
@@ -282,7 +287,7 @@ private:
void WriteCommonPropertyGroupGlobals(
cmVisualStudio10TargetGenerator::Elem& e1);
bool HasCustomCommands() const;
bool HasCustomCommandsSource() const;
std::unordered_map<std::string, ConfigToSettings> ParsedToolTargetSettings;
bool PropertyIsSameInAllConfigs(const ConfigToSettings& toolSettings,

View File

@@ -1,7 +1,6 @@
cmake_policy(SET CMP0053 NEW)
include(RunCMake)
run_cmake(VsDotnetSdkCustomCommandsTarget)
run_cmake(VsDotnetSdkCustomCommandsSource)
run_cmake(VsDotnetSdkStartupObject)
run_cmake(VsDotnetSdkDefines)
@@ -18,3 +17,16 @@ function(run_VsDotnetSdk)
run_cmake_command(VsDotnetSdk-build ${CMAKE_COMMAND} --build . -- ${build_flags})
endfunction()
run_VsDotnetSdk()
function(runCmakeAndBuild CASE)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
set(RunCMake_TEST_NO_CLEAN 1)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
run_cmake(${CASE})
set(build_flags /restore)
run_cmake_command(${CASE}-build ${CMAKE_COMMAND} --build . -- ${build_flags})
run_cmake_command(${CASE}-build ${CMAKE_COMMAND} --build .)
endfunction()
runCmakeAndBuild(VsDotnetSdkCustomCommandsTarget)

View File

@@ -0,0 +1 @@
.*"This should happen!".*

View File

@@ -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.

View File

@@ -8,5 +8,5 @@ set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk")
add_library(foo SHARED lib1.cs)
add_custom_command(TARGET foo
PRE_BUILD
COMMAND echo "This shouldn't happen!"
COMMAND echo "This should happen!"
VERBATIM)