cmExportInstallFileGenerator.cxx 19 KB

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