cmExportInstallFileGenerator.cxx 19 KB

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