Skip to content

Commit 8339f32

Browse files
committed
[Frontend] Infer simulator environment in the frontend, too.
Some code paths that see target triples go through the frontend without seeing the driver. Therefore, perform the same "simulator" inference for x86 iOS/tvOS/watchOS triples also in the frontend, to ensure that we remain compatible. Also make sure that -print-target-info performs the appropriate adjustment.
1 parent 856855d commit 8339f32

File tree

7 files changed

+58
-20
lines changed

7 files changed

+58
-20
lines changed

include/swift/AST/DiagnosticsDriver.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ ERROR(error_unexpected_input_file,none,
8484

8585
ERROR(error_unknown_target,none,
8686
"unknown target '%0'", (StringRef))
87-
WARNING(warning_inferred_simulator_target,none,
88-
"inferring simulator environment for target '%0'; "
89-
"use '-target %1' instead", (StringRef, StringRef))
9087

9188
ERROR(error_framework_bridging_header,none,
9289
"using bridging headers with framework targets is unsupported", ())

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ ERROR(error_unsupported_target_arch, none,
6161
ERROR(error_unsupported_opt_for_target, none,
6262
"unsupported option '%0' for target '%1'", (StringRef, StringRef))
6363

64+
WARNING(warning_inferred_simulator_target,none,
65+
"inferring simulator environment for target '%0'; "
66+
"use '-target %1' instead", (StringRef, StringRef))
67+
6468
ERROR(error_argument_not_allowed_with, none,
6569
"argument '%0' is not allowed with '%1'", (StringRef, StringRef))
6670

include/swift/Basic/Platform.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ namespace swift {
4646
/// Returns true if the given triple represents a macCatalyst environment.
4747
bool tripleIsMacCatalystEnvironment(const llvm::Triple &triple);
4848

49+
/// Determine whether the triple infers the "simulator" environment.
50+
bool tripleInfersSimulatorEnvironment(const llvm::Triple &triple);
51+
4952
/// Returns true if the given -target triple and -target-variant triple
5053
/// can be zippered.
5154
bool triplesAreValidForZippering(const llvm::Triple &target,

lib/Basic/Platform.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ bool swift::tripleIsMacCatalystEnvironment(const llvm::Triple &triple) {
3737
triple.getEnvironment() == llvm::Triple::MacABI;
3838
}
3939

40+
bool swift::tripleInfersSimulatorEnvironment(const llvm::Triple &triple) {
41+
switch (triple.getOS()) {
42+
case llvm::Triple::IOS:
43+
case llvm::Triple::TvOS:
44+
case llvm::Triple::WatchOS:
45+
return !triple.isSimulatorEnvironment() &&
46+
(triple.getArch() == llvm::Triple::x86 ||
47+
triple.getArch() == llvm::Triple::x86_64) &&
48+
!tripleIsMacCatalystEnvironment(triple);
49+
50+
default:
51+
return false;
52+
}
53+
}
54+
4055
bool swift::triplesAreValidForZippering(const llvm::Triple &target,
4156
const llvm::Triple &targetVariant) {
4257
// The arch and vendor must match.

lib/Driver/Driver.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -270,27 +270,23 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) {
270270

271271
llvm::Triple target(DefaultTargetTriple);
272272

273+
// Backward compatibility hack: infer "simulator" environment for x86
274+
// iOS/tvOS/watchOS.
275+
if (tripleInfersSimulatorEnvironment(target)) {
276+
// Set the simulator environment.
277+
target.setEnvironment(llvm::Triple::EnvironmentType::Simulator);
278+
279+
auto newTargetTriple = target.normalize();
280+
Diags.diagnose(SourceLoc(), diag::warning_inferred_simulator_target,
281+
DefaultTargetTriple, newTargetTriple);
282+
283+
DefaultTargetTriple = newTargetTriple;
284+
}
285+
273286
switch (target.getOS()) {
274287
case llvm::Triple::IOS:
275288
case llvm::Triple::TvOS:
276289
case llvm::Triple::WatchOS:
277-
// Backward compatibility hack: infer "simulator" environment for x86
278-
// iOS/tvOS/watchOS.
279-
if (!target.isSimulatorEnvironment() &&
280-
(target.getArch() == llvm::Triple::x86 ||
281-
target.getArch() == llvm::Triple::x86_64) &&
282-
!tripleIsMacCatalystEnvironment(target)) {
283-
// Set the simulator environment.
284-
target.setEnvironment(llvm::Triple::EnvironmentType::Simulator);
285-
286-
auto newTargetTriple = target.normalize();
287-
Diags.diagnose(SourceLoc(), diag::warning_inferred_simulator_target,
288-
DefaultTargetTriple, newTargetTriple);
289-
290-
DefaultTargetTriple = newTargetTriple;
291-
}
292-
LLVM_FALLTHROUGH;
293-
294290
case llvm::Triple::Darwin:
295291
case llvm::Triple::MacOSX: {
296292
Optional<llvm::Triple> targetVariant;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,22 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
553553

554554
llvm::Triple Target = Opts.Target;
555555
StringRef TargetArg;
556+
std::string TargetArgScratch;
557+
556558
if (const Arg *A = Args.getLastArg(OPT_target)) {
557559
Target = llvm::Triple(A->getValue());
558560
TargetArg = A->getValue();
561+
562+
// Backward compatibility hack: infer "simulator" environment for x86
563+
// iOS/tvOS/watchOS. The driver takes care of this for the frontend
564+
// most of the time, but loading of old .swiftinterface files goes
565+
// directly to the frontend.
566+
if (tripleInfersSimulatorEnvironment(Target)) {
567+
// Set the simulator environment.
568+
Target.setEnvironment(llvm::Triple::EnvironmentType::Simulator);
569+
TargetArgScratch = Target.str();
570+
TargetArg = TargetArgScratch;
571+
}
559572
}
560573

561574
if (const Arg *A = Args.getLastArg(OPT_target_variant)) {

test/Driver/print_target_info.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// RUN: %swift_driver -print-target-info -target x86_64-apple-macosx10.15 -target-variant x86_64-apple-ios13-macabi | %FileCheck -check-prefix CHECK-ZIPPERED %s
88
// RUN: %target-swift-frontend -print-target-info -target x86_64-apple-macosx10.15 -target-variant x86_64-apple-ios13-macabi | %FileCheck -check-prefix CHECK-ZIPPERED %s
99

10+
// RUN: %swift_driver -print-target-info -target x86_64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS-SIM %s
11+
1012
// CHECK-IOS: "target": {
1113
// CHECK-IOS: "triple": "arm64-apple-ios12.0",
1214
// CHECK-IOS: "unversionedTriple": "arm64-apple-ios",
@@ -50,3 +52,11 @@
5052
// CHECK-ZIPPERED: "swiftRuntimeCompatibilityVersion": "5.1"
5153
// CHECK-ZIPPERED: "librariesRequireRPath": false
5254
// CHECK-ZIPPERED: }
55+
56+
// CHECK-IOS-SIM: "target": {
57+
// CHECK-IOS-SIM: "triple": "x86_64-apple-ios12.0-simulator",
58+
// CHECK-IOS-SIM: "unversionedTriple": "x86_64-apple-ios-simulator",
59+
// CHECK-IOS-SIM: "moduleTriple": "x86_64-apple-ios-simulator",
60+
// CHECK-IOS-SIM: "swiftRuntimeCompatibilityVersion": "5.0",
61+
// CHECK-IOS-SIM: "librariesRequireRPath": true
62+
// CHECK-IOS-SIM: }

0 commit comments

Comments
 (0)