- Proposal: SE-00XX
- Author(s): Erica Sadun
- Status: TBD
- Review manager: TBD
This proposal introduces a build configuration test to differentiate device and simulator builds. This test represent a common categorization requirement for Metal, Keychain, and AVFoundation Camera code.
This proposal was discussed on-list in the Expanding Build Configuration Tests for Simulator and Device targets thread.
Swift target detection is unnecessarily complicated and error prone as they're based on testing for target architectures that may potentially change over time. Making simulator detection dependent on a mismatch between architecture and the operating system shows an obvious gap in the current build configuration test suite.
// Test for a simulator destination
#if (arch(i386) || arch(x86_64)) && (!os(OSX))
print("Simulator")
#else
print("Device")
#endif
// More restrictive test for iOS simulator
// Adjust the os test for watchOS, tvOS
#if (arch(i386) || arch(x86_64)) && os(iOS)
// iOS simulator code
#endif
This proposal adds #if target(simulator)
and #if target(device)
to distinguish whether
application code is compiled to run in a simulated environment or on a device. Code
running on desktop systems are considered to be on-device.
This proposal does not impact existing code. As it is unnecessarily complicated to craft fixits that look for common test patterns, I recommend only that the new configurations be mentioned in release notes and added to the Using Swift with Cocoa and Objective-C document.
Swift currently supports the following configuration tests:
- The literals
true
andfalse
- The
os()
function that tests forOSX, iOS, watchOS, tvOS, Linux, Windows, and FreeBSD
- The
arch()
function that tests forx86_64, arm, arm64, i386, powerpc64, and powerpc64le
- The
swift()
function that tests for specific Swift language releases, e.g.swift(>=2.2)
There are no alternatives considered.