cmExportInstallFileGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 "cmAlgorithms.h"
  5. #include "cmExportSet.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 "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include "cmTargetExport.h"
  19. #include <sstream>
  20. #include <utility>
  21. class cmExportSetMap;
  22. cmExportInstallFileGenerator::cmExportInstallFileGenerator(
  23. cmInstallExportGenerator* iegen)
  24. : IEGen(iegen)
  25. {
  26. }
  27. std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
  28. {
  29. std::string glob = this->FileBase;
  30. glob += "-*";
  31. glob += this->FileExt;
  32. return glob;
  33. }
  34. bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
  35. {
  36. std::vector<cmTargetExport*> allTargets;
  37. {
  38. std::string expectedTargets;
  39. std::string sep;
  40. for (cmTargetExport* te :
  41. *this->IEGen->GetExportSet()->GetTargetExports()) {
  42. expectedTargets += sep + this->Namespace + te->Target->GetExportName();
  43. sep = " ";
  44. if (this->ExportedTargets.insert(te->Target).second) {
  45. allTargets.push_back(te);
  46. } else {
  47. std::ostringstream e;
  48. e << "install(EXPORT \"" << this->IEGen->GetExportSet()->GetName()
  49. << "\" ...) "
  50. << "includes target \"" << te->Target->GetName()
  51. << "\" more than once in the export set.";
  52. cmSystemTools::Error(e.str().c_str());
  53. return false;
  54. }
  55. }
  56. this->GenerateExpectedTargetsCode(os, expectedTargets);
  57. }
  58. // Compute the relative import prefix for the file
  59. this->GenerateImportPrefix(os);
  60. std::vector<std::string> missingTargets;
  61. bool require2_8_12 = false;
  62. bool require3_0_0 = false;
  63. bool require3_1_0 = false;
  64. bool requiresConfigFiles = false;
  65. // Create all the imported targets.
  66. for (cmTargetExport* te : allTargets) {
  67. cmGeneratorTarget* gt = te->Target;
  68. cmStateEnums::TargetType targetType = this->GetExportTargetType(te);
  69. requiresConfigFiles =
  70. requiresConfigFiles || targetType != cmStateEnums::INTERFACE_LIBRARY;
  71. this->GenerateImportTargetCode(os, gt, targetType);
  72. ImportPropertyMap properties;
  73. this->PopulateIncludeDirectoriesInterface(
  74. te, cmGeneratorExpression::InstallInterface, properties, missingTargets);
  75. this->PopulateSourcesInterface(te, cmGeneratorExpression::InstallInterface,
  76. properties, missingTargets);
  77. this->PopulateInterfaceProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES", gt,
  78. cmGeneratorExpression::InstallInterface,
  79. properties, missingTargets);
  80. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gt,
  81. cmGeneratorExpression::InstallInterface,
  82. properties, missingTargets);
  83. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gt,
  84. cmGeneratorExpression::InstallInterface,
  85. properties, missingTargets);
  86. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gt,
  87. cmGeneratorExpression::InstallInterface,
  88. properties, missingTargets);
  89. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gt,
  90. cmGeneratorExpression::InstallInterface,
  91. properties, missingTargets);
  92. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gt,
  93. cmGeneratorExpression::InstallInterface,
  94. properties, missingTargets);
  95. this->PopulateLinkDirectoriesInterface(
  96. te, cmGeneratorExpression::InstallInterface, properties, missingTargets);
  97. this->PopulateLinkDependsInterface(
  98. te, cmGeneratorExpression::InstallInterface, properties, missingTargets);
  99. std::string errorMessage;
  100. if (!this->PopulateExportProperties(gt, properties, errorMessage)) {
  101. cmSystemTools::Error(errorMessage.c_str());
  102. return false;
  103. }
  104. const bool newCMP0022Behavior =
  105. gt->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  106. gt->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  107. if (newCMP0022Behavior) {
  108. if (this->PopulateInterfaceLinkLibrariesProperty(
  109. gt, cmGeneratorExpression::InstallInterface, properties,
  110. missingTargets) &&
  111. !this->ExportOld) {
  112. require2_8_12 = true;
  113. }
  114. }
  115. if (targetType == cmStateEnums::INTERFACE_LIBRARY) {
  116. require3_0_0 = true;
  117. }
  118. if (gt->GetProperty("INTERFACE_SOURCES")) {
  119. // We can only generate INTERFACE_SOURCES in CMake 3.3, but CMake 3.1
  120. // can consume them.
  121. require3_1_0 = true;
  122. }
  123. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gt,
  124. properties);
  125. this->PopulateCompatibleInterfaceProperties(gt, properties);
  126. this->GenerateInterfaceProperties(gt, os, properties);
  127. }
  128. if (require3_1_0) {
  129. this->GenerateRequiredCMakeVersion(os, "3.1.0");
  130. } else if (require3_0_0) {
  131. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  132. } else if (require2_8_12) {
  133. this->GenerateRequiredCMakeVersion(os, "2.8.12");
  134. }
  135. this->LoadConfigFiles(os);
  136. this->CleanupTemporaryVariables(os);
  137. this->GenerateImportedFileCheckLoop(os);
  138. bool result = true;
  139. // Generate an import file for each configuration.
  140. // Don't do this if we only export INTERFACE_LIBRARY targets.
  141. if (requiresConfigFiles) {
  142. for (std::string const& c : this->Configurations) {
  143. if (!this->GenerateImportFileConfig(c, missingTargets)) {
  144. result = false;
  145. }
  146. }
  147. }
  148. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  149. return result;
  150. }
  151. void cmExportInstallFileGenerator::GenerateImportPrefix(std::ostream& os)
  152. {
  153. // Set an _IMPORT_PREFIX variable for import location properties
  154. // to reference if they are relative to the install prefix.
  155. std::string installPrefix =
  156. this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition(
  157. "CMAKE_INSTALL_PREFIX");
  158. std::string const& expDest = this->IEGen->GetDestination();
  159. if (cmSystemTools::FileIsFullPath(expDest)) {
  160. // The export file is being installed to an absolute path so the
  161. // package is not relocatable. Use the configured install prefix.
  162. /* clang-format off */
  163. os <<
  164. "# The installation prefix configured by this project.\n"
  165. "set(_IMPORT_PREFIX \"" << installPrefix << "\")\n"
  166. "\n";
  167. /* clang-format on */
  168. } else {
  169. // Add code to compute the installation prefix relative to the
  170. // import file location.
  171. std::string absDest = installPrefix + "/" + expDest;
  172. std::string absDestS = absDest + "/";
  173. os << "# Compute the installation prefix relative to this file.\n"
  174. << "get_filename_component(_IMPORT_PREFIX"
  175. << " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
  176. if (cmHasLiteralPrefix(absDestS, "/lib/") ||
  177. cmHasLiteralPrefix(absDestS, "/lib64/") ||
  178. cmHasLiteralPrefix(absDestS, "/libx32/") ||
  179. cmHasLiteralPrefix(absDestS, "/usr/lib/") ||
  180. cmHasLiteralPrefix(absDestS, "/usr/lib64/") ||
  181. cmHasLiteralPrefix(absDestS, "/usr/libx32/")) {
  182. // Handle "/usr move" symlinks created by some Linux distros.
  183. /* clang-format off */
  184. os <<
  185. "# Use original install prefix when loaded through a\n"
  186. "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
  187. "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
  188. "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
  189. "if(_realCurr STREQUAL _realOrig)\n"
  190. " set(_IMPORT_PREFIX \"" << absDest << "\")\n"
  191. "endif()\n"
  192. "unset(_realOrig)\n"
  193. "unset(_realCurr)\n";
  194. /* clang-format on */
  195. }
  196. std::string dest = expDest;
  197. while (!dest.empty()) {
  198. os << "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" "
  199. "PATH)\n";
  200. dest = cmSystemTools::GetFilenamePath(dest);
  201. }
  202. os << "if(_IMPORT_PREFIX STREQUAL \"/\")\n"
  203. << " set(_IMPORT_PREFIX \"\")\n"
  204. << "endif()\n"
  205. << "\n";
  206. }
  207. }
  208. void cmExportInstallFileGenerator::CleanupTemporaryVariables(std::ostream& os)
  209. {
  210. /* clang-format off */
  211. os << "# Cleanup temporary variables.\n"
  212. << "set(_IMPORT_PREFIX)\n"
  213. << "\n";
  214. /* clang-format on */
  215. }
  216. void cmExportInstallFileGenerator::LoadConfigFiles(std::ostream& os)
  217. {
  218. // Now load per-configuration properties for them.
  219. /* clang-format off */
  220. os << "# Load information for each installed configuration.\n"
  221. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  222. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  223. << this->GetConfigImportFileGlob() << "\")\n"
  224. << "foreach(f ${CONFIG_FILES})\n"
  225. << " include(${f})\n"
  226. << "endforeach()\n"
  227. << "\n";
  228. /* clang-format on */
  229. }
  230. void cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string& input)
  231. {
  232. std::string::size_type pos = 0;
  233. std::string::size_type lastPos = pos;
  234. while ((pos = input.find("$<INSTALL_PREFIX>", lastPos)) !=
  235. std::string::npos) {
  236. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  237. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  238. lastPos = endPos;
  239. }
  240. }
  241. bool cmExportInstallFileGenerator::GenerateImportFileConfig(
  242. const std::string& config, std::vector<std::string>& missingTargets)
  243. {
  244. // Skip configurations not enabled for this export.
  245. if (!this->IEGen->InstallsForConfig(config)) {
  246. return true;
  247. }
  248. // Construct the name of the file to generate.
  249. std::string fileName = this->FileDir;
  250. fileName += "/";
  251. fileName += this->FileBase;
  252. fileName += "-";
  253. if (!config.empty()) {
  254. fileName += cmSystemTools::LowerCase(config);
  255. } else {
  256. fileName += "noconfig";
  257. }
  258. fileName += this->FileExt;
  259. // Open the output file to generate it.
  260. cmGeneratedFileStream exportFileStream(fileName, true);
  261. if (!exportFileStream) {
  262. std::string se = cmSystemTools::GetLastSystemError();
  263. std::ostringstream e;
  264. e << "cannot write to file \"" << fileName << "\": " << se;
  265. cmSystemTools::Error(e.str().c_str());
  266. return false;
  267. }
  268. std::ostream& os = exportFileStream;
  269. // Start with the import file header.
  270. this->GenerateImportHeaderCode(os, config);
  271. // Generate the per-config target information.
  272. this->GenerateImportConfig(os, config, missingTargets);
  273. // End with the import file footer.
  274. this->GenerateImportFooterCode(os);
  275. // Record this per-config import file.
  276. this->ConfigImportFiles[config] = fileName;
  277. return true;
  278. }
  279. void cmExportInstallFileGenerator::GenerateImportTargetsConfig(
  280. std::ostream& os, const std::string& config, std::string const& suffix,
  281. std::vector<std::string>& missingTargets)
  282. {
  283. // Add each target in the set to the export.
  284. for (cmTargetExport* te : *this->IEGen->GetExportSet()->GetTargetExports()) {
  285. // Collect import properties for this target.
  286. if (this->GetExportTargetType(te) == 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 = "IMPORTED_IMPLIB";
  348. prop += suffix;
  349. // Append the installed file name.
  350. value += itgen->GetInstallFilename(target, config,
  351. cmInstallTargetGenerator::NameImplib);
  352. // Store the property.
  353. properties[prop] = value;
  354. importedLocations.insert(prop);
  355. } else if (itgen->GetTarget()->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  356. // Construct the property name.
  357. std::string prop = "IMPORTED_OBJECTS";
  358. prop += suffix;
  359. // Compute all the object files inside this target and setup
  360. // IMPORTED_OBJECTS as a list of object files
  361. std::vector<std::string> objects;
  362. itgen->GetInstallObjectNames(config, objects);
  363. for (std::string& obj : objects) {
  364. obj = value + obj;
  365. }
  366. // Store the property.
  367. properties[prop] = cmJoin(objects, ";");
  368. importedLocations.insert(prop);
  369. } else {
  370. // Construct the property name.
  371. std::string prop = "IMPORTED_LOCATION";
  372. prop += suffix;
  373. // Append the installed file name.
  374. if (target->IsAppBundleOnApple()) {
  375. value += itgen->GetInstallFilename(target, config);
  376. value += ".app/Contents/MacOS/";
  377. value += itgen->GetInstallFilename(target, config);
  378. } else {
  379. value += itgen->GetInstallFilename(target, config,
  380. cmInstallTargetGenerator::NameReal);
  381. }
  382. // Store the property.
  383. properties[prop] = value;
  384. importedLocations.insert(prop);
  385. }
  386. }
  387. cmStateEnums::TargetType cmExportInstallFileGenerator::GetExportTargetType(
  388. cmTargetExport const* targetExport) const
  389. {
  390. cmStateEnums::TargetType targetType = targetExport->Target->GetType();
  391. // An OBJECT library installed with no OBJECTS DESTINATION
  392. // is transformed to an INTERFACE library.
  393. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  394. targetExport->ObjectsGenerator == nullptr) {
  395. targetType = cmStateEnums::INTERFACE_LIBRARY;
  396. }
  397. return targetType;
  398. }
  399. void cmExportInstallFileGenerator::HandleMissingTarget(
  400. std::string& link_libs, std::vector<std::string>& missingTargets,
  401. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  402. {
  403. const std::string name = dependee->GetName();
  404. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  405. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  406. int targetOccurrences = static_cast<int>(namespaces.size());
  407. if (targetOccurrences == 1) {
  408. std::string missingTarget = namespaces[0];
  409. missingTarget += dependee->GetExportName();
  410. link_libs += missingTarget;
  411. missingTargets.push_back(std::move(missingTarget));
  412. } else {
  413. // All exported targets should be known here and should be unique.
  414. // This is probably user-error.
  415. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  416. }
  417. }
  418. std::vector<std::string> cmExportInstallFileGenerator::FindNamespaces(
  419. cmGlobalGenerator* gg, const std::string& name)
  420. {
  421. std::vector<std::string> namespaces;
  422. const cmExportSetMap& exportSets = gg->GetExportSets();
  423. for (auto const& expIt : exportSets) {
  424. const cmExportSet* exportSet = expIt.second;
  425. std::vector<cmTargetExport*> const* targets =
  426. exportSet->GetTargetExports();
  427. bool containsTarget = false;
  428. for (cmTargetExport* target : *targets) {
  429. if (name == target->TargetName) {
  430. containsTarget = true;
  431. break;
  432. }
  433. }
  434. if (containsTarget) {
  435. std::vector<cmInstallExportGenerator const*> const* installs =
  436. exportSet->GetInstallations();
  437. for (cmInstallExportGenerator const* install : *installs) {
  438. namespaces.push_back(install->GetNamespace());
  439. }
  440. }
  441. }
  442. return namespaces;
  443. }
  444. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  445. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  446. {
  447. std::ostringstream e;
  448. e << "install(EXPORT \"" << this->IEGen->GetExportSet()->GetName()
  449. << "\" ...) "
  450. << "includes target \"" << depender->GetName()
  451. << "\" which requires target \"" << dependee->GetName() << "\" ";
  452. if (occurrences == 0) {
  453. e << "that is not in the export set.";
  454. } else {
  455. e << "that is not in this export set, but " << occurrences
  456. << " times in others.";
  457. }
  458. cmSystemTools::Error(e.str().c_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. }