Index: include/llvm/LibDriver/LibDriver.h =================================================================== --- /dev/null +++ include/llvm/LibDriver/LibDriver.h @@ -0,0 +1,24 @@ +//===- llvm/LibDriver/LibDriver.h - lib.exe-compatible driver ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Defines an interface to a lib.exe-compatible driver that also understands +// bitcode files. Used by llvm-lib and lld-link2 /lib. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBDRIVER_LIBDRIVER_H +#define LLVM_LIBDRIVER_LIBDRIVER_H + +namespace llvm { + +int LibDriverMain(int argc, const char **argv); + +} + +#endif Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -19,3 +19,4 @@ add_subdirectory(ProfileData) add_subdirectory(Fuzzer) add_subdirectory(Passes) +add_subdirectory(LibDriver) Index: lib/LLVMBuild.txt =================================================================== --- lib/LLVMBuild.txt +++ lib/LLVMBuild.txt @@ -17,8 +17,8 @@ [common] subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine - LineEditor Linker IR IRReader LTO MC Object Option Passes ProfileData Support - TableGen Target Transforms + LibDriver LineEditor Linker IR IRReader LTO MC Object Option Passes ProfileData + Support TableGen Target Transforms [component_0] type = Group Index: lib/LibDriver/CMakeLists.txt =================================================================== --- /dev/null +++ lib/LibDriver/CMakeLists.txt @@ -0,0 +1,8 @@ +set(LLVM_TARGET_DEFINITIONS Options.td) +tablegen(LLVM Options.inc -gen-opt-parser-defs) +add_public_tablegen_target(LibOptionsTableGen) + +add_llvm_library(LLVMLibDriver + LibDriver.cpp + ) +add_dependencies(LLVMLibDriver LibOptionsTableGen) Index: lib/LibDriver/LLVMBuild.txt =================================================================== --- lib/LibDriver/LLVMBuild.txt +++ lib/LibDriver/LLVMBuild.txt @@ -1,4 +1,4 @@ -;===- ./lib/LLVMBuild.txt --------------------------------------*- Conf -*--===; +;===- ./lib/LibDriver/LLVMBuild.txt ----------------------------*- Conf -*--===; ; ; The LLVM Compiler Infrastructure ; @@ -15,12 +15,8 @@ ; ;===------------------------------------------------------------------------===; -[common] -subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine - LineEditor Linker IR IRReader LTO MC Object Option Passes ProfileData Support - TableGen Target Transforms - [component_0] -type = Group -name = Libraries -parent = $ROOT +type = Library +name = LibDriver +parent = Libraries +required_libraries = Object Option Support Index: lib/LibDriver/LibDriver.cpp =================================================================== --- /dev/null +++ lib/LibDriver/LibDriver.cpp @@ -0,0 +1,90 @@ +#include "llvm/LibDriver/LibDriver.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Object/ArchiveWriter.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +namespace { + +enum { + OPT_INVALID = 0, +#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID, +#include "Options.inc" +#undef OPTION +}; + +#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#include "Options.inc" +#undef PREFIX + +static const llvm::opt::OptTable::Info infoTable[] = { +#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ + { \ + X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \ + OPT_##GROUP, OPT_##ALIAS, X6 \ + }, +#include "Options.inc" +#undef OPTION +}; + +class LibOptTable : public llvm::opt::OptTable { +public: + LibOptTable() : OptTable(infoTable, llvm::array_lengthof(infoTable), true) {} +}; + +} + +static std::string getOutputPath(llvm::opt::InputArgList *Args) { + if (auto *Arg = Args->getLastArg(OPT_out)) + return Arg->getValue(); + for (auto *Arg : Args->filtered(OPT_INPUT)) { + if (!StringRef(Arg->getValue()).endswith_lower(".obj")) + continue; + SmallString<128> Val = StringRef(Arg->getValue()); + llvm::sys::path::replace_extension(Val, ".lib"); + return Val.str(); + } + llvm_unreachable("internal error"); +} + +int llvm::LibDriverMain(int Argc, const char **Argv) { + LibOptTable Table; + unsigned MissingIndex; + unsigned MissingCount; + std::unique_ptr Args( + Table.ParseArgs(&Argv[1], &Argv[Argc], MissingIndex, MissingCount)); + if (MissingCount) { + llvm::errs() << "missing arg value for \"" + << Args->getArgString(MissingIndex) + << "\", expected " << MissingCount + << (MissingCount == 1 ? " argument.\n" : " arguments.\n"); + return 1; + } + for (auto *Arg : Args->filtered(OPT_UNKNOWN)) + llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n"; + + if (Args->filtered_begin(OPT_INPUT) == Args->filtered_end()) { + llvm::errs() << "no input files.\n"; + return 1; + } + + std::vector Members; + for (auto *Arg : Args->filtered(OPT_INPUT)) + Members.emplace_back(Arg->getValue(), llvm::sys::path::filename(Arg->getValue())); + + std::pair Result = + llvm::WriteArchive(getOutputPath(Args.get()), Members, /*WriteSymtab=*/true); + if (Result.second) { + if (Result.first.empty()) + Result.first = Argv[0]; + llvm::errs() << Result.first << ": " << Result.second.message() << "\n"; + return 1; + } + + return 0; +} Index: lib/LibDriver/Options.td =================================================================== --- /dev/null +++ lib/LibDriver/Options.td @@ -0,0 +1,17 @@ +include "llvm/Option/OptParser.td" + +// lib.exe accepts options starting with either a dash or a slash. + +// Flag that takes no arguments. +class F : Flag<["/", "-", "-?"], name>; + +// Flag that takes one argument after ":". +class P : + Joined<["/", "-", "-?"], name#":">, HelpText; + +def out : P<"out", "Path to file to write output">; + +//============================================================================== +// The flags below do nothing. They are defined only for lib.exe compatibility. +//============================================================================== +def nologo : F<"nologo">; Index: test/Object/nm-archive.test =================================================================== --- test/Object/nm-archive.test +++ test/Object/nm-archive.test @@ -24,6 +24,10 @@ RUN: llvm-ar rcs %t2 %t1 RUN: llvm-nm %t2 | FileCheck %s -check-prefix BITCODE +RUN: rm -f %t2 +RUN: llvm-lib /out:%t2 %t1 +RUN: llvm-nm %t2 | FileCheck %s -check-prefix BITCODE + BITCODE: U SomeOtherFunction BITCODE-NEXT: T main BITCODE-NEXT: U puts Index: tools/CMakeLists.txt =================================================================== --- tools/CMakeLists.txt +++ tools/CMakeLists.txt @@ -23,6 +23,7 @@ add_llvm_tool_subdirectory(llvm-ar) add_llvm_tool_subdirectory(llvm-nm) add_llvm_tool_subdirectory(llvm-size) +add_llvm_tool_subdirectory(llvm-lib) add_llvm_tool_subdirectory(llvm-cov) add_llvm_tool_subdirectory(llvm-profdata) Index: tools/LLVMBuild.txt =================================================================== --- tools/LLVMBuild.txt +++ tools/LLVMBuild.txt @@ -17,9 +17,9 @@ [common] subdirectories = bugpoint llc lli llvm-ar llvm-as llvm-bcanalyzer llvm-cov - llvm-diff llvm-dis llvm-dwarfdump llvm-extract llvm-jitlistener llvm-link - llvm-lto llvm-mc llvm-nm llvm-objdump llvm-pdbdump llvm-profdata llvm-rtdyld - llvm-size macho-dump opt llvm-mcmarkup verify-uselistorder dsymutil + llvm-diff llvm-dis llvm-dwarfdump llvm-extract llvm-jitlistener llvm-lib + llvm-link llvm-lto llvm-mc llvm-nm llvm-objdump llvm-pdbdump llvm-profdata + llvm-rtdyld llvm-size macho-dump opt llvm-mcmarkup verify-uselistorder dsymutil [component_0] type = Group Index: tools/llvm-lib/CMakeLists.txt =================================================================== --- /dev/null +++ tools/llvm-lib/CMakeLists.txt @@ -0,0 +1,8 @@ +set(LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + LibDriver + ) + +add_llvm_tool(llvm-lib + llvm-lib.cpp + ) Index: tools/llvm-lib/LLVMBuild.txt =================================================================== --- tools/llvm-lib/LLVMBuild.txt +++ tools/llvm-lib/LLVMBuild.txt @@ -1,4 +1,4 @@ -;===- ./lib/LLVMBuild.txt --------------------------------------*- Conf -*--===; +;===- ./tools/llvm-ar/LLVMBuild.txt ----------------------------*- Conf -*--===; ; ; The LLVM Compiler Infrastructure ; @@ -15,12 +15,7 @@ ; ;===------------------------------------------------------------------------===; -[common] -subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine - LineEditor Linker IR IRReader LTO MC Object Option Passes ProfileData Support - TableGen Target Transforms - [component_0] -type = Group -name = Libraries -parent = $ROOT +type = Tool +name = llvm-lib +parent = Tools Index: tools/llvm-lib/llvm-lib.cpp =================================================================== --- /dev/null +++ tools/llvm-lib/llvm-lib.cpp @@ -0,0 +1,23 @@ +//===-- llvm-lib.cpp - lib.exe compatibile archive utility ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This is a lib.exe compatible archive utility that also understands bitcode. +// +//===----------------------------------------------------------------------===// + +#include "llvm/LibDriver/LibDriver.h" +#include "llvm/Support/TargetSelect.h" + +int main(int argc, char **argv) { + llvm::InitializeAllTargetInfos(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllAsmParsers(); + + return llvm::LibDriverMain(argc, const_cast(argv)); +}