cmFindBase.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "cmCMakePath.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmPolicies.h"
  13. #include "cmProperty.h"
  14. #include "cmRange.h"
  15. #include "cmSearchPath.h"
  16. #include "cmState.h"
  17. #include "cmStateTypes.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h"
  20. #include "cmake.h"
  21. class cmExecutionStatus;
  22. cmFindBase::cmFindBase(std::string findCommandName, cmExecutionStatus& status)
  23. : cmFindCommon(status)
  24. , FindCommandName(std::move(findCommandName))
  25. {
  26. }
  27. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  28. {
  29. if (argsIn.size() < 2) {
  30. this->SetError("called with incorrect number of arguments");
  31. return false;
  32. }
  33. // copy argsIn into args so it can be modified,
  34. // in the process extract the DOC "documentation"
  35. size_t size = argsIn.size();
  36. std::vector<std::string> args;
  37. bool foundDoc = false;
  38. for (unsigned int j = 0; j < size; ++j) {
  39. if (foundDoc || argsIn[j] != "DOC") {
  40. if (argsIn[j] == "ENV") {
  41. if (j + 1 < size) {
  42. j++;
  43. cmSystemTools::GetPath(args, argsIn[j].c_str());
  44. }
  45. } else {
  46. args.push_back(argsIn[j]);
  47. }
  48. } else {
  49. if (j + 1 < size) {
  50. foundDoc = true;
  51. this->VariableDocumentation = argsIn[j + 1];
  52. j++;
  53. if (j >= size) {
  54. break;
  55. }
  56. }
  57. }
  58. }
  59. if (args.size() < 2) {
  60. this->SetError("called with incorrect number of arguments");
  61. return false;
  62. }
  63. this->VariableName = args[0];
  64. if (this->CheckForVariableInCache()) {
  65. this->AlreadyInCache = true;
  66. return true;
  67. }
  68. this->AlreadyInCache = false;
  69. // Find what search path locations have been enabled/disable
  70. this->SelectDefaultSearchModes();
  71. // Find the current root path mode.
  72. this->SelectDefaultRootPathMode();
  73. // Find the current bundle/framework search policy.
  74. this->SelectDefaultMacMode();
  75. bool newStyle = false;
  76. enum Doing
  77. {
  78. DoingNone,
  79. DoingNames,
  80. DoingPaths,
  81. DoingPathSuffixes,
  82. DoingHints
  83. };
  84. Doing doing = DoingNames; // assume it starts with a name
  85. for (unsigned int j = 1; j < args.size(); ++j) {
  86. if (args[j] == "NAMES") {
  87. doing = DoingNames;
  88. newStyle = true;
  89. } else if (args[j] == "PATHS") {
  90. doing = DoingPaths;
  91. newStyle = true;
  92. } else if (args[j] == "HINTS") {
  93. doing = DoingHints;
  94. newStyle = true;
  95. } else if (args[j] == "PATH_SUFFIXES") {
  96. doing = DoingPathSuffixes;
  97. newStyle = true;
  98. } else if (args[j] == "NAMES_PER_DIR") {
  99. doing = DoingNone;
  100. if (this->NamesPerDirAllowed) {
  101. this->NamesPerDir = true;
  102. } else {
  103. this->SetError("does not support NAMES_PER_DIR");
  104. return false;
  105. }
  106. } else if (args[j] == "NO_SYSTEM_PATH") {
  107. doing = DoingNone;
  108. this->NoDefaultPath = true;
  109. } else if (args[j] == "REQUIRED") {
  110. doing = DoingNone;
  111. this->Required = true;
  112. newStyle = true;
  113. } else if (this->CheckCommonArgument(args[j])) {
  114. doing = DoingNone;
  115. } else {
  116. // Some common arguments were accidentally supported by CMake
  117. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  118. // must support it even though it is not documented.
  119. if (doing == DoingNames) {
  120. this->Names.push_back(args[j]);
  121. } else if (doing == DoingPaths) {
  122. this->UserGuessArgs.push_back(args[j]);
  123. } else if (doing == DoingHints) {
  124. this->UserHintsArgs.push_back(args[j]);
  125. } else if (doing == DoingPathSuffixes) {
  126. this->AddPathSuffix(args[j]);
  127. }
  128. }
  129. }
  130. if (this->VariableDocumentation.empty()) {
  131. this->VariableDocumentation = "Where can ";
  132. if (this->Names.empty()) {
  133. this->VariableDocumentation += "the (unknown) library be found";
  134. } else if (this->Names.size() == 1) {
  135. this->VariableDocumentation +=
  136. "the " + this->Names.front() + " library be found";
  137. } else {
  138. this->VariableDocumentation += "one of the ";
  139. this->VariableDocumentation +=
  140. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  141. this->VariableDocumentation +=
  142. " or " + this->Names.back() + " libraries be found";
  143. }
  144. }
  145. // look for old style
  146. // FIND_*(VAR name path1 path2 ...)
  147. if (!newStyle && !this->Names.empty()) {
  148. // All the short-hand arguments have been recorded as names.
  149. std::vector<std::string> shortArgs = this->Names;
  150. this->Names.clear(); // clear out any values in Names
  151. this->Names.push_back(shortArgs[0]);
  152. cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
  153. }
  154. this->ExpandPaths();
  155. this->ComputeFinalPaths();
  156. return true;
  157. }
  158. void cmFindBase::ExpandPaths()
  159. {
  160. if (!this->NoDefaultPath) {
  161. if (!this->NoPackageRootPath) {
  162. this->FillPackageRootPath();
  163. }
  164. if (!this->NoCMakePath) {
  165. this->FillCMakeVariablePath();
  166. }
  167. if (!this->NoCMakeEnvironmentPath) {
  168. this->FillCMakeEnvironmentPath();
  169. }
  170. }
  171. this->FillUserHintsPath();
  172. if (!this->NoDefaultPath) {
  173. if (!this->NoSystemEnvironmentPath) {
  174. this->FillSystemEnvironmentPath();
  175. }
  176. if (!this->NoCMakeSystemPath) {
  177. this->FillCMakeSystemVariablePath();
  178. }
  179. }
  180. this->FillUserGuessPath();
  181. }
  182. void cmFindBase::FillCMakeEnvironmentPath()
  183. {
  184. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  185. // Add CMAKE_*_PATH environment variables
  186. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  187. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  188. paths.AddEnvPath(var);
  189. if (this->CMakePathName == "PROGRAM") {
  190. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  191. } else {
  192. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  193. }
  194. paths.AddSuffixes(this->SearchPathSuffixes);
  195. }
  196. void cmFindBase::FillPackageRootPath()
  197. {
  198. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  199. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  200. for (std::vector<std::string> const& pkgPaths :
  201. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  202. paths.AddPrefixPaths(pkgPaths);
  203. }
  204. paths.AddSuffixes(this->SearchPathSuffixes);
  205. }
  206. void cmFindBase::FillCMakeVariablePath()
  207. {
  208. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  209. // Add CMake variables of the same name as the previous environment
  210. // variables CMAKE_*_PATH to be used most of the time with -D
  211. // command line options
  212. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_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 = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  240. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  241. paths.AddCMakePath(var);
  242. if (this->CMakePathName == "PROGRAM") {
  243. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  244. } else {
  245. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  246. }
  247. paths.AddSuffixes(this->SearchPathSuffixes);
  248. }
  249. void cmFindBase::FillUserHintsPath()
  250. {
  251. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  252. for (std::string const& p : this->UserHintsArgs) {
  253. paths.AddUserPath(p);
  254. }
  255. paths.AddSuffixes(this->SearchPathSuffixes);
  256. }
  257. void cmFindBase::FillUserGuessPath()
  258. {
  259. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  260. for (std::string const& p : this->UserGuessArgs) {
  261. paths.AddUserPath(p);
  262. }
  263. paths.AddSuffixes(this->SearchPathSuffixes);
  264. }
  265. bool cmFindBase::CheckForVariableInCache()
  266. {
  267. if (cmProp cacheValue = this->Makefile->GetDefinition(this->VariableName)) {
  268. cmState* state = this->Makefile->GetState();
  269. cmProp cacheEntry = state->GetCacheEntryValue(this->VariableName);
  270. bool found = !cmIsNOTFOUND(*cacheValue);
  271. bool cached = cacheEntry != nullptr;
  272. auto cacheType = cached ? state->GetCacheEntryType(this->VariableName)
  273. : cmStateEnums::UNINITIALIZED;
  274. if (cached && cacheType != cmStateEnums::UNINITIALIZED) {
  275. this->VariableType = cacheType;
  276. if (const auto* hs =
  277. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING")) {
  278. this->VariableDocumentation = *hs;
  279. }
  280. }
  281. if (found) {
  282. // If the user specifies the entry on the command line without a
  283. // type we should add the type and docstring but keep the
  284. // original value. Tell the subclass implementations to do
  285. // this.
  286. if (cached && cacheType == cmStateEnums::UNINITIALIZED) {
  287. this->AlreadyInCacheWithoutMetaInfo = true;
  288. }
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. void cmFindBase::NormalizeFindResult()
  295. {
  296. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) ==
  297. cmPolicies::NEW) {
  298. // ensure the path returned by find_* command is absolute
  299. const auto* existingValue =
  300. this->Makefile->GetDefinition(this->VariableName);
  301. std::string value;
  302. if (!existingValue->empty()) {
  303. value =
  304. cmCMakePath(*existingValue, cmCMakePath::auto_format)
  305. .Absolute(cmCMakePath(
  306. this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
  307. .Normal()
  308. .GenericString();
  309. // value = cmSystemTools::CollapseFullPath(*existingValue);
  310. if (!cmSystemTools::FileExists(value, false)) {
  311. value = *existingValue;
  312. }
  313. }
  314. // If the user specifies the entry on the command line without a
  315. // type we should add the type and docstring but keep the original
  316. // value.
  317. if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
  318. this->Makefile->GetCMakeInstance()->AddCacheEntry(
  319. this->VariableName, value.c_str(), this->VariableDocumentation.c_str(),
  320. this->VariableType);
  321. // if there was a definition then remove it
  322. // This is required to ensure same behavior as
  323. // cmMakefile::AddCacheDefinition.
  324. // See #22038 for problems raised by this behavior.
  325. this->Makefile->RemoveDefinition(this->VariableName);
  326. }
  327. } else {
  328. // If the user specifies the entry on the command line without a
  329. // type we should add the type and docstring but keep the original
  330. // value.
  331. if (this->AlreadyInCacheWithoutMetaInfo) {
  332. this->Makefile->AddCacheDefinition(this->VariableName, "",
  333. this->VariableDocumentation.c_str(),
  334. this->VariableType);
  335. }
  336. }
  337. }
  338. void cmFindBase::StoreFindResult(const std::string& value)
  339. {
  340. bool force =
  341. this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) == cmPolicies::NEW;
  342. if (!value.empty()) {
  343. this->Makefile->AddCacheDefinition(this->VariableName, value,
  344. this->VariableDocumentation.c_str(),
  345. this->VariableType, force);
  346. return;
  347. }
  348. this->Makefile->AddCacheDefinition(
  349. this->VariableName, cmStrCat(this->VariableName, "-NOTFOUND"),
  350. this->VariableDocumentation.c_str(), this->VariableType, force);
  351. if (this->Required) {
  352. this->Makefile->IssueMessage(
  353. MessageType::FATAL_ERROR,
  354. cmStrCat("Could not find ", this->VariableName, " using the following ",
  355. (this->FindCommandName == "find_file" ||
  356. this->FindCommandName == "find_path"
  357. ? "files"
  358. : "names"),
  359. ": ", cmJoin(this->Names, ", ")));
  360. cmSystemTools::SetFatalErrorOccured();
  361. }
  362. }
  363. cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
  364. cmFindBase const* findBase)
  365. : FindCommand(findBase)
  366. , CommandName(std::move(commandName))
  367. {
  368. }
  369. cmFindBaseDebugState::~cmFindBaseDebugState()
  370. {
  371. if (this->FindCommand->DebugMode) {
  372. std::string buffer =
  373. cmStrCat(this->CommandName, " called with the following settings:\n");
  374. buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
  375. buffer += cmStrCat(
  376. " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
  377. "\n");
  378. buffer += cmStrCat(
  379. " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
  380. buffer += " Framework\n";
  381. buffer += cmStrCat(" Only Search Frameworks: ",
  382. this->FindCommand->SearchFrameworkOnly, "\n");
  383. buffer += cmStrCat(" Search Frameworks Last: ",
  384. this->FindCommand->SearchFrameworkLast, "\n");
  385. buffer += cmStrCat(" Search Frameworks First: ",
  386. this->FindCommand->SearchFrameworkFirst, "\n");
  387. buffer += " AppBundle\n";
  388. buffer += cmStrCat(" Only Search AppBundle: ",
  389. this->FindCommand->SearchAppBundleOnly, "\n");
  390. buffer += cmStrCat(" Search AppBundle Last: ",
  391. this->FindCommand->SearchAppBundleLast, "\n");
  392. buffer += cmStrCat(" Search AppBundle First: ",
  393. this->FindCommand->SearchAppBundleFirst, "\n");
  394. if (this->FindCommand->NoDefaultPath) {
  395. buffer += " NO_DEFAULT_PATH Enabled\n";
  396. } else {
  397. buffer += cmStrCat(
  398. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
  399. " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
  400. !this->FindCommand->NoCMakeEnvironmentPath, "\n",
  401. " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
  402. !this->FindCommand->NoSystemEnvironmentPath, "\n",
  403. " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
  404. !this->FindCommand->NoCMakeSystemPath, "\n");
  405. }
  406. buffer +=
  407. cmStrCat(this->CommandName, " considered the following locations:\n");
  408. for (auto const& state : this->FailedSearchLocations) {
  409. std::string path = cmStrCat(" ", state.path);
  410. if (!state.regexName.empty()) {
  411. path = cmStrCat(path, "/", state.regexName);
  412. }
  413. buffer += cmStrCat(path, "\n");
  414. }
  415. if (!this->FoundSearchLocation.path.empty()) {
  416. buffer += cmStrCat("The item was found at\n ",
  417. this->FoundSearchLocation.path, "\n");
  418. } else {
  419. buffer += "The item was not found.\n";
  420. }
  421. this->FindCommand->DebugMessage(buffer);
  422. }
  423. }
  424. void cmFindBaseDebugState::FoundAt(std::string const& path,
  425. std::string regexName)
  426. {
  427. if (this->FindCommand->DebugMode) {
  428. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  429. }
  430. }
  431. void cmFindBaseDebugState::FailedAt(std::string const& path,
  432. std::string regexName)
  433. {
  434. if (this->FindCommand->DebugMode) {
  435. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  436. }
  437. }