|
| 1 | +//===--------------- GenericUnixToolchain+LinkerSupport.swift -------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 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 WindowsToolchain { |
| 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 | + sdkPath: String?, |
| 24 | + sanitizers: Set<Sanitizer>, |
| 25 | + targetInfo: FrontendTargetInfo |
| 26 | + ) throws -> AbsolutePath { |
| 27 | + let targetTriple = targetInfo.target.triple |
| 28 | + switch linkerOutputType { |
| 29 | + case .dynamicLibrary: |
| 30 | + // Same options as an executable, just with '-shared' |
| 31 | + commandLine.appendFlags("-parse-as-library", "-emit-library") |
| 32 | + fallthrough |
| 33 | + case .executable: |
| 34 | + if !targetTriple.triple.isEmpty { |
| 35 | + commandLine.appendFlag("-target") |
| 36 | + commandLine.appendFlag(targetTriple.triple) |
| 37 | + } |
| 38 | + commandLine.appendFlag("-emit-executable") |
| 39 | + default: |
| 40 | + break |
| 41 | + } |
| 42 | + |
| 43 | + switch linkerOutputType { |
| 44 | + case .staticLibrary: |
| 45 | + commandLine.append(.joinedOptionAndPath("-out:", outputFile)) |
| 46 | + commandLine.append(contentsOf: inputs.map { .path($0.file) }) |
| 47 | + return try getToolPath(.staticLinker) |
| 48 | + default: |
| 49 | + // Configure the toolchain. |
| 50 | + // |
| 51 | + // By default use the system `clang` to perform the link. We use `clang` for |
| 52 | + // the driver here because we do not wish to select a particular C++ runtime. |
| 53 | + // Furthermore, until C++ interop is enabled, we cannot have a dependency on |
| 54 | + // C++ code from pure Swift code. If linked libraries are C++ based, they |
| 55 | + // should properly link C++. In the case of static linking, the user can |
| 56 | + // explicitly specify the C++ runtime to link against. This is particularly |
| 57 | + // important for platforms like android where as it is a Linux platform, the |
| 58 | + // default C++ runtime is `libstdc++` which is unsupported on the target but |
| 59 | + // as the builds are usually cross-compiled from Linux, libstdc++ is going to |
| 60 | + // be present. This results in linking the wrong version of libstdc++ |
| 61 | + // generating invalid binaries. It is also possible to use different C++ |
| 62 | + // runtimes than the default C++ runtime for the platform (e.g. libc++ on |
| 63 | + // Windows rather than msvcprt). When C++ interop is enabled, we will need to |
| 64 | + // surface this via a driver flag. For now, opt for the simpler approach of |
| 65 | + // just using `clang` and avoid a dependency on the C++ runtime. |
| 66 | + var clangPath = try getToolPath(.clang) |
| 67 | + if let toolsDirPath = parsedOptions.getLastArgument(.toolsDirectory) { |
| 68 | + // FIXME: What if this isn't an absolute path? |
| 69 | + let toolsDir = try AbsolutePath(validating: toolsDirPath.asSingle) |
| 70 | + |
| 71 | + // If there is a clang in the toolchain folder, use that instead. |
| 72 | + if let tool = lookupExecutablePath(filename: "clang.exe", searchPaths: [toolsDir]) { |
| 73 | + clangPath = tool |
| 74 | + } |
| 75 | + |
| 76 | + // Look for binutils in the toolchain folder. |
| 77 | + commandLine.appendFlag("-B") |
| 78 | + commandLine.appendPath(toolsDir) |
| 79 | + } |
| 80 | + |
| 81 | + let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib, |
| 82 | + negative: .noStaticStdlib, |
| 83 | + default: false) |
| 84 | + let staticExecutable = parsedOptions.hasFlag(positive: .staticExecutable, |
| 85 | + negative: .noStaticExecutable, |
| 86 | + default: false) |
| 87 | + let hasRuntimeArgs = !(staticStdlib || staticExecutable) |
| 88 | + |
| 89 | + let runtimePaths = try runtimeLibraryPaths( |
| 90 | + for: targetTriple, |
| 91 | + parsedOptions: &parsedOptions, |
| 92 | + sdkPath: sdkPath, |
| 93 | + isShared: hasRuntimeArgs |
| 94 | + ) |
| 95 | + |
| 96 | + if hasRuntimeArgs && targetTriple.environment != .android { |
| 97 | + // FIXME: We probably shouldn't be adding an rpath here unless we know |
| 98 | + // ahead of time the standard library won't be copied. |
| 99 | + for path in runtimePaths { |
| 100 | + commandLine.appendFlag(.Xlinker) |
| 101 | + commandLine.appendFlag("-rpath") |
| 102 | + commandLine.appendFlag(.Xlinker) |
| 103 | + commandLine.appendPath(path) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + let sharedResourceDirPath = try computeResourceDirPath( |
| 108 | + for: targetTriple, |
| 109 | + parsedOptions: &parsedOptions, |
| 110 | + isShared: true |
| 111 | + ) |
| 112 | + |
| 113 | + let swiftrtPath = sharedResourceDirPath |
| 114 | + .appending( |
| 115 | + components: "x86_64", "swiftrt.o" |
| 116 | + ) |
| 117 | + commandLine.appendPath(swiftrtPath) |
| 118 | + |
| 119 | + let inputFiles: [Job.ArgTemplate] = inputs.compactMap { input in |
| 120 | + // Autolink inputs are handled specially |
| 121 | + if input.type == .autolink { |
| 122 | + return .responseFilePath(input.file) |
| 123 | + } else if input.type == .object { |
| 124 | + return .path(input.file) |
| 125 | + } else { |
| 126 | + return nil |
| 127 | + } |
| 128 | + } |
| 129 | + commandLine.append(contentsOf: inputFiles) |
| 130 | + |
| 131 | + let fSystemArgs = parsedOptions.arguments(for: .F, .Fsystem) |
| 132 | + for opt in fSystemArgs { |
| 133 | + if opt.option == .Fsystem { |
| 134 | + commandLine.appendFlag("-iframework") |
| 135 | + } else { |
| 136 | + commandLine.appendFlag(.F) |
| 137 | + } |
| 138 | + commandLine.appendPath(try VirtualPath(path: opt.argument.asSingle)) |
| 139 | + } |
| 140 | + |
| 141 | + // Add the runtime library link paths. |
| 142 | + for path in runtimePaths { |
| 143 | + commandLine.appendFlag(.L) |
| 144 | + commandLine.appendPath(path) |
| 145 | + } |
| 146 | + |
| 147 | + // Link the standard library. In two paths, we do this using a .lnk file |
| 148 | + // if we're going that route, we'll set `linkFilePath` to the path to that |
| 149 | + // file. |
| 150 | + var linkFilePath: AbsolutePath? = try computeResourceDirPath( |
| 151 | + for: targetTriple, |
| 152 | + parsedOptions: &parsedOptions, |
| 153 | + isShared: false |
| 154 | + ) |
| 155 | + |
| 156 | + if staticExecutable { |
| 157 | + linkFilePath = linkFilePath?.appending(component: "static-executable-args.lnk") |
| 158 | + } else if staticStdlib { |
| 159 | + linkFilePath = linkFilePath?.appending(component: "static-stdlib-args.lnk") |
| 160 | + } else { |
| 161 | + linkFilePath = nil |
| 162 | + commandLine.appendFlag("-lswiftCore") |
| 163 | + } |
| 164 | + |
| 165 | + if let linkFile = linkFilePath { |
| 166 | + guard fileSystem.isFile(linkFile) else { |
| 167 | + fatalError("\(linkFile.pathString) not found") |
| 168 | + } |
| 169 | + commandLine.append(.responseFilePath(.absolute(linkFile))) |
| 170 | + } |
| 171 | + |
| 172 | + // Explicitly pass the target to the linker |
| 173 | + commandLine.appendFlag("--target=\(targetTriple.triple)") |
| 174 | + |
| 175 | + // Delegate to Clang for sanitizers. It will figure out the correct linker |
| 176 | + // options. |
| 177 | + if linkerOutputType == .executable && !sanitizers.isEmpty { |
| 178 | + let sanitizerNames = sanitizers |
| 179 | + .map { $0.rawValue } |
| 180 | + .sorted() // Sort so we get a stable, testable order |
| 181 | + .joined(separator: ",") |
| 182 | + commandLine.appendFlag("-fsanitize=\(sanitizerNames)") |
| 183 | + |
| 184 | + // The TSan runtime depends on the blocks runtime and libdispatch. |
| 185 | + if sanitizers.contains(.thread) { |
| 186 | + commandLine.appendFlag("-lBlocksRuntime") |
| 187 | + commandLine.appendFlag("-ldispatch") |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if parsedOptions.hasArgument(.profileGenerate) { |
| 192 | + let libProfile = sharedResourceDirPath |
| 193 | + .parentDirectory // remove platform name |
| 194 | + .appending(components: "clang", "lib", targetTriple.osName, |
| 195 | + "libclangrt_profile-\(targetTriple.archName).a") |
| 196 | + commandLine.appendPath(libProfile) |
| 197 | + |
| 198 | + // HACK: Hard-coded from llvm::getInstrProfRuntimeHookVarName() |
| 199 | + commandLine.appendFlag("-u__llvm_profile_runtime") |
| 200 | + } |
| 201 | + |
| 202 | + // Run clang++ in verbose mode if "-v" is set |
| 203 | + try commandLine.appendLast(.v, from: &parsedOptions) |
| 204 | + |
| 205 | + // These custom arguments should be right before the object file at the |
| 206 | + // end. |
| 207 | + try commandLine.append( |
| 208 | + contentsOf: parsedOptions.arguments(in: .linkerOption) |
| 209 | + ) |
| 210 | + try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions) |
| 211 | + try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions) |
| 212 | + |
| 213 | + // This should be the last option, for convenience in checking output. |
| 214 | + commandLine.appendFlag(.o) |
| 215 | + commandLine.appendPath(outputFile) |
| 216 | + return clangPath |
| 217 | + } |
| 218 | + |
| 219 | + } |
| 220 | +} |
0 commit comments