cmFindPathCommand.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "cmFindPathCommand.h"
  4. #include <utility>
  5. #include "cmsys/Glob.hxx"
  6. #include "cmStateTypes.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. class cmExecutionStatus;
  10. cmFindPathCommand::cmFindPathCommand(std::string findCommandName,
  11. cmExecutionStatus& status)
  12. : cmFindBase(std::move(findCommandName), status)
  13. {
  14. this->EnvironmentPath = "INCLUDE";
  15. this->IncludeFileInPath = false;
  16. this->VariableDocumentation = "Path to a file.";
  17. this->VariableType = cmStateEnums::PATH;
  18. }
  19. cmFindPathCommand::cmFindPathCommand(cmExecutionStatus& status)
  20. : cmFindPathCommand("find_path", status)
  21. {
  22. }
  23. // cmFindPathCommand
  24. bool cmFindPathCommand::InitialPass(std::vector<std::string> const& argsIn)
  25. {
  26. this->DebugMode = this->ComputeIfDebugModeWanted();
  27. this->CMakePathName = "INCLUDE";
  28. if (!this->ParseArguments(argsIn)) {
  29. return false;
  30. }
  31. if (this->AlreadyInCache) {
  32. this->NormalizeFindResult();
  33. return true;
  34. }
  35. std::string result = this->FindHeader();
  36. this->StoreFindResult(result);
  37. return true;
  38. }
  39. std::string cmFindPathCommand::FindHeader()
  40. {
  41. cmFindBaseDebugState debug(this->FindCommandName, this);
  42. std::string header;
  43. if (this->SearchFrameworkFirst || this->SearchFrameworkOnly) {
  44. header = this->FindFrameworkHeader(debug);
  45. }
  46. if (header.empty() && !this->SearchFrameworkOnly) {
  47. header = this->FindNormalHeader(debug);
  48. }
  49. if (header.empty() && this->SearchFrameworkLast) {
  50. header = this->FindFrameworkHeader(debug);
  51. }
  52. return header;
  53. }
  54. std::string cmFindPathCommand::FindHeaderInFramework(
  55. std::string const& file, std::string const& dir) const
  56. {
  57. std::string fileName = file;
  58. std::string frameWorkName;
  59. std::string::size_type pos = fileName.find('/');
  60. // if there is a / in the name try to find the header as a framework
  61. // For example bar/foo.h would look for:
  62. // bar.framework/Headers/foo.h
  63. if (pos != std::string::npos) {
  64. // remove the name from the slash;
  65. fileName = fileName.substr(pos + 1);
  66. frameWorkName = file;
  67. frameWorkName =
  68. frameWorkName.substr(0, frameWorkName.size() - fileName.size() - 1);
  69. // if the framework has a path in it then just use the filename
  70. if (frameWorkName.find('/') != std::string::npos) {
  71. fileName = file;
  72. frameWorkName.clear();
  73. }
  74. if (!frameWorkName.empty()) {
  75. std::string fpath = cmStrCat(dir, frameWorkName, ".framework");
  76. std::string intPath = cmStrCat(fpath, "/Headers/", fileName);
  77. if (cmSystemTools::FileExists(intPath)) {
  78. if (this->IncludeFileInPath) {
  79. return intPath;
  80. }
  81. return fpath;
  82. }
  83. }
  84. }
  85. // if it is not found yet or not a framework header, then do a glob search
  86. // for all frameworks in the directory: dir/*.framework/Headers/<file>
  87. std::string glob = cmStrCat(dir, "*.framework/Headers/", file);
  88. cmsys::Glob globIt;
  89. globIt.FindFiles(glob);
  90. std::vector<std::string> files = globIt.GetFiles();
  91. if (!files.empty()) {
  92. std::string fheader = cmSystemTools::CollapseFullPath(files[0]);
  93. if (this->IncludeFileInPath) {
  94. return fheader;
  95. }
  96. fheader.resize(fheader.size() - file.size());
  97. return fheader;
  98. }
  99. return "";
  100. }
  101. std::string cmFindPathCommand::FindNormalHeader(cmFindBaseDebugState& debug)
  102. {
  103. std::string tryPath;
  104. for (std::string const& n : this->Names) {
  105. for (std::string const& sp : this->SearchPaths) {
  106. tryPath = cmStrCat(sp, n);
  107. if (cmSystemTools::FileExists(tryPath)) {
  108. debug.FoundAt(tryPath);
  109. if (this->IncludeFileInPath) {
  110. return tryPath;
  111. }
  112. return sp;
  113. }
  114. debug.FailedAt(tryPath);
  115. }
  116. }
  117. return "";
  118. }
  119. std::string cmFindPathCommand::FindFrameworkHeader(cmFindBaseDebugState& debug)
  120. {
  121. for (std::string const& n : this->Names) {
  122. for (std::string const& sp : this->SearchPaths) {
  123. std::string fwPath = this->FindHeaderInFramework(n, sp);
  124. fwPath.empty() ? debug.FailedAt(fwPath) : debug.FoundAt(fwPath);
  125. if (!fwPath.empty()) {
  126. return fwPath;
  127. }
  128. }
  129. }
  130. return "";
  131. }
  132. bool cmFindPath(std::vector<std::string> const& args,
  133. cmExecutionStatus& status)
  134. {
  135. return cmFindPathCommand(status).InitialPass(args);
  136. }