Skip to content

macOS: compile Objective-C sources with clang when building with GCC#3073

Open
tomjn wants to merge 1 commit into
beyond-all-reason:masterfrom
tomjn:fix/macos-objc-clang
Open

macOS: compile Objective-C sources with clang when building with GCC#3073
tomjn wants to merge 1 commit into
beyond-all-reason:masterfrom
tomjn:fix/macos-objc-clang

Conversation

@tomjn

@tomjn tomjn commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

What

On macOS, enable the OBJC/OBJCXX languages and — when the C/C++ compiler is GCC — route Objective-C / Objective-C++ (.m / .mm) sources to clang, while the rest of the engine keeps building with GCC.

Why

GCC on macOS cannot target Apple's Objective-C runtime, so any .m / .mm source fails to build (or interoperate) under a GCC toolchain. macOS platform integration in the renderer needs a small amount of Objective-C (e.g. Metal presentation glue), so today the only way to compile those few files is to build the entire engine with AppleClang.

This lets the engine stay on GCC for everything except the handful of Objective-C files. CMake selects a compiler per source language (.m→OBJC, .mm→OBJCXX, .c/.cpp→C/CXX), so Objective-C sources are routed to clang automatically — no per-file or per-target handling. CMake also links libc++ for targets containing those objects automatically.

This is deliberately a standalone, atomic change so that both in-flight macOS renderer efforts (the Mesa/Zink path #2991 and the EGL path #3060 — each of which adds a single .mm) can build on a shared, gcc-primary foundation instead of carrying their own toolchain plumbing.

Scope / ABI note

Objective-C only. clang compiles .m/.mm against libc++, so these sources must expose an extern "C" boundary — no C++ standard-library type may cross to the engine's libstdc++ side. C++ that must be compiled with clang is intentionally out of scope: that would need clang to use libstdc++ for ABI compatibility, which macOS does not ship; such code should instead be isolated behind a C boundary.

Testing

  • Configures cleanly on macOS arm64 with Homebrew GCC 16 (enable_language(OBJC/OBJCXX) test-compiles Objective-C with clang during configure); normal GCC targets (e.g. fmt) build unaffected.
  • Verified the routing itself with a minimal CMake project: a target mixing main.cpp + plain.cpp + objc.mm compiles the .cpp files with g++ and objc.mm with clang++ -x objective-c++, links (CMake auto-adds -lc++), and runs correctly.
  • No change on non-Apple platforms; no change when already building with AppleClang.

AI usage disclosure

Designed and drafted with AI assistance (Claude Code); reviewed and build/PoC-verified by a human.

GCC on macOS cannot target Apple's Objective-C runtime, so any Objective-C
or Objective-C++ source (.m / .mm) fails to build (or interoperate) under a
GCC toolchain. macOS platform integration in the renderer needs a small
amount of such code (e.g. Metal presentation glue), so the engine otherwise
has to be built entirely with AppleClang just to compile those few files.

Enable the OBJC and OBJCXX languages on Apple and, when the C/C++ compiler
is GCC, point them at clang. CMake selects a compiler per source language
(.m -> OBJC, .mm -> OBJCXX, .c/.cpp -> C/CXX), so Objective-C sources are
routed to clang automatically with no per-file or per-target handling, while
the rest of the engine keeps building with GCC. CMake also links libc++ for
targets containing such objects automatically.

Scope: this covers Objective-C only, which keeps a clean ABI story -- clang
compiles .m/.mm against libc++, and these sources must expose an extern "C"
boundary so no C++ standard-library type crosses to the engine's libstdc++
side. C++ that must be compiled with clang is intentionally out of scope: it
would require clang to use libstdc++ for ABI compatibility, which macOS does
not ship; such code should instead be isolated behind a C boundary.

No functional change on non-Apple platforms, and no change when already
building with AppleClang (the languages are simply enabled).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant