From 1d815cac84475fd33865148a356f8def8ab20859 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 9 Jun 2015 21:50:22 +0000 Subject: [PATCH] LibDriver, llvm-lib: introduce. llvm-lib is intended to be a lib.exe compatible utility that also understands bitcode. The implementation lives in a library so that lld can use it to implement /lib. Differential Revision: http://reviews.llvm.org/D10297 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239434 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/LibDriver/LibDriver.h | 24 +++++ lib/CMakeLists.txt | 1 + lib/LLVMBuild.txt | 4 +- lib/LibDriver/CMakeLists.txt | 8 ++ lib/LibDriver/LLVMBuild.txt | 22 +++++ lib/LibDriver/LibDriver.cpp | 133 ++++++++++++++++++++++++++++ lib/LibDriver/Makefile | 20 +++++ lib/LibDriver/Options.td | 17 ++++ lib/Makefile | 2 +- test/CMakeLists.txt | 1 + test/Object/nm-archive.test | 9 ++ test/lit.cfg | 1 + tools/llvm-ar/CMakeLists.txt | 10 +++ tools/llvm-ar/Makefile | 5 +- tools/llvm-ar/install_symlink.cmake | 6 ++ tools/llvm-ar/llvm-ar.cpp | 17 ++-- 16 files changed, 270 insertions(+), 10 deletions(-) create mode 100644 include/llvm/LibDriver/LibDriver.h create mode 100644 lib/LibDriver/CMakeLists.txt create mode 100644 lib/LibDriver/LLVMBuild.txt create mode 100644 lib/LibDriver/LibDriver.cpp create mode 100644 lib/LibDriver/Makefile create mode 100644 lib/LibDriver/Options.td diff --git a/include/llvm/LibDriver/LibDriver.h b/include/llvm/LibDriver/LibDriver.h new file mode 100644 index 00000000000..99c783c95cb --- /dev/null +++ b/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 diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ce10998768d..d00c10f5802 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -19,3 +19,4 @@ add_subdirectory(LineEditor) add_subdirectory(ProfileData) add_subdirectory(Fuzzer) add_subdirectory(Passes) +add_subdirectory(LibDriver) diff --git a/lib/LLVMBuild.txt b/lib/LLVMBuild.txt index 7e7ebc54597..d2a2a5705b3 100644 --- a/lib/LLVMBuild.txt +++ b/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 diff --git a/lib/LibDriver/CMakeLists.txt b/lib/LibDriver/CMakeLists.txt new file mode 100644 index 00000000000..ab53a684344 --- /dev/null +++ b/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) diff --git a/lib/LibDriver/LLVMBuild.txt b/lib/LibDriver/LLVMBuild.txt new file mode 100644 index 00000000000..799dc997c0b --- /dev/null +++ b/lib/LibDriver/LLVMBuild.txt @@ -0,0 +1,22 @@ +;===- ./lib/LibDriver/LLVMBuild.txt ----------------------------*- Conf -*--===; +; +; The LLVM Compiler Infrastructure +; +; This file is distributed under the University of Illinois Open Source +; License. See LICENSE.TXT for details. +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = LibDriver +parent = Libraries +required_libraries = Object Option Support diff --git a/lib/LibDriver/LibDriver.cpp b/lib/LibDriver/LibDriver.cpp new file mode 100644 index 00000000000..d028e4bbca5 --- /dev/null +++ b/lib/LibDriver/LibDriver.cpp @@ -0,0 +1,133 @@ +//===- LibDriver.cpp - lib.exe-compatible driver --------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#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/CommandLine.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"); +} + +namespace { +// FIXME: Should re-use StringSaver from lld. +class StrDupSaver : public cl::StringSaver { + std::vector Dups; + +public: + ~StrDupSaver() override { + for (std::vector::iterator I = Dups.begin(), E = Dups.end(); I != E; + ++I) { + char *Dup = *I; + free(Dup); + } + } + const char *SaveString(const char *Str) override { + char *Dup = strdup(Str); + Dups.push_back(Dup); + return Dup; + } +}; +} + +int llvm::libDriverMain(int Argc, const char **Argv) { + SmallVector NewArgv(Argv, Argv + Argc); + StrDupSaver Saver; + cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgv); + Argv = &NewArgv[0]; + Argc = static_cast(NewArgv.size()); + + 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; +} diff --git a/lib/LibDriver/Makefile b/lib/LibDriver/Makefile new file mode 100644 index 00000000000..1c62eac9093 --- /dev/null +++ b/lib/LibDriver/Makefile @@ -0,0 +1,20 @@ +##===- lib/LibDriver/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +LIBRARYNAME = LLVMLibDriver +BUILD_ARCHIVE := 1 +BUILT_SOURCES = Options.inc +TABLEGEN_INC_FILES_COMMON = 1 + +include $(LEVEL)/Makefile.common + +$(ObjDir)/Options.inc.tmp : Options.td $(LLVM_TBLGEN) $(ObjDir)/.dir + $(Echo) "Building lib Driver Option tables with tblgen" + $(Verb) $(LLVMTableGen) -gen-opt-parser-defs -o $(call SYSPATH, $@) $< diff --git a/lib/LibDriver/Options.td b/lib/LibDriver/Options.td new file mode 100644 index 00000000000..0db84bcffb7 --- /dev/null +++ b/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">; diff --git a/lib/Makefile b/lib/Makefile index f75ca584dbe..9b76126b80a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -12,6 +12,6 @@ include $(LEVEL)/Makefile.config PARALLEL_DIRS := IR AsmParser Bitcode Analysis Transforms CodeGen Target \ ExecutionEngine Linker LTO MC Object Option DebugInfo \ - IRReader LineEditor ProfileData Passes + IRReader LineEditor ProfileData Passes LibDriver include $(LEVEL)/Makefile.common diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6e7dfd7c539..f49df542f4e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,6 +34,7 @@ set(LLVM_TEST_DEPENDS llvm-dsymutil llvm-dwarfdump llvm-extract + llvm-lib llvm-link llvm-lto llvm-mc diff --git a/test/Object/nm-archive.test b/test/Object/nm-archive.test index a9ae9cbbfbd..4cd58d33cf2 100644 --- a/test/Object/nm-archive.test +++ b/test/Object/nm-archive.test @@ -24,6 +24,15 @@ RUN: rm -f %t2 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 + +RUN: rm -f %t2 +RUN: echo /out:%t2 %t1 > %t.rsp +RUN: llvm-lib @%t.rsp +RUN: llvm-nm %t2 | FileCheck %s -check-prefix BITCODE + BITCODE: U SomeOtherFunction BITCODE-NEXT: T main BITCODE-NEXT: U puts diff --git a/test/lit.cfg b/test/lit.cfg index c9b1320f9d7..6d3c4125642 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -236,6 +236,7 @@ for pattern in [r"\bbugpoint\b(?!-)", r"\bllvm-dwarfdump\b", r"\bllvm-extract\b", r"\bllvm-go\b", + r"\bllvm-lib\b", r"\bllvm-link\b", r"\bllvm-lto\b", r"\bllvm-mc\b", diff --git a/tools/llvm-ar/CMakeLists.txt b/tools/llvm-ar/CMakeLists.txt index 3782c87e4d3..05ffe36fa7f 100644 --- a/tools/llvm-ar/CMakeLists.txt +++ b/tools/llvm-ar/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} Core + LibDriver Object Support ) @@ -26,4 +27,13 @@ add_custom_command(OUTPUT ${llvm_ranlib} add_custom_target(llvm-ranlib ALL DEPENDS ${llvm_ranlib}) set_target_properties(llvm-ranlib PROPERTIES FOLDER Tools) +set(llvm_lib "${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib${CMAKE_EXECUTABLE_SUFFIX}") + +add_custom_command(OUTPUT ${llvm_lib} + COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${llvm_ar_binary}" "${llvm_lib}" + DEPENDS llvm-ar) + +add_custom_target(llvm-lib ALL DEPENDS ${llvm_lib}) +set_target_properties(llvm-lib PROPERTIES FOLDER Tools) + install(SCRIPT install_symlink.cmake -DCMAKE_INSTALL_PREFIX=\"${CMAKE_INSTALL_PREFIX}\") diff --git a/tools/llvm-ar/Makefile b/tools/llvm-ar/Makefile index e10d6ac6ab6..824bb947300 100644 --- a/tools/llvm-ar/Makefile +++ b/tools/llvm-ar/Makefile @@ -10,9 +10,12 @@ LEVEL := ../.. TOOLNAME := llvm-ar TOOLALIAS = llvm-ranlib -LINK_COMPONENTS := all-targets bitreader support object +LINK_COMPONENTS := all-targets bitreader libdriver support object # This tool has no plugins, optimize startup time. TOOL_NO_EXPORTS := 1 include $(LEVEL)/Makefile.common + +all-local:: + $(Verb) $(AliasTool) $(notdir $(ToolBuildPath)) $(ToolDir)/llvm-lib$(EXEEXT) diff --git a/tools/llvm-ar/install_symlink.cmake b/tools/llvm-ar/install_symlink.cmake index e313897b8b3..d48431b128d 100644 --- a/tools/llvm-ar/install_symlink.cmake +++ b/tools/llvm-ar/install_symlink.cmake @@ -23,3 +23,9 @@ message("Creating llvm-ranlib") execute_process( COMMAND "${CMAKE_COMMAND}" -E ${LINK_OR_COPY} "llvm-ar${EXECUTABLE_SUFFIX}" "llvm-ranlib${EXECUTABLE_SUFFIX}" WORKING_DIRECTORY "${bindir}") + +message("Creating llvm-lib") + +execute_process( + COMMAND "${CMAKE_COMMAND}" -E ${LINK_OR_COPY} "llvm-ar${EXECUTABLE_SUFFIX}" "llvm-lib${EXECUTABLE_SUFFIX}" + WORKING_DIRECTORY "${bindir}") diff --git a/tools/llvm-ar/llvm-ar.cpp b/tools/llvm-ar/llvm-ar.cpp index 1f55e8a4968..6782b9c126a 100644 --- a/tools/llvm-ar/llvm-ar.cpp +++ b/tools/llvm-ar/llvm-ar.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" +#include "llvm/LibDriver/LibDriver.h" #include "llvm/Object/Archive.h" #include "llvm/Object/ArchiveWriter.h" #include "llvm/Object/ObjectFile.h" @@ -716,6 +717,15 @@ int main(int argc, char **argv) { PrettyStackTraceProgram X(argc, argv); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + llvm::InitializeAllTargetInfos(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllAsmParsers(); + + StringRef Stem = sys::path::stem(ToolName); + if (Stem.find("ranlib") == StringRef::npos && + Stem.find("lib") != StringRef::npos) + return libDriverMain(argc, const_cast(argv)); + // Have the command line options parsed and handle things // like --help and --version. cl::ParseCommandLineOptions(argc, argv, @@ -723,14 +733,9 @@ int main(int argc, char **argv) { " This program archives bitcode files into single libraries\n" ); - llvm::InitializeAllTargetInfos(); - llvm::InitializeAllTargetMCs(); - llvm::InitializeAllAsmParsers(); - - StringRef Stem = sys::path::stem(ToolName); if (Stem.find("ar") != StringRef::npos) return ar_main(); if (Stem.find("ranlib") != StringRef::npos) return ranlib_main(); - fail("Not ranlib or ar!"); + fail("Not ranlib, ar or lib!"); }