Skip to content

Instantly share code, notes, and snippets.

@erica
Last active November 1, 2016 06:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/6c3892fe603659b6e5ab to your computer and use it in GitHub Desktop.
Save erica/6c3892fe603659b6e5ab to your computer and use it in GitHub Desktop.

Expanding Build Configuration Tests for Simulator and Device targets

  • Proposal: SE-00XX
  • Author(s): Erica Sadun
  • Status: TBD
  • Review manager: TBD

Introduction

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.

Motivation

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

Detail 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.

Impact 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.

Current Art

Swift currently supports the following configuration tests:

  • The literals true and false
  • The os() function that tests for OSX, iOS, watchOS, tvOS, Linux, Windows, and FreeBSD
  • The arch() function that tests for x86_64, arm, arm64, i386, powerpc64, and powerpc64le
  • The swift() function that tests for specific Swift language releases, e.g. swift(>=2.2)

Alternatives Considered

There are no alternatives considered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment