-
Notifications
You must be signed in to change notification settings - Fork 213
[WIP] Implement WindowsToolchain #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9fcd3f2
Implement WindowsToolchain
stevapple 9076ab2
Fix swiftrtPath
stevapple 2c9716b
Fix various problems
stevapple 8b7b0df
Derive executable file name from Toolchain
stevapple 3c0feb3
Fix addPlatformSpecificLinkerArgs for WindowsToolchain
stevapple 633801d
Fix clangLibraryPath for Windows
stevapple 59877f1
Disable profiling support on Windows
stevapple 4b69ec6
Disable unsupported sanitizers on Windows
stevapple 31d2556
Merge branch 'master' into windows-toolchain
stevapple d8c4f60
Fix cross-compilation problems
stevapple ed1b8c2
Fix CRT support for Windows
stevapple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,12 @@ extension Toolchain { | |
| resourceDirBase = sdkPath | ||
| .appending(components: "usr", "lib", | ||
| isShared ? "swift" : "swift_static") | ||
| } else if triple.isWindows, | ||
| let SDKROOT = env["SDKROOT"], | ||
| let sdkPath = try? AbsolutePath(validating: SDKROOT) { | ||
| resourceDirBase = sdkPath | ||
| .appending(components: "usr", "lib", | ||
| isShared ? "swift" : "swift_static") | ||
| } else { | ||
| resourceDirBase = try getToolPath(.swiftCompiler) | ||
| .parentDirectory // remove /swift | ||
|
|
@@ -48,6 +54,21 @@ extension Toolchain { | |
| for triple: Triple, | ||
| parsedOptions: inout ParsedOptions | ||
| ) throws -> AbsolutePath { | ||
| /// Use the one in `VCTools` if exists on Windows. | ||
| if triple.isWindows, | ||
| let vctools = env["VCToolsInstallDir"], | ||
| let root = try? AbsolutePath(validating: vctools) { | ||
| let archName: String = { | ||
| switch triple.arch { | ||
| case .aarch64, .aarch64_32: return "arm64" | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case .arm: return "arm" | ||
| case .x86: return "x86" | ||
| case nil, .x86_64: return "x64" | ||
| default: fatalError("unknown arch \(triple.archName) on Windows") | ||
| } | ||
| }() | ||
| return root.appending(components: "lib", archName) | ||
| } | ||
| return try computeResourceDirPath(for: triple, | ||
| parsedOptions: &parsedOptions, | ||
| isShared: true) | ||
|
|
@@ -258,3 +279,5 @@ extension DarwinToolchain { | |
| } | ||
|
|
||
| } | ||
|
|
||
| // TODO: See whether Windows needs `addArgsToLinkStdlib`. | ||
|
||
219 changes: 219 additions & 0 deletions
219
Sources/SwiftDriver/Jobs/WindowsToolchain+LinkerSupport.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| //===---------------- WindowsToolchain+LinkerSupport.swift ----------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| import TSCBasic | ||
| import SwiftOptions | ||
|
|
||
| extension WindowsToolchain { | ||
| public func addPlatformSpecificLinkerArgs( | ||
| to commandLine: inout [Job.ArgTemplate], | ||
| parsedOptions: inout ParsedOptions, | ||
| linkerOutputType: LinkOutputType, | ||
| inputs: [TypedVirtualPath], | ||
| outputFile: VirtualPath, | ||
| shouldUseInputFileList: Bool, | ||
| sdkPath: String?, | ||
| sanitizers: Set<Sanitizer>, | ||
| targetInfo: FrontendTargetInfo | ||
| ) throws -> AbsolutePath { | ||
| let targetTriple = targetInfo.target.triple | ||
| switch linkerOutputType { | ||
| case .dynamicLibrary: | ||
| // Same options as an executable, just with '-shared' | ||
| commandLine.appendFlags("-parse-as-library", "-emit-library") | ||
| fallthrough | ||
| case .executable: | ||
| if !targetTriple.triple.isEmpty { | ||
| commandLine.appendFlag("-target") | ||
| commandLine.appendFlag(targetTriple.triple) | ||
| } | ||
| commandLine.appendFlag("-emit-executable") | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| default: | ||
| break | ||
| } | ||
|
|
||
| switch linkerOutputType { | ||
| case .staticLibrary: | ||
| commandLine.append(.joinedOptionAndPath("-out:", outputFile)) | ||
| commandLine.append(contentsOf: inputs.map { .path($0.file) }) | ||
| return try getToolPath(.staticLinker) | ||
| default: | ||
| // Configure the toolchain. | ||
| // | ||
| // By default use the system `clang` to perform the link. We use `clang` for | ||
| // the driver here because we do not wish to select a particular C++ runtime. | ||
| // Furthermore, until C++ interop is enabled, we cannot have a dependency on | ||
| // C++ code from pure Swift code. If linked libraries are C++ based, they | ||
| // should properly link C++. In the case of static linking, the user can | ||
| // explicitly specify the C++ runtime to link against. This is particularly | ||
| // important for platforms like android where as it is a Linux platform, the | ||
| // default C++ runtime is `libstdc++` which is unsupported on the target but | ||
| // as the builds are usually cross-compiled from Linux, libstdc++ is going to | ||
| // be present. This results in linking the wrong version of libstdc++ | ||
| // generating invalid binaries. It is also possible to use different C++ | ||
| // runtimes than the default C++ runtime for the platform (e.g. libc++ on | ||
| // Windows rather than msvcprt). When C++ interop is enabled, we will need to | ||
| // surface this via a driver flag. For now, opt for the simpler approach of | ||
| // just using `clang` and avoid a dependency on the C++ runtime. | ||
| var clangPath = try getToolPath(.clang) | ||
| if let toolsDirPath = parsedOptions.getLastArgument(.toolsDirectory) { | ||
| // FIXME: What if this isn't an absolute path? | ||
| let toolsDir = try AbsolutePath(validating: toolsDirPath.asSingle) | ||
|
|
||
| // If there is a clang in the toolchain folder, use that instead. | ||
| if let tool = lookupExecutablePath(filename: "clang.exe", searchPaths: [toolsDir]) { | ||
| clangPath = tool | ||
| } | ||
|
|
||
| // Look for binutils in the toolchain folder. | ||
| commandLine.appendFlag("-B") | ||
| commandLine.appendPath(toolsDir) | ||
| } | ||
|
|
||
| let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib, | ||
| negative: .noStaticStdlib, | ||
| default: false) | ||
| let staticExecutable = parsedOptions.hasFlag(positive: .staticExecutable, | ||
| negative: .noStaticExecutable, | ||
| default: false) | ||
| let hasRuntimeArgs = !(staticStdlib || staticExecutable) | ||
|
|
||
| let runtimePaths = try runtimeLibraryPaths( | ||
| for: targetTriple, | ||
| parsedOptions: &parsedOptions, | ||
| sdkPath: sdkPath, | ||
| isShared: hasRuntimeArgs | ||
| ) | ||
|
|
||
| if hasRuntimeArgs { | ||
| // FIXME: We probably shouldn't be adding an rpath here unless we know | ||
| // ahead of time the standard library won't be copied. | ||
| for path in runtimePaths { | ||
| commandLine.appendFlag(.Xlinker) | ||
| commandLine.appendFlag("-rpath") | ||
| commandLine.appendFlag(.Xlinker) | ||
| commandLine.appendPath(path) | ||
| } | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| let sharedResourceDirPath = try computeResourceDirPath( | ||
| for: targetTriple, | ||
| parsedOptions: &parsedOptions, | ||
| isShared: true | ||
| ) | ||
|
|
||
| let swiftrtPath = sharedResourceDirPath.appending( | ||
| components: "windows", targetTriple.archName, "swiftrt.obj" | ||
| ) | ||
| commandLine.appendPath(swiftrtPath) | ||
|
|
||
| let inputFiles: [Job.ArgTemplate] = inputs.compactMap { input in | ||
| // Autolink inputs are handled specially | ||
| if input.type == .autolink { | ||
| return .responseFilePath(input.file) | ||
| } else if input.type == .object { | ||
| return .path(input.file) | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
| commandLine.append(contentsOf: inputFiles) | ||
|
|
||
| let fSystemArgs = parsedOptions.arguments(for: .F, .Fsystem) | ||
| for opt in fSystemArgs { | ||
| if opt.option == .Fsystem { | ||
| commandLine.appendFlag("-iframework") | ||
| } else { | ||
| commandLine.appendFlag(.F) | ||
| } | ||
| commandLine.appendPath(try VirtualPath(path: opt.argument.asSingle)) | ||
| } | ||
|
|
||
| // Add the runtime library link paths. | ||
| for path in runtimePaths { | ||
| commandLine.appendFlag(.L) | ||
| commandLine.appendPath(path) | ||
| } | ||
|
|
||
| // Link the standard library. In two paths, we do this using a .lnk file | ||
| // if we're going that route, we'll set `linkFilePath` to the path to that | ||
| // file. | ||
| var linkFilePath: AbsolutePath? = try computeResourceDirPath( | ||
| for: targetTriple, | ||
| parsedOptions: &parsedOptions, | ||
| isShared: false | ||
| ) | ||
|
|
||
| if staticExecutable { | ||
| linkFilePath = linkFilePath?.appending(component: "static-executable-args.lnk") | ||
| } else if staticStdlib { | ||
| linkFilePath = linkFilePath?.appending(component: "static-stdlib-args.lnk") | ||
| } else { | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| linkFilePath = nil | ||
| commandLine.appendFlag("-lswiftCore") | ||
| } | ||
|
|
||
| if let linkFile = linkFilePath { | ||
| guard fileSystem.isFile(linkFile) else { | ||
| fatalError("\(linkFile.pathString) not found") | ||
| } | ||
| commandLine.append(.responseFilePath(.absolute(linkFile))) | ||
| } | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Explicitly pass the target to the linker | ||
| commandLine.appendFlag("--target=\(targetTriple.triple)") | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Delegate to Clang for sanitizers. It will figure out the correct linker | ||
| // options. | ||
| if linkerOutputType == .executable && !sanitizers.isEmpty { | ||
| let sanitizerNames = sanitizers | ||
| .map { $0.rawValue } | ||
| .sorted() // Sort so we get a stable, testable order | ||
| .joined(separator: ",") | ||
| commandLine.appendFlag("-fsanitize=\(sanitizerNames)") | ||
|
|
||
| // The TSan runtime depends on the blocks runtime and libdispatch. | ||
| if sanitizers.contains(.thread) { | ||
| commandLine.appendFlag("-lBlocksRuntime") | ||
| commandLine.appendFlag("-ldispatch") | ||
| } | ||
| } | ||
|
|
||
| if parsedOptions.hasArgument(.profileGenerate) { | ||
| let libProfile = sharedResourceDirPath | ||
| .parentDirectory // remove platform name | ||
| .appending(components: "clang", "lib", targetTriple.osName, | ||
| "libclangrt_profile-\(targetTriple.archName).a") | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| commandLine.appendPath(libProfile) | ||
|
|
||
| // HACK: Hard-coded from llvm::getInstrProfRuntimeHookVarName() | ||
| commandLine.appendFlag("-u__llvm_profile_runtime") | ||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Run clang++ in verbose mode if "-v" is set | ||
| try commandLine.appendLast(.v, from: &parsedOptions) | ||
|
|
||
| // These custom arguments should be right before the object file at the | ||
| // end. | ||
| try commandLine.append( | ||
| contentsOf: parsedOptions.arguments(in: .linkerOption) | ||
| ) | ||
| try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions) | ||
| try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions) | ||
|
|
||
| // This should be the last option, for convenience in checking output. | ||
| commandLine.appendFlag(.o) | ||
| commandLine.appendPath(outputFile) | ||
| return clangPath | ||
| } | ||
|
|
||
stevapple marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.