cmFindPathCommand.cxx 4.8 KB

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