cmExportInstallFileGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. std::string errorMessage;
  96. if (!this->PopulateExportProperties(gt, properties, errorMessage)) {
  97. cmSystemTools::Error(errorMessage.c_str());
  98. return false;
  99. }
  100. const bool newCMP0022Behavior =
  101. gt->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  102. gt->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  103. if (newCMP0022Behavior) {
  104. if (this->PopulateInterfaceLinkLibrariesProperty(
  105. gt, cmGeneratorExpression::InstallInterface, properties,
  106. missingTargets) &&
  107. !this->ExportOld) {
  108. require2_8_12 = true;
  109. }
  110. }
  111. if (targetType == cmStateEnums::INTERFACE_LIBRARY) {
  112. require3_0_0 = true;
  113. }
  114. if (gt->GetProperty("INTERFACE_SOURCES")) {
  115. // We can only generate INTERFACE_SOURCES in CMake 3.3, but CMake 3.1
  116. // can consume them.
  117. require3_1_0 = true;
  118. }
  119. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gt,
  120. properties);
  121. this->PopulateCompatibleInterfaceProperties(gt, properties);
  122. this->GenerateInterfaceProperties(gt, os, properties);
  123. }
  124. if (require3_1_0) {
  125. this->GenerateRequiredCMakeVersion(os, "3.1.0");
  126. } else if (require3_0_0) {
  127. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  128. } else if (require2_8_12) {
  129. this->GenerateRequiredCMakeVersion(os, "2.8.12");
  130. }
  131. this->LoadConfigFiles(os);
  132. this->CleanupTemporaryVariables(os);
  133. this->GenerateImportedFileCheckLoop(os);
  134. bool result = true;
  135. // Generate an import file for each configuration.
  136. // Don't do this if we only export INTERFACE_LIBRARY targets.
  137. if (requiresConfigFiles) {
  138. for (std::string const& c : this->Configurations) {
  139. if (!this->GenerateImportFileConfig(c, missingTargets)) {
  140. result = false;
  141. }
  142. }
  143. }
  144. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  145. return result;
  146. }
  147. void cmExportInstallFileGenerator::GenerateImportPrefix(std::ostream& os)
  148. {
  149. // Set an _IMPORT_PREFIX variable for import location properties
  150. // to reference if they are relative to the install prefix.
  151. std::string installPrefix =
  152. this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition(
  153. "CMAKE_INSTALL_PREFIX");
  154. std::string const& expDest = this->IEGen->GetDestination();
  155. if (cmSystemTools::FileIsFullPath(expDest)) {
  156. // The export file is being installed to an absolute path so the
  157. // package is not relocatable. Use the configured install prefix.
  158. /* clang-format off */
  159. os <<
  160. "# The installation prefix configured by this project.\n"
  161. "set(_IMPORT_PREFIX \"" << installPrefix << "\")\n"
  162. "\n";
  163. /* clang-format on */
  164. } else {
  165. // Add code to compute the installation prefix relative to the
  166. // import file location.
  167. std::string absDest = installPrefix + "/" + expDest;
  168. std::string absDestS = absDest + "/";
  169. os << "# Compute the installation prefix relative to this file.\n"
  170. << "get_filename_component(_IMPORT_PREFIX"
  171. << " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
  172. if (cmHasLiteralPrefix(absDestS, "/lib/") ||
  173. cmHasLiteralPrefix(absDestS, "/lib64/") ||
  174. cmHasLiteralPrefix(absDestS, "/libx32/") ||
  175. cmHasLiteralPrefix(absDestS, "/usr/lib/") ||
  176. cmHasLiteralPrefix(absDestS, "/usr/lib64/") ||
  177. cmHasLiteralPrefix(absDestS, "/usr/libx32/")) {
  178. // Handle "/usr move" symlinks created by some Linux distros.
  179. /* clang-format off */
  180. os <<
  181. "# Use original install prefix when loaded through a\n"
  182. "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
  183. "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
  184. "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
  185. "if(_realCurr STREQUAL _realOrig)\n"
  186. " set(_IMPORT_PREFIX \"" << absDest << "\")\n"
  187. "endif()\n"
  188. "unset(_realOrig)\n"
  189. "unset(_realCurr)\n";
  190. /* clang-format on */
  191. }
  192. std::string dest = expDest;
  193. while (!dest.empty()) {
  194. os << "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" "
  195. "PATH)\n";
  196. dest = cmSystemTools::GetFilenamePath(dest);
  197. }
  198. os << "if(_IMPORT_PREFIX STREQUAL \"/\")\n"
  199. << " set(_IMPORT_PREFIX \"\")\n"
  200. << "endif()\n"
  201. << "\n";
  202. }
  203. }
  204. void cmExportInstallFileGenerator::CleanupTemporaryVariables(std::ostream& os)
  205. {
  206. /* clang-format off */
  207. os << "# Cleanup temporary variables.\n"
  208. << "set(_IMPORT_PREFIX)\n"
  209. << "\n";
  210. /* clang-format on */
  211. }
  212. void cmExportInstallFileGenerator::LoadConfigFiles(std::ostream& os)
  213. {
  214. // Now load per-configuration properties for them.
  215. /* clang-format off */
  216. os << "# Load information for each installed configuration.\n"
  217. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  218. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  219. << this->GetConfigImportFileGlob() << "\")\n"
  220. << "foreach(f ${CONFIG_FILES})\n"
  221. << " include(${f})\n"
  222. << "endforeach()\n"
  223. << "\n";
  224. /* clang-format on */
  225. }
  226. void cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string& input)
  227. {
  228. std::string::size_type pos = 0;
  229. std::string::size_type lastPos = pos;
  230. while ((pos = input.find("$<INSTALL_PREFIX>", lastPos)) !=
  231. std::string::npos) {
  232. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  233. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  234. lastPos = endPos;
  235. }
  236. }
  237. bool cmExportInstallFileGenerator::GenerateImportFileConfig(
  238. const std::string& config, std::vector<std::string>& missingTargets)
  239. {
  240. // Skip configurations not enabled for this export.
  241. if (!this->IEGen->InstallsForConfig(config)) {
  242. return true;
  243. }
  244. // Construct the name of the file to generate.
  245. std::string fileName = this->FileDir;
  246. fileName += "/";
  247. fileName += this->FileBase;
  248. fileName += "-";
  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.c_str(), 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().c_str());
  262. return false;
  263. }
  264. std::ostream& os = exportFileStream;
  265. // Start with the import file header.
  266. this->GenerateImportHeaderCode(os, config);
  267. // Generate the per-config target information.
  268. this->GenerateImportConfig(os, config, missingTargets);
  269. // End with the import file footer.
  270. this->GenerateImportFooterCode(os);
  271. // Record this per-config import file.
  272. this->ConfigImportFiles[config] = fileName;
  273. return true;
  274. }
  275. void cmExportInstallFileGenerator::GenerateImportTargetsConfig(
  276. std::ostream& os, const std::string& config, std::string const& suffix,
  277. std::vector<std::string>& missingTargets)
  278. {
  279. // Add each target in the set to the export.
  280. for (cmTargetExport* te : *this->IEGen->GetExportSet()->GetTargetExports()) {
  281. // Collect import properties for this target.
  282. if (this->GetExportTargetType(te) == cmStateEnums::INTERFACE_LIBRARY) {
  283. continue;
  284. }
  285. ImportPropertyMap properties;
  286. std::set<std::string> importedLocations;
  287. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  288. properties, importedLocations);
  289. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  290. properties, importedLocations);
  291. this->SetImportLocationProperty(config, suffix, te->RuntimeGenerator,
  292. properties, importedLocations);
  293. this->SetImportLocationProperty(config, suffix, te->ObjectsGenerator,
  294. properties, importedLocations);
  295. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  296. properties, importedLocations);
  297. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  298. properties, importedLocations);
  299. // If any file location was set for the target add it to the
  300. // import file.
  301. if (!properties.empty()) {
  302. // Get the rest of the target details.
  303. cmGeneratorTarget* gtgt = te->Target;
  304. this->SetImportDetailProperties(config, suffix, gtgt, properties,
  305. missingTargets);
  306. this->SetImportLinkInterface(config, suffix,
  307. cmGeneratorExpression::InstallInterface,
  308. gtgt, properties, missingTargets);
  309. // TODO: PUBLIC_HEADER_LOCATION
  310. // This should wait until the build feature propagation stuff
  311. // is done. Then this can be a propagated include directory.
  312. // this->GenerateImportProperty(config, te->HeaderGenerator,
  313. // properties);
  314. // Generate code in the export file.
  315. this->GenerateImportPropertyCode(os, config, gtgt, properties);
  316. this->GenerateImportedFileChecksCode(os, gtgt, properties,
  317. importedLocations);
  318. }
  319. }
  320. }
  321. void cmExportInstallFileGenerator::SetImportLocationProperty(
  322. const std::string& config, std::string const& suffix,
  323. cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,
  324. std::set<std::string>& importedLocations)
  325. {
  326. // Skip rules that do not match this configuration.
  327. if (!(itgen && itgen->InstallsForConfig(config))) {
  328. return;
  329. }
  330. // Get the target to be installed.
  331. cmGeneratorTarget* target = itgen->GetTarget();
  332. // Construct the installed location of the target.
  333. std::string dest = itgen->GetDestination(config);
  334. std::string value;
  335. if (!cmSystemTools::FileIsFullPath(dest)) {
  336. // The target is installed relative to the installation prefix.
  337. value = "${_IMPORT_PREFIX}/";
  338. }
  339. value += dest;
  340. value += "/";
  341. if (itgen->IsImportLibrary()) {
  342. // Construct the property name.
  343. std::string prop = "IMPORTED_IMPLIB";
  344. prop += suffix;
  345. // Append the installed file name.
  346. value += itgen->GetInstallFilename(target, config,
  347. 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 = "IMPORTED_OBJECTS";
  354. prop += suffix;
  355. // Compute all the object files inside this target and setup
  356. // IMPORTED_OBJECTS as a list of object files
  357. std::vector<std::string> objects;
  358. itgen->GetInstallObjectNames(config, objects);
  359. for (std::string& obj : objects) {
  360. obj = value + obj;
  361. }
  362. // Store the property.
  363. properties[prop] = cmJoin(objects, ";");
  364. importedLocations.insert(prop);
  365. } else {
  366. // Construct the property name.
  367. std::string prop = "IMPORTED_LOCATION";
  368. prop += suffix;
  369. // Append the installed file name.
  370. if (target->IsAppBundleOnApple()) {
  371. value += itgen->GetInstallFilename(target, config);
  372. value += ".app/Contents/MacOS/";
  373. value += itgen->GetInstallFilename(target, config);
  374. } else {
  375. value += itgen->GetInstallFilename(target, config,
  376. 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* depender, cmGeneratorTarget* dependee)
  398. {
  399. const std::string name = dependee->GetName();
  400. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  401. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  402. int targetOccurrences = static_cast<int>(namespaces.size());
  403. if (targetOccurrences == 1) {
  404. std::string missingTarget = namespaces[0];
  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, targetOccurrences);
  412. }
  413. }
  414. std::vector<std::string> cmExportInstallFileGenerator::FindNamespaces(
  415. cmGlobalGenerator* gg, const std::string& name)
  416. {
  417. std::vector<std::string> namespaces;
  418. const cmExportSetMap& exportSets = gg->GetExportSets();
  419. for (auto const& expIt : exportSets) {
  420. const cmExportSet* exportSet = expIt.second;
  421. std::vector<cmTargetExport*> const* targets =
  422. exportSet->GetTargetExports();
  423. bool containsTarget = false;
  424. for (cmTargetExport* target : *targets) {
  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. namespaces.push_back(install->GetNamespace());
  435. }
  436. }
  437. }
  438. return namespaces;
  439. }
  440. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  441. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  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 (occurrences == 0) {
  449. e << "that is not in the export set.";
  450. } else {
  451. e << "that is not in this export set, but " << occurrences
  452. << " times in others.";
  453. }
  454. cmSystemTools::Error(e.str().c_str());
  455. }
  456. std::string cmExportInstallFileGenerator::InstallNameDir(
  457. cmGeneratorTarget* target, const std::string& /*config*/)
  458. {
  459. std::string install_name_dir;
  460. cmMakefile* mf = target->Target->GetMakefile();
  461. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  462. install_name_dir = target->GetInstallNameDirForInstallTree();
  463. }
  464. return install_name_dir;
  465. }