cmFindPackageCommand.cxx 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFindPackageCommand.h"
  11. #include <cmsys/Directory.hxx>
  12. #include <cmsys/RegularExpression.hxx>
  13. #include <cmsys/Encoding.hxx>
  14. #ifdef CMAKE_BUILD_WITH_CMAKE
  15. #include "cmVariableWatch.h"
  16. #endif
  17. #if defined(__HAIKU__)
  18. #include <string.h>
  19. #include <FindDirectory.h>
  20. #include <StorageDefs.h>
  21. #endif
  22. //----------------------------------------------------------------------------
  23. cmFindPackageCommand::cmFindPackageCommand()
  24. {
  25. this->CMakePathName = "PACKAGE";
  26. this->Quiet = false;
  27. this->Required = false;
  28. this->NoUserRegistry = false;
  29. this->NoSystemRegistry = false;
  30. this->NoBuilds = false;
  31. this->UseConfigFiles = true;
  32. this->UseFindModules = true;
  33. this->DebugMode = false;
  34. this->UseLib64Paths = false;
  35. this->PolicyScope = true;
  36. this->VersionMajor = 0;
  37. this->VersionMinor = 0;
  38. this->VersionPatch = 0;
  39. this->VersionTweak = 0;
  40. this->VersionCount = 0;
  41. this->VersionExact = false;
  42. this->VersionFoundMajor = 0;
  43. this->VersionFoundMinor = 0;
  44. this->VersionFoundPatch = 0;
  45. this->VersionFoundTweak = 0;
  46. this->VersionFoundCount = 0;
  47. this->RequiredCMakeVersion = 0;
  48. }
  49. //----------------------------------------------------------------------------
  50. bool cmFindPackageCommand
  51. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  52. {
  53. if(args.size() < 1)
  54. {
  55. this->SetError("called with incorrect number of arguments");
  56. return false;
  57. }
  58. // Lookup required version of CMake.
  59. if(const char* rv =
  60. this->Makefile->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION"))
  61. {
  62. unsigned int v[3] = {0,0,0};
  63. sscanf(rv, "%u.%u.%u", &v[0], &v[1], &v[2]);
  64. this->RequiredCMakeVersion = CMake_VERSION_ENCODE(v[0],v[1],v[2]);
  65. }
  66. // Check for debug mode.
  67. this->DebugMode = this->Makefile->IsOn("CMAKE_FIND_DEBUG_MODE");
  68. // Lookup target architecture, if any.
  69. if(const char* arch =
  70. this->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE"))
  71. {
  72. this->LibraryArchitecture = arch;
  73. }
  74. // Lookup whether lib64 paths should be used.
  75. if(this->Makefile->PlatformIs64Bit() &&
  76. this->Makefile->GetCMakeInstance()
  77. ->GetPropertyAsBool("FIND_LIBRARY_USE_LIB64_PATHS"))
  78. {
  79. this->UseLib64Paths = true;
  80. }
  81. // Find the current root path mode.
  82. this->SelectDefaultRootPathMode();
  83. // Find the current bundle/framework search policy.
  84. this->SelectDefaultMacMode();
  85. // Record options.
  86. this->Name = args[0];
  87. std::string components;
  88. const char* components_sep = "";
  89. std::set<std::string> requiredComponents;
  90. std::set<std::string> optionalComponents;
  91. // Always search directly in a generated path.
  92. this->SearchPathSuffixes.push_back("");
  93. // Parse the arguments.
  94. enum Doing { DoingNone, DoingComponents, DoingOptionalComponents, DoingNames,
  95. DoingPaths, DoingPathSuffixes, DoingConfigs, DoingHints };
  96. Doing doing = DoingNone;
  97. cmsys::RegularExpression version("^[0-9.]+$");
  98. bool haveVersion = false;
  99. std::set<unsigned int> configArgs;
  100. std::set<unsigned int> moduleArgs;
  101. for(unsigned int i=1; i < args.size(); ++i)
  102. {
  103. if(args[i] == "QUIET")
  104. {
  105. this->Quiet = true;
  106. doing = DoingNone;
  107. }
  108. else if(args[i] == "EXACT")
  109. {
  110. this->VersionExact = true;
  111. doing = DoingNone;
  112. }
  113. else if(args[i] == "MODULE")
  114. {
  115. moduleArgs.insert(i);
  116. doing = DoingNone;
  117. }
  118. else if(args[i] == "CONFIG")
  119. {
  120. configArgs.insert(i);
  121. doing = DoingNone;
  122. }
  123. else if(args[i] == "NO_MODULE")
  124. {
  125. configArgs.insert(i);
  126. doing = DoingNone;
  127. }
  128. else if(args[i] == "REQUIRED")
  129. {
  130. this->Required = true;
  131. doing = DoingComponents;
  132. }
  133. else if(args[i] == "COMPONENTS")
  134. {
  135. doing = DoingComponents;
  136. }
  137. else if(args[i] == "OPTIONAL_COMPONENTS")
  138. {
  139. doing = DoingOptionalComponents;
  140. }
  141. else if(args[i] == "NAMES")
  142. {
  143. configArgs.insert(i);
  144. doing = DoingNames;
  145. }
  146. else if(args[i] == "PATHS")
  147. {
  148. configArgs.insert(i);
  149. doing = DoingPaths;
  150. }
  151. else if(args[i] == "HINTS")
  152. {
  153. configArgs.insert(i);
  154. doing = DoingHints;
  155. }
  156. else if(args[i] == "PATH_SUFFIXES")
  157. {
  158. configArgs.insert(i);
  159. doing = DoingPathSuffixes;
  160. }
  161. else if(args[i] == "CONFIGS")
  162. {
  163. configArgs.insert(i);
  164. doing = DoingConfigs;
  165. }
  166. else if(args[i] == "NO_POLICY_SCOPE")
  167. {
  168. this->PolicyScope = false;
  169. doing = DoingNone;
  170. }
  171. else if(args[i] == "NO_CMAKE_PACKAGE_REGISTRY")
  172. {
  173. this->NoUserRegistry = true;
  174. configArgs.insert(i);
  175. doing = DoingNone;
  176. }
  177. else if(args[i] == "NO_CMAKE_SYSTEM_PACKAGE_REGISTRY")
  178. {
  179. this->NoSystemRegistry = true;
  180. configArgs.insert(i);
  181. doing = DoingNone;
  182. }
  183. else if(args[i] == "NO_CMAKE_BUILDS_PATH")
  184. {
  185. this->NoBuilds = true;
  186. configArgs.insert(i);
  187. doing = DoingNone;
  188. }
  189. else if(this->CheckCommonArgument(args[i]))
  190. {
  191. configArgs.insert(i);
  192. doing = DoingNone;
  193. }
  194. else if((doing == DoingComponents) || (doing == DoingOptionalComponents))
  195. {
  196. // Set a variable telling the find script whether this component
  197. // is required.
  198. const char* isRequired = "1";
  199. if (doing == DoingOptionalComponents)
  200. {
  201. isRequired = "0";
  202. optionalComponents.insert(args[i]);
  203. }
  204. else
  205. {
  206. requiredComponents.insert(args[i]);
  207. }
  208. std::string req_var = this->Name + "_FIND_REQUIRED_" + args[i];
  209. this->AddFindDefinition(req_var.c_str(), isRequired);
  210. // Append to the list of required components.
  211. components += components_sep;
  212. components += args[i];
  213. components_sep = ";";
  214. }
  215. else if(doing == DoingNames)
  216. {
  217. this->Names.push_back(args[i]);
  218. }
  219. else if(doing == DoingPaths)
  220. {
  221. this->AddUserPath(args[i], this->UserPaths);
  222. }
  223. else if(doing == DoingHints)
  224. {
  225. this->AddUserPath(args[i], this->UserHints);
  226. }
  227. else if(doing == DoingPathSuffixes)
  228. {
  229. this->AddPathSuffix(args[i]);
  230. }
  231. else if(doing == DoingConfigs)
  232. {
  233. if(args[i].find_first_of(":/\\") != args[i].npos ||
  234. cmSystemTools::GetFilenameLastExtension(args[i]) != ".cmake")
  235. {
  236. cmOStringStream e;
  237. e << "given CONFIGS option followed by invalid file name \""
  238. << args[i] << "\". The names given must be file names without "
  239. << "a path and with a \".cmake\" extension.";
  240. this->SetError(e.str().c_str());
  241. return false;
  242. }
  243. this->Configs.push_back(args[i]);
  244. }
  245. else if(!haveVersion && version.find(args[i].c_str()))
  246. {
  247. haveVersion = true;
  248. this->Version = args[i];
  249. }
  250. else
  251. {
  252. cmOStringStream e;
  253. e << "called with invalid argument \"" << args[i].c_str() << "\"";
  254. this->SetError(e.str().c_str());
  255. return false;
  256. }
  257. }
  258. std::vector<std::string> doubledComponents;
  259. std::set_intersection(requiredComponents.begin(), requiredComponents.end(),
  260. optionalComponents.begin(), optionalComponents.end(),
  261. std::back_inserter(doubledComponents));
  262. if(!doubledComponents.empty())
  263. {
  264. cmOStringStream e;
  265. e << "called with components that are both required and optional:\n";
  266. for(unsigned int i=0; i<doubledComponents.size(); ++i)
  267. {
  268. e << " " << doubledComponents[i] << "\n";
  269. }
  270. this->SetError(e.str().c_str());
  271. return false;
  272. }
  273. // Maybe choose one mode exclusively.
  274. this->UseFindModules = configArgs.empty();
  275. this->UseConfigFiles = moduleArgs.empty();
  276. if(!this->UseFindModules && !this->UseConfigFiles)
  277. {
  278. cmOStringStream e;
  279. e << "given options exclusive to Module mode:\n";
  280. for(std::set<unsigned int>::const_iterator si = moduleArgs.begin();
  281. si != moduleArgs.end(); ++si)
  282. {
  283. e << " " << args[*si] << "\n";
  284. }
  285. e << "and options exclusive to Config mode:\n";
  286. for(std::set<unsigned int>::const_iterator si = configArgs.begin();
  287. si != configArgs.end(); ++si)
  288. {
  289. e << " " << args[*si] << "\n";
  290. }
  291. e << "The options are incompatible.";
  292. this->SetError(e.str().c_str());
  293. return false;
  294. }
  295. // Ignore EXACT with no version.
  296. if(this->Version.empty() && this->VersionExact)
  297. {
  298. this->VersionExact = false;
  299. this->Makefile->IssueMessage(
  300. cmake::AUTHOR_WARNING, "Ignoring EXACT since no version is requested.");
  301. }
  302. if(this->Version.empty() || components.empty())
  303. {
  304. // Check whether we are recursing inside "Find<name>.cmake" within
  305. // another find_package(<name>) call.
  306. std::string mod = this->Name;
  307. mod += "_FIND_MODULE";
  308. if(this->Makefile->IsOn(mod.c_str()))
  309. {
  310. if(this->Version.empty())
  311. {
  312. // Get version information from the outer call if necessary.
  313. // Requested version string.
  314. std::string ver = this->Name;
  315. ver += "_FIND_VERSION";
  316. this->Version = this->Makefile->GetSafeDefinition(ver.c_str());
  317. // Whether an exact version is required.
  318. std::string exact = this->Name;
  319. exact += "_FIND_VERSION_EXACT";
  320. this->VersionExact = this->Makefile->IsOn(exact.c_str());
  321. }
  322. if(components.empty())
  323. {
  324. std::string components_var = this->Name + "_FIND_COMPONENTS";
  325. components = this->Makefile->GetSafeDefinition(components_var.c_str());
  326. }
  327. }
  328. }
  329. if(!this->Version.empty())
  330. {
  331. // Try to parse the version number and store the results that were
  332. // successfully parsed.
  333. unsigned int parsed_major;
  334. unsigned int parsed_minor;
  335. unsigned int parsed_patch;
  336. unsigned int parsed_tweak;
  337. this->VersionCount = sscanf(this->Version.c_str(), "%u.%u.%u.%u",
  338. &parsed_major, &parsed_minor,
  339. &parsed_patch, &parsed_tweak);
  340. switch(this->VersionCount)
  341. {
  342. case 4: this->VersionTweak = parsed_tweak; // no break!
  343. case 3: this->VersionPatch = parsed_patch; // no break!
  344. case 2: this->VersionMinor = parsed_minor; // no break!
  345. case 1: this->VersionMajor = parsed_major; // no break!
  346. default: break;
  347. }
  348. }
  349. std::string disableFindPackageVar = "CMAKE_DISABLE_FIND_PACKAGE_";
  350. disableFindPackageVar += this->Name;
  351. if(this->Makefile->IsOn(disableFindPackageVar.c_str()))
  352. {
  353. if (this->Required)
  354. {
  355. cmOStringStream e;
  356. e << "for module " << this->Name << " called with REQUIRED, but "
  357. << disableFindPackageVar
  358. << " is enabled. A REQUIRED package cannot be disabled.";
  359. this->SetError(e.str().c_str());
  360. return false;
  361. }
  362. return true;
  363. }
  364. this->SetModuleVariables(components);
  365. // See if there is a Find<package>.cmake module.
  366. if(this->UseFindModules)
  367. {
  368. bool foundModule = false;
  369. if(!this->FindModule(foundModule))
  370. {
  371. this->AppendSuccessInformation();
  372. return false;
  373. }
  374. if(foundModule)
  375. {
  376. this->AppendSuccessInformation();
  377. return true;
  378. }
  379. }
  380. if(this->UseFindModules && this->UseConfigFiles &&
  381. this->Makefile->IsOn("CMAKE_FIND_PACKAGE_WARN_NO_MODULE"))
  382. {
  383. cmOStringStream aw;
  384. if(this->RequiredCMakeVersion >= CMake_VERSION_ENCODE(2,8,8))
  385. {
  386. aw << "find_package called without either MODULE or CONFIG option and "
  387. "no Find" << this->Name << ".cmake module is in CMAKE_MODULE_PATH. "
  388. "Add MODULE to exclusively request Module mode and fail if "
  389. "Find" << this->Name << ".cmake is missing. "
  390. "Add CONFIG to exclusively request Config mode and search for a "
  391. "package configuration file provided by " << this->Name <<
  392. " (" << this->Name << "Config.cmake or " <<
  393. cmSystemTools::LowerCase(this->Name) << "-config.cmake). ";
  394. }
  395. else
  396. {
  397. aw << "find_package called without NO_MODULE option and no "
  398. "Find" << this->Name << ".cmake module is in CMAKE_MODULE_PATH. "
  399. "Add NO_MODULE to exclusively request Config mode and search for a "
  400. "package configuration file provided by " << this->Name <<
  401. " (" << this->Name << "Config.cmake or " <<
  402. cmSystemTools::LowerCase(this->Name) << "-config.cmake). "
  403. "Otherwise make Find" << this->Name << ".cmake available in "
  404. "CMAKE_MODULE_PATH.";
  405. }
  406. aw << "\n"
  407. "(Variable CMAKE_FIND_PACKAGE_WARN_NO_MODULE enabled this warning.)";
  408. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, aw.str());
  409. }
  410. // No find module. Assume the project has a CMake config file. Use
  411. // a <package>_DIR cache variable to locate it.
  412. this->Variable = this->Name;
  413. this->Variable += "_DIR";
  414. // Add the default name.
  415. if(this->Names.empty())
  416. {
  417. this->Names.push_back(this->Name);
  418. }
  419. // Add the default configs.
  420. if(this->Configs.empty())
  421. {
  422. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  423. ni != this->Names.end(); ++ni)
  424. {
  425. std::string config = *ni;
  426. config += "Config.cmake";
  427. this->Configs.push_back(config);
  428. config = cmSystemTools::LowerCase(*ni);
  429. config += "-config.cmake";
  430. this->Configs.push_back(config);
  431. }
  432. }
  433. // get igonored paths from vars and reroot them.
  434. std::vector<std::string> ignored;
  435. this->GetIgnoredPaths(ignored);
  436. this->RerootPaths(ignored);
  437. // Construct a set of ignored paths
  438. this->IgnoredPaths.clear();
  439. this->IgnoredPaths.insert(ignored.begin(), ignored.end());
  440. // Find and load the package.
  441. bool result = this->HandlePackageMode();
  442. this->AppendSuccessInformation();
  443. return result;
  444. }
  445. //----------------------------------------------------------------------------
  446. void cmFindPackageCommand::SetModuleVariables(const std::string& components)
  447. {
  448. this->AddFindDefinition("CMAKE_FIND_PACKAGE_NAME", this->Name.c_str());
  449. // Store the list of components.
  450. std::string components_var = this->Name + "_FIND_COMPONENTS";
  451. this->AddFindDefinition(components_var.c_str(), components.c_str());
  452. if(this->Quiet)
  453. {
  454. // Tell the module that is about to be read that it should find
  455. // quietly.
  456. std::string quietly = this->Name;
  457. quietly += "_FIND_QUIETLY";
  458. this->AddFindDefinition(quietly.c_str(), "1");
  459. }
  460. if(this->Required)
  461. {
  462. // Tell the module that is about to be read that it should report
  463. // a fatal error if the package is not found.
  464. std::string req = this->Name;
  465. req += "_FIND_REQUIRED";
  466. this->AddFindDefinition(req.c_str(), "1");
  467. }
  468. if(!this->Version.empty())
  469. {
  470. // Tell the module that is about to be read what version of the
  471. // package has been requested.
  472. std::string ver = this->Name;
  473. ver += "_FIND_VERSION";
  474. this->AddFindDefinition(ver.c_str(), this->Version.c_str());
  475. char buf[64];
  476. sprintf(buf, "%u", this->VersionMajor);
  477. this->AddFindDefinition((ver+"_MAJOR").c_str(), buf);
  478. sprintf(buf, "%u", this->VersionMinor);
  479. this->AddFindDefinition((ver+"_MINOR").c_str(), buf);
  480. sprintf(buf, "%u", this->VersionPatch);
  481. this->AddFindDefinition((ver+"_PATCH").c_str(), buf);
  482. sprintf(buf, "%u", this->VersionTweak);
  483. this->AddFindDefinition((ver+"_TWEAK").c_str(), buf);
  484. sprintf(buf, "%u", this->VersionCount);
  485. this->AddFindDefinition((ver+"_COUNT").c_str(), buf);
  486. // Tell the module whether an exact version has been requested.
  487. std::string exact = this->Name;
  488. exact += "_FIND_VERSION_EXACT";
  489. this->AddFindDefinition(exact.c_str(), this->VersionExact? "1":"0");
  490. }
  491. }
  492. //----------------------------------------------------------------------------
  493. void cmFindPackageCommand::AddFindDefinition(const std::string& var,
  494. const char* val)
  495. {
  496. if(const char* old = this->Makefile->GetDefinition(var))
  497. {
  498. this->OriginalDefs[var].exists = true;
  499. this->OriginalDefs[var].value = old;
  500. }
  501. else
  502. {
  503. this->OriginalDefs[var].exists = false;
  504. }
  505. this->Makefile->AddDefinition(var, val);
  506. }
  507. //----------------------------------------------------------------------------
  508. void cmFindPackageCommand::RestoreFindDefinitions()
  509. {
  510. for(std::map<cmStdString, OriginalDef>::iterator
  511. i = this->OriginalDefs.begin(); i != this->OriginalDefs.end(); ++i)
  512. {
  513. OriginalDef const& od = i->second;
  514. if(od.exists)
  515. {
  516. this->Makefile->AddDefinition(i->first.c_str(), od.value.c_str());
  517. }
  518. else
  519. {
  520. this->Makefile->RemoveDefinition(i->first.c_str());
  521. }
  522. }
  523. }
  524. //----------------------------------------------------------------------------
  525. bool cmFindPackageCommand::FindModule(bool& found)
  526. {
  527. std::string module = "Find";
  528. module += this->Name;
  529. module += ".cmake";
  530. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  531. if ( mfile.size() )
  532. {
  533. // Load the module we found, and set "<name>_FIND_MODULE" to true
  534. // while inside it.
  535. found = true;
  536. std::string var = this->Name;
  537. var += "_FIND_MODULE";
  538. this->Makefile->AddDefinition(var.c_str(), "1");
  539. bool result = this->ReadListFile(mfile.c_str(), DoPolicyScope);
  540. this->Makefile->RemoveDefinition(var.c_str());
  541. return result;
  542. }
  543. return true;
  544. }
  545. //----------------------------------------------------------------------------
  546. bool cmFindPackageCommand::HandlePackageMode()
  547. {
  548. this->ConsideredConfigs.clear();
  549. // Support old capitalization behavior.
  550. std::string upperDir = cmSystemTools::UpperCase(this->Name);
  551. std::string upperFound = cmSystemTools::UpperCase(this->Name);
  552. upperDir += "_DIR";
  553. upperFound += "_FOUND";
  554. // Try to find the config file.
  555. const char* def = this->Makefile->GetDefinition(this->Variable.c_str());
  556. // Try to load the config file if the directory is known
  557. bool fileFound = false;
  558. if (this->UseConfigFiles)
  559. {
  560. if(!cmSystemTools::IsOff(def))
  561. {
  562. // Get the directory from the variable value.
  563. std::string dir = def;
  564. cmSystemTools::ConvertToUnixSlashes(dir);
  565. // Treat relative paths with respect to the current source dir.
  566. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  567. {
  568. dir = "/" + dir;
  569. dir = this->Makefile->GetCurrentDirectory() + dir;
  570. }
  571. // The file location was cached. Look for the correct file.
  572. std::string file;
  573. if (this->FindConfigFile(dir, file))
  574. {
  575. this->FileFound = file;
  576. fileFound = true;
  577. }
  578. def = this->Makefile->GetDefinition(this->Variable.c_str());
  579. }
  580. // Search for the config file if it is not already found.
  581. if(cmSystemTools::IsOff(def) || !fileFound)
  582. {
  583. fileFound = this->FindConfig();
  584. def = this->Makefile->GetDefinition(this->Variable.c_str());
  585. }
  586. // Sanity check.
  587. if(fileFound && this->FileFound.empty())
  588. {
  589. this->Makefile->IssueMessage(
  590. cmake::INTERNAL_ERROR, "fileFound is true but FileFound is empty!");
  591. fileFound = false;
  592. }
  593. }
  594. std::string foundVar = this->Name;
  595. foundVar += "_FOUND";
  596. std::string notFoundMessageVar = this->Name;
  597. notFoundMessageVar += "_NOT_FOUND_MESSAGE";
  598. std::string notFoundMessage;
  599. // If the directory for the config file was found, try to read the file.
  600. bool result = true;
  601. bool found = false;
  602. bool configFileSetFOUNDFalse = false;
  603. if(fileFound)
  604. {
  605. if ((this->Makefile->IsDefinitionSet(foundVar.c_str()))
  606. && (this->Makefile->IsOn(foundVar.c_str()) == false))
  607. {
  608. // by removing Foo_FOUND here if it is FALSE, we don't really change
  609. // the situation for the Config file which is about to be included,
  610. // but we make it possible to detect later on whether the Config file
  611. // has set Foo_FOUND to FALSE itself:
  612. this->Makefile->RemoveDefinition(foundVar.c_str());
  613. }
  614. this->Makefile->RemoveDefinition(notFoundMessageVar.c_str());
  615. // Set the version variables before loading the config file.
  616. // It may override them.
  617. this->StoreVersionFound();
  618. // Parse the configuration file.
  619. if(this->ReadListFile(this->FileFound.c_str(), DoPolicyScope))
  620. {
  621. // The package has been found.
  622. found = true;
  623. // Check whether the Config file has set Foo_FOUND to FALSE:
  624. if ((this->Makefile->IsDefinitionSet(foundVar.c_str()))
  625. && (this->Makefile->IsOn(foundVar.c_str()) == false))
  626. {
  627. // we get here if the Config file has set Foo_FOUND actively to FALSE
  628. found = false;
  629. configFileSetFOUNDFalse = true;
  630. notFoundMessage = this->Makefile->GetSafeDefinition(
  631. notFoundMessageVar.c_str());
  632. }
  633. }
  634. else
  635. {
  636. // The configuration file is invalid.
  637. result = false;
  638. }
  639. }
  640. if (result && !found && (!this->Quiet || this->Required))
  641. {
  642. // The variable is not set.
  643. cmOStringStream e;
  644. cmOStringStream aw;
  645. if (configFileSetFOUNDFalse)
  646. {
  647. e << "Found package configuration file:\n"
  648. " " << this->FileFound << "\n"
  649. "but it set " << foundVar << " to FALSE so package \"" <<
  650. this->Name << "\" is considered to be NOT FOUND.";
  651. if (!notFoundMessage.empty())
  652. {
  653. e << " Reason given by package: \n" << notFoundMessage << "\n";
  654. }
  655. }
  656. // If there are files in ConsideredConfigs, it means that FooConfig.cmake
  657. // have been found, but they didn't have appropriate versions.
  658. else if (this->ConsideredConfigs.size() > 0)
  659. {
  660. e << "Could not find a configuration file for package \""
  661. << this->Name << "\" that "
  662. << (this->VersionExact? "exactly matches" : "is compatible with")
  663. << " requested version \"" << this->Version << "\".\n"
  664. << "The following configuration files were considered but not "
  665. "accepted:\n";
  666. for(std::vector<ConfigFileInfo>::size_type i=0;
  667. i<this->ConsideredConfigs.size(); i++)
  668. {
  669. e << " " << this->ConsideredConfigs[i].filename
  670. << ", version: " << this->ConsideredConfigs[i].version << "\n";
  671. }
  672. }
  673. else
  674. {
  675. std::string requestedVersionString;
  676. if(!this->Version.empty())
  677. {
  678. requestedVersionString = " (requested version ";
  679. requestedVersionString += this->Version;
  680. requestedVersionString += ")";
  681. }
  682. if (this->UseConfigFiles)
  683. {
  684. if(this->UseFindModules)
  685. {
  686. e << "By not providing \"Find" << this->Name << ".cmake\" in "
  687. "CMAKE_MODULE_PATH this project has asked CMake to find a "
  688. "package configuration file provided by \""<<this->Name<< "\", "
  689. "but CMake did not find one.\n";
  690. }
  691. if(this->Configs.size() == 1)
  692. {
  693. e << "Could not find a package configuration file named \""
  694. << this->Configs[0] << "\" provided by package \""
  695. << this->Name << "\"" << requestedVersionString <<".\n";
  696. }
  697. else
  698. {
  699. e << "Could not find a package configuration file provided by \""
  700. << this->Name << "\"" << requestedVersionString
  701. << " with any of the following names:\n";
  702. for(std::vector<std::string>::const_iterator ci =
  703. this->Configs.begin();
  704. ci != this->Configs.end(); ++ci)
  705. {
  706. e << " " << *ci << "\n";
  707. }
  708. }
  709. e << "Add the installation prefix of \"" << this->Name << "\" to "
  710. "CMAKE_PREFIX_PATH or set \"" << this->Variable << "\" to a "
  711. "directory containing one of the above files. "
  712. "If \"" << this->Name << "\" provides a separate development "
  713. "package or SDK, be sure it has been installed.";
  714. }
  715. else // if(!this->UseFindModules && !this->UseConfigFiles)
  716. {
  717. e << "No \"Find" << this->Name << ".cmake\" found in "
  718. << "CMAKE_MODULE_PATH.";
  719. aw<< "Find"<< this->Name <<".cmake must either be part of this "
  720. "project itself, in this case adjust CMAKE_MODULE_PATH so that "
  721. "it points to the correct location inside its source tree.\n"
  722. "Or it must be installed by a package which has already been "
  723. "found via find_package(). In this case make sure that "
  724. "package has indeed been found and adjust CMAKE_MODULE_PATH to "
  725. "contain the location where that package has installed "
  726. "Find" << this->Name << ".cmake. This must be a location "
  727. "provided by that package. This error in general means that "
  728. "the buildsystem of this project is relying on a Find-module "
  729. "without ensuring that it is actually available.\n";
  730. }
  731. }
  732. this->Makefile->IssueMessage(
  733. this->Required? cmake::FATAL_ERROR : cmake::WARNING, e.str());
  734. if (this->Required)
  735. {
  736. cmSystemTools::SetFatalErrorOccured();
  737. }
  738. if (!aw.str().empty())
  739. {
  740. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,aw.str());
  741. }
  742. }
  743. // Set a variable marking whether the package was found.
  744. this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  745. // Set a variable naming the configuration file that was found.
  746. std::string fileVar = this->Name;
  747. fileVar += "_CONFIG";
  748. if(found)
  749. {
  750. this->Makefile->AddDefinition(fileVar.c_str(), this->FileFound.c_str());
  751. }
  752. else
  753. {
  754. this->Makefile->RemoveDefinition(fileVar.c_str());
  755. }
  756. std::string consideredConfigsVar = this->Name;
  757. consideredConfigsVar += "_CONSIDERED_CONFIGS";
  758. std::string consideredVersionsVar = this->Name;
  759. consideredVersionsVar += "_CONSIDERED_VERSIONS";
  760. std::string consideredConfigFiles;
  761. std::string consideredVersions;
  762. const char* sep = "";
  763. for(std::vector<ConfigFileInfo>::size_type i=0;
  764. i<this->ConsideredConfigs.size(); i++)
  765. {
  766. consideredConfigFiles += sep;
  767. consideredVersions += sep;
  768. consideredConfigFiles += this->ConsideredConfigs[i].filename;
  769. consideredVersions += this->ConsideredConfigs[i].version;
  770. sep = ";";
  771. }
  772. this->Makefile->AddDefinition(consideredConfigsVar.c_str(),
  773. consideredConfigFiles.c_str());
  774. this->Makefile->AddDefinition(consideredVersionsVar.c_str(),
  775. consideredVersions.c_str());
  776. return result;
  777. }
  778. //----------------------------------------------------------------------------
  779. bool cmFindPackageCommand::FindConfig()
  780. {
  781. // Compute the set of search prefixes.
  782. this->ComputePrefixes();
  783. // Look for the project's configuration file.
  784. bool found = false;
  785. // Search for frameworks.
  786. if(!found && (this->SearchFrameworkFirst || this->SearchFrameworkOnly))
  787. {
  788. found = this->FindFrameworkConfig();
  789. }
  790. // Search for apps.
  791. if(!found && (this->SearchAppBundleFirst || this->SearchAppBundleOnly))
  792. {
  793. found = this->FindAppBundleConfig();
  794. }
  795. // Search prefixes.
  796. if(!found && !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  797. {
  798. found = this->FindPrefixedConfig();
  799. }
  800. // Search for frameworks.
  801. if(!found && this->SearchFrameworkLast)
  802. {
  803. found = this->FindFrameworkConfig();
  804. }
  805. // Search for apps.
  806. if(!found && this->SearchAppBundleLast)
  807. {
  808. found = this->FindAppBundleConfig();
  809. }
  810. // Store the entry in the cache so it can be set by the user.
  811. std::string init;
  812. if(found)
  813. {
  814. init = cmSystemTools::GetFilenamePath(this->FileFound);
  815. }
  816. else
  817. {
  818. init = this->Variable + "-NOTFOUND";
  819. }
  820. std::string help =
  821. "The directory containing a CMake configuration file for ";
  822. help += this->Name;
  823. help += ".";
  824. // We force the value since we do not get here if it was already set.
  825. this->Makefile->AddCacheDefinition(this->Variable.c_str(),
  826. init.c_str(), help.c_str(),
  827. cmCacheManager::PATH, true);
  828. return found;
  829. }
  830. //----------------------------------------------------------------------------
  831. bool cmFindPackageCommand::FindPrefixedConfig()
  832. {
  833. std::vector<std::string>& prefixes = this->SearchPaths;
  834. for(std::vector<std::string>::const_iterator pi = prefixes.begin();
  835. pi != prefixes.end(); ++pi)
  836. {
  837. if(this->SearchPrefix(*pi))
  838. {
  839. return true;
  840. }
  841. }
  842. return false;
  843. }
  844. //----------------------------------------------------------------------------
  845. bool cmFindPackageCommand::FindFrameworkConfig()
  846. {
  847. std::vector<std::string>& prefixes = this->SearchPaths;
  848. for(std::vector<std::string>::const_iterator i = prefixes.begin();
  849. i != prefixes.end(); ++i)
  850. {
  851. if(this->SearchFrameworkPrefix(*i))
  852. {
  853. return true;
  854. }
  855. }
  856. return false;
  857. }
  858. //----------------------------------------------------------------------------
  859. bool cmFindPackageCommand::FindAppBundleConfig()
  860. {
  861. std::vector<std::string>& prefixes = this->SearchPaths;
  862. for(std::vector<std::string>::const_iterator i = prefixes.begin();
  863. i != prefixes.end(); ++i)
  864. {
  865. if(this->SearchAppBundlePrefix(*i))
  866. {
  867. return true;
  868. }
  869. }
  870. return false;
  871. }
  872. //----------------------------------------------------------------------------
  873. bool cmFindPackageCommand::ReadListFile(const char* f, PolicyScopeRule psr)
  874. {
  875. if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(), f, 0,
  876. !this->PolicyScope || psr == NoPolicyScope))
  877. {
  878. return true;
  879. }
  880. std::string e = "Error reading CMake code from \"";
  881. e += f;
  882. e += "\".";
  883. this->SetError(e.c_str());
  884. return false;
  885. }
  886. //----------------------------------------------------------------------------
  887. void cmFindPackageCommand::AppendToFoundProperty(bool found)
  888. {
  889. std::vector<std::string> foundContents;
  890. const char *foundProp =
  891. this->Makefile->GetCMakeInstance()->GetProperty("PACKAGES_FOUND");
  892. if (foundProp && *foundProp)
  893. {
  894. std::string tmp = foundProp;
  895. cmSystemTools::ExpandListArgument(tmp, foundContents, false);
  896. std::vector<std::string>::iterator nameIt = std::find(
  897. foundContents.begin(), foundContents.end(), this->Name);
  898. if(nameIt != foundContents.end())
  899. {
  900. foundContents.erase(nameIt);
  901. }
  902. }
  903. std::vector<std::string> notFoundContents;
  904. const char *notFoundProp =
  905. this->Makefile->GetCMakeInstance()->GetProperty("PACKAGES_NOT_FOUND");
  906. if (notFoundProp && *notFoundProp)
  907. {
  908. std::string tmp = notFoundProp;
  909. cmSystemTools::ExpandListArgument(tmp, notFoundContents, false);
  910. std::vector<std::string>::iterator nameIt = std::find(
  911. notFoundContents.begin(), notFoundContents.end(), this->Name);
  912. if(nameIt != notFoundContents.end())
  913. {
  914. notFoundContents.erase(nameIt);
  915. }
  916. }
  917. if(found)
  918. {
  919. foundContents.push_back(this->Name);
  920. }
  921. else
  922. {
  923. notFoundContents.push_back(this->Name);
  924. }
  925. std::string tmp;
  926. const char* sep ="";
  927. for(size_t i=0; i<foundContents.size(); i++)
  928. {
  929. tmp += sep;
  930. tmp += foundContents[i];
  931. sep = ";";
  932. }
  933. this->Makefile->GetCMakeInstance()->SetProperty("PACKAGES_FOUND",
  934. tmp.c_str());
  935. tmp = "";
  936. sep = "";
  937. for(size_t i=0; i<notFoundContents.size(); i++)
  938. {
  939. tmp += sep;
  940. tmp += notFoundContents[i];
  941. sep = ";";
  942. }
  943. this->Makefile->GetCMakeInstance()->SetProperty("PACKAGES_NOT_FOUND",
  944. tmp.c_str());
  945. }
  946. //----------------------------------------------------------------------------
  947. void cmFindPackageCommand::AppendSuccessInformation()
  948. {
  949. {
  950. std::string transitivePropName = "_CMAKE_";
  951. transitivePropName += this->Name + "_TRANSITIVE_DEPENDENCY";
  952. this->Makefile->GetCMakeInstance()
  953. ->SetProperty(transitivePropName.c_str(), "False");
  954. }
  955. std::string found = this->Name;
  956. found += "_FOUND";
  957. std::string upperFound = cmSystemTools::UpperCase(found);
  958. const char* upperResult = this->Makefile->GetDefinition(upperFound.c_str());
  959. const char* result = this->Makefile->GetDefinition(found.c_str());
  960. bool packageFound = ((cmSystemTools::IsOn(result))
  961. || (cmSystemTools::IsOn(upperResult)));
  962. this->AppendToFoundProperty(packageFound);
  963. // Record whether the find was quiet or not, so this can be used
  964. // e.g. in FeatureSummary.cmake
  965. std::string quietInfoPropName = "_CMAKE_";
  966. quietInfoPropName += this->Name;
  967. quietInfoPropName += "_QUIET";
  968. this->Makefile->GetCMakeInstance()->SetProperty(quietInfoPropName.c_str(),
  969. this->Quiet ? "TRUE" : "FALSE");
  970. // set a global property to record the required version of this package
  971. std::string versionInfoPropName = "_CMAKE_";
  972. versionInfoPropName += this->Name;
  973. versionInfoPropName += "_REQUIRED_VERSION";
  974. std::string versionInfo;
  975. if(!this->Version.empty())
  976. {
  977. versionInfo = this->VersionExact ? "==" : ">=";
  978. versionInfo += " ";
  979. versionInfo += this->Version;
  980. }
  981. this->Makefile->GetCMakeInstance()->SetProperty(versionInfoPropName.c_str(),
  982. versionInfo.c_str());
  983. if (this->Required)
  984. {
  985. std::string requiredInfoPropName = "_CMAKE_";
  986. requiredInfoPropName += this->Name;
  987. requiredInfoPropName += "_TYPE";
  988. this->Makefile->GetCMakeInstance()->SetProperty(
  989. requiredInfoPropName.c_str(), "REQUIRED");
  990. }
  991. // Restore original state of "_FIND_" variables we set.
  992. this->RestoreFindDefinitions();
  993. }
  994. //----------------------------------------------------------------------------
  995. void cmFindPackageCommand::ComputePrefixes()
  996. {
  997. this->AddPrefixesCMakeVariable();
  998. this->AddPrefixesCMakeEnvironment();
  999. this->AddPrefixesUserHints();
  1000. this->AddPrefixesSystemEnvironment();
  1001. this->AddPrefixesUserRegistry();
  1002. this->AddPrefixesBuilds();
  1003. this->AddPrefixesCMakeSystemVariable();
  1004. this->AddPrefixesSystemRegistry();
  1005. this->AddPrefixesUserGuess();
  1006. this->ComputeFinalPaths();
  1007. }
  1008. //----------------------------------------------------------------------------
  1009. void cmFindPackageCommand::AddPrefixesCMakeEnvironment()
  1010. {
  1011. if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
  1012. {
  1013. // Check the environment variable with the same name as the cache
  1014. // entry.
  1015. std::string env;
  1016. if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
  1017. {
  1018. cmSystemTools::ConvertToUnixSlashes(env);
  1019. this->AddPathInternal(env, EnvPath);
  1020. }
  1021. this->AddEnvPath("CMAKE_PREFIX_PATH");
  1022. this->AddEnvPath("CMAKE_FRAMEWORK_PATH");
  1023. this->AddEnvPath("CMAKE_APPBUNDLE_PATH");
  1024. }
  1025. }
  1026. //----------------------------------------------------------------------------
  1027. void cmFindPackageCommand::AddPrefixesCMakeVariable()
  1028. {
  1029. if(!this->NoCMakePath && !this->NoDefaultPath)
  1030. {
  1031. this->AddCMakePath("CMAKE_PREFIX_PATH");
  1032. this->AddCMakePath("CMAKE_FRAMEWORK_PATH");
  1033. this->AddCMakePath("CMAKE_APPBUNDLE_PATH");
  1034. }
  1035. }
  1036. //----------------------------------------------------------------------------
  1037. void cmFindPackageCommand::AddPrefixesSystemEnvironment()
  1038. {
  1039. if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
  1040. {
  1041. // Use the system search path to generate prefixes.
  1042. // Relative paths are interpreted with respect to the current
  1043. // working directory.
  1044. std::vector<std::string> tmp;
  1045. cmSystemTools::GetPath(tmp);
  1046. for(std::vector<std::string>::iterator i = tmp.begin();
  1047. i != tmp.end(); ++i)
  1048. {
  1049. std::string const& d = *i;
  1050. // If the path is a PREFIX/bin case then add its parent instead.
  1051. if((cmHasLiteralSuffix(d, "/bin")) ||
  1052. (cmHasLiteralSuffix(d, "/sbin")))
  1053. {
  1054. this->AddPathInternal(cmSystemTools::GetFilenamePath(d), EnvPath);
  1055. }
  1056. else
  1057. {
  1058. this->AddPathInternal(d, EnvPath);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. //----------------------------------------------------------------------------
  1064. void cmFindPackageCommand::AddPrefixesUserRegistry()
  1065. {
  1066. if(this->NoUserRegistry || this->NoDefaultPath)
  1067. {
  1068. return;
  1069. }
  1070. #if defined(_WIN32) && !defined(__CYGWIN__)
  1071. this->LoadPackageRegistryWinUser();
  1072. #elif defined(__HAIKU__)
  1073. char dir[B_PATH_NAME_LENGTH];
  1074. if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  1075. B_OK)
  1076. {
  1077. std::string fname = dir;
  1078. fname += "/cmake/packages/";
  1079. fname += Name;
  1080. this->LoadPackageRegistryDir(fname);
  1081. }
  1082. #else
  1083. if(const char* home = cmSystemTools::GetEnv("HOME"))
  1084. {
  1085. std::string dir = home;
  1086. dir += "/.cmake/packages/";
  1087. dir += this->Name;
  1088. this->LoadPackageRegistryDir(dir);
  1089. }
  1090. #endif
  1091. }
  1092. //----------------------------------------------------------------------------
  1093. void cmFindPackageCommand::AddPrefixesSystemRegistry()
  1094. {
  1095. if(this->NoSystemRegistry || this->NoDefaultPath)
  1096. {
  1097. return;
  1098. }
  1099. #if defined(_WIN32) && !defined(__CYGWIN__)
  1100. this->LoadPackageRegistryWinSystem();
  1101. #endif
  1102. }
  1103. #if defined(_WIN32) && !defined(__CYGWIN__)
  1104. # include <windows.h>
  1105. # undef GetCurrentDirectory
  1106. // http://msdn.microsoft.com/en-us/library/aa384253%28v=vs.85%29.aspx
  1107. # if !defined(KEY_WOW64_32KEY)
  1108. # define KEY_WOW64_32KEY 0x0200
  1109. # endif
  1110. # if !defined(KEY_WOW64_64KEY)
  1111. # define KEY_WOW64_64KEY 0x0100
  1112. # endif
  1113. //----------------------------------------------------------------------------
  1114. void cmFindPackageCommand::LoadPackageRegistryWinUser()
  1115. {
  1116. // HKEY_CURRENT_USER\\Software shares 32-bit and 64-bit views.
  1117. this->LoadPackageRegistryWin(true, 0);
  1118. }
  1119. //----------------------------------------------------------------------------
  1120. void cmFindPackageCommand::LoadPackageRegistryWinSystem()
  1121. {
  1122. // HKEY_LOCAL_MACHINE\\SOFTWARE has separate 32-bit and 64-bit views.
  1123. // Prefer the target platform view first.
  1124. if(this->Makefile->PlatformIs64Bit())
  1125. {
  1126. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY);
  1127. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY);
  1128. }
  1129. else
  1130. {
  1131. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY);
  1132. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY);
  1133. }
  1134. }
  1135. //----------------------------------------------------------------------------
  1136. void cmFindPackageCommand::LoadPackageRegistryWin(bool user,
  1137. unsigned int view)
  1138. {
  1139. std::wstring key = L"Software\\Kitware\\CMake\\Packages\\";
  1140. key += cmsys::Encoding::ToWide(this->Name);
  1141. std::set<std::wstring> bad;
  1142. HKEY hKey;
  1143. if(RegOpenKeyExW(user? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE, key.c_str(),
  1144. 0, KEY_QUERY_VALUE|view, &hKey) == ERROR_SUCCESS)
  1145. {
  1146. DWORD valueType = REG_NONE;
  1147. wchar_t name[16383]; // RegEnumValue docs limit name to 32767 _bytes_
  1148. std::vector<wchar_t> data(512);
  1149. bool done = false;
  1150. DWORD index = 0;
  1151. while(!done)
  1152. {
  1153. DWORD nameSize = static_cast<DWORD>(sizeof(name));
  1154. DWORD dataSize = static_cast<DWORD>(data.size()*sizeof(data[0]));
  1155. switch(RegEnumValueW(hKey, index, name, &nameSize,
  1156. 0, &valueType, (BYTE*)&data[0], &dataSize))
  1157. {
  1158. case ERROR_SUCCESS:
  1159. ++index;
  1160. if(valueType == REG_SZ)
  1161. {
  1162. data[dataSize] = 0;
  1163. cmsys_ios::stringstream ss(cmsys::Encoding::ToNarrow(&data[0]));
  1164. if(!this->CheckPackageRegistryEntry(ss))
  1165. {
  1166. // The entry is invalid.
  1167. bad.insert(name);
  1168. }
  1169. }
  1170. break;
  1171. case ERROR_MORE_DATA:
  1172. data.resize((dataSize+sizeof(data[0])-1)/sizeof(data[0]));
  1173. break;
  1174. case ERROR_NO_MORE_ITEMS: default: done = true; break;
  1175. }
  1176. }
  1177. RegCloseKey(hKey);
  1178. }
  1179. // Remove bad values if possible.
  1180. if(user && !bad.empty() &&
  1181. RegOpenKeyExW(HKEY_CURRENT_USER, key.c_str(),
  1182. 0, KEY_SET_VALUE|view, &hKey) == ERROR_SUCCESS)
  1183. {
  1184. for(std::set<std::wstring>::const_iterator vi = bad.begin();
  1185. vi != bad.end(); ++vi)
  1186. {
  1187. RegDeleteValueW(hKey, vi->c_str());
  1188. }
  1189. RegCloseKey(hKey);
  1190. }
  1191. }
  1192. #else
  1193. //----------------------------------------------------------------------------
  1194. class cmFindPackageCommandHoldFile
  1195. {
  1196. const char* File;
  1197. public:
  1198. cmFindPackageCommandHoldFile(const char* f): File(f) {}
  1199. ~cmFindPackageCommandHoldFile()
  1200. { if(this->File) { cmSystemTools::RemoveFile(this->File); } }
  1201. void Release() { this->File = 0; }
  1202. };
  1203. //----------------------------------------------------------------------------
  1204. void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir)
  1205. {
  1206. cmsys::Directory files;
  1207. if(!files.Load(dir.c_str()))
  1208. {
  1209. return;
  1210. }
  1211. std::string fname;
  1212. for(unsigned long i=0; i < files.GetNumberOfFiles(); ++i)
  1213. {
  1214. fname = dir;
  1215. fname += "/";
  1216. fname += files.GetFile(i);
  1217. if(!cmSystemTools::FileIsDirectory(fname.c_str()))
  1218. {
  1219. // Hold this file hostage until it behaves.
  1220. cmFindPackageCommandHoldFile holdFile(fname.c_str());
  1221. // Load the file.
  1222. cmsys::ifstream fin(fname.c_str(), std::ios::in | cmsys_ios_binary);
  1223. if(fin && this->CheckPackageRegistryEntry(fin))
  1224. {
  1225. // The file references an existing package, so release it.
  1226. holdFile.Release();
  1227. }
  1228. }
  1229. }
  1230. // TODO: Wipe out the directory if it is empty.
  1231. }
  1232. #endif
  1233. //----------------------------------------------------------------------------
  1234. bool cmFindPackageCommand::CheckPackageRegistryEntry(std::istream& is)
  1235. {
  1236. // Parse the content of one package registry entry.
  1237. std::string fname;
  1238. if(cmSystemTools::GetLineFromStream(is, fname) &&
  1239. cmSystemTools::FileIsFullPath(fname.c_str()))
  1240. {
  1241. // The first line in the stream is the full path to a file or
  1242. // directory containing the package.
  1243. if(cmSystemTools::FileExists(fname.c_str()))
  1244. {
  1245. // The path exists. Look for the package here.
  1246. if(!cmSystemTools::FileIsDirectory(fname.c_str()))
  1247. {
  1248. fname = cmSystemTools::GetFilenamePath(fname);
  1249. }
  1250. this->AddPathInternal(fname, FullPath);
  1251. return true;
  1252. }
  1253. else
  1254. {
  1255. // The path does not exist. Assume the stream content is
  1256. // associated with an old package that no longer exists, and
  1257. // delete it to keep the package registry clean.
  1258. return false;
  1259. }
  1260. }
  1261. else
  1262. {
  1263. // The first line in the stream is not the full path to a file or
  1264. // directory. Assume the stream content was created by a future
  1265. // version of CMake that uses a different format, and leave it.
  1266. return true;
  1267. }
  1268. }
  1269. //----------------------------------------------------------------------------
  1270. void cmFindPackageCommand::AddPrefixesBuilds()
  1271. {
  1272. if(!this->NoBuilds && !this->NoDefaultPath)
  1273. {
  1274. // It is likely that CMake will have recently built the project.
  1275. for(int i=0; i <= 10; ++i)
  1276. {
  1277. cmOStringStream r;
  1278. r <<
  1279. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  1280. "Settings\\StartPath;WhereBuild" << i << "]";
  1281. std::string f = r.str();
  1282. cmSystemTools::ExpandRegistryValues(f);
  1283. cmSystemTools::ConvertToUnixSlashes(f);
  1284. if(cmSystemTools::FileIsFullPath(f.c_str()) &&
  1285. cmSystemTools::FileIsDirectory(f.c_str()))
  1286. {
  1287. this->AddPathInternal(f, FullPath);
  1288. }
  1289. }
  1290. }
  1291. }
  1292. //----------------------------------------------------------------------------
  1293. void cmFindPackageCommand::AddPrefixesCMakeSystemVariable()
  1294. {
  1295. if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
  1296. {
  1297. this->AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH");
  1298. this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  1299. this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  1300. }
  1301. }
  1302. //----------------------------------------------------------------------------
  1303. void cmFindPackageCommand::AddPrefixesUserGuess()
  1304. {
  1305. // Add guesses specified by the caller.
  1306. this->AddPathsInternal(this->UserPaths, CMakePath);
  1307. }
  1308. //----------------------------------------------------------------------------
  1309. void cmFindPackageCommand::AddPrefixesUserHints()
  1310. {
  1311. // Add hints specified by the caller.
  1312. this->AddPathsInternal(this->UserHints, CMakePath);
  1313. }
  1314. //----------------------------------------------------------------------------
  1315. bool cmFindPackageCommand::SearchDirectory(std::string const& dir)
  1316. {
  1317. assert(!dir.empty() && dir[dir.size()-1] == '/');
  1318. // Check each path suffix on this directory.
  1319. for(std::vector<std::string>::const_iterator
  1320. si = this->SearchPathSuffixes.begin();
  1321. si != this->SearchPathSuffixes.end(); ++si)
  1322. {
  1323. std::string d = dir;
  1324. if(!si->empty())
  1325. {
  1326. d += *si;
  1327. d += "/";
  1328. }
  1329. if(this->CheckDirectory(d))
  1330. {
  1331. return true;
  1332. }
  1333. }
  1334. return false;
  1335. }
  1336. //----------------------------------------------------------------------------
  1337. bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
  1338. {
  1339. assert(!dir.empty() && dir[dir.size()-1] == '/');
  1340. // Look for the file in this directory.
  1341. std::string d = dir.substr(0, dir.size()-1);
  1342. if(this->FindConfigFile(d, this->FileFound))
  1343. {
  1344. // Remove duplicate slashes.
  1345. cmSystemTools::ConvertToUnixSlashes(this->FileFound);
  1346. return true;
  1347. }
  1348. return false;
  1349. }
  1350. //----------------------------------------------------------------------------
  1351. bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
  1352. std::string& file)
  1353. {
  1354. if (this->IgnoredPaths.count(dir))
  1355. {
  1356. return false;
  1357. }
  1358. for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
  1359. ci != this->Configs.end(); ++ci)
  1360. {
  1361. file = dir;
  1362. file += "/";
  1363. file += *ci;
  1364. if(this->DebugMode)
  1365. {
  1366. fprintf(stderr, "Checking file [%s]\n", file.c_str());
  1367. }
  1368. if(cmSystemTools::FileExists(file.c_str(), true) &&
  1369. this->CheckVersion(file))
  1370. {
  1371. return true;
  1372. }
  1373. }
  1374. return false;
  1375. }
  1376. //----------------------------------------------------------------------------
  1377. bool cmFindPackageCommand::CheckVersion(std::string const& config_file)
  1378. {
  1379. bool result = false; // by default, assume the version is not ok.
  1380. bool haveResult = false;
  1381. std::string version = "unknown";
  1382. // Get the filename without the .cmake extension.
  1383. std::string::size_type pos = config_file.rfind('.');
  1384. std::string version_file_base = config_file.substr(0, pos);
  1385. // Look for foo-config-version.cmake
  1386. std::string version_file = version_file_base;
  1387. version_file += "-version.cmake";
  1388. if ((haveResult == false)
  1389. && (cmSystemTools::FileExists(version_file.c_str(), true)))
  1390. {
  1391. result = this->CheckVersionFile(version_file, version);
  1392. haveResult = true;
  1393. }
  1394. // Look for fooConfigVersion.cmake
  1395. version_file = version_file_base;
  1396. version_file += "Version.cmake";
  1397. if ((haveResult == false)
  1398. && (cmSystemTools::FileExists(version_file.c_str(), true)))
  1399. {
  1400. result = this->CheckVersionFile(version_file, version);
  1401. haveResult = true;
  1402. }
  1403. // If no version was requested a versionless package is acceptable.
  1404. if ((haveResult == false) && (this->Version.empty()))
  1405. {
  1406. result = true;
  1407. haveResult = true;
  1408. }
  1409. ConfigFileInfo configFileInfo;
  1410. configFileInfo.filename = config_file;
  1411. configFileInfo.version = version;
  1412. this->ConsideredConfigs.push_back(configFileInfo);
  1413. return result;
  1414. }
  1415. //----------------------------------------------------------------------------
  1416. bool cmFindPackageCommand::CheckVersionFile(std::string const& version_file,
  1417. std::string& result_version)
  1418. {
  1419. // The version file will be loaded in an isolated scope.
  1420. cmMakefile::ScopePushPop varScope(this->Makefile);
  1421. cmMakefile::PolicyPushPop polScope(this->Makefile);
  1422. static_cast<void>(varScope);
  1423. static_cast<void>(polScope);
  1424. // Clear the output variables.
  1425. this->Makefile->RemoveDefinition("PACKAGE_VERSION");
  1426. this->Makefile->RemoveDefinition("PACKAGE_VERSION_UNSUITABLE");
  1427. this->Makefile->RemoveDefinition("PACKAGE_VERSION_COMPATIBLE");
  1428. this->Makefile->RemoveDefinition("PACKAGE_VERSION_EXACT");
  1429. // Set the input variables.
  1430. this->Makefile->AddDefinition("PACKAGE_FIND_NAME", this->Name.c_str());
  1431. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION",
  1432. this->Version.c_str());
  1433. char buf[64];
  1434. sprintf(buf, "%u", this->VersionMajor);
  1435. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MAJOR", buf);
  1436. sprintf(buf, "%u", this->VersionMinor);
  1437. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MINOR", buf);
  1438. sprintf(buf, "%u", this->VersionPatch);
  1439. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_PATCH", buf);
  1440. sprintf(buf, "%u", this->VersionTweak);
  1441. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_TWEAK", buf);
  1442. sprintf(buf, "%u", this->VersionCount);
  1443. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_COUNT", buf);
  1444. // Load the version check file. Pass NoPolicyScope because we do
  1445. // our own policy push/pop independent of CMP0011.
  1446. bool suitable = false;
  1447. if(this->ReadListFile(version_file.c_str(), NoPolicyScope))
  1448. {
  1449. // Check the output variables.
  1450. bool okay = this->Makefile->IsOn("PACKAGE_VERSION_EXACT");
  1451. bool unsuitable = this->Makefile->IsOn("PACKAGE_VERSION_UNSUITABLE");
  1452. if(!okay && !this->VersionExact)
  1453. {
  1454. okay = this->Makefile->IsOn("PACKAGE_VERSION_COMPATIBLE");
  1455. }
  1456. // The package is suitable if the version is okay and not
  1457. // explicitly unsuitable.
  1458. suitable = !unsuitable && (okay || this->Version.empty());
  1459. if(suitable)
  1460. {
  1461. // Get the version found.
  1462. this->VersionFound =
  1463. this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1464. // Try to parse the version number and store the results that were
  1465. // successfully parsed.
  1466. unsigned int parsed_major;
  1467. unsigned int parsed_minor;
  1468. unsigned int parsed_patch;
  1469. unsigned int parsed_tweak;
  1470. this->VersionFoundCount =
  1471. sscanf(this->VersionFound.c_str(), "%u.%u.%u.%u",
  1472. &parsed_major, &parsed_minor,
  1473. &parsed_patch, &parsed_tweak);
  1474. switch(this->VersionFoundCount)
  1475. {
  1476. case 4: this->VersionFoundTweak = parsed_tweak; // no break!
  1477. case 3: this->VersionFoundPatch = parsed_patch; // no break!
  1478. case 2: this->VersionFoundMinor = parsed_minor; // no break!
  1479. case 1: this->VersionFoundMajor = parsed_major; // no break!
  1480. default: break;
  1481. }
  1482. }
  1483. }
  1484. result_version = this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1485. if (result_version.empty())
  1486. {
  1487. result_version = "unknown";
  1488. }
  1489. // Succeed if the version is suitable.
  1490. return suitable;
  1491. }
  1492. //----------------------------------------------------------------------------
  1493. void cmFindPackageCommand::StoreVersionFound()
  1494. {
  1495. // Store the whole version string.
  1496. std::string ver = this->Name;
  1497. ver += "_VERSION";
  1498. if(this->VersionFound.empty())
  1499. {
  1500. this->Makefile->RemoveDefinition(ver.c_str());
  1501. }
  1502. else
  1503. {
  1504. this->Makefile->AddDefinition(ver.c_str(), this->VersionFound.c_str());
  1505. }
  1506. // Store the version components.
  1507. char buf[64];
  1508. sprintf(buf, "%u", this->VersionFoundMajor);
  1509. this->Makefile->AddDefinition((ver+"_MAJOR").c_str(), buf);
  1510. sprintf(buf, "%u", this->VersionFoundMinor);
  1511. this->Makefile->AddDefinition((ver+"_MINOR").c_str(), buf);
  1512. sprintf(buf, "%u", this->VersionFoundPatch);
  1513. this->Makefile->AddDefinition((ver+"_PATCH").c_str(), buf);
  1514. sprintf(buf, "%u", this->VersionFoundTweak);
  1515. this->Makefile->AddDefinition((ver+"_TWEAK").c_str(), buf);
  1516. sprintf(buf, "%u", this->VersionFoundCount);
  1517. this->Makefile->AddDefinition((ver+"_COUNT").c_str(), buf);
  1518. }
  1519. //----------------------------------------------------------------------------
  1520. #include <cmsys/Glob.hxx>
  1521. #include <cmsys/String.h>
  1522. #include <cmsys/auto_ptr.hxx>
  1523. class cmFileList;
  1524. class cmFileListGeneratorBase
  1525. {
  1526. public:
  1527. virtual ~cmFileListGeneratorBase() {}
  1528. protected:
  1529. bool Consider(std::string const& fullPath, cmFileList& listing);
  1530. private:
  1531. bool Search(cmFileList&);
  1532. virtual bool Search(std::string const& parent, cmFileList&) = 0;
  1533. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const = 0;
  1534. friend class cmFileList;
  1535. cmFileListGeneratorBase* SetNext(cmFileListGeneratorBase const& next);
  1536. cmsys::auto_ptr<cmFileListGeneratorBase> Next;
  1537. };
  1538. class cmFileList
  1539. {
  1540. public:
  1541. cmFileList(): First(), Last(0) {}
  1542. virtual ~cmFileList() {}
  1543. cmFileList& operator/(cmFileListGeneratorBase const& rhs)
  1544. {
  1545. if(this->Last)
  1546. {
  1547. this->Last = this->Last->SetNext(rhs);
  1548. }
  1549. else
  1550. {
  1551. this->First = rhs.Clone();
  1552. this->Last = this->First.get();
  1553. }
  1554. return *this;
  1555. }
  1556. bool Search()
  1557. {
  1558. if(this->First.get())
  1559. {
  1560. return this->First->Search(*this);
  1561. }
  1562. return false;
  1563. }
  1564. private:
  1565. virtual bool Visit(std::string const& fullPath) = 0;
  1566. friend class cmFileListGeneratorBase;
  1567. cmsys::auto_ptr<cmFileListGeneratorBase> First;
  1568. cmFileListGeneratorBase* Last;
  1569. };
  1570. class cmFindPackageFileList: public cmFileList
  1571. {
  1572. public:
  1573. cmFindPackageFileList(cmFindPackageCommand* fpc,
  1574. bool use_suffixes = true):
  1575. cmFileList(), FPC(fpc), UseSuffixes(use_suffixes) {}
  1576. private:
  1577. bool Visit(std::string const& fullPath)
  1578. {
  1579. if(this->UseSuffixes)
  1580. {
  1581. return this->FPC->SearchDirectory(fullPath);
  1582. }
  1583. else
  1584. {
  1585. return this->FPC->CheckDirectory(fullPath);
  1586. }
  1587. }
  1588. cmFindPackageCommand* FPC;
  1589. bool UseSuffixes;
  1590. };
  1591. bool cmFileListGeneratorBase::Search(cmFileList& listing)
  1592. {
  1593. return this->Search("", listing);
  1594. }
  1595. cmFileListGeneratorBase*
  1596. cmFileListGeneratorBase::SetNext(cmFileListGeneratorBase const& next)
  1597. {
  1598. this->Next = next.Clone();
  1599. return this->Next.get();
  1600. }
  1601. bool cmFileListGeneratorBase::Consider(std::string const& fullPath,
  1602. cmFileList& listing)
  1603. {
  1604. if(this->Next.get())
  1605. {
  1606. return this->Next->Search(fullPath + "/", listing);
  1607. }
  1608. else
  1609. {
  1610. return listing.Visit(fullPath + "/");
  1611. }
  1612. }
  1613. class cmFileListGeneratorFixed: public cmFileListGeneratorBase
  1614. {
  1615. public:
  1616. cmFileListGeneratorFixed(std::string const& str):
  1617. cmFileListGeneratorBase(), String(str) {}
  1618. cmFileListGeneratorFixed(cmFileListGeneratorFixed const& r):
  1619. cmFileListGeneratorBase(), String(r.String) {}
  1620. private:
  1621. std::string String;
  1622. virtual bool Search(std::string const& parent, cmFileList& lister)
  1623. {
  1624. std::string fullPath = parent + this->String;
  1625. return this->Consider(fullPath, lister);
  1626. }
  1627. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1628. {
  1629. cmsys::auto_ptr<cmFileListGeneratorBase>
  1630. g(new cmFileListGeneratorFixed(*this));
  1631. return g;
  1632. }
  1633. };
  1634. class cmFileListGeneratorEnumerate: public cmFileListGeneratorBase
  1635. {
  1636. public:
  1637. cmFileListGeneratorEnumerate(std::vector<std::string> const& v):
  1638. cmFileListGeneratorBase(), Vector(v) {}
  1639. cmFileListGeneratorEnumerate(cmFileListGeneratorEnumerate const& r):
  1640. cmFileListGeneratorBase(), Vector(r.Vector) {}
  1641. private:
  1642. std::vector<std::string> const& Vector;
  1643. virtual bool Search(std::string const& parent, cmFileList& lister)
  1644. {
  1645. for(std::vector<std::string>::const_iterator i = this->Vector.begin();
  1646. i != this->Vector.end(); ++i)
  1647. {
  1648. if(this->Consider(parent + *i, lister))
  1649. {
  1650. return true;
  1651. }
  1652. }
  1653. return false;
  1654. }
  1655. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1656. {
  1657. cmsys::auto_ptr<cmFileListGeneratorBase>
  1658. g(new cmFileListGeneratorEnumerate(*this));
  1659. return g;
  1660. }
  1661. };
  1662. class cmFileListGeneratorProject: public cmFileListGeneratorBase
  1663. {
  1664. public:
  1665. cmFileListGeneratorProject(std::vector<std::string> const& names):
  1666. cmFileListGeneratorBase(), Names(names) {}
  1667. cmFileListGeneratorProject(cmFileListGeneratorProject const& r):
  1668. cmFileListGeneratorBase(), Names(r.Names) {}
  1669. private:
  1670. std::vector<std::string> const& Names;
  1671. virtual bool Search(std::string const& parent, cmFileList& lister)
  1672. {
  1673. // Construct a list of matches.
  1674. std::vector<std::string> matches;
  1675. cmsys::Directory d;
  1676. d.Load(parent.c_str());
  1677. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1678. {
  1679. const char* fname = d.GetFile(i);
  1680. if(strcmp(fname, ".") == 0 ||
  1681. strcmp(fname, "..") == 0)
  1682. {
  1683. continue;
  1684. }
  1685. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1686. ni != this->Names.end(); ++ni)
  1687. {
  1688. if(cmsysString_strncasecmp(fname, ni->c_str(),
  1689. ni->length()) == 0)
  1690. {
  1691. matches.push_back(fname);
  1692. }
  1693. }
  1694. }
  1695. for(std::vector<std::string>::const_iterator i = matches.begin();
  1696. i != matches.end(); ++i)
  1697. {
  1698. if(this->Consider(parent + *i, lister))
  1699. {
  1700. return true;
  1701. }
  1702. }
  1703. return false;
  1704. }
  1705. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1706. {
  1707. cmsys::auto_ptr<cmFileListGeneratorBase>
  1708. g(new cmFileListGeneratorProject(*this));
  1709. return g;
  1710. }
  1711. };
  1712. class cmFileListGeneratorMacProject: public cmFileListGeneratorBase
  1713. {
  1714. public:
  1715. cmFileListGeneratorMacProject(std::vector<std::string> const& names,
  1716. const char* ext):
  1717. cmFileListGeneratorBase(), Names(names), Extension(ext) {}
  1718. cmFileListGeneratorMacProject(cmFileListGeneratorMacProject const& r):
  1719. cmFileListGeneratorBase(), Names(r.Names), Extension(r.Extension) {}
  1720. private:
  1721. std::vector<std::string> const& Names;
  1722. std::string Extension;
  1723. virtual bool Search(std::string const& parent, cmFileList& lister)
  1724. {
  1725. // Construct a list of matches.
  1726. std::vector<std::string> matches;
  1727. cmsys::Directory d;
  1728. d.Load(parent.c_str());
  1729. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1730. {
  1731. const char* fname = d.GetFile(i);
  1732. if(strcmp(fname, ".") == 0 ||
  1733. strcmp(fname, "..") == 0)
  1734. {
  1735. continue;
  1736. }
  1737. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1738. ni != this->Names.end(); ++ni)
  1739. {
  1740. std::string name = *ni;
  1741. name += this->Extension;
  1742. if(cmsysString_strcasecmp(fname, name.c_str()) == 0)
  1743. {
  1744. matches.push_back(fname);
  1745. }
  1746. }
  1747. }
  1748. for(std::vector<std::string>::const_iterator i = matches.begin();
  1749. i != matches.end(); ++i)
  1750. {
  1751. if(this->Consider(parent + *i, lister))
  1752. {
  1753. return true;
  1754. }
  1755. }
  1756. return false;
  1757. }
  1758. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1759. {
  1760. cmsys::auto_ptr<cmFileListGeneratorBase>
  1761. g(new cmFileListGeneratorMacProject(*this));
  1762. return g;
  1763. }
  1764. };
  1765. class cmFileListGeneratorCaseInsensitive: public cmFileListGeneratorBase
  1766. {
  1767. public:
  1768. cmFileListGeneratorCaseInsensitive(std::string const& str):
  1769. cmFileListGeneratorBase(), String(str) {}
  1770. cmFileListGeneratorCaseInsensitive(
  1771. cmFileListGeneratorCaseInsensitive const& r):
  1772. cmFileListGeneratorBase(), String(r.String) {}
  1773. private:
  1774. std::string String;
  1775. virtual bool Search(std::string const& parent, cmFileList& lister)
  1776. {
  1777. // Look for matching files.
  1778. std::vector<std::string> matches;
  1779. cmsys::Directory d;
  1780. d.Load(parent.c_str());
  1781. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1782. {
  1783. const char* fname = d.GetFile(i);
  1784. if(strcmp(fname, ".") == 0 ||
  1785. strcmp(fname, "..") == 0)
  1786. {
  1787. continue;
  1788. }
  1789. if(cmsysString_strcasecmp(fname, this->String.c_str()) == 0)
  1790. {
  1791. if(this->Consider(parent + fname, lister))
  1792. {
  1793. return true;
  1794. }
  1795. }
  1796. }
  1797. return false;
  1798. }
  1799. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1800. {
  1801. cmsys::auto_ptr<cmFileListGeneratorBase>
  1802. g(new cmFileListGeneratorCaseInsensitive(*this));
  1803. return g;
  1804. }
  1805. };
  1806. class cmFileListGeneratorGlob: public cmFileListGeneratorBase
  1807. {
  1808. public:
  1809. cmFileListGeneratorGlob(std::string const& str):
  1810. cmFileListGeneratorBase(), Pattern(str) {}
  1811. cmFileListGeneratorGlob(cmFileListGeneratorGlob const& r):
  1812. cmFileListGeneratorBase(), Pattern(r.Pattern) {}
  1813. private:
  1814. std::string Pattern;
  1815. virtual bool Search(std::string const& parent, cmFileList& lister)
  1816. {
  1817. // Glob the set of matching files.
  1818. std::string expr = parent;
  1819. expr += this->Pattern;
  1820. cmsys::Glob g;
  1821. if(!g.FindFiles(expr))
  1822. {
  1823. return false;
  1824. }
  1825. std::vector<std::string> const& files = g.GetFiles();
  1826. // Look for directories among the matches.
  1827. for(std::vector<std::string>::const_iterator fi = files.begin();
  1828. fi != files.end(); ++fi)
  1829. {
  1830. if(cmSystemTools::FileIsDirectory(fi->c_str()))
  1831. {
  1832. if(this->Consider(*fi, lister))
  1833. {
  1834. return true;
  1835. }
  1836. }
  1837. }
  1838. return false;
  1839. }
  1840. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1841. {
  1842. cmsys::auto_ptr<cmFileListGeneratorBase>
  1843. g(new cmFileListGeneratorGlob(*this));
  1844. return g;
  1845. }
  1846. };
  1847. //----------------------------------------------------------------------------
  1848. bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
  1849. {
  1850. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1851. if(this->DebugMode)
  1852. {
  1853. fprintf(stderr, "Checking prefix [%s]\n", prefix_in.c_str());
  1854. }
  1855. // Skip this if the prefix does not exist.
  1856. if(!cmSystemTools::FileIsDirectory(prefix_in.c_str()))
  1857. {
  1858. return false;
  1859. }
  1860. // PREFIX/ (useful on windows or in build trees)
  1861. if(this->SearchDirectory(prefix_in))
  1862. {
  1863. return true;
  1864. }
  1865. // Strip the trailing slash because the path generator is about to
  1866. // add one.
  1867. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1868. // PREFIX/(cmake|CMake)/ (useful on windows or in build trees)
  1869. {
  1870. cmFindPackageFileList lister(this);
  1871. lister
  1872. / cmFileListGeneratorFixed(prefix)
  1873. / cmFileListGeneratorCaseInsensitive("cmake");
  1874. if(lister.Search())
  1875. {
  1876. return true;
  1877. }
  1878. }
  1879. // PREFIX/(Foo|foo|FOO).*/
  1880. {
  1881. cmFindPackageFileList lister(this);
  1882. lister
  1883. / cmFileListGeneratorFixed(prefix)
  1884. / cmFileListGeneratorProject(this->Names);
  1885. if(lister.Search())
  1886. {
  1887. return true;
  1888. }
  1889. }
  1890. // PREFIX/(Foo|foo|FOO).*/(cmake|CMake)/
  1891. {
  1892. cmFindPackageFileList lister(this);
  1893. lister
  1894. / cmFileListGeneratorFixed(prefix)
  1895. / cmFileListGeneratorProject(this->Names)
  1896. / cmFileListGeneratorCaseInsensitive("cmake");
  1897. if(lister.Search())
  1898. {
  1899. return true;
  1900. }
  1901. }
  1902. // Construct list of common install locations (lib and share).
  1903. std::vector<std::string> common;
  1904. if(!this->LibraryArchitecture.empty())
  1905. {
  1906. common.push_back("lib/"+this->LibraryArchitecture);
  1907. }
  1908. if(this->UseLib64Paths)
  1909. {
  1910. common.push_back("lib64");
  1911. }
  1912. common.push_back("lib");
  1913. common.push_back("share");
  1914. // PREFIX/(lib/ARCH|lib|share)/cmake/(Foo|foo|FOO).*/
  1915. {
  1916. cmFindPackageFileList lister(this);
  1917. lister
  1918. / cmFileListGeneratorFixed(prefix)
  1919. / cmFileListGeneratorEnumerate(common)
  1920. / cmFileListGeneratorFixed("cmake")
  1921. / cmFileListGeneratorProject(this->Names);
  1922. if(lister.Search())
  1923. {
  1924. return true;
  1925. }
  1926. }
  1927. // PREFIX/(lib/ARCH|lib|share)/(Foo|foo|FOO).*/
  1928. {
  1929. cmFindPackageFileList lister(this);
  1930. lister
  1931. / cmFileListGeneratorFixed(prefix)
  1932. / cmFileListGeneratorEnumerate(common)
  1933. / cmFileListGeneratorProject(this->Names);
  1934. if(lister.Search())
  1935. {
  1936. return true;
  1937. }
  1938. }
  1939. // PREFIX/(lib/ARCH|lib|share)/(Foo|foo|FOO).*/(cmake|CMake)/
  1940. {
  1941. cmFindPackageFileList lister(this);
  1942. lister
  1943. / cmFileListGeneratorFixed(prefix)
  1944. / cmFileListGeneratorEnumerate(common)
  1945. / cmFileListGeneratorProject(this->Names)
  1946. / cmFileListGeneratorCaseInsensitive("cmake");
  1947. if(lister.Search())
  1948. {
  1949. return true;
  1950. }
  1951. }
  1952. return false;
  1953. }
  1954. //----------------------------------------------------------------------------
  1955. bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in)
  1956. {
  1957. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1958. if(this->DebugMode)
  1959. {
  1960. fprintf(stderr, "Checking framework prefix [%s]\n", prefix_in.c_str());
  1961. }
  1962. // Strip the trailing slash because the path generator is about to
  1963. // add one.
  1964. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1965. // <prefix>/Foo.framework/Resources/
  1966. {
  1967. cmFindPackageFileList lister(this);
  1968. lister
  1969. / cmFileListGeneratorFixed(prefix)
  1970. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1971. / cmFileListGeneratorFixed("Resources");
  1972. if(lister.Search())
  1973. {
  1974. return true;
  1975. }
  1976. }
  1977. // <prefix>/Foo.framework/Resources/CMake/
  1978. {
  1979. cmFindPackageFileList lister(this);
  1980. lister
  1981. / cmFileListGeneratorFixed(prefix)
  1982. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1983. / cmFileListGeneratorFixed("Resources")
  1984. / cmFileListGeneratorCaseInsensitive("cmake");
  1985. if(lister.Search())
  1986. {
  1987. return true;
  1988. }
  1989. }
  1990. // <prefix>/Foo.framework/Versions/*/Resources/
  1991. {
  1992. cmFindPackageFileList lister(this);
  1993. lister
  1994. / cmFileListGeneratorFixed(prefix)
  1995. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1996. / cmFileListGeneratorFixed("Versions")
  1997. / cmFileListGeneratorGlob("*/Resources");
  1998. if(lister.Search())
  1999. {
  2000. return true;
  2001. }
  2002. }
  2003. // <prefix>/Foo.framework/Versions/*/Resources/CMake/
  2004. {
  2005. cmFindPackageFileList lister(this);
  2006. lister
  2007. / cmFileListGeneratorFixed(prefix)
  2008. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2009. / cmFileListGeneratorFixed("Versions")
  2010. / cmFileListGeneratorGlob("*/Resources")
  2011. / cmFileListGeneratorCaseInsensitive("cmake");
  2012. if(lister.Search())
  2013. {
  2014. return true;
  2015. }
  2016. }
  2017. return false;
  2018. }
  2019. //----------------------------------------------------------------------------
  2020. bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in)
  2021. {
  2022. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  2023. if(this->DebugMode)
  2024. {
  2025. fprintf(stderr, "Checking bundle prefix [%s]\n", prefix_in.c_str());
  2026. }
  2027. // Strip the trailing slash because the path generator is about to
  2028. // add one.
  2029. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  2030. // <prefix>/Foo.app/Contents/Resources
  2031. {
  2032. cmFindPackageFileList lister(this);
  2033. lister
  2034. / cmFileListGeneratorFixed(prefix)
  2035. / cmFileListGeneratorMacProject(this->Names, ".app")
  2036. / cmFileListGeneratorFixed("Contents/Resources");
  2037. if(lister.Search())
  2038. {
  2039. return true;
  2040. }
  2041. }
  2042. // <prefix>/Foo.app/Contents/Resources/CMake
  2043. {
  2044. cmFindPackageFileList lister(this);
  2045. lister
  2046. / cmFileListGeneratorFixed(prefix)
  2047. / cmFileListGeneratorMacProject(this->Names, ".app")
  2048. / cmFileListGeneratorFixed("Contents/Resources")
  2049. / cmFileListGeneratorCaseInsensitive("cmake");
  2050. if(lister.Search())
  2051. {
  2052. return true;
  2053. }
  2054. }
  2055. return false;
  2056. }
  2057. // TODO: Debug cmsys::Glob double slash problem.