编译 Godot 源码主要依赖 SCons 构建系统,步骤如下:
✅ 通用步骤(所有平台)
| 步骤 | 操作 |
|---|---|
| 1. 获取源码 | git clone https://github.com/godotengine/godot.git |
| 2. 安装依赖 | 安装 Python 3.5+ 和 SCons 3.0+:pip install scons |
| 3. 查看支持平台 | scons platform=list |
| 4. 编译命令 | scons platform=<平台> target=editor(编辑器)scons platform=<平台> target=template_release(发布模板) |
🖥️ 各平台简要说明
| 平台 | 关键命令或注意事项 |
|---|---|
| Linux | scons platform=linuxbsd target=editor |
| Windows (VS) | 使用 “x64 Native Tools Command Prompt”:scons platform=windows target=editor |
| macOS | 需安装 Xcode 命令行工具:scons platform=macos arch=arm64 --jobs=$(sysctl -n hw.logicalcpu) |
| Android | 需配置 Android SDK、NDK、Java,命令示例:scons platform=android target=template_release generate_apk=yes |
| Web | 需安装 Emscripten:scons platform=web target=template_release |
⚙️ 常用构建参数
| 参数 | 说明 |
|---|---|
target=editor / template_release / template_debug | 构建目标 |
deprecated=no | 禁用废弃功能,减小体积 |
production=yes | 启用优化构建 |
custom_modules="路径" | 添加外部模块 |
--jobs=8 | 使用 8 线程编译,加快构建速度 |
如需进一步裁剪模块、优化体积或定制功能,可编辑 custom.py 或使用 scons --help 查看所有可用参数。
✅ 什么是 SCons?
SCons 是一个用 Python 编写的开源自动化构建工具,功能类似于传统的 make,但比 make 更强大、跨平台、易扩展。它使用 Python 脚本作为构建配置文件(如 SConstruct),自动分析依赖关系、支持并行编译,并内置对 C/C++、Java、Fortran、Qt 等多种语言的支持。
✅ 如何安装 SCons?
✅ 方法一:使用 pip 安装(推荐)
适用于 Windows、macOS、Linux,前提是你已安装 Python(≥3.6):
bash
python -m pip install scons若你希望安装到用户目录(无需管理员权限):
bash
python -m pip install --user scons安装特定版本(如 3.1.2):
bash
python -m pip install scons==3.1.2✅ 方法二:使用系统包管理器(Linux)
| 系统 | 命令 |
|---|---|
| Ubuntu / Debian | sudo apt-get install scons |
| CentOS / Fedora | sudo yum install scons 或 sudo dnf install scons |
| Arch Linux | sudo pacman -S scons |
✅ 方法三:源码安装(适用于无网络环境)
- 从官网下载源码包:https://scons.org/pages/download.html
- 解压并进入目录:bash
tar -xzf scons-*.tar.gz cd scons-* sudo python setup.py install
✅ 验证安装是否成功
bash
scons --version输出类似如下即表示成功:
SCons by Steven Knight et al.:
SCons: v4.5.2✅ 提示
- Windows 用户:安装后请确保
scons.exe所在路径(如C:\Python39\Scripts)已添加到系统环境变量PATH中。 - Anaconda 用户 可使用:bash
conda install -c conda-forge scons
如你打算编译 Godot 或其他使用 SCons 的项目,安装完成后即可直接运行:
bash
scons platform=linuxbsd target=editor在 Windows 64 位系统上编译 Godot 纯 2D 版本(禁用 3D) 并最小化输出文件大小,请使用以下命令:
✅ 推荐构建命令(Windows 64 位,MSVC)
安装 Visual Studio 2022 或 2019 确保勾选了 “使用 C++ 的桌面开发” 工作负载。
使用 管理员模式 打开 “x64 Native Tools Command Prompt for VS”(Visual Studio 提供的命令行工具),进入 Godot 源码目录后执行:
bash
scons platform=windows target=template_release tools=no bits=64 disable_3d=yes optimize=size lto=full deprecated=no debug_symbols=no production=yes module_text_server_adv_enabled=no✅ 关键参数说明
| 参数 | 作用 |
|---|---|
platform=windows | 目标平台为 Windows |
target=template_release | 构建发布版运行时(不含编辑器) |
tools=no | 不编译编辑器,必须关闭才能禁用 3D |
bits=64 | 明确指定为 64 位 |
disable_3d=yes | 禁用整个 3D 引擎,显著减小体积 |
optimize=size | 优化为最小体积 |
lto=full | 启用链接时优化(需较多内存,体积更小) |
deprecated=no | 移除废弃功能 |
debug_symbols=no | 不包含调试符号,体积更小 |
production=yes | 启用所有优化选项 |
module_text_server_adv_enabled=no | 禁用高级文本模块,改用轻量版 |
✅ 编译完成后
生成的文件将位于:
bin\godot.windows.template_release.64.exe该文件即为 仅支持 2D 的最小运行时版本,不含编辑器、3D 模块、调试符号等内容。
✅ 额外建议(可选)
- 如需进一步裁剪模块,可创建
custom.py文件或使用profile=custom.py来集中管理构建选项。 - 若使用 MinGW 编译器,可加上
use_mingw=yes use_lto=yes以进一步减小体积。
完成后,可将该 .exe 作为导出模板使用,或通过 Godot 的“导出”功能将其打包为游戏运行时使用。
