cmExportInstallFileGenerator.cxx 18 KB

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