cmFindPackageCommand.cxx 68 KB

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