|
| 1 | +//===---- WebAssemblyToolChains.cpp - Job invocations (WebAssembly-specific) ------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "ToolChains.h" |
| 14 | + |
| 15 | +#include "swift/Basic/Dwarf.h" |
| 16 | +#include "swift/Basic/LLVM.h" |
| 17 | +#include "swift/Basic/Platform.h" |
| 18 | +#include "swift/Basic/Range.h" |
| 19 | +#include "swift/Basic/TaskQueue.h" |
| 20 | +#include "swift/Config.h" |
| 21 | +#include "swift/Driver/Compilation.h" |
| 22 | +#include "swift/Driver/Driver.h" |
| 23 | +#include "swift/Driver/Job.h" |
| 24 | +#include "swift/Option/Options.h" |
| 25 | +#include "swift/Option/SanitizerOptions.h" |
| 26 | +#include "clang/Basic/Version.h" |
| 27 | +#include "clang/Driver/Util.h" |
| 28 | +#include "llvm/ADT/StringSwitch.h" |
| 29 | +#include "llvm/Option/Arg.h" |
| 30 | +#include "llvm/Option/ArgList.h" |
| 31 | +#include "llvm/ProfileData/InstrProf.h" |
| 32 | +#include "llvm/Support/FileSystem.h" |
| 33 | +#include "llvm/Support/Path.h" |
| 34 | +#include "llvm/Support/Process.h" |
| 35 | +#include "llvm/Support/Program.h" |
| 36 | + |
| 37 | +using namespace swift; |
| 38 | +using namespace swift::driver; |
| 39 | +using namespace llvm::opt; |
| 40 | + |
| 41 | +std::string toolchains::WebAssembly::sanitizerRuntimeLibName(StringRef Sanitizer, |
| 42 | + bool shared) const { |
| 43 | + return (Twine("clang_rt.") + Sanitizer + "-" + |
| 44 | + this->getTriple().getArchName() + ".lib") |
| 45 | + .str(); |
| 46 | +} |
| 47 | + |
| 48 | +ToolChain::InvocationInfo toolchains::WebAssembly::constructInvocation( |
| 49 | + const AutolinkExtractJobAction &job, const JobContext &context) const { |
| 50 | + assert(context.Output.getPrimaryOutputType() == file_types::TY_AutolinkFile); |
| 51 | + |
| 52 | + InvocationInfo II{"swift-autolink-extract"}; |
| 53 | + ArgStringList &Arguments = II.Arguments; |
| 54 | + II.allowsResponseFiles = true; |
| 55 | + |
| 56 | + addPrimaryInputsOfType(Arguments, context.Inputs, context.Args, |
| 57 | + file_types::TY_Object); |
| 58 | + addInputsOfType(Arguments, context.InputActions, file_types::TY_Object); |
| 59 | + |
| 60 | + Arguments.push_back("-o"); |
| 61 | + Arguments.push_back( |
| 62 | + context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); |
| 63 | + |
| 64 | + return II; |
| 65 | +} |
| 66 | + |
| 67 | +ToolChain::InvocationInfo |
| 68 | +toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job, |
| 69 | + const JobContext &context) const { |
| 70 | + assert(context.Output.getPrimaryOutputType() == file_types::TY_Image && |
| 71 | + "Invalid linker output type."); |
| 72 | + |
| 73 | + ArgStringList Arguments; |
| 74 | + |
| 75 | + std::string Target = getTriple().str(); |
| 76 | + if (!Target.empty()) { |
| 77 | + Arguments.push_back("-target"); |
| 78 | + Arguments.push_back(context.Args.MakeArgString(Target)); |
| 79 | + } |
| 80 | + |
| 81 | + switch (job.getKind()) { |
| 82 | + case LinkKind::None: |
| 83 | + llvm_unreachable("invalid link kind"); |
| 84 | + case LinkKind::Executable: |
| 85 | + // Default case, nothing extra needed. |
| 86 | + break; |
| 87 | + case LinkKind::DynamicLibrary: |
| 88 | + llvm_unreachable("WebAssembly doesn't support dynamic library yet"); |
| 89 | + case LinkKind::StaticLibrary: |
| 90 | + llvm_unreachable("invalid link kind"); |
| 91 | + } |
| 92 | + |
| 93 | + // Select the linker to use. |
| 94 | + std::string Linker; |
| 95 | + if (const Arg *A = context.Args.getLastArg(options::OPT_use_ld)) { |
| 96 | + Linker = A->getValue(); |
| 97 | + } |
| 98 | + if (!Linker.empty()) |
| 99 | + Arguments.push_back(context.Args.MakeArgString("-fuse-ld=" + Linker)); |
| 100 | + |
| 101 | + |
| 102 | + const char *Clang = "clang"; |
| 103 | + if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) { |
| 104 | + StringRef toolchainPath(A->getValue()); |
| 105 | + |
| 106 | + // If there is a clang in the toolchain folder, use that instead. |
| 107 | + if (auto tool = llvm::sys::findProgramByName("clang", {toolchainPath})) |
| 108 | + Clang = context.Args.MakeArgString(tool.get()); |
| 109 | + } |
| 110 | + |
| 111 | + SmallVector<std::string, 4> RuntimeLibPaths; |
| 112 | + getRuntimeLibraryPaths(RuntimeLibPaths, context.Args, context.OI.SDKPath, |
| 113 | + /*Shared=*/false); |
| 114 | + |
| 115 | + SmallString<128> SharedResourceDirPath; |
| 116 | + getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/false); |
| 117 | + |
| 118 | + SmallString<128> swiftrtPath = SharedResourceDirPath; |
| 119 | + llvm::sys::path::append(swiftrtPath, |
| 120 | + swift::getMajorArchitectureName(getTriple())); |
| 121 | + llvm::sys::path::append(swiftrtPath, "swiftrt.o"); |
| 122 | + Arguments.push_back(context.Args.MakeArgString(swiftrtPath)); |
| 123 | + |
| 124 | + addPrimaryInputsOfType(Arguments, context.Inputs, context.Args, |
| 125 | + file_types::TY_Object); |
| 126 | + addInputsOfType(Arguments, context.InputActions, file_types::TY_Object); |
| 127 | + |
| 128 | + if (!context.OI.SDKPath.empty()) { |
| 129 | + Arguments.push_back("--sysroot"); |
| 130 | + Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath)); |
| 131 | + } |
| 132 | + |
| 133 | + // Add any autolinking scripts to the arguments |
| 134 | + for (const Job *Cmd : context.Inputs) { |
| 135 | + auto &OutputInfo = Cmd->getOutput(); |
| 136 | + if (OutputInfo.getPrimaryOutputType() == file_types::TY_AutolinkFile) |
| 137 | + Arguments.push_back(context.Args.MakeArgString( |
| 138 | + Twine("@") + OutputInfo.getPrimaryOutputFilename())); |
| 139 | + } |
| 140 | + |
| 141 | + // Add the runtime library link paths. |
| 142 | + for (auto path : RuntimeLibPaths) { |
| 143 | + Arguments.push_back("-L"); |
| 144 | + Arguments.push_back(context.Args.MakeArgString(path)); |
| 145 | + } |
| 146 | + // Link the standard library. In two paths, we do this using a .lnk file; |
| 147 | + // if we're going that route, we'll set `linkFilePath` to the path to that |
| 148 | + // file. |
| 149 | + SmallString<128> linkFilePath; |
| 150 | + getResourceDirPath(linkFilePath, context.Args, /*Shared=*/false); |
| 151 | + llvm::sys::path::append(linkFilePath, "static-executable-args.lnk"); |
| 152 | + |
| 153 | + auto linkFile = linkFilePath.str(); |
| 154 | + if (llvm::sys::fs::is_regular_file(linkFile)) { |
| 155 | + Arguments.push_back(context.Args.MakeArgString(Twine("@") + linkFile)); |
| 156 | + } else { |
| 157 | + llvm::report_fatal_error(linkFile + " not found"); |
| 158 | + } |
| 159 | + |
| 160 | + // Explicitly pass the target to the linker |
| 161 | + Arguments.push_back( |
| 162 | + context.Args.MakeArgString("--target=" + getTriple().str())); |
| 163 | + |
| 164 | + // Delegate to Clang for sanitizers. It will figure out the correct linker |
| 165 | + // options. |
| 166 | + if (job.getKind() == LinkKind::Executable && context.OI.SelectedSanitizers) { |
| 167 | + Arguments.push_back(context.Args.MakeArgString( |
| 168 | + "-fsanitize=" + getSanitizerList(context.OI.SelectedSanitizers))); |
| 169 | + |
| 170 | + // The TSan runtime depends on the blocks runtime and libdispatch. |
| 171 | + if (context.OI.SelectedSanitizers & SanitizerKind::Thread) { |
| 172 | + Arguments.push_back("-lBlocksRuntime"); |
| 173 | + Arguments.push_back("-ldispatch"); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (context.Args.hasArg(options::OPT_profile_generate)) { |
| 178 | + SmallString<128> LibProfile(SharedResourceDirPath); |
| 179 | + llvm::sys::path::remove_filename(LibProfile); // remove platform name |
| 180 | + llvm::sys::path::append(LibProfile, "clang", "lib"); |
| 181 | + |
| 182 | + llvm::sys::path::append(LibProfile, getTriple().getOSName(), |
| 183 | + Twine("libclang_rt.profile-") + |
| 184 | + getTriple().getArchName() + ".a"); |
| 185 | + Arguments.push_back(context.Args.MakeArgString(LibProfile)); |
| 186 | + Arguments.push_back(context.Args.MakeArgString( |
| 187 | + Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); |
| 188 | + } |
| 189 | + |
| 190 | + // Run clang++ in verbose mode if "-v" is set |
| 191 | + if (context.Args.hasArg(options::OPT_v)) { |
| 192 | + Arguments.push_back("-v"); |
| 193 | + } |
| 194 | + |
| 195 | + // These custom arguments should be right before the object file at the end. |
| 196 | + context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group); |
| 197 | + context.Args.AddAllArgs(Arguments, options::OPT_Xlinker); |
| 198 | + context.Args.AddAllArgValues(Arguments, options::OPT_Xclang_linker); |
| 199 | + |
| 200 | + // This should be the last option, for convenience in checking output. |
| 201 | + Arguments.push_back("-o"); |
| 202 | + Arguments.push_back( |
| 203 | + context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); |
| 204 | + |
| 205 | + InvocationInfo II{Clang, Arguments}; |
| 206 | + II.allowsResponseFiles = true; |
| 207 | + |
| 208 | + return II; |
| 209 | +} |
| 210 | + |
| 211 | +ToolChain::InvocationInfo |
| 212 | +toolchains::WebAssembly::constructInvocation(const StaticLinkJobAction &job, |
| 213 | + const JobContext &context) const { |
| 214 | + assert(context.Output.getPrimaryOutputType() == file_types::TY_Image && |
| 215 | + "Invalid linker output type."); |
| 216 | + |
| 217 | + ArgStringList Arguments; |
| 218 | + |
| 219 | + const char *AR = "llvm-ar"; |
| 220 | + Arguments.push_back("crs"); |
| 221 | + |
| 222 | + Arguments.push_back( |
| 223 | + context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); |
| 224 | + |
| 225 | + addPrimaryInputsOfType(Arguments, context.Inputs, context.Args, |
| 226 | + file_types::TY_Object); |
| 227 | + addInputsOfType(Arguments, context.InputActions, file_types::TY_Object); |
| 228 | + |
| 229 | + InvocationInfo II{AR, Arguments}; |
| 230 | + |
| 231 | + return II; |
| 232 | +} |
0 commit comments