summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2015-06-09 21:50:22 +0000
committerPeter Collingbourne <peter@pcc.me.uk>2015-06-09 21:50:22 +0000
commitbc05163f15cdff70cf2be97d8f48af08b5e87ea5 (patch)
tree39c799c35f948f949806bd4c83a7ae79f62c085f /llvm/lib
parentf12c030f4879e2bf2caca21976f5de9ba8c48bdd (diff)
downloadbcm5719-llvm-bc05163f15cdff70cf2be97d8f48af08b5e87ea5.tar.gz
bcm5719-llvm-bc05163f15cdff70cf2be97d8f48af08b5e87ea5.zip
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 llvm-svn: 239434
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/CMakeLists.txt1
-rw-r--r--llvm/lib/LLVMBuild.txt4
-rw-r--r--llvm/lib/LibDriver/CMakeLists.txt8
-rw-r--r--llvm/lib/LibDriver/LLVMBuild.txt22
-rw-r--r--llvm/lib/LibDriver/LibDriver.cpp133
-rw-r--r--llvm/lib/LibDriver/Makefile20
-rw-r--r--llvm/lib/LibDriver/Options.td17
-rw-r--r--llvm/lib/Makefile2
8 files changed, 204 insertions, 3 deletions
diff --git a/llvm/lib/CMakeLists.txt b/llvm/lib/CMakeLists.txt
index ce10998768d..d00c10f5802 100644
--- a/llvm/lib/CMakeLists.txt
+++ b/llvm/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/llvm/lib/LLVMBuild.txt b/llvm/lib/LLVMBuild.txt
index 7e7ebc54597..d2a2a5705b3 100644
--- a/llvm/lib/LLVMBuild.txt
+++ b/llvm/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/llvm/lib/LibDriver/CMakeLists.txt b/llvm/lib/LibDriver/CMakeLists.txt
new file mode 100644
index 00000000000..ab53a684344
--- /dev/null
+++ b/llvm/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/llvm/lib/LibDriver/LLVMBuild.txt b/llvm/lib/LibDriver/LLVMBuild.txt
new file mode 100644
index 00000000000..799dc997c0b
--- /dev/null
+++ b/llvm/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/llvm/lib/LibDriver/LibDriver.cpp b/llvm/lib/LibDriver/LibDriver.cpp
new file mode 100644
index 00000000000..d028e4bbca5
--- /dev/null
+++ b/llvm/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<char *> Dups;
+
+public:
+ ~StrDupSaver() override {
+ for (std::vector<char *>::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<const char *, 20> NewArgv(Argv, Argv + Argc);
+ StrDupSaver Saver;
+ cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgv);
+ Argv = &NewArgv[0];
+ Argc = static_cast<int>(NewArgv.size());
+
+ LibOptTable Table;
+ unsigned MissingIndex;
+ unsigned MissingCount;
+ std::unique_ptr<llvm::opt::InputArgList> 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<llvm::NewArchiveIterator> Members;
+ for (auto *Arg : Args->filtered(OPT_INPUT))
+ Members.emplace_back(Arg->getValue(),
+ llvm::sys::path::filename(Arg->getValue()));
+
+ std::pair<StringRef, std::error_code> 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/llvm/lib/LibDriver/Makefile b/llvm/lib/LibDriver/Makefile
new file mode 100644
index 00000000000..1c62eac9093
--- /dev/null
+++ b/llvm/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/llvm/lib/LibDriver/Options.td b/llvm/lib/LibDriver/Options.td
new file mode 100644
index 00000000000..0db84bcffb7
--- /dev/null
+++ b/llvm/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<string name> : Flag<["/", "-", "-?"], name>;
+
+// Flag that takes one argument after ":".
+class P<string name, string help> :
+ Joined<["/", "-", "-?"], name#":">, HelpText<help>;
+
+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/llvm/lib/Makefile b/llvm/lib/Makefile
index f75ca584dbe..9b76126b80a 100644
--- a/llvm/lib/Makefile
+++ b/llvm/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
OpenPOWER on IntegriCloud