cmExportInstallFileGenerator.cxx 19 KB

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