Skip to content

Commit 61c83d2

Browse files
committed
[Driver] Switch -print-target-triple to -print-target-info and add more info.
Rather than only emitting the target triple, provide additional information about that particular target, including the module triple (i.e., what file names will be used for Swift modules for that triple), the runtime compatibility version if there is one, and whether linking with rpaths is required for the standard library and other libraries shipped with Swift. Encode this as JSON so we can extend it in the future. For now, it looks like this: ``` { "target": { "triple": "arm64-apple-ios12.0", "moduleTriple": "arm64-apple-ios", "swiftRuntimeCompatibilityVersion": "5.0", "librariesRequireRPath": true } } ``` Which you can deserialize into a TargetInfo instance as defined below: ``` struct Target: Codable { /// The target triple. var triple: String /// The triple used for module file names. var moduleTriple: String /// If this platform provides the Swift runtime, the Swift language version /// with which that runtime is compatible. var swiftRuntimeCompatibilityVersion: String? /// Whether linking against the Swift libraries requires the use of rpaths. var librariesRequireRPath: Bool } struct TargetInfo: Codable { var target: Target } ``` Implements rdar://problem/47095159.
1 parent cfe25ea commit 61c83d2

File tree

9 files changed

+70
-27
lines changed

9 files changed

+70
-27
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class FrontendOptions {
249249

250250
/// Indicates that the frontend should print the target triple and then
251251
/// exit.
252-
bool PrintTargetTriple = false;
252+
bool PrintTargetInfo = false;
253253

254254
/// Should we sort SIL functions, vtables, witness tables, and global
255255
/// variables by name when we print it out. This eases diffing of SIL files.

include/swift/Option/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +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">,
887+
def print_target_info : Flag<["-"], "print-target-info">,
888888
Flags<[FrontendOption]>,
889-
HelpText<"Generate code for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
889+
HelpText<"Print target information for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
890890

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

lib/Driver/Driver.cpp

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

2125-
if (Args.hasArg(options::OPT_print_target_triple)) {
2125+
if (Args.hasArg(options::OPT_print_target_info)) {
21262126
SmallVector<const char *, 5> commandLine;
21272127
commandLine.push_back("-frontend");
2128-
commandLine.push_back("-print-target-triple");
2128+
commandLine.push_back("-print-target-info");
21292129
if (const Arg *TargetArg = Args.getLastArg(options::OPT_target)) {
21302130
commandLine.push_back("-target");
21312131
commandLine.push_back(TargetArg->getValue());

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ 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;
107+
if (Args.hasArg(OPT_print_target_info)) {
108+
Opts.PrintTargetInfo = true;
109109
}
110110

111111
if (const Arg *A = Args.getLastArg(OPT_verify_generic_signatures)) {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 35 additions & 2 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"
@@ -1863,6 +1864,38 @@ createJSONFixItDiagnosticConsumerIfNeeded(
18631864
});
18641865
}
18651866

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

18671900
int swift::performFrontend(ArrayRef<const char *> Args,
18681901
const char *Argv0, void *MainAddr,
@@ -1987,8 +2020,8 @@ int swift::performFrontend(ArrayRef<const char *> Args,
19872020
return finishDiagProcessing(0);
19882021
}
19892022

1990-
if (Invocation.getFrontendOptions().PrintTargetTriple) {
1991-
llvm::outs() << Invocation.getLangOptions().Target.getTriple() << "\n";
2023+
if (Invocation.getFrontendOptions().PrintTargetInfo) {
2024+
printTargetInfo(Invocation, llvm::outs());
19922025
return finishDiagProcessing(0);
19932026
}
19942027

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

test/Driver/print_target_triple.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/Driver/print_target_triple_macos.swift

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)