cmFindBase.cxx 11 KB

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