-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
driver.cpp
217 lines (184 loc) · 7.5 KB
/
driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//===--- driver.cpp - Swift Compiler Driver -------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This is the entry point to the swift compiler driver.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/Basic/Program.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/FrontendUtil.h"
#include "swift/Driver/Job.h"
#include "swift/Driver/ToolChain.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/FrontendTool/FrontendTool.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <stdlib.h>
using namespace swift;
using namespace swift::driver;
std::string getExecutablePath(const char *FirstArg) {
void *P = (void *)(intptr_t)getExecutablePath;
return llvm::sys::fs::getMainExecutable(FirstArg, P);
}
/// Run 'swift-autolink-extract'.
extern int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr);
extern int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr);
/// Run 'swift-format'
extern int swift_format_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr);
/// Determine if the given invocation should run as a subcommand.
///
/// \param ExecName The name of the argv[0] we were invoked as.
/// \param SubcommandName On success, the full name of the subcommand to invoke.
/// \param Args On return, the adjusted program arguments to use.
/// \returns True if running as a subcommand.
static bool shouldRunAsSubcommand(StringRef ExecName,
SmallString<256> &SubcommandName,
SmallVectorImpl<const char *> &Args) {
assert(Args.size() > 0);
// If we are not run as 'swift', don't do anything special. This doesn't work
// with symlinks with alternate names, but we can't detect 'swift' vs 'swiftc'
// if we try and resolve using the actual executable path.
if (ExecName != "swift")
return false;
// If there are no program arguments, always invoke as normal.
if (Args.size() == 1)
return false;
// Otherwise, we have a program argument. If it looks like an option or a
// path, then invoke in interactive mode with the arguments as given.
StringRef FirstArg(Args[1]);
if (FirstArg.startswith("-") || FirstArg.find('.') != StringRef::npos ||
FirstArg.find('/') != StringRef::npos)
return false;
// Otherwise, we should have some sort of subcommand. Get the subcommand name
// and remove it from the program arguments.
StringRef Subcommand = Args[1];
Args.erase(&Args[1]);
// If the subcommand is the "built-in" 'repl', then use the
// normal driver.
if (Subcommand == "repl")
return false;
// Form the subcommand name.
SubcommandName.assign("swift-");
SubcommandName.append(Subcommand);
return true;
}
extern int apinotes_main(ArrayRef<const char *> Args);
int main(int argc_, const char **argv_) {
INITIALIZE_LLVM(argc_, argv_);
SmallVector<const char *, 256> argv;
llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
std::error_code EC = llvm::sys::Process::GetArgumentVector(
argv, llvm::ArrayRef<const char *>(argv_, argc_), ArgAllocator);
if (EC) {
llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
return 1;
}
// Check if this invocation should execute a subcommand.
StringRef ExecName = llvm::sys::path::stem(argv[0]);
SmallString<256> SubcommandName;
if (shouldRunAsSubcommand(ExecName, SubcommandName, argv)) {
// We are running as a subcommand, try to find the subcommand adjacent to
// the executable we are running as.
SmallString<256> SubcommandPath(
llvm::sys::path::parent_path(getExecutablePath(argv[0])));
llvm::sys::path::append(SubcommandPath, SubcommandName);
// If we didn't find the tool there, let the OS search for it.
if (!llvm::sys::fs::exists(SubcommandPath)) {
// Search for the program and use the path if found. If there was an
// error, ignore it and just let the exec fail.
auto result = llvm::sys::findProgramByName(SubcommandName);
if (!result.getError())
SubcommandPath = *result;
}
// Rewrite the program argument.
argv[0] = SubcommandPath.c_str();
// Execute the subcommand.
argv.push_back(nullptr);
ExecuteInPlace(SubcommandPath.c_str(), argv.data());
// If we reach here then an error occurred (typically a missing path).
std::string ErrorString = llvm::sys::StrError();
llvm::errs() << "error: unable to invoke subcommand: " << argv[0]
<< " (" << ErrorString << ")\n";
return 2;
}
// Handle integrated tools.
if (argv.size() > 1) {
StringRef FirstArg(argv[1]);
if (FirstArg == "-frontend") {
return performFrontend(llvm::makeArrayRef(argv.data()+2,
argv.data()+argv.size()),
argv[0], (void *)(intptr_t)getExecutablePath);
}
if (FirstArg == "-modulewrap") {
return modulewrap_main(llvm::makeArrayRef(argv.data()+2,
argv.data()+argv.size()),
argv[0], (void *)(intptr_t)getExecutablePath);
}
if (FirstArg == "-apinotes") {
return apinotes_main(llvm::makeArrayRef(argv.data()+1,
argv.data()+argv.size()));
}
}
std::string Path = getExecutablePath(argv[0]);
PrintingDiagnosticConsumer PDC;
SourceManager SM;
DiagnosticEngine Diags(SM);
Diags.addConsumer(PDC);
Driver TheDriver(Path, ExecName, argv, Diags);
switch (TheDriver.getDriverKind()) {
case Driver::DriverKind::AutolinkExtract:
return autolink_extract_main(
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv),
argv[0], (void *)(intptr_t)getExecutablePath);
case Driver::DriverKind::SwiftFormat:
return swift_format_main(
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv),
argv[0], (void *)(intptr_t)getExecutablePath);
default:
break;
}
std::unique_ptr<llvm::opt::InputArgList> ArgList =
TheDriver.parseArgStrings(ArrayRef<const char*>(argv).slice(1));
if (Diags.hadAnyError())
return 1;
std::unique_ptr<ToolChain> TC = TheDriver.buildToolChain(*ArgList);
if (Diags.hadAnyError())
return 1;
std::unique_ptr<Compilation> C =
TheDriver.buildCompilation(*TC, std::move(ArgList));
if (Diags.hadAnyError())
return 1;
if (C) {
return C->performJobs();
}
return 0;
}