Up to date

This page is up to date for Godot 4.3. If you still find outdated information, please open an issue.

為 macOS 進行編譯

備註

這個頁面描述的是如何從源碼編譯 macOS 編輯器和匯出範本二進位檔案。如果你要找的是匯出專案到 macOS,請閱讀 為 macOS 匯出

系統需求

若要在 macOS 下編譯,必須滿足下列需求:

備註

若有安裝 Homebrew ,則可以通過下列指令來輕鬆安裝 SCons 與 yasm:

brew install scons

安裝 Homebrew 時,若還未有適用於 Xcode 的命令行工具,也會自動安裝。

若有安裝 Homebrew ,則可以通過下列指令來輕鬆安裝 SCons 與 yasm:

sudo port install scons

也參考

要獲取編譯所需的 Godot 源碼,請參閱 取得原始碼

有關配合 Godot 使用 SCons 的一般說明,請參考 建置系統簡介

編譯

開啟終端機,移至 Godot 原始碼的根目錄。

若要為 Intel (x86-64) 核心的 Mac 編譯,請使用:

scons platform=macos arch=x86_64

若要為 Apple Silicon (ARM64) 核心的 Mac 編譯,請使用 (僅可用於 Godot 3.2.3+):

scons platform=macos arch=arm64

若要使用單一「Universal 2」二進位執行檔來同時支援兩個架構,請執行上述兩個指令,然後通過 lipo 來將這兩個檔案捆綁在一起:

lipo -create bin/godot.macos.editor.x86_64 bin/godot.macos.editor.arm64 -output bin/godot.macos.editor.universal

小訣竅

If you are compiling Godot to make changes or contribute to the engine, you may want to use the SCons options dev_build=yes or dev_mode=yes. See 開發 for more info.

若沒出現問題的話,產生的二進位執行檔會被放在 bin/ 子資料夾中。該執行檔包含了完整的 Godot 遊戲引擎,且不需任何相依性套件即可執行。執行該檔案會開啟專案管理員。

備註

若想在客製化建置上使用與 Godot 官方釋出版本不同的編輯器設定,可以通過在 bin/ 資料夾中建立檔名為 ._sc__sc_ 的檔案來開啟 自包含模式

To create an .app bundle like in the official builds, you need to use the template located in misc/dist/macos_tools.app. Typically, for an optimized editor binary built with dev_build=yes:

cp -r misc/dist/macos_tools.app ./Godot.app
mkdir -p Godot.app/Contents/MacOS
cp bin/godot.macos.editor.universal Godot.app/Contents/MacOS/Godot
chmod +x Godot.app/Contents/MacOS/Godot
codesign --force --timestamp --options=runtime --entitlements misc/dist/macos/editor.entitlements -s - Godot.app

備註

如果您正在建立「master」分支,您還需要包含對 MoltenVK Vulkan 可移植性庫的支援。預設情況下,它將透過您安裝的適用於 macOS 的 Vulkan SDK 進行靜態連結。您也可以選擇透過傳遞“use_volk=yes”並將動態庫包含在“.app”包中來動態連結它:

mkdir -p Godot.app/Contents/Frameworks
cp <Vulkan SDK path>/macOS/lib/libMoltenVK.dylib Godot.app/Contents/Frameworks/libMoltenVK.dylib

編譯無周邊版本與伺服器版本

若要編譯用於以編輯器功能來自動化匯出專案的 無周邊 版本,請使用:

scons platform=macos target=editor

然後使用“--headless”命令列參數:

./bin/godot.macos.editor.x86_64 --headless

如果要編譯除錯版本的*伺服器*,支援:ref:遠端除錯工具 <doc_command_line_tutorial>,那麼請使用:

scons platform=macos target=template_debug

若要編譯可執行專屬遊戲伺服器的 伺服器 版本,請使用:

scons platform=macos target=template_release production=yes

建置匯出樣板

要建立macOS的匯出範本,你必須用 tools=no (無編輯器)進行編譯,並分別為 target=release (發行範本)和 target=release_debug.

Official templates are Universal 2 binaries which support both ARM64 and Intel x86_64 architectures.

  • To support ARM64 (Apple Silicon) + Intel x86_64:

    scons platform=macos target=template_debug arch=arm64
    scons platform=macos target=template_release arch=arm64
    scons platform=macos target=template_debug arch=x86_64
    scons platform=macos target=template_release arch=x86_64 generate_bundle=yes
    
  • To support ARM64 (Apple Silicon) only (smaller file size, but less compatible with older hardware):

    scons platform=macos target=template_debug arch=arm64
    scons platform=macos target=template_release arch=arm64 generate_bundle=yes
    

To create an .app bundle like in the official builds, you need to use the template located in misc/dist/macos_template.app. This process can be automated by using the generate_bundle=yes option on the last SCons command used to build export templates (so that all binaries can be included). This option also takes care of calling lipo to create an Universal 2 binary from two separate ARM64 and x86_64 binaries (if both were compiled beforehand).

備註

You also need to include support for the MoltenVK Vulkan portability library. By default, it will be linked statically from your installation of the Vulkan SDK for macOS. You can also choose to link it dynamically by passing use_volk=yes and including the dynamic library in your .app bundle:

mkdir -p macos_template.app/Contents/Frameworks
cp <Vulkan SDK path>/macOS/libs/libMoltenVK.dylib macos_template.app/Contents/Frameworks/libMoltenVK.dylib

In most cases, static linking should be preferred as it makes distribution easier. The main upside of dynamic linking is that it allows updating MoltenVK without having to recompile export templates.

然後你可以壓縮 osx_template.app 資料夾來複製 Godot 官方發行的 osx.zip 範本:

zip -r9 macos.zip macos_template.app

在 Linux 上為 macOS 進行交叉編譯

可以在 Linux 環境下為 macOS 編譯 (Windows 下也許可以使用適用於 Linux 的 Windows 子系統來編譯)。若要在 Linux 下為 macOS 交叉編譯,則必須安裝 OSXCross 來將 macOS 作為建置目標使用。首先,請依照下列指示安裝 OSXCross:

先 Clone OSXCross 儲存庫 至電腦上的某處 (或下載 ZIP 檔然後解壓縮)。如:

git clone --depth=1 https://github.com/tpoechtrager/osxcross.git "$HOME/osxcross"
  1. 請依照這個指示來打包 SDK:https://github.com/tpoechtrager/osxcross#packaging-the-sdk

  2. 請依照這個指示來安裝 OSXCross:https://github.com/tpoechtrager/osxcross#installation

之後,還需要將 OSXCROSS_ROOT 定義為 OSXCross 安裝的路徑 (也就是與儲存庫 Clone 或解壓縮的路徑)。如:

export OSXCROSS_ROOT="$HOME/osxcross"

之後就可以與平常一樣使用 SCons:

scons platform=macos

若 OSXCross 的 SDK 版本與 SCons 建置系統所預期不同,則必須通過 osxcross_sdk 參數來指定一個版本:

scons platform=macos osxcross_sdk=darwin15