cmIncludeCommand.cxx 5.6 KB

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