Skip to content

Commit 179e3c2

Browse files
authored
Merge pull request swiftlang#28602 from DougGregor/print-target-triple
[Driver/Frontend] Add -print-target-triple
2 parents e1e7a3f + 61c83d2 commit 179e3c2

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-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 PrintTargetInfo = 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
@@ -893,6 +893,9 @@ def target : Separate<["-"], "target">,
893893
HelpText<"Generate code for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
894894
def target_legacy_spelling : Joined<["--"], "target=">,
895895
Flags<[FrontendOption]>, Alias<target>;
896+
def print_target_info : Flag<["-"], "print-target-info">,
897+
Flags<[FrontendOption]>,
898+
HelpText<"Print target information for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
896899

897900
def target_cpu : Separate<["-"], "target-cpu">, Flags<[FrontendOption, ModuleInterfaceOption]>,
898901
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
@@ -2124,6 +2124,31 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
21242124
std::end(commandArgs));
21252125
}
21262126

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

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_info)) {
108+
Opts.PrintTargetInfo = 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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "swift/Basic/JSONSerialization.h"
4343
#include "swift/Basic/LLVMContext.h"
4444
#include "swift/Basic/LLVMInitialize.h"
45+
#include "swift/Basic/Platform.h"
4546
#include "swift/Basic/PrettyStackTrace.h"
4647
#include "swift/Basic/SourceManager.h"
4748
#include "swift/Basic/Statistic.h"
@@ -1866,6 +1867,38 @@ createJSONFixItDiagnosticConsumerIfNeeded(
18661867
});
18671868
}
18681869

1870+
/// Print information about the selected target in JSON.
1871+
static void printTargetInfo(CompilerInvocation &invocation,
1872+
llvm::raw_ostream &out) {
1873+
out << "{\n";
1874+
1875+
// Target information.
1876+
auto &langOpts = invocation.getLangOptions();
1877+
out << " \"target\": {\n";
1878+
1879+
out << " \"triple\": \"";
1880+
out.write_escaped(langOpts.Target.getTriple());
1881+
out << "\",\n";
1882+
1883+
out << " \"moduleTriple\": \"";
1884+
out.write_escaped(getTargetSpecificModuleTriple(langOpts.Target).getTriple());
1885+
out << "\",\n";
1886+
1887+
if (auto runtimeVersion = getSwiftRuntimeCompatibilityVersionForTarget(
1888+
langOpts.Target)) {
1889+
out << " \"swiftRuntimeCompatibilityVersion\": \"";
1890+
out.write_escaped(runtimeVersion->getAsString());
1891+
out << "\",\n";
1892+
}
1893+
1894+
out << " \"librariesRequireRPath\": "
1895+
<< (tripleRequiresRPathForSwiftInOS(langOpts.Target) ? "true" : "false")
1896+
<< "\n";
1897+
1898+
out << " }\n";
1899+
1900+
out << "}\n";
1901+
}
18691902

18701903
int swift::performFrontend(ArrayRef<const char *> Args,
18711904
const char *Argv0, void *MainAddr,
@@ -1990,6 +2023,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
19902023
return finishDiagProcessing(0);
19912024
}
19922025

2026+
if (Invocation.getFrontendOptions().PrintTargetInfo) {
2027+
printTargetInfo(Invocation, llvm::outs());
2028+
return finishDiagProcessing(0);
2029+
}
2030+
19932031
if (Invocation.getFrontendOptions().RequestedAction ==
19942032
FrontendOptions::ActionType::NoneAction) {
19952033
Instance->getDiags().diagnose(SourceLoc(),

test/Driver/print_target_info.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %swift_driver -print-target-info -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s
2+
// RUN: %target-swift-frontend -print-target-info -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s
3+
4+
// RUN: %swift_driver -print-target-info -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s
5+
// RUN: %target-swift-frontend -print-target-info -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s
6+
7+
// CHECK-IOS: "target": {
8+
// CHECK-IOS: "triple": "arm64-apple-ios12.0",
9+
// CHECK-IOS: "moduleTriple": "arm64-apple-ios",
10+
// CHECK-IOS: "swiftRuntimeCompatibilityVersion": "5.0",
11+
// CHECK-IOS: "librariesRequireRPath": true
12+
// CHECK-IOS: }
13+
14+
// CHECK-LINUX: "target": {
15+
// CHECK-LINUX: "triple": "x86_64-unknown-linux",
16+
// CHECK-LINUX: "moduleTriple": "x86_64-unknown-linux",
17+
// CHECK-LINUX: "librariesRequireRPath": false
18+
// CHECK-LINUX: }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test that -print-target-info 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-info | %FileCheck %s
6+
// RUN: %target-swift-frontend -print-target-info | %FileCheck %s
7+
8+
// REQUIRES: OS=macosx
9+
10+
// CHECK: "triple": "x86_64-apple-macosx

0 commit comments

Comments
 (0)