cmFindBase.cxx 10 KB

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