cmIncludeCommand.cxx 5.8 KB

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