cmExportInstallFileGenerator.cxx 19 KB

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