diff --git a/llvm/test/tools/llvm-libtool-darwin/L-and-l.test b/llvm/test/tools/llvm-libtool-darwin/L-and-l.test --- a/llvm/test/tools/llvm-libtool-darwin/L-and-l.test +++ b/llvm/test/tools/llvm-libtool-darwin/L-and-l.test @@ -62,6 +62,20 @@ # RUN: llvm-nm --print-armap %t.lib | \ # RUN: FileCheck %s --check-prefix=SINGLE-SYMBOLS -DPREFIX=%basename_t.tmp --match-full-lines +## Check that if two different files with the same names are explicitly +## specified, the command fails. +# RUN: cp %t-input2.o %t/dirname +# RUN: not llvm-libtool-darwin -static -o %t.lib \ +# RUN: %t/dirname/%basename_t.tmp-input2.o %t-input2.o 2>&1 | \ +# RUN: FileCheck %s --check-prefix=DUPLICATE-INPUT \ +# RUN: -DFILE=%basename_t.tmp-input2.o \ +# RUN: -DINPUTA=%t/dirname/%basename_t.tmp-input2.o \ +# RUN: -DINPUTB=%t-input2.o + +# DUPLICATE-INPUT: error: file '[[FILE]]' was specified multiple times. +# DUPLICATE-INPUT-DAG: [[INPUTA]] +# DUPLICATE-INPUT-DAG: [[INPUTB]] + ## -l option combined with an input file: # RUN: llvm-libtool-darwin -static -o %t.lib %t-input1.o -linput2 -L%t/dirname # RUN: llvm-ar t %t.lib | \ @@ -69,22 +83,31 @@ # RUN: llvm-nm --print-armap %t.lib | \ # RUN: FileCheck %s --check-prefix=CHECK-SYMBOLS -DPREFIX=%basename_t.tmp --match-full-lines +## Specify the same file with a -l option and an input file: +# RUN: rm -rf %t/copy +# RUN: mkdir -p %t/copy +# RUN: cp %t-input1.o %t/copy +## With the two inputs pointing to conflicting different files. +# RUN: not llvm-libtool-darwin -static -o %t.lib %t-input1.o \ +# RUN: -l%basename_t.tmp-input1.o -L%t/copy 2>&1 | \ +# RUN: FileCheck %s --check-prefix=DUPLICATE-INPUT \ +# RUN: -DFILE=%basename_t.tmp-input1.o \ +# RUN: -DINPUTA=%t-input1.o -DINPUTB=%t/copy/%basename_t.tmp-input1.o + +## With the two inputs pointing to the exact same file. +# RUN: not llvm-libtool-darwin -static -o %t.lib \ +# RUN: %t/copy/%basename_t.tmp-input1.o -l%basename_t.tmp-input1.o -L%t/copy 2>&1 | \ +# RUN: FileCheck %s --check-prefix=DUPLICATE-INPUT -DFILE=%basename_t.tmp-input1.o \ +# RUN: -DINPUTA=%t/copy/%basename_t.tmp-input1.o \ +# RUN: -DINPUTB=%t/copy/%basename_t.tmp-input1.o + ## Specify same -l option twice: -## cctools' libtool raises a warning in this case. -## The warning is not yet implemented for llvm-libtool-darwin. -# RUN: llvm-libtool-darwin -static -o %t.lib -l%basename_t.tmp-input2.o -l%basename_t.tmp-input2.o -L%T -# RUN: llvm-ar t %t.lib | \ -# RUN: FileCheck %s --check-prefix=DOUBLE-NAMES --implicit-check-not={{.}} -DPREFIX=%basename_t.tmp -# RUN: llvm-nm --print-armap %t.lib | \ -# RUN: FileCheck %s --check-prefix=DOUBLE-SYMBOLS -DPREFIX=%basename_t.tmp --match-full-lines - -# DOUBLE-NAMES: [[PREFIX]]-input2.o -# DOUBLE-NAMES-NEXT: [[PREFIX]]-input2.o - -# DOUBLE-SYMBOLS: Archive map -# DOUBLE-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o -# DOUBLE-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o -# DOUBLE-SYMBOLS-EMPTY: +# RUN: not llvm-libtool-darwin -static -o %t.lib -l%basename_t.tmp-input2.o \ +# RUN: -l%basename_t.tmp-input2.o -L%T 2>&1 | \ +# RUN: FileCheck %s --check-prefix=DUPLICATE-INPUT \ +# RUN: -DFILE=%basename_t.tmp-input2.o \ +# RUN: -DINPUTA=%t-input2.o \ +# RUN: -DINPUTB=%t-input2.o ## Check that an error is thrown when the input library cannot be found: # RUN: not llvm-libtool-darwin -static -o %t.lib -lfile-will-not-exist.o 2>&1 | \ diff --git a/llvm/test/tools/llvm-libtool-darwin/archive-flattening.test b/llvm/test/tools/llvm-libtool-darwin/archive-flattening.test --- a/llvm/test/tools/llvm-libtool-darwin/archive-flattening.test +++ b/llvm/test/tools/llvm-libtool-darwin/archive-flattening.test @@ -39,26 +39,39 @@ # FORMAT-NOT: {{.}} ## Passing both archive, bitcode and object file: -# RUN: llvm-libtool-darwin -static -o %t.lib %t-x86_64.bc %t.correct.ar %t-input1.o +# RUN: cp %t-x86_64.bc %t-x86_64.bc.copy +# RUN: cp %t-input1.o %t-input1.o.copy +# RUN: llvm-libtool-darwin -static -o %t.lib %t-x86_64.bc.copy %t.correct.ar %t-input1.o.copy # RUN: llvm-ar t %t.lib | \ # RUN: FileCheck %s --check-prefix=BOTH-NAMES --implicit-check-not={{.}} -DPREFIX=%basename_t.tmp # RUN: llvm-nm --print-armap %t.lib | \ # RUN: FileCheck %s --check-prefix=BOTH-SYMBOLS -DPREFIX=%basename_t.tmp --match-full-lines -# BOTH-NAMES: [[PREFIX]]-x86_64.bc +# BOTH-NAMES: [[PREFIX]]-x86_64.bc.copy # BOTH-NAMES-NEXT: [[PREFIX]]-x86_64.bc # BOTH-NAMES-NEXT: [[PREFIX]]-input1.o # BOTH-NAMES-NEXT: [[PREFIX]]-input2.o -# BOTH-NAMES-NEXT: [[PREFIX]]-input1.o +# BOTH-NAMES-NEXT: [[PREFIX]]-input1.o.copy # BOTH-SYMBOLS: Archive map -# BOTH-SYMBOLS-NEXT: _x86_64 in [[PREFIX]]-x86_64.bc +# BOTH-SYMBOLS-NEXT: _x86_64 in [[PREFIX]]-x86_64.bc.copy # BOTH-SYMBOLS-NEXT: _x86_64 in [[PREFIX]]-x86_64.bc # BOTH-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o # BOTH-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o -# BOTH-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o +# BOTH-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o.copy # BOTH-SYMBOLS-EMPTY: +# RUN: not llvm-libtool-darwin -static -o %t.lib %t-x86_64.bc %t.correct.ar %t-input1.o 2>&1| \ +# RUN: FileCheck %s --check-prefix=DUPLICATE-INPUT -DFILEA=%basename_t.tmp-input1.o \ +# RUN: -DPATHA=%t-input1.o -DFILEB=%basename_t.tmp-x86_64.bc -DPATHB=%t-x86_64.bc -DPATHCORRECT=%t.correct.ar + +# DUPLICATE-INPUT: file '[[FILEA]]' was specified multiple times. +# DUPLICATE-INPUT-DAG: [[PATHA]] +# DUPLICATE-INPUT-DAG: [[PATHCORRECT]] +# DUPLICATE-INPUT: file '[[FILEB]]' was specified multiple times. +# DUPLICATE-INPUT-DAG: [[PATHB]] +# DUPLICATE-INPUT-DAG: [[PATHCORRECT]] + ## Cannot read archive: # RUN: echo '!' > %t-invalid-archive.lib # RUN: echo 'invalid' >> %t-invalid-archive.lib diff --git a/llvm/test/tools/llvm-libtool-darwin/create-static-lib.test b/llvm/test/tools/llvm-libtool-darwin/create-static-lib.test --- a/llvm/test/tools/llvm-libtool-darwin/create-static-lib.test +++ b/llvm/test/tools/llvm-libtool-darwin/create-static-lib.test @@ -49,22 +49,18 @@ # OVERWRITE-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o # OVERWRITE-SYMBOLS-EMPTY: -## Duplicate a binary: -## cctools' libtool raises a warning in this case. -## The warning is not yet implemented for llvm-libtool-darwin. -# RUN: llvm-libtool-darwin -static -o %t.lib %t-input1.o %t-input2.o %t-input1.o 2>&1 | \ -# RUN: FileCheck %s --allow-empty --implicit-check-not={{.}} -# RUN: llvm-ar t %t.lib | \ -# RUN: FileCheck %s --check-prefix=DUPLICATE-NAMES --implicit-check-not={{.}} -DPREFIX=%basename_t.tmp -# RUN: llvm-nm --print-armap %t.lib | \ -# RUN: FileCheck %s --check-prefix=DUPLICATE-SYMBOLS -DPREFIX=%basename_t.tmp --match-full-lines +## Duplicate a binary: This should raise an error. +# RUN: not llvm-libtool-darwin -static -o %t.lib %t-input1.o %t-input2.o %t-input1.o 2>&1 | \ +# RUN: FileCheck %s --check-prefix=DUPLICATE-INPUT -DFILE=%basename_t.tmp-input1.o \ +# RUN: -DINPUTA=%t-input1.o -DINPUTB=%t-input1.o -# DUPLICATE-NAMES: [[PREFIX]]-input1.o -# DUPLICATE-NAMES-NEXT: [[PREFIX]]-input2.o -# DUPLICATE-NAMES-NEXT: [[PREFIX]]-input1.o +# DUPLICATE-INPUT: error: file '[[FILE]]' was specified multiple times. +# DUPLICATE-INPUT-DAG: [[INPUTA]] +# DUPLICATE-INPUT-DAG: [[INPUTB]] -# DUPLICATE-SYMBOLS: Archive map -# DUPLICATE-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o -# DUPLICATE-SYMBOLS-NEXT: _symbol2 in [[PREFIX]]-input2.o -# DUPLICATE-SYMBOLS-NEXT: _symbol1 in [[PREFIX]]-input1.o -# DUPLICATE-SYMBOLS-EMPTY: +## Make sure we can combine object files with the same name if +## they are for different architectures. +# RUN: mkdir -p %t/arm64 %t/armv7 +# RUN: llvm-as %S/Inputs/arm64-ios.ll -o %t/arm64/out.bc +# RUN: llvm-as %S/Inputs/armv7-ios.ll -o %t/armv7/out.bc +# RUN: llvm-libtool-darwin -static %t/arm64/out.bc %t/armv7/out.bc -o %t.lib diff --git a/llvm/test/tools/llvm-libtool-darwin/universal-bitcode-output.test b/llvm/test/tools/llvm-libtool-darwin/universal-bitcode-output.test --- a/llvm/test/tools/llvm-libtool-darwin/universal-bitcode-output.test +++ b/llvm/test/tools/llvm-libtool-darwin/universal-bitcode-output.test @@ -14,7 +14,8 @@ # ARCHS: Architectures in the fat file: [[FILE]] are: armv7 arm64 ## Check that the files with the same architecture are combined in an archive: -# RUN: llvm-libtool-darwin -static -o %t.lib %t-arm64.bc %t-arm64.bc %t-armv7.bc +# RUN: cp %t-arm64.bc %t-arm64.bc.copy +# RUN: llvm-libtool-darwin -static -o %t.lib %t-arm64.bc %t-arm64.bc.copy %t-armv7.bc # RUN: llvm-lipo -info %t.lib | \ # RUN: FileCheck %s --check-prefix=ARCHS -DFILE=%t.lib # RUN: llvm-objdump --macho --arch all --all-headers %t.lib | \ @@ -26,7 +27,7 @@ # UNIVERSAL-MEMBERS: Archive : [[FILE]] (architecture arm64) # UNIVERSAL-MEMBERS-NEXT: __.SYMDEF # UNIVERSAL-MEMBERS-NEXT: [[PREFIX]]-arm64.bc -# UNIVERSAL-MEMBERS-NEXT: [[PREFIX]]-arm64.bc +# UNIVERSAL-MEMBERS-NEXT: [[PREFIX]]-arm64.bc.copy ## Check that the files extracted from a universal output are archives: # RUN: llvm-libtool-darwin -static -o %t.lib %t-arm64.bc %t-armv7.bc diff --git a/llvm/test/tools/llvm-libtool-darwin/universal-object-output.test b/llvm/test/tools/llvm-libtool-darwin/universal-object-output.test --- a/llvm/test/tools/llvm-libtool-darwin/universal-object-output.test +++ b/llvm/test/tools/llvm-libtool-darwin/universal-object-output.test @@ -14,7 +14,8 @@ # ARCHS: Architectures in the fat file: [[FILE]] are: armv6 armv7 ## Check that the files with the same architecture are combined in an archive: -# RUN: llvm-libtool-darwin -static -o %t.lib %t.armv6 %t.armv6 %t.armv7 +# RUN: cp %t.armv6 %t.armv6.copy +# RUN: llvm-libtool-darwin -static -o %t.lib %t.armv6 %t.armv6.copy %t.armv7 # RUN: llvm-lipo -info %t.lib | \ # RUN: FileCheck %s --check-prefix=ARCHS -DFILE=%t.lib # RUN: llvm-objdump --macho --arch all --all-headers %t.lib | \ @@ -23,7 +24,7 @@ # UNIVERSAL-MEMBERS: Archive : [[FILE]] (architecture armv6) # UNIVERSAL-MEMBERS-NEXT: __.SYMDEF # UNIVERSAL-MEMBERS-NEXT: [[PREFIX]].armv6 -# UNIVERSAL-MEMBERS-NEXT: [[PREFIX]].armv6 +# UNIVERSAL-MEMBERS-NEXT: [[PREFIX]].armv6.copy # UNIVERSAL-MEMBERS: Archive : [[FILE]] (architecture armv7) # UNIVERSAL-MEMBERS-NEXT: __.SYMDEF # UNIVERSAL-MEMBERS-NEXT: [[PREFIX]].armv7 diff --git a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp --- a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp +++ b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp @@ -23,6 +23,7 @@ #include "llvm/Support/LineIterator.h" #include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/WithColor.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/TextAPI/Architecture.h" #include @@ -31,9 +32,6 @@ static LLVMContext LLVMCtx; -typedef std::map> - MembersPerArchitectureMap; - cl::OptionCategory LibtoolCategory("llvm-libtool-darwin Options"); static cl::opt OutputFile("o", cl::desc("Specify output filename"), @@ -101,6 +99,28 @@ "/usr/local/lib", }; +// NewArchiveMemberList instances serve as collections of archive members and +// information about those members. +class NewArchiveMemberList { + std::vector Members; + // This vector contains the file that each NewArchiveMember from Members came + // from. Therefore, it has the same size as Members. + std::vector Files; + +public: + // Add a NewArchiveMember and the file it came from to the list. + void pushBack(NewArchiveMember &&Member, StringRef File) { + Members.push_back(std::move(Member)); + Files.push_back(File); + } + + const std::vector &getMembers() const { return Members; } + + const std::vector &getFiles() const { return Files; } +}; + +typedef std::map MembersPerArchitectureMap; + struct Config { bool Deterministic = true; // Updated by 'D' and 'U' modifiers. uint32_t ArchCPUType; @@ -297,7 +317,7 @@ WithColor::warning() << Member.MemberName + " has no symbols\n"; uint64_t FileCPUID = getCPUID(FileCPUType, FileCPUSubtype); - Members[FileCPUID].push_back(std::move(Member)); + Members[FileCPUID].pushBack(std::move(Member), FileName); return Error::success(); } @@ -328,7 +348,7 @@ } uint64_t FileCPUID = getCPUID(*FileCPUTypeOrErr, *FileCPUSubTypeOrErr); - Members[FileCPUID].push_back(std::move(Member)); + Members[FileCPUID].pushBack(std::move(Member), FileName); return Error::success(); } @@ -463,6 +483,42 @@ return Slices; } +static Error +checkForDuplicates(const MembersPerArchitectureMap &MembersPerArch) { + for (const auto &M : MembersPerArch) { + const auto &Members = M.second.getMembers(); + const auto &Files = M.second.getFiles(); + std::map> MembersToFiles; + for (auto iterators = std::make_pair(Members.cbegin(), Files.cbegin()); + iterators.first != Members.cend(); + // which should imply iterators.second != Files.cend() + ++iterators.first, ++iterators.second) { + MembersToFiles[iterators.first->MemberName].push_back(*iterators.second); + } + + std::string ErrorData; + raw_string_ostream ErrorStream(ErrorData); + for (const std::pair> &pair : + MembersToFiles) { + if (pair.second.size() > 1) { + ErrorStream << "file '" << pair.first.str() + << "' was specified multiple times.\n"; + + for (StringRef OriginalFile : pair.second) { + ErrorStream << "in: " << OriginalFile.str() << '\n'; + } + ErrorStream << '\n'; + } + } + + ErrorStream.flush(); + if (ErrorData.size() > 0) { + return createStringError(std::errc::invalid_argument, ErrorData.c_str()); + } + } + return Error::success(); +} + static Error createStaticLibrary(const Config &C) { MembersPerArchitectureMap NewMembers; std::vector> FileBuffers; @@ -481,19 +537,23 @@ ArchType.c_str()); } + if (Error E = checkForDuplicates(NewMembers)) { + return E; + } + if (NewMembers.size() == 1) { if (Error E = - writeArchive(OutputFile, NewMembers.begin()->second, + writeArchive(OutputFile, NewMembers.begin()->second.getMembers(), /*WriteSymtab=*/true, /*Kind=*/object::Archive::K_DARWIN, C.Deterministic, /*Thin=*/false)) return E; } else { SmallVector, 2> OutputBinaries; - for (const std::pair> &M : + for (const std::pair &M : NewMembers) { Expected> OutputBufferOrErr = - writeArchiveToBuffer(M.second, + writeArchiveToBuffer(M.second.getMembers(), /*WriteSymtab=*/true, /*Kind=*/object::Archive::K_DARWIN, C.Deterministic,