# \`#if os(Darwin)\`, a shorthand for checking for Darwin platforms

**URL:** <https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146>\
**Category:** Discussion\
**Created:** [June 15, 2022, 8:30am UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146 "2022-06-15T08:30:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 15, 2022, 8:30am UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/1 "2022-06-15T08:30:50Z")

</div>

I've made a working implementation [here](https://github.com/apple/swift/pull/59426), with CI tests passing & I've tested locally.

Previously discussed in 2016 in [this thread](https://forums.swift.org/t/idea-support-if-os-darwin-as-shorthand-for-os-ios-os-osx-os-watchos-os-tvos/1493).

The idea is basically just adding one `#if os` directive but all inclusive for Darwin platforms. Currently, to check for Darwin platforms, you have to type out a lengthy statement:

```swift
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
// Darwin code
#else
// Non Darwin code
#endif

```

You will see stuff like that very frequently in Apple's own Swift code, the issue is that this is _not_ future-proof for any future Darwin platforms, and quite lengthy to type out & read, which is why I believe this would be better:

```swift
#if os(Darwin)
// Darwin code
#else
// Non Darwin code
#endif

```

I've also considered a `@available` support, however, honestly, that's not realistic as most developers don't know the Darwin version number for specific iOS/macOS/tvOS/watchOS releases.

As mentioned above in this post, I've made a working [implementation](https://github.com/apple/swift/pull/59426) of this, and have tested successfully with CI and locally.

Please let me know what you think.

---

<div class="post-metadata">

**Author:** ![mickeyl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mickeyl/32/12258_2.png) [@mickeyl](https://forums.swift.org/u/mickeyl)\
**Post date:** [June 15, 2022, 8:40am UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/2 "2022-06-15T08:40:20Z")

</div>

Excellent. I have been missing a simple way to distinguish APPLE from !APPLE like this for ages. In the meantime I have been using

`#if canImport(ObjectiveC)`

instead, which works, but is not quite the correct semantics.

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 15, 2022, 8:48am UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/3 "2022-06-15T08:48:41Z")

</div>

That's one of the reasons I made that impl, I've seen people do stuff such as

- `#if _runtime(_ObjC)`
- `#if canImport(ObjectiveC)`
- `#if canImport(Darwin)`  
Which, I don't believe it would be good practice to distinguish Apple platforms from non-Apple platforms using those methods, hopefully `#if os(Darwin)` changes that if it does pass.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [June 15, 2022, 11:35am UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/4 "2022-06-15T11:35:18Z")

</div>

I'll admit to using canImport:

```swift
#if canImport(Darwin)

import Darwin
typealias DirectoryStreamPointer = UnsafeMutablePointer<DIR>?

#elseif canImport(Glibc)

import Glibc
typealias DirectoryStreamPointer = OpaquePointer?

#else

#error("Unsupported Platform")

#endif

```

I think proper #if support would be nicer, but I am a bit torn with this platform specific conditionals that seem to be in vogue - I was raised with conditionals on features ('have pthreads', 'have kqueue', 'have epoll' etc) - that might be different for the same platform depending on version, but that is a different aspect...)

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [June 16, 2022, 5:56pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/5 "2022-06-16T17:56:16Z")

</div>

We’ve generally tried to make sure that `#if os(osName)` options are mutually exclusive—that is, that there’s only one `osName` that will be true at a time—to avoid the confusing tangle of target defines that we see in Objective-C, where for instance `TARGET_OS_MAC` is true on iOS. I’m not sure if we’ve actually held the line on that, though.

Having said that, I think it’d be wonderful to have this functionality—just perhaps not in `os(…)`.

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 16, 2022, 5:57pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/6 "2022-06-16T17:57:42Z")

</div>

> [@beccadax](#):
>
> Having said that, I think it’d be wonderful to have this functionality—just perhaps not in `os(…)`.

Actually, my first thought on how to implement this was to introduce something like `#if platform`, with Darwin and Unix being options, though, to be honest, I couldn't figure it out.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 16, 2022, 7:48pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/7 "2022-06-16T19:48:41Z")

</div>

Thinking out loud here—what about `#if kernel`?

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [June 16, 2022, 7:56pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/8 "2022-06-16T19:56:48Z")

</div>

Hmm, I'm torn on this. On the one hand, this makes sense for the Darwin based platforms, and also would fix up the `os(Android) || os(Linux)` cases. On the other hand, what about baremetal platforms (i.e. no kernel) and platforms such as cygwin? Would it make sense to return "NT" as the kernel even though the user may expect Linux? Another thing to consider is something like personalities.

As a crappy compromise, something like `#if vendor(Apple)` could work to group together the Apple OSes perhaps?

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 16, 2022, 7:58pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/9 "2022-06-16T19:58:33Z")

</div>

> [@compnerd](#):
>
> As a crappy compromise, something like `#if vendor(Apple)` could work to group together the Apple OSes perhaps?

I don’t get it, why the new `vendor` statement? `#if os(Darwin)` makes most sense here, it works with the existing `#if os` statements we have, don’t get why `#if vendor` would be better

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 16, 2022, 8:00pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/10 "2022-06-16T20:00:19Z")

</div>

> [@compnerd](#):
>
> On the other hand, what about baremetal platforms (i.e. no kernel) and platforms such as cygwin? Would it make sense to return "NT" as the kernel even though the user may expect Linux?

I'd imagine we could do `kernel(none) && os(...)` and `kernel(nt) && targetEnvironment(cygwin)`?

> [@NSXPCConnection](#):
>
> I don’t get it, why the new `vendor` statement? `#if os(Darwin)` makes most sense here, it works with the existing `#if os` statements we have, don’t get why `#if vendor` would be better

To maintain `os` options as mutually exclusive.

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 16, 2022, 8:01pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/11 "2022-06-16T20:01:24Z")

</div>

I kind of like the idea of `#if kernel`, though some may argue it’s out of scope for the Swift compiler to be giving that information..

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 16, 2022, 8:03pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/12 "2022-06-16T20:03:06Z")

</div>

> [@xwu](#):
>
> To maintain `os` options as mutually exclusive.

If this is a tangible goal which is really _that_ important, we could instead do `#if platform` and provide options to check for Darwin, Unix, or BSD in general from there, though that may confuse people due to the wording being similar to `#if os`.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [June 16, 2022, 8:07pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/13 "2022-06-16T20:07:52Z")

</div>

> [@xwu](#):
>
> I'd imagine we could do `kernel(none) && os(...)` and `kernel(nt) && targetEnvironment(cygwin)`?

I think that this could work, but I do worry about the complexity. I may want to build the code for BSD to run on Linux with the BSD personality. That is to say, I expect most users would not expect the following behavior:

```swift
#if os(Linux) && kernel(BSD)
// this is valid
#endif

```

> [@NSXPCConnection](#):
>
> If this is a tangible goal which is really _that_ important, we could instead do `#if platform` and provide options to check for Darwin, Unix, or BSD in general from there, though that may confuse people due to the wording being similar to `#if os`.

I think that `platform` works better than `kernel` (cygwin and MinGW are more platforms than kernels or OSes, it also covers WASI well IMO). I'm not sure I understand the "Unix" category. Would that be SCO Unix? SystemV Unix? All the Unix derivates?

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 16, 2022, 8:14pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/14 "2022-06-16T20:14:34Z")

</div>

> [@compnerd](#):
>
> I think that `platform` works better than `kernel` (cygwin and MinGW are more platforms than kernels or OSes, it also covers WASI well IMO). I'm not sure I understand the "Unix" category. Would that be SCO Unix? SystemV Unix? All the Unix derivates?

That’d be up for discussion if such an idea does get far. With that being said, I’m still against not using `#if os` for `Darwin`, I do get a goal was to keep `#if os` options mutually exclusive however `#if os` is already familiar to most Swift programmers, especially ones writing cross platform code.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [June 16, 2022, 8:55pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/15 "2022-06-16T20:55:28Z")

</div>

> [@hassila](#):
>
> I'll admit to using canImport:
> 
> ```swift
> #if canImport(Darwin)
> 
> import Darwin
> typealias DirectoryStreamPointer = UnsafeMutablePointer<DIR>?
> 
> #elseif canImport(Glibc)
> 
> import Glibc
> typealias DirectoryStreamPointer = OpaquePointer?
> 
> #else
> 
> #error("Unsupported Platform")
> 
> #endif
> 
> ```

I actually think this is correct. If you're trying to guard `import Darwin` and uses of the `Darwin` module, then isn't `canImport(Darwin)` expressing precisely the thing you want?

What exactly is the API surface of the `os(Darwin)` SDK? Can I assume _only_ that it includes the `Darwin` module? If so, is it not a synonym for `canImport(Darwin)`?

Or does `os(Darwin)` also imply the availability of other Apple frameworks, such as SwiftUI? What about frameworks that are not available on every Apple platform, such as AppKit and UIKit, or the recently-announced ClockKit?

Correct me if I'm wrong, but I don't believe Apple has ever defined a set of frameworks that all Darwin-family platforms will support. Pretty much the only thing that seems reasonable to assume is that `import Darwin` will work on a Darwin platform (kind of, by definition). IMO, it's up to Apple's platform team whether or not they want to introduce an expanded concept of a "Darwin OS" which includes more than just that one module, and which other modules `os(Darwin)` should include.

On a related note, hopefully one day we'll add some kind of `CStdlib` module, which will cut down the boilerplate for cross-platform code. So you could just `import CStdlib`, and handle the platform differences as and where they occur.

> [@Pitch: The CStdlib module](https://forums.swift.org/t/pitch-the-cstdlib-module/51373):
>
> I would like to pitch a new module offered by Swift by default. The module would re-export the correct module that contains the POSIX or POSIX-like C standard library for the current platform, if any; it would not be imported by default, but would allow "reasonably cross-platform" code to avoid using lengthy #if canImport(…) chains to gain access to all possible stdlibs, given that they have different names on different OSs. For example, the module may be named CStdlib. It would behave identica…

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 16, 2022, 8:58pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/16 "2022-06-16T20:58:27Z")

</div>

> [@Karl](#):
>
> What exactly is the API surface of the `os(Darwin)` SDK? Can I assume _only_ that it includes the `Darwin` module? If so, is it not a synonym for `canImport(Darwin)`?

It’s an alias to check for the 4 Darwin platforms. I think that’s reasonable for anyone to think.

It is the very exact same as

```swift
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)

```

Which, if you read apple’s own cross-platform Swift code, you’ll find is used _extensively_, `#if os(Darwin)`, in the implementation I have made, is the exact same.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [June 16, 2022, 9:09pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/17 "2022-06-16T21:09:30Z")

</div>

How much of that code predates `canImport`?

Are developers choosing not to use `canImport`? Is it proving to be inadequate in some way?

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [June 16, 2022, 9:15pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/18 "2022-06-16T21:15:55Z")

</div>

> [@Karl](#):
>
> How much of that code predates `canImport`

None.

> [@Karl](#):
>
> Are developers choosing not to use `canImport`

For this purpose? Seems so.

Regardless of all these, I still think it would be just better to have the Darwin alias to make existing code using the `#if os` statements more readable

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [June 16, 2022, 9:38pm UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/19 "2022-06-16T21:38:08Z")

</div>

Is it possible that is just a style choice? That it isn't due to a functionality gap in `canImport`?

If `canImport` really is deficient in some way, it would be nice to know more about that. At least for me, I write plenty of cross-platform code, and `canImport(Darwin)` seems to be working fine.

I'd be interested to know which limitations developers are encountering. I'm not sure how we can judge whether `os(Darwin)` would be better, without learning more about why developers aren't using the existing solution. Currently it seems we're just guessing at why developers happened to write a thing in a certain way.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [June 17, 2022, 8:25am UTC](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146/20 "2022-06-17T08:25:13Z")

</div>

I do agree it was correct, that was a bad example - I picked the first one I had - here's perhaps a better example:

```swift
    public static var defaultPluginExtension: String {
        #if canImport(Darwin)
            return "dylib"
        #elseif canImport(Glibc)
            return "so"
        #else
            #error("Unsupported Platform")
        #endif
    }

```

There it feels a bit strange to use canImport, but it's the best mechanism I found.

[Next page](https://forums.swift.org/t/if-os-darwin-a-shorthand-for-checking-for-darwin-platforms/58146.md?page=2)
