使用Mono编译

需求

  • Mono 5.12.0或以上版本
  • MSBuild
  • NuGet
  • On Linux/macOS only: pkg-config

You may need to import necessary certificates for NuGet to perform HTTPS requests.

The recommended method is to use curl's CA (Certificate Autorities) certificate bundle.

Run the following commands to download and import it. On Windows, you can run it from the Mono command line prompt (or the regular prompt if you added Mono's bin directory to your PATH environment variable):

# If using PowerShell, replace `curl` with `curl.exe` below.
curl -LO https://curl.haxx.se/ca/cacert.pem
cert-sync --user cacert.pem

Alternatively, you can use the following command, though it's deprecated and may not work correctly:

mozroots --import --sync

环境变量

默认情况下,SCons会尝试在Windows上的Windows注册表中或通过其他平台上的 pkg-config 找到Mono。您可以通过将 mono_prefix 命令行选项传递给SCons来指定其他安装目录。如, scons [...] mono_prefix=%ProgramFiles%/Mono

这是包含子目录 includelib 的目录。

启用 Mono 模块

默认情况下,构建时禁用Mono模块。要启用它,请将选项 module_mono_enabled=yes 添加到SCons命令行。

生成胶水代码

胶水源码是包装函数,将被管理的方法调用。必须在构建最终二进制文件之前生成这些源文件。为了生成它们,首先,您必须使用选项 tools=yesmono_glue=no 构建一个临时的Godot二进制文件:

scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=no

构建完成后,您需要运行带有参数 --generate-mono-glue 的编译后的可执行文件,后跟输出目录的路径。在Godot目录中,此路径必须是 modules/mono/glue

<godot_binary> --generate-mono-glue modules/mono/glue

这个命令告诉 Godot 在 modules/mono/glue/Managed/Generatedmodules/mono/glue/mono_glue.gen.cpp 文件和C#代码。生成这些文件后,您可以为所有所需目标构建Godot,而无需重复此过程。

<godot_binary> refers to the tools binary you compiled above with the Mono module enabled. Its exact name will differ based on your system and configuration, but should be of the form bin/godot.<platform>.tools.<bits>.mono, e.g. bin/godot.x11.tools.64.mono or bin/godot.windows.tools.64.mono.exe. Be especially aware of the .mono suffix! If you've previously compiled Godot without Mono support, you might have similarly named binaries without this suffix. These binaries can't be used to generate the Mono glue.

注意

  • 不要使用 mono_glue=no 来构建您的发布版 Godot。 此选项会禁用C#脚本,它仅用于控制生成胶水代码的临时二进制文件。如果Godot是在没有胶水源码的情况下构建的,它将在启动时打印一条警告。
  • 每次ClassDB注册的API更改时,都必须重新生成胶水源码。即,例如,当将新方法注册到脚本API时,或该方法的参数之一发生更改时。如果ClassDB和胶水源码之间的API不匹配,Godot将在启动时打印一条错误。

用Mono胶水代码重构

一旦您生成了Mono胶水代码,就可以使用 mono_glue=yes 构建最终的二进制文件。这是 mono_glue 的默认值,所以您也可以省略它。您可以构建启用Mono的编辑器:

scons p=<platform> tools=yes module_mono_enabled=yes mono_glue=yes

启用Mono的导出模板:

scons p=<platform> tools=no module_mono_enabled=yes mono_glue=yes

如果一切正常,除了正常的输出,SCons应该在 bin 目录中创建了以下文件:

  • 如果您不是静态链接Mono运行时,则构建脚本会将Mono运行时共享库(monosgen-2.0)放置在输出目录中的Godot二进制文件旁边。分发Godot时,请确保包括此库。以Android为目标平台时,不需要任何额外的步骤,因为该库会自动复制到 #platform/android/java/libs,而Gradle会处理其余的工作。
  • 与“经典”Godot构建不同,在启用Mono模块(取决于目标平台)的情况下构建时,将为编辑器和导出模板两者一块创建数据目录。 该目录对于正常运行很重要,必须与Godot一起分发。 有关此目录的更多详细信息,参见 Data directory

示例

示例(Windows)

# Build temporary binary
scons p=windows tools=yes module_mono_enabled=yes mono_glue=no
# Generate glue sources
bin\godot.windows.tools.64.mono --generate-mono-glue modules/mono/glue

### Build binaries normally
# Editor
scons p=windows target=release_debug tools=yes module_mono_enabled=yes
# Export templates
scons p=windows target=release_debug tools=no module_mono_enabled=yes
scons p=windows target=release tools=no module_mono_enabled=yes

示例(x11)

# Build temporary binary
scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no
# Generate glue sources
bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue

### Build binaries normally
# Editor
scons p=x11 target=release_debug tools=yes module_mono_enabled=yes
# Export templates
scons p=x11 target=release_debug tools=no module_mono_enabled=yes
scons p=x11 target=release tools=no module_mono_enabled=yes

数据目录

数据目录是启用了Mono模块的Godot二进制文件的依赖项。它包含对Godot正确运行的重要文件。它必须与Godot可执行文件一起分发。

注解

The information below doesn't apply for Android, iOS and WASM, as there is no data directory for these platforms.

导出模板

The name of the data directory for an export template differs based on the configuration it was built with. The format is data.mono.<platform>.<bits>.<target>, e.g. data.mono.x11.32.release_debug or data.mono.windows.64.release.

必须以其原始名称将该目录放置在Godot导出模板旁边。导出项目时,Godot还会将此目录与游戏可执行文件一起复制,但名称将更改为 data_<APPNAME> ,其中 <APPNAME> 是项目设置 application/config/name 中指定的应用程序名称。

对于macOS,将导出模板压缩为ZIP存档,则数据目录的内容可以放置在ZIP存档内的以下位置:

bin/data.mono.<platform>.<bits>.<target>/Mono/lib /osx_template.app/Contents/Frameworks/GodotSharp/Mono/lib
bin/data.mono.<platform>.<bits>.<target>/Mono/etc /osx_template.app/Contents/Resources/GodotSharp/Mono/etc

编辑器

Godot编辑器的数据目录名称将始终为 GodotSharp。该目录的内容如下:

  • Api
  • Mono (可选)
  • Tools

Api 子目录包含Godot API程序集。在macOS上,如果Godot编辑器作为捆绑分发,则数据目录的内容可能位于以下位置:

bin/data.mono.<platform>.<bits>.<target>/Api <bundle_name>.app/Contents/Frameworks/GodotSharp/Api
bin/data.mono.<platform>.<bits>.<target>/Mono/lib <bundle_name>.app/Contents/Frameworks/GodotSharp/Mono/lib
bin/data.mono.<platform>.<bits>.<target>/Mono/etc <bundle_name>.app/Contents/Resources/GodotSharp/Mono/etc
bin/data.mono.<platform>.<bits>.<target>/Tools <bundle_name>.app/Contents/Frameworks/GodotSharp/Tools

Mono 子目录是可选的。分发编辑器时将需要它,因为当用户安装的Mono版本与构建Godot编辑器的版本不同时会出现问题。生成编辑器时,将 copy_mono_root=yes 传递给SCons以便创建此文件夹及其内容。

Tools 子目录包含编辑器所需的工具,如 GodotTools 程序集及其依赖项。

Building the Mono runtime

When building Godot for the desktop, you will likely use the pre-built Mono runtime that is installed on your system. This likely won't be the case when targeting other platforms like Android, iOS and WebAssembly. You will have to build the Mono runtime yourself for those platforms.

We recommend using these build scripts. They simplify this process but also include some patches needed for proper functioning with Godot. See the README on the link above for instructions on how to use the scripts.

目标平台为Android

与为其他平台构建相比,使用Mono为Android编译导出模板要简单一些,因为构建后无需其他步骤。无需担心运行时依赖项,例如数据目录或共享库(动态链接时),因为它们会自动添加到Gradle项目中。

Once you've built Mono, you can proceed to build Godot with the instructions described in this page and the Compiling for Android page. Make sure to let SCons know about the location of the Mono runtime you've just built, e.g.: scons [...] mono_prefix="$HOME/mono-installs/android-armeabi-v7a-release" (This path may be different on your system).

Targeting iOS

Once you've built Mono, you can proceed to build Godot with the instructions described in this page and the Compiling for iOS page. Make sure to let SCons know about the location of the Mono runtime you've just built, e.g.: scons [...] mono_prefix="$HOME/mono-installs/ios-arm64-release" (This path may be different on your system).

After building Godot for each architecture, you will notice SCons has copied the Mono libraries for each of them to the output directory:

#bin/libmono-native.iphone.<arch>.a
#bin/libmonosgen-2.0.iphone.<arch>.a
#bin/libmonoprofiler-log.iphone.<arch>.a

#bin/libmono-ilgen.iphone.<arch>.a
#bin/libmono-ee-interp.iphone.<arch>.a
#bin/libmono-icall-table.iphone.<arch>.a

The last three are only for iOS devices and are not available for the iOS simulator.

These libraries must be put in universal (multi-architecture) "fat" files to be distributed with the export templates.

The following bash script will create the "fat" libraries in the directory #bin/ios/iphone-mono-libs:

mkdir -p bin/ios
mkdir -p bin/ios/iphone-mono-libs

lipo -create bin/libmonosgen-2.0.iphone.arm64.a bin/libmonosgen-2.0.iphone.x86_64.a -output bin/ios/iphone-mono-libs/libmonosgen-2.0.iphone.fat.a
lipo -create bin/libmono-native.iphone.arm64.a bin/libmono-native.iphone.x86_64.a -output bin/ios/iphone-mono-libs/libmono-native.iphone.fat.a
lipo -create bin/libmono-profiler-log.iphone.arm64.a bin/libmono-profiler-log.iphone.x86_64.a -output bin/ios/iphone-mono-libs/libmono-profiler-log.iphone.fat.a

# The Mono libraries for the interpreter are not available for simulator builds
lipo -create bin/libmono-ee-interp.iphone.arm64.a -output bin/ios/iphone-mono-libs/libmono-ee-interp.iphone.fat.a
lipo -create bin/libmono-icall-table.iphone.arm64.a -output bin/ios/iphone-mono-libs/libmono-icall-table.iphone.fat.a
lipo -create bin/libmono-ilgen.iphone.arm64.a -output bin/ios/iphone-mono-libs/libmono-ilgen.iphone.fat.a

The iphone-mono-libs folder must be distributed with the export templates. The Godot editor will look for the libraries in <templates>/iphone-mono-libs/lib<name>.iphone.fat.a.

Targeting WebAssembly

Building for WebAssembly currently involves the same process regardless of whether the Mono module is enabled.

Once you've built Mono, you can proceed to build Godot with the instructions described in this page and the Compiling for the Web page. Make sure to let SCons know about the location of the Mono runtime you've just built, e.g.: scons [...] mono_prefix="$HOME/mono-installs/wasm-runtime-release" (This path may be different on your system).

Base Class Library

The export templates must also include the BCL (Base Class Library) for each target platform. Godot looks for the BCL folder at <templates>/bcl/<target_platform>, where <target_platform> is the same name passed to the SCons platform option, e.g.: <templates>/bcl/windows, <templates>/bcl/javascript.

Alternatively, Godot will look for them in the following locations:

Android <templates>/bcl/monodroid
iOS <templates>/bcl/monotouch
WebAssembly <templates>/bcl/wasm
Linux and macOS <templates>/bcl/net_4_x
Windows <templates>/bcl/net_4_x_win

As of now, we're assuming the same BCL profile can be used for both Linux and macOS, but this may change in the future as they're not guaranteed to be the same (as is the case with the Windows BCL).

If the target platform is the same as the platform of the Godot editor, then the editor will use the BCL it's running on (<data_folder>/Mono/lib/mono/4.5) if it cannot find the BCL in the export templates.

AOT cross-compilers

To perform ahead-of-time (AOT) compilation for other platforms, Godot needs to have access to the Mono cross-compilers for that platform and architecture.

Godot will look for the cross-compiler executable in the AOT compilers folder. The location of this folder is <data_folder>/Tools/aot-compilers/.

In order to build the cross-compilers we recommend using these build scripts.

After building them, copy the executable to the Godot AOT compilers directory. The executable name is <triple>-mono-sgen, e.g.: aarch64-apple-darwin-mono-sgen.

命令行选项

以下是使用Mono模块进行构建时可用的命令行选项的列表:

  • module_mono_enabled=yes | no
    • Build Godot with the Mono module enabled.
  • mono_glue=yes | no
    • Whether to include the glue source files in the build and define MONO_GLUE_DISABLED as a preprocessor macro.
  • mono_prefix=path
    • Path to the Mono installation directory for the target platform and architecture.
  • xbuild_fallback=yes | no
    • Whether to fallback to xbuild if MSBuild is not available.
  • mono_static=yes | no
    • Whether to link the Mono runtime statically.
    • The default is yes for iOS and WASM, and no for other platforms.
  • copy_mono_root=yes | no
    • Whether to copy the Mono framework assemblies and configuration files required by the Godot editor.