cmFindBase.cxx 11 KB

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