cmFindBase.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmFindBase.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <deque>
  7. #include <iterator>
  8. #include <map>
  9. #include <memory>
  10. #include <utility>
  11. #include <cm/optional>
  12. #include <cmext/algorithm>
  13. #include <cmext/string_view>
  14. #include "cmCMakePath.h"
  15. #include "cmConfigureLog.h"
  16. #include "cmExecutionStatus.h"
  17. #include "cmList.h"
  18. #include "cmListFileCache.h"
  19. #include "cmMakefile.h"
  20. #include "cmMessageType.h"
  21. #include "cmPolicies.h"
  22. #include "cmRange.h"
  23. #include "cmSearchPath.h"
  24. #include "cmState.h"
  25. #include "cmStateTypes.h"
  26. #include "cmStringAlgorithms.h"
  27. #include "cmSystemTools.h"
  28. #include "cmValue.h"
  29. #include "cmWindowsRegistry.h"
  30. #include "cmake.h"
  31. cmFindBase::cmFindBase(std::string findCommandName, cmExecutionStatus& status)
  32. : cmFindCommon(status)
  33. , FindCommandName(std::move(findCommandName))
  34. {
  35. }
  36. cmFindBase::~cmFindBase()
  37. {
  38. if (this->DebugState) {
  39. this->DebugState->Write();
  40. }
  41. }
  42. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  43. {
  44. if (argsIn.size() < 2) {
  45. this->SetError("called with incorrect number of arguments");
  46. return false;
  47. }
  48. // copy argsIn into args so it can be modified,
  49. // in the process extract the DOC "documentation"
  50. // and handle options NO_CACHE and ENV
  51. size_t size = argsIn.size();
  52. std::vector<std::string> args;
  53. bool foundDoc = false;
  54. for (unsigned int j = 0; j < size; ++j) {
  55. if (foundDoc || argsIn[j] != "DOC") {
  56. if (argsIn[j] == "NO_CACHE") {
  57. this->StoreResultInCache = false;
  58. } else if (argsIn[j] == "ENV") {
  59. if (j + 1 < size) {
  60. j++;
  61. std::vector<std::string> p =
  62. cmSystemTools::GetEnvPathNormalized(argsIn[j]);
  63. std::move(p.begin(), p.end(), std::back_inserter(args));
  64. }
  65. } else {
  66. args.push_back(argsIn[j]);
  67. }
  68. } else {
  69. if (j + 1 < size) {
  70. foundDoc = true;
  71. this->VariableDocumentation = argsIn[j + 1];
  72. j++;
  73. if (j >= size) {
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. if (args.size() < 2) {
  80. this->SetError("called with incorrect number of arguments");
  81. return false;
  82. }
  83. this->VariableName = args[0];
  84. this->InitialState = this->GetInitialState();
  85. if (this->IsFound()) {
  86. return true;
  87. }
  88. // Find what search path locations have been enabled/disable
  89. this->SelectDefaultSearchModes();
  90. // Find the current root path mode.
  91. this->SelectDefaultRootPathMode();
  92. // Find the current bundle/framework search policy.
  93. this->SelectDefaultMacMode();
  94. bool newStyle = false;
  95. bool haveRequiredOrOptional = false;
  96. enum Doing
  97. {
  98. DoingNone,
  99. DoingNames,
  100. DoingPaths,
  101. DoingPathSuffixes,
  102. DoingHints
  103. };
  104. Doing doing = DoingNames; // assume it starts with a name
  105. for (unsigned int j = 1; j < args.size(); ++j) {
  106. if (args[j] == "NAMES") {
  107. doing = DoingNames;
  108. newStyle = true;
  109. } else if (args[j] == "PATHS") {
  110. doing = DoingPaths;
  111. newStyle = true;
  112. } else if (args[j] == "HINTS") {
  113. doing = DoingHints;
  114. newStyle = true;
  115. } else if (args[j] == "PATH_SUFFIXES") {
  116. doing = DoingPathSuffixes;
  117. newStyle = true;
  118. } else if (args[j] == "NAMES_PER_DIR") {
  119. doing = DoingNone;
  120. if (this->NamesPerDirAllowed) {
  121. this->NamesPerDir = true;
  122. } else {
  123. this->SetError("does not support NAMES_PER_DIR");
  124. return false;
  125. }
  126. } else if (args[j] == "NO_SYSTEM_PATH") {
  127. doing = DoingNone;
  128. this->NoDefaultPath = true;
  129. } else if (args[j] == "REQUIRED") {
  130. doing = DoingNone;
  131. if (haveRequiredOrOptional && !this->Required) {
  132. this->SetError("cannot be both REQUIRED and OPTIONAL");
  133. return false;
  134. }
  135. this->Required = true;
  136. newStyle = true;
  137. haveRequiredOrOptional = true;
  138. } else if (args[j] == "OPTIONAL") {
  139. doing = DoingNone;
  140. if (haveRequiredOrOptional && this->Required) {
  141. this->SetError("cannot be both REQUIRED and OPTIONAL");
  142. return false;
  143. }
  144. newStyle = true;
  145. haveRequiredOrOptional = true;
  146. } else if (args[j] == "REGISTRY_VIEW") {
  147. if (++j == args.size()) {
  148. this->SetError("missing required argument for REGISTRY_VIEW");
  149. return false;
  150. }
  151. auto view = cmWindowsRegistry::ToView(args[j]);
  152. if (view) {
  153. this->RegistryView = *view;
  154. } else {
  155. this->SetError(
  156. cmStrCat("given invalid value for REGISTRY_VIEW: ", args[j]));
  157. return false;
  158. }
  159. } else if (args[j] == "VALIDATOR") {
  160. if (++j == args.size()) {
  161. this->SetError("missing required argument for VALIDATOR");
  162. return false;
  163. }
  164. auto command = this->Makefile->GetState()->GetCommand(args[j]);
  165. if (!command) {
  166. this->SetError(cmStrCat(
  167. "command specified for VALIDATOR is undefined: ", args[j], '.'));
  168. return false;
  169. }
  170. // ensure a macro is not specified as validator
  171. auto const& validatorName = args[j];
  172. cmList macros{ this->Makefile->GetProperty("MACROS") };
  173. if (std::find_if(macros.begin(), macros.end(),
  174. [&validatorName](std::string const& item) {
  175. return cmSystemTools::Strucmp(validatorName.c_str(),
  176. item.c_str()) == 0;
  177. }) != macros.end()) {
  178. this->SetError(cmStrCat(
  179. "command specified for VALIDATOR is not a function: ", args[j],
  180. '.'));
  181. return false;
  182. }
  183. this->ValidatorName = args[j];
  184. } else if (this->CheckCommonArgument(args[j])) {
  185. doing = DoingNone;
  186. } else {
  187. // Some common arguments were accidentally supported by CMake
  188. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  189. // must support it even though it is not documented.
  190. if (doing == DoingNames) {
  191. this->Names.push_back(args[j]);
  192. } else if (doing == DoingPaths) {
  193. this->UserGuessArgs.push_back(args[j]);
  194. } else if (doing == DoingHints) {
  195. this->UserHintsArgs.push_back(args[j]);
  196. } else if (doing == DoingPathSuffixes) {
  197. this->AddPathSuffix(args[j]);
  198. }
  199. }
  200. }
  201. if (!haveRequiredOrOptional) {
  202. this->Required = this->Makefile->IsOn("CMAKE_FIND_REQUIRED");
  203. }
  204. if (this->VariableDocumentation.empty()) {
  205. this->VariableDocumentation = "Where can ";
  206. if (this->Names.empty()) {
  207. this->VariableDocumentation += "the (unknown) library be found";
  208. } else if (this->Names.size() == 1) {
  209. this->VariableDocumentation +=
  210. "the " + this->Names.front() + " library be found";
  211. } else {
  212. this->VariableDocumentation += "one of the ";
  213. this->VariableDocumentation +=
  214. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  215. this->VariableDocumentation +=
  216. " or " + this->Names.back() + " libraries be found";
  217. }
  218. }
  219. // look for old style
  220. // FIND_*(VAR name path1 path2 ...)
  221. if (!newStyle && !this->Names.empty()) {
  222. // All the short-hand arguments have been recorded as names.
  223. std::vector<std::string> shortArgs = this->Names;
  224. this->Names.clear(); // clear out any values in Names
  225. this->Names.push_back(shortArgs[0]);
  226. cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
  227. }
  228. this->ExpandPaths();
  229. this->ComputeFinalPaths(IgnorePaths::Yes);
  230. return true;
  231. }
  232. bool cmFindBase::Validate(std::string const& path) const
  233. {
  234. if (this->ValidatorName.empty()) {
  235. return true;
  236. }
  237. // The validator command will be executed in an isolated scope.
  238. cmMakefile::ScopePushPop varScope(this->Makefile);
  239. cmMakefile::PolicyPushPop polScope(this->Makefile);
  240. static_cast<void>(varScope);
  241. static_cast<void>(polScope);
  242. auto resultName =
  243. cmStrCat("CMAKE_"_s, cmSystemTools::UpperCase(this->FindCommandName),
  244. "_VALIDATOR_STATUS"_s);
  245. this->Makefile->AddDefinitionBool(resultName, true);
  246. cmListFileFunction validator(
  247. this->ValidatorName, 0, 0,
  248. { cmListFileArgument(resultName, cmListFileArgument::Unquoted, 0),
  249. cmListFileArgument(path, cmListFileArgument::Quoted, 0) });
  250. cmExecutionStatus status(*this->Makefile);
  251. if (this->Makefile->ExecuteCommand(validator, status)) {
  252. return this->Makefile->GetDefinition(resultName).IsOn();
  253. }
  254. return false;
  255. }
  256. void cmFindBase::ExpandPaths()
  257. {
  258. if (!this->NoDefaultPath) {
  259. if (!this->NoPackageRootPath) {
  260. this->FillPackageRootPath();
  261. }
  262. if (!this->NoCMakePath) {
  263. this->FillCMakeVariablePath();
  264. }
  265. if (!this->NoCMakeEnvironmentPath) {
  266. this->FillCMakeEnvironmentPath();
  267. }
  268. }
  269. this->FillUserHintsPath();
  270. if (!this->NoDefaultPath) {
  271. if (!this->NoSystemEnvironmentPath) {
  272. this->FillSystemEnvironmentPath();
  273. }
  274. if (!this->NoCMakeSystemPath) {
  275. this->FillCMakeSystemVariablePath();
  276. }
  277. }
  278. this->FillUserGuessPath();
  279. }
  280. void cmFindBase::FillCMakeEnvironmentPath()
  281. {
  282. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  283. // Add CMAKE_*_PATH environment variables
  284. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  285. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  286. paths.AddEnvPath(var);
  287. if (this->CMakePathName == "PROGRAM") {
  288. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  289. } else {
  290. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  291. }
  292. paths.AddSuffixes(this->SearchPathSuffixes);
  293. }
  294. void cmFindBase::FillPackageRootPath()
  295. {
  296. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  297. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  298. for (std::vector<std::string> const& pkgPaths :
  299. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  300. paths.AddPrefixPaths(pkgPaths);
  301. }
  302. paths.AddSuffixes(this->SearchPathSuffixes);
  303. }
  304. void cmFindBase::FillCMakeVariablePath()
  305. {
  306. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  307. // Add CMake variables of the same name as the previous environment
  308. // variables CMAKE_*_PATH to be used most of the time with -D
  309. // command line options
  310. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  311. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  312. paths.AddCMakePath(var);
  313. if (this->CMakePathName == "PROGRAM") {
  314. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  315. } else {
  316. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  317. }
  318. paths.AddSuffixes(this->SearchPathSuffixes);
  319. }
  320. void cmFindBase::FillSystemEnvironmentPath()
  321. {
  322. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  323. // Add LIB or INCLUDE
  324. if (!this->EnvironmentPath.empty()) {
  325. paths.AddEnvPath(this->EnvironmentPath);
  326. }
  327. // Add PATH
  328. paths.AddEnvPath("PATH");
  329. paths.AddSuffixes(this->SearchPathSuffixes);
  330. }
  331. namespace {
  332. struct entry_to_remove
  333. {
  334. entry_to_remove(std::string const& name, cmMakefile* makefile)
  335. {
  336. if (cmValue to_skip = makefile->GetDefinition(
  337. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_COUNT"))) {
  338. cmStrToLong(*to_skip, &count);
  339. }
  340. if (cmValue prefix_value = makefile->GetDefinition(
  341. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_VALUE"))) {
  342. value = *prefix_value;
  343. }
  344. }
  345. bool valid() const { return count > 0 && !value.empty(); }
  346. void remove_self(std::vector<std::string>& entries) const
  347. {
  348. if (this->valid()) {
  349. long to_skip = this->count;
  350. size_t index_to_remove = 0;
  351. for (auto const& path : entries) {
  352. if (path == this->value && --to_skip == 0) {
  353. break;
  354. }
  355. ++index_to_remove;
  356. }
  357. if (index_to_remove < entries.size() && to_skip == 0) {
  358. entries.erase(entries.begin() + index_to_remove);
  359. }
  360. }
  361. }
  362. long count = -1;
  363. std::string value;
  364. };
  365. }
  366. void cmFindBase::FillCMakeSystemVariablePath()
  367. {
  368. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  369. bool const install_prefix_in_list =
  370. !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX");
  371. bool const remove_install_prefix = this->NoCMakeInstallPath;
  372. bool const add_install_prefix = !this->NoCMakeInstallPath &&
  373. this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX");
  374. // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and
  375. // `CMAKE_INSTALL_PREFIX`.
  376. // Either we need to remove `CMAKE_INSTALL_PREFIX`, add
  377. // `CMAKE_INSTALL_PREFIX`, or do nothing.
  378. //
  379. // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence
  380. // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is
  381. // computed by `CMakeSystemSpecificInformation.cmake` while constructing
  382. // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains
  383. // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove
  384. // some other entry by mistake ( likewise for `CMAKE_STAGING_PREFIX` )
  385. entry_to_remove install_entry("INSTALL", this->Makefile);
  386. entry_to_remove staging_entry("STAGING", this->Makefile);
  387. if (remove_install_prefix && install_prefix_in_list &&
  388. (install_entry.valid() || staging_entry.valid())) {
  389. cmValue prefix_paths =
  390. this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH");
  391. // remove entries from CMAKE_SYSTEM_PREFIX_PATH
  392. cmList expanded{ *prefix_paths };
  393. install_entry.remove_self(expanded);
  394. staging_entry.remove_self(expanded);
  395. for (std::string& p : expanded) {
  396. p = cmSystemTools::CollapseFullPath(
  397. p, this->Makefile->GetCurrentSourceDirectory());
  398. }
  399. paths.AddPrefixPaths(expanded);
  400. } else if (add_install_prefix && !install_prefix_in_list) {
  401. paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
  402. paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
  403. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  404. } else {
  405. // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct
  406. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  407. }
  408. std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  409. paths.AddCMakePath(var);
  410. if (this->CMakePathName == "PROGRAM") {
  411. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  412. } else {
  413. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  414. }
  415. paths.AddSuffixes(this->SearchPathSuffixes);
  416. }
  417. void cmFindBase::FillUserHintsPath()
  418. {
  419. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  420. for (std::string const& p : this->UserHintsArgs) {
  421. paths.AddUserPath(p);
  422. }
  423. paths.AddSuffixes(this->SearchPathSuffixes);
  424. }
  425. void cmFindBase::FillUserGuessPath()
  426. {
  427. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  428. for (std::string const& p : this->UserGuessArgs) {
  429. paths.AddUserPath(p);
  430. }
  431. paths.AddSuffixes(this->SearchPathSuffixes);
  432. }
  433. cmFindBase::FindState cmFindBase::GetInitialState()
  434. {
  435. if (cmValue value = this->Makefile->GetDefinition(this->VariableName)) {
  436. cmState* state = this->Makefile->GetState();
  437. cmValue cacheEntry = state->GetCacheEntryValue(this->VariableName);
  438. bool found = !cmIsNOTFOUND(*value);
  439. bool cached = cacheEntry != nullptr;
  440. auto cacheType = cached ? state->GetCacheEntryType(this->VariableName)
  441. : cmStateEnums::UNINITIALIZED;
  442. if (cached && cacheType != cmStateEnums::UNINITIALIZED) {
  443. this->VariableType = cacheType;
  444. if (auto const& hs =
  445. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING")) {
  446. this->VariableDocumentation = *hs;
  447. }
  448. }
  449. if (found) {
  450. // If the user specifies the entry on the command line without a
  451. // type we should add the type and docstring but keep the
  452. // original value. Tell the subclass implementations to do
  453. // this.
  454. if (cached && cacheType == cmStateEnums::UNINITIALIZED) {
  455. this->AlreadyInCacheWithoutMetaInfo = true;
  456. }
  457. return FindState::Found;
  458. }
  459. return FindState::NotFound;
  460. }
  461. return FindState::Undefined;
  462. }
  463. bool cmFindBase::IsFound() const
  464. {
  465. return this->InitialState == FindState::Found;
  466. }
  467. bool cmFindBase::IsDefined() const
  468. {
  469. return this->InitialState != FindState::Undefined;
  470. }
  471. void cmFindBase::NormalizeFindResult()
  472. {
  473. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) ==
  474. cmPolicies::NEW) {
  475. // ensure the path returned by find_* command is absolute
  476. auto const& existingValue =
  477. this->Makefile->GetDefinition(this->VariableName);
  478. std::string value;
  479. if (!existingValue->empty()) {
  480. value =
  481. cmCMakePath(*existingValue, cmCMakePath::auto_format)
  482. .Absolute(cmCMakePath(
  483. this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
  484. .Normal()
  485. .GenericString();
  486. if (!cmSystemTools::FileExists(value, false)) {
  487. value = *existingValue;
  488. }
  489. }
  490. if (this->StoreResultInCache) {
  491. // If the user specifies the entry on the command line without a
  492. // type we should add the type and docstring but keep the original
  493. // value.
  494. if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
  495. this->Makefile->GetCMakeInstance()->AddCacheEntry(
  496. this->VariableName, value, this->VariableDocumentation,
  497. this->VariableType);
  498. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  499. cmPolicies::NEW) {
  500. if (this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  501. this->Makefile->AddDefinition(this->VariableName, value);
  502. }
  503. } else {
  504. // if there was a definition then remove it
  505. // This is required to ensure same behavior as
  506. // cmMakefile::AddCacheDefinition.
  507. this->Makefile->RemoveDefinition(this->VariableName);
  508. }
  509. }
  510. } else {
  511. // ensure a normal variable is defined.
  512. this->Makefile->AddDefinition(this->VariableName, value);
  513. }
  514. } else {
  515. // If the user specifies the entry on the command line without a
  516. // type we should add the type and docstring but keep the original
  517. // value.
  518. if (this->StoreResultInCache) {
  519. if (this->AlreadyInCacheWithoutMetaInfo) {
  520. this->Makefile->AddCacheDefinition(this->VariableName, "",
  521. this->VariableDocumentation,
  522. this->VariableType);
  523. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  524. cmPolicies::NEW &&
  525. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  526. this->Makefile->AddDefinition(
  527. this->VariableName,
  528. *this->Makefile->GetCMakeInstance()->GetCacheDefinition(
  529. this->VariableName));
  530. }
  531. }
  532. } else {
  533. // ensure a normal variable is defined.
  534. this->Makefile->AddDefinition(
  535. this->VariableName,
  536. this->Makefile->GetSafeDefinition(this->VariableName));
  537. }
  538. }
  539. }
  540. void cmFindBase::StoreFindResult(std::string const& value)
  541. {
  542. bool force =
  543. this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) == cmPolicies::NEW;
  544. bool updateNormalVariable =
  545. this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) == cmPolicies::NEW;
  546. if (!value.empty()) {
  547. if (this->StoreResultInCache) {
  548. this->Makefile->AddCacheDefinition(this->VariableName, value,
  549. this->VariableDocumentation,
  550. this->VariableType, force);
  551. if (updateNormalVariable &&
  552. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  553. this->Makefile->AddDefinition(this->VariableName, value);
  554. }
  555. } else {
  556. this->Makefile->AddDefinition(this->VariableName, value);
  557. }
  558. return;
  559. }
  560. auto notFound = cmStrCat(this->VariableName, "-NOTFOUND");
  561. if (this->StoreResultInCache) {
  562. this->Makefile->AddCacheDefinition(this->VariableName, notFound,
  563. this->VariableDocumentation,
  564. this->VariableType, force);
  565. if (updateNormalVariable &&
  566. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  567. this->Makefile->AddDefinition(this->VariableName, notFound);
  568. }
  569. } else {
  570. this->Makefile->AddDefinition(this->VariableName, notFound);
  571. }
  572. if (this->Required) {
  573. this->Makefile->IssueMessage(
  574. MessageType::FATAL_ERROR,
  575. cmStrCat("Could not find ", this->VariableName, " using the following ",
  576. (this->FindCommandName == "find_file" ||
  577. this->FindCommandName == "find_path"
  578. ? "files"
  579. : "names"),
  580. ": ", cmJoin(this->Names, ", ")));
  581. cmSystemTools::SetFatalErrorOccurred();
  582. }
  583. }
  584. cmFindBaseDebugState::cmFindBaseDebugState(cmFindBase const* findBase)
  585. : cmFindCommonDebugState(findBase->FindCommandName, findBase)
  586. , FindBaseCommand(findBase)
  587. {
  588. }
  589. cmFindBaseDebugState::~cmFindBaseDebugState() = default;
  590. void cmFindBaseDebugState::FoundAtImpl(std::string const& path,
  591. std::string regexName)
  592. {
  593. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  594. }
  595. void cmFindBaseDebugState::FailedAtImpl(std::string const& path,
  596. std::string regexName)
  597. {
  598. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  599. }
  600. void cmFindBaseDebugState::WriteDebug() const
  601. {
  602. // clang-format off
  603. auto buffer =
  604. cmStrCat(
  605. this->CommandName, " called with the following settings:"
  606. "\n VAR: ", this->FindBaseCommand->VariableName,
  607. "\n NAMES: ", cmWrap('"', this->FindBaseCommand->Names, '"', "\n "),
  608. "\n Documentation: ", this->FindBaseCommand->VariableDocumentation,
  609. "\n Framework"
  610. "\n Only Search Frameworks: ", this->FindBaseCommand->SearchFrameworkOnly,
  611. "\n Search Frameworks Last: ", this->FindBaseCommand->SearchFrameworkLast,
  612. "\n Search Frameworks First: ", this->FindBaseCommand->SearchFrameworkFirst,
  613. "\n AppBundle"
  614. "\n Only Search AppBundle: ", this->FindBaseCommand->SearchAppBundleOnly,
  615. "\n Search AppBundle Last: ", this->FindBaseCommand->SearchAppBundleLast,
  616. "\n Search AppBundle First: ", this->FindBaseCommand->SearchAppBundleFirst,
  617. "\n"
  618. );
  619. // clang-format on
  620. if (this->FindCommand->NoDefaultPath) {
  621. buffer += " NO_DEFAULT_PATH Enabled\n";
  622. } else {
  623. // clang-format off
  624. buffer += cmStrCat(
  625. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath,
  626. "\n CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ", !this->FindCommand->NoCMakeEnvironmentPath,
  627. "\n CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ", !this->FindCommand->NoSystemEnvironmentPath,
  628. "\n CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ", !this->FindCommand->NoCMakeSystemPath,
  629. "\n CMAKE_FIND_USE_INSTALL_PREFIX: ", !this->FindCommand->NoCMakeInstallPath,
  630. "\n"
  631. );
  632. // clang-format on
  633. }
  634. buffer +=
  635. cmStrCat(this->CommandName, " considered the following locations:\n");
  636. for (auto const& state : this->FailedSearchLocations) {
  637. std::string path = cmStrCat(" ", state.path);
  638. if (!state.regexName.empty()) {
  639. path = cmStrCat(path, '/', state.regexName);
  640. }
  641. buffer += cmStrCat(path, '\n');
  642. }
  643. if (this->HasBeenFound()) {
  644. buffer += cmStrCat("The item was found at\n ",
  645. this->FoundSearchLocation.path, '\n');
  646. } else {
  647. buffer += "The item was not found.\n";
  648. }
  649. this->FindCommand->DebugMessage(buffer);
  650. }
  651. #ifndef CMAKE_BOOTSTRAP
  652. void cmFindBaseDebugState::WriteEvent(cmConfigureLog& log,
  653. cmMakefile const& mf) const
  654. {
  655. log.BeginEvent("find-v1", mf);
  656. // Mode is the Command name without the "find_" prefix
  657. log.WriteValue("mode"_s, this->CommandName.substr(5));
  658. log.WriteValue("variable"_s, this->FindBaseCommand->VariableName);
  659. log.WriteValue("description"_s,
  660. this->FindBaseCommand->VariableDocumentation);
  661. // Yes, this needs to return a `std::string`. If it returns a `const char*`,
  662. // the `WriteValue` method prefers the `bool` overload. There's no overload
  663. // for a `cm::string_view` because the underlying JSON library doesn't
  664. // support `string_view` arguments itself.
  665. auto search_opt_to_str = [](bool first, bool last,
  666. bool only) -> std::string {
  667. return first ? "FIRST" : (last ? "LAST" : (only ? "ONLY" : "NEVER"));
  668. };
  669. log.BeginObject("settings"_s);
  670. log.WriteValue("SearchFramework"_s,
  671. search_opt_to_str(this->FindCommand->SearchFrameworkFirst,
  672. this->FindCommand->SearchFrameworkLast,
  673. this->FindCommand->SearchFrameworkOnly));
  674. log.WriteValue("SearchAppBundle"_s,
  675. search_opt_to_str(this->FindCommand->SearchAppBundleFirst,
  676. this->FindCommand->SearchAppBundleLast,
  677. this->FindCommand->SearchAppBundleOnly));
  678. log.WriteValue("CMAKE_FIND_USE_CMAKE_PATH"_s,
  679. !this->FindCommand->NoCMakePath);
  680. log.WriteValue("CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH"_s,
  681. !this->FindCommand->NoCMakeEnvironmentPath);
  682. log.WriteValue("CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH"_s,
  683. !this->FindCommand->NoSystemEnvironmentPath);
  684. log.WriteValue("CMAKE_FIND_USE_CMAKE_SYSTEM_PATH"_s,
  685. !this->FindCommand->NoCMakeSystemPath);
  686. log.WriteValue("CMAKE_FIND_USE_INSTALL_PREFIX"_s,
  687. !this->FindCommand->NoCMakeInstallPath);
  688. log.EndObject();
  689. if (!this->FindBaseCommand->Names.empty()) {
  690. log.WriteValue("names"_s, this->FindBaseCommand->Names);
  691. }
  692. std::vector<std::string> directories;
  693. directories.reserve(this->FailedSearchLocations.size());
  694. for (auto const& location : this->FailedSearchLocations) {
  695. directories.push_back(location.path);
  696. }
  697. if (!this->FindCommand->SearchPaths.empty()) {
  698. log.WriteValue("candidate_directories"_s, this->FindCommand->SearchPaths);
  699. }
  700. if (!directories.empty()) {
  701. log.WriteValue("searched_directories"_s, directories);
  702. }
  703. if (!this->FoundSearchLocation.path.empty()) {
  704. log.WriteValue("found"_s, this->FoundSearchLocation.path);
  705. } else {
  706. log.WriteValue("found"_s, false);
  707. }
  708. this->WriteSearchVariables(log, mf);
  709. log.EndEvent();
  710. }
  711. std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
  712. cmFindBaseDebugState::ExtraSearchVariables() const
  713. {
  714. std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
  715. extraSearches;
  716. if (!this->FindBaseCommand->EnvironmentPath.empty()) {
  717. extraSearches.emplace_back(VariableSource::EnvironmentList,
  718. this->FindBaseCommand->EnvironmentPath);
  719. }
  720. return extraSearches;
  721. }
  722. #endif