cmFindPackageCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. return false;
  116. }
  117. if(foundModule)
  118. {
  119. return true;
  120. }
  121. }
  122. // No find module. Assume the project has a CMake config file. Use
  123. // a <NAME>_DIR cache variable to locate it.
  124. this->Variable = this->Name;
  125. this->Variable += "_DIR";
  126. this->Config = this->Name;
  127. this->Config += "Config.cmake";
  128. // Support old capitalization behavior.
  129. std::string upperDir = cmSystemTools::UpperCase(this->Name);
  130. std::string upperFound = cmSystemTools::UpperCase(this->Name);
  131. upperDir += "_DIR";
  132. upperFound += "_FOUND";
  133. bool needCompatibility = false;
  134. if(!(upperDir == this->Variable))
  135. {
  136. const char* versionValue =
  137. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  138. if(atof(versionValue) < 1.7)
  139. {
  140. needCompatibility = true;
  141. }
  142. }
  143. // Try to find the config file.
  144. const char* def = this->Makefile->GetDefinition(this->Variable.c_str());
  145. if(needCompatibility && cmSystemTools::IsOff(def))
  146. {
  147. // Use the setting of the old name of the variable to provide the
  148. // value of the new.
  149. const char* oldDef = this->Makefile->GetDefinition(upperDir.c_str());
  150. if(!cmSystemTools::IsOff(oldDef))
  151. {
  152. this->Makefile->AddDefinition(this->Variable.c_str(), oldDef);
  153. def = this->Makefile->GetDefinition(this->Variable.c_str());
  154. }
  155. }
  156. if(cmSystemTools::IsOff(def))
  157. {
  158. if(!this->FindConfig())
  159. {
  160. return false;
  161. }
  162. }
  163. // If the config file was found, load it.
  164. bool result = true;
  165. bool found = false;
  166. def = this->Makefile->GetDefinition(this->Variable.c_str());
  167. if(!cmSystemTools::IsOff(def))
  168. {
  169. std::string f = def;
  170. f += "/";
  171. f += this->Config;
  172. if(cmSystemTools::FileExists(f.c_str()))
  173. {
  174. if(this->ReadListFile(f.c_str()))
  175. {
  176. found = true;
  177. }
  178. else
  179. {
  180. result = false;
  181. }
  182. }
  183. else
  184. {
  185. cmOStringStream e;
  186. e << this->Variable << " is set to \"" << def << "\", which is "
  187. << "not a directory containing " << this->Config;
  188. cmSystemTools::Error(e.str().c_str());
  189. if(required)
  190. {
  191. cmSystemTools::SetFatalErrorOccured();
  192. }
  193. result = true;
  194. }
  195. }
  196. else if(!quiet || required)
  197. {
  198. cmOStringStream e;
  199. e << this->Variable << " is not set. It must be set to the directory "
  200. << "containing " << this->Config << " in order to use "
  201. << this->Name << ".";
  202. cmSystemTools::Error(e.str().c_str());
  203. if(required)
  204. {
  205. cmSystemTools::SetFatalErrorOccured();
  206. }
  207. result = true;
  208. }
  209. // Set a variable marking whether the package was found.
  210. std::string foundVar = this->Name;
  211. foundVar += "_FOUND";
  212. this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  213. if(needCompatibility)
  214. {
  215. // Listfiles will be looking for the capitalized version of the
  216. // name. Provide it.
  217. this->Makefile->AddDefinition
  218. (upperDir.c_str(),
  219. this->Makefile->GetDefinition(this->Variable.c_str()));
  220. this->Makefile->AddDefinition
  221. (upperFound.c_str(),
  222. this->Makefile->GetDefinition(foundVar.c_str()));
  223. }
  224. #ifdef CMAKE_BUILD_WITH_CMAKE
  225. if(!(upperDir == this->Variable))
  226. {
  227. if(needCompatibility)
  228. {
  229. // Listfiles may use the capitalized version of the name.
  230. // Remove any previously added watch.
  231. this->Makefile->GetVariableWatch()->RemoveWatch(
  232. upperDir.c_str(),
  233. cmFindPackageNeedBackwardsCompatibility
  234. );
  235. this->Makefile->GetVariableWatch()->RemoveWatch(
  236. upperFound.c_str(),
  237. cmFindPackageNeedBackwardsCompatibility
  238. );
  239. }
  240. else
  241. {
  242. // Listfiles should not be using the capitalized version of the
  243. // name. Add a watch to warn the user.
  244. this->Makefile->GetVariableWatch()->AddWatch(
  245. upperDir.c_str(),
  246. cmFindPackageNeedBackwardsCompatibility
  247. );
  248. this->Makefile->GetVariableWatch()->AddWatch(
  249. upperFound.c_str(),
  250. cmFindPackageNeedBackwardsCompatibility
  251. );
  252. }
  253. }
  254. #endif
  255. return result;
  256. }
  257. //----------------------------------------------------------------------------
  258. bool cmFindPackageCommand::FindModule(bool& found, bool quiet, bool required)
  259. {
  260. std::string module = "Find";
  261. module += this->Name;
  262. module += ".cmake";
  263. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  264. if ( mfile.size() )
  265. {
  266. if(quiet)
  267. {
  268. // Tell the module that is about to be read that it should find
  269. // quietly.
  270. std::string quietly = this->Name;
  271. quietly += "_FIND_QUIETLY";
  272. this->Makefile->AddDefinition(quietly.c_str(), "1");
  273. }
  274. if(required)
  275. {
  276. // Tell the module that is about to be read that it should report
  277. // a fatal error if the package is not found.
  278. std::string req = this->Name;
  279. req += "_FIND_REQUIRED";
  280. this->Makefile->AddDefinition(req.c_str(), "1");
  281. }
  282. // Load the module we found.
  283. found = true;
  284. return this->ReadListFile(mfile.c_str());
  285. }
  286. return true;
  287. }
  288. //----------------------------------------------------------------------------
  289. bool cmFindPackageCommand::FindConfig()
  290. {
  291. std::string help = "The directory containing ";
  292. help += this->Config;
  293. help += ".";
  294. // Construct the list of relative paths to each prefix to be
  295. // searched.
  296. std::string rel = "/lib/";
  297. rel += cmSystemTools::LowerCase(this->Name);
  298. this->Relatives.push_back(rel);
  299. rel = "/lib/";
  300. rel += this->Name;
  301. this->Relatives.push_back(rel);
  302. // It is likely that CMake will have recently built the project.
  303. for(int i=1; i <= 10; ++i)
  304. {
  305. cmOStringStream r;
  306. r << "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  307. "Settings\\StartPath;WhereBuild" << i << "]";
  308. std::string entry = r.str();
  309. cmSystemTools::ExpandRegistryValues(entry);
  310. cmSystemTools::ConvertToUnixSlashes(entry);
  311. if(cmSystemTools::FileIsDirectory(entry.c_str()))
  312. {
  313. this->Builds.push_back(entry);
  314. }
  315. }
  316. // The project may be installed. Use the system search path to
  317. // construct a list of possible install prefixes.
  318. std::vector<std::string> systemPath;
  319. cmSystemTools::GetPath(systemPath);
  320. for(std::vector<std::string>::iterator i = systemPath.begin();
  321. i != systemPath.end(); ++i)
  322. {
  323. *i += "/..";
  324. if(cmSystemTools::FileIsDirectory(i->c_str()))
  325. {
  326. this->Prefixes.push_back(cmSystemTools::CollapseFullPath(i->c_str()));
  327. }
  328. }
  329. #if !defined(WIN32) || defined(__CYGWIN__)
  330. this->Prefixes.push_back("/usr/local");
  331. this->Prefixes.push_back("/usr");
  332. #endif
  333. // Look for the project's configuration file.
  334. std::string init = this->SearchForConfig();
  335. // Store the entry in the cache so it can be set by the user.
  336. this->Makefile->AddCacheDefinition(this->Variable.c_str(),
  337. init.c_str(),
  338. help.c_str(),
  339. cmCacheManager::PATH);
  340. return true;
  341. }
  342. //----------------------------------------------------------------------------
  343. std::string cmFindPackageCommand::SearchForConfig() const
  344. {
  345. // Check the environment variable.
  346. std::string env;
  347. if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
  348. {
  349. cmSystemTools::ConvertToUnixSlashes(env);
  350. std::string f = env;
  351. f += "/";
  352. f += this->Config;
  353. if(cmSystemTools::FileExists(f.c_str()))
  354. {
  355. return env;
  356. }
  357. }
  358. // Search the build directories.
  359. for(std::vector<cmStdString>::const_iterator b = this->Builds.begin();
  360. b != this->Builds.end(); ++b)
  361. {
  362. std::string f = *b;
  363. f += "/";
  364. f += this->Config;
  365. if(cmSystemTools::FileExists(f.c_str()))
  366. {
  367. return *b;
  368. }
  369. }
  370. // Search paths relative to each installation prefix.
  371. for(std::vector<cmStdString>::const_iterator p = this->Prefixes.begin();
  372. p != this->Prefixes.end(); ++p)
  373. {
  374. std::string prefix = *p;
  375. for(std::vector<cmStdString>::const_iterator r = this->Relatives.begin();
  376. r != this->Relatives.end(); ++r)
  377. {
  378. std::string dir = prefix;
  379. dir += *r;
  380. std::string f = dir;
  381. f += "/";
  382. f += this->Config;
  383. if(cmSystemTools::FileExists(f.c_str()))
  384. {
  385. return dir;
  386. }
  387. }
  388. }
  389. return this->Variable + "-NOTFOUND";
  390. }
  391. //----------------------------------------------------------------------------
  392. bool cmFindPackageCommand::ReadListFile(const char* f)
  393. {
  394. if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),f))
  395. {
  396. return true;
  397. }
  398. std::string e = "Error reading CMake code from \"";
  399. e += f;
  400. e += "\".";
  401. this->SetError(e.c_str());
  402. return false;
  403. }