forked from OSchip/llvm-project
222 lines
8.0 KiB
C++
222 lines
8.0 KiB
C++
//===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "WebAssembly.h"
|
|
#include "CommonArgs.h"
|
|
#include "clang/Driver/Compilation.h"
|
|
#include "clang/Driver/Driver.h"
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
#include "clang/Driver/Options.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/Path.h"
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
using namespace clang::driver;
|
|
using namespace clang::driver::tools;
|
|
using namespace clang::driver::toolchains;
|
|
using namespace clang;
|
|
using namespace llvm::opt;
|
|
|
|
/// Following the conventions in https://wiki.debian.org/Multiarch/Tuples,
|
|
/// we remove the vendor field to form the multiarch triple.
|
|
static std::string getMultiarchTriple(const Driver &D,
|
|
const llvm::Triple &TargetTriple,
|
|
StringRef SysRoot) {
|
|
return (TargetTriple.getArchName() + "-" +
|
|
TargetTriple.getOSAndEnvironmentName()).str();
|
|
}
|
|
|
|
std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
|
|
const ToolChain &ToolChain = getToolChain();
|
|
if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
|
|
StringRef UseLinker = A->getValue();
|
|
if (!UseLinker.empty()) {
|
|
if (llvm::sys::path::is_absolute(UseLinker) &&
|
|
llvm::sys::fs::can_execute(UseLinker))
|
|
return UseLinker;
|
|
|
|
// Accept 'lld', and 'ld' as aliases for the default linker
|
|
if (UseLinker != "lld" && UseLinker != "ld")
|
|
ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
|
|
<< A->getAsString(Args);
|
|
}
|
|
}
|
|
|
|
return ToolChain.GetProgramPath(ToolChain.getDefaultLinker());
|
|
}
|
|
|
|
void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
|
const InputInfo &Output,
|
|
const InputInfoList &Inputs,
|
|
const ArgList &Args,
|
|
const char *LinkingOutput) const {
|
|
|
|
const ToolChain &ToolChain = getToolChain();
|
|
const char *Linker = Args.MakeArgString(getLinkerPath(Args));
|
|
ArgStringList CmdArgs;
|
|
|
|
if (Args.hasArg(options::OPT_s))
|
|
CmdArgs.push_back("--strip-all");
|
|
|
|
Args.AddAllArgs(CmdArgs, options::OPT_L);
|
|
Args.AddAllArgs(CmdArgs, options::OPT_u);
|
|
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
|
|
|
|
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
|
|
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
|
|
|
|
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
|
|
|
|
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
|
if (ToolChain.ShouldLinkCXXStdlib(Args))
|
|
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
|
|
|
if (Args.hasArg(options::OPT_pthread)) {
|
|
CmdArgs.push_back("-lpthread");
|
|
CmdArgs.push_back("--shared-memory");
|
|
}
|
|
|
|
CmdArgs.push_back("-lc");
|
|
AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
|
|
}
|
|
|
|
CmdArgs.push_back("-o");
|
|
CmdArgs.push_back(Output.getFilename());
|
|
|
|
C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs));
|
|
}
|
|
|
|
WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
|
|
const llvm::opt::ArgList &Args)
|
|
: ToolChain(D, Triple, Args) {
|
|
|
|
assert(Triple.isArch32Bit() != Triple.isArch64Bit());
|
|
|
|
getProgramPaths().push_back(getDriver().getInstalledDir());
|
|
|
|
if (getTriple().getOS() == llvm::Triple::UnknownOS) {
|
|
// Theoretically an "unknown" OS should mean no standard libraries, however
|
|
// it could also mean that a custom set of libraries is in use, so just add
|
|
// /lib to the search path. Disable multiarch in this case, to discourage
|
|
// paths containing "unknown" from acquiring meanings.
|
|
getFilePaths().push_back(getDriver().SysRoot + "/lib");
|
|
} else {
|
|
const std::string MultiarchTriple =
|
|
getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
|
|
getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
|
|
}
|
|
}
|
|
|
|
bool WebAssembly::IsMathErrnoDefault() const { return false; }
|
|
|
|
bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
|
|
|
|
bool WebAssembly::UseObjCMixedDispatch() const { return true; }
|
|
|
|
bool WebAssembly::isPICDefault() const { return false; }
|
|
|
|
bool WebAssembly::isPIEDefault() const { return false; }
|
|
|
|
bool WebAssembly::isPICDefaultForced() const { return false; }
|
|
|
|
bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; }
|
|
|
|
bool WebAssembly::hasBlocksRuntime() const { return false; }
|
|
|
|
// TODO: Support profiling.
|
|
bool WebAssembly::SupportsProfiling() const { return false; }
|
|
|
|
bool WebAssembly::HasNativeLLVMSupport() const { return true; }
|
|
|
|
void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
|
|
ArgStringList &CC1Args,
|
|
Action::OffloadKind) const {
|
|
if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array,
|
|
options::OPT_fno_use_init_array, true))
|
|
CC1Args.push_back("-fuse-init-array");
|
|
|
|
// '-pthread' implies '-target-feature +atomics'
|
|
if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
|
|
false)) {
|
|
if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
|
|
false))
|
|
getDriver().Diag(diag::err_drv_argument_not_allowed_with)
|
|
<< "-pthread"
|
|
<< "-mno-atomics";
|
|
CC1Args.push_back("-target-feature");
|
|
CC1Args.push_back("+atomics");
|
|
}
|
|
}
|
|
|
|
ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const {
|
|
return ToolChain::RLT_CompilerRT;
|
|
}
|
|
|
|
ToolChain::CXXStdlibType
|
|
WebAssembly::GetCXXStdlibType(const ArgList &Args) const {
|
|
if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
|
|
StringRef Value = A->getValue();
|
|
if (Value != "libc++")
|
|
getDriver().Diag(diag::err_drv_invalid_stdlib_name)
|
|
<< A->getAsString(Args);
|
|
}
|
|
return ToolChain::CST_Libcxx;
|
|
}
|
|
|
|
void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
|
|
ArgStringList &CC1Args) const {
|
|
if (!DriverArgs.hasArg(options::OPT_nostdinc)) {
|
|
if (getTriple().getOS() != llvm::Triple::UnknownOS) {
|
|
const std::string MultiarchTriple =
|
|
getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
|
|
addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include/" + MultiarchTriple);
|
|
}
|
|
addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
|
|
}
|
|
}
|
|
|
|
void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
|
|
ArgStringList &CC1Args) const {
|
|
if (!DriverArgs.hasArg(options::OPT_nostdlibinc) &&
|
|
!DriverArgs.hasArg(options::OPT_nostdincxx)) {
|
|
if (getTriple().getOS() != llvm::Triple::UnknownOS) {
|
|
const std::string MultiarchTriple =
|
|
getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot);
|
|
addSystemInclude(DriverArgs, CC1Args,
|
|
getDriver().SysRoot + "/include/" + MultiarchTriple + "/c++/v1");
|
|
}
|
|
addSystemInclude(DriverArgs, CC1Args,
|
|
getDriver().SysRoot + "/include/c++/v1");
|
|
}
|
|
}
|
|
|
|
void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
|
|
llvm::opt::ArgStringList &CmdArgs) const {
|
|
|
|
switch (GetCXXStdlibType(Args)) {
|
|
case ToolChain::CST_Libcxx:
|
|
CmdArgs.push_back("-lc++");
|
|
CmdArgs.push_back("-lc++abi");
|
|
break;
|
|
case ToolChain::CST_Libstdcxx:
|
|
llvm_unreachable("invalid stdlib name");
|
|
}
|
|
}
|
|
|
|
SanitizerMask WebAssembly::getSupportedSanitizers() const {
|
|
SanitizerMask Res = ToolChain::getSupportedSanitizers();
|
|
if (getTriple().isOSEmscripten()) {
|
|
Res |= SanitizerKind::Vptr;
|
|
}
|
|
return Res;
|
|
}
|
|
|
|
Tool *WebAssembly::buildLinker() const {
|
|
return new tools::wasm::Linker(*this);
|
|
}
|