cmFindBase.cxx 22 KB

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