David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1 | //===--- WebAssembly.cpp - WebAssembly ToolChain Implementation -*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "WebAssembly.h" |
| 10 | #include "CommonArgs.h" |
Sam Clegg | 818dd86 | 2019-06-13 09:42:43 +0000 | [diff] [blame^] | 11 | #include "clang/Config/config.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Compilation.h" |
| 13 | #include "clang/Driver/Driver.h" |
Sam Clegg | ffbfc0f | 2018-01-12 17:54:49 +0000 | [diff] [blame] | 14 | #include "clang/Driver/DriverDiagnostic.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 15 | #include "clang/Driver/Options.h" |
Sam Clegg | fc5ddee | 2019-03-28 17:45:18 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FileSystem.h" |
| 17 | #include "llvm/Support/Path.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 18 | #include "llvm/Option/ArgList.h" |
| 19 | |
| 20 | using namespace clang::driver; |
| 21 | using namespace clang::driver::tools; |
| 22 | using namespace clang::driver::toolchains; |
| 23 | using namespace clang; |
| 24 | using namespace llvm::opt; |
| 25 | |
Dan Gohman | 055a6f0 | 2019-01-15 06:58:16 +0000 | [diff] [blame] | 26 | /// Following the conventions in https://wiki.debian.org/Multiarch/Tuples, |
| 27 | /// we remove the vendor field to form the multiarch triple. |
| 28 | static std::string getMultiarchTriple(const Driver &D, |
| 29 | const llvm::Triple &TargetTriple, |
| 30 | StringRef SysRoot) { |
| 31 | return (TargetTriple.getArchName() + "-" + |
| 32 | TargetTriple.getOSAndEnvironmentName()).str(); |
| 33 | } |
| 34 | |
Sam Clegg | fc5ddee | 2019-03-28 17:45:18 +0000 | [diff] [blame] | 35 | std::string wasm::Linker::getLinkerPath(const ArgList &Args) const { |
| 36 | const ToolChain &ToolChain = getToolChain(); |
| 37 | if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { |
| 38 | StringRef UseLinker = A->getValue(); |
| 39 | if (!UseLinker.empty()) { |
| 40 | if (llvm::sys::path::is_absolute(UseLinker) && |
| 41 | llvm::sys::fs::can_execute(UseLinker)) |
| 42 | return UseLinker; |
| 43 | |
| 44 | // Accept 'lld', and 'ld' as aliases for the default linker |
| 45 | if (UseLinker != "lld" && UseLinker != "ld") |
| 46 | ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name) |
| 47 | << A->getAsString(Args); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return ToolChain.GetProgramPath(ToolChain.getDefaultLinker()); |
| 52 | } |
| 53 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 54 | void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 55 | const InputInfo &Output, |
| 56 | const InputInfoList &Inputs, |
| 57 | const ArgList &Args, |
| 58 | const char *LinkingOutput) const { |
| 59 | |
| 60 | const ToolChain &ToolChain = getToolChain(); |
Sam Clegg | fc5ddee | 2019-03-28 17:45:18 +0000 | [diff] [blame] | 61 | const char *Linker = Args.MakeArgString(getLinkerPath(Args)); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 62 | ArgStringList CmdArgs; |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 63 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 64 | if (Args.hasArg(options::OPT_s)) |
| 65 | CmdArgs.push_back("--strip-all"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 66 | |
| 67 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
Sam Clegg | d09a356 | 2017-12-02 23:11:13 +0000 | [diff] [blame] | 68 | Args.AddAllArgs(CmdArgs, options::OPT_u); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 69 | ToolChain.AddFilePathLibArgs(Args, CmdArgs); |
| 70 | |
Sam Clegg | 471d7af | 2017-10-27 18:10:19 +0000 | [diff] [blame] | 71 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) |
| 72 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o"))); |
| 73 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 74 | AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); |
| 75 | |
| 76 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { |
Nico Weber | 0ee47d9 | 2017-07-25 18:02:57 +0000 | [diff] [blame] | 77 | if (ToolChain.ShouldLinkCXXStdlib(Args)) |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 78 | ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); |
| 79 | |
Thomas Lively | 80ff58e | 2019-03-22 22:25:37 +0000 | [diff] [blame] | 80 | if (Args.hasArg(options::OPT_pthread)) { |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 81 | CmdArgs.push_back("-lpthread"); |
Thomas Lively | 80ff58e | 2019-03-22 22:25:37 +0000 | [diff] [blame] | 82 | CmdArgs.push_back("--shared-memory"); |
| 83 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 84 | |
| 85 | CmdArgs.push_back("-lc"); |
Sam Clegg | a08631e | 2017-10-27 00:26:07 +0000 | [diff] [blame] | 86 | AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 89 | CmdArgs.push_back("-o"); |
| 90 | CmdArgs.push_back(Output.getFilename()); |
| 91 | |
| 92 | C.addCommand(llvm::make_unique<Command>(JA, *this, Linker, CmdArgs, Inputs)); |
| 93 | } |
| 94 | |
| 95 | WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple, |
| 96 | const llvm::opt::ArgList &Args) |
Heejin Ahn | 4fa8dd9 | 2018-08-31 20:57:00 +0000 | [diff] [blame] | 97 | : ToolChain(D, Triple, Args) { |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 98 | |
| 99 | assert(Triple.isArch32Bit() != Triple.isArch64Bit()); |
Sam Clegg | 27ea156 | 2017-05-09 17:47:50 +0000 | [diff] [blame] | 100 | |
| 101 | getProgramPaths().push_back(getDriver().getInstalledDir()); |
| 102 | |
Dan Gohman | 055a6f0 | 2019-01-15 06:58:16 +0000 | [diff] [blame] | 103 | if (getTriple().getOS() == llvm::Triple::UnknownOS) { |
| 104 | // Theoretically an "unknown" OS should mean no standard libraries, however |
| 105 | // it could also mean that a custom set of libraries is in use, so just add |
| 106 | // /lib to the search path. Disable multiarch in this case, to discourage |
| 107 | // paths containing "unknown" from acquiring meanings. |
| 108 | getFilePaths().push_back(getDriver().SysRoot + "/lib"); |
| 109 | } else { |
| 110 | const std::string MultiarchTriple = |
| 111 | getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot); |
| 112 | getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple); |
| 113 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool WebAssembly::IsMathErrnoDefault() const { return false; } |
| 117 | |
| 118 | bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; } |
| 119 | |
| 120 | bool WebAssembly::UseObjCMixedDispatch() const { return true; } |
| 121 | |
| 122 | bool WebAssembly::isPICDefault() const { return false; } |
| 123 | |
| 124 | bool WebAssembly::isPIEDefault() const { return false; } |
| 125 | |
| 126 | bool WebAssembly::isPICDefaultForced() const { return false; } |
| 127 | |
| 128 | bool WebAssembly::IsIntegratedAssemblerDefault() const { return true; } |
| 129 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 130 | bool WebAssembly::hasBlocksRuntime() const { return false; } |
| 131 | |
| 132 | // TODO: Support profiling. |
| 133 | bool WebAssembly::SupportsProfiling() const { return false; } |
| 134 | |
| 135 | bool WebAssembly::HasNativeLLVMSupport() const { return true; } |
| 136 | |
| 137 | void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs, |
Gheorghe-Teodor Bercea | f0f2960 | 2017-07-06 16:22:21 +0000 | [diff] [blame] | 138 | ArgStringList &CC1Args, |
| 139 | Action::OffloadKind) const { |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 140 | if (DriverArgs.hasFlag(clang::driver::options::OPT_fuse_init_array, |
| 141 | options::OPT_fno_use_init_array, true)) |
| 142 | CC1Args.push_back("-fuse-init-array"); |
Heejin Ahn | 9d5a089 | 2019-02-11 22:47:50 +0000 | [diff] [blame] | 143 | |
Thomas Lively | 70f5fc1 | 2019-03-02 00:18:09 +0000 | [diff] [blame] | 144 | // '-pthread' implies '-target-feature +atomics' |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 145 | if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread, |
| 146 | false)) { |
| 147 | if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics, |
| 148 | false)) |
| 149 | getDriver().Diag(diag::err_drv_argument_not_allowed_with) |
| 150 | << "-pthread" |
| 151 | << "-mno-atomics"; |
Heejin Ahn | 9d5a089 | 2019-02-11 22:47:50 +0000 | [diff] [blame] | 152 | CC1Args.push_back("-target-feature"); |
| 153 | CC1Args.push_back("+atomics"); |
| 154 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | ToolChain::RuntimeLibType WebAssembly::GetDefaultRuntimeLibType() const { |
| 158 | return ToolChain::RLT_CompilerRT; |
| 159 | } |
| 160 | |
Heejin Ahn | 4fa8dd9 | 2018-08-31 20:57:00 +0000 | [diff] [blame] | 161 | ToolChain::CXXStdlibType |
| 162 | WebAssembly::GetCXXStdlibType(const ArgList &Args) const { |
Sam Clegg | ffbfc0f | 2018-01-12 17:54:49 +0000 | [diff] [blame] | 163 | if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { |
| 164 | StringRef Value = A->getValue(); |
| 165 | if (Value != "libc++") |
| 166 | getDriver().Diag(diag::err_drv_invalid_stdlib_name) |
| 167 | << A->getAsString(Args); |
| 168 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 169 | return ToolChain::CST_Libcxx; |
| 170 | } |
| 171 | |
| 172 | void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 173 | ArgStringList &CC1Args) const { |
Sam Clegg | 818dd86 | 2019-06-13 09:42:43 +0000 | [diff] [blame^] | 174 | if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) |
| 175 | return; |
| 176 | |
| 177 | const Driver &D = getDriver(); |
| 178 | |
| 179 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
| 180 | SmallString<128> P(D.ResourceDir); |
| 181 | llvm::sys::path::append(P, "include"); |
| 182 | addSystemInclude(DriverArgs, CC1Args, P); |
Dan Gohman | 055a6f0 | 2019-01-15 06:58:16 +0000 | [diff] [blame] | 183 | } |
Sam Clegg | 818dd86 | 2019-06-13 09:42:43 +0000 | [diff] [blame^] | 184 | |
| 185 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
| 186 | return; |
| 187 | |
| 188 | // Check for configure-time C include directories. |
| 189 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 190 | if (CIncludeDirs != "") { |
| 191 | SmallVector<StringRef, 5> dirs; |
| 192 | CIncludeDirs.split(dirs, ":"); |
| 193 | for (StringRef dir : dirs) { |
| 194 | StringRef Prefix = |
| 195 | llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : ""; |
| 196 | addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); |
| 197 | } |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (getTriple().getOS() != llvm::Triple::UnknownOS) { |
| 202 | const std::string MultiarchTriple = |
| 203 | getMultiarchTriple(D, getTriple(), D.SysRoot); |
| 204 | addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple); |
| 205 | } |
| 206 | addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include"); |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
| 210 | ArgStringList &CC1Args) const { |
| 211 | if (!DriverArgs.hasArg(options::OPT_nostdlibinc) && |
Dan Gohman | 055a6f0 | 2019-01-15 06:58:16 +0000 | [diff] [blame] | 212 | !DriverArgs.hasArg(options::OPT_nostdincxx)) { |
| 213 | if (getTriple().getOS() != llvm::Triple::UnknownOS) { |
| 214 | const std::string MultiarchTriple = |
| 215 | getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); |
| 216 | addSystemInclude(DriverArgs, CC1Args, |
Sam Clegg | 818dd86 | 2019-06-13 09:42:43 +0000 | [diff] [blame^] | 217 | getDriver().SysRoot + "/include/" + MultiarchTriple + |
| 218 | "/c++/v1"); |
Dan Gohman | 055a6f0 | 2019-01-15 06:58:16 +0000 | [diff] [blame] | 219 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 220 | addSystemInclude(DriverArgs, CC1Args, |
| 221 | getDriver().SysRoot + "/include/c++/v1"); |
Dan Gohman | 055a6f0 | 2019-01-15 06:58:16 +0000 | [diff] [blame] | 222 | } |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Sam Clegg | ffbfc0f | 2018-01-12 17:54:49 +0000 | [diff] [blame] | 225 | void WebAssembly::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, |
| 226 | llvm::opt::ArgStringList &CmdArgs) const { |
| 227 | |
| 228 | switch (GetCXXStdlibType(Args)) { |
| 229 | case ToolChain::CST_Libcxx: |
| 230 | CmdArgs.push_back("-lc++"); |
| 231 | CmdArgs.push_back("-lc++abi"); |
| 232 | break; |
| 233 | case ToolChain::CST_Libstdcxx: |
| 234 | llvm_unreachable("invalid stdlib name"); |
| 235 | } |
| 236 | } |
| 237 | |
Thomas Lively | 5458cd4 | 2019-05-29 18:31:50 +0000 | [diff] [blame] | 238 | SanitizerMask WebAssembly::getSupportedSanitizers() const { |
| 239 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 240 | if (getTriple().isOSEmscripten()) { |
Thomas Lively | 5e7ca75 | 2019-06-06 01:38:12 +0000 | [diff] [blame] | 241 | Res |= SanitizerKind::Vptr | SanitizerKind::Leak; |
Thomas Lively | 5458cd4 | 2019-05-29 18:31:50 +0000 | [diff] [blame] | 242 | } |
| 243 | return Res; |
| 244 | } |
| 245 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 246 | Tool *WebAssembly::buildLinker() const { |
| 247 | return new tools::wasm::Linker(*this); |
| 248 | } |