cmFindBase.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <cstddef>
  5. #include <deque>
  6. #include <map>
  7. #include <utility>
  8. #include <cmext/algorithm>
  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. }
  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 what search path locations have been enabled/disable
  64. this->SelectDefaultSearchModes();
  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. } else {
  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. 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. }
  120. if (this->VariableDocumentation.empty()) {
  121. this->VariableDocumentation = "Where can ";
  122. if (this->Names.empty()) {
  123. this->VariableDocumentation += "the (unknown) library be found";
  124. } else if (this->Names.size() == 1) {
  125. this->VariableDocumentation +=
  126. "the " + this->Names.front() + " library be found";
  127. } else {
  128. this->VariableDocumentation += "one of the ";
  129. this->VariableDocumentation +=
  130. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  131. this->VariableDocumentation +=
  132. " or " + this->Names.back() + " libraries be found";
  133. }
  134. }
  135. // look for old style
  136. // FIND_*(VAR name path1 path2 ...)
  137. if (!newStyle && !this->Names.empty()) {
  138. // All the short-hand arguments have been recorded as names.
  139. std::vector<std::string> shortArgs = this->Names;
  140. this->Names.clear(); // clear out any values in Names
  141. this->Names.push_back(shortArgs[0]);
  142. cm::append(this->UserGuessArgs, 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 = cmStrCat("CMAKE_", this->CMakePathName, "_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::vector<std::string> const& pkgPaths :
  191. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  192. paths.AddPrefixPaths(pkgPaths);
  193. }
  194. paths.AddSuffixes(this->SearchPathSuffixes);
  195. }
  196. void cmFindBase::FillCMakeVariablePath()
  197. {
  198. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  199. // Add CMake variables of the same name as the previous environment
  200. // variables CMAKE_*_PATH to be used most of the time with -D
  201. // command line options
  202. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  203. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  204. paths.AddCMakePath(var);
  205. if (this->CMakePathName == "PROGRAM") {
  206. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  207. } else {
  208. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  209. }
  210. paths.AddSuffixes(this->SearchPathSuffixes);
  211. }
  212. void cmFindBase::FillSystemEnvironmentPath()
  213. {
  214. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  215. // Add LIB or INCLUDE
  216. if (!this->EnvironmentPath.empty()) {
  217. paths.AddEnvPath(this->EnvironmentPath);
  218. #if defined(_WIN32) || defined(__CYGWIN__)
  219. paths.AddEnvPrefixPath("PATH", true);
  220. #endif
  221. }
  222. // Add PATH
  223. paths.AddEnvPath("PATH");
  224. paths.AddSuffixes(this->SearchPathSuffixes);
  225. }
  226. void cmFindBase::FillCMakeSystemVariablePath()
  227. {
  228. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  229. std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  230. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  231. paths.AddCMakePath(var);
  232. if (this->CMakePathName == "PROGRAM") {
  233. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  234. } else {
  235. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  236. }
  237. paths.AddSuffixes(this->SearchPathSuffixes);
  238. }
  239. void cmFindBase::FillUserHintsPath()
  240. {
  241. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  242. for (std::string const& p : this->UserHintsArgs) {
  243. paths.AddUserPath(p);
  244. }
  245. paths.AddSuffixes(this->SearchPathSuffixes);
  246. }
  247. void cmFindBase::FillUserGuessPath()
  248. {
  249. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  250. for (std::string const& p : this->UserGuessArgs) {
  251. paths.AddUserPath(p);
  252. }
  253. paths.AddSuffixes(this->SearchPathSuffixes);
  254. }
  255. bool cmFindBase::CheckForVariableInCache()
  256. {
  257. if (const char* cacheValue =
  258. this->Makefile->GetDefinition(this->VariableName)) {
  259. cmState* state = this->Makefile->GetState();
  260. const char* cacheEntry = state->GetCacheEntryValue(this->VariableName);
  261. bool found = !cmIsNOTFOUND(cacheValue);
  262. bool cached = cacheEntry != nullptr;
  263. if (found) {
  264. // If the user specifies the entry on the command line without a
  265. // type we should add the type and docstring but keep the
  266. // original value. Tell the subclass implementations to do
  267. // this.
  268. if (cached &&
  269. state->GetCacheEntryType(this->VariableName) ==
  270. cmStateEnums::UNINITIALIZED) {
  271. this->AlreadyInCacheWithoutMetaInfo = true;
  272. }
  273. return true;
  274. }
  275. if (cached) {
  276. const char* hs =
  277. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
  278. this->VariableDocumentation = hs ? hs : "(none)";
  279. }
  280. }
  281. return false;
  282. }
  283. cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
  284. cmFindBase const* findBase)
  285. : FindCommand(findBase)
  286. , CommandName(std::move(commandName))
  287. {
  288. }
  289. cmFindBaseDebugState::~cmFindBaseDebugState()
  290. {
  291. if (this->FindCommand->DebugMode) {
  292. std::string buffer =
  293. cmStrCat(this->CommandName, " called with the following settings:\n");
  294. buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
  295. buffer += cmStrCat(
  296. " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
  297. "\n");
  298. buffer += cmStrCat(
  299. " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
  300. buffer += " Framework\n";
  301. buffer += cmStrCat(" Only Search Frameworks: ",
  302. this->FindCommand->SearchFrameworkOnly, "\n");
  303. buffer += cmStrCat(" Search Frameworks Last: ",
  304. this->FindCommand->SearchFrameworkLast, "\n");
  305. buffer += cmStrCat(" Search Frameworks First: ",
  306. this->FindCommand->SearchFrameworkFirst, "\n");
  307. buffer += " AppBundle\n";
  308. buffer += cmStrCat(" Only Search AppBundle: ",
  309. this->FindCommand->SearchAppBundleOnly, "\n");
  310. buffer += cmStrCat(" Search AppBundle Last: ",
  311. this->FindCommand->SearchAppBundleLast, "\n");
  312. buffer += cmStrCat(" Search AppBundle First: ",
  313. this->FindCommand->SearchAppBundleFirst, "\n");
  314. if (this->FindCommand->NoDefaultPath) {
  315. buffer += " NO_DEFAULT_PATH Enabled\n";
  316. } else {
  317. buffer += cmStrCat(
  318. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
  319. " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
  320. !this->FindCommand->NoCMakeEnvironmentPath, "\n",
  321. " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
  322. !this->FindCommand->NoSystemEnvironmentPath, "\n",
  323. " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
  324. !this->FindCommand->NoCMakeSystemPath, "\n");
  325. }
  326. buffer +=
  327. cmStrCat(this->CommandName, " considered the following locations:\n");
  328. for (auto const& state : this->FailedSearchLocations) {
  329. std::string path = cmStrCat(" ", state.path);
  330. if (!state.regexName.empty()) {
  331. path = cmStrCat(path, "/", state.regexName);
  332. }
  333. buffer += cmStrCat(path, "\n");
  334. }
  335. if (!this->FoundSearchLocation.path.empty()) {
  336. buffer += cmStrCat("The item was found at\n ",
  337. this->FoundSearchLocation.path, "\n");
  338. } else {
  339. buffer += "The item was not found.\n";
  340. }
  341. this->FindCommand->DebugMessage(buffer);
  342. }
  343. }
  344. void cmFindBaseDebugState::FoundAt(std::string const& path,
  345. std::string regexName)
  346. {
  347. if (this->FindCommand->DebugMode) {
  348. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  349. }
  350. }
  351. void cmFindBaseDebugState::FailedAt(std::string const& path,
  352. std::string regexName)
  353. {
  354. if (this->FindCommand->DebugMode) {
  355. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  356. }
  357. }