Gist: target.md · GitHub
Feedback, corrections, suggestions requested and welcome. Thanks, -- Erica
Expanding Build Configuration Tests for Simulator and Device targets
Proposal: SE-00XX
Author(s): Erica Sadun <http://github.com/erica>
Status: TBD
Review manager: TBD
<target.md · GitHub
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 <applewebdata://E091D50D-DE8F-40D6-9BFD-E493DCEAA4F4> 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
<target.md · GitHub Design
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.
<target.md · GitHub on Existing Code
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.
<target.md · GitHub Art
Swift currently supports the following configuration tests:
The literals true and false
The os() function that tests for OSX, iOS, watchOS, tvOS, and Linux
The arch() function that tests for x86_64, arm, arm64, and i386
The swift() function that tests for specific Swift language releases, e.g. swift(>=2.2)
<target.md · GitHub Considered
There are no alternatives considered.