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
|
//===--- modulewrap_main.cpp - module wrapping utility --------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Wraps .swiftmodule files inside an object file container so they
// can be passed to the linker directly. Mostly useful for platforms
// where the debug info typically stays in the executable.
// (ie. ELF-based platforms).
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/Option/Options.h"
#include "swift/Serialization/Validation.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/TypeLowering.h"
#include "swift/Subsystems.h"
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/Bitstream/BitstreamReader.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/VirtualOutputBackends.h"
using namespace llvm::opt;
using namespace swift;
class ModuleWrapInvocation {
private:
std::string MainExecutablePath;
std::string OutputFilename = "-";
llvm::Triple TargetTriple;
std::vector<std::string> InputFilenames;
bool UseSharedResourceFolder = true;
bool EnableObjCInterop = true;
public:
bool hasSingleInput() const { return InputFilenames.size() == 1; }
const std::string &getFilenameOfFirstInput() const {
return InputFilenames[0];
}
void setMainExecutablePath(const std::string &Path) {
MainExecutablePath = Path;
}
const std::string &getOutputFilename() { return OutputFilename; }
const std::vector<std::string> &getInputFilenames() { return InputFilenames; }
llvm::Triple &getTargetTriple() { return TargetTriple; }
bool useSharedResourceFolder() { return UseSharedResourceFolder; }
bool enableObjCInterop() { return EnableObjCInterop; }
int parseArgs(llvm::ArrayRef<const char *> Args, DiagnosticEngine &Diags) {
using namespace options;
// Parse frontend command line options using Swift's option table.
std::unique_ptr<llvm::opt::OptTable> Table = createSwiftOptTable();
unsigned MissingIndex;
unsigned MissingCount;
llvm::opt::InputArgList ParsedArgs =
Table->ParseArgs(Args, MissingIndex, MissingCount,
ModuleWrapOption);
if (MissingCount) {
Diags.diagnose(SourceLoc(), diag::error_missing_arg_value,
ParsedArgs.getArgString(MissingIndex), MissingCount);
return 1;
}
if (const Arg *A = ParsedArgs.getLastArg(options::OPT_target))
TargetTriple = llvm::Triple(llvm::Triple::normalize(A->getValue()));
else
TargetTriple = llvm::Triple(llvm::sys::getDefaultTargetTriple());
if (ParsedArgs.hasArg(OPT_UNKNOWN)) {
for (const Arg *A : ParsedArgs.filtered(OPT_UNKNOWN)) {
Diags.diagnose(SourceLoc(), diag::error_unknown_arg,
A->getAsString(ParsedArgs));
}
return true;
}
if (ParsedArgs.getLastArg(OPT_help)) {
std::string ExecutableName =
llvm::sys::path::stem(MainExecutablePath).str();
Table->printHelp(llvm::outs(), ExecutableName.c_str(),
"Swift Module Wrapper", options::ModuleWrapOption, 0,
/*ShowAllAliases*/false);
return 1;
}
for (const Arg *A : ParsedArgs.filtered(OPT_INPUT)) {
InputFilenames.push_back(A->getValue());
}
if (InputFilenames.empty()) {
Diags.diagnose(SourceLoc(), diag::error_mode_requires_an_input_file);
return 1;
}
if (const Arg *A = ParsedArgs.getLastArg(OPT_o)) {
OutputFilename = A->getValue();
}
if (ParsedArgs.hasFlag(OPT_static_executable, OPT_no_static_executable,
false) ||
ParsedArgs.hasFlag(OPT_static_stdlib, OPT_no_static_stdlib, false) ||
ParsedArgs.hasArg(OPT_static)) {
UseSharedResourceFolder = false;
}
EnableObjCInterop = ParsedArgs.hasFlag(OPT_enable_objc_interop,
OPT_disable_objc_interop, TargetTriple.isOSDarwin());
return 0;
}
};
int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
void *MainAddr) {
INITIALIZE_LLVM();
CompilerInstance Instance;
PrintingDiagnosticConsumer PDC;
Instance.addDiagnosticConsumer(&PDC);
ModuleWrapInvocation Invocation;
std::string MainExecutablePath =
llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
Invocation.setMainExecutablePath(MainExecutablePath);
// Parse arguments.
if (Invocation.parseArgs(Args, Instance.getDiags()) != 0) {
return 1;
}
if (!Invocation.hasSingleInput()) {
Instance.getDiags().diagnose(SourceLoc(),
diag::error_mode_requires_one_input_file);
return 1;
}
StringRef Filename = Invocation.getFilenameOfFirstInput();
auto ErrOrBuf = llvm::MemoryBuffer::getFile(Filename);
if (!ErrOrBuf) {
Instance.getDiags().diagnose(
SourceLoc(), diag::error_no_such_file_or_directory, Filename);
return 1;
}
// Superficially verify that the input is a swift module file.
if (!serialization::isSerializedAST(ErrOrBuf.get()->getBuffer())) {
Instance.getDiags().diagnose(SourceLoc(), diag::error_parse_input_file,
Filename, "signature mismatch");
return 1;
}
// Wrap the bitstream in a module object file. To use the ClangImporter to
// create the module loader, we need to properly set the runtime library path.
SearchPathOptions SearchPathOpts;
SmallString<128> RuntimeResourcePath;
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
MainExecutablePath, Invocation.useSharedResourceFolder(),
RuntimeResourcePath);
SearchPathOpts.RuntimeResourcePath = std::string(RuntimeResourcePath.str());
SourceManager SrcMgr;
SILOptions SILOpts;
TypeCheckerOptions TypeCheckOpts;
LangOptions LangOpts;
ClangImporterOptions ClangImporterOpts;
symbolgraphgen::SymbolGraphOptions SymbolGraphOpts;
CASOptions CASOpts;
LangOpts.Target = Invocation.getTargetTriple();
LangOpts.EnableObjCInterop = Invocation.enableObjCInterop();
ASTContext &ASTCtx = *ASTContext::get(
LangOpts, TypeCheckOpts, SILOpts, SearchPathOpts, ClangImporterOpts,
SymbolGraphOpts, CASOpts, SrcMgr, Instance.getDiags(),
llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>());
registerParseRequestFunctions(ASTCtx.evaluator);
registerTypeCheckerRequestFunctions(ASTCtx.evaluator);
ASTCtx.addModuleLoader(ClangImporter::create(ASTCtx, ""), true);
ModuleDecl *M = ModuleDecl::create(ASTCtx.getIdentifier("swiftmodule"), ASTCtx);
std::unique_ptr<Lowering::TypeConverter> TC(
new Lowering::TypeConverter(*M, ASTCtx.SILOpts.EnableSILOpaqueValues));
std::unique_ptr<SILModule> SM = SILModule::createEmptyModule(M, *TC, SILOpts);
createSwiftModuleObjectFile(*SM, (*ErrOrBuf)->getBuffer(),
Invocation.getOutputFilename());
return 0;
}
|