Skip to content

Commit cfe25ea

Browse files
committed
[Driver/Frontend] Add -print-target-triple
Add a -print-target-triple command line option to the Swift frontend and driver to allow other tools (e.g., SwiftPM) to query the host triple as it is understood by the Swift compiler. This follows the precedent set by Clang. Implements rdar://problem/57434967.
1 parent 345d1f4 commit cfe25ea

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ class FrontendOptions {
247247
/// Indicates whether full help (including "hidden" options) should be shown.
248248
bool PrintHelpHidden = false;
249249

250+
/// Indicates that the frontend should print the target triple and then
251+
/// exit.
252+
bool PrintTargetTriple = false;
253+
250254
/// Should we sort SIL functions, vtables, witness tables, and global
251255
/// variables by name when we print it out. This eases diffing of SIL files.
252256
bool EmitSortedSIL = false;

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,9 @@ def target : Separate<["-"], "target">,
884884
HelpText<"Generate code for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
885885
def target_legacy_spelling : Joined<["--"], "target=">,
886886
Flags<[FrontendOption]>, Alias<target>;
887+
def print_target_triple : Flag<["-"], "print-target-triple">,
888+
Flags<[FrontendOption]>,
889+
HelpText<"Generate code for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
887890

888891
def target_cpu : Separate<["-"], "target-cpu">, Flags<[FrontendOption, ModuleInterfaceOption]>,
889892
HelpText<"Generate code for a particular CPU variant">;

lib/Driver/Driver.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,31 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
21222122
std::end(commandArgs));
21232123
}
21242124

2125+
if (Args.hasArg(options::OPT_print_target_triple)) {
2126+
SmallVector<const char *, 5> commandLine;
2127+
commandLine.push_back("-frontend");
2128+
commandLine.push_back("-print-target-triple");
2129+
if (const Arg *TargetArg = Args.getLastArg(options::OPT_target)) {
2130+
commandLine.push_back("-target");
2131+
commandLine.push_back(TargetArg->getValue());
2132+
}
2133+
2134+
std::string executable = getSwiftProgramPath();
2135+
2136+
sys::TaskQueue queue;
2137+
queue.addTask(executable.c_str(), commandLine);
2138+
queue.execute(nullptr,
2139+
[](sys::ProcessId PID, int returnCode,
2140+
StringRef output, StringRef errors,
2141+
sys::TaskProcessInformation ProcInfo,
2142+
void *unused) -> sys::TaskFinishedResponse {
2143+
llvm::outs() << output;
2144+
llvm::errs() << errors;
2145+
return sys::TaskFinishedResponse::ContinueExecution;
2146+
});
2147+
return false;
2148+
}
2149+
21252150
return true;
21262151
}
21272152

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ bool ArgsToFrontendOptionsConverter::convert(
104104
Opts.IgnoreSwiftSourceInfo |= Args.hasArg(OPT_ignore_module_source_info);
105105
computeHelpOptions();
106106

107+
if (Args.hasArg(OPT_print_target_triple)) {
108+
Opts.PrintTargetTriple = true;
109+
}
110+
107111
if (const Arg *A = Args.getLastArg(OPT_verify_generic_signatures)) {
108112
Opts.VerifyGenericSignaturesInModule = A->getValue();
109113
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
19871987
return finishDiagProcessing(0);
19881988
}
19891989

1990+
if (Invocation.getFrontendOptions().PrintTargetTriple) {
1991+
llvm::outs() << Invocation.getLangOptions().Target.getTriple() << "\n";
1992+
return finishDiagProcessing(0);
1993+
}
1994+
19901995
if (Invocation.getFrontendOptions().RequestedAction ==
19911996
FrontendOptions::ActionType::NoneAction) {
19921997
Instance->getDiags().diagnose(SourceLoc(),

test/Driver/print_target_triple.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %swift_driver -print-target-triple -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s
2+
// RUN: %target-swift-frontend -print-target-triple -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s
3+
4+
// RUN: %swift_driver -print-target-triple -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s
5+
// RUN: %target-swift-frontend -print-target-triple -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s
6+
7+
// CHECK-IOS: arm64-apple-ios12.0
8+
// CHECK-LINUX: x86_64-unknown-linux
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test that -print-target-triple infers the host OS when that host OS is macOS
2+
//
3+
// We could duplicate this test for other host platforms.
4+
5+
// RUN: %swift_driver -print-target-triple | %FileCheck %s
6+
// RUN: %target-swift-frontend -print-target-triple | %FileCheck %s
7+
8+
// REQUIRES: OS=macosx
9+
10+
// CHECK: x86_64-apple-macosx

0 commit comments

Comments
 (0)