cmFindBase.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. for(unsigned int j = 1; j < shortArgs.size(); ++j)
  178. {
  179. this->UserGuessArgs.push_back(shortArgs[j]);
  180. }
  181. }
  182. this->ExpandPaths();
  183. this->ComputeFinalPaths();
  184. return true;
  185. }
  186. void cmFindBase::ExpandPaths()
  187. {
  188. if(!this->NoDefaultPath)
  189. {
  190. if(!this->NoCMakePath)
  191. {
  192. this->FillCMakeVariablePath();
  193. }
  194. if(!this->NoCMakeEnvironmentPath)
  195. {
  196. this->FillCMakeEnvironmentPath();
  197. }
  198. if(!this->NoSystemEnvironmentPath)
  199. {
  200. this->FillSystemEnvironmentPath();
  201. }
  202. if(!this->NoCMakeSystemPath)
  203. {
  204. this->FillCMakeSystemVariablePath();
  205. }
  206. }
  207. this->FillUserHintsPath();
  208. this->FillUserGuessPath();
  209. }
  210. //----------------------------------------------------------------------------
  211. void cmFindBase::FillCMakeEnvironmentPath()
  212. {
  213. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  214. // Add CMAKE_*_PATH environment variables
  215. std::string var = "CMAKE_";
  216. var += this->CMakePathName;
  217. var += "_PATH";
  218. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  219. paths.AddEnvPath(var);
  220. if(this->CMakePathName == "PROGRAM")
  221. {
  222. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  223. }
  224. else
  225. {
  226. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  227. }
  228. paths.AddSuffixes(this->SearchPathSuffixes);
  229. }
  230. //----------------------------------------------------------------------------
  231. void cmFindBase::FillCMakeVariablePath()
  232. {
  233. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMake];
  234. // Add CMake varibles of the same name as the previous environment
  235. // varibles CMAKE_*_PATH to be used most of the time with -D
  236. // command line options
  237. std::string var = "CMAKE_";
  238. var += this->CMakePathName;
  239. var += "_PATH";
  240. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  241. paths.AddCMakePath(var);
  242. if(this->CMakePathName == "PROGRAM")
  243. {
  244. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  245. }
  246. else
  247. {
  248. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  249. }
  250. paths.AddSuffixes(this->SearchPathSuffixes);
  251. }
  252. //----------------------------------------------------------------------------
  253. void cmFindBase::FillSystemEnvironmentPath()
  254. {
  255. cmSearchPath &paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  256. // Add LIB or INCLUDE
  257. if(!this->EnvironmentPath.empty())
  258. {
  259. paths.AddEnvPath(this->EnvironmentPath);
  260. }
  261. // Add PATH
  262. paths.AddEnvPath("PATH");
  263. paths.AddSuffixes(this->SearchPathSuffixes);
  264. }
  265. //----------------------------------------------------------------------------
  266. void cmFindBase::FillCMakeSystemVariablePath()
  267. {
  268. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMakeSystem];
  269. std::string var = "CMAKE_SYSTEM_";
  270. var += this->CMakePathName;
  271. var += "_PATH";
  272. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  273. paths.AddCMakePath(var);
  274. if(this->CMakePathName == "PROGRAM")
  275. {
  276. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  277. }
  278. else
  279. {
  280. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  281. }
  282. paths.AddSuffixes(this->SearchPathSuffixes);
  283. }
  284. //----------------------------------------------------------------------------
  285. void cmFindBase::FillUserHintsPath()
  286. {
  287. cmSearchPath &paths = this->LabeledPaths[PathLabel::Hints];
  288. for(std::vector<std::string>::const_iterator p = this->UserHintsArgs.begin();
  289. p != this->UserHintsArgs.end(); ++p)
  290. {
  291. paths.AddUserPath(*p);
  292. }
  293. paths.AddSuffixes(this->SearchPathSuffixes);
  294. }
  295. //----------------------------------------------------------------------------
  296. void cmFindBase::FillUserGuessPath()
  297. {
  298. cmSearchPath &paths = this->LabeledPaths[PathLabel::Guess];
  299. for(std::vector<std::string>::const_iterator p = this->UserGuessArgs.begin();
  300. p != this->UserGuessArgs.end(); ++p)
  301. {
  302. paths.AddUserPath(*p);
  303. }
  304. paths.AddSuffixes(this->SearchPathSuffixes);
  305. }
  306. //----------------------------------------------------------------------------
  307. void cmFindBase::PrintFindStuff()
  308. {
  309. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  310. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  311. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  312. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  313. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  314. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  315. std::cerr << "VariableName " << this->VariableName << "\n";
  316. std::cerr << "VariableDocumentation "
  317. << this->VariableDocumentation << "\n";
  318. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  319. std::cerr << "NoCMakeEnvironmentPath "
  320. << this->NoCMakeEnvironmentPath << "\n";
  321. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  322. std::cerr << "NoSystemEnvironmentPath "
  323. << this->NoSystemEnvironmentPath << "\n";
  324. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  325. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  326. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  327. std::cerr << "Names ";
  328. for(unsigned int i =0; i < this->Names.size(); ++i)
  329. {
  330. std::cerr << this->Names[i] << " ";
  331. }
  332. std::cerr << "\n";
  333. std::cerr << "\n";
  334. std::cerr << "SearchPathSuffixes ";
  335. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  336. {
  337. std::cerr << this->SearchPathSuffixes[i] << "\n";
  338. }
  339. std::cerr << "\n";
  340. std::cerr << "SearchPaths\n";
  341. for(std::vector<std::string>::const_iterator i = this->SearchPaths.begin();
  342. i != this->SearchPaths.end(); ++i)
  343. {
  344. std::cerr << "[" << *i << "]\n";
  345. }
  346. }
  347. bool cmFindBase::CheckForVariableInCache()
  348. {
  349. if(const char* cacheValue =
  350. this->Makefile->GetDefinition(this->VariableName))
  351. {
  352. cmCacheManager::CacheIterator it =
  353. this->Makefile->GetCacheManager()->
  354. GetCacheIterator(this->VariableName.c_str());
  355. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  356. bool cached = !it.IsAtEnd();
  357. if(found)
  358. {
  359. // If the user specifies the entry on the command line without a
  360. // type we should add the type and docstring but keep the
  361. // original value. Tell the subclass implementations to do
  362. // this.
  363. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  364. {
  365. this->AlreadyInCacheWithoutMetaInfo = true;
  366. }
  367. return true;
  368. }
  369. else if(cached)
  370. {
  371. const char* hs = it.GetProperty("HELPSTRING");
  372. this->VariableDocumentation = hs?hs:"(none)";
  373. }
  374. }
  375. return false;
  376. }