The documentation in TargetConditionals.h
has this diagram (it seems, as of 2022; any platform):
+---------------------------------------------------------------------+
| TARGET_OS_MAC |
| +---+ +-----------------------------------------------+ +---------+ |
| | | | TARGET_OS_IPHONE | | | |
| | | | +---------------+ +----+ +-------+ +--------+ | | | |
| | | | | IOS | | | | | | | | | | |
| |OSX| | |+-------------+| | TV | | WATCH | | BRIDGE | | |DRIVERKIT| |
| | | | || MACCATALYST || | | | | | | | | | |
| | | | |+-------------+| | | | | | | | | | |
| | | | +---------------+ +----+ +-------+ +--------+ | | | |
| +---+ +-----------------------------------------------+ +---------+ |
+---------------------------------------------------------------------+
This tells us:
⚠️ But wait! ⚠️
I got that from the iOS 14 (macOS 11, watchOS 7) SDK. If I look back into the iOS 13 (macOS 10.15, watchOS 6) SDK, I see this:
+----------------------------------------------------------------+
| TARGET_OS_MAC |
| +---+ +-----------------------------------------------------+ |
| | | | TARGET_OS_IPHONE | |
| |OSX| | +-----+ +----+ +-------+ +--------+ +-------------+ | |
| | | | | IOS | | TV | | WATCH | | BRIDGE | | MACCATALYST | | |
| | | | +-----+ +----+ +-------+ +--------+ +-------------+ | |
| +---+ +-----------------------------------------------------+ |
+----------------------------------------------------------------+
Notably, TARGET_OS_DRIVERKIT
is new in 14, and TARGET_OS_MACCATALYST
is inside IOS
now. This tells us that compiling against the iOS 14 / macOS 11 SDK can break some C code written for iOS 13 / macOS 10.15, if it assumes that TARGET_OS_MACCATALYST
and TARGET_OS_IOS
are completely separate.
There's More!
Additionally, these are defined:
TARGET_OS_SIMULATOR
is just for iOS, tvOS, and watchOS simulators. You can further refine this using the above #define
s
TARGET_OS_WIN32
is in case you wanna use Apple's SDKs to make Windows apps. I don't personally know of any other than Apple's own (like iTunes, Safari, and QuickTime). This might become useful now that Swift has Windows support, if you want to take existing Objective-C code with you.
TARGET_OS_UNIX
is for non-Apple UNIX systems
And these are deprecated, and should not be used anymore. That said, you might find them in code you have to maintain, so here's what they meant:
TARGET_IPHONE_SIMULATOR
used to mean the iPhoneOS simulator. Use TARGET_OS_SIMULATOR
instead (along with TARGET_OS_IOS
to target only iOS simulators)
TARGET_OS_EMBEDDED
used to mean iOS, tvOS, and watchOS non-simulated devices. Use the standard OS targets instead.
TARGET_OS_NANO
probably used to mean iPod Nano (I can't find any historical usage online). Apple advises to use TARGET_OS_WATCH
instead.
Something else to note is that the TargetConditionals.h
which is used in swift-corelibs-foundation is significantly different, and includes #define
s for Android, Cygwin, and other not-explicitly-supported-but-they-technically-work platforms.
I'm not entirely sure what to make of this. I would guess it's for compiling the Swift Foundation framework, and not for consuming it, since Swift doesn't consume #define
s.