Static libraries de-duplication: keep first occurrence

Fixes: #26335
This commit is contained in:
Marc Chevrier
2024-09-29 15:56:04 +02:00
committed by Brad King
parent 9b5c805bf6
commit cd418d4bb6
9 changed files with 134 additions and 24 deletions
+63 -2
View File
@@ -381,6 +381,34 @@ public:
target->GetBacktrace());
CM_FALLTHROUGH;
case cmPolicies::NEW: {
// Policy 0179 applies only when policy 0156 is new
switch (target->GetPolicyStatusCMP0179()) {
case cmPolicies::WARN:
if (!makefile->GetCMakeInstance()->GetIsInTryCompile() &&
makefile->PolicyOptionalWarningEnabled(
"CMAKE_POLICY_WARNING_CMP0179")) {
makefile->GetCMakeInstance()->IssueMessage(
MessageType::AUTHOR_WARNING,
cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0179),
"\nSince the policy is not set, static libraries "
"de-duplication will keep the last occurrence of the "
"static libraries."),
target->GetBacktrace());
}
CM_FALLTHROUGH;
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
makefile->GetCMakeInstance()->IssueMessage(
MessageType::FATAL_ERROR,
cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0179),
target->GetBacktrace());
CM_FALLTHROUGH;
case cmPolicies::NEW:
break;
}
if (auto libProcessing = makefile->GetDefinition(cmStrCat(
"CMAKE_", linkLanguage, "_LINK_LIBRARIES_PROCESSING"))) {
// UNICITY keyword is just for compatibility with previous
@@ -444,9 +472,42 @@ public:
void AddLibraries(const std::vector<size_t>& libEntries)
{
if (this->Order == Reverse) {
std::vector<size_t> entries;
if (this->Deduplication == All &&
this->Target->GetPolicyStatusCMP0179() == cmPolicies::NEW) {
// keep the first occurrence of the static libraries
std::set<size_t> emitted{ this->Emitted };
std::set<std::string> importedEmitted;
for (auto index : libEntries) {
LinkEntry const& entry = this->Entries[index];
if (!entry.Target ||
entry.Target->GetType() != cmStateEnums::STATIC_LIBRARY) {
entries.emplace_back(index);
continue;
}
if (this->IncludeEntry(entry)) {
entries.emplace_back(index);
continue;
}
if (entry.Target->IsImported()) {
if (emitted.insert(index).second &&
importedEmitted
.insert(cmSystemTools::GetRealPath(entry.Item.Value))
.second) {
entries.emplace_back(index);
}
continue;
}
if (emitted.insert(index).second) {
entries.emplace_back(index);
}
}
} else {
entries = libEntries;
}
// Iterate in reverse order so we can keep only the last occurrence
// of a library.
this->AddLibraries(cmReverseRange(libEntries));
// of the shared libraries.
this->AddLibraries(cmReverseRange(entries));
} else {
this->AddLibraries(cmMakeRange(libEntries));
}
+7 -2
View File
@@ -545,7 +545,11 @@ class cmMakefile;
SELECT(POLICY, CMP0177, "install() DESTINATION paths are normalized.", 3, \
31, 0, cmPolicies::WARN) \
SELECT(POLICY, CMP0178, "Test command lines preserve empty arguments.", 3, \
31, 0, cmPolicies::WARN)
31, 0, cmPolicies::WARN) \
SELECT(POLICY, CMP0179, \
"De-duplication of static libraries on link lines keeps first " \
"occurrence.", \
3, 31, 0, cmPolicies::WARN)
#define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
#define CM_FOR_EACH_POLICY_ID(POLICY) \
@@ -589,7 +593,8 @@ class cmMakefile;
F(CMP0156) \
F(CMP0157) \
F(CMP0160) \
F(CMP0162)
F(CMP0162) \
F(CMP0179)
#define CM_FOR_EACH_CUSTOM_COMMAND_POLICY(F) \
F(CMP0116) \