cmake/Source/cmMakefileUtilityTargetGenerator.cxx

144 lines
5.2 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmMakefileUtilityTargetGenerator.h"
2017-04-14 19:02:05 +02:00
#include <ostream>
#include <string>
2018-04-23 21:13:27 +02:00
#include <utility>
2017-04-14 19:02:05 +02:00
#include <vector>
2020-02-01 23:06:01 +01:00
#include <cm/memory>
#include "cmGeneratedFileStream.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorTarget.h"
#include "cmGlobalUnixMakefileGenerator3.h"
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmMakefile.h"
2016-10-30 18:24:19 +01:00
#include "cmOSXBundleGenerator.h"
2021-09-14 00:13:48 +02:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2016-07-09 11:21:54 +02:00
cmMakefileUtilityTargetGenerator::cmMakefileUtilityTargetGenerator(
cmGeneratorTarget* target)
: cmMakefileTargetGenerator(target)
{
this->CustomCommandDriver = OnUtility;
2020-08-30 11:54:41 +02:00
this->OSXBundleGenerator = cm::make_unique<cmOSXBundleGenerator>(target);
2012-08-04 10:26:08 +03:00
this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
}
2019-11-11 23:01:05 +01:00
cmMakefileUtilityTargetGenerator::~cmMakefileUtilityTargetGenerator() =
default;
void cmMakefileUtilityTargetGenerator::WriteRuleFiles()
{
this->CreateRuleFile();
2016-07-09 11:21:54 +02:00
*this->BuildFileStream << "# Utility rule file for "
<< this->GeneratorTarget->GetName() << ".\n\n";
2021-09-14 00:13:48 +02:00
const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")
? "$(CMAKE_BINARY_DIR)/"
: "");
// Include the dependencies for the target.
std::string dependFile =
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.make");
*this->BuildFileStream
<< "# Include any custom commands dependencies for this target.\n"
<< this->GlobalGenerator->IncludeDirective << " " << root
<< cmSystemTools::ConvertToOutputPath(
this->LocalGenerator->MaybeRelativeToTopBinDir(dependFile))
<< "\n\n";
if (!cmSystemTools::FileExists(dependFile)) {
// Write an empty dependency file.
cmGeneratedFileStream depFileStream(
dependFile, false, this->GlobalGenerator->GetMakefileEncoding());
depFileStream << "# Empty custom commands generated dependencies file for "
<< this->GeneratorTarget->GetName() << ".\n"
<< "# This may be replaced when dependencies are built.\n";
}
std::string dependTimestamp =
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts");
if (!cmSystemTools::FileExists(dependTimestamp)) {
// Write a dependency timestamp file.
cmGeneratedFileStream depFileStream(
dependTimestamp, false, this->GlobalGenerator->GetMakefileEncoding());
depFileStream << "# CMAKE generated file: DO NOT EDIT!\n"
<< "# Timestamp file for custom commands dependencies "
"management for "
<< this->GeneratorTarget->GetName() << ".\n";
}
2016-07-09 11:21:54 +02:00
if (!this->NoRuleMessages) {
2012-02-18 12:40:36 +02:00
// Include the progress variables for the target.
*this->BuildFileStream
<< "# Include the progress variables for this target.\n"
2015-08-17 11:37:30 +02:00
<< this->GlobalGenerator->IncludeDirective << " " << root
2016-10-30 18:24:19 +01:00
<< cmSystemTools::ConvertToOutputPath(
2021-09-14 00:13:48 +02:00
this->LocalGenerator->MaybeRelativeToTopBinDir(
2018-04-23 21:13:27 +02:00
this->ProgressFileNameFull))
2012-02-18 12:40:36 +02:00
<< "\n\n";
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
// write the custom commands for this target
this->WriteTargetBuildRules();
// Collect the commands and dependencies.
std::vector<std::string> commands;
std::vector<std::string> depends;
// Utility targets store their rules in pre- and post-build commands.
2016-07-09 11:21:54 +02:00
this->LocalGenerator->AppendCustomDepends(
depends, this->GeneratorTarget->GetPreBuildCommands());
2016-07-09 11:21:54 +02:00
this->LocalGenerator->AppendCustomDepends(
depends, this->GeneratorTarget->GetPostBuildCommands());
2016-07-09 11:21:54 +02:00
this->LocalGenerator->AppendCustomCommands(
commands, this->GeneratorTarget->GetPreBuildCommands(),
2016-10-30 18:24:19 +01:00
this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
// Depend on all custom command outputs for sources
this->DriveCustomCommands(depends);
2016-07-09 11:21:54 +02:00
this->LocalGenerator->AppendCustomCommands(
commands, this->GeneratorTarget->GetPostBuildCommands(),
2016-10-30 18:24:19 +01:00
this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
// Add dependencies on targets that must be built first.
this->AppendTargetDepends(depends);
// Add a dependency on the rule file itself.
this->LocalGenerator->AppendRuleDepend(depends,
this->BuildFileNameFull.c_str());
// If the rule is empty add the special empty rule dependency needed
// by some make tools.
2016-07-09 11:21:54 +02:00
if (depends.empty() && commands.empty()) {
std::string hack = this->GlobalGenerator->GetEmptyRuleHackDepends();
2016-07-09 11:21:54 +02:00
if (!hack.empty()) {
2018-04-23 21:13:27 +02:00
depends.push_back(std::move(hack));
}
2016-07-09 11:21:54 +02:00
}
// Write the rule.
2018-01-26 17:06:56 +01:00
this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
2016-03-13 13:35:51 +01:00
this->GeneratorTarget->GetName(),
depends, commands, true);
// Write the main driver rule to build everything in this target.
2016-03-13 13:35:51 +01:00
this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
// Write clean target
this->WriteTargetCleanRules();
// Write the dependency generation rule. This must be done last so
// that multiple output pair information is available.
this->WriteTargetDependRules();
// close the streams
this->CloseFileStreams();
}