cmIncludeCommand.cxx 6.0 KB

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