cmFindPackageCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 "cmFindPackageCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. #ifdef CMAKE_BUILD_WITH_CMAKE
  16. #include "cmVariableWatch.h"
  17. #endif
  18. void cmFindPackageNeedBackwardsCompatibility(const std::string& variable,
  19. int access_type, void*, const char* newValue,
  20. const cmMakefile*)
  21. {
  22. (void)newValue;
  23. #ifdef CMAKE_BUILD_WITH_CMAKE
  24. if(access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  25. {
  26. std::string message = "An attempt was made to access a variable: ";
  27. message += variable;
  28. message +=
  29. " that has not been defined. This variable is created by the "
  30. "FIND_PACKAGE command. CMake version 1.6 always converted the "
  31. "variable name to upper-case, but this behavior is no longer the "
  32. "case. To fix this you might need to set the cache value of "
  33. "CMAKE_BACKWARDS_COMPATIBILITY to 1.6 or less. If you are writing a "
  34. "CMake listfile, you should change the variable reference to use "
  35. "the case of the argument to FIND_PACKAGE.";
  36. cmSystemTools::Error(message.c_str());
  37. }
  38. #else
  39. (void)variable;
  40. (void)access_type;
  41. #endif
  42. }
  43. //----------------------------------------------------------------------------
  44. bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
  45. {
  46. if(args.size() < 1)
  47. {
  48. this->SetError("called with incorrect number of arguments");
  49. return false;
  50. }
  51. // Record options.
  52. this->Name = args[0];
  53. bool quiet = false;
  54. bool required = false;
  55. bool no_module = false;
  56. std::string components;
  57. const char* components_sep = "";
  58. // Parse the arguments.
  59. bool doing_components = false;
  60. cmsys::RegularExpression version("^[0-9.]+$");
  61. bool haveVersion = false;
  62. for(unsigned int i=1; i < args.size(); ++i)
  63. {
  64. if(args[i] == "QUIET")
  65. {
  66. quiet = true;
  67. doing_components = false;
  68. }
  69. else if(args[i] == "NO_MODULE")
  70. {
  71. no_module = true;
  72. doing_components = false;
  73. }
  74. else if(args[i] == "REQUIRED")
  75. {
  76. required = true;
  77. doing_components = true;
  78. }
  79. else if(args[i] == "COMPONENTS")
  80. {
  81. doing_components = true;
  82. }
  83. else if(doing_components)
  84. {
  85. // Set a variable telling the find script this component
  86. // is required.
  87. std::string req_var = Name + "_FIND_REQUIRED_" + args[i];
  88. this->Makefile->AddDefinition(req_var.c_str(), "1");
  89. // Append to the list of required components.
  90. components += components_sep;
  91. components += args[i];
  92. components_sep = ";";
  93. }
  94. else if(!haveVersion && version.find(args[i].c_str()))
  95. {
  96. haveVersion = true;
  97. }
  98. else
  99. {
  100. cmOStringStream e;
  101. e << "called with invalid argument \"" << args[i].c_str() << "\"";
  102. this->SetError(e.str().c_str());
  103. return false;
  104. }
  105. }
  106. // Store the list of components.
  107. std::string components_var = Name + "_FIND_COMPONENTS";
  108. this->Makefile->AddDefinition(components_var.c_str(), components.c_str());
  109. // See if there is a Find<name>.cmake module.
  110. if(!no_module)
  111. {
  112. bool foundModule = false;
  113. if(!this->FindModule(foundModule, quiet, required))
  114. {
  115. this->AppendSuccessInformation(quiet);
  116. return false;
  117. }
  118. if(foundModule)
  119. {
  120. this->AppendSuccessInformation(quiet);
  121. return true;
  122. }
  123. }
  124. // No find module. Assume the project has a CMake config file. Use
  125. // a <NAME>_DIR cache variable to locate it.
  126. this->Variable = this->Name;
  127. this->Variable += "_DIR";
  128. this->Config = this->Name;
  129. this->Config += "Config.cmake";
  130. // Support old capitalization behavior.
  131. std::string upperDir = cmSystemTools::UpperCase(this->Name);
  132. std::string upperFound = cmSystemTools::UpperCase(this->Name);
  133. upperDir += "_DIR";
  134. upperFound += "_FOUND";
  135. bool needCompatibility = false;
  136. if(!(upperDir == this->Variable))
  137. {
  138. const char* versionValue =
  139. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  140. if(atof(versionValue) < 1.7)
  141. {
  142. needCompatibility = true;
  143. }
  144. }
  145. // Try to find the config file.
  146. const char* def = this->Makefile->GetDefinition(this->Variable.c_str());
  147. if(needCompatibility && cmSystemTools::IsOff(def))
  148. {
  149. // Use the setting of the old name of the variable to provide the
  150. // value of the new.
  151. const char* oldDef = this->Makefile->GetDefinition(upperDir.c_str());
  152. if(!cmSystemTools::IsOff(oldDef))
  153. {
  154. this->Makefile->AddDefinition(this->Variable.c_str(), oldDef);
  155. def = this->Makefile->GetDefinition(this->Variable.c_str());
  156. }
  157. }
  158. if(cmSystemTools::IsOff(def))
  159. {
  160. if(!this->FindConfig())
  161. {
  162. this->AppendSuccessInformation(quiet);
  163. return false;
  164. }
  165. }
  166. // If the config file was found, load it.
  167. bool result = true;
  168. bool found = false;
  169. def = this->Makefile->GetDefinition(this->Variable.c_str());
  170. if(!cmSystemTools::IsOff(def))
  171. {
  172. std::string f = def;
  173. f += "/";
  174. f += this->Config;
  175. if(cmSystemTools::FileExists(f.c_str()))
  176. {
  177. if(this->ReadListFile(f.c_str()))
  178. {
  179. found = true;
  180. }
  181. else
  182. {
  183. result = false;
  184. }
  185. }
  186. else
  187. {
  188. cmOStringStream e;
  189. e << this->Variable << " is set to \"" << def << "\", which is "
  190. << "not a directory containing " << this->Config;
  191. cmSystemTools::Error(e.str().c_str());
  192. if(required)
  193. {
  194. cmSystemTools::SetFatalErrorOccured();
  195. }
  196. result = true;
  197. }
  198. }
  199. else if(!quiet || required)
  200. {
  201. cmOStringStream e;
  202. e << this->Variable << " is not set. It must be set to the directory "
  203. << "containing " << this->Config << " in order to use "
  204. << this->Name << ".";
  205. cmSystemTools::Error(e.str().c_str());
  206. if(required)
  207. {
  208. cmSystemTools::SetFatalErrorOccured();
  209. }
  210. result = true;
  211. }
  212. // Set a variable marking whether the package was found.
  213. std::string foundVar = this->Name;
  214. foundVar += "_FOUND";
  215. this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  216. if(needCompatibility)
  217. {
  218. // Listfiles will be looking for the capitalized version of the
  219. // name. Provide it.
  220. this->Makefile->AddDefinition
  221. (upperDir.c_str(),
  222. this->Makefile->GetDefinition(this->Variable.c_str()));
  223. this->Makefile->AddDefinition
  224. (upperFound.c_str(),
  225. this->Makefile->GetDefinition(foundVar.c_str()));
  226. }
  227. #ifdef CMAKE_BUILD_WITH_CMAKE
  228. if(!(upperDir == this->Variable))
  229. {
  230. if(needCompatibility)
  231. {
  232. // Listfiles may use the capitalized version of the name.
  233. // Remove any previously added watch.
  234. this->Makefile->GetVariableWatch()->RemoveWatch(
  235. upperDir.c_str(),
  236. cmFindPackageNeedBackwardsCompatibility
  237. );
  238. this->Makefile->GetVariableWatch()->RemoveWatch(
  239. upperFound.c_str(),
  240. cmFindPackageNeedBackwardsCompatibility
  241. );
  242. }
  243. else
  244. {
  245. // Listfiles should not be using the capitalized version of the
  246. // name. Add a watch to warn the user.
  247. this->Makefile->GetVariableWatch()->AddWatch(
  248. upperDir.c_str(),
  249. cmFindPackageNeedBackwardsCompatibility
  250. );
  251. this->Makefile->GetVariableWatch()->AddWatch(
  252. upperFound.c_str(),
  253. cmFindPackageNeedBackwardsCompatibility
  254. );
  255. }
  256. }
  257. #endif
  258. this->AppendSuccessInformation(quiet);
  259. return result;
  260. }
  261. //----------------------------------------------------------------------------
  262. bool cmFindPackageCommand::FindModule(bool& found, bool quiet, bool required)
  263. {
  264. std::string module = "Find";
  265. module += this->Name;
  266. module += ".cmake";
  267. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  268. if ( mfile.size() )
  269. {
  270. if(quiet)
  271. {
  272. // Tell the module that is about to be read that it should find
  273. // quietly.
  274. std::string quietly = this->Name;
  275. quietly += "_FIND_QUIETLY";
  276. this->Makefile->AddDefinition(quietly.c_str(), "1");
  277. }
  278. if(required)
  279. {
  280. // Tell the module that is about to be read that it should report
  281. // a fatal error if the package is not found.
  282. std::string req = this->Name;
  283. req += "_FIND_REQUIRED";
  284. this->Makefile->AddDefinition(req.c_str(), "1");
  285. }
  286. // Load the module we found.
  287. found = true;
  288. return this->ReadListFile(mfile.c_str());
  289. }
  290. return true;
  291. }
  292. //----------------------------------------------------------------------------
  293. bool cmFindPackageCommand::FindConfig()
  294. {
  295. std::string help = "The directory containing ";
  296. help += this->Config;
  297. help += ".";
  298. // Construct the list of relative paths to each prefix to be
  299. // searched.
  300. std::string rel = "/lib/";
  301. rel += cmSystemTools::LowerCase(this->Name);
  302. this->Relatives.push_back(rel);
  303. rel = "/lib/";
  304. rel += this->Name;
  305. this->Relatives.push_back(rel);
  306. // It is likely that CMake will have recently built the project.
  307. for(int i=1; i <= 10; ++i)
  308. {
  309. cmOStringStream r;
  310. r << "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  311. "Settings\\StartPath;WhereBuild" << i << "]";
  312. std::string entry = r.str();
  313. cmSystemTools::ExpandRegistryValues(entry);
  314. cmSystemTools::ConvertToUnixSlashes(entry);
  315. if(cmSystemTools::FileIsDirectory(entry.c_str()))
  316. {
  317. this->Builds.push_back(entry);
  318. }
  319. }
  320. // The project may be installed. Use the system search path to
  321. // construct a list of possible install prefixes.
  322. std::vector<std::string> systemPath;
  323. cmSystemTools::GetPath(systemPath);
  324. for(std::vector<std::string>::iterator i = systemPath.begin();
  325. i != systemPath.end(); ++i)
  326. {
  327. *i += "/..";
  328. if(cmSystemTools::FileIsDirectory(i->c_str()))
  329. {
  330. this->Prefixes.push_back(cmSystemTools::CollapseFullPath(i->c_str()));
  331. }
  332. }
  333. #if !defined(WIN32) || defined(__CYGWIN__)
  334. this->Prefixes.push_back("/usr/local");
  335. this->Prefixes.push_back("/usr");
  336. #endif
  337. // Look for the project's configuration file.
  338. std::string init = this->SearchForConfig();
  339. // Store the entry in the cache so it can be set by the user.
  340. this->Makefile->AddCacheDefinition(this->Variable.c_str(),
  341. init.c_str(),
  342. help.c_str(),
  343. cmCacheManager::PATH);
  344. return true;
  345. }
  346. //----------------------------------------------------------------------------
  347. std::string cmFindPackageCommand::SearchForConfig() const
  348. {
  349. // Check the environment variable.
  350. std::string env;
  351. if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
  352. {
  353. cmSystemTools::ConvertToUnixSlashes(env);
  354. std::string f = env;
  355. f += "/";
  356. f += this->Config;
  357. if(cmSystemTools::FileExists(f.c_str()))
  358. {
  359. return env;
  360. }
  361. }
  362. // Search the build directories.
  363. for(std::vector<cmStdString>::const_iterator b = this->Builds.begin();
  364. b != this->Builds.end(); ++b)
  365. {
  366. std::string f = *b;
  367. f += "/";
  368. f += this->Config;
  369. if(cmSystemTools::FileExists(f.c_str()))
  370. {
  371. return *b;
  372. }
  373. }
  374. // Search paths relative to each installation prefix.
  375. for(std::vector<cmStdString>::const_iterator p = this->Prefixes.begin();
  376. p != this->Prefixes.end(); ++p)
  377. {
  378. std::string prefix = *p;
  379. for(std::vector<cmStdString>::const_iterator r = this->Relatives.begin();
  380. r != this->Relatives.end(); ++r)
  381. {
  382. std::string dir = prefix;
  383. dir += *r;
  384. std::string f = dir;
  385. f += "/";
  386. f += this->Config;
  387. if(cmSystemTools::FileExists(f.c_str()))
  388. {
  389. return dir;
  390. }
  391. }
  392. }
  393. return this->Variable + "-NOTFOUND";
  394. }
  395. //----------------------------------------------------------------------------
  396. bool cmFindPackageCommand::ReadListFile(const char* f)
  397. {
  398. if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),f))
  399. {
  400. return true;
  401. }
  402. std::string e = "Error reading CMake code from \"";
  403. e += f;
  404. e += "\".";
  405. this->SetError(e.c_str());
  406. return false;
  407. }
  408. //----------------------------------------------------------------------------
  409. void cmFindPackageCommand::AppendToProperty(const char* propertyName)
  410. {
  411. std::string propertyValue;
  412. const char *prop =
  413. this->Makefile->GetCMakeInstance()->GetProperty(propertyName);
  414. if (prop && *prop)
  415. {
  416. propertyValue = prop;
  417. std::vector<std::string> contents;
  418. cmSystemTools::ExpandListArgument(propertyValue, contents, false);
  419. bool alreadyInserted = false;
  420. for(std::vector<std::string>::const_iterator it = contents.begin();
  421. it != contents.end(); ++ it )
  422. {
  423. if (*it == this->Name)
  424. {
  425. alreadyInserted = true;
  426. break;
  427. }
  428. }
  429. if (!alreadyInserted)
  430. {
  431. propertyValue += ";";
  432. propertyValue += this->Name;
  433. }
  434. }
  435. else
  436. {
  437. propertyValue = this->Name;
  438. }
  439. this->Makefile->GetCMakeInstance()->SetProperty(propertyName,
  440. propertyValue.c_str());
  441. }
  442. //----------------------------------------------------------------------------
  443. void cmFindPackageCommand::AppendSuccessInformation(bool quiet)
  444. {
  445. std::string found = this->Name;
  446. found += "_FOUND";
  447. std::string upperFound = cmSystemTools::UpperCase(found);
  448. const char* upperResult = this->Makefile->GetDefinition(upperFound.c_str());
  449. const char* result = this->Makefile->GetDefinition(found.c_str());
  450. if ((cmSystemTools::IsOn(result)) || (cmSystemTools::IsOn(upperResult)))
  451. {
  452. this->AppendToProperty("PACKAGES_FOUND");
  453. if (!quiet)
  454. {
  455. this->AppendToProperty("ENABLED_FEATURES");
  456. }
  457. }
  458. else
  459. {
  460. this->AppendToProperty("PACKAGES_NOT_FOUND");
  461. if (!quiet)
  462. {
  463. this->AppendToProperty("DISABLED_FEATURES");
  464. }
  465. }
  466. }