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: bundle cmake in toolchain, use emsdk Python for zero-config builds

esengine7f130ea5eb61
diff --git a/build-tools/toolchain/package.js b/build-tools/toolchain/package.js
index 560055263..297b20637 100644
--- a/build-tools/toolchain/package.js
+++ b/build-tools/toolchain/package.js
@@ -1,14 +1,21 @@
#!/usr/bin/env node
/**
- * Copies engine C++ source into Tauri resources for editor bundling.
+ * Copies engine C++ source and cmake into Tauri resources for editor bundling.
*
* Usage: node package.js
*/
import fs from 'fs';
import path from 'path';
-import { mkdir, rm, cp, readdir, stat, writeFile } from 'fs/promises';
+import os from 'os';
+import { pipeline } from 'stream/promises';
+import { createReadStream } from 'fs';
+import { createGunzip } from 'zlib';
+import { mkdir, rm, cp, readdir, stat, writeFile, chmod } from 'fs/promises';
+import { execSync } from 'child_process';
+
+const CMAKE_VERSION = '3.31.7';
const ROOT_DIR = path.resolve(import.meta.dirname, '../..');
const OUTPUT_DIR = path.join(ROOT_DIR, 'desktop/src-tauri/toolchain');
@@ -80,8 +87,113 @@ async function main() {
}
const size = await dirSize(engineSrcDir);
- log(`\nDone! Engine source: ${formatSize(size)}`);
- log(`Output: ${engineSrcDir}`);
+ log(`Engine source: ${formatSize(size)}`);
+
+ // Download and bundle cmake
+ await downloadCmake();
+
+ const totalSize = await dirSize(OUTPUT_DIR);
+ log(`\nDone! Total toolchain: ${formatSize(totalSize)}`);
+ log(`Output: ${OUTPUT_DIR}`);
+}
+
+function getCmakeDownloadInfo() {
+ const platform = os.platform();
+ const arch = os.arch();
+
+ if (platform === 'darwin') {
+ return {
+ filename: `cmake-${CMAKE_VERSION}-macos-universal.tar.gz`,
+ stripPrefix: `cmake-${CMAKE_VERSION}-macos-universal/CMake.app/Contents`,
+ format: 'tar.gz',
+ };
+ } else if (platform === 'win32') {
+ return {
+ filename: `cmake-${CMAKE_VERSION}-windows-x86_64.zip`,
+ stripPrefix: `cmake-${CMAKE_VERSION}-windows-x86_64`,
+ format: 'zip',
+ };
+ } else {
+ const linuxArch = arch === 'arm64' ? 'aarch64' : 'x86_64';
+ return {
+ filename: `cmake-${CMAKE_VERSION}-linux-${linuxArch}.tar.gz`,
+ stripPrefix: `cmake-${CMAKE_VERSION}-linux-${linuxArch}`,
+ format: 'tar.gz',
+ };
+ }
+}
+
+async function downloadCmake() {
+ const cmakeDir = path.join(OUTPUT_DIR, 'cmake');
+ const cmakeBin = path.join(cmakeDir, 'bin', os.platform() === 'win32' ? 'cmake.exe' : 'cmake');
+
+ if (fs.existsSync(cmakeBin)) {
+ log(`cmake already present at ${cmakeDir}`);
+ return;
+ }
+
+ const info = getCmakeDownloadInfo();
+ const url = `https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${info.filename}`;
+
+ log(`Downloading cmake ${CMAKE_VERSION}...`);
+ log(` URL: ${url}`);
+
+ const tmpDir = path.join(os.tmpdir(), `cmake-download-${Date.now()}`);
+ await mkdir(tmpDir, { recursive: true });
+ const archivePath = path.join(tmpDir, info.filename);
+
+ try {
+ execSync(`curl -L -o "${archivePath}" "${url}"`, { stdio: 'inherit' });
+
+ log('Extracting cmake...');
+
+ if (fs.existsSync(cmakeDir)) {
+ await rm(cmakeDir, { recursive: true, force: true });
+ }
+ await mkdir(cmakeDir, { recursive: true });
+
+ if (info.format === 'tar.gz') {
+ execSync(`tar xzf "${archivePath}" -C "${tmpDir}"`, { stdio: 'inherit' });
+ } else {
+ execSync(`unzip -q "${archivePath}" -d "${tmpDir}"`, { stdio: 'inherit' });
+ }
+
+ const extractedRoot = path.join(tmpDir, info.stripPrefix);
+
+ // Copy only bin/cmake and share/cmake-* (minimal set)
+ await mkdir(path.join(cmakeDir, 'bin'), { recursive: true });
+
+ const cmakeExeName = os.platform() === 'win32' ? 'cmake.exe' : 'cmake';
+ await cp(
+ path.join(extractedRoot, 'bin', cmakeExeName),
+ path.join(cmakeDir, 'bin', cmakeExeName),
+ );
+ if (os.platform() !== 'win32') {
+ await chmod(path.join(cmakeDir, 'bin', 'cmake'), 0o755);
+ }
+
+ // Find the share/cmake-* directory (e.g., share/cmake-3.31)
+ const shareDir = path.join(extractedRoot, 'share');
+ const shareEntries = await readdir(shareDir);
+ const cmakeShareDir = shareEntries.find(e => e.startsWith('cmake-'));
+ if (!cmakeShareDir) {
+ throw new Error('cmake share directory not found in archive');
+ }
+ await cp(
+ path.join(shareDir, cmakeShareDir),
+ path.join(cmakeDir, 'share', cmakeShareDir),
+ { recursive: true },
+ );
+
+ // Verify
+ const testResult = execSync(`"${path.join(cmakeDir, 'bin', cmakeExeName)}" --version`, { encoding: 'utf8' });
+ log(` cmake installed: ${testResult.split('\n')[0]}`);
+
+ const cmakeSize = await dirSize(cmakeDir);
+ log(` cmake size: ${formatSize(cmakeSize)}`);
+ } finally {
+ await rm(tmpDir, { recursive: true, force: true }).catch(() => {});
+ }
}
main().catch(err => {
diff --git a/desktop/src-tauri/src/compiler.rs b/desktop/src-tauri/src/compiler.rs
index 0f87a9640..e8b6ea282 100644
--- a/desktop/src-tauri/src/compiler.rs
+++ b/desktop/src-tauri/src/compiler.rs
@@ -202,7 +202,41 @@ fn get_emcc_version(emsdk_path: &Path) -> Option<String> {
re_version
}
-fn find_cmake() -> Option<(PathBuf, String)> {
+fn find_bundled_cmake(app: &AppHandle) -> Option<PathBuf> {
+ let cmake_name = if cfg!(windows) { "cmake.exe" } else { "cmake" };
+
+ // Bundled in resources
+ if let Ok(resource_dir) = app.path().resource_dir() {
+ let bundled = resource_dir.join("toolchain/cmake/bin").join(cmake_name);
+ if bundled.exists() {
+ return Some(bundled);
+ }
+ }
+
+ // Dev mode fallback
+ let dev_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+ .join("toolchain/cmake/bin")
+ .join(cmake_name);
+ if dev_path.exists() {
+ return Some(dev_path);
+ }
+
+ None
+}
+
+fn find_cmake(app: &AppHandle) -> Option<(PathBuf, String)> {
+ // Prefer bundled cmake
+ if let Some(bundled) = find_bundled_cmake(app) {
+ let output = std::process::Command::new(&bundled)
+ .arg("--version")
+ .output()
+ .ok()?;
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let version = stdout.lines().next()?.split_whitespace().last()?.to_string();
+ return Some((bundled, version));
+ }
+
+ // Fall back to system cmake
let cmake = if cfg!(windows) { "cmake.exe" } else { "cmake" };
let output = std::process::Command::new(cmake)
.arg("--version")
@@ -245,6 +279,33 @@ fn find_python() -> Option<(PathBuf, String)> {
None
}
+fn find_emsdk_python(emsdk_path: &Path) -> Option<(PathBuf, String)> {
+ // emsdk bundles Python at <emsdk>/python/<version>/bin/python3
+ let python_dir = emsdk_path.join("python");
+ if !python_dir.exists() {
+ return None;
+ }
+ let entries = std::fs::read_dir(&python_dir).ok()?;
+ for entry in entries.flatten() {
+ let bin_name = if cfg!(windows) { "python.exe" } else { "python3" };
+ let python_bin = entry.path().join("bin").join(bin_name);
+ if python_bin.exists() {
+ let output = std::process::Command::new(&python_bin)
+ .arg("--version")
+ .output()
+ .ok()?;
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let version = stdout
+ .trim()
+ .strip_prefix("Python ")
+ .unwrap_or(stdout.trim())
+ .to_string();
+ return Some((python_bin, version));
+ }
+ }
+ None
+}
+
fn which_sync(bin: &str) -> Option<PathBuf> {
let cmd = if cfg!(windows) { "where" } else { "which" };
let output = std::process::Command::new(cmd)
@@ -357,12 +418,14 @@ pub fn get_toolchain_status(app: AppHandle) -> ToolchainStatus {
})
.unwrap_or((None, None));
- let (cmake_found, cmake_version) = find_cmake()
+ let (cmake_found, cmake_version) = find_cmake(&app)
.map(|(_, v)| (true, Some(v)))
.unwrap_or((false, None));
- let (python_found, python_version) = find_python()
+ let python_from_emsdk = emsdk_path.as_ref().and_then(|p| find_emsdk_python(&PathBuf::from(p)));
+ let (python_found, python_version) = python_from_emsdk
.map(|(_, v)| (true, Some(v)))
+ .or_else(|| find_python().map(|(_, v)| (true, Some(v))))
.unwrap_or((false, None));
let emscripten_ok = emscripten_version
@@ -520,13 +583,19 @@ pub async fn compile_wasm(
}
}
- let env_vars = build_env_vars(&emsdk_dir);
+ let env_vars = build_env_vars(&emsdk_dir, &app);
+
+ // Resolve cmake binary (bundled or system)
+ let cmake_bin = find_bundled_cmake(&app)
+ .or_else(|| which_sync(if cfg!(windows) { "cmake.exe" } else { "cmake" }))
+ .ok_or("cmake not found. Run toolchain packaging first.")?;
+ let cmake_str = cmake_bin.to_string_lossy().to_string();
// Configure
emit_progress(&app, "configure", "Configuring CMake...", 0.1);
let emcmake = emsdk_dir.join("upstream/emscripten/emcmake");
- let mut cmake_args = vec!["cmake".to_string()];
+ let mut cmake_args = vec![cmake_str.clone()];
cmake_args.extend(build_cmake_flags(&options));
cmake_args.push(engine_src.to_string_lossy().to_string());
@@ -550,7 +619,7 @@ pub async fn compile_wasm(
run_command_streamed(
&app,
- "cmake",
+ &cmake_str,
&[
"--build".to_string(),
".".to_string(),
@@ -576,7 +645,7 @@ pub async fn compile_wasm(
};
run_command_streamed(
&app,
- "cmake",
+ &cmake_str,
&[
"--build".to_string(),
".".to_string(),
@@ -599,7 +668,7 @@ pub async fn compile_wasm(
emit_progress(&app, "compile", "Compiling Physics module...", 0.65);
run_command_streamed(
&app,
- "cmake",
+ &cmake_str,
&[
"--build".to_string(),
".".to_string(),
@@ -788,7 +857,7 @@ fn build_cmake_flags(options: &CompileOptions) -> Vec<String> {
flags
}
-fn build_env_vars(emsdk_dir: &Path) -> HashMap<String, String> {
+fn build_env_vars(emsdk_dir: &Path, app: &AppHandle) -> HashMap<String, String> {
let mut env = HashMap::new();
let upstream_dir = emsdk_dir.join("upstream");
@@ -800,16 +869,28 @@ fn build_env_vars(emsdk_dir: &Path) -> HashMap<String, String> {
emsdk_dir.join(".emscripten").to_string_lossy().to_string(),
);
+ // Use emsdk-bundled Python so users don't need system Python
+ if let Some((python_path, _)) = find_emsdk_python(emsdk_dir) {
+ env.insert("EMSDK_PYTHON".to_string(), python_path.to_string_lossy().to_string());
+ }
+
let path_sep = if cfg!(windows) { ";" } else { ":" };
let system_path = std::env::var("PATH").unwrap_or_default();
- let new_path = [
+
+ let mut path_entries = vec![
emscripten_dir.to_string_lossy().to_string(),
upstream_dir.join("bin").to_string_lossy().to_string(),
- system_path,
- ]
- .join(path_sep);
+ ];
+
+ // Add bundled cmake to PATH
+ if let Some(bundled_cmake) = find_bundled_cmake(app) {
+ if let Some(cmake_bin_dir) = bundled_cmake.parent() {
+ path_entries.push(cmake_bin_dir.to_string_lossy().to_string());
+ }
+ }
- env.insert("PATH".to_string(), new_path);
+ path_entries.push(system_path);
+ env.insert("PATH".to_string(), path_entries.join(path_sep));
env
}
diff --git a/editor/src/builder/BuildSettingsDialog.ts b/editor/src/builder/BuildSettingsDialog.ts
index 9e83e7abd..2ef09433c 100644
--- a/editor/src/builder/BuildSettingsDialog.ts
+++ b/editor/src/builder/BuildSettingsDialog.ts
@@ -678,18 +678,21 @@ export class BuildSettingsDialog {
}
private renderToolchainRows(s: NonNullable<typeof this.toolchainStatus_>): string {
- const row = (label: string, value: string | null, ok: boolean, minVersion?: string) => {
+ const row = (label: string, value: string | null, ok: boolean, minVersion?: string, note?: string) => {
const cls = ok ? '' : ' es-warning';
let display = value ?? 'not found';
if (value && !ok && minVersion) {
display += ` (requires >= ${minVersion})`;
}
+ if (note) {
+ display += ` <span class="es-build-module-desc">(${note})</span>`;
+ }
return `<div class="es-build-toolchain-row${cls}">${label}: ${display}</div>`;
};
return [
row('Emscripten', s.emscripten_version ?? (s.emsdk_path ? 'unknown' : null), s.emscripten_ok, '5.0.0'),
- row('CMake', s.cmake_version, s.cmake_ok, '3.16'),
- row('Python', s.python_version, s.python_ok, '3.0'),
+ row('CMake', s.cmake_version, s.cmake_ok, '3.16', s.cmake_ok ? 'bundled' : undefined),
+ row('Python', s.python_version, s.python_ok, '3.0', s.python_ok ? 'from emsdk' : undefined),
].join('');
}