cmIncludeCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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::NEW:
  96. mfile = "";
  97. break;
  98. }
  99. }
  100. }
  101. if (!mfile.empty()) {
  102. fname = mfile;
  103. }
  104. }
  105. std::string fname_abs = cmSystemTools::CollapseFullPath(
  106. fname, status.GetMakefile().GetCurrentSourceDirectory());
  107. cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator();
  108. if (gg->IsExportedTargetsFile(fname_abs)) {
  109. const char* modal = nullptr;
  110. std::ostringstream e;
  111. MessageType messageType = MessageType::AUTHOR_WARNING;
  112. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0024)) {
  113. case cmPolicies::WARN:
  114. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0024) << "\n";
  115. modal = "should";
  116. CM_FALLTHROUGH;
  117. case cmPolicies::OLD:
  118. break;
  119. case cmPolicies::NEW:
  120. modal = "may";
  121. messageType = MessageType::FATAL_ERROR;
  122. }
  123. if (modal) {
  124. e << "The file\n " << fname_abs
  125. << "\nwas generated by the export() "
  126. "command. It "
  127. << modal
  128. << " not be used as the argument to the "
  129. "include() command. Use ALIAS targets instead to refer to targets "
  130. "by alternative names.\n";
  131. status.GetMakefile().IssueMessage(messageType, e.str());
  132. if (messageType == MessageType::FATAL_ERROR) {
  133. return false;
  134. }
  135. }
  136. gg->CreateGenerationObjects();
  137. gg->GenerateImportFile(fname_abs);
  138. }
  139. std::string listFile = cmSystemTools::CollapseFullPath(
  140. fname, status.GetMakefile().GetCurrentSourceDirectory());
  141. const bool fileDoesnotExist = !cmSystemTools::FileExists(listFile);
  142. const bool fileIsDirectory = cmSystemTools::FileIsDirectory(listFile);
  143. if (fileDoesnotExist || fileIsDirectory) {
  144. if (!resultVarName.empty()) {
  145. status.GetMakefile().AddDefinition(resultVarName, "NOTFOUND");
  146. }
  147. if (optional) {
  148. return true;
  149. }
  150. if (fileDoesnotExist) {
  151. status.SetError(cmStrCat("could not find requested file:\n ", fname));
  152. return false;
  153. }
  154. if (fileIsDirectory) {
  155. status.SetError(cmStrCat("requested file is a directory:\n ", fname));
  156. return false;
  157. }
  158. }
  159. bool readit =
  160. status.GetMakefile().ReadDependentFile(listFile, noPolicyScope);
  161. // add the location of the included file if a result variable was given
  162. if (!resultVarName.empty()) {
  163. status.GetMakefile().AddDefinition(
  164. resultVarName, readit ? fname_abs.c_str() : "NOTFOUND");
  165. }
  166. if (!optional && !readit && !cmSystemTools::GetFatalErrorOccurred()) {
  167. std::string m = cmStrCat("could not load requested file:\n ", fname);
  168. status.SetError(m);
  169. return false;
  170. }
  171. return true;
  172. }