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