cmFindPathCommand.cxx 4.5 KB

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