cmFindBase.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. cmFindBase::cmFindBase()
  12. {
  13. this->AlreadyInCache = false;
  14. this->AlreadyInCacheWithoutMetaInfo = false;
  15. this->NamesPerDir = false;
  16. this->NamesPerDirAllowed = false;
  17. }
  18. //----------------------------------------------------------------------------
  19. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  20. {
  21. if(argsIn.size() < 2 )
  22. {
  23. this->SetError("called with incorrect number of arguments");
  24. return false;
  25. }
  26. // copy argsIn into args so it can be modified,
  27. // in the process extract the DOC "documentation"
  28. size_t size = argsIn.size();
  29. std::vector<std::string> args;
  30. bool foundDoc = false;
  31. for(unsigned int j = 0; j < size; ++j)
  32. {
  33. if(foundDoc || argsIn[j] != "DOC" )
  34. {
  35. if(argsIn[j] == "ENV")
  36. {
  37. if(j+1 < size)
  38. {
  39. j++;
  40. cmSystemTools::GetPath(args, argsIn[j].c_str());
  41. }
  42. }
  43. else
  44. {
  45. args.push_back(argsIn[j]);
  46. }
  47. }
  48. else
  49. {
  50. if(j+1 < size)
  51. {
  52. foundDoc = true;
  53. this->VariableDocumentation = argsIn[j+1];
  54. j++;
  55. if(j >= size)
  56. {
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. if(args.size() < 2 )
  63. {
  64. this->SetError("called with incorrect number of arguments");
  65. return false;
  66. }
  67. this->VariableName = args[0];
  68. if(this->CheckForVariableInCache())
  69. {
  70. this->AlreadyInCache = true;
  71. return true;
  72. }
  73. this->AlreadyInCache = false;
  74. // Find the current root path mode.
  75. this->SelectDefaultRootPathMode();
  76. // Find the current bundle/framework search policy.
  77. this->SelectDefaultMacMode();
  78. bool newStyle = false;
  79. enum Doing { DoingNone, DoingNames, DoingPaths, DoingPathSuffixes,
  80. DoingHints };
  81. Doing doing = DoingNames; // assume it starts with a name
  82. for (unsigned int j = 1; j < args.size(); ++j)
  83. {
  84. if(args[j] == "NAMES")
  85. {
  86. doing = DoingNames;
  87. newStyle = true;
  88. }
  89. else if (args[j] == "PATHS")
  90. {
  91. doing = DoingPaths;
  92. newStyle = true;
  93. }
  94. else if (args[j] == "HINTS")
  95. {
  96. doing = DoingHints;
  97. newStyle = true;
  98. }
  99. else if (args[j] == "PATH_SUFFIXES")
  100. {
  101. doing = DoingPathSuffixes;
  102. newStyle = true;
  103. }
  104. else if (args[j] == "NAMES_PER_DIR")
  105. {
  106. doing = DoingNone;
  107. if(this->NamesPerDirAllowed)
  108. {
  109. this->NamesPerDir = true;
  110. }
  111. else
  112. {
  113. this->SetError("does not support NAMES_PER_DIR");
  114. return false;
  115. }
  116. }
  117. else if (args[j] == "NO_SYSTEM_PATH")
  118. {
  119. doing = DoingNone;
  120. this->NoDefaultPath = true;
  121. }
  122. else if (this->CheckCommonArgument(args[j]))
  123. {
  124. doing = DoingNone;
  125. // Some common arguments were accidentally supported by CMake
  126. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  127. // must support it even though it is not documented.
  128. }
  129. else if(doing == DoingNames)
  130. {
  131. this->Names.push_back(args[j]);
  132. }
  133. else if(doing == DoingPaths)
  134. {
  135. this->UserGuessArgs.push_back(args[j]);
  136. }
  137. else if(doing == DoingHints)
  138. {
  139. this->UserHintsArgs.push_back(args[j]);
  140. }
  141. else if(doing == DoingPathSuffixes)
  142. {
  143. this->AddPathSuffix(args[j]);
  144. }
  145. }
  146. if(this->VariableDocumentation.size() == 0)
  147. {
  148. this->VariableDocumentation = "Where can ";
  149. if(this->Names.size() == 0)
  150. {
  151. this->VariableDocumentation += "the (unknown) library be found";
  152. }
  153. else if(this->Names.size() == 1)
  154. {
  155. this->VariableDocumentation += "the "
  156. + this->Names[0] + " library be found";
  157. }
  158. else
  159. {
  160. this->VariableDocumentation += "one of the " + this->Names[0];
  161. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  162. {
  163. this->VariableDocumentation += ", " + this->Names[j];
  164. }
  165. this->VariableDocumentation += " or "
  166. + this->Names[this->Names.size() - 1] + " libraries be found";
  167. }
  168. }
  169. // look for old style
  170. // FIND_*(VAR name path1 path2 ...)
  171. if(!newStyle)
  172. {
  173. // All the short-hand arguments have been recorded as names.
  174. std::vector<std::string> shortArgs = this->Names;
  175. this->Names.clear(); // clear out any values in Names
  176. this->Names.push_back(shortArgs[0]);
  177. this->UserGuessArgs.insert(this->UserGuessArgs.end(),
  178. shortArgs.begin() + 1, shortArgs.end());
  179. }
  180. this->ExpandPaths();
  181. this->ComputeFinalPaths();
  182. return true;
  183. }
  184. void cmFindBase::ExpandPaths()
  185. {
  186. if(!this->NoDefaultPath)
  187. {
  188. if(!this->NoCMakePath)
  189. {
  190. this->FillCMakeVariablePath();
  191. }
  192. if(!this->NoCMakeEnvironmentPath)
  193. {
  194. this->FillCMakeEnvironmentPath();
  195. }
  196. if(!this->NoSystemEnvironmentPath)
  197. {
  198. this->FillSystemEnvironmentPath();
  199. }
  200. if(!this->NoCMakeSystemPath)
  201. {
  202. this->FillCMakeSystemVariablePath();
  203. }
  204. }
  205. this->FillUserHintsPath();
  206. this->FillUserGuessPath();
  207. }
  208. //----------------------------------------------------------------------------
  209. void cmFindBase::FillCMakeEnvironmentPath()
  210. {
  211. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  212. // Add CMAKE_*_PATH environment variables
  213. std::string var = "CMAKE_";
  214. var += this->CMakePathName;
  215. var += "_PATH";
  216. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  217. paths.AddEnvPath(var);
  218. if(this->CMakePathName == "PROGRAM")
  219. {
  220. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  221. }
  222. else
  223. {
  224. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  225. }
  226. paths.AddSuffixes(this->SearchPathSuffixes);
  227. }
  228. //----------------------------------------------------------------------------
  229. void cmFindBase::FillCMakeVariablePath()
  230. {
  231. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMake];
  232. // Add CMake varibles of the same name as the previous environment
  233. // varibles CMAKE_*_PATH to be used most of the time with -D
  234. // command line options
  235. std::string var = "CMAKE_";
  236. var += this->CMakePathName;
  237. var += "_PATH";
  238. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  239. paths.AddCMakePath(var);
  240. if(this->CMakePathName == "PROGRAM")
  241. {
  242. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  243. }
  244. else
  245. {
  246. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  247. }
  248. paths.AddSuffixes(this->SearchPathSuffixes);
  249. }
  250. //----------------------------------------------------------------------------
  251. void cmFindBase::FillSystemEnvironmentPath()
  252. {
  253. cmSearchPath &paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  254. // Add LIB or INCLUDE
  255. if(!this->EnvironmentPath.empty())
  256. {
  257. paths.AddEnvPath(this->EnvironmentPath);
  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 ";
  326. for(unsigned int i =0; i < this->Names.size(); ++i)
  327. {
  328. std::cerr << this->Names[i] << " ";
  329. }
  330. std::cerr << "\n";
  331. std::cerr << "\n";
  332. std::cerr << "SearchPathSuffixes ";
  333. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  334. {
  335. std::cerr << this->SearchPathSuffixes[i] << "\n";
  336. }
  337. std::cerr << "\n";
  338. std::cerr << "SearchPaths\n";
  339. for(std::vector<std::string>::const_iterator i = this->SearchPaths.begin();
  340. i != this->SearchPaths.end(); ++i)
  341. {
  342. std::cerr << "[" << *i << "]\n";
  343. }
  344. }
  345. bool cmFindBase::CheckForVariableInCache()
  346. {
  347. if(const char* cacheValue =
  348. this->Makefile->GetDefinition(this->VariableName))
  349. {
  350. cmCacheManager::CacheIterator it =
  351. this->Makefile->GetCacheManager()->
  352. GetCacheIterator(this->VariableName.c_str());
  353. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  354. bool cached = !it.IsAtEnd();
  355. if(found)
  356. {
  357. // If the user specifies the entry on the command line without a
  358. // type we should add the type and docstring but keep the
  359. // original value. Tell the subclass implementations to do
  360. // this.
  361. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  362. {
  363. this->AlreadyInCacheWithoutMetaInfo = true;
  364. }
  365. return true;
  366. }
  367. else if(cached)
  368. {
  369. const char* hs = it.GetProperty("HELPSTRING");
  370. this->VariableDocumentation = hs?hs:"(none)";
  371. }
  372. }
  373. return false;
  374. }