cmIncludeCommand.cxx 4.3 KB

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