cmake/Source/cmIncludeCommand.cxx

192 lines
6.1 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 "cmIncludeCommand.h"
2020-08-30 11:54:41 +02:00
#include <map>
2017-04-14 19:02:05 +02:00
#include <sstream>
2020-08-30 11:54:41 +02:00
#include <utility>
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2017-04-14 19:02:05 +02:00
#include "cmPolicies.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
// cmIncludeCommand
2020-02-01 23:06:01 +01:00
bool cmIncludeCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2020-08-30 11:54:41 +02:00
static std::map<std::string, cmPolicies::PolicyID> DeprecatedModules;
if (DeprecatedModules.empty()) {
2023-07-02 19:51:09 +02:00
DeprecatedModules["Dart"] = cmPolicies::CMP0145;
2020-08-30 11:54:41 +02:00
DeprecatedModules["Documentation"] = cmPolicies::CMP0106;
2023-07-02 19:51:09 +02:00
DeprecatedModules["FindCUDA"] = cmPolicies::CMP0146;
DeprecatedModules["FindDart"] = cmPolicies::CMP0145;
DeprecatedModules["FindPythonInterp"] = cmPolicies::CMP0148;
DeprecatedModules["FindPythonLibs"] = cmPolicies::CMP0148;
2021-09-14 00:13:48 +02:00
DeprecatedModules["WriteCompilerDetectionHeader"] = cmPolicies::CMP0120;
2020-08-30 11:54:41 +02:00
}
2016-10-30 18:24:19 +01:00
if (args.empty() || args.size() > 4) {
2020-02-01 23:06:01 +01:00
status.SetError("called with wrong number of arguments. "
"include() only takes one file.");
2016-07-09 11:21:54 +02:00
return false;
}
bool optional = false;
bool noPolicyScope = false;
std::string fname = args[0];
std::string resultVarName;
2011-01-16 11:35:12 +01:00
2016-07-09 11:21:54 +02:00
for (unsigned int i = 1; i < args.size(); i++) {
if (args[i] == "OPTIONAL") {
if (optional) {
2020-02-01 23:06:01 +01:00
status.SetError("called with invalid arguments: OPTIONAL used twice");
return false;
}
2016-07-09 11:21:54 +02:00
optional = true;
} else if (args[i] == "RESULT_VARIABLE") {
if (!resultVarName.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("called with invalid arguments: "
"only one result variable allowed");
return false;
2016-07-09 11:21:54 +02:00
}
if (++i < args.size()) {
resultVarName = args[i];
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.SetError("called with no value for RESULT_VARIABLE.");
return false;
}
2016-07-09 11:21:54 +02:00
} else if (args[i] == "NO_POLICY_SCOPE") {
noPolicyScope = true;
2016-07-09 11:21:54 +02:00
} else if (i > 1) // compat.: in previous cmake versions the second
2013-11-03 12:27:13 +02:00
// parameter was ignored if it wasn't "OPTIONAL"
2016-07-09 11:21:54 +02:00
{
2020-02-01 23:06:01 +01:00
std::string errorText =
cmStrCat("called with invalid argument: ", args[i]);
status.SetError(errorText);
2016-07-09 11:21:54 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (fname.empty()) {
2020-02-01 23:06:01 +01:00
status.GetMakefile().IssueMessage(
MessageType::AUTHOR_WARNING,
"include() given empty file name (ignored).");
2012-08-04 10:26:08 +03:00
return true;
2016-07-09 11:21:54 +02:00
}
2012-08-04 10:26:08 +03:00
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileIsFullPath(fname)) {
2020-08-30 11:54:41 +02:00
bool system = false;
// Not a path. Maybe module.
2020-02-01 23:06:01 +01:00
std::string module = cmStrCat(fname, ".cmake");
2020-08-30 11:54:41 +02:00
std::string mfile = status.GetMakefile().GetModulesFile(module, system);
if (system) {
auto ModulePolicy = DeprecatedModules.find(fname);
if (ModulePolicy != DeprecatedModules.end()) {
cmPolicies::PolicyStatus PolicyStatus =
status.GetMakefile().GetPolicyStatus(ModulePolicy->second);
switch (PolicyStatus) {
case cmPolicies::WARN: {
status.GetMakefile().IssueMessage(
MessageType::AUTHOR_WARNING,
cmStrCat(cmPolicies::GetPolicyWarning(ModulePolicy->second),
"\n"));
CM_FALLTHROUGH;
}
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
mfile = "";
break;
}
}
}
2016-07-09 11:21:54 +02:00
if (!mfile.empty()) {
2017-04-14 19:02:05 +02:00
fname = mfile;
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
std::string fname_abs = cmSystemTools::CollapseFullPath(
2020-02-01 23:06:01 +01:00
fname, status.GetMakefile().GetCurrentSourceDirectory());
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator();
2016-07-09 11:21:54 +02:00
if (gg->IsExportedTargetsFile(fname_abs)) {
2018-01-26 17:06:56 +01:00
const char* modal = nullptr;
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2019-11-11 23:01:05 +01:00
MessageType messageType = MessageType::AUTHOR_WARNING;
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0024)) {
2014-08-03 19:52:23 +02:00
case cmPolicies::WARN:
2015-08-17 11:37:30 +02:00
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0024) << "\n";
2014-08-03 19:52:23 +02:00
modal = "should";
2021-11-20 13:41:27 +01:00
CM_FALLTHROUGH;
2014-08-03 19:52:23 +02:00
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
modal = "may";
2019-11-11 23:01:05 +01:00
messageType = MessageType::FATAL_ERROR;
2016-07-09 11:21:54 +02:00
}
if (modal) {
2018-08-09 18:06:22 +02:00
e << "The file\n " << fname_abs
<< "\nwas generated by the export() "
"command. It "
2016-07-09 11:21:54 +02:00
<< modal
<< " not be used as the argument to the "
"include() command. Use ALIAS targets instead to refer to targets "
"by alternative names.\n";
2020-02-01 23:06:01 +01:00
status.GetMakefile().IssueMessage(messageType, e.str());
2019-11-11 23:01:05 +01:00
if (messageType == MessageType::FATAL_ERROR) {
2014-08-03 19:52:23 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
gg->CreateGenerationObjects();
2014-08-03 19:52:23 +02:00
gg->GenerateImportFile(fname_abs);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
std::string listFile = cmSystemTools::CollapseFullPath(
2020-02-01 23:06:01 +01:00
fname, status.GetMakefile().GetCurrentSourceDirectory());
2021-09-14 00:13:48 +02:00
const bool fileDoesnotExist = !cmSystemTools::FileExists(listFile);
const bool fileIsDirectory = cmSystemTools::FileIsDirectory(listFile);
if (fileDoesnotExist || fileIsDirectory) {
2016-07-09 11:21:54 +02:00
if (!resultVarName.empty()) {
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(resultVarName, "NOTFOUND");
2015-08-17 11:37:30 +02:00
}
2021-09-14 00:13:48 +02:00
if (optional) {
return true;
}
if (fileDoesnotExist) {
status.SetError(cmStrCat("could not find requested file:\n ", fname));
return false;
}
if (fileIsDirectory) {
status.SetError(cmStrCat("requested file is a directory:\n ", fname));
return false;
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2020-02-01 23:06:01 +01:00
bool readit =
status.GetMakefile().ReadDependentFile(listFile, noPolicyScope);
2011-01-16 11:35:12 +01:00
// add the location of the included file if a result variable was given
2016-07-09 11:21:54 +02:00
if (!resultVarName.empty()) {
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(
resultVarName, readit ? fname_abs.c_str() : "NOTFOUND");
2016-07-09 11:21:54 +02:00
}
2022-08-04 22:12:04 +02:00
if (!optional && !readit && !cmSystemTools::GetFatalErrorOccurred()) {
2021-09-14 00:13:48 +02:00
std::string m = cmStrCat("could not load requested file:\n ", fname);
2020-02-01 23:06:01 +01:00
status.SetError(m);
return false;
2016-07-09 11:21:54 +02:00
}
return true;
}