cmFindPathCommand.cxx 4.5 KB

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