Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(native): an xcframework so the app runs in the simulator too

esengine6e4829e4d59f
diff --git a/build-tools/cli.js b/build-tools/cli.js
index 6fd3cb87a..5adb86009 100644
--- a/build-tools/cli.js
+++ b/build-tools/cli.js
@@ -167,6 +167,7 @@ program
.option('--abi <abi>', 'Android ABI', 'arm64-v8a')
.option('--platform <platform>', 'Android platform', 'android-33')
.option('--ios-min <version>', 'iOS deployment target', '17.0')
+ .option('--simulator', 'iOS: build the simulator slice (needs a simulator Dawn)', false)
.option('--package', 'Android: also package a signed APK (aapt2 + zipalign + apksigner)', false)
.option('--host <host>', 'Android --package: js (QuickJS host) or cpp (smoke test)')
.option('--keystore <path>', 'Android --package: signing keystore (default: the debug one)')
@@ -182,6 +183,7 @@ program
abi: options.abi,
platform: options.platform,
iosMin: options.iosMin,
+ simulator: options.simulator,
package: options.package,
host: options.host,
keystore: options.keystore,
diff --git a/build-tools/tasks/native.js b/build-tools/tasks/native.js
index ea9738b98..33bfbeeea 100644
--- a/build-tools/tasks/native.js
+++ b/build-tools/tasks/native.js
@@ -10,7 +10,7 @@
import path from 'path';
import { existsSync, readFileSync, writeFileSync } from 'fs';
-import { mkdir } from 'fs/promises';
+import { mkdir, rm } from 'fs/promises';
import config from '../build.config.js';
import * as logger from '../utils/logger.js';
import { runCommand, getCpuCount, resolvePython } from '../utils/emscripten.js';
@@ -161,10 +161,34 @@ async function iosDeveloperDir() {
return xcode;
}
+// A device build and a simulator build are different target triples, so their
+// objects can never link together. The app links an .xcframework instead, and
+// Xcode picks the slice matching whatever you selected in the toolbar.
+const IOS_SLICES = {
+ device: { dir: 'build-native-ios', sysroot: 'iphoneos', label: 'device' },
+ simulator: { dir: 'build-native-ios-sim', sysroot: 'iphonesimulator', label: 'simulator' },
+};
+const XCFRAMEWORK = path.join('build-native-ios', 'Estella.xcframework');
+
+// Rebuild the xcframework from whichever slices exist. A device-only framework is
+// valid — Xcode then simply has nothing to offer a simulator target.
+async function assembleXcframework(rootDir, env) {
+ const libs = [];
+ for (const slice of Object.values(IOS_SLICES)) {
+ const lib = path.join(rootDir, slice.dir, 'libestella_ios.a');
+ if (existsSync(lib)) libs.push('-library', lib);
+ }
+ const out = path.join(rootDir, XCFRAMEWORK);
+ await rm(out, { recursive: true, force: true });
+ await runCommand('xcodebuild', ['-create-xcframework', ...libs, '-output', out], { cwd: rootDir, env, silent: true });
+ logger.success(`iOS framework: ${XCFRAMEWORK} (${libs.length / 2} slice${libs.length === 2 ? '' : 's'})`);
+}
+
async function buildIosHost(options) {
if (process.platform !== 'darwin') throw new Error('The iOS host builds on macOS only.');
const rootDir = config.paths.root;
const { dawnDir, dawnBuild } = dawnPaths(options);
+ const slice = options.simulator ? IOS_SLICES.simulator : IOS_SLICES.device;
// Unlike Android there is no pure-C++ reference host for iOS: the app IS the
// JS host, so QuickJS is required rather than opt-in.
@@ -177,12 +201,12 @@ async function buildIosHost(options) {
const env = developerDir ? { DEVELOPER_DIR: developerDir } : undefined;
if (developerDir) logger.info(`Using DEVELOPER_DIR=${developerDir} (the active one has no iPhoneOS SDK)`);
- const buildDir = path.join(rootDir, 'build-native-ios');
+ const buildDir = path.join(rootDir, slice.dir);
await mkdir(buildDir, { recursive: true });
const genDir = await prepareGenerated(rootDir, buildDir, quickjs);
const deploymentTarget = options.iosMin || '17.0';
- logger.step(`Configuring iOS host (arm64, iOS ${deploymentTarget})...`);
+ logger.step(`Configuring iOS host (arm64 ${slice.label}, iOS ${deploymentTarget})...`);
await runCommand('cmake', [
'-S', path.join(rootDir, 'native'),
'-B', buildDir,
@@ -190,7 +214,7 @@ async function buildIosHost(options) {
'-DCMAKE_SYSTEM_NAME=iOS',
'-DCMAKE_OSX_ARCHITECTURES=arm64',
`-DCMAKE_OSX_DEPLOYMENT_TARGET=${deploymentTarget}`,
- '-DCMAKE_OSX_SYSROOT=iphoneos',
+ `-DCMAKE_OSX_SYSROOT=${slice.sysroot}`,
'-DCMAKE_BUILD_TYPE=Release',
'-DCMAKE_EXPORT_COMPILE_COMMANDS=ON',
`-DESTELLA_DAWN_DIR=${fwd(dawnDir)}`,
@@ -199,10 +223,11 @@ async function buildIosHost(options) {
`-DESTELLA_NATIVE_GEN_DIR=${fwd(genDir)}`,
], { cwd: rootDir, env });
- logger.step('Building iOS host...');
+ logger.step(`Building iOS host (${slice.label})...`);
await runCommand('cmake', ['--build', buildDir, '-j', String(getCpuCount())], { cwd: rootDir, env });
- logger.success(`iOS host: ${path.join('build-native-ios', 'libestella_js_host.a')}`);
+ logger.success(`iOS host: ${path.join(slice.dir, 'libestella_ios.a')}`);
+ await assembleXcframework(rootDir, env);
logger.info('Next: cd native/ios && xcodegen && open EstellaiOS.xcodeproj — pick your Team, then Run.');
}
diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt
index c5446516e..f4e996742 100644
--- a/native/CMakeLists.txt
+++ b/native/CMakeLists.txt
@@ -26,7 +26,8 @@ set(TP "${REPO}/third_party")
# Prebuilt monolithic Dawn: a shared library on Android, a static one on iOS (where
# the app embeds everything). Override ESTELLA_DAWN_LIB if your build lays it out
-# elsewhere.
+# elsewhere. A device build and a simulator build are different target triples, so
+# each needs its own Dawn — see ESTELLA_IOS_SIMULATOR below.
if(ESTELLA_IOS)
if(NOT ESTELLA_DAWN_LIB)
set(ESTELLA_DAWN_LIB "${ESTELLA_DAWN_BUILD}/src/dawn/native/libwebgpu_dawn.a")
diff --git a/native/README.md b/native/README.md
index 3c3b0f375..b113acdce 100644
--- a/native/README.md
+++ b/native/README.md
@@ -137,6 +137,19 @@ node build-tools/cli.js native --target ios \
cd native/ios && xcodegen && open EstellaiOS.xcodeproj
```
+**To run in the simulator** — a different target triple, so it needs its own Dawn
+and its own slice (`-DCMAKE_OSX_SYSROOT=iphonesimulator` on the Dawn command
+above, into `$DAWN/out-ios-sim`):
+
+```sh
+node build-tools/cli.js native --target ios --simulator \
+ --dawn "$DAWN" --dawn-build "$DAWN/out-ios-sim" --quickjs "$QJS"
+```
+
+Both slices land in `build-native-ios/Estella.xcframework`, which is what the app
+links — Xcode picks the one matching the toolbar. arm64 only, so an Intel Mac's
+simulator would need a third Dawn.
+
Then pick your Team under *Signing & Capabilities*, select your device and Run.
The CMake build merges host + engine + QuickJS + Dawn into one archive
(`build-native-ios/libestella_ios.a`), so the Xcode project is a signing and
@@ -163,10 +176,10 @@ serves from `assets/`.
## Status
**Android** is proven on device (Snapdragon 8 Elite / Adreno 830, 120 fps).
-**iOS** builds end to end: Dawn for iOS, the engine, QuickJS and the host all
-compile for arm64, merge into `libestella_ios.a`, and link into a 12 MB iOS
-executable with no unresolved symbols. It has not *run* on a device yet — that
-needs your signing.
+**iOS** runs: the demo renders in the simulator at a steady 60 fps — the clear
+colour, a ShapeRenderer and `logo.png` loaded through the real `Assets.loadTexture`
+pipeline — booting in ~20 ms off the bytecode cache. Not yet run on a physical
+device, which needs your signing.
The engine core + render path are proven on device. The JS host runs the **real
SDK**: `ESEngine.createNativeApp()`
diff --git a/native/ios/project.yml b/native/ios/project.yml
index f25672c23..8be628103 100644
--- a/native/ios/project.yml
+++ b/native/ios/project.yml
@@ -34,13 +34,18 @@ targets:
# -ObjC keeps the view controller / app delegate classes in the static
# library alive; the linker would otherwise drop TUs nothing references.
OTHER_LDFLAGS: -ObjC -lc++
- # Xcode links the .a below as -lestella_ios, so the CMake output dir has to
- # be on the search path — without it the link fails with "not found".
- LIBRARY_SEARCH_PATHS: $(SRCROOT)/../../build-native-ios
TARGETED_DEVICE_FAMILY: "1,2"
ENABLE_BITCODE: NO
+ # arm64 only, devices and simulator alike: the CMake build and Dawn are
+ # both arm64: a Release simulator build would otherwise also want x86_64,
+ # which no slice provides. (An Intel-Mac simulator needs its own Dawn.)
+ ARCHS: arm64
dependencies:
- - framework: ../../build-native-ios/libestella_ios.a
+ # An .xcframework, not a bare .a: device and simulator are different target
+ # triples whose objects cannot link together, and Xcode picks the slice that
+ # matches whatever the toolbar is pointed at. Build the simulator slice with
+ # `cli native --target ios --simulator`.
+ - framework: ../../build-native-ios/Estella.xcframework
embed: false
- sdk: UIKit.framework
- sdk: Metal.framework