cmFindBase.cxx 11 KB

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