Skip to content

Instantly share code, notes, and snippets.

@erica
Last active December 11, 2018 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/a87c7e203dab5abab006 to your computer and use it in GitHub Desktop.
Save erica/a87c7e203dab5abab006 to your computer and use it in GitHub Desktop.

Expanding Build Configuration Tests

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

Introduction

Expanding the configuration test suite to include Darwin support (to test for Apple platforms) was first introduced on the Swift-Evolution list by Lily Ballard. Her idea was greeted warmly and quickly expanded upon by developers looking to introduce the kinds of conditional configuration they're used to in other languages. This proposal introduces a subset of the configurations requested on that thread.

SE-0020 added #if swift to enable code to compile with specific versions of the Swift language. This proposal introduces several further compile-time configuration, adding configuration tests for x, y, z, and w.

This proposal was discussed on-list in the thread.

Current 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)

Motivation

As Swift expands to a growing developer base and to new platforms, the existing suite of configuration tests begins to show strain. Shortcuts taking advantage of current limitations introduce fragility. For example, testing #if os(iOS) || os(OSX) || os(watchOS) || os(tvOS) may miss new platform targets and #if !os(Linux) may introduce errors should Swift extend to other non-Apple platforms, such as Windows.

Swift also misses a built-in #if debug test. Although you can specify command-line flags using -D <#flag#> (e.g. -D debug) and test in-code (#if debug), there's no consistent system-supplied way to differentiate code meant only for debug builds.

This proposal introduces a minimal set of utility configuration tests.

Detail Design

  • #if platform(Apple) tests for platform families, including Apple, Linux, Unix, Windows
  • #if imports(UIKit) tests for module support, differentiating, for example, Mac code from iOS and tvOS code.
  • #if target(simulator) and #if target(device) represent a common categorization requirement, such as Metal, Keychain, and AVFoundation Camera code.
  • #if bitwidth(32) and #if bitwidth(64) tests for 32 and 64 bit architecture families.
  • #if endian(big) and #if endian(little) tests for big and little endianness.
  • #if config(debug) tests for debug builds.

Note

Joe Groff writes, "We specifically avoided making debug/release an #if condition because we considered #if to be the wrong point at which to start conditionalizing code generation for assertions. Though the final executable image's behavior is unavoidably dependent on whether asserts are enabled, we didn't want the SIL for inlineable code to be, since that would mean libraries with inlineable code would need to ship three times the amount of serialized SIL to support the right behavior in -Onone, -O, and -Ounchecked builds. Instead, the standard library has some hidden helper functions, _isDebugAssertConfiguration, _isReleaseAssertConfiguration, and _isFastAssertConfiguration, which are guaranteed to be constant-folded away before final code generation."

Not included in this proposal at this time: "Is the target built for running tests", #if destination() tests for specific destinations, #if destination(BSD), #if destination(Ubuntu, >=14.04) to establish common-ground calls for, for example, BSD destinations or specific Linux distros, or "Has a module been imported elsewhere in my project?, e.g. #if imported(UIKit)"

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