cmIncludeCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <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["FindCUDA"] = cmPolicies::CMP0146;
  24. DeprecatedModules["FindDart"] = cmPolicies::CMP0145;
  25. DeprecatedModules["FindGCCXML"] = cmPolicies::CMP0188;
  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. status.GetMakefile().IssueMessage(
  110. MessageType::FATAL_ERROR,
  111. cmStrCat(
  112. "The file\n ", fname_abs,
  113. "\nwas generated by the export() "
  114. "command. It may not be used as the argument to the "
  115. "include() command. Use ALIAS targets instead to refer to targets "
  116. "by alternative names.\n"));
  117. return false;
  118. }
  119. std::string listFile = cmSystemTools::CollapseFullPath(
  120. fname, status.GetMakefile().GetCurrentSourceDirectory());
  121. bool const fileDoesnotExist = !cmSystemTools::FileExists(listFile);
  122. bool const fileIsDirectory = cmSystemTools::FileIsDirectory(listFile);
  123. if (fileDoesnotExist || fileIsDirectory) {
  124. if (!resultVarName.empty()) {
  125. status.GetMakefile().AddDefinition(resultVarName, "NOTFOUND");
  126. }
  127. if (optional) {
  128. return true;
  129. }
  130. if (fileDoesnotExist) {
  131. status.SetError(cmStrCat("could not find requested file:\n ", fname));
  132. return false;
  133. }
  134. if (fileIsDirectory) {
  135. status.SetError(cmStrCat("requested file is a directory:\n ", fname));
  136. return false;
  137. }
  138. }
  139. bool readit =
  140. status.GetMakefile().ReadDependentFile(listFile, noPolicyScope);
  141. // add the location of the included file if a result variable was given
  142. if (!resultVarName.empty()) {
  143. status.GetMakefile().AddDefinition(
  144. resultVarName, readit ? fname_abs.c_str() : "NOTFOUND");
  145. }
  146. if (!optional && !readit && !cmSystemTools::GetFatalErrorOccurred()) {
  147. std::string m = cmStrCat("could not load requested file:\n ", fname);
  148. status.SetError(m);
  149. return false;
  150. }
  151. return true;
  152. }