Platform-specific code

Platform-specific code

On the PekoScript side

platform macos {
    // compiled only for macOS
}

platform macos | linux {
    // several targets, separated by |
}

arch arm {
    // compiled only for arm
}

Platform names are macos, windows, linux, ios, and android. Architectures are arm and x86_64. There is no else form and no negation.

Two things to know.

The excluded branch is not type-checked at all. A platform windows { } block is skipped entirely on a macOS build, so type errors, missing symbols, and bad imports inside it surface only when you actually build for Windows. Check each target you support:

peko test src/main.peko --os windows --arch x86_64

An unknown platform name is not an error. It simply never matches, so a typo silently removes the code. There is no diagnostic for this, so spell them carefully.

On the C side

The toolchain defines no Peko-specific macro. Detection uses the standard predefines that come from the target triple:

Macro Target
_WIN32 Windows
__APPLE__ any Apple platform
TARGET_OS_IPHONE iOS, after including TargetConditionals.h
TARGET_OS_OSX macOS, same
__ANDROID__ Android
__linux__ Linux

The Apple split needs the explicit include:

#if defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_IPHONE
/* iOS */
#else
/* macOS */
#endif
#endif

Shipping several implementations

Every listed source is compiled for every target. There is no per-platform source list. So a file that serves one platform must compile to nothing elsewhere, by wrapping its entire body:

/* The whole file compiles to nothing off Android. */
#if defined(__ANDROID__)
...
#endif

The usual shape is one file per platform plus a fallback, all listed together:

sources = [
    "c/dialog/peko_dialog_apple.m",
    "c/dialog/peko_dialog_windows.c",
    "c/dialog/peko_dialog_linux.c",
    "c/dialog/peko_dialog_fallback.c",
]

Each guards itself, and the fallback provides a stub so the symbol always resolves.

Keep the per-OS link requirements in the package that needs them, so a command-line binary never links a GUI framework it does not use.