cmFindPathCommand.cxx 5.0 KB

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