llvm.org GIT mirror llvm / a844915
Move lib/LibDriver -> lib/ToolDrivers/llvm-lib. NFCI. This reorganisation prevents us from cluttering up the top-level lib directory with more driver libraries such as llvm-dlltool (see D29892). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302995 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
13 changed file(s) with 253 addition(s) and 253 deletion(s). Raw diff Collapse all Expand all
+0
-24
include/llvm/LibDriver/LibDriver.h less more
None //===- llvm/LibDriver/LibDriver.h - lib.exe-compatible driver ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines an interface to a lib.exe-compatible driver that also understands
10 // bitcode files. Used by llvm-lib and lld-link /lib.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIBDRIVER_LIBDRIVER_H
15 #define LLVM_LIBDRIVER_LIBDRIVER_H
16
17 namespace llvm {
18 template class ArrayRef;
19
20 int libDriverMain(ArrayRef ARgs);
21 }
22
23 #endif
0 //===- llvm-lib/LibDriver.h - lib.exe-compatible driver ---------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines an interface to a lib.exe-compatible driver that also understands
10 // bitcode files. Used by llvm-lib and lld-link /lib.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TOOLDRIVERS_LLVM_LIB_LIBDRIVER_H
15 #define LLVM_TOOLDRIVERS_LLVM_LIB_LIBDRIVER_H
16
17 namespace llvm {
18 template class ArrayRef;
19
20 int libDriverMain(ArrayRef ARgs);
21 }
22
23 #endif
2020 add_subdirectory(ProfileData)
2121 add_subdirectory(Fuzzer)
2222 add_subdirectory(Passes)
23 add_subdirectory(LibDriver)
23 add_subdirectory(ToolDrivers)
2424 add_subdirectory(XRay)
2323 DebugInfo
2424 Demangle
2525 ExecutionEngine
26 LibDriver
2726 LineEditor
2827 Linker
2928 IR
3837 Support
3938 TableGen
4039 Target
40 ToolDrivers
4141 Transforms
4242
4343 [component_0]
+0
-8
lib/LibDriver/CMakeLists.txt less more
None set(LLVM_TARGET_DEFINITIONS Options.td)
1 tablegen(LLVM Options.inc -gen-opt-parser-defs)
2 add_public_tablegen_target(LibOptionsTableGen)
3
4 add_llvm_library(LLVMLibDriver
5 LibDriver.cpp
6 )
7 add_dependencies(LLVMLibDriver LibOptionsTableGen)
+0
-22
lib/LibDriver/LLVMBuild.txt less more
None ;===- ./lib/LibDriver/LLVMBuild.txt ----------------------------*- Conf -*--===;
1 ;
2 ; The LLVM Compiler Infrastructure
3 ;
4 ; This file is distributed under the University of Illinois Open Source
5 ; License. See LICENSE.TXT for details.
6 ;
7 ;===------------------------------------------------------------------------===;
8 ;
9 ; This is an LLVMBuild description file for the components in this subdirectory.
10 ;
11 ; For more information on the LLVMBuild system, please see:
12 ;
13 ; http://llvm.org/docs/LLVMBuild.html
14 ;
15 ;===------------------------------------------------------------------------===;
16
17 [component_0]
18 type = Library
19 name = LibDriver
20 parent = Libraries
21 required_libraries = Object Option Support
+0
-171
lib/LibDriver/LibDriver.cpp less more
None //===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines an interface to a lib.exe-compatible driver that also understands
10 // bitcode files. Used by llvm-lib and lld-link /lib.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/LibDriver/LibDriver.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Object/ArchiveWriter.h"
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Option/Option.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/StringSaver.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/Process.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 namespace {
29
30 enum {
31 OPT_INVALID = 0,
32 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
33 #include "Options.inc"
34 #undef OPTION
35 };
36
37 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
38 #include "Options.inc"
39 #undef PREFIX
40
41 static const llvm::opt::OptTable::Info infoTable[] = {
42 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
43 { \
44 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
45 OPT_##GROUP, OPT_##ALIAS, X6 \
46 },
47 #include "Options.inc"
48 #undef OPTION
49 };
50
51 class LibOptTable : public llvm::opt::OptTable {
52 public:
53 LibOptTable() : OptTable(infoTable, true) {}
54 };
55
56 }
57
58 static std::string getOutputPath(llvm::opt::InputArgList *Args,
59 const llvm::NewArchiveMember &FirstMember) {
60 if (auto *Arg = Args->getLastArg(OPT_out))
61 return Arg->getValue();
62 SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
63 llvm::sys::path::replace_extension(Val, ".lib");
64 return Val.str();
65 }
66
67 static std::vector getSearchPaths(llvm::opt::InputArgList *Args,
68 StringSaver &Saver) {
69 std::vector Ret;
70 // Add current directory as first item of the search path.
71 Ret.push_back("");
72
73 // Add /libpath flags.
74 for (auto *Arg : Args->filtered(OPT_libpath))
75 Ret.push_back(Arg->getValue());
76
77 // Add $LIB.
78 Optional EnvOpt = sys::Process::GetEnv("LIB");
79 if (!EnvOpt.hasValue())
80 return Ret;
81 StringRef Env = Saver.save(*EnvOpt);
82 while (!Env.empty()) {
83 StringRef Path;
84 std::tie(Path, Env) = Env.split(';');
85 Ret.push_back(Path);
86 }
87 return Ret;
88 }
89
90 static Optional findInputFile(StringRef File,
91 ArrayRef Paths) {
92 for (auto Dir : Paths) {
93 SmallString<128> Path = Dir;
94 sys::path::append(Path, File);
95 if (sys::fs::exists(Path))
96 return Path.str().str();
97 }
98 return Optional();
99 }
100
101 int llvm::libDriverMain(llvm::ArrayRef ArgsArr) {
102 SmallVector NewArgs(ArgsArr.begin(), ArgsArr.end());
103 BumpPtrAllocator Alloc;
104 StringSaver Saver(Alloc);
105 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
106 ArgsArr = NewArgs;
107
108 LibOptTable Table;
109 unsigned MissingIndex;
110 unsigned MissingCount;
111 llvm::opt::InputArgList Args =
112 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
113 if (MissingCount) {
114 llvm::errs() << "missing arg value for \""
115 << Args.getArgString(MissingIndex) << "\", expected "
116 << MissingCount
117 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
118 return 1;
119 }
120 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
121 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
122
123 if (!Args.hasArgNoClaim(OPT_INPUT)) {
124 // No input files. To match lib.exe, silently do nothing.
125 return 0;
126 }
127
128 std::vector SearchPaths = getSearchPaths(&Args, Saver);
129
130 std::vector Members;
131 for (auto *Arg : Args.filtered(OPT_INPUT)) {
132 Optional Path = findInputFile(Arg->getValue(), SearchPaths);
133 if (!Path.hasValue()) {
134 llvm::errs() << Arg->getValue() << ": no such file or directory\n";
135 return 1;
136 }
137 Expected MOrErr =
138 NewArchiveMember::getFile(Saver.save(*Path), /*Deterministic=*/true);
139 if (!MOrErr) {
140 handleAllErrors(MOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
141 llvm::errs() << Arg->getValue() << ": " << EIB.message() << "\n";
142 });
143 return 1;
144 }
145 sys::fs::file_magic Magic =
146 sys::fs::identify_magic(MOrErr->Buf->getBuffer());
147 if (Magic != sys::fs::file_magic::coff_object &&
148 Magic != sys::fs::file_magic::bitcode &&
149 Magic != sys::fs::file_magic::windows_resource) {
150 llvm::errs() << Arg->getValue()
151 << ": not a COFF object, bitcode or resource file\n";
152 return 1;
153 }
154 Members.emplace_back(std::move(*MOrErr));
155 }
156
157 std::pair Result =
158 llvm::writeArchive(getOutputPath(&Args, Members[0]), Members,
159 /*WriteSymtab=*/true, object::Archive::K_GNU,
160 /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin));
161
162 if (Result.second) {
163 if (Result.first.empty())
164 Result.first = ArgsArr[0];
165 llvm::errs() << Result.first << ": " << Result.second.message() << "\n";
166 return 1;
167 }
168
169 return 0;
170 }
+0
-25
lib/LibDriver/Options.td less more
None include "llvm/Option/OptParser.td"
1
2 // lib.exe accepts options starting with either a dash or a slash.
3
4 // Flag that takes no arguments.
5 class F : Flag<["/", "-", "-?"], name>;
6
7 // Flag that takes one argument after ":".
8 class P :
9 Joined<["/", "-", "-?"], name#":">, HelpText;
10
11 def libpath: P<"libpath", "Object file search path">;
12 def out : P<"out", "Path to file to write output">;
13
14 def llvmlibthin : F<"llvmlibthin">;
15
16 //==============================================================================
17 // The flags below do nothing. They are defined only for lib.exe compatibility.
18 //==============================================================================
19
20 class QF : Joined<["/", "-", "-?"], name#":">;
21
22 def ignore : QF<"ignore">;
23 def machine: QF<"machine">;
24 def nologo : F<"nologo">;
0 set(LLVM_TARGET_DEFINITIONS Options.td)
1 tablegen(LLVM Options.inc -gen-opt-parser-defs)
2 add_public_tablegen_target(LibOptionsTableGen)
3
4 add_llvm_library(LLVMLibDriver
5 LibDriver.cpp
6 )
7 add_dependencies(LLVMLibDriver LibOptionsTableGen)
0 ;===- ./lib/LibDriver/LLVMBuild.txt ----------------------------*- Conf -*--===;
1 ;
2 ; The LLVM Compiler Infrastructure
3 ;
4 ; This file is distributed under the University of Illinois Open Source
5 ; License. See LICENSE.TXT for details.
6 ;
7 ;===------------------------------------------------------------------------===;
8 ;
9 ; This is an LLVMBuild description file for the components in this subdirectory.
10 ;
11 ; For more information on the LLVMBuild system, please see:
12 ;
13 ; http://llvm.org/docs/LLVMBuild.html
14 ;
15 ;===------------------------------------------------------------------------===;
16
17 [component_0]
18 type = Library
19 name = LibDriver
20 parent = Libraries
21 required_libraries = Object Option Support
0 //===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines an interface to a lib.exe-compatible driver that also understands
10 // bitcode files. Used by llvm-lib and lld-link /lib.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Object/ArchiveWriter.h"
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Option/Option.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/StringSaver.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/Process.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 namespace {
29
30 enum {
31 OPT_INVALID = 0,
32 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
33 #include "Options.inc"
34 #undef OPTION
35 };
36
37 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
38 #include "Options.inc"
39 #undef PREFIX
40
41 static const llvm::opt::OptTable::Info infoTable[] = {
42 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
43 { \
44 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
45 OPT_##GROUP, OPT_##ALIAS, X6 \
46 },
47 #include "Options.inc"
48 #undef OPTION
49 };
50
51 class LibOptTable : public llvm::opt::OptTable {
52 public:
53 LibOptTable() : OptTable(infoTable, true) {}
54 };
55
56 }
57
58 static std::string getOutputPath(llvm::opt::InputArgList *Args,
59 const llvm::NewArchiveMember &FirstMember) {
60 if (auto *Arg = Args->getLastArg(OPT_out))
61 return Arg->getValue();
62 SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
63 llvm::sys::path::replace_extension(Val, ".lib");
64 return Val.str();
65 }
66
67 static std::vector getSearchPaths(llvm::opt::InputArgList *Args,
68 StringSaver &Saver) {
69 std::vector Ret;
70 // Add current directory as first item of the search path.
71 Ret.push_back("");
72
73 // Add /libpath flags.
74 for (auto *Arg : Args->filtered(OPT_libpath))
75 Ret.push_back(Arg->getValue());
76
77 // Add $LIB.
78 Optional EnvOpt = sys::Process::GetEnv("LIB");
79 if (!EnvOpt.hasValue())
80 return Ret;
81 StringRef Env = Saver.save(*EnvOpt);
82 while (!Env.empty()) {
83 StringRef Path;
84 std::tie(Path, Env) = Env.split(';');
85 Ret.push_back(Path);
86 }
87 return Ret;
88 }
89
90 static Optional findInputFile(StringRef File,
91 ArrayRef Paths) {
92 for (auto Dir : Paths) {
93 SmallString<128> Path = Dir;
94 sys::path::append(Path, File);
95 if (sys::fs::exists(Path))
96 return Path.str().str();
97 }
98 return Optional();
99 }
100
101 int llvm::libDriverMain(llvm::ArrayRef ArgsArr) {
102 SmallVector NewArgs(ArgsArr.begin(), ArgsArr.end());
103 BumpPtrAllocator Alloc;
104 StringSaver Saver(Alloc);
105 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
106 ArgsArr = NewArgs;
107
108 LibOptTable Table;
109 unsigned MissingIndex;
110 unsigned MissingCount;
111 llvm::opt::InputArgList Args =
112 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
113 if (MissingCount) {
114 llvm::errs() << "missing arg value for \""
115 << Args.getArgString(MissingIndex) << "\", expected "
116 << MissingCount
117 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
118 return 1;
119 }
120 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
121 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
122
123 if (!Args.hasArgNoClaim(OPT_INPUT)) {
124 // No input files. To match lib.exe, silently do nothing.
125 return 0;
126 }
127
128 std::vector SearchPaths = getSearchPaths(&Args, Saver);
129
130 std::vector Members;
131 for (auto *Arg : Args.filtered(OPT_INPUT)) {
132 Optional Path = findInputFile(Arg->getValue(), SearchPaths);
133 if (!Path.hasValue()) {
134 llvm::errs() << Arg->getValue() << ": no such file or directory\n";
135 return 1;
136 }
137 Expected MOrErr =
138 NewArchiveMember::getFile(Saver.save(*Path), /*Deterministic=*/true);
139 if (!MOrErr) {
140 handleAllErrors(MOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
141 llvm::errs() << Arg->getValue() << ": " << EIB.message() << "\n";
142 });
143 return 1;
144 }
145 sys::fs::file_magic Magic =
146 sys::fs::identify_magic(MOrErr->Buf->getBuffer());
147 if (Magic != sys::fs::file_magic::coff_object &&
148 Magic != sys::fs::file_magic::bitcode &&
149 Magic != sys::fs::file_magic::windows_resource) {
150 llvm::errs() << Arg->getValue()
151 << ": not a COFF object, bitcode or resource file\n";
152 return 1;
153 }
154 Members.emplace_back(std::move(*MOrErr));
155 }
156
157 std::pair Result =
158 llvm::writeArchive(getOutputPath(&Args, Members[0]), Members,
159 /*WriteSymtab=*/true, object::Archive::K_GNU,
160 /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin));
161
162 if (Result.second) {
163 if (Result.first.empty())
164 Result.first = ArgsArr[0];
165 llvm::errs() << Result.first << ": " << Result.second.message() << "\n";
166 return 1;
167 }
168
169 return 0;
170 }
0 include "llvm/Option/OptParser.td"
1
2 // lib.exe accepts options starting with either a dash or a slash.
3
4 // Flag that takes no arguments.
5 class F : Flag<["/", "-", "-?"], name>;
6
7 // Flag that takes one argument after ":".
8 class P :
9 Joined<["/", "-", "-?"], name#":">, HelpText;
10
11 def libpath: P<"libpath", "Object file search path">;
12 def out : P<"out", "Path to file to write output">;
13
14 def llvmlibthin : F<"llvmlibthin">;
15
16 //==============================================================================
17 // The flags below do nothing. They are defined only for lib.exe compatibility.
18 //==============================================================================
19
20 class QF : Joined<["/", "-", "-?"], name#":">;
21
22 def ignore : QF<"ignore">;
23 def machine: QF<"machine">;
24 def nologo : F<"nologo">;
1515 #include "llvm/ADT/Triple.h"
1616 #include "llvm/IR/LLVMContext.h"
1717 #include "llvm/IR/Module.h"
18 #include "llvm/LibDriver/LibDriver.h"
18 #include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
1919 #include "llvm/Object/Archive.h"
2020 #include "llvm/Object/ArchiveWriter.h"
2121 #include "llvm/Object/MachO.h"