|
| 1 | +//===--------------- WebAssemblyToolchain+LinkerSupport.swift -------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 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 | +import TSCBasic |
| 13 | +import SwiftOptions |
| 14 | + |
| 15 | +extension WebAssemblyToolchain { |
| 16 | + public func addPlatformSpecificLinkerArgs( |
| 17 | + to commandLine: inout [Job.ArgTemplate], |
| 18 | + parsedOptions: inout ParsedOptions, |
| 19 | + linkerOutputType: LinkOutputType, |
| 20 | + inputs: [TypedVirtualPath], |
| 21 | + outputFile: VirtualPath, |
| 22 | + shouldUseInputFileList: Bool, |
| 23 | + lto: LTOKind?, |
| 24 | + sanitizers: Set<Sanitizer>, |
| 25 | + targetInfo: FrontendTargetInfo |
| 26 | + ) throws -> AbsolutePath { |
| 27 | + let targetTriple = targetInfo.target.triple |
| 28 | + switch linkerOutputType { |
| 29 | + case .dynamicLibrary: |
| 30 | + throw Error.dynamicLibrariesUnsupportedForTarget(targetTriple.triple) |
| 31 | + case .executable: |
| 32 | + if !targetTriple.triple.isEmpty { |
| 33 | + commandLine.appendFlag("-target") |
| 34 | + commandLine.appendFlag(targetTriple.triple) |
| 35 | + } |
| 36 | + |
| 37 | + // Select the linker to use. |
| 38 | + if let linkerArg = parsedOptions.getLastArgument(.useLd)?.asSingle { |
| 39 | + commandLine.appendFlag("-fuse-ld=\(linkerArg)") |
| 40 | + } |
| 41 | + |
| 42 | + // Configure the toolchain. |
| 43 | + // |
| 44 | + // By default use the system `clang` to perform the link. We use `clang` for |
| 45 | + // the driver here because we do not wish to select a particular C++ runtime. |
| 46 | + // Furthermore, until C++ interop is enabled, we cannot have a dependency on |
| 47 | + // C++ code from pure Swift code. If linked libraries are C++ based, they |
| 48 | + // should properly link C++. In the case of static linking, the user can |
| 49 | + // explicitly specify the C++ runtime to link against. This is particularly |
| 50 | + // important for platforms like android where as it is a Linux platform, the |
| 51 | + // default C++ runtime is `libstdc++` which is unsupported on the target but |
| 52 | + // as the builds are usually cross-compiled from Linux, libstdc++ is going to |
| 53 | + // be present. This results in linking the wrong version of libstdc++ |
| 54 | + // generating invalid binaries. It is also possible to use different C++ |
| 55 | + // runtimes than the default C++ runtime for the platform (e.g. libc++ on |
| 56 | + // Windows rather than msvcprt). When C++ interop is enabled, we will need to |
| 57 | + // surface this via a driver flag. For now, opt for the simpler approach of |
| 58 | + // just using `clang` and avoid a dependency on the C++ runtime. |
| 59 | + var clangPath = try getToolPath(.clang) |
| 60 | + if let toolsDirPath = parsedOptions.getLastArgument(.toolsDirectory) { |
| 61 | + // FIXME: What if this isn't an absolute path? |
| 62 | + let toolsDir = try AbsolutePath(validating: toolsDirPath.asSingle) |
| 63 | + |
| 64 | + // If there is a clang in the toolchain folder, use that instead. |
| 65 | + if let tool = lookupExecutablePath(filename: "clang", searchPaths: [toolsDir]) { |
| 66 | + clangPath = tool |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + guard !parsedOptions.hasArgument(.noStaticStdlib, .noStaticExecutable) else { |
| 71 | + throw Error.dynamicLibrariesUnsupportedForTarget(targetTriple.triple) |
| 72 | + } |
| 73 | + |
| 74 | + let runtimePaths = try runtimeLibraryPaths( |
| 75 | + for: targetTriple, |
| 76 | + parsedOptions: &parsedOptions, |
| 77 | + sdkPath: targetInfo.sdkPath?.path, |
| 78 | + isShared: false |
| 79 | + ) |
| 80 | + |
| 81 | + let resourceDirPath = try computeResourceDirPath( |
| 82 | + for: targetTriple, |
| 83 | + parsedOptions: &parsedOptions, |
| 84 | + isShared: false |
| 85 | + ) |
| 86 | + |
| 87 | + let swiftrtPath = resourceDirPath |
| 88 | + .appending( |
| 89 | + components: targetTriple.archName, "swiftrt.o" |
| 90 | + ) |
| 91 | + commandLine.appendPath(swiftrtPath) |
| 92 | + |
| 93 | + let inputFiles: [Job.ArgTemplate] = inputs.compactMap { input in |
| 94 | + // Autolink inputs are handled specially |
| 95 | + if input.type == .autolink { |
| 96 | + return .responseFilePath(input.file) |
| 97 | + } else if input.type == .object { |
| 98 | + return .path(input.file) |
| 99 | + } else { |
| 100 | + return nil |
| 101 | + } |
| 102 | + } |
| 103 | + commandLine.append(contentsOf: inputFiles) |
| 104 | + |
| 105 | + if let path = targetInfo.sdkPath?.path { |
| 106 | + commandLine.appendFlag("--sysroot") |
| 107 | + commandLine.appendPath(path) |
| 108 | + } |
| 109 | + |
| 110 | + // Add the runtime library link paths. |
| 111 | + for path in runtimePaths { |
| 112 | + commandLine.appendFlag(.L) |
| 113 | + commandLine.appendPath(path) |
| 114 | + } |
| 115 | + |
| 116 | + // Link the standard library. |
| 117 | + let linkFilePath: VirtualPath = resourceDirPath |
| 118 | + .appending(components: "static-executable-args.lnk") |
| 119 | + |
| 120 | + guard try fileSystem.exists(linkFilePath) else { |
| 121 | + fatalError("\(linkFilePath) not found") |
| 122 | + } |
| 123 | + commandLine.append(.responseFilePath(linkFilePath)) |
| 124 | + |
| 125 | + // Explicitly pass the target to the linker |
| 126 | + commandLine.appendFlag("--target=\(targetTriple.triple)") |
| 127 | + |
| 128 | + // Delegate to Clang for sanitizers. It will figure out the correct linker |
| 129 | + // options. |
| 130 | + guard sanitizers.isEmpty else { |
| 131 | + fatalError("WebAssembly does not support sanitizers, but a runtime library was found") |
| 132 | + } |
| 133 | + |
| 134 | + guard !parsedOptions.hasArgument(.profileGenerate) else { |
| 135 | + throw Error.profilingUnsupportedForTarget(targetTriple.triple) |
| 136 | + } |
| 137 | + |
| 138 | + // Run clang++ in verbose mode if "-v" is set |
| 139 | + try commandLine.appendLast(.v, from: &parsedOptions) |
| 140 | + |
| 141 | + // These custom arguments should be right before the object file at the |
| 142 | + // end. |
| 143 | + try commandLine.append( |
| 144 | + contentsOf: parsedOptions.arguments(in: .linkerOption) |
| 145 | + ) |
| 146 | + try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions) |
| 147 | + try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions) |
| 148 | + |
| 149 | + // This should be the last option, for convenience in checking output. |
| 150 | + commandLine.appendFlag(.o) |
| 151 | + commandLine.appendPath(outputFile) |
| 152 | + return clangPath |
| 153 | + case .staticLibrary: |
| 154 | + // We're using 'ar' as a linker |
| 155 | + commandLine.appendFlag("crs") |
| 156 | + commandLine.appendPath(outputFile) |
| 157 | + |
| 158 | + commandLine.append(contentsOf: inputs.map { .path($0.file) }) |
| 159 | + return try getToolPath(.staticLinker(lto)) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments