From 213fec4908a13bb0f414533eddfd72481bc50694 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Sun, 1 Aug 2021 11:46:20 +0200 Subject: add_custom_command(DEPFILE) independent from CMAKE_DEPENDS_USE_COMPILER Fixes: #22486 --- Source/cmMakefileTargetGenerator.cxx | 22 ++++++++++++++++++++-- ...mCommandDependencies-compiler-deps-legacy.cmake | 9 +++++++++ Tests/RunCMake/BuildDepends/RunCMakeTest.cmake | 4 ++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/BuildDepends/CustomCommandDependencies-compiler-deps-legacy.cmake diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 98c61fe..a3e5553 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -324,9 +324,25 @@ void cmMakefileTargetGenerator::WriteCommonCodeRules() this->LocalGenerator->MaybeRelativeToTopBinDir(dependFileNameFull)) << "\n"; + // Scan any custom commands to check if DEPFILE option is specified + bool ccGenerateDeps = false; + std::vector customCommands; + this->GeneratorTarget->GetCustomCommands(customCommands, + this->GetConfigName()); + for (cmSourceFile const* sf : customCommands) { + if (!sf->GetCustomCommand()->GetDepfile().empty()) { + ccGenerateDeps = true; + break; + } + } + std::string depsUseCompiler = "CMAKE_DEPENDS_USE_COMPILER"; - if (!this->Makefile->IsDefinitionSet(depsUseCompiler) || - this->Makefile->IsOn(depsUseCompiler)) { + bool compilerGenerateDeps = + this->GlobalGenerator->SupportsCompilerDependencies() && + (!this->Makefile->IsDefinitionSet(depsUseCompiler) || + this->Makefile->IsOn(depsUseCompiler)); + + if (compilerGenerateDeps || ccGenerateDeps) { std::string compilerDependFile = cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.make"); *this->BuildFileStream << "# Include any dependencies generated by the " @@ -361,7 +377,9 @@ void cmMakefileTargetGenerator::WriteCommonCodeRules() "management for " << this->GeneratorTarget->GetName() << ".\n"; } + } + if (compilerGenerateDeps) { // deactivate no longer needed legacy dependency files // Write an empty dependency file. cmGeneratedFileStream legacyDepFileStream( diff --git a/Tests/RunCMake/BuildDepends/CustomCommandDependencies-compiler-deps-legacy.cmake b/Tests/RunCMake/BuildDepends/CustomCommandDependencies-compiler-deps-legacy.cmake new file mode 100644 index 0000000..e13efb3 --- /dev/null +++ b/Tests/RunCMake/BuildDepends/CustomCommandDependencies-compiler-deps-legacy.cmake @@ -0,0 +1,9 @@ +enable_language(C) + +add_custom_command(OUTPUT main.c + DEPFILE main.c.d + COMMAND "${CMAKE_COMMAND}" -DINFILE=${CMAKE_CURRENT_SOURCE_DIR}/main.c -DOUTFILE=main.c -DDEPFILE=main.c.d + -P "${CMAKE_CURRENT_SOURCE_DIR}/GenerateDepFile.cmake" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") + +add_executable(main ${CMAKE_CURRENT_BINARY_DIR}/main.c) diff --git a/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake b/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake index f8c20c2..27bbff6 100644 --- a/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake +++ b/Tests/RunCMake/BuildDepends/RunCMakeTest.cmake @@ -170,6 +170,10 @@ endif() if (RunCMake_GENERATOR MATCHES "Makefiles") run_cmake(CustomCommandDependencies-BadArgs) + run_cmake_with_options(CustomCommandDependencies-compiler-deps-legacy -DCMAKE_DEPENDS_USE_COMPILER=FALSE) + set(RunCMake_TEST_NO_CLEAN 1) + run_cmake_command(CustomCommandDependencies-compiler-deps-legacy ${CMAKE_COMMAND} --build . --config Debug) + unset(RunCMake_TEST_NO_CLEAN) endif() if(RunCMake_GENERATOR MATCHES "Make|Ninja|Visual Studio|Xcode" AND -- cgit v0.12