cmExportFileGenerator.cxx 27 KB

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