cmExportInstallFileGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. gt, cmGeneratorExpression::InstallInterface, properties, missingTargets);
  76. this->PopulateSourcesInterface(gt, 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. gt, cmGeneratorExpression::InstallInterface, properties, missingTargets);
  101. this->PopulateLinkDependsInterface(
  102. gt, 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. exportFileStream.SetCopyIfDifferent(true);
  263. std::ostream& os = exportFileStream;
  264. // Start with the import file header.
  265. this->GenerateImportHeaderCode(os, config);
  266. // Generate the per-config target information.
  267. this->GenerateImportConfig(os, config, missingTargets);
  268. // End with the import file footer.
  269. this->GenerateImportFooterCode(os);
  270. // Record this per-config import file.
  271. this->ConfigImportFiles[config] = fileName;
  272. return true;
  273. }
  274. void cmExportInstallFileGenerator::GenerateImportTargetsConfig(
  275. std::ostream& os, const std::string& config, std::string const& suffix,
  276. std::vector<std::string>& missingTargets)
  277. {
  278. // Add each target in the set to the export.
  279. for (std::unique_ptr<cmTargetExport> const& te :
  280. this->IEGen->GetExportSet()->GetTargetExports()) {
  281. // Collect import properties for this target.
  282. if (this->GetExportTargetType(te.get()) ==
  283. cmStateEnums::INTERFACE_LIBRARY) {
  284. continue;
  285. }
  286. ImportPropertyMap properties;
  287. std::set<std::string> importedLocations;
  288. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  289. properties, importedLocations);
  290. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  291. properties, importedLocations);
  292. this->SetImportLocationProperty(config, suffix, te->RuntimeGenerator,
  293. properties, importedLocations);
  294. this->SetImportLocationProperty(config, suffix, te->ObjectsGenerator,
  295. properties, importedLocations);
  296. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  297. properties, importedLocations);
  298. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  299. properties, importedLocations);
  300. // If any file location was set for the target add it to the
  301. // import file.
  302. if (!properties.empty()) {
  303. // Get the rest of the target details.
  304. cmGeneratorTarget* gtgt = te->Target;
  305. this->SetImportDetailProperties(config, suffix, gtgt, properties,
  306. missingTargets);
  307. this->SetImportLinkInterface(config, suffix,
  308. cmGeneratorExpression::InstallInterface,
  309. gtgt, properties, missingTargets);
  310. // TODO: PUBLIC_HEADER_LOCATION
  311. // This should wait until the build feature propagation stuff
  312. // is done. Then this can be a propagated include directory.
  313. // this->GenerateImportProperty(config, te->HeaderGenerator,
  314. // properties);
  315. // Generate code in the export file.
  316. this->GenerateImportPropertyCode(os, config, gtgt, properties);
  317. this->GenerateImportedFileChecksCode(os, gtgt, properties,
  318. importedLocations);
  319. }
  320. }
  321. }
  322. void cmExportInstallFileGenerator::SetImportLocationProperty(
  323. const std::string& config, std::string const& suffix,
  324. cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,
  325. std::set<std::string>& importedLocations)
  326. {
  327. // Skip rules that do not match this configuration.
  328. if (!(itgen && itgen->InstallsForConfig(config))) {
  329. return;
  330. }
  331. // Get the target to be installed.
  332. cmGeneratorTarget* target = itgen->GetTarget();
  333. // Construct the installed location of the target.
  334. std::string dest = itgen->GetDestination(config);
  335. std::string value;
  336. if (!cmSystemTools::FileIsFullPath(dest)) {
  337. // The target is installed relative to the installation prefix.
  338. value = "${_IMPORT_PREFIX}/";
  339. }
  340. value += dest;
  341. value += "/";
  342. if (itgen->IsImportLibrary()) {
  343. // Construct the property name.
  344. std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix);
  345. // Append the installed file name.
  346. value += cmInstallTargetGenerator::GetInstallFilename(
  347. target, config, cmInstallTargetGenerator::NameImplib);
  348. // Store the property.
  349. properties[prop] = value;
  350. importedLocations.insert(prop);
  351. } else if (itgen->GetTarget()->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  352. // Construct the property name.
  353. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  354. // Compute all the object files inside this target and setup
  355. // IMPORTED_OBJECTS as a list of object files
  356. std::vector<std::string> objects;
  357. itgen->GetInstallObjectNames(config, objects);
  358. for (std::string& obj : objects) {
  359. obj = cmStrCat(value, obj);
  360. }
  361. // Store the property.
  362. properties[prop] = cmJoin(objects, ";");
  363. importedLocations.insert(prop);
  364. } else {
  365. // Construct the property name.
  366. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  367. // Append the installed file name.
  368. if (target->IsAppBundleOnApple()) {
  369. value += cmInstallTargetGenerator::GetInstallFilename(target, config);
  370. value += ".app/Contents/MacOS/";
  371. value += cmInstallTargetGenerator::GetInstallFilename(target, config);
  372. } else {
  373. value += cmInstallTargetGenerator::GetInstallFilename(
  374. target, config, cmInstallTargetGenerator::NameReal);
  375. }
  376. // Store the property.
  377. properties[prop] = value;
  378. importedLocations.insert(prop);
  379. }
  380. }
  381. cmStateEnums::TargetType cmExportInstallFileGenerator::GetExportTargetType(
  382. cmTargetExport const* targetExport) const
  383. {
  384. cmStateEnums::TargetType targetType = targetExport->Target->GetType();
  385. // An OBJECT library installed with no OBJECTS DESTINATION
  386. // is transformed to an INTERFACE library.
  387. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  388. targetExport->ObjectsGenerator == nullptr) {
  389. targetType = cmStateEnums::INTERFACE_LIBRARY;
  390. }
  391. return targetType;
  392. }
  393. void cmExportInstallFileGenerator::HandleMissingTarget(
  394. std::string& link_libs, std::vector<std::string>& missingTargets,
  395. cmGeneratorTarget const* depender, cmGeneratorTarget* dependee)
  396. {
  397. const std::string name = dependee->GetName();
  398. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  399. auto exportInfo = this->FindNamespaces(gg, name);
  400. std::vector<std::string> const& exportFiles = exportInfo.first;
  401. if (exportFiles.size() == 1) {
  402. std::string missingTarget = exportInfo.second;
  403. missingTarget += dependee->GetExportName();
  404. link_libs += missingTarget;
  405. missingTargets.push_back(std::move(missingTarget));
  406. } else {
  407. // All exported targets should be known here and should be unique.
  408. // This is probably user-error.
  409. this->ComplainAboutMissingTarget(depender, dependee, exportFiles);
  410. }
  411. }
  412. std::pair<std::vector<std::string>, std::string>
  413. cmExportInstallFileGenerator::FindNamespaces(cmGlobalGenerator* gg,
  414. const std::string& name)
  415. {
  416. std::vector<std::string> exportFiles;
  417. std::string ns;
  418. const cmExportSetMap& exportSets = gg->GetExportSets();
  419. for (auto const& expIt : exportSets) {
  420. const cmExportSet& exportSet = expIt.second;
  421. bool containsTarget = false;
  422. for (auto const& target : exportSet.GetTargetExports()) {
  423. if (name == target->TargetName) {
  424. containsTarget = true;
  425. break;
  426. }
  427. }
  428. if (containsTarget) {
  429. std::vector<cmInstallExportGenerator const*> const* installs =
  430. exportSet.GetInstallations();
  431. for (cmInstallExportGenerator const* install : *installs) {
  432. exportFiles.push_back(install->GetDestinationFile());
  433. ns = install->GetNamespace();
  434. }
  435. }
  436. }
  437. return { exportFiles, ns };
  438. }
  439. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  440. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  441. std::vector<std::string> const& exportFiles)
  442. {
  443. std::ostringstream e;
  444. e << "install(EXPORT \"" << this->IEGen->GetExportSet()->GetName()
  445. << "\" ...) "
  446. << "includes target \"" << depender->GetName()
  447. << "\" which requires target \"" << dependee->GetName() << "\" ";
  448. if (exportFiles.empty()) {
  449. e << "that is not in any export set.";
  450. } else {
  451. e << "that is not in this export set, but in multiple other export sets: "
  452. << cmJoin(exportFiles, ", ") << ".\n";
  453. e << "An exported target cannot depend upon another target which is "
  454. "exported multiple times. Consider consolidating the exports of the "
  455. "\""
  456. << dependee->GetName() << "\" target to a single export.";
  457. }
  458. cmSystemTools::Error(e.str());
  459. }
  460. std::string cmExportInstallFileGenerator::InstallNameDir(
  461. cmGeneratorTarget const* target, const std::string& config)
  462. {
  463. std::string install_name_dir;
  464. cmMakefile* mf = target->Target->GetMakefile();
  465. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  466. install_name_dir =
  467. target->GetInstallNameDirForInstallTree(config, "${_IMPORT_PREFIX}");
  468. }
  469. return install_name_dir;
  470. }