cmFindBase.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFindBase.h"
  14. cmFindBase::cmFindBase()
  15. {
  16. this->AlreadyInCache = false;
  17. this->NoDefaultPath = false;
  18. this->NoCMakePath = false;
  19. this->NoCMakeEnvironmentPath = false;
  20. this->NoSystemEnvironmentPath = false;
  21. this->NoCMakeSystemPath = false;
  22. // default is to search frameworks first on apple
  23. #if defined(__APPLE__)
  24. this->SearchFrameworkFirst = true;
  25. #else
  26. this->SearchFrameworkFirst = false;
  27. #endif
  28. this->SearchFrameworkOnly = false;
  29. this->SearchFrameworkLast = false;
  30. this->GenericDocumentation =
  31. " FIND_XXX(<VAR> name1 path1 path2 ...)\n"
  32. "This is the short-hand signature for the command that "
  33. "is sufficient in many cases. It is the same "
  34. "as FIND_XXX(<VAR> name1 PATHS path2 path2 ...)\n"
  35. " FIND_XXX(\n"
  36. " <VAR> \n"
  37. " name | NAMES name1 [name2 ...]\n"
  38. " PATHS path1 [path2 ... ENV var]\n"
  39. " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
  40. " [DOC \"cache documentation string\"]\n"
  41. " [NO_DEFAULT_PATH]\n"
  42. " [NO_CMAKE_ENVIRONMENT_PATH]\n"
  43. " [NO_CMAKE_PATH]\n"
  44. " [NO_SYSTEM_ENVIRONMENT_PATH]\n"
  45. " [NO_CMAKE_SYSTEM_PATH]\n"
  46. " )\n"
  47. ""
  48. "This command is used to find a SEARCH_XXX_DESC. "
  49. "A cache entry named by <VAR> is created to store the result "
  50. "of this command. If nothing is found, the result will be "
  51. "<VAR>-NOTFOUND. The name of the SEARCH_XXX that "
  52. "is searched for is specified by the names listed "
  53. "after the NAMES argument. Additional search locations "
  54. "can be specified after the PATHS argument. If ENV var is "
  55. "found in the PATHS section the environment variable var "
  56. "will be read and converted from a system environment variable to "
  57. "a cmake style list of paths. For example ENV PATH would be a way "
  58. "to list the system path variable. The argument "
  59. "after DOC will be used for the documentation string in "
  60. "the cache. PATH_SUFFIXES can be used to give sub directories "
  61. "that will be appended to the search paths.\n"
  62. "If NO_DEFAULT_PATH is specified, then no additional paths are "
  63. "added to the search. "
  64. "If NO_DEFAULT_PATH is not specified, the search process is as follows:\n"
  65. "1. Search cmake specific environment variables. This "
  66. "can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
  67. ""
  68. " CMAKE_FRAMEWORK_PATH\n"
  69. " CMAKE_XXX_PATH\n"
  70. "2. Search cmake variables with the same names as "
  71. "the cmake specific environment variables. These "
  72. "are intended to be used on the command line with a "
  73. "-DVAR=value. This can be skipped if NO_CMAKE_PATH "
  74. "is passed.\n"
  75. ""
  76. " CMAKE_FRAMEWORK_PATH\n"
  77. " CMAKE_XXX_PATH\n"
  78. "3. Search the standard system environment variables. "
  79. "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is an argument.\n"
  80. " PATH\n"
  81. " XXX_SYSTEM\n" // replace with "", LIB, or INCLUDE
  82. "4. Search cmake variables defined in the Platform files "
  83. "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
  84. "is passed.\n"
  85. " CMAKE_SYSTEM_FRAMEWORK_PATH\n"
  86. " CMAKE_SYSTEM_XXX_PATH\n"
  87. "5. Search the paths specified after PATHS or in the short-hand version "
  88. "of the command.\n"
  89. "On Darwin or systems supporting OSX Frameworks, the cmake variable"
  90. " CMAKE_FIND_FRAMEWORK can be set to empty or one of the following:\n"
  91. " \"FIRST\" - Try to find frameworks before standard\n"
  92. " libraries or headers. This is the default on Darwin.\n"
  93. " \"LAST\" - Try to find frameworks after standard\n"
  94. " libraries or headers.\n"
  95. " \"ONLY\" - Only try to find frameworks.\n"
  96. " \"NEVER\". - Never try to find frameworks.\n"
  97. "The reason the paths listed in the call to the command are searched "
  98. "last is that most users of CMake would expect things to be found "
  99. "first in the locations specified by their environment. Projects may "
  100. "override this behavior by simply calling the command twice:\n"
  101. " FIND_XXX(<VAR> NAMES name PATHS paths NO_DEFAULT_PATH)\n"
  102. " FIND_XXX(<VAR> NAMES name)\n"
  103. "Once one of these calls succeeds the result variable will be set "
  104. "and stored in the cache so that neither call will search again.";
  105. }
  106. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  107. {
  108. if(argsIn.size() < 2 )
  109. {
  110. this->SetError("called with incorrect number of arguments");
  111. return false;
  112. }
  113. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  114. if(ff == "NEVER")
  115. {
  116. this->SearchFrameworkLast = false;
  117. this->SearchFrameworkFirst = false;
  118. this->SearchFrameworkOnly = false;
  119. }
  120. else if (ff == "ONLY")
  121. {
  122. this->SearchFrameworkLast = false;
  123. this->SearchFrameworkFirst = false;
  124. this->SearchFrameworkOnly = true;
  125. }
  126. else if (ff == "FIRST")
  127. {
  128. this->SearchFrameworkLast = false;
  129. this->SearchFrameworkFirst = true;
  130. this->SearchFrameworkOnly = false;
  131. }
  132. else if (ff == "LAST")
  133. {
  134. this->SearchFrameworkLast = true;
  135. this->SearchFrameworkFirst = false;
  136. this->SearchFrameworkOnly = false;
  137. }
  138. // CMake versions below 2.3 did not search all these extra
  139. // locations. Preserve compatibility unless a modern argument is
  140. // passed.
  141. bool compatibility = false;
  142. const char* versionValue =
  143. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  144. int major = 0;
  145. int minor = 0;
  146. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  147. {
  148. versionValue = 0;
  149. }
  150. if(versionValue && (major < 2 || major == 2 && minor < 3))
  151. {
  152. compatibility = true;
  153. }
  154. // copy argsIn into args so it can be modified,
  155. // in the process extract the DOC "documentation"
  156. size_t size = argsIn.size();
  157. std::vector<std::string> args;
  158. bool foundDoc = false;
  159. for(unsigned int j = 0; j < size; ++j)
  160. {
  161. if(foundDoc || argsIn[j] != "DOC" )
  162. {
  163. if(argsIn[j] == "ENV")
  164. {
  165. if(j+1 < size)
  166. {
  167. j++;
  168. cmSystemTools::GetPath(args, argsIn[j].c_str());
  169. }
  170. }
  171. else
  172. {
  173. args.push_back(argsIn[j]);
  174. }
  175. }
  176. else
  177. {
  178. if(j+1 < size)
  179. {
  180. foundDoc = true;
  181. this->VariableDocumentation = argsIn[j+1];
  182. j++;
  183. if(j >= size)
  184. {
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. this->VariableName = args[0];
  191. if(this->CheckForVariableInCache())
  192. {
  193. this->AlreadyInCache = true;
  194. return true;
  195. }
  196. this->AlreadyInCache = false;
  197. std::vector<std::string> userPaths;
  198. std::string doc;
  199. bool doingNames = true; // assume it starts with a name
  200. bool doingPaths = false;
  201. bool doingPathSuf = false;
  202. bool newStyle = false;
  203. for (unsigned int j = 1; j < args.size(); ++j)
  204. {
  205. if(args[j] == "NAMES")
  206. {
  207. doingNames = true;
  208. newStyle = true;
  209. doingPathSuf = false;
  210. doingPaths = false;
  211. }
  212. else if (args[j] == "PATHS")
  213. {
  214. doingPaths = true;
  215. newStyle = true;
  216. doingNames = false;
  217. doingPathSuf = false;
  218. }
  219. else if (args[j] == "PATH_SUFFIXES")
  220. {
  221. compatibility = false;
  222. doingPathSuf = true;
  223. newStyle = true;
  224. doingNames = false;
  225. doingPaths = false;
  226. }
  227. else if (args[j] == "NO_SYSTEM_PATH")
  228. {
  229. doingPaths = false;
  230. doingPathSuf = false;
  231. doingNames = false;
  232. this->NoDefaultPath = true;
  233. }
  234. else if (args[j] == "NO_DEFAULT_PATH")
  235. {
  236. compatibility = false;
  237. doingPaths = false;
  238. doingPathSuf = false;
  239. doingNames = false;
  240. this->NoDefaultPath = true;
  241. }
  242. else if (args[j] == "NO_CMAKE_ENVIRONMENT_PATH")
  243. {
  244. compatibility = false;
  245. doingPaths = false;
  246. doingPathSuf = false;
  247. doingNames = false;
  248. this->NoCMakeEnvironmentPath = true;
  249. }
  250. else if (args[j] == "NO_CMAKE_PATH")
  251. {
  252. compatibility = false;
  253. doingPaths = false;
  254. doingPathSuf = false;
  255. doingNames = false;
  256. this->NoCMakePath = true;
  257. }
  258. else if (args[j] == "NO_SYSTEM_ENVIRONMENT_PATH")
  259. {
  260. compatibility = false;
  261. doingPaths = false;
  262. doingPathSuf = false;
  263. doingNames = false;
  264. this->NoSystemEnvironmentPath = true;
  265. }
  266. else if (args[j] == "NO_CMAKE_SYSTEM_PATH")
  267. {
  268. compatibility = false;
  269. doingPaths = false;
  270. doingPathSuf = false;
  271. doingNames = false;
  272. this->NoCMakeSystemPath = true;
  273. }
  274. else
  275. {
  276. if(doingNames)
  277. {
  278. this->Names.push_back(args[j]);
  279. }
  280. else if(doingPaths)
  281. {
  282. userPaths.push_back(args[j]);
  283. }
  284. else if(doingPathSuf)
  285. {
  286. this->SearchPathSuffixes.push_back(args[j]);
  287. }
  288. }
  289. }
  290. // Now that arguments have been parsed check the compatibility
  291. // setting. If we need to be compatible with CMake 2.2 and earlier
  292. // do not add the CMake system paths. It is safe to add the CMake
  293. // environment paths and system environment paths because that
  294. // existed in 2.2. It is safe to add the CMake user variable paths
  295. // because the user or project has explicitly set them.
  296. if(compatibility)
  297. {
  298. this->NoCMakeSystemPath = true;
  299. }
  300. if(this->VariableDocumentation.size() == 0)
  301. {
  302. this->VariableDocumentation = "Whare can ";
  303. if(this->Names.size() == 0)
  304. {
  305. this->VariableDocumentation += "the (unknown) library be found";
  306. }
  307. else if(this->Names.size() == 1)
  308. {
  309. this->VariableDocumentation += "the " + this->Names[0] + " library be found";
  310. }
  311. else
  312. {
  313. this->VariableDocumentation += "one of the " + this->Names[0];
  314. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  315. {
  316. this->VariableDocumentation += ", " + this->Names[j];
  317. }
  318. this->VariableDocumentation += " or " + this->Names[this->Names.size() - 1] + " libraries be found";
  319. }
  320. }
  321. // look for old style
  322. // FIND_*(VAR name path1 path2 ...)
  323. if(!newStyle)
  324. {
  325. this->Names.clear(); // clear out any values in Names
  326. this->Names.push_back(args[1]);
  327. for(unsigned int j = 2; j < args.size(); ++j)
  328. {
  329. userPaths.push_back(args[j]);
  330. }
  331. }
  332. this->ExpandPaths(userPaths);
  333. return true;
  334. }
  335. void cmFindBase::ExpandPaths(std::vector<std::string> userPaths)
  336. {
  337. // if NO Default paths was not specified add the
  338. // standard search paths.
  339. if(!this->NoDefaultPath)
  340. {
  341. if(!this->NoCMakeEnvironmentPath)
  342. {
  343. // Add CMAKE_*_PATH environment variables
  344. this->AddEnvironmentVairables();
  345. }
  346. if(!this->NoCMakePath)
  347. {
  348. // Add CMake varibles of the same name as the previous environment
  349. // varibles CMAKE_*_PATH to be used most of the time with -D
  350. // command line options
  351. this->AddCMakeVairables();
  352. }
  353. if(!this->NoSystemEnvironmentPath)
  354. {
  355. // add System environment PATH and (LIB or INCLUDE)
  356. this->AddSystemEnvironmentVairables();
  357. }
  358. if(!this->NoCMakeSystemPath)
  359. {
  360. // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
  361. this->AddCMakeSystemVariables();
  362. }
  363. }
  364. // add the paths specified in the FIND_* call
  365. for(unsigned int i =0; i < userPaths.size(); ++i)
  366. {
  367. this->SearchPaths.push_back(userPaths[i]);
  368. }
  369. // clean things up
  370. this->ExpandRegistryAndCleanPath();
  371. }
  372. void cmFindBase::AddEnvironmentVairables()
  373. {
  374. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  375. {
  376. cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
  377. }
  378. std::string var = "CMAKE_";
  379. var += this->CMakePathName;
  380. var += "_PATH";
  381. cmSystemTools::GetPath(this->SearchPaths, var.c_str());
  382. if(this->SearchFrameworkLast)
  383. {
  384. cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
  385. }
  386. }
  387. void cmFindBase::AddCMakeVairables()
  388. {
  389. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  390. {
  391. if(const char* path =
  392. this->Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
  393. {
  394. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  395. }
  396. }
  397. std::string var = "CMAKE_";
  398. var += this->CMakePathName;
  399. var += "_PATH";
  400. if(const char* path = this->Makefile->GetDefinition(var.c_str()))
  401. {
  402. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  403. }
  404. if(this->SearchFrameworkLast)
  405. {
  406. if(const char* path =
  407. this->Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
  408. {
  409. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  410. }
  411. }
  412. }
  413. void cmFindBase::AddSystemEnvironmentVairables()
  414. {
  415. // Add LIB or INCLUDE
  416. if(this->EnvironmentPath.size())
  417. {
  418. cmSystemTools::GetPath(this->SearchPaths, this->EnvironmentPath.c_str());
  419. }
  420. // Add PATH
  421. cmSystemTools::GetPath(this->SearchPaths);
  422. }
  423. void cmFindBase::AddCMakeSystemVariables()
  424. {
  425. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  426. {
  427. if(const char* path = this->Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
  428. {
  429. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  430. }
  431. }
  432. std::string var = "CMAKE_SYSTEM_";
  433. var += this->CMakePathName;
  434. var += "_PATH";
  435. if(const char* path = this->Makefile->GetDefinition(var.c_str()))
  436. {
  437. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  438. }
  439. if(this->SearchFrameworkLast)
  440. {
  441. if(const char* path = this->Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
  442. {
  443. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  444. }
  445. }
  446. }
  447. void cmFindBase::ExpandRegistryAndCleanPath()
  448. {
  449. std::vector<std::string> finalPath;
  450. std::vector<std::string>::iterator i;
  451. for(i = this->SearchPaths.begin();
  452. i != this->SearchPaths.end(); ++i)
  453. {
  454. cmSystemTools::ExpandRegistryValues(*i);
  455. cmSystemTools::GlobDirs(i->c_str(), finalPath);
  456. }
  457. this->SearchPaths.clear();
  458. // convert all paths to unix slashes
  459. for(i = finalPath.begin();
  460. i != finalPath.end(); ++i)
  461. {
  462. cmSystemTools::ConvertToUnixSlashes(*i);
  463. // copy each finalPath combined with SearchPathSuffixes
  464. // to the SearchPaths ivar
  465. for(std::vector<std::string>::iterator j = this->SearchPathSuffixes.begin();
  466. j != this->SearchPathSuffixes.end(); ++j)
  467. {
  468. std::string p = *i + std::string("/") + *j;
  469. if(cmSystemTools::FileIsDirectory(p.c_str()))
  470. {
  471. this->SearchPaths.push_back(p);
  472. }
  473. }
  474. }
  475. // now put the path without the path suffixes in the SearchPaths
  476. for(i = finalPath.begin();
  477. i != finalPath.end(); ++i)
  478. {
  479. if(cmSystemTools::FileIsDirectory(i->c_str()))
  480. {
  481. this->SearchPaths.push_back(*i);
  482. }
  483. }
  484. }
  485. void cmFindBase::PrintFindStuff()
  486. {
  487. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  488. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  489. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  490. std::cerr << "VariableName " << this->VariableName << "\n";
  491. std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
  492. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  493. std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath << "\n";
  494. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  495. std::cerr << "NoSystemEnvironmentPath " << this->NoSystemEnvironmentPath << "\n";
  496. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  497. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  498. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  499. std::cerr << "Names ";
  500. for(unsigned int i =0; i < this->Names.size(); ++i)
  501. {
  502. std::cerr << this->Names[i] << " ";
  503. }
  504. std::cerr << "\n";
  505. std::cerr << "\n";
  506. std::cerr << "SearchPathSuffixes ";
  507. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  508. {
  509. std::cerr << this->SearchPathSuffixes[i] << "\n";
  510. }
  511. std::cerr << "\n";
  512. std::cerr << "SearchPaths\n";
  513. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  514. {
  515. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  516. }
  517. }
  518. bool cmFindBase::CheckForVariableInCache()
  519. {
  520. const char* cacheValue
  521. = this->Makefile->GetDefinition(this->VariableName.c_str());
  522. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  523. {
  524. return true;
  525. }
  526. if(cacheValue)
  527. {
  528. cmCacheManager::CacheIterator it =
  529. this->Makefile->GetCacheManager()->GetCacheIterator(this->VariableName.c_str());
  530. if(!it.IsAtEnd())
  531. {
  532. const char* hs = it.GetProperty("HELPSTRING");
  533. this->VariableDocumentation = hs?hs:"(none)";
  534. }
  535. }
  536. return false;
  537. }