cmFindPackageCommand.cxx 12 KB

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