cmExportInstallFileGenerator.cxx 18 KB

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