cmExportFileGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmExportFileGenerator.h"
  4. #include <array>
  5. #include <cstddef>
  6. #include <sstream>
  7. #include <utility>
  8. #include <cm/memory>
  9. #include <cm/string_view>
  10. #include <cmext/string_view>
  11. #include "cmsys/FStream.hxx"
  12. #include "cmComputeLinkInformation.h"
  13. #include "cmFindPackageStack.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmLinkItem.h"
  17. #include "cmList.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmMessageType.h"
  21. #include "cmPropertyMap.h"
  22. #include "cmStateTypes.h"
  23. #include "cmStringAlgorithms.h"
  24. #include "cmSystemTools.h"
  25. #include "cmTarget.h"
  26. #include "cmValue.h"
  27. cmExportFileGenerator::cmExportFileGenerator() = default;
  28. void cmExportFileGenerator::AddConfiguration(std::string const& config)
  29. {
  30. this->Configurations.push_back(config);
  31. }
  32. void cmExportFileGenerator::SetExportFile(char const* mainFile)
  33. {
  34. this->MainImportFile = mainFile;
  35. this->FileDir = cmSystemTools::GetFilenamePath(this->MainImportFile);
  36. this->FileBase =
  37. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  38. this->FileExt =
  39. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  40. }
  41. std::string const& cmExportFileGenerator::GetMainExportFileName() const
  42. {
  43. return this->MainImportFile;
  44. }
  45. bool cmExportFileGenerator::GenerateImportFile()
  46. {
  47. // Open the output file to generate it.
  48. std::unique_ptr<cmsys::ofstream> foutPtr;
  49. if (this->AppendMode) {
  50. // Open for append.
  51. auto openmodeApp = std::ios::app;
  52. foutPtr = cm::make_unique<cmsys::ofstream>(this->MainImportFile.c_str(),
  53. openmodeApp);
  54. } else {
  55. // Generate atomically and with copy-if-different.
  56. std::unique_ptr<cmGeneratedFileStream> ap(
  57. new cmGeneratedFileStream(this->MainImportFile, true));
  58. ap->SetCopyIfDifferent(true);
  59. foutPtr = std::move(ap);
  60. }
  61. if (!foutPtr || !*foutPtr) {
  62. std::string se = cmSystemTools::GetLastSystemError();
  63. std::ostringstream e;
  64. e << "cannot write to file \"" << this->MainImportFile << "\": " << se;
  65. cmSystemTools::Error(e.str());
  66. return false;
  67. }
  68. return this->GenerateImportFile(*foutPtr);
  69. }
  70. std::string cmExportFileGenerator::PropertyConfigSuffix(
  71. std::string const& config)
  72. {
  73. // Construct the property configuration suffix.
  74. if (config.empty()) {
  75. return "_NOCONFIG";
  76. }
  77. return cmStrCat('_', cmSystemTools::UpperCase(config));
  78. }
  79. void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
  80. std::string const& config)
  81. {
  82. // Generate the per-config target information.
  83. this->GenerateImportTargetsConfig(os, config, PropertyConfigSuffix(config));
  84. }
  85. bool cmExportFileGenerator::PopulateInterfaceProperties(
  86. cmGeneratorTarget const* target, std::string const& includesDestinationDirs,
  87. cmGeneratorExpression::PreprocessContext preprocessRule,
  88. ImportPropertyMap& properties)
  89. {
  90. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", target,
  91. preprocessRule, properties);
  92. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", target,
  93. preprocessRule, properties);
  94. this->PopulateInterfaceProperty("INTERFACE_PRECOMPILE_HEADERS", target,
  95. preprocessRule, properties);
  96. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", target,
  97. preprocessRule, properties);
  98. this->PopulateInterfaceProperty("INTERFACE_AUTOMOC_MACRO_NAMES", target,
  99. preprocessRule, properties);
  100. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", target,
  101. preprocessRule, properties);
  102. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", target,
  103. preprocessRule, properties);
  104. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
  105. target, properties);
  106. this->PopulateInterfaceProperty("SPDX_LICENSE", target, preprocessRule,
  107. properties);
  108. std::string errorMessage;
  109. if (!this->PopulateCxxModuleExportProperties(
  110. target, properties, preprocessRule, includesDestinationDirs,
  111. errorMessage)) {
  112. this->ReportError(errorMessage);
  113. return false;
  114. }
  115. if (!this->PopulateExportProperties(target, properties, errorMessage)) {
  116. this->ReportError(errorMessage);
  117. return false;
  118. }
  119. this->PopulateCompatibleInterfaceProperties(target, properties);
  120. this->PopulateCustomTransitiveInterfaceProperties(target, preprocessRule,
  121. properties);
  122. return true;
  123. }
  124. void cmExportFileGenerator::PopulateInterfaceProperty(
  125. std::string const& propName, cmGeneratorTarget const* target,
  126. ImportPropertyMap& properties) const
  127. {
  128. cmValue input = target->GetProperty(propName);
  129. if (input) {
  130. properties[propName] = *input;
  131. }
  132. }
  133. void cmExportFileGenerator::PopulateInterfaceProperty(
  134. std::string const& propName, std::string const& outputName,
  135. cmGeneratorTarget const* target,
  136. cmGeneratorExpression::PreprocessContext preprocessRule,
  137. ImportPropertyMap& properties)
  138. {
  139. cmValue input = target->GetProperty(propName);
  140. if (input) {
  141. if (input->empty()) {
  142. // Set to empty
  143. properties[outputName].clear();
  144. return;
  145. }
  146. std::string prepro =
  147. cmGeneratorExpression::Preprocess(*input, preprocessRule);
  148. if (!prepro.empty()) {
  149. this->ResolveTargetsInGeneratorExpressions(prepro, target);
  150. properties[outputName] = prepro;
  151. }
  152. }
  153. }
  154. void cmExportFileGenerator::PopulateInterfaceProperty(
  155. std::string const& propName, cmGeneratorTarget const* target,
  156. cmGeneratorExpression::PreprocessContext preprocessRule,
  157. ImportPropertyMap& properties)
  158. {
  159. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  160. properties);
  161. }
  162. bool cmExportFileGenerator::PopulateInterfaceLinkLibrariesProperty(
  163. cmGeneratorTarget const* target,
  164. cmGeneratorExpression::PreprocessContext preprocessRule,
  165. ImportPropertyMap& properties)
  166. {
  167. if (!target->IsLinkable()) {
  168. return false;
  169. }
  170. static std::array<std::string, 3> const linkIfaceProps = {
  171. { "INTERFACE_LINK_LIBRARIES", "INTERFACE_LINK_LIBRARIES_DIRECT",
  172. "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE" }
  173. };
  174. bool hadINTERFACE_LINK_LIBRARIES = false;
  175. for (std::string const& linkIfaceProp : linkIfaceProps) {
  176. if (cmValue input = target->GetProperty(linkIfaceProp)) {
  177. std::string prepro =
  178. cmGeneratorExpression::Preprocess(*input, preprocessRule);
  179. if (!prepro.empty()) {
  180. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  181. ReplaceFreeTargets);
  182. properties[linkIfaceProp] = prepro;
  183. hadINTERFACE_LINK_LIBRARIES = true;
  184. }
  185. }
  186. }
  187. return hadINTERFACE_LINK_LIBRARIES;
  188. }
  189. void cmExportFileGenerator::AddImportPrefix(std::string& exportDirs) const
  190. {
  191. std::vector<std::string> entries;
  192. cmGeneratorExpression::Split(exportDirs, entries);
  193. exportDirs.clear();
  194. char const* sep = "";
  195. cm::string_view const& prefixWithSlash = this->GetImportPrefixWithSlash();
  196. for (std::string const& e : entries) {
  197. exportDirs += sep;
  198. sep = ";";
  199. if (!cmSystemTools::FileIsFullPath(e) &&
  200. !cmHasPrefix(e, prefixWithSlash)) {
  201. exportDirs += prefixWithSlash;
  202. }
  203. exportDirs += e;
  204. }
  205. }
  206. namespace {
  207. void getPropertyContents(cmGeneratorTarget const* tgt, std::string const& prop,
  208. std::set<std::string>& ifaceProperties)
  209. {
  210. cmValue p = tgt->GetProperty(prop);
  211. if (!p) {
  212. return;
  213. }
  214. cmList content{ *p };
  215. ifaceProperties.insert(content.begin(), content.end());
  216. }
  217. void getCompatibleInterfaceProperties(cmGeneratorTarget const* target,
  218. std::set<std::string>& ifaceProperties,
  219. std::string const& config)
  220. {
  221. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  222. // object libraries have no link information, so nothing to compute
  223. return;
  224. }
  225. cmComputeLinkInformation* info = target->GetLinkInformation(config);
  226. if (!info) {
  227. cmLocalGenerator* lg = target->GetLocalGenerator();
  228. std::ostringstream e;
  229. e << "Exporting the target \"" << target->GetName()
  230. << "\" is not "
  231. "allowed since its linker language cannot be determined";
  232. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  233. return;
  234. }
  235. cmComputeLinkInformation::ItemVector const& deps = info->GetItems();
  236. for (auto const& dep : deps) {
  237. if (!dep.Target || dep.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  238. continue;
  239. }
  240. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_BOOL",
  241. ifaceProperties);
  242. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_STRING",
  243. ifaceProperties);
  244. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  245. ifaceProperties);
  246. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  247. ifaceProperties);
  248. }
  249. }
  250. }
  251. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  252. cmGeneratorTarget const* gtarget, ImportPropertyMap& properties) const
  253. {
  254. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL", gtarget,
  255. properties);
  256. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING", gtarget,
  257. properties);
  258. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MIN", gtarget,
  259. properties);
  260. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MAX", gtarget,
  261. properties);
  262. std::set<std::string> ifaceProperties;
  263. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  264. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  265. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  266. ifaceProperties);
  267. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  268. ifaceProperties);
  269. if (gtarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  270. std::vector<std::string> configNames =
  271. gtarget->Target->GetMakefile()->GetGeneratorConfigs(
  272. cmMakefile::IncludeEmptyConfig);
  273. for (std::string const& cn : configNames) {
  274. getCompatibleInterfaceProperties(gtarget, ifaceProperties, cn);
  275. }
  276. }
  277. for (std::string const& ip : ifaceProperties) {
  278. this->PopulateInterfaceProperty("INTERFACE_" + ip, gtarget, properties);
  279. }
  280. }
  281. void cmExportFileGenerator::PopulateCustomTransitiveInterfaceProperties(
  282. cmGeneratorTarget const* target,
  283. cmGeneratorExpression::PreprocessContext preprocessRule,
  284. ImportPropertyMap& properties)
  285. {
  286. this->PopulateInterfaceProperty("TRANSITIVE_COMPILE_PROPERTIES", target,
  287. properties);
  288. this->PopulateInterfaceProperty("TRANSITIVE_LINK_PROPERTIES", target,
  289. properties);
  290. cmGeneratorTarget::CheckLinkLibrariesSuppressionRAII suppress;
  291. std::set<std::string> ifaceProperties;
  292. for (std::string const& config : this->Configurations) {
  293. for (auto const& i : target->GetCustomTransitiveProperties(
  294. config, cmGeneratorTarget::PropertyFor::Interface)) {
  295. ifaceProperties.emplace(i.second.InterfaceName);
  296. }
  297. }
  298. for (std::string const& ip : ifaceProperties) {
  299. this->PopulateInterfaceProperty(ip, target, preprocessRule, properties);
  300. }
  301. }
  302. bool cmExportFileGenerator::NoteLinkedTarget(
  303. cmGeneratorTarget const* /*target*/, std::string const& /*linkedName*/,
  304. cmGeneratorTarget const* /*linkedTarget*/)
  305. {
  306. // Default implementation does nothing; only needed by some generators.
  307. return true;
  308. }
  309. bool cmExportFileGenerator::AddTargetNamespace(std::string& input,
  310. cmGeneratorTarget const* target,
  311. cmLocalGenerator const* lg)
  312. {
  313. cmGeneratorTarget::TargetOrString resolved =
  314. target->ResolveTargetReference(input, lg);
  315. cmGeneratorTarget* tgt = resolved.Target;
  316. if (!tgt) {
  317. input = resolved.String;
  318. return false;
  319. }
  320. cmFindPackageStack const& pkgStack = tgt->Target->GetFindPackageStack();
  321. if (!pkgStack.Empty() ||
  322. tgt->Target->GetProperty("EXPORT_FIND_PACKAGE_NAME")) {
  323. this->ExternalTargets.emplace(tgt);
  324. }
  325. if (tgt->IsImported()) {
  326. input = tgt->GetName();
  327. return this->NoteLinkedTarget(target, input, tgt);
  328. }
  329. if (this->ExportedTargets.find(tgt) != this->ExportedTargets.end()) {
  330. input = this->Namespace + tgt->GetExportName();
  331. } else {
  332. std::string namespacedTarget;
  333. this->HandleMissingTarget(namespacedTarget, target, tgt);
  334. if (!namespacedTarget.empty()) {
  335. input = namespacedTarget;
  336. } else {
  337. input = tgt->GetName();
  338. }
  339. }
  340. return this->NoteLinkedTarget(target, input, tgt);
  341. }
  342. void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  343. std::string& input, cmGeneratorTarget const* target,
  344. FreeTargetsReplace replace)
  345. {
  346. cmLocalGenerator const* lg = target->GetLocalGenerator();
  347. if (replace == NoReplaceFreeTargets) {
  348. this->ResolveTargetsInGeneratorExpression(input, target, lg);
  349. return;
  350. }
  351. std::vector<std::string> parts;
  352. cmGeneratorExpression::Split(input, parts);
  353. std::string sep;
  354. input.clear();
  355. for (std::string& li : parts) {
  356. if (target->IsLinkLookupScope(li, lg)) {
  357. continue;
  358. }
  359. if (cmGeneratorExpression::Find(li) == std::string::npos) {
  360. this->AddTargetNamespace(li, target, lg);
  361. } else {
  362. this->ResolveTargetsInGeneratorExpression(li, target, lg);
  363. }
  364. input += sep + li;
  365. sep = ";";
  366. }
  367. }
  368. void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  369. std::string& input, cmGeneratorTarget const* target,
  370. cmLocalGenerator const* lg)
  371. {
  372. std::string::size_type pos = 0;
  373. std::string::size_type lastPos = pos;
  374. while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) !=
  375. std::string::npos) {
  376. std::string::size_type nameStartPos = pos + cmStrLen("$<TARGET_PROPERTY:");
  377. std::string::size_type closePos = input.find('>', nameStartPos);
  378. std::string::size_type commaPos = input.find(',', nameStartPos);
  379. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  380. if (commaPos == std::string::npos // Implied 'this' target
  381. || closePos == std::string::npos // Incomplete expression.
  382. || closePos < commaPos // Implied 'this' target
  383. || nextOpenPos < commaPos) // Non-literal
  384. {
  385. lastPos = nameStartPos;
  386. continue;
  387. }
  388. std::string targetName =
  389. input.substr(nameStartPos, commaPos - nameStartPos);
  390. if (this->AddTargetNamespace(targetName, target, lg)) {
  391. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  392. }
  393. lastPos = nameStartPos + targetName.size() + 1;
  394. }
  395. std::string errorString;
  396. pos = 0;
  397. lastPos = pos;
  398. while ((pos = input.find("$<TARGET_NAME:", lastPos)) != std::string::npos) {
  399. std::string::size_type nameStartPos = pos + cmStrLen("$<TARGET_NAME:");
  400. std::string::size_type endPos = input.find('>', nameStartPos);
  401. if (endPos == std::string::npos) {
  402. errorString = "$<TARGET_NAME:...> expression incomplete";
  403. break;
  404. }
  405. std::string targetName = input.substr(nameStartPos, endPos - nameStartPos);
  406. if (targetName.find("$<") != std::string::npos) {
  407. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  408. "literal.";
  409. break;
  410. }
  411. if (!this->AddTargetNamespace(targetName, target, lg)) {
  412. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  413. "reachable target.";
  414. break;
  415. }
  416. input.replace(pos, endPos - pos + 1, targetName);
  417. lastPos = pos + targetName.size();
  418. }
  419. pos = 0;
  420. lastPos = pos;
  421. while (errorString.empty() &&
  422. (pos = input.find("$<LINK_ONLY:", lastPos)) != std::string::npos) {
  423. std::string::size_type nameStartPos = pos + cmStrLen("$<LINK_ONLY:");
  424. std::string::size_type endPos = input.find('>', nameStartPos);
  425. if (endPos == std::string::npos) {
  426. errorString = "$<LINK_ONLY:...> expression incomplete";
  427. break;
  428. }
  429. std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
  430. if (cmGeneratorExpression::IsValidTargetName(libName) &&
  431. this->AddTargetNamespace(libName, target, lg)) {
  432. input.replace(nameStartPos, endPos - nameStartPos, libName);
  433. }
  434. lastPos = nameStartPos + libName.size() + 1;
  435. }
  436. while (errorString.empty() &&
  437. (pos = input.find("$<COMPILE_ONLY:", lastPos)) != std::string::npos) {
  438. std::string::size_type nameStartPos = pos + cmStrLen("$<COMPILE_ONLY:");
  439. std::string::size_type endPos = input.find('>', nameStartPos);
  440. if (endPos == std::string::npos) {
  441. errorString = "$<COMPILE_ONLY:...> expression incomplete";
  442. break;
  443. }
  444. std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
  445. if (cmGeneratorExpression::IsValidTargetName(libName) &&
  446. this->AddTargetNamespace(libName, target, lg)) {
  447. input.replace(nameStartPos, endPos - nameStartPos, libName);
  448. }
  449. lastPos = nameStartPos + libName.size() + 1;
  450. }
  451. this->ReplaceInstallPrefix(input);
  452. if (!errorString.empty()) {
  453. target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  454. errorString);
  455. }
  456. }
  457. void cmExportFileGenerator::ReplaceInstallPrefix(std::string& /*unused*/) const
  458. {
  459. // Do nothing
  460. }
  461. void cmExportFileGenerator::SetImportDetailProperties(
  462. std::string const& config, std::string const& suffix,
  463. cmGeneratorTarget const* target, ImportPropertyMap& properties)
  464. {
  465. // Get the makefile in which to lookup target information.
  466. cmMakefile* mf = target->Makefile;
  467. // Add the soname for unix shared libraries.
  468. if (target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  469. target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  470. if (!target->IsDLLPlatform()) {
  471. std::string prop;
  472. std::string value;
  473. if (target->HasSOName(config)) {
  474. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  475. value = this->InstallNameDir(target, config);
  476. }
  477. prop = "IMPORTED_SONAME";
  478. value += target->GetSOName(config);
  479. } else {
  480. prop = "IMPORTED_NO_SONAME";
  481. value = "TRUE";
  482. }
  483. prop += suffix;
  484. properties[prop] = value;
  485. }
  486. }
  487. // Add the transitive link dependencies for this configuration.
  488. if (cmLinkInterface const* iface =
  489. target->GetLinkInterface(config, target)) {
  490. this->SetImportLinkProperty(
  491. suffix, target, "IMPORTED_LINK_INTERFACE_LANGUAGES", iface->Languages,
  492. properties, ImportLinkPropertyTargetNames::No);
  493. // Export IMPORTED_LINK_DEPENDENT_LIBRARIES to help consuming linkers
  494. // find private dependencies of shared libraries.
  495. std::size_t oldMissingTargetsSize = this->MissingTargets.size();
  496. auto oldExternalTargets = this->ExternalTargets;
  497. this->SetImportLinkProperty(
  498. suffix, target, "IMPORTED_LINK_DEPENDENT_LIBRARIES", iface->SharedDeps,
  499. properties, ImportLinkPropertyTargetNames::Yes);
  500. // Avoid enforcing shared library private dependencies as public package
  501. // dependencies by ignoring missing targets added for them.
  502. this->MissingTargets.resize(oldMissingTargetsSize);
  503. this->ExternalTargets = std::move(oldExternalTargets);
  504. if (iface->Multiplicity > 0) {
  505. std::string prop =
  506. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  507. properties[prop] = std::to_string(iface->Multiplicity);
  508. }
  509. }
  510. // Add information if this target is a managed target
  511. if (target->GetManagedType(config) !=
  512. cmGeneratorTarget::ManagedType::Native) {
  513. std::string prop = cmStrCat("IMPORTED_COMMON_LANGUAGE_RUNTIME", suffix);
  514. std::string propval;
  515. if (cmValue p = target->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  516. propval = *p;
  517. } else if (target->IsCSharpOnly()) {
  518. // C# projects do not have the /clr flag, so we set the property
  519. // here to mark the target as (only) managed (i.e. no .lib file
  520. // to link to). Otherwise the COMMON_LANGUAGE_RUNTIME target
  521. // property would have to be set manually for C# targets to make
  522. // exporting/importing work.
  523. propval = "CSharp";
  524. }
  525. properties[prop] = propval;
  526. }
  527. }
  528. namespace {
  529. std::string const& asString(std::string const& l)
  530. {
  531. return l;
  532. }
  533. std::string const& asString(cmLinkItem const& l)
  534. {
  535. return l.AsStr();
  536. }
  537. }
  538. template <typename T>
  539. void cmExportFileGenerator::SetImportLinkProperty(
  540. std::string const& suffix, cmGeneratorTarget const* target,
  541. std::string const& propName, std::vector<T> const& entries,
  542. ImportPropertyMap& properties, ImportLinkPropertyTargetNames targetNames)
  543. {
  544. // Skip the property if there are no entries.
  545. if (entries.empty()) {
  546. return;
  547. }
  548. cmLocalGenerator const* lg = target->GetLocalGenerator();
  549. // Construct the property value.
  550. std::string link_entries;
  551. char const* sep = "";
  552. for (T const& l : entries) {
  553. // Separate this from the previous entry.
  554. link_entries += sep;
  555. sep = ";";
  556. if (targetNames == ImportLinkPropertyTargetNames::Yes) {
  557. std::string temp = asString(l);
  558. this->AddTargetNamespace(temp, target, lg);
  559. link_entries += temp;
  560. } else {
  561. link_entries += asString(l);
  562. }
  563. }
  564. // Store the property.
  565. std::string prop = cmStrCat(propName, suffix);
  566. properties[prop] = link_entries;
  567. }
  568. template void cmExportFileGenerator::SetImportLinkProperty<std::string>(
  569. std::string const&, cmGeneratorTarget const*, std::string const&,
  570. std::vector<std::string> const&, ImportPropertyMap& properties,
  571. ImportLinkPropertyTargetNames);
  572. template void cmExportFileGenerator::SetImportLinkProperty<cmLinkItem>(
  573. std::string const&, cmGeneratorTarget const*, std::string const&,
  574. std::vector<cmLinkItem> const&, ImportPropertyMap& properties,
  575. ImportLinkPropertyTargetNames);
  576. namespace {
  577. enum class ExportWhen
  578. {
  579. Defined,
  580. Always,
  581. };
  582. enum class PropertyType
  583. {
  584. Strings,
  585. Paths,
  586. IncludePaths,
  587. };
  588. bool PropertyTypeIsForPaths(PropertyType pt)
  589. {
  590. switch (pt) {
  591. case PropertyType::Strings:
  592. return false;
  593. case PropertyType::Paths:
  594. case PropertyType::IncludePaths:
  595. return true;
  596. }
  597. return false;
  598. }
  599. }
  600. bool cmExportFileGenerator::PopulateCxxModuleExportProperties(
  601. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  602. cmGeneratorExpression::PreprocessContext ctx,
  603. std::string const& includesDestinationDirs, std::string& errorMessage)
  604. {
  605. if (!gte->HaveCxx20ModuleSources(&errorMessage)) {
  606. return true;
  607. }
  608. struct ModuleTargetPropertyTable
  609. {
  610. cm::static_string_view Name;
  611. ExportWhen Cond;
  612. };
  613. ModuleTargetPropertyTable const exportedDirectModuleProperties[] = {
  614. { "CXX_EXTENSIONS"_s, ExportWhen::Defined },
  615. // Always define this property as it is an intrinsic property of the target
  616. // and should not be inherited from the in-scope `CMAKE_CXX_MODULE_STD`
  617. // variable.
  618. //
  619. // TODO(cxxmodules): A future policy may make this "ON" based on the target
  620. // policies if unset. Add a new `ExportWhen` condition to handle it when
  621. // this happens.
  622. { "CXX_MODULE_STD"_s, ExportWhen::Always },
  623. };
  624. for (auto const& prop : exportedDirectModuleProperties) {
  625. auto const propNameStr = std::string(prop.Name);
  626. cmValue propValue = gte->Target->GetComputedProperty(
  627. propNameStr, *gte->Target->GetMakefile());
  628. if (!propValue) {
  629. propValue = gte->Target->GetProperty(propNameStr);
  630. }
  631. if (propValue) {
  632. properties[propNameStr] =
  633. cmGeneratorExpression::Preprocess(*propValue, ctx);
  634. } else if (prop.Cond == ExportWhen::Always) {
  635. properties[propNameStr] = "";
  636. }
  637. }
  638. struct ModulePropertyTable
  639. {
  640. cm::static_string_view Name;
  641. PropertyType Type;
  642. };
  643. ModulePropertyTable const exportedModuleProperties[] = {
  644. { "INCLUDE_DIRECTORIES"_s, PropertyType::IncludePaths },
  645. { "COMPILE_DEFINITIONS"_s, PropertyType::Strings },
  646. { "COMPILE_OPTIONS"_s, PropertyType::Strings },
  647. { "COMPILE_FEATURES"_s, PropertyType::Strings },
  648. };
  649. for (auto const& propEntry : exportedModuleProperties) {
  650. auto const propNameStr = std::string(propEntry.Name);
  651. cmValue prop = gte->Target->GetComputedProperty(
  652. propNameStr, *gte->Target->GetMakefile());
  653. if (!prop) {
  654. prop = gte->Target->GetProperty(propNameStr);
  655. }
  656. if (prop) {
  657. auto const exportedPropName =
  658. cmStrCat("IMPORTED_CXX_MODULES_", propEntry.Name);
  659. properties[exportedPropName] =
  660. cmGeneratorExpression::Preprocess(*prop, ctx);
  661. if (ctx == cmGeneratorExpression::InstallInterface &&
  662. PropertyTypeIsForPaths(propEntry.Type)) {
  663. this->ReplaceInstallPrefix(properties[exportedPropName]);
  664. this->AddImportPrefix(properties[exportedPropName]);
  665. if (propEntry.Type == PropertyType::IncludePaths &&
  666. !includesDestinationDirs.empty()) {
  667. if (!properties[exportedPropName].empty()) {
  668. properties[exportedPropName] += ';';
  669. }
  670. properties[exportedPropName] += includesDestinationDirs;
  671. }
  672. }
  673. }
  674. }
  675. cm::static_string_view const exportedLinkModuleProperties[] = {
  676. "LINK_LIBRARIES"_s,
  677. };
  678. for (auto const& propName : exportedLinkModuleProperties) {
  679. auto const propNameStr = std::string(propName);
  680. cmValue prop = gte->Target->GetComputedProperty(
  681. propNameStr, *gte->Target->GetMakefile());
  682. if (!prop) {
  683. prop = gte->Target->GetProperty(propNameStr);
  684. }
  685. if (prop) {
  686. auto const exportedPropName =
  687. cmStrCat("IMPORTED_CXX_MODULES_", propName);
  688. auto value = cmGeneratorExpression::Preprocess(*prop, ctx);
  689. this->ResolveTargetsInGeneratorExpressions(value, gte,
  690. ReplaceFreeTargets);
  691. properties[exportedPropName] = value;
  692. }
  693. }
  694. return true;
  695. }
  696. bool cmExportFileGenerator::PopulateExportProperties(
  697. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  698. std::string& errorMessage) const
  699. {
  700. auto const& targetProperties = gte->Target->GetProperties();
  701. if (cmValue exportProperties =
  702. targetProperties.GetPropertyValue("EXPORT_PROPERTIES")) {
  703. for (auto& prop : cmList{ *exportProperties }) {
  704. /* Black list reserved properties */
  705. if (cmHasLiteralPrefix(prop, "IMPORTED_") ||
  706. cmHasLiteralPrefix(prop, "INTERFACE_")) {
  707. std::ostringstream e;
  708. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  709. << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
  710. << "properties are reserved.";
  711. errorMessage = e.str();
  712. return false;
  713. }
  714. cmValue propertyValue = targetProperties.GetPropertyValue(prop);
  715. if (!propertyValue) {
  716. // Asked to export a property that isn't defined on the target. Do not
  717. // consider this an error, there's just nothing to export.
  718. continue;
  719. }
  720. std::string evaluatedValue = cmGeneratorExpression::Preprocess(
  721. *propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
  722. if (evaluatedValue != *propertyValue) {
  723. std::ostringstream e;
  724. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  725. << prop << "\" in EXPORT_PROPERTIES but this property contains a "
  726. << "generator expression. This is not allowed.";
  727. errorMessage = e.str();
  728. return false;
  729. }
  730. properties[prop] = *propertyValue;
  731. }
  732. }
  733. return true;
  734. }