blob: d636dca7a2c7baaa7b02de9d241e5e537f4dd859 [file] [log] [blame]
Peter Collingbournebc051632015-06-09 21:50:22 +00001//===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Defines an interface to a lib.exe-compatible driver that also understands
Peter Collingbourne327ce8f2015-08-06 19:00:42 +000011// bitcode files. Used by llvm-lib and lld-link /lib.
Peter Collingbournebc051632015-06-09 21:50:22 +000012//
13//===----------------------------------------------------------------------===//
14
Peter Collingbournec6f07c42017-05-13 22:06:46 +000015#include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
Peter Collingbournebc051632015-06-09 21:50:22 +000016#include "llvm/ADT/STLExtras.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/Magic.h"
Peter Collingbournebc051632015-06-09 21:50:22 +000018#include "llvm/Object/ArchiveWriter.h"
19#include "llvm/Option/Arg.h"
20#include "llvm/Option/ArgList.h"
21#include "llvm/Option/Option.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Path.h"
Peter Collingbourne70708272015-06-20 00:57:12 +000024#include "llvm/Support/Process.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/Support/StringSaver.h"
Peter Collingbournebc051632015-06-09 21:50:22 +000026#include "llvm/Support/raw_ostream.h"
27
28using namespace llvm;
29
30namespace {
31
32enum {
33 OPT_INVALID = 0,
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000034#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
Peter Collingbournebc051632015-06-09 21:50:22 +000035#include "Options.inc"
36#undef OPTION
37};
38
39#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
40#include "Options.inc"
41#undef PREFIX
42
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000043static const opt::OptTable::Info InfoTable[] = {
Martell Malonecc82cdf2017-08-23 02:10:28 +000044#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000045 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \
Martell Malonecc82cdf2017-08-23 02:10:28 +000046 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
Peter Collingbournebc051632015-06-09 21:50:22 +000047#include "Options.inc"
48#undef OPTION
49};
50
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000051class LibOptTable : public opt::OptTable {
Peter Collingbournebc051632015-06-09 21:50:22 +000052public:
Martell Malonecc82cdf2017-08-23 02:10:28 +000053 LibOptTable() : OptTable(InfoTable, true) {}
Peter Collingbournebc051632015-06-09 21:50:22 +000054};
55
Alexander Kornienkof00654e2015-06-23 09:49:53 +000056}
Peter Collingbournebc051632015-06-09 21:50:22 +000057
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000058static std::string getOutputPath(opt::InputArgList *Args,
59 const NewArchiveMember &FirstMember) {
Peter Collingbournebc051632015-06-09 21:50:22 +000060 if (auto *Arg = Args->getLastArg(OPT_out))
61 return Arg->getValue();
Peter Collingbourne8ec68fa2016-06-29 22:27:42 +000062 SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000063 sys::path::replace_extension(Val, ".lib");
Peter Collingbourne7a544f72015-07-08 19:00:46 +000064 return Val.str();
Peter Collingbournebc051632015-06-09 21:50:22 +000065}
66
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000067static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args,
Peter Collingbourne70708272015-06-20 00:57:12 +000068 StringSaver &Saver) {
69 std::vector<StringRef> 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<std::string> 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
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000090static std::string findInputFile(StringRef File, ArrayRef<StringRef> Paths) {
91 for (StringRef Dir : Paths) {
Peter Collingbourne70708272015-06-20 00:57:12 +000092 SmallString<128> Path = Dir;
93 sys::path::append(Path, File);
94 if (sys::fs::exists(Path))
95 return Path.str().str();
96 }
Rui Ueyamaf2f4e032017-09-06 22:05:32 +000097 return "";
Peter Collingbourne70708272015-06-20 00:57:12 +000098}
99
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000100int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
Rafael Espindola454adf62015-06-13 12:49:52 +0000101 BumpPtrAllocator Alloc;
Rafael Espindolab82455d2015-08-13 01:07:02 +0000102 StringSaver Saver(Alloc);
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000103
104 // Parse command line arguments.
105 SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
David Blaikie8b31d412015-06-21 06:31:56 +0000106 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
107 ArgsArr = NewArgs;
Peter Collingbournebc051632015-06-09 21:50:22 +0000108
109 LibOptTable Table;
110 unsigned MissingIndex;
111 unsigned MissingCount;
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000112 opt::InputArgList Args =
David Blaikiedb3d31d2015-06-22 22:06:37 +0000113 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
Peter Collingbournebc051632015-06-09 21:50:22 +0000114 if (MissingCount) {
115 llvm::errs() << "missing arg value for \""
David Blaikiedb3d31d2015-06-22 22:06:37 +0000116 << Args.getArgString(MissingIndex) << "\", expected "
117 << MissingCount
Peter Collingbournebc051632015-06-09 21:50:22 +0000118 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
119 return 1;
120 }
David Blaikiedb3d31d2015-06-22 22:06:37 +0000121 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
Peter Collingbournebc051632015-06-09 21:50:22 +0000122 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
123
Nico Weber17172c62018-07-14 02:29:44 +0000124 // Handle /help
125 if (Args.hasArg(OPT_help)) {
126 Table.PrintHelp(outs(), ArgsArr[0], "LLVM Lib");
127 return 0;
128 }
129
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000130 // If no input files, silently do nothing to match lib.exe.
131 if (!Args.hasArgNoClaim(OPT_INPUT))
Peter Collingbourne5413f6f2016-04-13 19:36:04 +0000132 return 0;
Peter Collingbournebc051632015-06-09 21:50:22 +0000133
David Blaikiedb3d31d2015-06-22 22:06:37 +0000134 std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
Peter Collingbourne70708272015-06-20 00:57:12 +0000135
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000136 // Create a NewArchiveMember for each input file.
137 std::vector<NewArchiveMember> Members;
David Blaikiedb3d31d2015-06-22 22:06:37 +0000138 for (auto *Arg : Args.filtered(OPT_INPUT)) {
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000139 std::string Path = findInputFile(Arg->getValue(), SearchPaths);
140 if (Path.empty()) {
Peter Collingbourne70708272015-06-20 00:57:12 +0000141 llvm::errs() << Arg->getValue() << ": no such file or directory\n";
142 return 1;
143 }
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000144
Peter Collingbourne8ec68fa2016-06-29 22:27:42 +0000145 Expected<NewArchiveMember> MOrErr =
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000146 NewArchiveMember::getFile(Saver.save(Path), /*Deterministic=*/true);
Peter Collingbourne8ec68fa2016-06-29 22:27:42 +0000147 if (!MOrErr) {
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000148 handleAllErrors(MOrErr.takeError(), [&](const ErrorInfoBase &EIB) {
Peter Collingbourne8ec68fa2016-06-29 22:27:42 +0000149 llvm::errs() << Arg->getValue() << ": " << EIB.message() << "\n";
150 });
151 return 1;
152 }
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000153
154 file_magic Magic = identify_magic(MOrErr->Buf->getBuffer());
155 if (Magic != file_magic::coff_object && Magic != file_magic::bitcode &&
156 Magic != file_magic::windows_resource) {
Peter Collingbournee0895542016-12-15 19:37:46 +0000157 llvm::errs() << Arg->getValue()
158 << ": not a COFF object, bitcode or resource file\n";
Peter Collingbourneb677fe02016-12-14 22:19:22 +0000159 return 1;
160 }
Peter Collingbourne8ec68fa2016-06-29 22:27:42 +0000161 Members.emplace_back(std::move(*MOrErr));
Peter Collingbourne70708272015-06-20 00:57:12 +0000162 }
Peter Collingbournebc051632015-06-09 21:50:22 +0000163
Rui Ueyamaf2f4e032017-09-06 22:05:32 +0000164 // Create an archive file.
Rui Ueyama01d02652017-08-30 22:11:03 +0000165 std::string OutputPath = getOutputPath(&Args, Members[0]);
Rafael Espindola25cbdf22017-09-21 23:13:36 +0000166 if (Error E =
167 writeArchive(OutputPath, Members,
168 /*WriteSymtab=*/true, object::Archive::K_GNU,
169 /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) {
170 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
171 llvm::errs() << OutputPath << ": " << EI.message() << "\n";
172 });
Peter Collingbournebc051632015-06-09 21:50:22 +0000173 return 1;
174 }
175
176 return 0;
177}