cmExportInstallFileGenerator.cxx 17 KB

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