cmFindBase.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "cmAlgorithms.h"
  5. #include "cmState.h"
  6. #include "cmStateTypes.h"
  7. cmFindBase::cmFindBase()
  8. {
  9. this->AlreadyInCache = false;
  10. this->AlreadyInCacheWithoutMetaInfo = false;
  11. this->NamesPerDir = false;
  12. this->NamesPerDirAllowed = false;
  13. }
  14. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  15. {
  16. if (argsIn.size() < 2) {
  17. this->SetError("called with incorrect number of arguments");
  18. return false;
  19. }
  20. // copy argsIn into args so it can be modified,
  21. // in the process extract the DOC "documentation"
  22. size_t size = argsIn.size();
  23. std::vector<std::string> args;
  24. bool foundDoc = false;
  25. for (unsigned int j = 0; j < size; ++j) {
  26. if (foundDoc || argsIn[j] != "DOC") {
  27. if (argsIn[j] == "ENV") {
  28. if (j + 1 < size) {
  29. j++;
  30. cmSystemTools::GetPath(args, argsIn[j].c_str());
  31. }
  32. } else {
  33. args.push_back(argsIn[j]);
  34. }
  35. } else {
  36. if (j + 1 < size) {
  37. foundDoc = true;
  38. this->VariableDocumentation = argsIn[j + 1];
  39. j++;
  40. if (j >= size) {
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. if (args.size() < 2) {
  47. this->SetError("called with incorrect number of arguments");
  48. return false;
  49. }
  50. this->VariableName = args[0];
  51. if (this->CheckForVariableInCache()) {
  52. this->AlreadyInCache = true;
  53. return true;
  54. }
  55. this->AlreadyInCache = false;
  56. // Find the current root path mode.
  57. this->SelectDefaultRootPathMode();
  58. // Find the current bundle/framework search policy.
  59. this->SelectDefaultMacMode();
  60. bool newStyle = false;
  61. enum Doing
  62. {
  63. DoingNone,
  64. DoingNames,
  65. DoingPaths,
  66. DoingPathSuffixes,
  67. DoingHints
  68. };
  69. Doing doing = DoingNames; // assume it starts with a name
  70. for (unsigned int j = 1; j < args.size(); ++j) {
  71. if (args[j] == "NAMES") {
  72. doing = DoingNames;
  73. newStyle = true;
  74. } else if (args[j] == "PATHS") {
  75. doing = DoingPaths;
  76. newStyle = true;
  77. } else if (args[j] == "HINTS") {
  78. doing = DoingHints;
  79. newStyle = true;
  80. } else if (args[j] == "PATH_SUFFIXES") {
  81. doing = DoingPathSuffixes;
  82. newStyle = true;
  83. } else if (args[j] == "NAMES_PER_DIR") {
  84. doing = DoingNone;
  85. if (this->NamesPerDirAllowed) {
  86. this->NamesPerDir = true;
  87. } else {
  88. this->SetError("does not support NAMES_PER_DIR");
  89. return false;
  90. }
  91. } else if (args[j] == "NO_SYSTEM_PATH") {
  92. doing = DoingNone;
  93. this->NoDefaultPath = true;
  94. } else if (this->CheckCommonArgument(args[j])) {
  95. doing = DoingNone;
  96. // Some common arguments were accidentally supported by CMake
  97. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  98. // must support it even though it is not documented.
  99. } else if (doing == DoingNames) {
  100. this->Names.push_back(args[j]);
  101. } else if (doing == DoingPaths) {
  102. this->UserGuessArgs.push_back(args[j]);
  103. } else if (doing == DoingHints) {
  104. this->UserHintsArgs.push_back(args[j]);
  105. } else if (doing == DoingPathSuffixes) {
  106. this->AddPathSuffix(args[j]);
  107. }
  108. }
  109. if (this->VariableDocumentation.empty()) {
  110. this->VariableDocumentation = "Where can ";
  111. if (this->Names.empty()) {
  112. this->VariableDocumentation += "the (unknown) library be found";
  113. } else if (this->Names.size() == 1) {
  114. this->VariableDocumentation +=
  115. "the " + this->Names[0] + " library be found";
  116. } else {
  117. this->VariableDocumentation += "one of the ";
  118. this->VariableDocumentation +=
  119. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  120. this->VariableDocumentation +=
  121. " or " + this->Names[this->Names.size() - 1] + " libraries be found";
  122. }
  123. }
  124. // look for old style
  125. // FIND_*(VAR name path1 path2 ...)
  126. if (!newStyle) {
  127. // All the short-hand arguments have been recorded as names.
  128. std::vector<std::string> shortArgs = this->Names;
  129. this->Names.clear(); // clear out any values in Names
  130. this->Names.push_back(shortArgs[0]);
  131. this->UserGuessArgs.insert(this->UserGuessArgs.end(),
  132. shortArgs.begin() + 1, shortArgs.end());
  133. }
  134. this->ExpandPaths();
  135. this->ComputeFinalPaths();
  136. return true;
  137. }
  138. void cmFindBase::ExpandPaths()
  139. {
  140. if (!this->NoDefaultPath) {
  141. if (!this->NoCMakePath) {
  142. this->FillCMakeVariablePath();
  143. }
  144. if (!this->NoCMakeEnvironmentPath) {
  145. this->FillCMakeEnvironmentPath();
  146. }
  147. }
  148. this->FillUserHintsPath();
  149. if (!this->NoDefaultPath) {
  150. if (!this->NoSystemEnvironmentPath) {
  151. this->FillSystemEnvironmentPath();
  152. }
  153. if (!this->NoCMakeSystemPath) {
  154. this->FillCMakeSystemVariablePath();
  155. }
  156. }
  157. this->FillUserGuessPath();
  158. }
  159. void cmFindBase::FillCMakeEnvironmentPath()
  160. {
  161. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  162. // Add CMAKE_*_PATH environment variables
  163. std::string var = "CMAKE_";
  164. var += this->CMakePathName;
  165. var += "_PATH";
  166. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  167. paths.AddEnvPath(var);
  168. if (this->CMakePathName == "PROGRAM") {
  169. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  170. } else {
  171. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  172. }
  173. paths.AddSuffixes(this->SearchPathSuffixes);
  174. }
  175. void cmFindBase::FillCMakeVariablePath()
  176. {
  177. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  178. // Add CMake varibles of the same name as the previous environment
  179. // varibles CMAKE_*_PATH to be used most of the time with -D
  180. // command line options
  181. std::string var = "CMAKE_";
  182. var += this->CMakePathName;
  183. var += "_PATH";
  184. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  185. paths.AddCMakePath(var);
  186. if (this->CMakePathName == "PROGRAM") {
  187. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  188. } else {
  189. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  190. }
  191. paths.AddSuffixes(this->SearchPathSuffixes);
  192. }
  193. void cmFindBase::FillSystemEnvironmentPath()
  194. {
  195. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  196. // Add LIB or INCLUDE
  197. if (!this->EnvironmentPath.empty()) {
  198. paths.AddEnvPath(this->EnvironmentPath);
  199. #if defined(_WIN32) || defined(__CYGWIN__)
  200. paths.AddEnvPrefixPath("PATH", true);
  201. #endif
  202. }
  203. // Add PATH
  204. paths.AddEnvPath("PATH");
  205. paths.AddSuffixes(this->SearchPathSuffixes);
  206. }
  207. void cmFindBase::FillCMakeSystemVariablePath()
  208. {
  209. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  210. std::string var = "CMAKE_SYSTEM_";
  211. var += this->CMakePathName;
  212. var += "_PATH";
  213. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  214. paths.AddCMakePath(var);
  215. if (this->CMakePathName == "PROGRAM") {
  216. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  217. } else {
  218. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  219. }
  220. paths.AddSuffixes(this->SearchPathSuffixes);
  221. }
  222. void cmFindBase::FillUserHintsPath()
  223. {
  224. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  225. for (std::vector<std::string>::const_iterator p =
  226. this->UserHintsArgs.begin();
  227. p != this->UserHintsArgs.end(); ++p) {
  228. paths.AddUserPath(*p);
  229. }
  230. paths.AddSuffixes(this->SearchPathSuffixes);
  231. }
  232. void cmFindBase::FillUserGuessPath()
  233. {
  234. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  235. for (std::vector<std::string>::const_iterator p =
  236. this->UserGuessArgs.begin();
  237. p != this->UserGuessArgs.end(); ++p) {
  238. paths.AddUserPath(*p);
  239. }
  240. paths.AddSuffixes(this->SearchPathSuffixes);
  241. }
  242. void cmFindBase::PrintFindStuff()
  243. {
  244. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  245. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  246. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  247. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  248. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  249. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  250. std::cerr << "VariableName " << this->VariableName << "\n";
  251. std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
  252. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  253. std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath
  254. << "\n";
  255. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  256. std::cerr << "NoSystemEnvironmentPath " << this->NoSystemEnvironmentPath
  257. << "\n";
  258. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  259. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  260. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  261. std::cerr << "Names " << cmJoin(this->Names, " ") << "\n";
  262. std::cerr << "\n";
  263. std::cerr << "SearchPathSuffixes ";
  264. std::cerr << cmJoin(this->SearchPathSuffixes, "\n") << "\n";
  265. std::cerr << "SearchPaths\n";
  266. std::cerr << cmWrap("[", this->SearchPaths, "]", "\n") << "\n";
  267. }
  268. bool cmFindBase::CheckForVariableInCache()
  269. {
  270. if (const char* cacheValue =
  271. this->Makefile->GetDefinition(this->VariableName)) {
  272. cmState* state = this->Makefile->GetState();
  273. const char* cacheEntry = state->GetCacheEntryValue(this->VariableName);
  274. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  275. bool cached = cacheEntry ? true : false;
  276. if (found) {
  277. // If the user specifies the entry on the command line without a
  278. // type we should add the type and docstring but keep the
  279. // original value. Tell the subclass implementations to do
  280. // this.
  281. if (cached &&
  282. state->GetCacheEntryType(this->VariableName) ==
  283. cmStateEnums::UNINITIALIZED) {
  284. this->AlreadyInCacheWithoutMetaInfo = true;
  285. }
  286. return true;
  287. }
  288. if (cached) {
  289. const char* hs =
  290. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
  291. this->VariableDocumentation = hs ? hs : "(none)";
  292. }
  293. }
  294. return false;
  295. }