cmake/Source/cmIncludeCommand.cxx

141 lines
4.3 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"
2017-04-14 19:02:05 +02:00
#include <sstream>
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmPolicies.h"
#include "cmSystemTools.h"
#include "cmake.h"
class cmExecutionStatus;
// cmIncludeCommand
2016-07-09 11:21:54 +02:00
bool cmIncludeCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty() || args.size() > 4) {
2016-07-09 11:21:54 +02:00
this->SetError("called with wrong number of arguments. "
"include() only takes one file.");
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) {
this->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()) {
this->SetError("called with invalid arguments: "
2016-07-09 11:21:54 +02:00
"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 {
this->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
{
std::string errorText = "called with invalid argument: ";
errorText += args[i];
this->SetError(errorText);
return false;
}
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (fname.empty()) {
2012-08-04 10:26:08 +03:00
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
"include() given empty file name (ignored).");
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)) {
// Not a path. Maybe module.
std::string module = fname;
module += ".cmake";
std::string mfile = this->Makefile->GetModulesFile(module.c_str());
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(
fname, this->Makefile->GetCurrentSourceDirectory());
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
cmGlobalGenerator* gg = this->Makefile->GetGlobalGenerator();
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;
2014-08-03 19:52:23 +02:00
cmake::MessageType messageType = cmake::AUTHOR_WARNING;
2016-07-09 11:21:54 +02:00
switch (this->Makefile->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";
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
modal = "may";
messageType = cmake::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";
2015-04-27 22:25:09 +02:00
this->Makefile->IssueMessage(messageType, e.str());
2016-07-09 11:21:54 +02:00
if (messageType == cmake::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(
2017-04-14 19:02:05 +02:00
fname, this->Makefile->GetCurrentSourceDirectory());
2018-04-23 21:13:27 +02:00
if (optional && !cmSystemTools::FileExists(listFile)) {
2016-07-09 11:21:54 +02:00
if (!resultVarName.empty()) {
2015-08-17 11:37:30 +02:00
this->Makefile->AddDefinition(resultVarName, "NOTFOUND");
}
2016-07-09 11:21:54 +02:00
return true;
}
2015-08-17 11:37:30 +02:00
2011-01-16 11:35:12 +01:00
bool readit =
2015-08-17 11:37:30 +02:00
this->Makefile->ReadDependentFile(listFile.c_str(), 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()) {
this->Makefile->AddDefinition(resultVarName,
readit ? fname_abs.c_str() : "NOTFOUND");
}
if (!optional && !readit && !cmSystemTools::GetFatalErrorOccured()) {
std::string m = "could not find load file:\n"
" ";
m += fname;
2015-04-27 22:25:09 +02:00
this->SetError(m);
return false;
2016-07-09 11:21:54 +02:00
}
return true;
}