接收命令行参数控制程序编译行为

在 build.cs 以及 target.cs 中可以获取命令行参数,对编译选项进行控制:

1
2
3
4
5
string[] CmdArgs = Environment.GetCommandLineArgs();
foreach (string CmdLineArg in CmdArgs)
{
Console.WriteLine("CmdLineArg: " + CmdLineArg);
}

编译时输出:

1
2
3
4
5
6
7
8
9
0>E:\UnrealEngine\Launcher\UE_4.26\Engine\Build\BatchFiles\Build.bat GWorldEditor Win64 Development -Project="E:\UnrealProjects\GWorld\GWorld.uproject" -WaitMutex -FromMsBuild -test
0>CmdLineArg: ..\..\Engine\Binaries\DotNET\UnrealBuildTool.exe
0>CmdLineArg: GWorldEditor
0>CmdLineArg: Win64
0>CmdLineArg: Development
0>CmdLineArg: -Project=E:\UnrealProjects\GWorld\GWorld.uproject
0>CmdLineArg: -WaitMutex
0>CmdLineArg: -FromMsBuild
0>CmdLineArg: -test

基于传递的参数可以动态地修改编译行为,比如添加宏、依赖的模块等。

以动态开启 ASAN 的方法,开启 ADDRESS SANITIZER,可以更好的进行内存异常访问分析:iOS Address Sanitizer in UE4

在 Target.cs 中开启:

1
Environment.SetEnvironmentVariable("ENABLE_ADDRESS_SANITIZER", "YES");

使用命令行控制在打包阶段是否开启 ASAN:

1
2
3
4
5
6
7
8
9
10
11
12
string[] CmdArgs = Environment.GetCommandLineArgs();
foreach (string CmdLineArg in CmdArgs)
{
if (CmdLineArg.Equals("-asan"))
{
if (Target.Platform == UnrealTargetPlatform.Mac ||
Target.Platform == UnrealTargetPlatform.IOS)
{
Environment.SetEnvironmentVariable("ENABLE_ADDRESS_SANITIZER", "YES");
}
}
}

在打包时传递 -asan 参数给 UBT 即可,