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