cmExportInstallFileGenerator.cxx 18 KB

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