Skip to content

Commit 4c12a75

Browse files
committed
[llvm-libtool-darwin] Add -warnings_as_errors
libtool can currently produce 2 warnings: 1. No symbols were in the object file 2. An object file with the same basename was specified multiple times The first warning here is often harmless and may just mean you have some translation units with no symbols for the target you're building for. The second warning can lead to real issues like those mentioned in https://reviews.llvm.org/D113130 where ODR violations can slip in. This introduces a new -warnings_as_errors flag that can be used by build systems that want to verify they never hit these warnings. For example with bazel the libtool caller first uniques names to make sure the duplicate base name case is not possible, but if that doesn't work as expected, having it fail would be preferred. It's also worth noting that llvm-libtool-darwin works around an issue that cctools libtool experiences related to debug info and duplicate basenames, the workaround is described here: https://github.com/llvm/llvm-project/blob/30baa5d2a450d5e302d8cba3fc7a26a59d4b7ae1/llvm/lib/Object/ArchiveWriter.cpp#L424-L465 And it avoids this bug: https://github.com/keith/radars/tree/f0cbbb1c37126ec6528c132510b29e08566377a7/DuplicateBasenameIssue Differential Revision: https://reviews.llvm.org/D118931
1 parent d1ecfaa commit 4c12a75

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

llvm/docs/CommandGuide/llvm-libtool-darwin.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ OPTIONS
6868

6969
Do not warn about files that have no symbols.
7070

71+
.. option:: -warnings_as_errors
72+
73+
Produce a non-zero exit status if any warnings are emitted.
74+
7175
.. option:: -o <filename>
7276

7377
Specify the output file name. Must be specified exactly once.

llvm/test/tools/llvm-libtool-darwin/create-static-lib.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
# DUPLICATE-INPUT-DAG: [[INPUTA]]
5959
# DUPLICATE-INPUT-DAG: [[INPUTB]]
6060

61+
# RUN: not llvm-libtool-darwin -warnings_as_errors -static -o %t.lib %t-input1.o %t-input2.o %t-input1.o 2>&1 | \
62+
# RUN: FileCheck %s --check-prefix=ERROR-DUPLICATE-INPUT -DFILE=%basename_t.tmp-input1.o \
63+
# RUN: -DINPUTA=%t-input1.o -DINPUTB=%t-input1.o
64+
65+
# ERROR-DUPLICATE-INPUT: error: file '[[FILE]]' was specified multiple times.
66+
# ERROR-DUPLICATE-INPUT-DAG: [[INPUTA]]
67+
# ERROR-DUPLICATE-INPUT-DAG: [[INPUTB]]
68+
6169
## Make sure we can combine object files with the same name if
6270
## they are for different architectures.
6371
# RUN: mkdir -p %t/arm64 %t/armv7

llvm/test/tools/llvm-libtool-darwin/no-symbols-warning.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
# WARNING: warning: '[[PREFIX]]-x86_64-empty.o': has no symbols for architecture x86_64
1313

14+
# RUN: not llvm-libtool-darwin -static -warnings_as_errors -o %t-error.lib %t-x86_64-empty.o 2>&1 | \
15+
# RUN: FileCheck --check-prefix=ERROR %s -DPREFIX=%basename_t.tmp
16+
17+
# ERROR: error: '[[PREFIX]]-x86_64-empty.o': has no symbols for architecture x86_64
18+
1419
# RUN: llvm-libtool-darwin -no_warning_for_no_symbols -static -o %t.lib \
1520
# RUN: %t-x86_64-empty.o 2>&1 | \
1621
# RUN: FileCheck %s --allow-empty --implicit-check-not='warning:'

llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ static cl::opt<bool> NoWarningForNoSymbols(
9898
cl::desc("Do not warn about files that have no symbols"),
9999
cl::cat(LibtoolCategory), cl::init(false));
100100

101+
static cl::opt<bool> WarningsAsErrors("warnings_as_errors",
102+
cl::desc("Treat warnings as errors"),
103+
cl::cat(LibtoolCategory),
104+
cl::init(false));
105+
101106
static const std::array<std::string, 3> StandardSearchDirs{
102107
"/lib",
103108
"/usr/lib",
@@ -370,10 +375,17 @@ class MembersBuilder {
370375
return Error::success();
371376
}
372377

373-
if (!NoWarningForNoSymbols && O->symbols().empty())
374-
WithColor::warning() << "'" + Member.MemberName +
375-
"': has no symbols for architecture " +
376-
O->getArchTriple().getArchName() + "\n";
378+
if (!NoWarningForNoSymbols && O->symbols().empty()) {
379+
Error E = createFileError(
380+
Member.MemberName,
381+
createStringError(std::errc::invalid_argument,
382+
"has no symbols for architecture %s",
383+
O->getArchTriple().getArchName().str().c_str()));
384+
385+
if (WarningsAsErrors)
386+
return E;
387+
WithColor::defaultWarningHandler(std::move(E));
388+
}
377389

378390
uint64_t FileCPUID = getCPUID(FileCPUType, FileCPUSubtype);
379391
Builder.Data.MembersPerArchitecture[FileCPUID].push_back(
@@ -581,8 +593,11 @@ static Error createStaticLibrary(const Config &C) {
581593

582594
const auto &NewMembers = DataOrError->MembersPerArchitecture;
583595

584-
if (Error E = checkForDuplicates(NewMembers))
596+
if (Error E = checkForDuplicates(NewMembers)) {
597+
if (WarningsAsErrors)
598+
return E;
585599
WithColor::defaultWarningHandler(std::move(E));
600+
}
586601

587602
if (NewMembers.size() == 1)
588603
return writeArchive(OutputFile, NewMembers.begin()->second.getMembers(),

0 commit comments

Comments
 (0)