cmake/Source/cmLocalUnixMakefileGenerator3.cxx

2252 lines
79 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 "cmLocalUnixMakefileGenerator3.h"
2017-04-14 19:02:05 +02:00
#include <algorithm>
2020-08-30 11:54:41 +02:00
#include <cassert>
2020-02-01 23:06:01 +01:00
#include <cstdio>
2021-09-14 00:13:48 +02:00
#include <functional>
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <utility>
2020-02-01 23:06:01 +01:00
#include <cm/memory>
2023-05-23 16:38:00 +02:00
#include <cm/optional>
2021-09-14 00:13:48 +02:00
#include <cm/string_view>
2020-08-30 11:54:41 +02:00
#include <cm/vector>
#include <cmext/algorithm>
2021-09-14 00:13:48 +02:00
#include <cmext/string_view>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
#include "cmsys/Terminal.h"
2021-09-14 00:13:48 +02:00
#include "cmCMakePath.h"
2019-11-11 23:01:05 +01:00
#include "cmCustomCommand.h" // IWYU pragma: keep
2016-07-09 11:21:54 +02:00
#include "cmCustomCommandGenerator.h"
2021-09-14 00:13:48 +02:00
#include "cmDependsCompiler.h"
2019-11-11 23:01:05 +01:00
#include "cmFileTimeCache.h"
#include "cmGeneratedFileStream.h"
2019-11-11 23:01:05 +01:00
#include "cmGeneratorExpression.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorTarget.h"
#include "cmGlobalGenerator.h"
#include "cmGlobalUnixMakefileGenerator3.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2019-11-11 23:01:05 +01:00
#include "cmListFileCache.h"
2016-10-30 18:24:19 +01:00
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmMakefileTargetGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmOutputConverter.h"
2019-11-11 23:01:05 +01:00
#include "cmRange.h"
2017-04-14 19:02:05 +02:00
#include "cmRulePlaceholderExpander.h"
#include "cmSourceFile.h"
2016-10-30 18:24:19 +01:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateSnapshot.h"
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2021-09-14 00:13:48 +02:00
#include "cmTargetDepend.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
#include "cmVersion.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
// Include dependency scanners for supported languages. Only the
// C/C++ scanner is needed for bootstrapping CMake.
#include "cmDependsC.h"
2020-02-01 23:06:01 +01:00
#ifndef CMAKE_BOOTSTRAP
2018-08-09 18:06:22 +02:00
# include "cmDependsFortran.h"
# include "cmDependsJava.h"
#endif
2021-09-14 00:13:48 +02:00
namespace {
// Helper function used below.
2021-09-14 00:13:48 +02:00
std::string cmSplitExtension(std::string const& in, std::string& base)
{
std::string ext;
2016-07-09 11:21:54 +02:00
std::string::size_type dot_pos = in.rfind('.');
if (dot_pos != std::string::npos) {
// Remove the extension first in case &base == &in.
2017-07-20 19:35:53 +02:00
ext = in.substr(dot_pos);
base = in.substr(0, dot_pos);
2016-07-09 11:21:54 +02:00
} else {
base = in;
2016-07-09 11:21:54 +02:00
}
return ext;
}
2021-09-14 00:13:48 +02:00
// Helper predicate for removing absolute paths that don't point to the
// source or binary directory. It is used when CMAKE_DEPENDS_IN_PROJECT_ONLY
// is set ON, to only consider in-project dependencies during the build.
class NotInProjectDir
{
public:
// Constructor with the source and binary directory's path
NotInProjectDir(cm::string_view sourceDir, cm::string_view binaryDir)
: SourceDir(sourceDir)
, BinaryDir(binaryDir)
{
}
// Operator evaluating the predicate
bool operator()(const std::string& p) const
{
auto path = cmCMakePath(p).Normal();
// Keep all relative paths:
if (path.IsRelative()) {
return false;
}
// If it's an absolute path, check if it starts with the source
// directory:
return !(cmCMakePath(this->SourceDir).IsPrefix(path) ||
cmCMakePath(this->BinaryDir).IsPrefix(path));
}
private:
// The path to the source directory
cm::string_view SourceDir;
// The path to the binary directory
cm::string_view BinaryDir;
};
}
2016-07-09 11:21:54 +02:00
cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3(
cmGlobalGenerator* gg, cmMakefile* mf)
2023-07-02 19:51:09 +02:00
: cmLocalCommonGenerator(gg, mf)
{
this->MakefileVariableSize = 0;
this->ColorMakefile = false;
this->SkipPreprocessedSourceRules = false;
this->SkipAssemblySourceRules = false;
this->MakeCommandEscapeTargetTwice = false;
this->BorlandMakeCurlyHack = false;
}
2019-11-11 23:01:05 +01:00
cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3() = default;
2020-08-30 11:54:41 +02:00
std::string cmLocalUnixMakefileGenerator3::GetConfigName() const
{
auto const& configNames = this->GetConfigNames();
assert(configNames.size() == 1);
return configNames.front();
}
void cmLocalUnixMakefileGenerator3::Generate()
{
// Record whether some options are enabled to avoid checking many
// times later.
2016-07-09 11:21:54 +02:00
if (!this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
2022-08-04 22:12:04 +02:00
if (this->Makefile->IsSet("CMAKE_COLOR_MAKEFILE")) {
this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE");
} else {
this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_DIAGNOSTICS");
}
2016-07-09 11:21:54 +02:00
}
this->SkipPreprocessedSourceRules =
this->Makefile->IsOn("CMAKE_SKIP_PREPROCESSED_SOURCE_RULES");
this->SkipAssemblySourceRules =
this->Makefile->IsOn("CMAKE_SKIP_ASSEMBLY_SOURCE_RULES");
// Generate the rule files for each target.
2009-10-04 10:30:41 +03:00
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
2021-09-14 00:13:48 +02:00
for (cmGeneratorTarget* gt :
this->GlobalGenerator->GetLocalGeneratorTargetsInOrder(this)) {
if (!gt->IsInBuildSystem()) {
2014-08-03 19:52:23 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
auto& gtVisited = this->GetCommandsVisited(gt);
const auto& deps = this->GlobalGenerator->GetTargetDirectDepends(gt);
for (const auto& d : deps) {
// Take the union of visited source files of custom commands
auto depVisited = this->GetCommandsVisited(d);
gtVisited.insert(depVisited.begin(), depVisited.end());
}
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmMakefileTargetGenerator> tg(
2021-09-14 00:13:48 +02:00
cmMakefileTargetGenerator::New(gt));
2018-01-26 17:06:56 +01:00
if (tg) {
tg->WriteRuleFiles();
2009-10-04 10:30:41 +03:00
gg->RecordTargetProgress(tg.get());
}
2016-07-09 11:21:54 +02:00
}
// write the local Makefile
this->WriteLocalMakefile();
2011-01-16 11:35:12 +01:00
// Write the cmake file with information for this directory.
this->WriteDirectoryInformationFile();
}
2015-11-17 17:22:37 +01:00
void cmLocalUnixMakefileGenerator3::ComputeHomeRelativeOutputPath()
{
// Compute the path to use when referencing the current output
// directory from the top output directory.
2021-09-14 00:13:48 +02:00
this->HomeRelativeOutputPath =
this->MaybeRelativeToTopBinDir(this->GetCurrentBinaryDirectory());
2016-07-09 11:21:54 +02:00
if (this->HomeRelativeOutputPath == ".") {
2018-01-26 17:06:56 +01:00
this->HomeRelativeOutputPath.clear();
2016-07-09 11:21:54 +02:00
}
if (!this->HomeRelativeOutputPath.empty()) {
2015-11-17 17:22:37 +01:00
this->HomeRelativeOutputPath += "/";
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::GetLocalObjectFiles(
std::map<std::string, LocalObjectInfo>& localObjectFiles)
2015-04-27 22:25:09 +02:00
{
2020-08-30 11:54:41 +02:00
for (const auto& gt : this->GetGeneratorTargets()) {
2021-09-14 00:13:48 +02:00
if (!gt->CanCompileSources()) {
2015-04-27 22:25:09 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::vector<cmSourceFile const*> objectSources;
2020-08-30 11:54:41 +02:00
gt->GetObjectSources(objectSources, this->GetConfigName());
2015-04-27 22:25:09 +02:00
// Compute full path to object file directory for this target.
2020-02-01 23:06:01 +01:00
std::string dir = cmStrCat(gt->LocalGenerator->GetCurrentBinaryDirectory(),
2020-08-30 11:54:41 +02:00
'/', this->GetTargetDirectory(gt.get()), '/');
2015-04-27 22:25:09 +02:00
// Compute the name of each object file.
2018-01-26 17:06:56 +01:00
for (cmSourceFile const* sf : objectSources) {
2015-04-27 22:25:09 +02:00
bool hasSourceExtension = true;
2016-07-09 11:21:54 +02:00
std::string objectName =
this->GetObjectFileNameWithoutTarget(*sf, dir, &hasSourceExtension);
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileIsFullPath(objectName)) {
2015-04-27 22:25:09 +02:00
objectName = cmSystemTools::GetFilenameName(objectName);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
LocalObjectInfo& info = localObjectFiles[objectName];
info.HasSourceExtension = hasSourceExtension;
2020-08-30 11:54:41 +02:00
info.emplace_back(gt.get(), sf->GetLanguage());
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::GetIndividualFileTargets(
std::vector<std::string>& targets)
{
2015-04-27 22:25:09 +02:00
std::map<std::string, LocalObjectInfo> localObjectFiles;
this->GetLocalObjectFiles(localObjectFiles);
2018-01-26 17:06:56 +01:00
for (auto const& localObjectFile : localObjectFiles) {
targets.push_back(localObjectFile.first);
std::string::size_type dot_pos = localObjectFile.first.rfind(".");
std::string base = localObjectFile.first.substr(0, dot_pos);
if (localObjectFile.second.HasPreprocessRule) {
2009-10-04 10:30:41 +03:00
targets.push_back(base + ".i");
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
if (localObjectFile.second.HasAssembleRule) {
2009-10-04 10:30:41 +03:00
targets.push_back(base + ".s");
}
2016-07-09 11:21:54 +02:00
}
}
2023-07-02 19:51:09 +02:00
std::string cmLocalUnixMakefileGenerator3::GetLinkDependencyFile(
cmGeneratorTarget* target, std::string const& /*config*/) const
{
return cmStrCat(target->GetSupportDirectory(), "/link.d");
}
void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
{
// generate the includes
std::string ruleFileName = "Makefile";
// Open the rule file. This should be copy-if-different because the
// rules may depend on this file itself.
std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
2017-04-14 19:02:05 +02:00
cmGeneratedFileStream ruleFileStream(
2018-10-28 12:09:07 +01:00
ruleFileNameFull, false, this->GlobalGenerator->GetMakefileEncoding());
2016-07-09 11:21:54 +02:00
if (!ruleFileStream) {
return;
2016-07-09 11:21:54 +02:00
}
// always write the top makefile
2016-07-09 11:21:54 +02:00
if (!this->IsRootMakefile()) {
ruleFileStream.SetCopyIfDifferent(true);
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
// write the all rules
this->WriteLocalAllRules(ruleFileStream);
2011-01-16 11:35:12 +01:00
// only write local targets unless at the top Keep track of targets already
// listed.
2015-04-27 22:25:09 +02:00
std::set<std::string> emittedTargets;
2016-07-09 11:21:54 +02:00
if (!this->IsRootMakefile()) {
// write our targets, and while doing it collect up the object
// file rules
2016-07-09 11:21:54 +02:00
this->WriteLocalMakefileTargets(ruleFileStream, emittedTargets);
} else {
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
2016-07-09 11:21:54 +02:00
gg->WriteConvenienceRules(ruleFileStream, emittedTargets);
}
2016-07-09 11:21:54 +02:00
bool do_preprocess_rules = this->GetCreatePreprocessedSourceRules();
bool do_assembly_rules = this->GetCreateAssemblySourceRules();
2015-04-27 22:25:09 +02:00
std::map<std::string, LocalObjectInfo> localObjectFiles;
this->GetLocalObjectFiles(localObjectFiles);
// now write out the object rules
// for each object file name
2018-01-26 17:06:56 +01:00
for (auto& localObjectFile : localObjectFiles) {
// Add a convenience rule for building the object file.
2018-01-26 17:06:56 +01:00
this->WriteObjectConvenienceRule(
2019-11-11 23:01:05 +01:00
ruleFileStream, "target to build an object file", localObjectFile.first,
localObjectFile.second);
// Check whether preprocessing and assembly rules make sense.
// They make sense only for C and C++ sources.
2015-04-27 22:25:09 +02:00
bool lang_has_preprocessor = false;
bool lang_has_assembly = false;
2018-01-26 17:06:56 +01:00
for (LocalObjectEntry const& entry : localObjectFile.second) {
if (entry.Language == "C" || entry.Language == "CXX" ||
2021-09-14 00:13:48 +02:00
entry.Language == "CUDA" || entry.Language == "Fortran" ||
entry.Language == "HIP" || entry.Language == "ISPC") {
// Right now, C, C++, CUDA, Fortran, HIP and ISPC have both a
// preprocessor and the ability to generate assembly code
2015-04-27 22:25:09 +02:00
lang_has_preprocessor = true;
lang_has_assembly = true;
2012-04-19 19:04:21 +03:00
break;
}
2016-07-09 11:21:54 +02:00
}
// Add convenience rules for preprocessed and assembly files.
2016-07-09 11:21:54 +02:00
if (lang_has_preprocessor && do_preprocess_rules) {
2018-01-26 17:06:56 +01:00
std::string::size_type dot_pos = localObjectFile.first.rfind(".");
std::string base = localObjectFile.first.substr(0, dot_pos);
2019-11-11 23:01:05 +01:00
this->WriteObjectConvenienceRule(ruleFileStream,
"target to preprocess a source file",
(base + ".i"), localObjectFile.second);
2018-01-26 17:06:56 +01:00
localObjectFile.second.HasPreprocessRule = true;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (lang_has_assembly && do_assembly_rules) {
2018-01-26 17:06:56 +01:00
std::string::size_type dot_pos = localObjectFile.first.rfind(".");
std::string base = localObjectFile.first.substr(0, dot_pos);
2015-04-27 22:25:09 +02:00
this->WriteObjectConvenienceRule(
2016-07-09 11:21:54 +02:00
ruleFileStream, "target to generate assembly for a file",
2019-11-11 23:01:05 +01:00
(base + ".s"), localObjectFile.second);
2018-01-26 17:06:56 +01:00
localObjectFile.second.HasAssembleRule = true;
}
2016-07-09 11:21:54 +02:00
}
// add a help target as long as there isn;t a real target named help
2016-07-09 11:21:54 +02:00
if (emittedTargets.insert("help").second) {
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
2016-07-09 11:21:54 +02:00
gg->WriteHelpRule(ruleFileStream, this);
}
this->WriteSpecialTargetsBottom(ruleFileStream);
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteObjectConvenienceRule(
2019-11-11 23:01:05 +01:00
std::ostream& ruleFileStream, const char* comment, const std::string& output,
2016-07-09 11:21:54 +02:00
LocalObjectInfo const& info)
{
// If the rule includes the source file extension then create a
// version that has the extension removed. The help should include
// only the version without source extension.
bool inHelp = true;
2016-07-09 11:21:54 +02:00
if (info.HasSourceExtension) {
// Remove the last extension. This should be kept.
std::string outBase1 = output;
std::string outExt1 = cmSplitExtension(outBase1, outBase1);
// Now remove the source extension and put back the last
// extension.
std::string outNoExt;
cmSplitExtension(outBase1, outNoExt);
outNoExt += outExt1;
// Add a rule to drive the rule below.
std::vector<std::string> depends;
2019-11-11 23:01:05 +01:00
depends.emplace_back(output);
std::vector<std::string> no_commands;
2018-01-26 17:06:56 +01:00
this->WriteMakeRule(ruleFileStream, nullptr, outNoExt, depends,
2016-10-30 18:24:19 +01:00
no_commands, true, true);
inHelp = false;
2016-07-09 11:21:54 +02:00
}
// Recursively make the rule for each target using the object file.
std::vector<std::string> commands;
2018-01-26 17:06:56 +01:00
for (LocalObjectEntry const& t : info) {
std::string tgtMakefileName = this->GetRelativeTargetDirectory(t.Target);
std::string targetName = tgtMakefileName;
tgtMakefileName += "/build.make";
targetName += "/";
targetName += output;
commands.push_back(
2019-11-11 23:01:05 +01:00
this->GetRecursiveMakeCall(tgtMakefileName, targetName));
2016-07-09 11:21:54 +02:00
}
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
// Write the rule to the makefile.
std::vector<std::string> no_depends;
2016-07-09 11:21:54 +02:00
this->WriteMakeRule(ruleFileStream, comment, output, no_depends, commands,
true, inHelp);
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteLocalMakefileTargets(
std::ostream& ruleFileStream, std::set<std::string>& emitted)
{
std::vector<std::string> depends;
std::vector<std::string> commands;
// for each target we just provide a rule to cd up to the top and do a make
// on the target
std::string localName;
2020-08-30 11:54:41 +02:00
for (const auto& target : this->GetGeneratorTargets()) {
2018-01-26 17:06:56 +01:00
if ((target->GetType() == cmStateEnums::EXECUTABLE) ||
(target->GetType() == cmStateEnums::STATIC_LIBRARY) ||
(target->GetType() == cmStateEnums::SHARED_LIBRARY) ||
(target->GetType() == cmStateEnums::MODULE_LIBRARY) ||
(target->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
(target->GetType() == cmStateEnums::UTILITY)) {
emitted.insert(target->GetName());
// for subdirs add a rule to build this specific target by name.
2020-08-30 11:54:41 +02:00
localName =
cmStrCat(this->GetRelativeTargetDirectory(target.get()), "/rule");
commands.clear();
depends.clear();
2011-01-16 11:35:12 +01:00
// Build the target for this pass.
2020-02-01 23:06:01 +01:00
std::string makefile2 = "CMakeFiles/Makefile2";
2019-11-11 23:01:05 +01:00
commands.push_back(this->GetRecursiveMakeCall(makefile2, localName));
2016-07-09 11:21:54 +02:00
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
2015-04-27 22:25:09 +02:00
localName, depends, commands, true);
2011-01-16 11:35:12 +01:00
// Add a target with the canonical name (no prefix, suffix or path).
2018-01-26 17:06:56 +01:00
if (localName != target->GetName()) {
commands.clear();
depends.push_back(localName);
this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
2018-01-26 17:06:56 +01:00
target->GetName(), depends, commands, true);
2016-07-09 11:21:54 +02:00
}
// Add a fast rule to build the target
2020-08-30 11:54:41 +02:00
std::string makefileName = cmStrCat(
this->GetRelativeTargetDirectory(target.get()), "/build.make");
// make sure the makefile name is suitable for a makefile
2020-02-01 23:06:01 +01:00
std::string makeTargetName =
2020-08-30 11:54:41 +02:00
cmStrCat(this->GetRelativeTargetDirectory(target.get()), "/build");
2020-02-01 23:06:01 +01:00
localName = cmStrCat(target->GetName(), "/fast");
depends.clear();
commands.clear();
2016-07-09 11:21:54 +02:00
commands.push_back(
2019-11-11 23:01:05 +01:00
this->GetRecursiveMakeCall(makefileName, makeTargetName));
2016-07-09 11:21:54 +02:00
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
this->WriteMakeRule(ruleFileStream, "fast build rule for target.",
2015-04-27 22:25:09 +02:00
localName, depends, commands, true);
// Add a local name for the rule to relink the target before
// installation.
2020-08-30 11:54:41 +02:00
if (target->NeedRelinkBeforeInstall(this->GetConfigName())) {
makeTargetName = cmStrCat(
this->GetRelativeTargetDirectory(target.get()), "/preinstall");
2020-02-01 23:06:01 +01:00
localName = cmStrCat(target->GetName(), "/preinstall");
depends.clear();
commands.clear();
2016-07-09 11:21:54 +02:00
commands.push_back(
2019-11-11 23:01:05 +01:00
this->GetRecursiveMakeCall(makefile2, makeTargetName));
2016-07-09 11:21:54 +02:00
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
this->WriteMakeRule(ruleFileStream,
"Manual pre-install relink rule for target.",
2015-04-27 22:25:09 +02:00
localName, depends, commands, true);
}
}
2016-07-09 11:21:54 +02:00
}
}
void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
{
2020-02-01 23:06:01 +01:00
std::string infoFileName =
cmStrCat(this->GetCurrentBinaryDirectory(),
"/CMakeFiles/CMakeDirectoryInformation.cmake");
// Open the output file.
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream infoFileStream(infoFileName);
2016-07-09 11:21:54 +02:00
if (!infoFileStream) {
return;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
infoFileStream.SetCopyIfDifferent(true);
// Write the do not edit header.
this->WriteDisclaimer(infoFileStream);
// Setup relative path conversion tops.
2016-07-09 11:21:54 +02:00
/* clang-format off */
infoFileStream
<< "# Relative path conversion top directories.\n"
2015-08-17 11:37:30 +02:00
<< "set(CMAKE_RELATIVE_PATH_TOP_SOURCE \""
2021-09-14 00:13:48 +02:00
<< this->GetRelativePathTopSource() << "\")\n"
2015-08-17 11:37:30 +02:00
<< "set(CMAKE_RELATIVE_PATH_TOP_BINARY \""
2021-09-14 00:13:48 +02:00
<< this->GetRelativePathTopBinary() << "\")\n"
<< "\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
// Tell the dependency scanner to use unix paths if necessary.
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetForceUnixPaths()) {
/* clang-format off */
infoFileStream
<< "# Force unix paths in dependencies.\n"
2014-08-03 19:52:23 +02:00
<< "set(CMAKE_FORCE_UNIX_PATHS 1)\n"
<< "\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
}
// Store the include regular expressions for this directory.
2016-07-09 11:21:54 +02:00
infoFileStream << "\n"
<< "# The C and CXX include file regular expressions for "
<< "this directory.\n";
infoFileStream << "set(CMAKE_C_INCLUDE_REGEX_SCAN ";
2019-11-11 23:01:05 +01:00
cmLocalUnixMakefileGenerator3::WriteCMakeArgument(
infoFileStream, this->Makefile->GetIncludeRegularExpression());
2016-07-09 11:21:54 +02:00
infoFileStream << ")\n";
infoFileStream << "set(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
2019-11-11 23:01:05 +01:00
cmLocalUnixMakefileGenerator3::WriteCMakeArgument(
infoFileStream, this->Makefile->GetComplainRegularExpression());
2016-07-09 11:21:54 +02:00
infoFileStream << ")\n";
infoFileStream
2014-08-03 19:52:23 +02:00
<< "set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
2016-07-09 11:21:54 +02:00
infoFileStream << "set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
"${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
}
2016-07-09 11:21:54 +02:00
std::string cmLocalUnixMakefileGenerator3::ConvertToFullPath(
const std::string& localPath)
{
2020-02-01 23:06:01 +01:00
std::string dir =
cmStrCat(this->GetCurrentBinaryDirectory(), '/', localPath);
return dir;
}
2016-07-09 11:21:54 +02:00
const std::string& cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
{
return this->HomeRelativeOutputPath;
}
2020-08-30 11:54:41 +02:00
std::string cmLocalUnixMakefileGenerator3::ConvertToMakefilePath(
std::string const& path) const
{
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
return gg->ConvertToMakefilePath(path);
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteMakeRule(
std::ostream& os, const char* comment, const std::string& target,
const std::vector<std::string>& depends,
const std::vector<std::string>& commands, bool symbolic, bool in_help)
{
// Make sure there is a target.
2016-07-09 11:21:54 +02:00
if (target.empty()) {
2019-11-11 23:01:05 +01:00
std::string err("No target for WriteMakeRule! called with comment: ");
if (comment) {
err += comment;
}
cmSystemTools::Error(err);
return;
2016-07-09 11:21:54 +02:00
}
std::string replace;
// Write the comment describing the rule in the makefile.
2016-07-09 11:21:54 +02:00
if (comment) {
replace = comment;
std::string::size_type lpos = 0;
std::string::size_type rpos;
2016-07-09 11:21:54 +02:00
while ((rpos = replace.find('\n', lpos)) != std::string::npos) {
os << "# " << replace.substr(lpos, rpos - lpos) << "\n";
lpos = rpos + 1;
}
2016-07-09 11:21:54 +02:00
os << "# " << replace.substr(lpos) << "\n";
}
// Construct the left hand side of the rule.
2021-09-14 00:13:48 +02:00
std::string tgt =
this->ConvertToMakefilePath(this->MaybeRelativeToTopBinDir(target));
2015-04-27 22:25:09 +02:00
const char* space = "";
2016-07-09 11:21:54 +02:00
if (tgt.size() == 1) {
// Add a space before the ":" to avoid drive letter confusion on
// Windows.
space = " ";
2016-07-09 11:21:54 +02:00
}
// Mark the rule as symbolic if requested.
2016-07-09 11:21:54 +02:00
if (symbolic) {
2021-11-20 13:41:27 +01:00
if (cmValue sym =
2016-07-09 11:21:54 +02:00
this->Makefile->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE")) {
2021-09-14 00:13:48 +02:00
os << tgt << space << ": " << *sym << "\n";
}
2016-07-09 11:21:54 +02:00
}
// Write the rule.
2016-07-09 11:21:54 +02:00
if (depends.empty()) {
// No dependencies. The commands will always run.
2020-08-30 11:54:41 +02:00
os << tgt << space << ":\n";
2016-07-09 11:21:54 +02:00
} else {
// Split dependencies into multiple rule lines. This allows for
// very long dependency lists even on older make implementations.
2018-01-26 17:06:56 +01:00
for (std::string const& depend : depends) {
2020-08-30 11:54:41 +02:00
os << tgt << space << ": "
2021-09-14 00:13:48 +02:00
<< this->ConvertToMakefilePath(this->MaybeRelativeToTopBinDir(depend))
2020-08-30 11:54:41 +02:00
<< '\n';
}
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
if (!commands.empty()) {
// Write the list of commands.
os << cmWrap("\t", commands, "", "\n") << "\n";
}
2016-07-09 11:21:54 +02:00
if (symbolic && !this->IsWatcomWMake()) {
2020-08-30 11:54:41 +02:00
os << ".PHONY : " << tgt << "\n";
2016-07-09 11:21:54 +02:00
}
os << "\n";
// Add the output to the local help if requested.
2016-07-09 11:21:54 +02:00
if (in_help) {
this->LocalHelp.push_back(target);
2016-07-09 11:21:54 +02:00
}
}
2016-10-30 18:24:19 +01:00
std::string cmLocalUnixMakefileGenerator3::MaybeConvertWatcomShellCommand(
std::string const& cmd)
2012-02-18 12:40:36 +02:00
{
2018-04-23 21:13:27 +02:00
if (this->IsWatcomWMake() && cmSystemTools::FileIsFullPath(cmd) &&
2017-07-20 19:35:53 +02:00
cmd.find_first_of("( )") != std::string::npos) {
2012-02-18 12:40:36 +02:00
// On Watcom WMake use the windows short path for the command
// name. This is needed to avoid funny quoting problems on
// lines with shell redirection operators.
std::string scmd;
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetShortPath(cmd, scmd)) {
2016-10-30 18:24:19 +01:00
return this->ConvertToOutputFormat(scmd, cmOutputConverter::SHELL);
2012-02-18 12:40:36 +02:00
}
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
return std::string();
2012-02-18 12:40:36 +02:00
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteMakeVariables(
std::ostream& makefileStream)
{
this->WriteDivider(makefileStream);
2016-07-09 11:21:54 +02:00
makefileStream << "# Set environment variables for the build.\n"
<< "\n";
2015-08-17 11:37:30 +02:00
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
2016-07-09 11:21:54 +02:00
if (gg->DefineWindowsNULL) {
makefileStream << "!IF \"$(OS)\" == \"Windows_NT\"\n"
<< "NULL=\n"
<< "!ELSE\n"
<< "NULL=nul\n"
<< "!ENDIF\n";
}
if (this->IsWindowsShell()) {
makefileStream << "SHELL = cmd.exe\n"
<< "\n";
} else {
2009-10-04 10:30:41 +03:00
#if !defined(__VMS)
2016-07-09 11:21:54 +02:00
/* clang-format off */
makefileStream
<< "# The shell in which to execute make rules.\n"
<< "SHELL = /bin/sh\n"
<< "\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
2009-10-04 10:30:41 +03:00
#endif
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
std::string cmakeShellCommand =
this->MaybeConvertWatcomShellCommand(cmSystemTools::GetCMakeCommand());
if (cmakeShellCommand.empty()) {
cmakeShellCommand = this->ConvertToOutputFormat(
2020-08-30 11:54:41 +02:00
cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
2016-10-30 18:24:19 +01:00
}
2016-07-09 11:21:54 +02:00
/* clang-format off */
makefileStream
<< "# The CMake executable.\n"
<< "CMAKE_COMMAND = "
2016-10-30 18:24:19 +01:00
<< cmakeShellCommand
<< "\n"
<< "\n";
makefileStream
<< "# The command to remove a file.\n"
<< "RM = "
2016-10-30 18:24:19 +01:00
<< cmakeShellCommand
2020-08-30 11:54:41 +02:00
<< " -E rm -f\n"
<< "\n";
2012-06-27 20:52:58 +03:00
makefileStream
<< "# Escaping for special characters.\n"
<< "EQUALS = =\n"
<< "\n";
makefileStream
<< "# The top-level source directory on which CMake was run.\n"
<< "CMAKE_SOURCE_DIR = "
2016-10-30 18:24:19 +01:00
<< this->ConvertToOutputFormat(
2020-08-30 11:54:41 +02:00
this->GetSourceDirectory(), cmOutputConverter::SHELL)
<< "\n"
<< "\n";
makefileStream
<< "# The top-level build directory on which CMake was run.\n"
<< "CMAKE_BINARY_DIR = "
2016-10-30 18:24:19 +01:00
<< this->ConvertToOutputFormat(
2020-08-30 11:54:41 +02:00
this->GetBinaryDirectory(), cmOutputConverter::SHELL)
<< "\n"
<< "\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteSpecialTargetsTop(
std::ostream& makefileStream)
{
this->WriteDivider(makefileStream);
2016-07-09 11:21:54 +02:00
makefileStream << "# Special targets provided by cmake.\n"
<< "\n";
std::vector<std::string> no_commands;
std::vector<std::string> no_depends;
// Special target to cleanup operation of make tool.
// This should be the first target except for the default_target in
// the interface Makefile.
2016-07-09 11:21:54 +02:00
this->WriteMakeRule(makefileStream,
"Disable implicit rules so canonical targets will work.",
".SUFFIXES", no_depends, no_commands, false);
2016-07-09 11:21:54 +02:00
if (!this->IsNMake() && !this->IsWatcomWMake() &&
!this->BorlandMakeCurlyHack) {
// turn off RCS and SCCS automatic stuff from gmake
2020-08-30 11:54:41 +02:00
constexpr const char* vcs_rules[] = {
"%,v", "RCS/%", "RCS/%,v", "SCCS/s.%", "s.%",
};
2021-09-14 00:13:48 +02:00
for (const auto* vcs_rule : vcs_rules) {
2020-08-30 11:54:41 +02:00
std::vector<std::string> vcs_depend;
vcs_depend.emplace_back(vcs_rule);
this->WriteMakeRule(makefileStream, "Disable VCS-based implicit rules.",
"%", vcs_depend, no_commands, false);
}
2016-07-09 11:21:54 +02:00
}
// Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
std::vector<std::string> depends;
2019-11-11 23:01:05 +01:00
depends.emplace_back(".hpux_make_needs_suffix_list");
2018-01-26 17:06:56 +01:00
this->WriteMakeRule(makefileStream, nullptr, ".SUFFIXES", depends,
2016-10-30 18:24:19 +01:00
no_commands, false);
2016-07-09 11:21:54 +02:00
if (this->IsWatcomWMake()) {
2015-04-27 22:25:09 +02:00
// Switch on WMake feature, if an error or interrupt occurs during
// makefile processing, the current target being made may be deleted
// without prompting (the same as command line -e option).
2016-07-09 11:21:54 +02:00
/* clang-format off */
2015-04-27 22:25:09 +02:00
makefileStream <<
"\n"
".ERASE\n"
"\n"
;
2016-07-09 11:21:54 +02:00
/* clang-format on */
}
if (this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE")) {
/* clang-format off */
makefileStream
<< "# Produce verbose output by default.\n"
<< "VERBOSE = 1\n"
<< "\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
}
if (this->IsWatcomWMake()) {
/* clang-format off */
2015-04-27 22:25:09 +02:00
makefileStream <<
"!ifndef VERBOSE\n"
".SILENT\n"
"!endif\n"
"\n"
;
2016-07-09 11:21:54 +02:00
/* clang-format on */
} else {
2020-08-30 11:54:41 +02:00
makefileStream << "# Command-line flag to silence nested $(MAKE).\n"
"$(VERBOSE)MAKESILENT = -s\n"
"\n";
2015-04-27 22:25:09 +02:00
// Write special target to silence make output. This must be after
// the default target in case VERBOSE is set (which changes the
// name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
// "VERBOSE=1" to be added as a make variable which will change the
// name of this special target. This gives a make-time choice to
// the user.
2020-08-30 11:54:41 +02:00
// Write directly to the stream since WriteMakeRule escapes '$'.
makefileStream << "#Suppress display of executed commands.\n"
"$(VERBOSE).SILENT:\n"
"\n";
2016-07-09 11:21:54 +02:00
}
// Work-around for makes that drop rules that have no dependencies
// or commands.
2015-04-27 22:25:09 +02:00
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
std::string hack = gg->GetEmptyRuleHackDepends();
2016-07-09 11:21:54 +02:00
if (!hack.empty()) {
2018-04-23 21:13:27 +02:00
no_depends.push_back(std::move(hack));
2016-07-09 11:21:54 +02:00
}
std::string hack_cmd = gg->GetEmptyRuleHackCommand();
2016-07-09 11:21:54 +02:00
if (!hack_cmd.empty()) {
2018-04-23 21:13:27 +02:00
no_commands.push_back(std::move(hack_cmd));
2016-07-09 11:21:54 +02:00
}
// Special symbolic target that never exists to force dependers to
// run their rules.
2016-07-09 11:21:54 +02:00
this->WriteMakeRule(makefileStream, "A target that is always out of date.",
"cmake_force", no_depends, no_commands, true);
// Variables for reference by other rules.
this->WriteMakeVariables(makefileStream);
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteSpecialTargetsBottom(
std::ostream& makefileStream)
{
this->WriteDivider(makefileStream);
2016-07-09 11:21:54 +02:00
makefileStream << "# Special targets to cleanup operation of make.\n"
<< "\n";
// Write special "cmake_check_build_system" target to run cmake with
// the --check-build-system flag.
2018-08-09 18:06:22 +02:00
if (!this->GlobalGenerator->GlobalSettingIsOn(
"CMAKE_SUPPRESS_REGENERATION")) {
2016-07-09 11:21:54 +02:00
// Build command to run CMake to check if anything needs regenerating.
2018-08-09 18:06:22 +02:00
std::vector<std::string> commands;
cmake* cm = this->GlobalGenerator->GetCMakeInstance();
if (cm->DoWriteGlobVerifyTarget()) {
2020-02-01 23:06:01 +01:00
std::string rescanRule =
cmStrCat("$(CMAKE_COMMAND) -P ",
this->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
cmOutputConverter::SHELL));
2018-08-09 18:06:22 +02:00
commands.push_back(rescanRule);
}
2020-02-01 23:06:01 +01:00
std::string cmakefileName = "CMakeFiles/Makefile.cmake";
std::string runRule = cmStrCat(
2023-07-02 19:51:09 +02:00
"$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) ",
cm->GetIgnoreWarningAsError() ? "--compile-no-warning-as-error " : "",
2020-02-01 23:06:01 +01:00
"--check-build-system ",
this->ConvertToOutputFormat(cmakefileName, cmOutputConverter::SHELL),
" 0");
2016-07-09 11:21:54 +02:00
std::vector<std::string> no_depends;
2018-04-23 21:13:27 +02:00
commands.push_back(std::move(runRule));
2016-07-09 11:21:54 +02:00
if (!this->IsRootMakefile()) {
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
2016-07-09 11:21:54 +02:00
}
2018-08-09 18:06:22 +02:00
this->WriteMakeRule(makefileStream,
"Special rule to run CMake to check the build system "
"integrity.\n"
"No rule that depends on this can have "
"commands that come from listfiles\n"
"because they might be regenerated.",
"cmake_check_build_system", no_depends, commands,
true);
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteConvenienceRule(
std::ostream& ruleFileStream, const std::string& realTarget,
const std::string& helpTarget)
{
// A rule is only needed if the names are different.
2016-07-09 11:21:54 +02:00
if (realTarget != helpTarget) {
// The helper target depends on the real target.
std::vector<std::string> depends;
depends.push_back(realTarget);
// There are no commands.
std::vector<std::string> no_commands;
// Write the rule.
this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
helpTarget, depends, no_commands, true);
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
std::string cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(
2020-02-01 23:06:01 +01:00
cmGeneratorTarget const* target) const
{
2020-02-01 23:06:01 +01:00
std::string dir =
cmStrCat(this->HomeRelativeOutputPath, this->GetTargetDirectory(target));
2016-10-30 18:24:19 +01:00
return dir;
}
2018-04-23 21:13:27 +02:00
void cmLocalUnixMakefileGenerator3::AppendFlags(
std::string& flags, const std::string& newFlags) const
{
2016-07-09 11:21:54 +02:00
if (this->IsWatcomWMake() && !newFlags.empty()) {
std::string newf = newFlags;
2017-07-20 19:35:53 +02:00
if (newf.find("\\\"") != std::string::npos) {
cmSystemTools::ReplaceString(newf, "\\\"", "\"");
2015-04-27 22:25:09 +02:00
this->cmLocalGenerator::AppendFlags(flags, newf);
return;
}
2016-07-09 11:21:54 +02:00
}
this->cmLocalGenerator::AppendFlags(flags, newFlags);
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendRuleDepend(
std::vector<std::string>& depends, const char* ruleFileName)
{
// Add a dependency on the rule file itself unless an option to skip
// it is specifically enabled by the user or project.
2021-11-20 13:41:27 +01:00
cmValue nodep = this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
2021-09-14 00:13:48 +02:00
if (cmIsOff(nodep)) {
2019-11-11 23:01:05 +01:00
depends.emplace_back(ruleFileName);
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendRuleDepends(
std::vector<std::string>& depends, std::vector<std::string> const& ruleFiles)
2011-06-19 15:41:06 +03:00
{
// Add a dependency on the rule file itself unless an option to skip
// it is specifically enabled by the user or project.
2016-07-09 11:21:54 +02:00
if (!this->Makefile->IsOn("CMAKE_SKIP_RULE_DEPENDENCY")) {
2020-08-30 11:54:41 +02:00
cm::append(depends, ruleFiles);
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendCustomDepends(
std::vector<std::string>& depends, const std::vector<cmCustomCommand>& ccs)
{
2018-01-26 17:06:56 +01:00
for (cmCustomCommand const& cc : ccs) {
2020-08-30 11:54:41 +02:00
cmCustomCommandGenerator ccg(cc, this->GetConfigName(), this);
2015-04-27 22:25:09 +02:00
this->AppendCustomDepend(depends, ccg);
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendCustomDepend(
std::vector<std::string>& depends, cmCustomCommandGenerator const& ccg)
{
2018-01-26 17:06:56 +01:00
for (std::string const& d : ccg.GetDepends()) {
// Lookup the real name of the dependency in case it is a CMake target.
2011-01-16 11:35:12 +01:00
std::string dep;
2020-08-30 11:54:41 +02:00
if (this->GetRealDependency(d, this->GetConfigName(), dep)) {
2018-04-23 21:13:27 +02:00
depends.push_back(std::move(dep));
}
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendCustomCommands(
std::vector<std::string>& commands, const std::vector<cmCustomCommand>& ccs,
2016-10-30 18:24:19 +01:00
cmGeneratorTarget* target, std::string const& relative)
{
2018-01-26 17:06:56 +01:00
for (cmCustomCommand const& cc : ccs) {
2020-08-30 11:54:41 +02:00
cmCustomCommandGenerator ccg(cc, this->GetConfigName(), this);
2016-10-30 18:24:19 +01:00
this->AppendCustomCommand(commands, ccg, target, relative, true);
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendCustomCommand(
std::vector<std::string>& commands, cmCustomCommandGenerator const& ccg,
2016-10-30 18:24:19 +01:00
cmGeneratorTarget* target, std::string const& relative, bool echo_comment,
std::ostream* content)
{
// Optionally create a command to display the custom command's
// comment text. This is used for pre-build, pre-link, and
// post-build command comments. Custom build step commands have
// their comments generated elsewhere.
2016-07-09 11:21:54 +02:00
if (echo_comment) {
2023-05-23 16:38:00 +02:00
if (cm::optional<std::string> comment = ccg.GetComment()) {
this->AppendEcho(commands, *comment,
cmLocalUnixMakefileGenerator3::EchoGenerate);
}
2016-07-09 11:21:54 +02:00
}
// if the command specified a working directory use it.
2016-07-09 11:21:54 +02:00
std::string dir = this->GetCurrentBinaryDirectory();
2015-04-27 22:25:09 +02:00
std::string workingDir = ccg.GetWorkingDirectory();
2016-07-09 11:21:54 +02:00
if (!workingDir.empty()) {
dir = workingDir;
2016-07-09 11:21:54 +02:00
}
if (content) {
2009-10-04 10:30:41 +03:00
*content << dir;
2016-07-09 11:21:54 +02:00
}
2023-07-02 19:51:09 +02:00
auto rulePlaceholderExpander = this->CreateRulePlaceholderExpander();
2017-04-14 19:02:05 +02:00
// Add each command line to the set of commands.
std::vector<std::string> commands1;
2016-07-09 11:21:54 +02:00
for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
// Build the command line in a single string.
2011-01-16 11:35:12 +01:00
std::string cmd = ccg.GetCommand(c);
2016-07-09 11:21:54 +02:00
if (!cmd.empty()) {
2010-06-23 01:18:35 +03:00
// Use "call " before any invocations of .bat or .cmd files
// invoked as custom commands in the WindowsShell.
//
bool useCall = false;
2016-07-09 11:21:54 +02:00
if (this->IsWindowsShell()) {
2010-06-23 01:18:35 +03:00
std::string suffix;
2016-07-09 11:21:54 +02:00
if (cmd.size() > 4) {
suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size() - 4));
if (suffix == ".bat" || suffix == ".cmd") {
2010-06-23 01:18:35 +03:00
useCall = true;
}
}
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
cmSystemTools::ReplaceString(cmd, "/./", "/");
// Convert the command to a relative path only if the current
// working directory will be the start-output directory.
2017-07-20 19:35:53 +02:00
bool had_slash = cmd.find('/') != std::string::npos;
2016-07-09 11:21:54 +02:00
if (workingDir.empty()) {
2021-09-14 00:13:48 +02:00
cmd = this->MaybeRelativeToCurBinDir(cmd);
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
bool has_slash = cmd.find('/') != std::string::npos;
2016-07-09 11:21:54 +02:00
if (had_slash && !has_slash) {
// This command was specified as a path to a file in the
// current directory. Add a leading "./" so it can run
// without the current directory being in the search path.
2020-02-01 23:06:01 +01:00
cmd = cmStrCat("./", cmd);
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
std::string launcher;
// Short-circuit if there is no launcher.
2023-07-02 19:51:09 +02:00
std::string val = this->GetRuleLauncher(
target, "RULE_LAUNCH_CUSTOM",
this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
2021-09-14 00:13:48 +02:00
if (cmNonempty(val)) {
2018-10-28 12:09:07 +01:00
// Expand rule variables referenced in the given launcher command.
2017-04-14 19:02:05 +02:00
cmRulePlaceholderExpander::RuleVariables vars;
vars.CMTargetName = target->GetName().c_str();
2020-08-30 11:54:41 +02:00
vars.CMTargetType =
cmState::GetTargetTypeName(target->GetType()).c_str();
2016-10-30 18:24:19 +01:00
std::string output;
const std::vector<std::string>& outputs = ccg.GetOutputs();
if (!outputs.empty()) {
2017-04-14 19:02:05 +02:00
output = outputs[0];
2016-10-30 18:24:19 +01:00
if (workingDir.empty()) {
2021-09-14 00:13:48 +02:00
output = this->MaybeRelativeToCurBinDir(output);
2016-10-30 18:24:19 +01:00
}
2017-04-14 19:02:05 +02:00
output =
this->ConvertToOutputFormat(output, cmOutputConverter::SHELL);
2016-10-30 18:24:19 +01:00
}
vars.Output = output.c_str();
2023-07-02 19:51:09 +02:00
launcher = val;
2017-04-14 19:02:05 +02:00
rulePlaceholderExpander->ExpandRuleVariables(this, launcher, vars);
2016-10-30 18:24:19 +01:00
if (!launcher.empty()) {
launcher += " ";
}
}
std::string shellCommand = this->MaybeConvertWatcomShellCommand(cmd);
if (shellCommand.empty()) {
shellCommand =
this->ConvertToOutputFormat(cmd, cmOutputConverter::SHELL);
}
cmd = launcher + shellCommand;
2011-01-16 11:35:12 +01:00
ccg.AppendArguments(c, cmd);
2016-07-09 11:21:54 +02:00
if (content) {
2009-10-04 10:30:41 +03:00
// Rule content does not include the launcher.
2016-07-09 11:21:54 +02:00
*content << (cmd.c_str() + launcher.size());
}
if (this->BorlandMakeCurlyHack) {
// Borland Make has a very strange bug. If the first curly
// brace anywhere in the command string is a left curly, it
// must be written {{} instead of just {. Otherwise some
// curly braces are removed. The hack can be skipped if the
// first curly brace is the last character.
2016-07-09 11:21:54 +02:00
std::string::size_type lcurly = cmd.find('{');
2017-07-20 19:35:53 +02:00
if (lcurly != std::string::npos && lcurly < (cmd.size() - 1)) {
2016-07-09 11:21:54 +02:00
std::string::size_type rcurly = cmd.find('}');
2017-07-20 19:35:53 +02:00
if (rcurly == std::string::npos || rcurly > lcurly) {
// The first curly is a left curly. Use the hack.
2020-02-01 23:06:01 +01:00
cmd =
cmStrCat(cmd.substr(0, lcurly), "{{}", cmd.substr(lcurly + 1));
}
}
2016-07-09 11:21:54 +02:00
}
if (launcher.empty()) {
if (useCall) {
2020-02-01 23:06:01 +01:00
cmd = cmStrCat("call ", cmd);
2016-07-09 11:21:54 +02:00
} else if (this->IsNMake() && cmd[0] == '"') {
2020-02-01 23:06:01 +01:00
cmd = cmStrCat("echo >nul && ", cmd);
2010-06-23 01:18:35 +03:00
}
}
2018-04-23 21:13:27 +02:00
commands1.push_back(std::move(cmd));
}
2016-07-09 11:21:54 +02:00
}
// Setup the proper working directory for the commands.
2018-04-23 21:13:27 +02:00
this->CreateCDCommand(commands1, dir, relative);
// push back the custom commands
2020-08-30 11:54:41 +02:00
cm::append(commands, commands1);
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendCleanCommand(
2019-11-11 23:01:05 +01:00
std::vector<std::string>& commands, const std::set<std::string>& files,
2016-07-09 11:21:54 +02:00
cmGeneratorTarget* target, const char* filename)
{
2016-10-30 18:24:19 +01:00
std::string currentBinDir = this->GetCurrentBinaryDirectory();
2020-02-01 23:06:01 +01:00
std::string cleanfile = cmStrCat(
currentBinDir, '/', this->GetTargetDirectory(target), "/cmake_clean");
2016-07-09 11:21:54 +02:00
if (filename) {
2014-08-03 19:52:23 +02:00
cleanfile += "_";
cleanfile += filename;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
cleanfile += ".cmake";
2020-08-30 11:54:41 +02:00
cmsys::ofstream fout(cleanfile.c_str());
2016-07-09 11:21:54 +02:00
if (!fout) {
2020-08-30 11:54:41 +02:00
cmSystemTools::Error("Could not create " + cleanfile);
2016-07-09 11:21:54 +02:00
}
if (!files.empty()) {
2014-08-03 19:52:23 +02:00
fout << "file(REMOVE_RECURSE\n";
2018-01-26 17:06:56 +01:00
for (std::string const& file : files) {
2021-09-14 00:13:48 +02:00
std::string fc = this->MaybeRelativeToCurBinDir(file);
2015-11-17 17:22:37 +01:00
fout << " " << cmOutputConverter::EscapeForCMake(fc) << "\n";
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
fout << ")\n";
}
2018-04-23 21:13:27 +02:00
{
2021-09-14 00:13:48 +02:00
std::string remove = cmStrCat(
"$(CMAKE_COMMAND) -P ",
this->ConvertToOutputFormat(this->MaybeRelativeToCurBinDir(cleanfile),
cmOutputConverter::SHELL));
2018-04-23 21:13:27 +02:00
commands.push_back(std::move(remove));
}
2014-08-03 19:52:23 +02:00
// For the main clean rule add per-language cleaning.
2016-07-09 11:21:54 +02:00
if (!filename) {
2014-08-03 19:52:23 +02:00
// Get the set of source languages in the target.
2015-04-27 22:25:09 +02:00
std::set<std::string> languages;
2016-07-09 11:21:54 +02:00
target->GetLanguages(
languages, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
/* clang-format off */
2014-08-03 19:52:23 +02:00
fout << "\n"
<< "# Per-language clean rules from dependency scanning.\n"
2015-08-17 11:37:30 +02:00
<< "foreach(lang " << cmJoin(languages, " ") << ")\n"
2014-08-03 19:52:23 +02:00
<< " include(" << this->GetTargetDirectory(target)
<< "/cmake_clean_${lang}.cmake OPTIONAL)\n"
<< "endforeach()\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
}
}
2019-11-11 23:01:05 +01:00
void cmLocalUnixMakefileGenerator3::AppendDirectoryCleanCommand(
std::vector<std::string>& commands)
{
2023-07-02 19:51:09 +02:00
cmList cleanFiles;
2019-11-11 23:01:05 +01:00
// Look for additional files registered for cleaning in this directory.
2021-11-20 13:41:27 +01:00
if (cmValue prop_value =
2019-11-11 23:01:05 +01:00
this->Makefile->GetProperty("ADDITIONAL_CLEAN_FILES")) {
2023-07-02 19:51:09 +02:00
cleanFiles.assign(cmGeneratorExpression::Evaluate(
*prop_value, this,
this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE")));
2019-11-11 23:01:05 +01:00
}
if (cleanFiles.empty()) {
return;
}
2020-08-30 11:54:41 +02:00
const auto& rootLG = this->GetGlobalGenerator()->GetLocalGenerators().at(0);
2019-11-11 23:01:05 +01:00
std::string const& currentBinaryDir = this->GetCurrentBinaryDirectory();
2020-02-01 23:06:01 +01:00
std::string cleanfile =
cmStrCat(currentBinaryDir, "/CMakeFiles/cmake_directory_clean.cmake");
2019-11-11 23:01:05 +01:00
// Write clean script
{
2020-08-30 11:54:41 +02:00
cmsys::ofstream fout(cleanfile.c_str());
2019-11-11 23:01:05 +01:00
if (!fout) {
2020-08-30 11:54:41 +02:00
cmSystemTools::Error("Could not create " + cleanfile);
2019-11-11 23:01:05 +01:00
return;
}
fout << "file(REMOVE_RECURSE\n";
for (std::string const& cfl : cleanFiles) {
2021-09-14 00:13:48 +02:00
std::string fc = rootLG->MaybeRelativeToCurBinDir(
cmSystemTools::CollapseFullPath(cfl, currentBinaryDir));
2019-11-11 23:01:05 +01:00
fout << " " << cmOutputConverter::EscapeForCMake(fc) << "\n";
}
fout << ")\n";
}
// Create command
{
2021-09-14 00:13:48 +02:00
std::string remove = cmStrCat(
"$(CMAKE_COMMAND) -P ",
this->ConvertToOutputFormat(rootLG->MaybeRelativeToCurBinDir(cleanfile),
cmOutputConverter::SHELL));
2019-11-11 23:01:05 +01:00
commands.push_back(std::move(remove));
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AppendEcho(
std::vector<std::string>& commands, std::string const& text, EchoColor color,
EchoProgress const* progress)
{
// Choose the color for the text.
std::string color_name;
2016-07-09 11:21:54 +02:00
if (this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile) {
// See cmake::ExecuteEchoColor in cmake.cxx for these options.
// This color set is readable on both black and white backgrounds.
2016-07-09 11:21:54 +02:00
switch (color) {
case EchoNormal:
break;
case EchoDepend:
color_name = "--magenta --bold ";
break;
case EchoBuild:
color_name = "--green ";
break;
case EchoLink:
2015-08-17 11:37:30 +02:00
color_name = "--green --bold ";
break;
case EchoGenerate:
color_name = "--blue --bold ";
break;
case EchoGlobal:
color_name = "--cyan ";
break;
}
2016-07-09 11:21:54 +02:00
}
// Echo one line at a time.
std::string line;
line.reserve(200);
2016-07-09 11:21:54 +02:00
for (const char* c = text.c_str();; ++c) {
if (*c == '\n' || *c == '\0') {
// Avoid writing a blank last line on end-of-string.
2016-07-09 11:21:54 +02:00
if (*c != '\0' || !line.empty()) {
// Add a command to echo this line.
std::string cmd;
2016-07-09 11:21:54 +02:00
if (color_name.empty() && !progress) {
// Use the native echo command.
2020-02-01 23:06:01 +01:00
cmd = cmStrCat("@echo ", this->EscapeForShell(line, false, true));
2016-07-09 11:21:54 +02:00
} else {
// Use cmake to echo the text in color.
2020-02-01 23:06:01 +01:00
cmd = cmStrCat(
2023-07-02 19:51:09 +02:00
"@$(CMAKE_COMMAND) -E cmake_echo_color \"--switch=$(COLOR)\" ",
2020-02-01 23:06:01 +01:00
color_name);
2016-07-09 11:21:54 +02:00
if (progress) {
2015-08-17 11:37:30 +02:00
cmd += "--progress-dir=";
2020-08-30 11:54:41 +02:00
cmd += this->ConvertToOutputFormat(progress->Dir,
cmOutputConverter::SHELL);
2015-08-17 11:37:30 +02:00
cmd += " ";
cmd += "--progress-num=";
cmd += progress->Arg;
cmd += " ";
}
2016-07-09 11:21:54 +02:00
cmd += this->EscapeForShell(line);
}
2018-04-23 21:13:27 +02:00
commands.push_back(std::move(cmd));
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
// Reset the line to empty.
2018-01-26 17:06:56 +01:00
line.clear();
2015-08-17 11:37:30 +02:00
// Progress appears only on first line.
2018-01-26 17:06:56 +01:00
progress = nullptr;
2015-08-17 11:37:30 +02:00
// Terminate on end-of-string.
2016-07-09 11:21:54 +02:00
if (*c == '\0') {
return;
}
2016-07-09 11:21:54 +02:00
} else if (*c != '\r') {
// Append this character to the current line.
line += *c;
}
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
std::string cmLocalUnixMakefileGenerator3::CreateMakeVariable(
2017-07-20 19:35:53 +02:00
std::string const& s, std::string const& s2)
{
2020-02-01 23:06:01 +01:00
std::string unmodified = cmStrCat(s, s2);
// if there is no restriction on the length of make variables
2013-11-03 12:27:13 +02:00
// and there are no "." characters in the string, then return the
// unmodified combination.
2017-07-20 19:35:53 +02:00
if ((!this->MakefileVariableSize &&
unmodified.find('.') == std::string::npos) &&
(!this->MakefileVariableSize &&
unmodified.find('+') == std::string::npos) &&
(!this->MakefileVariableSize &&
unmodified.find('-') == std::string::npos)) {
return unmodified;
2016-07-09 11:21:54 +02:00
}
// see if the variable has been defined before and return
// the modified version of the variable
2020-02-01 23:06:01 +01:00
auto i = this->MakeVariableMap.find(unmodified);
2016-07-09 11:21:54 +02:00
if (i != this->MakeVariableMap.end()) {
return i->second;
2016-07-09 11:21:54 +02:00
}
// start with the unmodified variable
std::string ret = unmodified;
// if this there is no value for this->MakefileVariableSize then
// the string must have bad characters in it
2016-07-09 11:21:54 +02:00
if (!this->MakefileVariableSize) {
std::replace(ret.begin(), ret.end(), '.', '_');
cmSystemTools::ReplaceString(ret, "-", "__");
2012-02-18 12:40:36 +02:00
cmSystemTools::ReplaceString(ret, "+", "___");
int ni = 0;
2022-03-29 21:10:50 +02:00
char buffer[12];
// make sure the _ version is not already used, if
// it is used then add number to the end of the variable
2016-07-09 11:21:54 +02:00
while (this->ShortMakeVariableMap.count(ret) && ni < 1000) {
++ni;
2022-03-29 21:10:50 +02:00
snprintf(buffer, sizeof(buffer), "%04d", ni);
ret = unmodified + buffer;
2016-07-09 11:21:54 +02:00
}
this->ShortMakeVariableMap[ret] = "1";
this->MakeVariableMap[unmodified] = ret;
return ret;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
// if the string is greater than 32 chars it is an invalid variable name
// for borland make
2016-07-09 11:21:54 +02:00
if (static_cast<int>(ret.size()) > this->MakefileVariableSize) {
int keep = this->MakefileVariableSize - 8;
int size = keep + 3;
std::string str1 = s;
std::string str2 = s2;
2013-11-03 12:27:13 +02:00
// we must shorten the combined string by 4 characters
// keep no more than 24 characters from the second string
2016-07-09 11:21:54 +02:00
if (static_cast<int>(str2.size()) > keep) {
str2 = str2.substr(0, keep);
2016-07-09 11:21:54 +02:00
}
if (static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size) {
str1 = str1.substr(0, size - str2.size());
2016-07-09 11:21:54 +02:00
}
2022-03-29 21:10:50 +02:00
char buffer[12];
int ni = 0;
2022-03-29 21:10:50 +02:00
snprintf(buffer, sizeof(buffer), "%04d", ni);
ret = str1 + str2 + buffer;
2016-07-09 11:21:54 +02:00
while (this->ShortMakeVariableMap.count(ret) && ni < 1000) {
++ni;
2022-03-29 21:10:50 +02:00
snprintf(buffer, sizeof(buffer), "%04d", ni);
ret = str1 + str2 + buffer;
2016-07-09 11:21:54 +02:00
}
if (ni == 1000) {
cmSystemTools::Error("Borland makefile variable length too long");
return unmodified;
2016-07-09 11:21:54 +02:00
}
// once an unused variable is found
this->ShortMakeVariableMap[ret] = "1";
2016-07-09 11:21:54 +02:00
}
// always make an entry into the unmodified to variable map
this->MakeVariableMap[unmodified] = ret;
return ret;
}
2019-11-11 23:01:05 +01:00
bool cmLocalUnixMakefileGenerator3::UpdateDependencies(
const std::string& tgtInfo, bool verbose, bool color)
{
// read in the target info file
2016-07-09 11:21:54 +02:00
if (!this->Makefile->ReadListFile(tgtInfo) ||
2022-08-04 22:12:04 +02:00
cmSystemTools::GetErrorOccurredFlag()) {
cmSystemTools::Error("Target DependInfo.cmake file not found");
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
bool status = true;
// Check if any multiple output pairs have a missing file.
this->CheckMultipleOutputs(verbose);
2019-11-11 23:01:05 +01:00
std::string const targetDir = cmSystemTools::GetFilenamePath(tgtInfo);
2021-09-14 00:13:48 +02:00
if (!this->Makefile->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES").empty()) {
// dependencies are managed by CMake itself
std::string const internalDependFile = targetDir + "/depend.internal";
std::string const dependFile = targetDir + "/depend.make";
// If the target DependInfo.cmake file has changed since the last
// time dependencies were scanned then force rescanning. This may
// happen when a new source file is added and CMake regenerates the
// project but no other sources were touched.
bool needRescanDependInfo = false;
cmFileTimeCache* ftc =
this->GlobalGenerator->GetCMakeInstance()->GetFileTimeCache();
{
int result;
if (!ftc->Compare(internalDependFile, tgtInfo, &result) || result < 0) {
if (verbose) {
cmSystemTools::Stdout(cmStrCat("Dependee \"", tgtInfo,
"\" is newer than depender \"",
internalDependFile, "\".\n"));
}
needRescanDependInfo = true;
}
}
2021-09-14 00:13:48 +02:00
// If the directory information is newer than depend.internal, include
// dirs may have changed. In this case discard all old dependencies.
bool needRescanDirInfo = false;
{
std::string dirInfoFile =
cmStrCat(this->GetCurrentBinaryDirectory(),
"/CMakeFiles/CMakeDirectoryInformation.cmake");
int result;
if (!ftc->Compare(internalDependFile, dirInfoFile, &result) ||
result < 0) {
if (verbose) {
cmSystemTools::Stdout(cmStrCat("Dependee \"", dirInfoFile,
"\" is newer than depender \"",
internalDependFile, "\".\n"));
}
needRescanDirInfo = true;
2009-10-04 10:30:41 +03:00
}
2021-09-14 00:13:48 +02:00
}
// Check the implicit dependencies to see if they are up to date.
// The build.make file may have explicit dependencies for the object
// files but these will not affect the scanning process so they need
// not be considered.
cmDepends::DependencyMap validDependencies;
bool needRescanDependencies = false;
if (!needRescanDirInfo) {
cmDependsC checker;
checker.SetVerbose(verbose);
checker.SetFileTimeCache(ftc);
// cmDependsC::Check() fills the vector validDependencies() with the
// dependencies for those files where they are still valid, i.e.
// neither the files themselves nor any files they depend on have
// changed. We don't do that if the CMakeDirectoryInformation.cmake
// file has changed, because then potentially all dependencies have
// changed. This information is given later on to cmDependsC, which
// then only rescans the files where it did not get valid dependencies
// via this dependency vector. This means that in the normal case, when
// only few or one file have been edited, then also only this one file
// is actually scanned again, instead of all files for this target.
needRescanDependencies =
!checker.Check(dependFile, internalDependFile, validDependencies);
}
if (needRescanDependInfo || needRescanDirInfo || needRescanDependencies) {
// The dependencies must be regenerated.
2022-11-16 20:14:03 +01:00
if (verbose) {
std::string targetName = cmSystemTools::GetFilenameName(targetDir);
targetName = targetName.substr(0, targetName.length() - 4);
std::string message =
cmStrCat("Scanning dependencies of target ", targetName);
cmSystemTools::MakefileColorEcho(
cmsysTerminal_Color_ForegroundMagenta |
cmsysTerminal_Color_ForegroundBold,
message.c_str(), true, color);
}
2021-09-14 00:13:48 +02:00
status = this->ScanDependencies(targetDir, dependFile,
internalDependFile, validDependencies);
2009-10-04 10:30:41 +03:00
}
}
2011-01-16 11:35:12 +01:00
2021-09-14 00:13:48 +02:00
auto depends =
this->Makefile->GetSafeDefinition("CMAKE_DEPENDS_DEPENDENCY_FILES");
if (!depends.empty()) {
// dependencies are managed by compiler
2023-07-02 19:51:09 +02:00
cmList depFiles{ depends, cmList::EmptyElements::Yes };
2021-09-14 00:13:48 +02:00
std::string const internalDepFile =
targetDir + "/compiler_depend.internal";
std::string const depFile = targetDir + "/compiler_depend.make";
cmDepends::DependencyMap dependencies;
cmDependsCompiler depsManager;
bool projectOnly = cmIsOn(
this->Makefile->GetSafeDefinition("CMAKE_DEPENDS_IN_PROJECT_ONLY"));
depsManager.SetVerbose(verbose);
depsManager.SetLocalGenerator(this);
if (!depsManager.CheckDependencies(
internalDepFile, depFiles, dependencies,
projectOnly ? NotInProjectDir(this->GetSourceDirectory(),
this->GetBinaryDirectory())
: std::function<bool(const std::string&)>())) {
// regenerate dependencies files
2022-11-16 20:14:03 +01:00
if (verbose) {
std::string targetName = cmCMakePath(targetDir)
.GetFileName()
.RemoveExtension()
.GenericString();
auto message =
cmStrCat("Consolidate compiler generated dependencies of target ",
targetName);
cmSystemTools::MakefileColorEcho(
cmsysTerminal_Color_ForegroundMagenta |
cmsysTerminal_Color_ForegroundBold,
message.c_str(), true, color);
}
2021-09-14 00:13:48 +02:00
// Open the make depends file. This should be copy-if-different
// because the make tool may try to reload it needlessly otherwise.
cmGeneratedFileStream ruleFileStream(
depFile, false, this->GlobalGenerator->GetMakefileEncoding());
ruleFileStream.SetCopyIfDifferent(true);
if (!ruleFileStream) {
return false;
}
// Open the cmake dependency tracking file. This should not be
// copy-if-different because dependencies are re-scanned when it is
// older than the DependInfo.cmake.
cmGeneratedFileStream internalRuleFileStream(
internalDepFile, false, this->GlobalGenerator->GetMakefileEncoding());
if (!internalRuleFileStream) {
return false;
}
this->WriteDisclaimer(ruleFileStream);
this->WriteDisclaimer(internalRuleFileStream);
depsManager.WriteDependencies(dependencies, ruleFileStream,
internalRuleFileStream);
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// The dependencies are already up-to-date.
2021-09-14 00:13:48 +02:00
return status;
}
2016-07-09 11:21:54 +02:00
bool cmLocalUnixMakefileGenerator3::ScanDependencies(
2019-11-11 23:01:05 +01:00
std::string const& targetDir, std::string const& dependFile,
std::string const& internalDependFile, cmDepends::DependencyMap& validDeps)
{
// Read the directory information file.
cmMakefile* mf = this->Makefile;
bool haveDirectoryInfo = false;
2019-11-11 23:01:05 +01:00
{
2020-02-01 23:06:01 +01:00
std::string dirInfoFile =
cmStrCat(this->GetCurrentBinaryDirectory(),
"/CMakeFiles/CMakeDirectoryInformation.cmake");
2019-11-11 23:01:05 +01:00
if (mf->ReadListFile(dirInfoFile) &&
2022-08-04 22:12:04 +02:00
!cmSystemTools::GetErrorOccurredFlag()) {
2019-11-11 23:01:05 +01:00
haveDirectoryInfo = true;
}
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
// Lookup useful directory information.
2016-07-09 11:21:54 +02:00
if (haveDirectoryInfo) {
// Test whether we need to force Unix paths.
2021-11-20 13:41:27 +01:00
if (cmValue force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS")) {
2020-02-01 23:06:01 +01:00
if (!cmIsOff(force)) {
cmSystemTools::SetForceUnixPaths(true);
}
2016-07-09 11:21:54 +02:00
}
// Setup relative path top directories.
2022-08-04 22:12:04 +02:00
cmValue relativePathTopSource =
mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_SOURCE");
cmValue relativePathTopBinary =
mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_BINARY");
if (relativePathTopSource && relativePathTopBinary) {
this->SetRelativePathTop(*relativePathTopSource, *relativePathTopBinary);
}
2016-07-09 11:21:54 +02:00
} else {
cmSystemTools::Error("Directory Information file not found");
}
// Open the make depends file. This should be copy-if-different
// because the make tool may try to reload it needlessly otherwise.
2017-04-14 19:02:05 +02:00
cmGeneratedFileStream ruleFileStream(
2019-11-11 23:01:05 +01:00
dependFile, false, this->GlobalGenerator->GetMakefileEncoding());
ruleFileStream.SetCopyIfDifferent(true);
2016-07-09 11:21:54 +02:00
if (!ruleFileStream) {
return false;
2016-07-09 11:21:54 +02:00
}
// Open the cmake dependency tracking file. This should not be
// copy-if-different because dependencies are re-scanned when it is
// older than the DependInfo.cmake.
2016-07-09 11:21:54 +02:00
cmGeneratedFileStream internalRuleFileStream(
2019-11-11 23:01:05 +01:00
internalDependFile, false, this->GlobalGenerator->GetMakefileEncoding());
2016-07-09 11:21:54 +02:00
if (!internalRuleFileStream) {
return false;
2016-07-09 11:21:54 +02:00
}
this->WriteDisclaimer(ruleFileStream);
this->WriteDisclaimer(internalRuleFileStream);
2011-01-16 11:35:12 +01:00
// for each language we need to scan, scan it
2023-07-02 19:51:09 +02:00
cmList langs{ mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES") };
2018-01-26 17:06:56 +01:00
for (std::string const& lang : langs) {
// construct the checker
// Create the scanner for this language
2019-11-11 23:01:05 +01:00
std::unique_ptr<cmDepends> scanner;
2017-04-14 19:02:05 +02:00
if (lang == "C" || lang == "CXX" || lang == "RC" || lang == "ASM" ||
2021-09-14 00:13:48 +02:00
lang == "OBJC" || lang == "OBJCXX" || lang == "CUDA" ||
lang == "HIP" || lang == "ISPC") {
// TODO: Handle RC (resource files) dependencies correctly.
2019-11-11 23:01:05 +01:00
scanner = cm::make_unique<cmDependsC>(this, targetDir, lang, &validDeps);
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
#ifndef CMAKE_BOOTSTRAP
2016-07-09 11:21:54 +02:00
else if (lang == "Fortran") {
2018-04-23 21:13:27 +02:00
ruleFileStream << "# Note that incremental build could trigger "
<< "a call to cmake_copy_f90_mod on each re-build\n";
2019-11-11 23:01:05 +01:00
scanner = cm::make_unique<cmDependsFortran>(this);
2016-07-09 11:21:54 +02:00
} else if (lang == "Java") {
2019-11-11 23:01:05 +01:00
scanner = cm::make_unique<cmDependsJava>();
2016-07-09 11:21:54 +02:00
}
#endif
2011-01-16 11:35:12 +01:00
2016-07-09 11:21:54 +02:00
if (scanner) {
scanner->SetLocalGenerator(this);
2019-11-11 23:01:05 +01:00
scanner->SetFileTimeCache(
this->GlobalGenerator->GetCMakeInstance()->GetFileTimeCache());
2015-04-27 22:25:09 +02:00
scanner->SetLanguage(lang);
2019-11-11 23:01:05 +01:00
scanner->SetTargetDirectory(targetDir);
scanner->Write(ruleFileStream, internalRuleFileStream);
}
2016-07-09 11:21:54 +02:00
}
return true;
}
void cmLocalUnixMakefileGenerator3::CheckMultipleOutputs(bool verbose)
{
cmMakefile* mf = this->Makefile;
// Get the string listing the multiple output pairs.
2021-11-20 13:41:27 +01:00
cmValue pairs_string = mf->GetDefinition("CMAKE_MULTIPLE_OUTPUT_PAIRS");
2016-07-09 11:21:54 +02:00
if (!pairs_string) {
return;
2016-07-09 11:21:54 +02:00
}
// Convert the string to a list and preserve empty entries.
2023-07-02 19:51:09 +02:00
cmList pairs{ *pairs_string, cmList::EmptyElements::Yes };
2020-02-01 23:06:01 +01:00
for (auto i = pairs.begin(); i != pairs.end() && (i + 1) != pairs.end();) {
const std::string& depender = *i++;
const std::string& dependee = *i++;
// If the depender is missing then delete the dependee to make
// sure both will be regenerated.
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(dependee) &&
!cmSystemTools::FileExists(depender)) {
2016-07-09 11:21:54 +02:00
if (verbose) {
2020-08-30 11:54:41 +02:00
cmSystemTools::Stdout(cmStrCat(
"Deleting primary custom command output \"", dependee,
"\" because another output \"", depender, "\" does not exist.\n"));
}
2016-07-09 11:21:54 +02:00
cmSystemTools::RemoveFile(dependee);
}
2016-07-09 11:21:54 +02:00
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteLocalAllRules(
std::ostream& ruleFileStream)
{
this->WriteDisclaimer(ruleFileStream);
// Write the main entry point target. This must be the VERY first
// target so that make with no arguments will run it.
{
2016-07-09 11:21:54 +02:00
// Just depend on the all target to drive the build.
std::vector<std::string> depends;
std::vector<std::string> no_commands;
2019-11-11 23:01:05 +01:00
depends.emplace_back("all");
2016-07-09 11:21:54 +02:00
// Write the rule.
2014-08-03 19:52:23 +02:00
this->WriteMakeRule(ruleFileStream,
2016-07-09 11:21:54 +02:00
"Default target executed when no arguments are "
"given to make.",
"default_target", depends, no_commands, true);
// Help out users that try "gmake target1 target2 -j".
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
if (gg->AllowNotParallel()) {
std::vector<std::string> no_depends;
2018-08-09 18:06:22 +02:00
this->WriteMakeRule(ruleFileStream,
"Allow only one \"make -f "
"Makefile2\" at a time, but pass "
"parallelism.",
2016-07-09 11:21:54 +02:00
".NOTPARALLEL", no_depends, no_commands, false);
2014-08-03 19:52:23 +02:00
}
}
this->WriteSpecialTargetsTop(ruleFileStream);
// Include the progress variables for the target.
// Write all global targets
this->WriteDivider(ruleFileStream);
2016-07-09 11:21:54 +02:00
ruleFileStream << "# Targets provided globally by CMake.\n"
<< "\n";
2020-08-30 11:54:41 +02:00
const auto& targets = this->GetGeneratorTargets();
for (const auto& gt : targets) {
2018-01-26 17:06:56 +01:00
if (gt->GetType() == cmStateEnums::GLOBAL_TARGET) {
2016-07-09 11:21:54 +02:00
std::string targetString =
2018-01-26 17:06:56 +01:00
"Special rule for the target " + gt->GetName();
std::vector<std::string> commands;
std::vector<std::string> depends;
2021-11-20 13:41:27 +01:00
cmValue p = gt->GetProperty("EchoString");
2020-08-30 11:54:41 +02:00
const char* text = p ? p->c_str() : "Running external command ...";
2019-11-11 23:01:05 +01:00
depends.reserve(gt->GetUtilities().size());
2020-08-30 11:54:41 +02:00
for (BT<std::pair<std::string, bool>> const& u : gt->GetUtilities()) {
depends.push_back(u.Value.first);
2019-11-11 23:01:05 +01:00
}
this->AppendEcho(commands, text,
cmLocalUnixMakefileGenerator3::EchoGlobal);
// Global targets store their rules in pre- and post-build commands.
2016-07-09 11:21:54 +02:00
this->AppendCustomDepends(depends, gt->GetPreBuildCommands());
this->AppendCustomDepends(depends, gt->GetPostBuildCommands());
2020-08-30 11:54:41 +02:00
this->AppendCustomCommands(commands, gt->GetPreBuildCommands(), gt.get(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
2020-08-30 11:54:41 +02:00
this->AppendCustomCommands(commands, gt->GetPostBuildCommands(),
gt.get(), this->GetCurrentBinaryDirectory());
2016-03-13 13:35:51 +01:00
std::string targetName = gt->GetName();
2016-07-09 11:21:54 +02:00
this->WriteMakeRule(ruleFileStream, targetString.c_str(), targetName,
depends, commands, true);
// Provide a "/fast" version of the target.
depends.clear();
2017-04-14 19:02:05 +02:00
if ((targetName == "install") || (targetName == "install/local") ||
(targetName == "install/strip")) {
// Provide a fast install target that does not depend on all
// but has the same command.
2019-11-11 23:01:05 +01:00
depends.emplace_back("preinstall/fast");
2016-07-09 11:21:54 +02:00
} else {
// Just forward to the real target so at least it will work.
depends.push_back(targetName);
commands.clear();
}
2016-07-09 11:21:54 +02:00
targetName += "/fast";
this->WriteMakeRule(ruleFileStream, targetString.c_str(), targetName,
depends, commands, true);
}
2016-07-09 11:21:54 +02:00
}
std::vector<std::string> depends;
std::vector<std::string> commands;
// Write the all rule.
2020-02-01 23:06:01 +01:00
std::string recursiveTarget =
cmStrCat(this->GetCurrentBinaryDirectory(), "/all");
2018-08-09 18:06:22 +02:00
bool regenerate =
!this->GlobalGenerator->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
if (regenerate) {
2019-11-11 23:01:05 +01:00
depends.emplace_back("cmake_check_build_system");
2018-08-09 18:06:22 +02:00
}
2020-02-01 23:06:01 +01:00
std::string progressDir =
cmStrCat(this->GetBinaryDirectory(), "/CMakeFiles");
2016-07-09 11:21:54 +02:00
{
2015-04-27 22:25:09 +02:00
std::ostringstream progCmd;
2016-07-09 11:21:54 +02:00
progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
2020-08-30 11:54:41 +02:00
progCmd << this->ConvertToOutputFormat(progressDir,
cmOutputConverter::SHELL);
2020-02-01 23:06:01 +01:00
std::string progressFile = "/CMakeFiles/progress.marks";
2016-07-09 11:21:54 +02:00
std::string progressFileNameFull = this->ConvertToFullPath(progressFile);
progCmd << " "
2020-08-30 11:54:41 +02:00
<< this->ConvertToOutputFormat(progressFileNameFull,
cmOutputConverter::SHELL);
commands.push_back(progCmd.str());
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
std::string mf2Dir = "CMakeFiles/Makefile2";
2019-11-11 23:01:05 +01:00
commands.push_back(this->GetRecursiveMakeCall(mf2Dir, recursiveTarget));
2016-07-09 11:21:54 +02:00
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
2016-07-09 11:21:54 +02:00
{
2015-04-27 22:25:09 +02:00
std::ostringstream progCmd;
progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
2020-08-30 11:54:41 +02:00
progCmd << this->ConvertToOutputFormat(progressDir,
cmOutputConverter::SHELL);
progCmd << " 0";
commands.push_back(progCmd.str());
2016-07-09 11:21:54 +02:00
}
this->WriteMakeRule(ruleFileStream, "The main all target", "all", depends,
commands, true);
// Write the clean rule.
2020-02-01 23:06:01 +01:00
recursiveTarget = cmStrCat(this->GetCurrentBinaryDirectory(), "/clean");
commands.clear();
depends.clear();
2019-11-11 23:01:05 +01:00
commands.push_back(this->GetRecursiveMakeCall(mf2Dir, recursiveTarget));
2016-07-09 11:21:54 +02:00
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
depends, commands, true);
commands.clear();
depends.clear();
2019-11-11 23:01:05 +01:00
depends.emplace_back("clean");
this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
depends, commands, true);
// Write the preinstall rule.
2020-02-01 23:06:01 +01:00
recursiveTarget = cmStrCat(this->GetCurrentBinaryDirectory(), "/preinstall");
commands.clear();
depends.clear();
2021-11-20 13:41:27 +01:00
cmValue noall =
this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
2021-09-14 00:13:48 +02:00
if (cmIsOff(noall)) {
// Drive the build before installing.
2019-11-11 23:01:05 +01:00
depends.emplace_back("all");
2018-08-09 18:06:22 +02:00
} else if (regenerate) {
// At least make sure the build system is up to date.
2019-11-11 23:01:05 +01:00
depends.emplace_back("cmake_check_build_system");
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
commands.push_back(this->GetRecursiveMakeCall(mf2Dir, recursiveTarget));
2016-07-09 11:21:54 +02:00
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
2016-10-30 18:24:19 +01:00
this->GetCurrentBinaryDirectory());
this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
"preinstall", depends, commands, true);
depends.clear();
this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
"preinstall/fast", depends, commands, true);
2018-08-09 18:06:22 +02:00
if (regenerate) {
// write the depend rule, really a recompute depends rule
depends.clear();
commands.clear();
cmake* cm = this->GlobalGenerator->GetCMakeInstance();
if (cm->DoWriteGlobVerifyTarget()) {
2020-02-01 23:06:01 +01:00
std::string rescanRule =
cmStrCat("$(CMAKE_COMMAND) -P ",
this->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
cmOutputConverter::SHELL));
2018-08-09 18:06:22 +02:00
commands.push_back(rescanRule);
}
2020-02-01 23:06:01 +01:00
std::string cmakefileName = "CMakeFiles/Makefile.cmake";
2018-08-09 18:06:22 +02:00
{
2020-02-01 23:06:01 +01:00
std::string runRule = cmStrCat(
2023-07-02 19:51:09 +02:00
"$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) ",
cm->GetIgnoreWarningAsError() ? "--compile-no-warning-as-error " : "",
2020-02-01 23:06:01 +01:00
"--check-build-system ",
this->ConvertToOutputFormat(cmakefileName, cmOutputConverter::SHELL),
" 1");
2018-08-09 18:06:22 +02:00
commands.push_back(std::move(runRule));
}
this->CreateCDCommand(commands, this->GetBinaryDirectory(),
this->GetCurrentBinaryDirectory());
this->WriteMakeRule(ruleFileStream, "clear depends", "depend", depends,
commands, true);
2018-04-23 21:13:27 +02:00
}
}
void cmLocalUnixMakefileGenerator3::ClearDependencies(cmMakefile* mf,
bool verbose)
{
// Get the list of target files to check
2021-11-20 13:41:27 +01:00
cmValue infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
2016-07-09 11:21:54 +02:00
if (!infoDef) {
return;
2016-07-09 11:21:54 +02:00
}
2023-07-02 19:51:09 +02:00
cmList files{ *infoDef };
// Each depend information file corresponds to a target. Clear the
// dependencies for that target.
cmDepends clearer;
clearer.SetVerbose(verbose);
2018-01-26 17:06:56 +01:00
for (std::string const& file : files) {
2021-09-14 00:13:48 +02:00
auto snapshot = mf->GetState()->CreateBaseSnapshot();
cmMakefile lmf(mf->GetGlobalGenerator(), snapshot);
lmf.ReadListFile(file);
2021-09-14 00:13:48 +02:00
if (!lmf.GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES").empty()) {
std::string dir = cmSystemTools::GetFilenamePath(file);
2021-09-14 00:13:48 +02:00
// Clear the implicit dependency makefile.
std::string dependFile = dir + "/depend.make";
clearer.Clear(dependFile);
2021-09-14 00:13:48 +02:00
// Remove the internal dependency check file to force
// regeneration.
std::string internalDependFile = dir + "/depend.internal";
cmSystemTools::RemoveFile(internalDependFile);
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
auto depsFiles = lmf.GetSafeDefinition("CMAKE_DEPENDS_DEPENDENCY_FILES");
if (!depsFiles.empty()) {
auto dir = cmCMakePath(file).GetParentPath();
// Clear the implicit dependency makefile.
auto depFile = cmCMakePath(dir).Append("compiler_depend.make");
clearer.Clear(depFile.GenericString());
// Remove the internal dependency check file
auto internalDepFile =
cmCMakePath(dir).Append("compiler_depend.internal");
cmSystemTools::RemoveFile(internalDepFile.GenericString());
// Touch timestamp file to force dependencies regeneration
auto DepTimestamp = cmCMakePath(dir).Append("compiler_depend.ts");
cmSystemTools::Touch(DepTimestamp.GenericString(), true);
// clear the dependencies files generated by the compiler
2023-07-02 19:51:09 +02:00
cmList dependencies{ depsFiles, cmList::EmptyElements::Yes };
2021-09-14 00:13:48 +02:00
cmDependsCompiler depsManager;
depsManager.SetVerbose(verbose);
depsManager.ClearDependencies(dependencies);
2016-07-09 11:21:54 +02:00
}
}
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteDependLanguageInfo(
std::ostream& cmakefileStream, cmGeneratorTarget* target)
{
2021-09-14 00:13:48 +02:00
// To enable dependencies filtering
cmakefileStream << "\n"
<< "# Consider dependencies only in project.\n"
<< "set(CMAKE_DEPENDS_IN_PROJECT_ONLY "
<< (cmIsOn(this->Makefile->GetSafeDefinition(
"CMAKE_DEPENDS_IN_PROJECT_ONLY"))
? "ON"
: "OFF")
<< ")\n\n";
auto const& implicitLangs =
this->GetImplicitDepends(target, cmDependencyScannerKind::CMake);
// list the languages
2021-09-14 00:13:48 +02:00
cmakefileStream << "# The set of languages for which implicit "
"dependencies are needed:\n";
2016-07-09 11:21:54 +02:00
cmakefileStream << "set(CMAKE_DEPENDS_LANGUAGES\n";
2018-01-26 17:06:56 +01:00
for (auto const& implicitLang : implicitLangs) {
cmakefileStream << " \"" << implicitLang.first << "\"\n";
2016-07-09 11:21:54 +02:00
}
cmakefileStream << " )\n";
2021-09-14 00:13:48 +02:00
if (!implicitLangs.empty()) {
// now list the files for each language
cmakefileStream
<< "# The set of files for implicit dependencies of each language:\n";
for (auto const& implicitLang : implicitLangs) {
const auto& lang = implicitLang.first;
cmakefileStream << "set(CMAKE_DEPENDS_CHECK_" << lang << "\n";
auto const& implicitPairs = implicitLang.second;
// for each file pair
for (auto const& implicitPair : implicitPairs) {
for (auto const& di : implicitPair.second) {
cmakefileStream << " \"" << di << "\" ";
cmakefileStream << "\"" << implicitPair.first << "\"\n";
}
}
2021-09-14 00:13:48 +02:00
cmakefileStream << " )\n";
2021-09-14 00:13:48 +02:00
// Tell the dependency scanner what compiler is used.
std::string cidVar = cmStrCat("CMAKE_", lang, "_COMPILER_ID");
2021-11-20 13:41:27 +01:00
cmValue cid = this->Makefile->GetDefinition(cidVar);
2021-09-14 00:13:48 +02:00
if (cmNonempty(cid)) {
cmakefileStream << "set(CMAKE_" << lang << "_COMPILER_ID \"" << *cid
<< "\")\n";
}
if (lang == "Fortran") {
std::string smodSep =
this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_SEP");
std::string smodExt =
this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
cmakefileStream << "set(CMAKE_Fortran_SUBMODULE_SEP \"" << smodSep
<< "\")\n";
cmakefileStream << "set(CMAKE_Fortran_SUBMODULE_EXT \"" << smodExt
<< "\")\n";
}
2019-11-11 23:01:05 +01:00
2021-09-14 00:13:48 +02:00
// Build a list of preprocessor definitions for the target.
std::set<std::string> defines;
this->GetTargetDefines(target, this->GetConfigName(), lang, defines);
if (!defines.empty()) {
/* clang-format off */
2015-08-17 11:37:30 +02:00
cmakefileStream
<< "\n"
<< "# Preprocessor definitions for this target.\n"
2021-09-14 00:13:48 +02:00
<< "set(CMAKE_TARGET_DEFINITIONS_" << lang << "\n";
/* clang-format on */
for (std::string const& define : defines) {
cmakefileStream << " " << cmOutputConverter::EscapeForCMake(define)
<< "\n";
}
cmakefileStream << " )\n";
}
// Target-specific include directories:
cmakefileStream << "\n"
<< "# The include file search paths:\n";
cmakefileStream << "set(CMAKE_" << lang << "_TARGET_INCLUDE_PATH\n";
std::vector<std::string> includes;
this->GetIncludeDirectories(includes, target, lang,
this->GetConfigName());
std::string const& binaryDir = this->GetState()->GetBinaryDirectory();
if (this->Makefile->IsOn("CMAKE_DEPENDS_IN_PROJECT_ONLY")) {
std::string const& sourceDir = this->GetState()->GetSourceDirectory();
cm::erase_if(includes, ::NotInProjectDir(sourceDir, binaryDir));
}
for (std::string const& include : includes) {
cmakefileStream << " \"" << this->MaybeRelativeToTopBinDir(include)
<< "\"\n";
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
cmakefileStream << " )\n";
}
2015-08-17 11:37:30 +02:00
2021-09-14 00:13:48 +02:00
// Store include transform rule properties. Write the directory
// rules first because they may be overridden by later target rules.
2023-07-02 19:51:09 +02:00
cmList transformRules;
2021-11-20 13:41:27 +01:00
if (cmValue xform =
2021-09-14 00:13:48 +02:00
this->Makefile->GetProperty("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM")) {
2023-07-02 19:51:09 +02:00
transformRules.assign(*xform);
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
if (cmValue xform =
2021-09-14 00:13:48 +02:00
target->GetProperty("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM")) {
2023-07-02 19:51:09 +02:00
transformRules.append(*xform);
2021-09-14 00:13:48 +02:00
}
if (!transformRules.empty()) {
cmakefileStream << "\nset(CMAKE_INCLUDE_TRANSFORMS\n";
for (std::string const& tr : transformRules) {
cmakefileStream << " " << cmOutputConverter::EscapeForCMake(tr)
<< "\n";
}
cmakefileStream << " )\n";
}
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
auto const& compilerLangs =
this->GetImplicitDepends(target, cmDependencyScannerKind::Compiler);
// list the dependency files managed by the compiler
cmakefileStream << "\n# The set of dependency files which are needed:\n";
cmakefileStream << "set(CMAKE_DEPENDS_DEPENDENCY_FILES\n";
for (auto const& compilerLang : compilerLangs) {
auto const& compilerPairs = compilerLang.second;
if (compilerLang.first == "CUSTOM"_s) {
for (auto const& compilerPair : compilerPairs) {
for (auto const& src : compilerPair.second) {
cmakefileStream << R"( "" ")"
<< this->MaybeRelativeToTopBinDir(compilerPair.first)
<< R"(" "custom" ")"
<< this->MaybeRelativeToTopBinDir(src) << "\"\n";
}
}
2023-07-02 19:51:09 +02:00
} else if (compilerLang.first == "LINK"_s) {
auto depFormat = this->Makefile->GetDefinition(
cmStrCat("CMAKE_", target->GetLinkerLanguage(this->GetConfigName()),
"_LINKER_DEPFILE_FORMAT"));
for (auto const& compilerPair : compilerPairs) {
for (auto const& src : compilerPair.second) {
cmakefileStream << R"( "" ")"
<< this->MaybeRelativeToTopBinDir(compilerPair.first)
<< "\" \"" << depFormat << "\" \""
<< this->MaybeRelativeToTopBinDir(src) << "\"\n";
}
}
2021-09-14 00:13:48 +02:00
} else {
auto depFormat = this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", compilerLang.first, "_DEPFILE_FORMAT"));
for (auto const& compilerPair : compilerPairs) {
for (auto const& src : compilerPair.second) {
cmakefileStream << " \"" << src << "\" \""
<< this->MaybeRelativeToTopBinDir(compilerPair.first)
<< "\" \"" << depFormat << "\" \""
<< this->MaybeRelativeToTopBinDir(compilerPair.first)
<< ".d\"\n";
}
}
}
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
cmakefileStream << " )\n";
}
void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
{
2016-07-09 11:21:54 +02:00
os << "# CMAKE generated file: DO NOT EDIT!\n"
<< "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
<< " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
<< cmVersion::GetMinorVersion() << "\n\n";
}
2016-07-09 11:21:54 +02:00
std::string cmLocalUnixMakefileGenerator3::GetRecursiveMakeCall(
2019-11-11 23:01:05 +01:00
const std::string& makefile, const std::string& tgt)
{
// Call make on the given file.
2020-02-01 23:06:01 +01:00
std::string cmd = cmStrCat(
2020-08-30 11:54:41 +02:00
"$(MAKE) $(MAKESILENT) -f ",
2020-02-01 23:06:01 +01:00
this->ConvertToOutputFormat(makefile, cmOutputConverter::SHELL), ' ');
2011-01-16 11:35:12 +01:00
2015-08-17 11:37:30 +02:00
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
2009-05-01 17:43:35 +03:00
// Pass down verbosity level.
2016-07-09 11:21:54 +02:00
if (!gg->MakeSilentFlag.empty()) {
2015-08-17 11:37:30 +02:00
cmd += gg->MakeSilentFlag;
cmd += " ";
2016-07-09 11:21:54 +02:00
}
// Most unix makes will pass the command line flags to make down to
// sub-invoked makes via an environment variable. However, some
// makes do not support that, so you have to pass the flags
// explicitly.
2016-07-09 11:21:54 +02:00
if (gg->PassMakeflags) {
cmd += "-$(MAKEFLAGS) ";
2016-07-09 11:21:54 +02:00
}
// Add the target.
2016-07-09 11:21:54 +02:00
if (!tgt.empty()) {
// The make target is always relative to the top of the build tree.
2021-09-14 00:13:48 +02:00
std::string tgt2 = this->MaybeRelativeToTopBinDir(tgt);
// The target may have been written with windows paths.
cmSystemTools::ConvertToOutputSlashes(tgt2);
// Escape one extra time if the make tool requires it.
2016-07-09 11:21:54 +02:00
if (this->MakeCommandEscapeTargetTwice) {
2015-04-27 22:25:09 +02:00
tgt2 = this->EscapeForShell(tgt2, true, false);
2016-07-09 11:21:54 +02:00
}
// The target name is now a string that should be passed verbatim
// on the command line.
2015-04-27 22:25:09 +02:00
cmd += this->EscapeForShell(tgt2, true, false);
2016-07-09 11:21:54 +02:00
}
return cmd;
}
void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
{
2016-07-09 11:21:54 +02:00
os << "#======================================"
2020-08-30 11:54:41 +02:00
"=======================================\n";
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::WriteCMakeArgument(std::ostream& os,
2020-08-30 11:54:41 +02:00
const std::string& s)
{
// Write the given string to the stream with escaping to get it back
// into CMake through the lexical scanner.
2020-08-30 11:54:41 +02:00
os << '"';
for (char c : s) {
if (c == '\\') {
os << "\\\\";
2020-08-30 11:54:41 +02:00
} else if (c == '"') {
os << "\\\"";
2016-07-09 11:21:54 +02:00
} else {
2020-08-30 11:54:41 +02:00
os << c;
}
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
os << '"';
}
2016-07-09 11:21:54 +02:00
std::string cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
2019-11-11 23:01:05 +01:00
const std::string& p, bool useWatcomQuote)
{
// Split the path into its components.
std::vector<std::string> components;
cmSystemTools::SplitPath(p, components);
2015-04-27 22:25:09 +02:00
// Open the quoted result.
std::string result;
2016-07-09 11:21:54 +02:00
if (useWatcomQuote) {
2015-04-27 22:25:09 +02:00
#if defined(_WIN32) && !defined(__CYGWIN__)
result = "'";
#else
result = "\"'";
#endif
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
result = "\"";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Return an empty path if there are no components.
2016-07-09 11:21:54 +02:00
if (!components.empty()) {
2015-04-27 22:25:09 +02:00
// Choose a slash direction and fix root component.
const char* slash = "/";
#if defined(_WIN32) && !defined(__CYGWIN__)
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::GetForceUnixPaths()) {
2015-04-27 22:25:09 +02:00
slash = "\\";
2018-01-26 17:06:56 +01:00
for (char& i : components[0]) {
if (i == '/') {
i = '\\';
2015-04-27 22:25:09 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
#endif
2015-04-27 22:25:09 +02:00
// Begin the quoted result with the root component.
result += components[0];
2016-07-09 11:21:54 +02:00
if (components.size() > 1) {
2015-08-17 11:37:30 +02:00
// Now add the rest of the components separated by the proper slash
// direction for this platform.
2020-02-01 23:06:01 +01:00
auto compEnd = std::remove(components.begin() + 1, components.end() - 1,
std::string());
auto compStart = components.begin() + 1;
2015-11-17 17:22:37 +01:00
result += cmJoin(cmMakeRange(compStart, compEnd), slash);
2015-04-27 22:25:09 +02:00
// Only the last component can be empty to avoid double slashes.
2015-08-17 11:37:30 +02:00
result += slash;
result += components.back();
}
2016-07-09 11:21:54 +02:00
}
// Close the quoted result.
2016-07-09 11:21:54 +02:00
if (useWatcomQuote) {
2015-04-27 22:25:09 +02:00
#if defined(_WIN32) && !defined(__CYGWIN__)
result += "'";
#else
result += "'\"";
#endif
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
result += "\"";
2016-07-09 11:21:54 +02:00
}
return result;
}
2016-07-09 11:21:54 +02:00
std::string cmLocalUnixMakefileGenerator3::GetTargetDirectory(
cmGeneratorTarget const* target) const
{
2020-02-01 23:06:01 +01:00
std::string dir = cmStrCat("CMakeFiles/", target->GetName());
2009-10-04 10:30:41 +03:00
#if defined(__VMS)
dir += "_dir";
#else
dir += ".dir";
2009-10-04 10:30:41 +03:00
#endif
return dir;
}
cmLocalUnixMakefileGenerator3::ImplicitDependLanguageMap const&
2021-09-14 00:13:48 +02:00
cmLocalUnixMakefileGenerator3::GetImplicitDepends(
const cmGeneratorTarget* tgt, cmDependencyScannerKind scanner)
{
2021-09-14 00:13:48 +02:00
return this->ImplicitDepends[tgt->GetName()][scanner];
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::AddImplicitDepends(
2019-11-11 23:01:05 +01:00
const cmGeneratorTarget* tgt, const std::string& lang,
2021-09-14 00:13:48 +02:00
const std::string& obj, const std::string& src,
cmDependencyScannerKind scanner)
{
2021-09-14 00:13:48 +02:00
this->ImplicitDepends[tgt->GetName()][scanner][lang][obj].push_back(src);
}
2016-07-09 11:21:54 +02:00
void cmLocalUnixMakefileGenerator3::CreateCDCommand(
2018-04-23 21:13:27 +02:00
std::vector<std::string>& commands, std::string const& tgtDir,
2016-10-30 18:24:19 +01:00
std::string const& relDir)
{
// do we need to cd?
2016-10-30 18:24:19 +01:00
if (tgtDir == relDir) {
return;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2011-06-19 15:41:06 +03:00
// In a Windows shell we must change drive letter too. The shell
// used by NMake and Borland make does not support "cd /d" so this
// feature simply cannot work with them (Borland make does not even
// support changing the drive letter with just "d:").
2015-08-17 11:37:30 +02:00
const char* cd_cmd = this->IsMinGWMake() ? "cd /d " : "cd ";
2011-06-19 15:41:06 +03:00
2015-08-17 11:37:30 +02:00
cmGlobalUnixMakefileGenerator3* gg =
static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
2016-07-09 11:21:54 +02:00
if (!gg->UnixCD) {
// On Windows we must perform each step separately and then change
// back because the shell keeps the working directory between
// commands.
2020-02-01 23:06:01 +01:00
std::string cmd =
cmStrCat(cd_cmd, this->ConvertToOutputForExisting(tgtDir));
2016-07-09 11:21:54 +02:00
commands.insert(commands.begin(), cmd);
// Change back to the starting directory.
2020-02-01 23:06:01 +01:00
cmd = cmStrCat(cd_cmd, this->ConvertToOutputForExisting(relDir));
2018-04-23 21:13:27 +02:00
commands.push_back(std::move(cmd));
2016-07-09 11:21:54 +02:00
} else {
// On UNIX we must construct a single shell command to change
// directory and build because make resets the directory between
// each command.
2016-10-30 18:24:19 +01:00
std::string outputForExisting = this->ConvertToOutputForExisting(tgtDir);
2015-08-17 11:37:30 +02:00
std::string prefix = cd_cmd + outputForExisting + " && ";
std::transform(commands.begin(), commands.end(), commands.begin(),
2018-01-26 17:06:56 +01:00
[&prefix](std::string const& s) { return prefix + s; });
2016-07-09 11:21:54 +02:00
}
}