cmIncludeCommand.cxx 5.4 KB

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