cmFindPackageCommand.cxx 66 KB

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