-
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 9 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
181 changes: 181 additions & 0 deletions
181
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,181 @@ | ||
| //===---------------- 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: | ||
| commandLine.appendFlags("-Xlinker", "-dll") | ||
| fallthrough | ||
| case .executable: | ||
| if !targetTriple.triple.isEmpty { | ||
| commandLine.appendFlag("-target") | ||
| commandLine.appendFlag(targetTriple.triple) | ||
| } | ||
| // 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 linker: String | ||
| if let arg = parsedOptions.getLastArgument(.useLd) { | ||
| linker = arg.asSingle | ||
| } else { | ||
| linker = "link" | ||
| } | ||
| commandLine.appendFlag("-fuse-ld=\(linker)") | ||
|
|
||
| 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 | ||
| ) | ||
|
|
||
| let sharedResourceDirPath = try computeResourceDirPath( | ||
| for: targetTriple, | ||
| parsedOptions: &parsedOptions, | ||
| isShared: true | ||
| ) | ||
|
|
||
| let swiftrtPath = sharedResourceDirPath.appending( | ||
| components: 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) | ||
| } | ||
|
|
||
| if hasRuntimeArgs { | ||
| commandLine.appendFlag("-lswiftCore") | ||
| } | ||
|
|
||
| // Explicitly pass the target to the linker | ||
| commandLine.appendFlags("-target", targetTriple.triple) | ||
|
|
||
| // 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) { | ||
| // Profiling support for Windows isn't ready yet. It should have been disabled. | ||
| fatalError("Profiling support should have been disabled on Windows.") | ||
| } | ||
|
|
||
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 | ||
| case .staticLibrary: | ||
| commandLine.append(.joinedOptionAndPath("-out:", outputFile)) | ||
| commandLine.append(contentsOf: inputs.map { .path($0.file) }) | ||
| return try getToolPath(.staticLinker) | ||
| } | ||
| } | ||
| } | ||
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
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.