cmFindBase.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFindBase.h"
  4. #include <cstddef>
  5. #include <deque>
  6. #include <map>
  7. #include <utility>
  8. #include <cmext/algorithm>
  9. #include "cmMakefile.h"
  10. #include "cmRange.h"
  11. #include "cmSearchPath.h"
  12. #include "cmState.h"
  13. #include "cmStateTypes.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. class cmExecutionStatus;
  17. cmFindBase::cmFindBase(cmExecutionStatus& status)
  18. : cmFindCommon(status)
  19. {
  20. this->AlreadyInCache = false;
  21. this->AlreadyInCacheWithoutMetaInfo = false;
  22. this->NamesPerDir = false;
  23. this->NamesPerDirAllowed = false;
  24. }
  25. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  26. {
  27. if (argsIn.size() < 2) {
  28. this->SetError("called with incorrect number of arguments");
  29. return false;
  30. }
  31. // copy argsIn into args so it can be modified,
  32. // in the process extract the DOC "documentation"
  33. size_t size = argsIn.size();
  34. std::vector<std::string> args;
  35. bool foundDoc = false;
  36. for (unsigned int j = 0; j < size; ++j) {
  37. if (foundDoc || argsIn[j] != "DOC") {
  38. if (argsIn[j] == "ENV") {
  39. if (j + 1 < size) {
  40. j++;
  41. cmSystemTools::GetPath(args, argsIn[j].c_str());
  42. }
  43. } else {
  44. args.push_back(argsIn[j]);
  45. }
  46. } else {
  47. if (j + 1 < size) {
  48. foundDoc = true;
  49. this->VariableDocumentation = argsIn[j + 1];
  50. j++;
  51. if (j >= size) {
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. if (args.size() < 2) {
  58. this->SetError("called with incorrect number of arguments");
  59. return false;
  60. }
  61. this->VariableName = args[0];
  62. if (this->CheckForVariableInCache()) {
  63. this->AlreadyInCache = true;
  64. return true;
  65. }
  66. this->AlreadyInCache = false;
  67. // Find what search path locations have been enabled/disable
  68. this->SelectDefaultSearchModes();
  69. // Find the current root path mode.
  70. this->SelectDefaultRootPathMode();
  71. // Find the current bundle/framework search policy.
  72. this->SelectDefaultMacMode();
  73. bool newStyle = false;
  74. enum Doing
  75. {
  76. DoingNone,
  77. DoingNames,
  78. DoingPaths,
  79. DoingPathSuffixes,
  80. DoingHints
  81. };
  82. Doing doing = DoingNames; // assume it starts with a name
  83. for (unsigned int j = 1; j < args.size(); ++j) {
  84. if (args[j] == "NAMES") {
  85. doing = DoingNames;
  86. newStyle = true;
  87. } else if (args[j] == "PATHS") {
  88. doing = DoingPaths;
  89. newStyle = true;
  90. } else if (args[j] == "HINTS") {
  91. doing = DoingHints;
  92. newStyle = true;
  93. } else if (args[j] == "PATH_SUFFIXES") {
  94. doing = DoingPathSuffixes;
  95. newStyle = true;
  96. } else if (args[j] == "NAMES_PER_DIR") {
  97. doing = DoingNone;
  98. if (this->NamesPerDirAllowed) {
  99. this->NamesPerDir = true;
  100. } else {
  101. this->SetError("does not support NAMES_PER_DIR");
  102. return false;
  103. }
  104. } else if (args[j] == "NO_SYSTEM_PATH") {
  105. doing = DoingNone;
  106. this->NoDefaultPath = true;
  107. } else if (this->CheckCommonArgument(args[j])) {
  108. doing = DoingNone;
  109. } else {
  110. // Some common arguments were accidentally supported by CMake
  111. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  112. // must support it even though it is not documented.
  113. if (doing == DoingNames) {
  114. this->Names.push_back(args[j]);
  115. } else if (doing == DoingPaths) {
  116. this->UserGuessArgs.push_back(args[j]);
  117. } else if (doing == DoingHints) {
  118. this->UserHintsArgs.push_back(args[j]);
  119. } else if (doing == DoingPathSuffixes) {
  120. this->AddPathSuffix(args[j]);
  121. }
  122. }
  123. }
  124. if (this->VariableDocumentation.empty()) {
  125. this->VariableDocumentation = "Where can ";
  126. if (this->Names.empty()) {
  127. this->VariableDocumentation += "the (unknown) library be found";
  128. } else if (this->Names.size() == 1) {
  129. this->VariableDocumentation +=
  130. "the " + this->Names.front() + " library be found";
  131. } else {
  132. this->VariableDocumentation += "one of the ";
  133. this->VariableDocumentation +=
  134. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  135. this->VariableDocumentation +=
  136. " or " + this->Names.back() + " libraries be found";
  137. }
  138. }
  139. // look for old style
  140. // FIND_*(VAR name path1 path2 ...)
  141. if (!newStyle && !this->Names.empty()) {
  142. // All the short-hand arguments have been recorded as names.
  143. std::vector<std::string> shortArgs = this->Names;
  144. this->Names.clear(); // clear out any values in Names
  145. this->Names.push_back(shortArgs[0]);
  146. cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
  147. }
  148. this->ExpandPaths();
  149. this->ComputeFinalPaths();
  150. return true;
  151. }
  152. void cmFindBase::ExpandPaths()
  153. {
  154. if (!this->NoDefaultPath) {
  155. if (!this->NoPackageRootPath) {
  156. this->FillPackageRootPath();
  157. }
  158. if (!this->NoCMakePath) {
  159. this->FillCMakeVariablePath();
  160. }
  161. if (!this->NoCMakeEnvironmentPath) {
  162. this->FillCMakeEnvironmentPath();
  163. }
  164. }
  165. this->FillUserHintsPath();
  166. if (!this->NoDefaultPath) {
  167. if (!this->NoSystemEnvironmentPath) {
  168. this->FillSystemEnvironmentPath();
  169. }
  170. if (!this->NoCMakeSystemPath) {
  171. this->FillCMakeSystemVariablePath();
  172. }
  173. }
  174. this->FillUserGuessPath();
  175. }
  176. void cmFindBase::FillCMakeEnvironmentPath()
  177. {
  178. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  179. // Add CMAKE_*_PATH environment variables
  180. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  181. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  182. paths.AddEnvPath(var);
  183. if (this->CMakePathName == "PROGRAM") {
  184. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  185. } else {
  186. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  187. }
  188. paths.AddSuffixes(this->SearchPathSuffixes);
  189. }
  190. void cmFindBase::FillPackageRootPath()
  191. {
  192. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  193. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  194. for (std::vector<std::string> const& pkgPaths :
  195. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  196. paths.AddPrefixPaths(pkgPaths);
  197. }
  198. paths.AddSuffixes(this->SearchPathSuffixes);
  199. }
  200. void cmFindBase::FillCMakeVariablePath()
  201. {
  202. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  203. // Add CMake variables of the same name as the previous environment
  204. // variables CMAKE_*_PATH to be used most of the time with -D
  205. // command line options
  206. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  207. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  208. paths.AddCMakePath(var);
  209. if (this->CMakePathName == "PROGRAM") {
  210. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  211. } else {
  212. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  213. }
  214. paths.AddSuffixes(this->SearchPathSuffixes);
  215. }
  216. void cmFindBase::FillSystemEnvironmentPath()
  217. {
  218. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  219. // Add LIB or INCLUDE
  220. if (!this->EnvironmentPath.empty()) {
  221. paths.AddEnvPath(this->EnvironmentPath);
  222. #if defined(_WIN32) || defined(__CYGWIN__)
  223. paths.AddEnvPrefixPath("PATH", true);
  224. #endif
  225. }
  226. // Add PATH
  227. paths.AddEnvPath("PATH");
  228. paths.AddSuffixes(this->SearchPathSuffixes);
  229. }
  230. void cmFindBase::FillCMakeSystemVariablePath()
  231. {
  232. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  233. std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  234. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  235. paths.AddCMakePath(var);
  236. if (this->CMakePathName == "PROGRAM") {
  237. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  238. } else {
  239. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  240. }
  241. paths.AddSuffixes(this->SearchPathSuffixes);
  242. }
  243. void cmFindBase::FillUserHintsPath()
  244. {
  245. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  246. for (std::string const& p : this->UserHintsArgs) {
  247. paths.AddUserPath(p);
  248. }
  249. paths.AddSuffixes(this->SearchPathSuffixes);
  250. }
  251. void cmFindBase::FillUserGuessPath()
  252. {
  253. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  254. for (std::string const& p : this->UserGuessArgs) {
  255. paths.AddUserPath(p);
  256. }
  257. paths.AddSuffixes(this->SearchPathSuffixes);
  258. }
  259. bool cmFindBase::CheckForVariableInCache()
  260. {
  261. if (const char* cacheValue =
  262. this->Makefile->GetDefinition(this->VariableName)) {
  263. cmState* state = this->Makefile->GetState();
  264. const char* cacheEntry = state->GetCacheEntryValue(this->VariableName);
  265. bool found = !cmIsNOTFOUND(cacheValue);
  266. bool cached = cacheEntry != nullptr;
  267. if (found) {
  268. // If the user specifies the entry on the command line without a
  269. // type we should add the type and docstring but keep the
  270. // original value. Tell the subclass implementations to do
  271. // this.
  272. if (cached &&
  273. state->GetCacheEntryType(this->VariableName) ==
  274. cmStateEnums::UNINITIALIZED) {
  275. this->AlreadyInCacheWithoutMetaInfo = true;
  276. }
  277. return true;
  278. }
  279. if (cached) {
  280. const char* hs =
  281. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
  282. this->VariableDocumentation = hs ? hs : "(none)";
  283. }
  284. }
  285. return false;
  286. }
  287. cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
  288. cmFindBase const* findBase)
  289. : FindCommand(findBase)
  290. , CommandName(std::move(commandName))
  291. {
  292. }
  293. cmFindBaseDebugState::~cmFindBaseDebugState()
  294. {
  295. if (this->FindCommand->DebugMode) {
  296. std::string buffer =
  297. cmStrCat(this->CommandName, " called with the following settings:\n");
  298. buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
  299. buffer += cmStrCat(
  300. " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
  301. "\n");
  302. buffer += cmStrCat(
  303. " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
  304. buffer += " Framework\n";
  305. buffer += cmStrCat(" Only Search Frameworks: ",
  306. this->FindCommand->SearchFrameworkOnly, "\n");
  307. buffer += cmStrCat(" Search Frameworks Last: ",
  308. this->FindCommand->SearchFrameworkLast, "\n");
  309. buffer += cmStrCat(" Search Frameworks First: ",
  310. this->FindCommand->SearchFrameworkFirst, "\n");
  311. buffer += " AppBundle\n";
  312. buffer += cmStrCat(" Only Search AppBundle: ",
  313. this->FindCommand->SearchAppBundleOnly, "\n");
  314. buffer += cmStrCat(" Search AppBundle Last: ",
  315. this->FindCommand->SearchAppBundleLast, "\n");
  316. buffer += cmStrCat(" Search AppBundle First: ",
  317. this->FindCommand->SearchAppBundleFirst, "\n");
  318. if (this->FindCommand->NoDefaultPath) {
  319. buffer += " NO_DEFAULT_PATH Enabled\n";
  320. } else {
  321. buffer += cmStrCat(
  322. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
  323. " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
  324. !this->FindCommand->NoCMakeEnvironmentPath, "\n",
  325. " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
  326. !this->FindCommand->NoSystemEnvironmentPath, "\n",
  327. " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
  328. !this->FindCommand->NoCMakeSystemPath, "\n");
  329. }
  330. buffer +=
  331. cmStrCat(this->CommandName, " considered the following locations:\n");
  332. for (auto const& state : this->FailedSearchLocations) {
  333. std::string path = cmStrCat(" ", state.path);
  334. if (!state.regexName.empty()) {
  335. path = cmStrCat(path, "/", state.regexName);
  336. }
  337. buffer += cmStrCat(path, "\n");
  338. }
  339. if (!this->FoundSearchLocation.path.empty()) {
  340. buffer += cmStrCat("The item was found at\n ",
  341. this->FoundSearchLocation.path, "\n");
  342. } else {
  343. buffer += "The item was not found.\n";
  344. }
  345. this->FindCommand->DebugMessage(buffer);
  346. }
  347. }
  348. void cmFindBaseDebugState::FoundAt(std::string const& path,
  349. std::string regexName)
  350. {
  351. if (this->FindCommand->DebugMode) {
  352. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  353. }
  354. }
  355. void cmFindBaseDebugState::FailedAt(std::string const& path,
  356. std::string regexName)
  357. {
  358. if (this->FindCommand->DebugMode) {
  359. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  360. }
  361. }