cmIncludeCommand.cxx 6.2 KB

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