cmExportInstallFileGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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->PopulateLinkDependsInterface(
  96. te, cmGeneratorExpression::InstallInterface, properties, missingTargets);
  97. std::string errorMessage;
  98. if (!this->PopulateExportProperties(gt, properties, errorMessage)) {
  99. cmSystemTools::Error(errorMessage.c_str());
  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 = this->FileDir;
  248. fileName += "/";
  249. fileName += this->FileBase;
  250. fileName += "-";
  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.c_str(), 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().c_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 (cmTargetExport* te : *this->IEGen->GetExportSet()->GetTargetExports()) {
  283. // Collect import properties for this target.
  284. if (this->GetExportTargetType(te) == cmStateEnums::INTERFACE_LIBRARY) {
  285. continue;
  286. }
  287. ImportPropertyMap properties;
  288. std::set<std::string> importedLocations;
  289. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  290. properties, importedLocations);
  291. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  292. properties, importedLocations);
  293. this->SetImportLocationProperty(config, suffix, te->RuntimeGenerator,
  294. properties, importedLocations);
  295. this->SetImportLocationProperty(config, suffix, te->ObjectsGenerator,
  296. properties, importedLocations);
  297. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  298. properties, importedLocations);
  299. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  300. properties, importedLocations);
  301. // If any file location was set for the target add it to the
  302. // import file.
  303. if (!properties.empty()) {
  304. // Get the rest of the target details.
  305. cmGeneratorTarget* gtgt = te->Target;
  306. this->SetImportDetailProperties(config, suffix, gtgt, properties,
  307. missingTargets);
  308. this->SetImportLinkInterface(config, suffix,
  309. cmGeneratorExpression::InstallInterface,
  310. gtgt, properties, missingTargets);
  311. // TODO: PUBLIC_HEADER_LOCATION
  312. // This should wait until the build feature propagation stuff
  313. // is done. Then this can be a propagated include directory.
  314. // this->GenerateImportProperty(config, te->HeaderGenerator,
  315. // properties);
  316. // Generate code in the export file.
  317. this->GenerateImportPropertyCode(os, config, gtgt, properties);
  318. this->GenerateImportedFileChecksCode(os, gtgt, properties,
  319. importedLocations);
  320. }
  321. }
  322. }
  323. void cmExportInstallFileGenerator::SetImportLocationProperty(
  324. const std::string& config, std::string const& suffix,
  325. cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,
  326. std::set<std::string>& importedLocations)
  327. {
  328. // Skip rules that do not match this configuration.
  329. if (!(itgen && itgen->InstallsForConfig(config))) {
  330. return;
  331. }
  332. // Get the target to be installed.
  333. cmGeneratorTarget* target = itgen->GetTarget();
  334. // Construct the installed location of the target.
  335. std::string dest = itgen->GetDestination(config);
  336. std::string value;
  337. if (!cmSystemTools::FileIsFullPath(dest)) {
  338. // The target is installed relative to the installation prefix.
  339. value = "${_IMPORT_PREFIX}/";
  340. }
  341. value += dest;
  342. value += "/";
  343. if (itgen->IsImportLibrary()) {
  344. // Construct the property name.
  345. std::string prop = "IMPORTED_IMPLIB";
  346. prop += suffix;
  347. // Append the installed file name.
  348. value += itgen->GetInstallFilename(target, config,
  349. 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 = "IMPORTED_OBJECTS";
  356. prop += 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 = 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 = "IMPORTED_LOCATION";
  370. prop += suffix;
  371. // Append the installed file name.
  372. if (target->IsAppBundleOnApple()) {
  373. value += itgen->GetInstallFilename(target, config);
  374. value += ".app/Contents/MacOS/";
  375. value += itgen->GetInstallFilename(target, config);
  376. } else {
  377. value += itgen->GetInstallFilename(target, config,
  378. cmInstallTargetGenerator::NameReal);
  379. }
  380. // Store the property.
  381. properties[prop] = value;
  382. importedLocations.insert(prop);
  383. }
  384. }
  385. cmStateEnums::TargetType cmExportInstallFileGenerator::GetExportTargetType(
  386. cmTargetExport const* targetExport) const
  387. {
  388. cmStateEnums::TargetType targetType = targetExport->Target->GetType();
  389. // An OBJECT library installed with no OBJECTS DESTINATION
  390. // is transformed to an INTERFACE library.
  391. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  392. targetExport->ObjectsGenerator == nullptr) {
  393. targetType = cmStateEnums::INTERFACE_LIBRARY;
  394. }
  395. return targetType;
  396. }
  397. void cmExportInstallFileGenerator::HandleMissingTarget(
  398. std::string& link_libs, std::vector<std::string>& missingTargets,
  399. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  400. {
  401. const std::string name = dependee->GetName();
  402. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  403. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  404. int targetOccurrences = static_cast<int>(namespaces.size());
  405. if (targetOccurrences == 1) {
  406. std::string missingTarget = namespaces[0];
  407. missingTarget += dependee->GetExportName();
  408. link_libs += missingTarget;
  409. missingTargets.push_back(std::move(missingTarget));
  410. } else {
  411. // All exported targets should be known here and should be unique.
  412. // This is probably user-error.
  413. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  414. }
  415. }
  416. std::vector<std::string> cmExportInstallFileGenerator::FindNamespaces(
  417. cmGlobalGenerator* gg, const std::string& name)
  418. {
  419. std::vector<std::string> namespaces;
  420. const cmExportSetMap& exportSets = gg->GetExportSets();
  421. for (auto const& expIt : exportSets) {
  422. const cmExportSet* exportSet = expIt.second;
  423. std::vector<cmTargetExport*> const* targets =
  424. exportSet->GetTargetExports();
  425. bool containsTarget = false;
  426. for (cmTargetExport* target : *targets) {
  427. if (name == target->TargetName) {
  428. containsTarget = true;
  429. break;
  430. }
  431. }
  432. if (containsTarget) {
  433. std::vector<cmInstallExportGenerator const*> const* installs =
  434. exportSet->GetInstallations();
  435. for (cmInstallExportGenerator const* install : *installs) {
  436. namespaces.push_back(install->GetNamespace());
  437. }
  438. }
  439. }
  440. return namespaces;
  441. }
  442. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  443. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  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 (occurrences == 0) {
  451. e << "that is not in the export set.";
  452. } else {
  453. e << "that is not in this export set, but " << occurrences
  454. << " times in others.";
  455. }
  456. cmSystemTools::Error(e.str().c_str());
  457. }
  458. std::string cmExportInstallFileGenerator::InstallNameDir(
  459. cmGeneratorTarget* target, const std::string& /*config*/)
  460. {
  461. std::string install_name_dir;
  462. cmMakefile* mf = target->Target->GetMakefile();
  463. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  464. install_name_dir = target->GetInstallNameDirForInstallTree();
  465. }
  466. return install_name_dir;
  467. }