cmSearchPath.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "cmSearchPath.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmFindCommon.h"
  6. #include "cmSystemTools.h"
  7. cmSearchPath::cmSearchPath(cmFindCommon* findCmd)
  8. : FC(findCmd)
  9. {
  10. }
  11. cmSearchPath::~cmSearchPath()
  12. {
  13. }
  14. void cmSearchPath::ExtractWithout(const std::set<std::string>& ignore,
  15. std::vector<std::string>& outPaths,
  16. bool clear) const
  17. {
  18. if (clear) {
  19. outPaths.clear();
  20. }
  21. for (std::vector<std::string>::const_iterator p = this->Paths.begin();
  22. p != this->Paths.end(); ++p) {
  23. if (ignore.count(*p) == 0) {
  24. outPaths.push_back(*p);
  25. }
  26. }
  27. }
  28. void cmSearchPath::AddPath(const std::string& path)
  29. {
  30. this->AddPathInternal(path);
  31. }
  32. void cmSearchPath::AddUserPath(const std::string& path)
  33. {
  34. assert(this->FC != CM_NULLPTR);
  35. std::vector<std::string> outPaths;
  36. // We should view the registry as the target application would view
  37. // it.
  38. cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
  39. cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
  40. if (this->FC->Makefile->PlatformIs64Bit()) {
  41. view = cmSystemTools::KeyWOW64_64;
  42. other_view = cmSystemTools::KeyWOW64_32;
  43. }
  44. // Expand using the view of the target application.
  45. std::string expanded = path;
  46. cmSystemTools::ExpandRegistryValues(expanded, view);
  47. cmSystemTools::GlobDirs(expanded, outPaths);
  48. // Executables can be either 32-bit or 64-bit, so expand using the
  49. // alternative view.
  50. if (expanded != path && this->FC->CMakePathName == "PROGRAM") {
  51. expanded = path;
  52. cmSystemTools::ExpandRegistryValues(expanded, other_view);
  53. cmSystemTools::GlobDirs(expanded, outPaths);
  54. }
  55. // Process them all from the current directory
  56. for (std::vector<std::string>::const_iterator p = outPaths.begin();
  57. p != outPaths.end(); ++p) {
  58. this->AddPathInternal(*p, this->FC->Makefile->GetCurrentSourceDirectory());
  59. }
  60. }
  61. void cmSearchPath::AddCMakePath(const std::string& variable)
  62. {
  63. assert(this->FC != CM_NULLPTR);
  64. // Get a path from a CMake variable.
  65. if (const char* value = this->FC->Makefile->GetDefinition(variable)) {
  66. std::vector<std::string> expanded;
  67. cmSystemTools::ExpandListArgument(value, expanded);
  68. for (std::vector<std::string>::const_iterator p = expanded.begin();
  69. p != expanded.end(); ++p) {
  70. this->AddPathInternal(*p,
  71. this->FC->Makefile->GetCurrentSourceDirectory());
  72. }
  73. }
  74. }
  75. void cmSearchPath::AddEnvPath(const std::string& variable)
  76. {
  77. std::vector<std::string> expanded;
  78. cmSystemTools::GetPath(expanded, variable.c_str());
  79. for (std::vector<std::string>::const_iterator p = expanded.begin();
  80. p != expanded.end(); ++p) {
  81. this->AddPathInternal(*p);
  82. }
  83. }
  84. void cmSearchPath::AddCMakePrefixPath(const std::string& variable)
  85. {
  86. assert(this->FC != CM_NULLPTR);
  87. // Get a path from a CMake variable.
  88. if (const char* value = this->FC->Makefile->GetDefinition(variable)) {
  89. std::vector<std::string> expanded;
  90. cmSystemTools::ExpandListArgument(value, expanded);
  91. this->AddPrefixPaths(expanded,
  92. this->FC->Makefile->GetCurrentSourceDirectory());
  93. }
  94. }
  95. static std::string cmSearchPathStripBin(std::string const& s)
  96. {
  97. // If the path is a PREFIX/bin case then add its parent instead.
  98. if ((cmHasLiteralSuffix(s, "/bin")) || (cmHasLiteralSuffix(s, "/sbin"))) {
  99. return cmSystemTools::GetFilenamePath(s);
  100. }
  101. return s;
  102. }
  103. void cmSearchPath::AddEnvPrefixPath(const std::string& variable, bool stripBin)
  104. {
  105. std::vector<std::string> expanded;
  106. cmSystemTools::GetPath(expanded, variable.c_str());
  107. if (stripBin) {
  108. std::transform(expanded.begin(), expanded.end(), expanded.begin(),
  109. cmSearchPathStripBin);
  110. }
  111. this->AddPrefixPaths(expanded);
  112. }
  113. void cmSearchPath::AddSuffixes(const std::vector<std::string>& suffixes)
  114. {
  115. std::vector<std::string> inPaths;
  116. inPaths.swap(this->Paths);
  117. this->Paths.reserve(inPaths.size() * (suffixes.size() + 1));
  118. for (std::vector<std::string>::iterator ip = inPaths.begin();
  119. ip != inPaths.end(); ++ip) {
  120. cmSystemTools::ConvertToUnixSlashes(*ip);
  121. // if *i is only / then do not add a //
  122. // this will get incorrectly considered a network
  123. // path on windows and cause huge delays.
  124. std::string p = *ip;
  125. if (!p.empty() && *p.rbegin() != '/') {
  126. p += "/";
  127. }
  128. // Combine with all the suffixes
  129. for (std::vector<std::string>::const_iterator s = suffixes.begin();
  130. s != suffixes.end(); ++s) {
  131. this->Paths.push_back(p + *s);
  132. }
  133. // And now the original w/o any suffix
  134. this->Paths.push_back(*ip);
  135. }
  136. }
  137. void cmSearchPath::AddPrefixPaths(const std::vector<std::string>& paths,
  138. const char* base)
  139. {
  140. assert(this->FC != CM_NULLPTR);
  141. // default for programs
  142. std::string subdir = "bin";
  143. if (this->FC->CMakePathName == "INCLUDE") {
  144. subdir = "include";
  145. } else if (this->FC->CMakePathName == "LIBRARY") {
  146. subdir = "lib";
  147. } else if (this->FC->CMakePathName == "FRAMEWORK") {
  148. subdir = ""; // ? what to do for frameworks ?
  149. }
  150. for (std::vector<std::string>::const_iterator p = paths.begin();
  151. p != paths.end(); ++p) {
  152. std::string dir = *p;
  153. if (!subdir.empty() && !dir.empty() && *dir.rbegin() != '/') {
  154. dir += "/";
  155. }
  156. if (subdir == "include" || subdir == "lib") {
  157. const char* arch =
  158. this->FC->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE");
  159. if (arch && *arch) {
  160. this->AddPathInternal(dir + subdir + "/" + arch, base);
  161. }
  162. }
  163. std::string add = dir + subdir;
  164. if (add != "/") {
  165. this->AddPathInternal(add, base);
  166. }
  167. if (subdir == "bin") {
  168. this->AddPathInternal(dir + "sbin", base);
  169. }
  170. if (!subdir.empty() && *p != "/") {
  171. this->AddPathInternal(*p, base);
  172. }
  173. }
  174. }
  175. void cmSearchPath::AddPathInternal(const std::string& path, const char* base)
  176. {
  177. assert(this->FC != CM_NULLPTR);
  178. std::string collapsed = cmSystemTools::CollapseFullPath(path, base);
  179. if (collapsed.empty()) {
  180. return;
  181. }
  182. // Insert the path if has not already been emitted.
  183. if (this->FC->SearchPathsEmitted.insert(collapsed).second) {
  184. this->Paths.push_back(collapsed);
  185. }
  186. }