cmFindPackageCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. cmSystemTools::ConvertToUnixSlashes(f);
  174. f += "/";
  175. f += this->Config;
  176. if(!cmSystemTools::FileIsFullPath(f.c_str()))
  177. {
  178. f = "/" + f;
  179. f = this->Makefile->GetCurrentDirectory() + f;
  180. }
  181. if(cmSystemTools::FileExists(f.c_str()))
  182. {
  183. if(this->ReadListFile(f.c_str()))
  184. {
  185. found = true;
  186. }
  187. else
  188. {
  189. result = false;
  190. }
  191. }
  192. else
  193. {
  194. cmOStringStream e;
  195. e << this->Variable << " is set to \"" << def << "\", which is "
  196. << "not a directory containing " << this->Config;
  197. cmSystemTools::Error(e.str().c_str());
  198. if(required)
  199. {
  200. cmSystemTools::SetFatalErrorOccured();
  201. }
  202. result = true;
  203. }
  204. }
  205. else if(!quiet || required)
  206. {
  207. cmOStringStream e;
  208. e << "FIND_PACKAGE could not find Find" << this->Name
  209. << ".cmake nor config file " << this->Config << ".\n"
  210. << "Adjust CMAKE_MODULE_PATH to find Find" << this->Name
  211. << ".cmake or set " << this->Variable
  212. << "\nto the directory containing " << this->Config
  213. << " in order to use " << this->Name << ".";
  214. cmSystemTools::Error(e.str().c_str());
  215. if(required)
  216. {
  217. cmSystemTools::SetFatalErrorOccured();
  218. }
  219. result = true;
  220. }
  221. // Set a variable marking whether the package was found.
  222. std::string foundVar = this->Name;
  223. foundVar += "_FOUND";
  224. this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  225. if(needCompatibility)
  226. {
  227. // Listfiles will be looking for the capitalized version of the
  228. // name. Provide it.
  229. this->Makefile->AddDefinition
  230. (upperDir.c_str(),
  231. this->Makefile->GetDefinition(this->Variable.c_str()));
  232. this->Makefile->AddDefinition
  233. (upperFound.c_str(),
  234. this->Makefile->GetDefinition(foundVar.c_str()));
  235. }
  236. #ifdef CMAKE_BUILD_WITH_CMAKE
  237. if(!(upperDir == this->Variable))
  238. {
  239. if(needCompatibility)
  240. {
  241. // Listfiles may use the capitalized version of the name.
  242. // Remove any previously added watch.
  243. this->Makefile->GetVariableWatch()->RemoveWatch(
  244. upperDir.c_str(),
  245. cmFindPackageNeedBackwardsCompatibility
  246. );
  247. }
  248. else
  249. {
  250. // Listfiles should not be using the capitalized version of the
  251. // name. Add a watch to warn the user.
  252. this->Makefile->GetVariableWatch()->AddWatch(
  253. upperDir.c_str(),
  254. cmFindPackageNeedBackwardsCompatibility
  255. );
  256. }
  257. }
  258. #endif
  259. this->AppendSuccessInformation(quiet);
  260. return result;
  261. }
  262. //----------------------------------------------------------------------------
  263. bool cmFindPackageCommand::FindModule(bool& found, bool quiet, bool required)
  264. {
  265. std::string module = "Find";
  266. module += this->Name;
  267. module += ".cmake";
  268. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  269. if ( mfile.size() )
  270. {
  271. if(quiet)
  272. {
  273. // Tell the module that is about to be read that it should find
  274. // quietly.
  275. std::string quietly = this->Name;
  276. quietly += "_FIND_QUIETLY";
  277. this->Makefile->AddDefinition(quietly.c_str(), "1");
  278. }
  279. if(required)
  280. {
  281. // Tell the module that is about to be read that it should report
  282. // a fatal error if the package is not found.
  283. std::string req = this->Name;
  284. req += "_FIND_REQUIRED";
  285. this->Makefile->AddDefinition(req.c_str(), "1");
  286. }
  287. // Load the module we found.
  288. found = true;
  289. return this->ReadListFile(mfile.c_str());
  290. }
  291. return true;
  292. }
  293. //----------------------------------------------------------------------------
  294. bool cmFindPackageCommand::FindConfig()
  295. {
  296. std::string help = "The directory containing ";
  297. help += this->Config;
  298. help += ".";
  299. // Construct the list of relative paths to each prefix to be
  300. // searched.
  301. std::string rel = "/lib/";
  302. rel += cmSystemTools::LowerCase(this->Name);
  303. this->Relatives.push_back(rel);
  304. rel = "/lib/";
  305. rel += this->Name;
  306. this->Relatives.push_back(rel);
  307. // It is likely that CMake will have recently built the project.
  308. for(int i=1; i <= 10; ++i)
  309. {
  310. cmOStringStream r;
  311. r << "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  312. "Settings\\StartPath;WhereBuild" << i << "]";
  313. std::string entry = r.str();
  314. cmSystemTools::ExpandRegistryValues(entry);
  315. cmSystemTools::ConvertToUnixSlashes(entry);
  316. if(cmSystemTools::FileIsDirectory(entry.c_str()))
  317. {
  318. this->Builds.push_back(entry);
  319. }
  320. }
  321. // The project may be installed. Use the system search path to
  322. // construct a list of possible install prefixes.
  323. std::vector<std::string> systemPath;
  324. cmSystemTools::GetPath(systemPath);
  325. for(std::vector<std::string>::iterator i = systemPath.begin();
  326. i != systemPath.end(); ++i)
  327. {
  328. *i += "/..";
  329. if(cmSystemTools::FileIsDirectory(i->c_str()))
  330. {
  331. this->Prefixes.push_back(cmSystemTools::CollapseFullPath(i->c_str()));
  332. }
  333. }
  334. #if !defined(WIN32) || defined(__CYGWIN__)
  335. this->Prefixes.push_back("/usr/local");
  336. this->Prefixes.push_back("/usr");
  337. #endif
  338. // Look for the project's configuration file.
  339. std::string init = this->SearchForConfig();
  340. // Store the entry in the cache so it can be set by the user.
  341. this->Makefile->AddCacheDefinition(this->Variable.c_str(),
  342. init.c_str(),
  343. help.c_str(),
  344. cmCacheManager::PATH);
  345. return true;
  346. }
  347. //----------------------------------------------------------------------------
  348. std::string cmFindPackageCommand::SearchForConfig() const
  349. {
  350. // Check the environment variable.
  351. std::string env;
  352. if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
  353. {
  354. cmSystemTools::ConvertToUnixSlashes(env);
  355. std::string f = env;
  356. f += "/";
  357. f += this->Config;
  358. if(cmSystemTools::FileExists(f.c_str()))
  359. {
  360. return env;
  361. }
  362. }
  363. // Search the build directories.
  364. for(std::vector<cmStdString>::const_iterator b = this->Builds.begin();
  365. b != this->Builds.end(); ++b)
  366. {
  367. std::string f = *b;
  368. f += "/";
  369. f += this->Config;
  370. if(cmSystemTools::FileExists(f.c_str()))
  371. {
  372. return *b;
  373. }
  374. }
  375. // Search paths relative to each installation prefix.
  376. for(std::vector<cmStdString>::const_iterator p = this->Prefixes.begin();
  377. p != this->Prefixes.end(); ++p)
  378. {
  379. std::string prefix = *p;
  380. for(std::vector<cmStdString>::const_iterator r = this->Relatives.begin();
  381. r != this->Relatives.end(); ++r)
  382. {
  383. std::string dir = prefix;
  384. dir += *r;
  385. std::string f = dir;
  386. f += "/";
  387. f += this->Config;
  388. if(cmSystemTools::FileExists(f.c_str()))
  389. {
  390. return dir;
  391. }
  392. }
  393. }
  394. return this->Variable + "-NOTFOUND";
  395. }
  396. //----------------------------------------------------------------------------
  397. bool cmFindPackageCommand::ReadListFile(const char* f)
  398. {
  399. if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),f))
  400. {
  401. return true;
  402. }
  403. std::string e = "Error reading CMake code from \"";
  404. e += f;
  405. e += "\".";
  406. this->SetError(e.c_str());
  407. return false;
  408. }
  409. //----------------------------------------------------------------------------
  410. void cmFindPackageCommand::AppendToProperty(const char* propertyName)
  411. {
  412. std::string propertyValue;
  413. const char *prop =
  414. this->Makefile->GetCMakeInstance()->GetProperty(propertyName);
  415. if (prop && *prop)
  416. {
  417. propertyValue = prop;
  418. std::vector<std::string> contents;
  419. cmSystemTools::ExpandListArgument(propertyValue, contents, false);
  420. bool alreadyInserted = false;
  421. for(std::vector<std::string>::const_iterator it = contents.begin();
  422. it != contents.end(); ++ it )
  423. {
  424. if (*it == this->Name)
  425. {
  426. alreadyInserted = true;
  427. break;
  428. }
  429. }
  430. if (!alreadyInserted)
  431. {
  432. propertyValue += ";";
  433. propertyValue += this->Name;
  434. }
  435. }
  436. else
  437. {
  438. propertyValue = this->Name;
  439. }
  440. this->Makefile->GetCMakeInstance()->SetProperty(propertyName,
  441. propertyValue.c_str());
  442. }
  443. //----------------------------------------------------------------------------
  444. void cmFindPackageCommand::AppendSuccessInformation(bool quiet)
  445. {
  446. std::string found = this->Name;
  447. found += "_FOUND";
  448. std::string upperFound = cmSystemTools::UpperCase(found);
  449. const char* upperResult = this->Makefile->GetDefinition(upperFound.c_str());
  450. const char* result = this->Makefile->GetDefinition(found.c_str());
  451. if ((cmSystemTools::IsOn(result)) || (cmSystemTools::IsOn(upperResult)))
  452. {
  453. this->AppendToProperty("PACKAGES_FOUND");
  454. if (!quiet)
  455. {
  456. this->AppendToProperty("ENABLED_FEATURES");
  457. }
  458. }
  459. else
  460. {
  461. this->AppendToProperty("PACKAGES_NOT_FOUND");
  462. if (!quiet)
  463. {
  464. this->AppendToProperty("DISABLED_FEATURES");
  465. }
  466. }
  467. }