cmExportInstallFileGenerator.cxx 19 KB

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