cmExportInstallFileGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 "cmExportInstallFileGenerator.h"
  11. #include "cmExportSet.h"
  12. #include "cmExportSetMap.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmInstallExportGenerator.h"
  17. #include "cmInstallTargetGenerator.h"
  18. #include "cmTargetExport.h"
  19. //----------------------------------------------------------------------------
  20. cmExportInstallFileGenerator
  21. ::cmExportInstallFileGenerator(cmInstallExportGenerator* iegen):
  22. IEGen(iegen)
  23. {
  24. }
  25. //----------------------------------------------------------------------------
  26. std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
  27. {
  28. std::string glob = this->FileBase;
  29. glob += "-*";
  30. glob += this->FileExt;
  31. return glob;
  32. }
  33. //----------------------------------------------------------------------------
  34. bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
  35. {
  36. std::vector<cmTargetExport*> allTargets;
  37. {
  38. std::string expectedTargets;
  39. std::string sep;
  40. for(std::vector<cmTargetExport*>::const_iterator
  41. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  42. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  43. {
  44. expectedTargets += sep + this->Namespace + (*tei)->Target->GetExportName();
  45. sep = " ";
  46. cmTargetExport * te = *tei;
  47. if(this->ExportedTargets.insert(te->Target).second)
  48. {
  49. allTargets.push_back(te);
  50. }
  51. else
  52. {
  53. cmOStringStream e;
  54. e << "install(EXPORT \""
  55. << this->IEGen->GetExportSet()->GetName()
  56. << "\" ...) " << "includes target \"" << te->Target->GetName()
  57. << "\" more than once in the export set.";
  58. cmSystemTools::Error(e.str().c_str());
  59. return false;
  60. }
  61. }
  62. this->GenerateExpectedTargetsCode(os, expectedTargets);
  63. }
  64. // Add code to compute the installation prefix relative to the
  65. // import file location.
  66. const char* installDest = this->IEGen->GetDestination();
  67. if(!cmSystemTools::FileIsFullPath(installDest))
  68. {
  69. std::string installPrefix =
  70. this->IEGen->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  71. std::string absDest = installPrefix + "/" + installDest;
  72. std::string absDestS = absDest + "/";
  73. os << "# Compute the installation prefix relative to this file.\n"
  74. << "get_filename_component(_IMPORT_PREFIX"
  75. << " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
  76. if(cmHasLiteralPrefix(absDestS.c_str(), "/lib/") ||
  77. cmHasLiteralPrefix(absDestS.c_str(), "/lib64/") ||
  78. cmHasLiteralPrefix(absDestS.c_str(), "/usr/lib/") ||
  79. cmHasLiteralPrefix(absDestS.c_str(), "/usr/lib64/"))
  80. {
  81. // Handle "/usr move" symlinks created by some Linux distros.
  82. os <<
  83. "# Use original install prefix when loaded through a\n"
  84. "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
  85. "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
  86. "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
  87. "if(_realCurr STREQUAL _realOrig)\n"
  88. " set(_IMPORT_PREFIX \"" << absDest << "\")\n"
  89. "endif()\n"
  90. "unset(_realOrig)\n"
  91. "unset(_realCurr)\n";
  92. }
  93. std::string dest = installDest;
  94. while(!dest.empty())
  95. {
  96. os <<
  97. "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" PATH)\n";
  98. dest = cmSystemTools::GetFilenamePath(dest);
  99. }
  100. os << "\n";
  101. // Import location properties may reference this variable.
  102. this->ImportPrefix = "${_IMPORT_PREFIX}/";
  103. }
  104. std::vector<std::string> missingTargets;
  105. bool require2_8_12 = false;
  106. bool require3_0_0 = false;
  107. bool requiresConfigFiles = false;
  108. // Create all the imported targets.
  109. for(std::vector<cmTargetExport*>::const_iterator
  110. tei = allTargets.begin();
  111. tei != allTargets.end(); ++tei)
  112. {
  113. cmTarget* te = (*tei)->Target;
  114. requiresConfigFiles = requiresConfigFiles
  115. || te->GetType() != cmTarget::INTERFACE_LIBRARY;
  116. this->GenerateImportTargetCode(os, te);
  117. ImportPropertyMap properties;
  118. this->PopulateIncludeDirectoriesInterface(*tei,
  119. cmGeneratorExpression::InstallInterface,
  120. properties, missingTargets);
  121. this->PopulateInterfaceProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES",
  122. te,
  123. cmGeneratorExpression::InstallInterface,
  124. properties, missingTargets);
  125. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
  126. te,
  127. cmGeneratorExpression::InstallInterface,
  128. properties, missingTargets);
  129. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS",
  130. te,
  131. cmGeneratorExpression::InstallInterface,
  132. properties, missingTargets);
  133. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS",
  134. te,
  135. cmGeneratorExpression::InstallInterface,
  136. properties, missingTargets);
  137. const bool newCMP0022Behavior =
  138. te->GetPolicyStatusCMP0022() != cmPolicies::WARN
  139. && te->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  140. if (newCMP0022Behavior)
  141. {
  142. if (this->PopulateInterfaceLinkLibrariesProperty(te,
  143. cmGeneratorExpression::InstallInterface,
  144. properties, missingTargets)
  145. && !this->ExportOld)
  146. {
  147. require2_8_12 = true;
  148. }
  149. }
  150. if (te->GetType() == cmTarget::INTERFACE_LIBRARY)
  151. {
  152. require3_0_0 = true;
  153. }
  154. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
  155. te, properties);
  156. this->PopulateCompatibleInterfaceProperties(te, properties);
  157. this->GenerateInterfaceProperties(te, os, properties);
  158. }
  159. if (require3_0_0)
  160. {
  161. this->GenerateRequiredCMakeVersion(os, "2.8.12.20131007");
  162. }
  163. else if (require2_8_12)
  164. {
  165. this->GenerateRequiredCMakeVersion(os, "2.8.12");
  166. }
  167. // Now load per-configuration properties for them.
  168. os << "# Load information for each installed configuration.\n"
  169. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  170. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  171. << this->GetConfigImportFileGlob() << "\")\n"
  172. << "foreach(f ${CONFIG_FILES})\n"
  173. << " include(${f})\n"
  174. << "endforeach()\n"
  175. << "\n";
  176. // Cleanup the import prefix variable.
  177. if(!this->ImportPrefix.empty())
  178. {
  179. os << "# Cleanup temporary variables.\n"
  180. << "set(_IMPORT_PREFIX)\n"
  181. << "\n";
  182. }
  183. this->GenerateImportedFileCheckLoop(os);
  184. bool result = true;
  185. // Generate an import file for each configuration.
  186. // Don't do this if we only export INTERFACE_LIBRARY targets.
  187. if (requiresConfigFiles)
  188. {
  189. for(std::vector<std::string>::const_iterator
  190. ci = this->Configurations.begin();
  191. ci != this->Configurations.end(); ++ci)
  192. {
  193. if(!this->GenerateImportFileConfig(ci->c_str(), missingTargets))
  194. {
  195. result = false;
  196. }
  197. }
  198. }
  199. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  200. return result;
  201. }
  202. //----------------------------------------------------------------------------
  203. void
  204. cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string &input)
  205. {
  206. std::string::size_type pos = 0;
  207. std::string::size_type lastPos = pos;
  208. while((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos)
  209. {
  210. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  211. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  212. lastPos = endPos;
  213. }
  214. }
  215. //----------------------------------------------------------------------------
  216. bool
  217. cmExportInstallFileGenerator::GenerateImportFileConfig(const char* config,
  218. std::vector<std::string> &missingTargets)
  219. {
  220. // Skip configurations not enabled for this export.
  221. if(!this->IEGen->InstallsForConfig(config))
  222. {
  223. return true;
  224. }
  225. // Construct the name of the file to generate.
  226. std::string fileName = this->FileDir;
  227. fileName += "/";
  228. fileName += this->FileBase;
  229. fileName += "-";
  230. if(config && *config)
  231. {
  232. fileName += cmSystemTools::LowerCase(config);
  233. }
  234. else
  235. {
  236. fileName += "noconfig";
  237. }
  238. fileName += this->FileExt;
  239. // Open the output file to generate it.
  240. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  241. if(!exportFileStream)
  242. {
  243. std::string se = cmSystemTools::GetLastSystemError();
  244. cmOStringStream e;
  245. e << "cannot write to file \"" << fileName.c_str()
  246. << "\": " << se;
  247. cmSystemTools::Error(e.str().c_str());
  248. return false;
  249. }
  250. std::ostream& os = exportFileStream;
  251. // Start with the import file header.
  252. this->GenerateImportHeaderCode(os, config);
  253. // Generate the per-config target information.
  254. this->GenerateImportConfig(os, config, missingTargets);
  255. // End with the import file footer.
  256. this->GenerateImportFooterCode(os);
  257. // Record this per-config import file.
  258. this->ConfigImportFiles[config] = fileName;
  259. return true;
  260. }
  261. //----------------------------------------------------------------------------
  262. void
  263. cmExportInstallFileGenerator
  264. ::GenerateImportTargetsConfig(std::ostream& os,
  265. const char* config, std::string const& suffix,
  266. std::vector<std::string> &missingTargets)
  267. {
  268. // Add each target in the set to the export.
  269. for(std::vector<cmTargetExport*>::const_iterator
  270. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  271. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  272. {
  273. // Collect import properties for this target.
  274. cmTargetExport const* te = *tei;
  275. if (te->Target->GetType() == cmTarget::INTERFACE_LIBRARY)
  276. {
  277. continue;
  278. }
  279. ImportPropertyMap properties;
  280. std::set<std::string> importedLocations;
  281. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  282. properties, importedLocations);
  283. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  284. properties, importedLocations);
  285. this->SetImportLocationProperty(config, suffix,
  286. te->RuntimeGenerator, properties,
  287. importedLocations);
  288. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  289. properties, importedLocations);
  290. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  291. properties, importedLocations);
  292. // If any file location was set for the target add it to the
  293. // import file.
  294. if(!properties.empty())
  295. {
  296. // Get the rest of the target details.
  297. this->SetImportDetailProperties(config, suffix,
  298. te->Target, properties, missingTargets);
  299. this->SetImportLinkInterface(config, suffix,
  300. cmGeneratorExpression::InstallInterface,
  301. te->Target, properties, missingTargets);
  302. // TOOD: PUBLIC_HEADER_LOCATION
  303. // This should wait until the build feature propagation stuff
  304. // is done. Then this can be a propagated include directory.
  305. // this->GenerateImportProperty(config, te->HeaderGenerator,
  306. // properties);
  307. // Generate code in the export file.
  308. this->GenerateImportPropertyCode(os, config, te->Target, properties);
  309. this->GenerateImportedFileChecksCode(os, te->Target, properties,
  310. importedLocations);
  311. }
  312. }
  313. }
  314. //----------------------------------------------------------------------------
  315. void
  316. cmExportInstallFileGenerator
  317. ::SetImportLocationProperty(const char* config, std::string const& suffix,
  318. cmInstallTargetGenerator* itgen,
  319. ImportPropertyMap& properties,
  320. std::set<std::string>& importedLocations
  321. )
  322. {
  323. // Skip rules that do not match this configuration.
  324. if(!(itgen && itgen->InstallsForConfig(config)))
  325. {
  326. return;
  327. }
  328. // Get the target to be installed.
  329. cmTarget* target = itgen->GetTarget();
  330. // Construct the installed location of the target.
  331. std::string dest = itgen->GetDestination();
  332. std::string value;
  333. if(!cmSystemTools::FileIsFullPath(dest.c_str()))
  334. {
  335. // The target is installed relative to the installation prefix.
  336. if(this->ImportPrefix.empty())
  337. {
  338. this->ComplainAboutImportPrefix(itgen);
  339. }
  340. value = this->ImportPrefix;
  341. }
  342. value += dest;
  343. value += "/";
  344. if(itgen->IsImportLibrary())
  345. {
  346. // Construct the property name.
  347. std::string prop = "IMPORTED_IMPLIB";
  348. prop += suffix;
  349. // Append the installed file name.
  350. value += itgen->GetInstallFilename(target, config,
  351. cmInstallTargetGenerator::NameImplib);
  352. // Store the property.
  353. properties[prop] = value;
  354. importedLocations.insert(prop);
  355. }
  356. else
  357. {
  358. // Construct the property name.
  359. std::string prop = "IMPORTED_LOCATION";
  360. prop += suffix;
  361. // Append the installed file name.
  362. if(target->IsAppBundleOnApple())
  363. {
  364. value += itgen->GetInstallFilename(target, config);
  365. value += ".app/Contents/MacOS/";
  366. value += itgen->GetInstallFilename(target, config);
  367. }
  368. else
  369. {
  370. value += itgen->GetInstallFilename(target, config,
  371. cmInstallTargetGenerator::NameReal);
  372. }
  373. // Store the property.
  374. properties[prop] = value;
  375. importedLocations.insert(prop);
  376. }
  377. }
  378. //----------------------------------------------------------------------------
  379. void
  380. cmExportInstallFileGenerator::HandleMissingTarget(
  381. std::string& link_libs, std::vector<std::string>& missingTargets,
  382. cmMakefile* mf, cmTarget* depender, cmTarget* dependee)
  383. {
  384. const std::string name = dependee->GetName();
  385. std::vector<std::string> namespaces = this->FindNamespaces(mf, name);
  386. int targetOccurrences = (int)namespaces.size();
  387. if (targetOccurrences == 1)
  388. {
  389. std::string missingTarget = namespaces[0];
  390. missingTarget += dependee->GetExportName();
  391. link_libs += missingTarget;
  392. missingTargets.push_back(missingTarget);
  393. }
  394. else
  395. {
  396. // All exported targets should be known here and should be unique.
  397. // This is probably user-error.
  398. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  399. }
  400. }
  401. //----------------------------------------------------------------------------
  402. std::vector<std::string>
  403. cmExportInstallFileGenerator
  404. ::FindNamespaces(cmMakefile* mf, const std::string& name)
  405. {
  406. std::vector<std::string> namespaces;
  407. cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();
  408. const cmExportSetMap& exportSets = gg->GetExportSets();
  409. for(cmExportSetMap::const_iterator expIt = exportSets.begin();
  410. expIt != exportSets.end();
  411. ++expIt)
  412. {
  413. const cmExportSet* exportSet = expIt->second;
  414. std::vector<cmTargetExport*> const* targets =
  415. exportSet->GetTargetExports();
  416. bool containsTarget = false;
  417. for(unsigned int i=0; i<targets->size(); i++)
  418. {
  419. if (name == (*targets)[i]->Target->GetName())
  420. {
  421. containsTarget = true;
  422. break;
  423. }
  424. }
  425. if (containsTarget)
  426. {
  427. std::vector<cmInstallExportGenerator const*> const* installs =
  428. exportSet->GetInstallations();
  429. for(unsigned int i=0; i<installs->size(); i++)
  430. {
  431. namespaces.push_back((*installs)[i]->GetNamespace());
  432. }
  433. }
  434. }
  435. return namespaces;
  436. }
  437. //----------------------------------------------------------------------------
  438. void
  439. cmExportInstallFileGenerator
  440. ::ComplainAboutImportPrefix(cmInstallTargetGenerator* itgen)
  441. {
  442. const char* installDest = this->IEGen->GetDestination();
  443. cmOStringStream e;
  444. e << "install(EXPORT \""
  445. << this->IEGen->GetExportSet()->GetName()
  446. << "\") given absolute "
  447. << "DESTINATION \"" << installDest << "\" but the export "
  448. << "references an installation of target \""
  449. << itgen->GetTarget()->GetName() << "\" which has relative "
  450. << "DESTINATION \"" << itgen->GetDestination() << "\".";
  451. cmSystemTools::Error(e.str().c_str());
  452. }
  453. //----------------------------------------------------------------------------
  454. void
  455. cmExportInstallFileGenerator
  456. ::ComplainAboutMissingTarget(cmTarget* depender,
  457. cmTarget* dependee,
  458. int occurrences)
  459. {
  460. cmOStringStream e;
  461. e << "install(EXPORT \""
  462. << this->IEGen->GetExportSet()->GetName()
  463. << "\" ...) "
  464. << "includes target \"" << depender->GetName()
  465. << "\" which requires target \"" << dependee->GetName() << "\" ";
  466. if (occurrences == 0)
  467. {
  468. e << "that is not in the export set.";
  469. }
  470. else
  471. {
  472. e << "that is not in this export set, but " << occurrences
  473. << " times in others.";
  474. }
  475. cmSystemTools::Error(e.str().c_str());
  476. }
  477. std::string
  478. cmExportInstallFileGenerator::InstallNameDir(cmTarget* target,
  479. const std::string&)
  480. {
  481. std::string install_name_dir;
  482. cmMakefile* mf = target->GetMakefile();
  483. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  484. {
  485. install_name_dir =
  486. target->GetInstallNameDirForInstallTree();
  487. }
  488. return install_name_dir;
  489. }