cmFindPathCommand.cxx 4.7 KB

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