Up to date

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

在 Linux 上為 iOS 進行交叉編譯

為 iOS 交叉編譯的步驟有點複雜,並需要許多步驟。但正確設定好環境後,便能隨時輕鬆為 iOS 編譯 Godot。

免責宣告

雖然可以在 Linux 上為 iOS 平台編譯,但 Apple 對於使用的工具有嚴格的限制 (特別是硬體方面),幾乎只允許使用 Apple 自家產品進行開發。因此,這裡提到的方法 並非官方的 。但是,依據 Apple 在 2010 年發表的一篇宣告 提到,Apple 放寬了一些 App Store 審查方針 ,只要最終產生的二進位檔不會下載任何程式碼,就可以使用任何的工具。這表示,依照本頁的說明來交叉編譯二進位執行檔應該沒什麼問題。

系統需求

  • XCode with the iOS SDK (you must be logged into an Apple ID to download Xcode).

  • Clang >= 3.5 ,安裝於用於開發的機器上,並且需放置於 PATH 內。版本必須 >= 3.5 才能處理 arm64 架構的建置目標。

  • xar and pbzx (required to extract the .xip archive Xcode comes in).

    • For building xar and pbzx, you may want to follow this guide.

  • cctools-port 用於所需的建置工具。編譯的過程很特別,會在稍後說明。

    • 還有一些額外的相依性套件:automake, autogen, libtool。

設定環境

準備 SDK

Extract the Xcode .xip file you downloaded from Apple's developer website:

mkdir xcode
xar -xf /path/to/Xcode_X.x.xip -C xcode
pbzx -n Content | cpio -i

[...]
######### Blocks

Note that for the commands below, you will need to replace the version (x.x) with whatever iOS SDK version you're using. If you don't know your iPhone SDK version, you can see the JSON file inside of Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs.

解壓縮 iOS SDK:

export IOS_SDK_VERSION="x.x"
mkdir -p iPhoneSDK/iPhoneOS${IOS_SDK_VERSION}.sdk
cp -r xcode/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/* iPhoneSDK/iPhoneOS${IOS_SDK_VERSION}.sdk
cp -r xcode/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/* iPhoneSDK/iPhoneOS${IOS_SDK_VERSION}.sdk/usr/include/c++
fusermount -u xcode

Pack the SDK so that cctools can use it:

cd iPhoneSDK
tar -cf - * | xz -9 -c - > iPhoneOS${IOS_SDK_VERSION}.sdk.tar.xz

Toolchain

建置 cctools:

git clone https://github.com/tpoechtrager/cctools-port.git
cd cctools-port/usage_examples/ios_toolchain
./build.sh /path/iPhoneOS${IOS_SDK_VERSION}.sdk.tar.xz arm64

複製該工具至適當的地方。請注意,用於建置的 SCons 腳本會在所提供的 Toolchain 二進位檔資料夾中的 usr/bin 中搜尋,因此必須複製到這樣的子資料夾中,如下列指令:

mkdir -p "$HOME/iostoolchain/usr"
cp -r target/bin "$HOME/iostoolchain/usr/"

Now you should have the iOS toolchain binaries in $HOME/iostoolchain/usr/bin.

為 iPhone 編譯 Godot

完成上述步驟後,需要將兩樣東西保留在開發環境中:建置 Toolchain 以及 iPhoneOS SDK 資料夾。因為儲存路徑需要在建置時主動提供給 SCons,所以這兩樣東西可以放在任何地方。

為了能偵測到 iPhone 平台,必須定義 OSXCROSS_IOS 環境變數,內容不拘。

export OSXCROSS_IOS="anything"

現在可以通過與一般編譯 Godot 相同的方法來用 SCons 為 iPhone 進行編譯,但需要在一些額外的參數中提供正確路徑:

scons platform=ios arch=arm64 target=template_release IOS_SDK_PATH="/path/to/iPhoneSDK" IOS_TOOLCHAIN_PATH="/path/to/iostoolchain" ios_triple="arm-apple-darwin11-"