cmFindBase.cxx 17 KB

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