cmIncludeCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmIncludeCommand.h"
  4. #include <map>
  5. #include <utility>
  6. #include "cmExecutionStatus.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmPolicies.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. // cmIncludeCommand
  14. bool cmIncludeCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. static std::map<std::string, cmPolicies::PolicyID> DeprecatedModules;
  18. if (DeprecatedModules.empty()) {
  19. DeprecatedModules["CMakeFindFrameworks"] = cmPolicies::CMP0173;
  20. DeprecatedModules["Dart"] = cmPolicies::CMP0145;
  21. DeprecatedModules["Documentation"] = cmPolicies::CMP0106;
  22. DeprecatedModules["FindBoost"] = cmPolicies::CMP0167;
  23. DeprecatedModules["FindCABLE"] = cmPolicies::CMP0191;
  24. DeprecatedModules["FindCUDA"] = cmPolicies::CMP0146;
  25. DeprecatedModules["FindDart"] = cmPolicies::CMP0145;
  26. DeprecatedModules["FindGCCXML"] = cmPolicies::CMP0188;
  27. DeprecatedModules["FindPythonInterp"] = cmPolicies::CMP0148;
  28. DeprecatedModules["FindPythonLibs"] = cmPolicies::CMP0148;
  29. DeprecatedModules["WriteCompilerDetectionHeader"] = cmPolicies::CMP0120;
  30. }
  31. if (args.empty() || args.size() > 4) {
  32. status.SetError("called with wrong number of arguments. "
  33. "include() only takes one file.");
  34. return false;
  35. }
  36. bool optional = false;
  37. bool noPolicyScope = false;
  38. std::string fname = args[0];
  39. std::string resultVarName;
  40. for (unsigned int i = 1; i < args.size(); i++) {
  41. if (args[i] == "OPTIONAL") {
  42. if (optional) {
  43. status.SetError("called with invalid arguments: OPTIONAL used twice");
  44. return false;
  45. }
  46. optional = true;
  47. } else if (args[i] == "RESULT_VARIABLE") {
  48. if (!resultVarName.empty()) {
  49. status.SetError("called with invalid arguments: "
  50. "only one result variable allowed");
  51. return false;
  52. }
  53. if (++i < args.size()) {
  54. resultVarName = args[i];
  55. } else {
  56. status.SetError("called with no value for RESULT_VARIABLE.");
  57. return false;
  58. }
  59. } else if (args[i] == "NO_POLICY_SCOPE") {
  60. noPolicyScope = true;
  61. } else if (i > 1) // compat.: in previous cmake versions the second
  62. // parameter was ignored if it wasn't "OPTIONAL"
  63. {
  64. std::string errorText =
  65. cmStrCat("called with invalid argument: ", args[i]);
  66. status.SetError(errorText);
  67. return false;
  68. }
  69. }
  70. if (fname.empty()) {
  71. status.GetMakefile().IssueMessage(
  72. MessageType::AUTHOR_WARNING,
  73. "include() given empty file name (ignored).");
  74. return true;
  75. }
  76. if (!cmSystemTools::FileIsFullPath(fname)) {
  77. bool system = false;
  78. // Not a path. Maybe module.
  79. std::string module = cmStrCat(fname, ".cmake");
  80. std::string mfile = status.GetMakefile().GetModulesFile(module, system);
  81. if (system) {
  82. auto ModulePolicy = DeprecatedModules.find(fname);
  83. if (ModulePolicy != DeprecatedModules.end()) {
  84. cmPolicies::PolicyStatus PolicyStatus =
  85. status.GetMakefile().GetPolicyStatus(ModulePolicy->second);
  86. switch (PolicyStatus) {
  87. case cmPolicies::WARN: {
  88. status.GetMakefile().IssueMessage(
  89. MessageType::AUTHOR_WARNING,
  90. cmStrCat(cmPolicies::GetPolicyWarning(ModulePolicy->second),
  91. '\n'));
  92. CM_FALLTHROUGH;
  93. }
  94. case cmPolicies::OLD:
  95. break;
  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. status.GetMakefile().IssueMessage(
  111. MessageType::FATAL_ERROR,
  112. cmStrCat(
  113. "The file\n ", fname_abs,
  114. "\nwas generated by the export() "
  115. "command. It may not be used as the argument to the "
  116. "include() command. Use ALIAS targets instead to refer to targets "
  117. "by alternative names.\n"));
  118. return false;
  119. }
  120. std::string listFile = cmSystemTools::CollapseFullPath(
  121. fname, status.GetMakefile().GetCurrentSourceDirectory());
  122. bool const fileDoesnotExist = !cmSystemTools::FileExists(listFile);
  123. bool const fileIsDirectory = cmSystemTools::FileIsDirectory(listFile);
  124. if (fileDoesnotExist || fileIsDirectory) {
  125. if (!resultVarName.empty()) {
  126. status.GetMakefile().AddDefinition(resultVarName, "NOTFOUND");
  127. }
  128. if (optional) {
  129. return true;
  130. }
  131. if (fileDoesnotExist) {
  132. status.SetError(cmStrCat("could not find requested file:\n ", fname));
  133. return false;
  134. }
  135. if (fileIsDirectory) {
  136. status.SetError(cmStrCat("requested file is a directory:\n ", fname));
  137. return false;
  138. }
  139. }
  140. bool readit =
  141. status.GetMakefile().ReadDependentFile(listFile, noPolicyScope);
  142. // add the location of the included file if a result variable was given
  143. if (!resultVarName.empty()) {
  144. status.GetMakefile().AddDefinition(
  145. resultVarName, readit ? fname_abs.c_str() : "NOTFOUND");
  146. }
  147. if (!optional && !readit && !cmSystemTools::GetFatalErrorOccurred()) {
  148. std::string m = cmStrCat("could not load requested file:\n ", fname);
  149. status.SetError(m);
  150. return false;
  151. }
  152. return true;
  153. }