cmFindBase.cxx 10.0 KB

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