cmIncludeCommand.cxx 6.3 KB

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