cmFindBase.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "cmFindBase.h"
  4. #include <deque>
  5. #include <iostream>
  6. #include <iterator>
  7. #include <map>
  8. #include <stddef.h>
  9. #include "cmAlgorithms.h"
  10. #include "cmMakefile.h"
  11. #include "cmSearchPath.h"
  12. #include "cmState.h"
  13. #include "cmStateTypes.h"
  14. #include "cmSystemTools.h"
  15. cmFindBase::cmFindBase()
  16. {
  17. this->AlreadyInCache = false;
  18. this->AlreadyInCacheWithoutMetaInfo = false;
  19. this->NamesPerDir = false;
  20. this->NamesPerDirAllowed = false;
  21. }
  22. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  23. {
  24. if (argsIn.size() < 2) {
  25. this->SetError("called with incorrect number of arguments");
  26. return false;
  27. }
  28. // copy argsIn into args so it can be modified,
  29. // in the process extract the DOC "documentation"
  30. size_t size = argsIn.size();
  31. std::vector<std::string> args;
  32. bool foundDoc = false;
  33. for (unsigned int j = 0; j < size; ++j) {
  34. if (foundDoc || argsIn[j] != "DOC") {
  35. if (argsIn[j] == "ENV") {
  36. if (j + 1 < size) {
  37. j++;
  38. cmSystemTools::GetPath(args, argsIn[j].c_str());
  39. }
  40. } else {
  41. args.push_back(argsIn[j]);
  42. }
  43. } else {
  44. if (j + 1 < size) {
  45. foundDoc = true;
  46. this->VariableDocumentation = argsIn[j + 1];
  47. j++;
  48. if (j >= size) {
  49. break;
  50. }
  51. }
  52. }
  53. }
  54. if (args.size() < 2) {
  55. this->SetError("called with incorrect number of arguments");
  56. return false;
  57. }
  58. this->VariableName = args[0];
  59. if (this->CheckForVariableInCache()) {
  60. this->AlreadyInCache = true;
  61. return true;
  62. }
  63. this->AlreadyInCache = false;
  64. // Find the current root path mode.
  65. this->SelectDefaultRootPathMode();
  66. // Find the current bundle/framework search policy.
  67. this->SelectDefaultMacMode();
  68. bool newStyle = false;
  69. enum Doing
  70. {
  71. DoingNone,
  72. DoingNames,
  73. DoingPaths,
  74. DoingPathSuffixes,
  75. DoingHints
  76. };
  77. Doing doing = DoingNames; // assume it starts with a name
  78. for (unsigned int j = 1; j < args.size(); ++j) {
  79. if (args[j] == "NAMES") {
  80. doing = DoingNames;
  81. newStyle = true;
  82. } else if (args[j] == "PATHS") {
  83. doing = DoingPaths;
  84. newStyle = true;
  85. } else if (args[j] == "HINTS") {
  86. doing = DoingHints;
  87. newStyle = true;
  88. } else if (args[j] == "PATH_SUFFIXES") {
  89. doing = DoingPathSuffixes;
  90. newStyle = true;
  91. } else if (args[j] == "NAMES_PER_DIR") {
  92. doing = DoingNone;
  93. if (this->NamesPerDirAllowed) {
  94. this->NamesPerDir = true;
  95. } else {
  96. this->SetError("does not support NAMES_PER_DIR");
  97. return false;
  98. }
  99. } else if (args[j] == "NO_SYSTEM_PATH") {
  100. doing = DoingNone;
  101. this->NoDefaultPath = true;
  102. } else if (this->CheckCommonArgument(args[j])) {
  103. doing = DoingNone;
  104. // Some common arguments were accidentally supported by CMake
  105. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  106. // must support it even though it is not documented.
  107. } else if (doing == DoingNames) {
  108. this->Names.push_back(args[j]);
  109. } else if (doing == DoingPaths) {
  110. this->UserGuessArgs.push_back(args[j]);
  111. } else if (doing == DoingHints) {
  112. this->UserHintsArgs.push_back(args[j]);
  113. } else if (doing == DoingPathSuffixes) {
  114. this->AddPathSuffix(args[j]);
  115. }
  116. }
  117. if (this->VariableDocumentation.empty()) {
  118. this->VariableDocumentation = "Where can ";
  119. if (this->Names.empty()) {
  120. this->VariableDocumentation += "the (unknown) library be found";
  121. } else if (this->Names.size() == 1) {
  122. this->VariableDocumentation +=
  123. "the " + this->Names.front() + " library be found";
  124. } else {
  125. this->VariableDocumentation += "one of the ";
  126. this->VariableDocumentation +=
  127. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  128. this->VariableDocumentation +=
  129. " or " + this->Names.back() + " libraries be found";
  130. }
  131. }
  132. // look for old style
  133. // FIND_*(VAR name path1 path2 ...)
  134. if (!newStyle) {
  135. // All the short-hand arguments have been recorded as names.
  136. std::vector<std::string> shortArgs = this->Names;
  137. this->Names.clear(); // clear out any values in Names
  138. this->Names.push_back(shortArgs[0]);
  139. this->UserGuessArgs.insert(this->UserGuessArgs.end(),
  140. shortArgs.begin() + 1, shortArgs.end());
  141. }
  142. this->ExpandPaths();
  143. this->ComputeFinalPaths();
  144. return true;
  145. }
  146. void cmFindBase::ExpandPaths()
  147. {
  148. if (!this->NoDefaultPath) {
  149. if (!this->NoPackageRootPath) {
  150. this->FillPackageRootPath();
  151. }
  152. if (!this->NoCMakePath) {
  153. this->FillCMakeVariablePath();
  154. }
  155. if (!this->NoCMakeEnvironmentPath) {
  156. this->FillCMakeEnvironmentPath();
  157. }
  158. }
  159. this->FillUserHintsPath();
  160. if (!this->NoDefaultPath) {
  161. if (!this->NoSystemEnvironmentPath) {
  162. this->FillSystemEnvironmentPath();
  163. }
  164. if (!this->NoCMakeSystemPath) {
  165. this->FillCMakeSystemVariablePath();
  166. }
  167. }
  168. this->FillUserGuessPath();
  169. }
  170. void cmFindBase::FillCMakeEnvironmentPath()
  171. {
  172. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  173. // Add CMAKE_*_PATH environment variables
  174. std::string var = "CMAKE_";
  175. var += this->CMakePathName;
  176. var += "_PATH";
  177. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  178. paths.AddEnvPath(var);
  179. if (this->CMakePathName == "PROGRAM") {
  180. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  181. } else {
  182. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  183. }
  184. paths.AddSuffixes(this->SearchPathSuffixes);
  185. }
  186. void cmFindBase::FillPackageRootPath()
  187. {
  188. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  189. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  190. for (std::deque<std::vector<std::string>>::const_reverse_iterator pkgPaths =
  191. this->Makefile->FindPackageRootPathStack.rbegin();
  192. pkgPaths != this->Makefile->FindPackageRootPathStack.rend();
  193. ++pkgPaths) {
  194. paths.AddPrefixPaths(*pkgPaths);
  195. }
  196. paths.AddSuffixes(this->SearchPathSuffixes);
  197. }
  198. void cmFindBase::FillCMakeVariablePath()
  199. {
  200. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  201. // Add CMake variables of the same name as the previous environment
  202. // variables CMAKE_*_PATH to be used most of the time with -D
  203. // command line options
  204. std::string var = "CMAKE_";
  205. var += this->CMakePathName;
  206. var += "_PATH";
  207. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  208. paths.AddCMakePath(var);
  209. if (this->CMakePathName == "PROGRAM") {
  210. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  211. } else {
  212. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  213. }
  214. paths.AddSuffixes(this->SearchPathSuffixes);
  215. }
  216. void cmFindBase::FillSystemEnvironmentPath()
  217. {
  218. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  219. // Add LIB or INCLUDE
  220. if (!this->EnvironmentPath.empty()) {
  221. paths.AddEnvPath(this->EnvironmentPath);
  222. #if defined(_WIN32) || defined(__CYGWIN__)
  223. paths.AddEnvPrefixPath("PATH", true);
  224. #endif
  225. }
  226. // Add PATH
  227. paths.AddEnvPath("PATH");
  228. paths.AddSuffixes(this->SearchPathSuffixes);
  229. }
  230. void cmFindBase::FillCMakeSystemVariablePath()
  231. {
  232. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  233. std::string var = "CMAKE_SYSTEM_";
  234. var += this->CMakePathName;
  235. var += "_PATH";
  236. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  237. paths.AddCMakePath(var);
  238. if (this->CMakePathName == "PROGRAM") {
  239. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  240. } else {
  241. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  242. }
  243. paths.AddSuffixes(this->SearchPathSuffixes);
  244. }
  245. void cmFindBase::FillUserHintsPath()
  246. {
  247. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  248. for (std::string const& p : this->UserHintsArgs) {
  249. paths.AddUserPath(p);
  250. }
  251. paths.AddSuffixes(this->SearchPathSuffixes);
  252. }
  253. void cmFindBase::FillUserGuessPath()
  254. {
  255. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  256. for (std::string const& p : this->UserGuessArgs) {
  257. paths.AddUserPath(p);
  258. }
  259. paths.AddSuffixes(this->SearchPathSuffixes);
  260. }
  261. void cmFindBase::PrintFindStuff()
  262. {
  263. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  264. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  265. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  266. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  267. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  268. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  269. std::cerr << "VariableName " << this->VariableName << "\n";
  270. std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
  271. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  272. std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath
  273. << "\n";
  274. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  275. std::cerr << "NoSystemEnvironmentPath " << this->NoSystemEnvironmentPath
  276. << "\n";
  277. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  278. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  279. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  280. std::cerr << "Names " << cmJoin(this->Names, " ") << "\n";
  281. std::cerr << "\n";
  282. std::cerr << "SearchPathSuffixes ";
  283. std::cerr << cmJoin(this->SearchPathSuffixes, "\n") << "\n";
  284. std::cerr << "SearchPaths\n";
  285. std::cerr << cmWrap("[", this->SearchPaths, "]", "\n") << "\n";
  286. }
  287. bool cmFindBase::CheckForVariableInCache()
  288. {
  289. if (const char* cacheValue =
  290. this->Makefile->GetDefinition(this->VariableName)) {
  291. cmState* state = this->Makefile->GetState();
  292. const char* cacheEntry = state->GetCacheEntryValue(this->VariableName);
  293. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  294. bool cached = cacheEntry != nullptr;
  295. if (found) {
  296. // If the user specifies the entry on the command line without a
  297. // type we should add the type and docstring but keep the
  298. // original value. Tell the subclass implementations to do
  299. // this.
  300. if (cached &&
  301. state->GetCacheEntryType(this->VariableName) ==
  302. cmStateEnums::UNINITIALIZED) {
  303. this->AlreadyInCacheWithoutMetaInfo = true;
  304. }
  305. return true;
  306. }
  307. if (cached) {
  308. const char* hs =
  309. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
  310. this->VariableDocumentation = hs ? hs : "(none)";
  311. }
  312. }
  313. return false;
  314. }