cmExportInstallFileGenerator.cxx 19 KB

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