Skip to content

Handle -version argument #49

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

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ extension Driver {
return
}

if parsedOptions.hasArgument(.version) || parsedOptions.hasArgument(.version_) {
// Follow gcc/clang behavior and use stdout for --version and stderr for -v.
try printVersion(outputStream: &stdoutStream)
return
}
if parsedOptions.hasArgument(.v) {
try printVersion(outputStream: &stderrStream)
}

// Plan the build.
let jobs = try planBuild()
if jobs.isEmpty { return }
Expand Down Expand Up @@ -632,6 +641,11 @@ extension Driver {

return try exec(path: tool, args: arguments)
}

private func printVersion<S: OutputByteStream>(outputStream: inout S) throws {
outputStream.write(try Process.checkNonZeroExit(args: swiftCompiler.pathString, "--version"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically in line with what we're doing today, but it reminds me that we shouldn't probably virtualize standard output / standard error for library clients. Constructing a Driver instance that happens to print help or version information shouldn't pollute the clients standard output / standard error: they should be able to capture that output themselves. Similarly, I could imagine clients wanting to handle process execution themselves rather than having the Driver initializer directly invoking processes. (Again, for another PR. This follows the precedent already established)

outputStream.flush()
}
}

extension Diagnostic.Message {
Expand Down