cmIncludeCommand.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "cmExecutionStatus.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmMessageType.h"
  9. #include "cmPolicies.h"
  10. #include "cmSystemTools.h"
  11. // cmIncludeCommand
  12. bool cmIncludeCommand(std::vector<std::string> const& args,
  13. cmExecutionStatus& status)
  14. {
  15. if (args.empty() || args.size() > 4) {
  16. status.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. status.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. status.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. status.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. status.SetError(errorText);
  51. return false;
  52. }
  53. }
  54. if (fname.empty()) {
  55. status.GetMakefile().IssueMessage(
  56. MessageType::AUTHOR_WARNING,
  57. "include() given empty file name (ignored).");
  58. return true;
  59. }
  60. if (!cmSystemTools::FileIsFullPath(fname)) {
  61. // Not a path. Maybe module.
  62. std::string module = fname;
  63. module += ".cmake";
  64. std::string mfile = status.GetMakefile().GetModulesFile(module);
  65. if (!mfile.empty()) {
  66. fname = mfile;
  67. }
  68. }
  69. std::string fname_abs = cmSystemTools::CollapseFullPath(
  70. fname, status.GetMakefile().GetCurrentSourceDirectory());
  71. cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator();
  72. if (gg->IsExportedTargetsFile(fname_abs)) {
  73. const char* modal = nullptr;
  74. std::ostringstream e;
  75. MessageType messageType = MessageType::AUTHOR_WARNING;
  76. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0024)) {
  77. case cmPolicies::WARN:
  78. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0024) << "\n";
  79. modal = "should";
  80. case cmPolicies::OLD:
  81. break;
  82. case cmPolicies::REQUIRED_IF_USED:
  83. case cmPolicies::REQUIRED_ALWAYS:
  84. case cmPolicies::NEW:
  85. modal = "may";
  86. messageType = MessageType::FATAL_ERROR;
  87. }
  88. if (modal) {
  89. e << "The file\n " << fname_abs
  90. << "\nwas generated by the export() "
  91. "command. It "
  92. << modal
  93. << " not be used as the argument to the "
  94. "include() command. Use ALIAS targets instead to refer to targets "
  95. "by alternative names.\n";
  96. status.GetMakefile().IssueMessage(messageType, e.str());
  97. if (messageType == MessageType::FATAL_ERROR) {
  98. return false;
  99. }
  100. }
  101. gg->CreateGenerationObjects();
  102. gg->GenerateImportFile(fname_abs);
  103. }
  104. std::string listFile = cmSystemTools::CollapseFullPath(
  105. fname, status.GetMakefile().GetCurrentSourceDirectory());
  106. if (optional && !cmSystemTools::FileExists(listFile)) {
  107. if (!resultVarName.empty()) {
  108. status.GetMakefile().AddDefinition(resultVarName, "NOTFOUND");
  109. }
  110. return true;
  111. }
  112. bool readit =
  113. status.GetMakefile().ReadDependentFile(listFile, noPolicyScope);
  114. // add the location of the included file if a result variable was given
  115. if (!resultVarName.empty()) {
  116. status.GetMakefile().AddDefinition(
  117. resultVarName, readit ? fname_abs.c_str() : "NOTFOUND");
  118. }
  119. if (!optional && !readit && !cmSystemTools::GetFatalErrorOccured()) {
  120. std::string m = "could not find load file:\n"
  121. " ";
  122. m += fname;
  123. status.SetError(m);
  124. return false;
  125. }
  126. return true;
  127. }