cmIncludeCommand.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmIncludeCommand.h"
  4. #include <sstream>
  5. #include "cmGlobalGenerator.h"
  6. #include "cmMakefile.h"
  7. #include "cmPolicies.h"
  8. #include "cmSystemTools.h"
  9. #include "cmake.h"
  10. class cmExecutionStatus;
  11. // cmIncludeCommand
  12. bool cmIncludeCommand::InitialPass(std::vector<std::string> const& args,
  13. cmExecutionStatus&)
  14. {
  15. if (args.empty() || args.size() > 4) {
  16. this->SetError("called with wrong number of arguments. "
  17. "include() only takes one file.");
  18. return false;
  19. }
  20. bool optional = false;
  21. bool noPolicyScope = false;
  22. std::string fname = args[0];
  23. std::string resultVarName;
  24. for (unsigned int i = 1; i < args.size(); i++) {
  25. if (args[i] == "OPTIONAL") {
  26. if (optional) {
  27. this->SetError("called with invalid arguments: OPTIONAL used twice");
  28. return false;
  29. }
  30. optional = true;
  31. } else if (args[i] == "RESULT_VARIABLE") {
  32. if (!resultVarName.empty()) {
  33. this->SetError("called with invalid arguments: "
  34. "only one result variable allowed");
  35. return false;
  36. }
  37. if (++i < args.size()) {
  38. resultVarName = args[i];
  39. } else {
  40. this->SetError("called with no value for RESULT_VARIABLE.");
  41. return false;
  42. }
  43. } else if (args[i] == "NO_POLICY_SCOPE") {
  44. noPolicyScope = true;
  45. } else if (i > 1) // compat.: in previous cmake versions the second
  46. // parameter was ignored if it wasn't "OPTIONAL"
  47. {
  48. std::string errorText = "called with invalid argument: ";
  49. errorText += args[i];
  50. this->SetError(errorText);
  51. return false;
  52. }
  53. }
  54. if (fname.empty()) {
  55. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
  56. "include() given empty file name (ignored).");
  57. return true;
  58. }
  59. if (!cmSystemTools::FileIsFullPath(fname)) {
  60. // Not a path. Maybe module.
  61. std::string module = fname;
  62. module += ".cmake";
  63. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  64. if (!mfile.empty()) {
  65. fname = mfile;
  66. }
  67. }
  68. std::string fname_abs = cmSystemTools::CollapseFullPath(
  69. fname, this->Makefile->GetCurrentSourceDirectory());
  70. cmGlobalGenerator* gg = this->Makefile->GetGlobalGenerator();
  71. if (gg->IsExportedTargetsFile(fname_abs)) {
  72. const char* modal = nullptr;
  73. std::ostringstream e;
  74. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  75. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0024)) {
  76. case cmPolicies::WARN:
  77. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0024) << "\n";
  78. modal = "should";
  79. case cmPolicies::OLD:
  80. break;
  81. case cmPolicies::REQUIRED_IF_USED:
  82. case cmPolicies::REQUIRED_ALWAYS:
  83. case cmPolicies::NEW:
  84. modal = "may";
  85. messageType = cmake::FATAL_ERROR;
  86. }
  87. if (modal) {
  88. e << "The file\n " << fname_abs << "\nwas generated by the export() "
  89. "command. It "
  90. << modal
  91. << " not be used as the argument to the "
  92. "include() command. Use ALIAS targets instead to refer to targets "
  93. "by alternative names.\n";
  94. this->Makefile->IssueMessage(messageType, e.str());
  95. if (messageType == cmake::FATAL_ERROR) {
  96. return false;
  97. }
  98. }
  99. gg->CreateGenerationObjects();
  100. gg->GenerateImportFile(fname_abs);
  101. }
  102. std::string listFile = cmSystemTools::CollapseFullPath(
  103. fname, this->Makefile->GetCurrentSourceDirectory());
  104. if (optional && !cmSystemTools::FileExists(listFile)) {
  105. if (!resultVarName.empty()) {
  106. this->Makefile->AddDefinition(resultVarName, "NOTFOUND");
  107. }
  108. return true;
  109. }
  110. bool readit =
  111. this->Makefile->ReadDependentFile(listFile.c_str(), noPolicyScope);
  112. // add the location of the included file if a result variable was given
  113. if (!resultVarName.empty()) {
  114. this->Makefile->AddDefinition(resultVarName,
  115. readit ? fname_abs.c_str() : "NOTFOUND");
  116. }
  117. if (!optional && !readit && !cmSystemTools::GetFatalErrorOccured()) {
  118. std::string m = "could not find load file:\n"
  119. " ";
  120. m += fname;
  121. this->SetError(m);
  122. return false;
  123. }
  124. return true;
  125. }