cmIncludeCommand.cxx 6.1 KB

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