cmFindPackageCommand.cxx 11 KB

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