Xcode: Add support of DEPFILE for add_custom_command, part 2

This MR extend the support of 'DEPFILE' to buildsystem version 1.

Issue: #20286
This commit is contained in:
Marc Chevrier 2021-04-16 14:39:23 +02:00
parent 94fb2516f0
commit 253aff6c94
9 changed files with 55 additions and 21 deletions

View File

@ -299,14 +299,6 @@ The options are:
For :ref:`Makefile Generators`, this option cannot be specified at the For :ref:`Makefile Generators`, this option cannot be specified at the
same time as ``IMPLICIT_DEPENDS`` option. same time as ``IMPLICIT_DEPENDS`` option.
.. note::
For the :generator:`Xcode` generator, this option requires that the
:ref:`Xcode Build System Selection` uses the ``buildsystem=12`` variant
or higher. This is the default when using Xcode 12 or above.
The :variable:`CMAKE_XCODE_BUILD_SYSTEM` variable indicates which variant
of the Xcode build system is used.
Examples: Generating Files Examples: Generating Files
^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -226,6 +226,9 @@ cmCustomCommandGenerator::cmCustomCommandGenerator(
case cmDepfileFormat::VsTlog: case cmDepfileFormat::VsTlog:
argv.emplace_back("vstlog"); argv.emplace_back("vstlog");
break; break;
case cmDepfileFormat::MakeDepfile:
argv.emplace_back("makedepfile");
break;
} }
argv.push_back(this->LG->GetSourceDirectory()); argv.push_back(this->LG->GetSourceDirectory());
argv.push_back(this->LG->GetCurrentSourceDirectory()); argv.push_back(this->LG->GetCurrentSourceDirectory());
@ -430,6 +433,7 @@ std::string cmCustomCommandGenerator::GetInternalDepfileName(
std::string extension; std::string extension;
switch (*this->LG->GetGlobalGenerator()->DepfileFormat()) { switch (*this->LG->GetGlobalGenerator()->DepfileFormat()) {
case cmDepfileFormat::GccDepfile: case cmDepfileFormat::GccDepfile:
case cmDepfileFormat::MakeDepfile:
extension = ".d"; extension = ".d";
break; break;
case cmDepfileFormat::VsTlog: case cmDepfileFormat::VsTlog:

View File

@ -2213,9 +2213,33 @@ void cmGlobalXCodeGenerator::CreateCustomRulesMakefile(
} }
} }
makefileStream << "\n\n"; makefileStream << "\n\n";
auto depfilesDirectory =
cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(),
"/CMakeFiles/d/");
for (auto const& command : commands) { for (auto const& command : commands) {
cmCustomCommandGenerator ccg(command, configName, cmCustomCommandGenerator ccg(
this->CurrentLocalGenerator); command, configName, this->CurrentLocalGenerator, true, {},
[this, &depfilesDirectory](const std::string& config,
const std::string& file) -> std::string {
return cmStrCat(
depfilesDirectory,
this->GetObjectId(cmXCodeObject::PBXShellScriptBuildPhase, file),
".", config, ".d");
});
auto depfile = ccg.GetInternalDepfile();
if (!depfile.empty()) {
makefileStream << "include "
<< cmSystemTools::ConvertToOutputPath(depfile) << "\n\n";
cmSystemTools::MakeDirectory(depfilesDirectory);
if (!cmSystemTools::FileExists(depfile)) {
cmSystemTools::Touch(depfile, true);
}
}
std::vector<std::string> realDepends; std::vector<std::string> realDepends;
realDepends.reserve(ccg.GetDepends().size()); realDepends.reserve(ccg.GetDepends().size());
for (auto const& d : ccg.GetDepends()) { for (auto const& d : ccg.GetDepends()) {

View File

@ -115,13 +115,12 @@ public:
/** /**
* Used to determine if this generator supports DEPFILE option. * Used to determine if this generator supports DEPFILE option.
*/ */
bool SupportsCustomCommandDepfile() const override bool SupportsCustomCommandDepfile() const override { return true; }
{
return this->XcodeBuildSystem >= BuildSystem::Twelve;
}
virtual cm::optional<cmDepfileFormat> DepfileFormat() const override virtual cm::optional<cmDepfileFormat> DepfileFormat() const override
{ {
return cmDepfileFormat::GccDepfile; return this->XcodeBuildSystem == BuildSystem::One
? cmDepfileFormat::MakeDepfile
: cmDepfileFormat::GccDepfile;
} }
bool SetSystemName(std::string const& s, cmMakefile* mf) override; bool SetSystemName(std::string const& s, cmMakefile* mf) override;

View File

@ -36,8 +36,9 @@ void WriteFilenameGcc(cmsys::ofstream& fout, const std::string& filename)
} }
} }
void WriteGccDepfile(cmsys::ofstream& fout, const cmLocalGenerator& lg, void WriteDepfile(cmDepfileFormat format, cmsys::ofstream& fout,
const cmGccDepfileContent& content) const cmLocalGenerator& lg,
const cmGccDepfileContent& content)
{ {
const auto& binDir = lg.GetBinaryDirectory(); const auto& binDir = lg.GetBinaryDirectory();
std::function<std::string(const std::string&)> formatPath = std::function<std::string(const std::string&)> formatPath =
@ -65,6 +66,18 @@ void WriteGccDepfile(cmsys::ofstream& fout, const cmLocalGenerator& lg,
} }
fout << '\n'; fout << '\n';
} }
if (format == cmDepfileFormat::MakeDepfile) {
// In this case, phony targets must be added for all dependencies
fout << "\n";
for (auto const& dep : content) {
for (auto const& path : dep.paths) {
fout << "\n";
WriteFilenameGcc(fout, formatPath(path));
fout << ":\n";
}
}
}
} }
// tlog format : always windows paths on Windows regardless the generator // tlog format : always windows paths on Windows regardless the generator
@ -122,7 +135,8 @@ bool cmTransformDepfile(cmDepfileFormat format, const cmLocalGenerator& lg,
} }
switch (format) { switch (format) {
case cmDepfileFormat::GccDepfile: case cmDepfileFormat::GccDepfile:
WriteGccDepfile(fout, lg, content); case cmDepfileFormat::MakeDepfile:
WriteDepfile(format, fout, lg, content);
break; break;
case cmDepfileFormat::VsTlog: case cmDepfileFormat::VsTlog:
WriteVsTlog(fout, lg, content); WriteVsTlog(fout, lg, content);

View File

@ -8,6 +8,7 @@ enum class cmDepfileFormat
{ {
GccDepfile, GccDepfile,
VsTlog, VsTlog,
MakeDepfile
}; };
class cmLocalGenerator; class cmLocalGenerator;

View File

@ -1527,6 +1527,8 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
format = cmDepfileFormat::GccDepfile; format = cmDepfileFormat::GccDepfile;
} else if (args[3] == "vstlog") { } else if (args[3] == "vstlog") {
format = cmDepfileFormat::VsTlog; format = cmDepfileFormat::VsTlog;
} else if (args[3] == "makedepfile") {
format = cmDepfileFormat::MakeDepfile;
} else { } else {
return 1; return 1;
} }

View File

@ -155,8 +155,7 @@ if (RunCMake_GENERATOR MATCHES "Makefiles")
run_cmake(CustomCommandDependencies-BadArgs) run_cmake(CustomCommandDependencies-BadArgs)
endif() endif()
if(RunCMake_GENERATOR MATCHES "Make|Ninja" OR if(RunCMake_GENERATOR MATCHES "Make|Ninja|Xcode")
(RunCMake_GENERATOR STREQUAL "Xcode" AND CMAKE_XCODE_BUILD_SYSTEM GREATER_EQUAL "12"))
unset(run_BuildDepends_skip_step_3) unset(run_BuildDepends_skip_step_3)
run_BuildDepends(CustomCommandDepfile) run_BuildDepends(CustomCommandDepfile)
set(run_BuildDepends_skip_step_3 1) set(run_BuildDepends_skip_step_3 1)

View File

@ -222,7 +222,6 @@ endif()
add_RunCMake_test(BuildDepends add_RunCMake_test(BuildDepends
-DMSVC_VERSION=${MSVC_VERSION} -DMSVC_VERSION=${MSVC_VERSION}
-DCMAKE_XCODE_BUILD_SYSTEM=${CMAKE_XCODE_BUILD_SYSTEM}
-DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID} -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}
-DCMake_TEST_BuildDepends_GNU_AS=${CMake_TEST_BuildDepends_GNU_AS} -DCMake_TEST_BuildDepends_GNU_AS=${CMake_TEST_BuildDepends_GNU_AS}
) )