UBT中的构建平台管理工具类UEBuildPlatform-Unreal4源码拆解-UnrealBuildTool功能流程解析

Unreal4源码拆解-UnrealBuildTool功能流程解析

知乎专栏:UBT源码解析

UBT中的构建平台管理工具类UEBuildPlatform

前言

  • UEBuildPlatform 是一个工具类,静态内容 包含了 所有注册平台的组织和构建平台信息等等 以及 相关处理函数
  • 同时也是一个基类。
  • 以此作为 基类拓展出了很多子类,均在Engine\Source\Programs\UnrealBuildTool目录下
  • 子类根据平台建立,主要增加保存了 对应平台SDK相关信息处理函数
    • Platform\Android\UEBuildAndroid.csAndroidPlatform
    • Platform\HoloLens\UEBuildHoloLens.csHoloLens
    • Platform\IOS\UEBuildIOS.csIOSPlatform
    • Platform\Linux\UEBuildLinux.csLinuxPlatform
    • Platform\Mac\UEBuildMac.csMacPlatform
    • Platform\Windows\UEBuildWindows.csWindowsPlatform

成员变量

静态成员变量

private static Dictionary<UnrealTargetPlatform, UEBuildPlatform> BuildPlatformDictionary = new Dictionary<UnrealTargetPlatform, UEBuildPlatform>();
static Dictionary<UnrealPlatformGroup, List> PlatformGroupDictionary = new Dictionary<UnrealPlatformGroup, List>();
private static string[] CachedPlatformFolderNames

实例成员变量

public readonly UnrealTargetPlatform Platform;
  • 保存当前子类实例对应的 UnrealTargetPlatform(独特字符串和ID)
private ReadOnlyHashSet CachedIncludedFolderNames;
  • 保存当前子类实例对应的 UnrealTargetPlatform 独特字符串和组织字符串
private ReadOnlyHashSet CachedExcludedFolderNames;
  • 保存所有除了当前子类实例对应的 UnrealTargetPlatform 独特字符串和组织字符串

静态成员方法

public static void RegisterPlatformWithGroup(UnrealTargetPlatform InPlatform, UnrealPlatformGroup InGroup)
  • 增加PlatformGroupDictionary,以组织名称为 Key ,平台名称为 Value 的静态字典对象,一个组织
  • 具体增加逻辑如下:在子类实例注册 SDK 时候 RegisterBuildPlatforms() 中调用此函数
    • Platform\Android\UEBuildAndroid.cs
      • RegisterPlatformWithGroup(UnrealTargetPlatform.Android, UnrealPlatformGroup.Android);
    • Platform\HoloLens\UEBuildHoloLens.cs
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.HoloLens, UnrealPlatformGroup.Microsoft);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.HoloLens, UnrealPlatformGroup.HoloLens);
    • Platform\IOS\UEBuildIOS.cs
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.IOS, UnrealPlatformGroup.Apple);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.IOS, UnrealPlatformGroup.IOS);
    • Platform\Linux\UEBuildLinux.cs
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Linux, UnrealPlatformGroup.Linux);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Linux, UnrealPlatformGroup.Unix);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Linux, UnrealPlatformGroup.Desktop);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.LinuxAArch64, UnrealPlatformGroup.Linux);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.LinuxAArch64, UnrealPlatformGroup.Unix);
    • Platform\Mac\UEBuildMac.cs
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Mac, UnrealPlatformGroup.Apple);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Mac, UnrealPlatformGroup.Desktop);
    • Platform\Lumin\UEBuildLumin.cs
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Lumin, UnrealPlatformGroup.Android);
    • Platform\Windows\UEBuildWindows.cs
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Win64, UnrealPlatformGroup.Windows);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Win64, UnrealPlatformGroup.Microsoft);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Win64, UnrealPlatformGroup.Desktop);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Win32, UnrealPlatformGroup.Windows);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Win32, UnrealPlatformGroup.Microsoft);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.Win32, UnrealPlatformGroup.Desktop);
    • Platform\TVOS\UEBuildTVOS.cs
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.TVOS, UnrealPlatformGroup.Apple);
      • UEBuildPlatform.RegisterPlatformWithGroup(UnrealTargetPlatform.TVOS, UnrealPlatformGroup.IOS);
public static void RegisterPlatforms(bool bIncludeNonInstalledPlatforms, bool bHostPlatformOnly)
  • 注册 目标平台SDK
  • 具体详情见
    • UnrealBuildTool 的构建平台注册流程-Unreal4源码拆解-UnrealBuildTool功能流程解析
public static string[] GetPlatformFolderNames()
  • 按照顺序缓存了所有 UnrealTargetPlatformUnrealPlatformGroup
  • 缓存到 CachedPlatformFolderNames
public static void RegisterBuildPlatform(UEBuildPlatform InBuildPlatform)
  • UEBuildPlatform 子类实例添加到 BuildPlatformDictionary
  • UEBuildPlatform 子类实例 .PlatformKey
public static bool IsDefaultBuildProduct(string FileName, string[] NamePrefixes, string[] NameSuffixes)
  • 直接看源代码,如果文件名满足前缀,后缀,拓展名相同且为.target或者.modules或者.version返回True
		public static bool IsDefaultBuildProduct(string FileName, string[] NamePrefixes, string[] NameSuffixes)
		{
			return UEBuildPlatform.IsBuildProductName(FileName, NamePrefixes, NameSuffixes, ".target")
				|| UEBuildPlatform.IsBuildProductName(FileName, NamePrefixes, NameSuffixes, ".modules")
				|| UEBuildPlatform.IsBuildProductName(FileName, NamePrefixes, NameSuffixes, ".version");
		}
public static bool IsBuildProductName(string FileName, string[] NamePrefixes, string[] NameSuffixes, string Extension)
  • 调用该函数重载进行拓展名比较,Index 参数设置为 0
public static bool IsBuildProductName(string FileName, int Index, int Count, string[] NamePrefixes, string[] NameSuffixes, string Extension)
  • 判断输入的文件名是否拓展名符合输入的拓展名,
  • 如果是的话则该函数重载比较前缀和后缀规范。
  • 否则返回 True
public static bool IsBuildProductName(string FileName, int Index, int Count, string[] NamePrefixes, string[] NameSuffixes)
  • 判断输入的文件名是否前缀符合 NamePrefixes 并且同时后缀符合 NameSuffixes(不包括扩展名)
public static bool IsPlatformAvailable(UnrealTargetPlatform Platform)
  • BuildPlatformDictionary 包含输入的 UnrealTargetPlatform 子类实例,且SDK 状态码为 SDKStatus.Valid
public static IEnumerable GetRegisteredPlatforms()
  • 返回所有 BuildPlatformDictionaryKey
public static List GetPlatformsInGroup(UnrealPlatformGroup InGroup)
  • 返回所有 PlatformGroupDictionaryvalue
public static IEnumerable GetPlatformGroups(UnrealTargetPlatform Platform)
  • 返回所有包含指定的目标平台的组织信息
public static UEBuildPlatform GetBuildPlatform(UnrealTargetPlatform InPlatform, bool bInAllowFailure = false)
  • 通过 key 查找 BuildPlatformDictionaryvalue
  • 找不到的话根据第二个参数选择执行。
    • true :返回 null
    • false :直接报错
public static void PlatformModifyHostModuleRules(string ModuleName, ModuleRules Rules, ReadOnlyTargetRules Target)
  • 会执行 BuildPlatformDictionary 的所有子类实例的ModifyModuleRulesForOtherPlatform
  • ModifyModuleRulesForOtherPlatform 在子类里会有不同实现。
public static String GetPathVarDelimiter()
  • 如果当前构建的主平台BuildHostPlatform.Current.Platform
    • UnrealTargetPlatform.Linux
    • UnrealTargetPlatform.Mac
    • UnrealTargetPlatform.LinuxAArch64
    • 返回冒号:
    • 其他全返回;
public static bool PlatformRequiresMonolithicBuilds(UnrealTargetPlatform InPlatform, UnrealTargetConfiguration InConfiguration)
  • 调用了 GetBuildPlatform 获取到指定 UnrealTargetPlatform 对应的平台BuildPlatform子类实例
  • 调用了子类实例的BuildPlatform.ShouldCompileMonolithicBinary
  • 判断是否需要全量编译二进制文件
protected static bool DoProjectSettingsMatchDefault(UnrealTargetPlatform Platform, DirectoryReference ProjectDirectoryName, string Section, string[] BoolKeys, string[] IntKeys, string[] StringKeys)
  • 检查给定的工程是否有默认构建配置文件,在 Automation工具 里面有用到
  • 具体位置在 Engine.ini[/Script/BuildSettings.BuildSettings] 域下
  • 检查默认构建配置文件和项目构建配置文件的 “bCompileApex”, “bCompileICU”,“bCompileRecast”, “bCompileSpeedTree”, “bCompileWithPluginSupport”, “bCompilePhysXVehicle”, “bCompileFreeType”,“bCompileForSize”, “bCompileCEF3”, “bCompileCustomSQLitePlatform
  • 这些变量是否相等
internal static bool IsPlatformInGroup(UnrealTargetPlatform Platform, UnrealPlatformGroup PlatformGroup)
  • 调用 GetPlatformsInGroup ,返回指定 PlatformGroup 的所有PlatformGroupDictionaryvalue
  • 看看返回的 values 中知否有指定的 Platform

实例成员方法

public ReadOnlyHashSet GetIncludedFolderNames()
  • 获取到 BuildPlatform 子类实例对应的目标平台的独特字符串
  • 加入到 CachedIncludedFolderNames
  • 获取到所有包含 BuildPlatform 子类实例对应的目标平台的组织的独特字符串
  • PlatformGroups 加入到 CachedIncludedFolderNames
public ReadOnlyHashSet GetExcludedFolderNames()
  • 把所有 UnrealTargetPlatformUnrealPlatformGroup除了GetIncludedFolderNames() 的其他所有的都找出来
public virtual VCProjectFileFormat GetRequiredVisualStudioVersion()
  • 基类返回 VCProjectFileFormat.Default
	enum VCProjectFileFormat
	{
		Default,          // Default to the best installed version, but allow SDKs to override
		VisualStudio2012, // Unsupported
		VisualStudio2013, // Unsupported
		VisualStudio2015,
		VisualStudio2017,
		VisualStudio2019,
		VisualStudio2022,
	}
public virtual bool CanBuildArchitecturesInSinglePass(IEnumerable InArchitectures)
  • 基类直接返回false
public virtual string GetDefaultArchitecture(FileReference ProjectFile)
  • 基类直接返回""
public virtual string GetFolderNameForArchitecture(string Architecture)
  • 基类直接返回输入参数
public void FindBuildProductsToClean(DirectoryReference BaseDir, string[] NamePrefixes, string[] NameSuffixes, List FilesToClean, List DirectoriesToClean)
  • 主要在CleanMode中调用。
  • 主要功能:
  • 参数解析:
    • 参数1会依次输入Engine和Enterprise目录和其下所有的插件目录,项目目录+Binaries+UnrealTargetPlatform的独特字符串比如Engine\Binaries\Win64
    • 参数2会根据TargetType做选择,并且追加一个项目名称
    public enum TargetType
     {
     	Game,
     	Editor,
     	Client,
     	Server,
     	Program,
     }
    //
    			case TargetType.Game:
     				return "UE4";
     			case TargetType.Client:
     				return "UE4Client";
     			case TargetType.Server:
     				return "UE4Server";
     			case TargetType.Editor:
     				return "UE4Editor";
    
    • 参数3是-+UnrealTargetPlatform的名称+-+UnrealTargetConfiguration枚举的字符串化比如:-Win64-Debug
    • 参数4,5传入空值
public virtual DirectoryReference GetBundleDirectory(ReadOnlyTargetRules Rules, List OutputFiles)
  • 基类直接返回null
public virtual string GetPlatformName()
  • 返回成员变量Platform的独特字符串
public virtual bool CanUseXGE()
  • 基类直接返回True
public virtual bool CanUseParallelExecutor()
  • return CanUseXGE()
public virtual bool CanUseDistcc()
  • 基类直接返回false
public virtual bool CanUseFASTBuild()
  • 基类直接返回false
public virtual bool CanUseSNDBS()
  • 基类直接返回false
public virtual string GetBinaryExtension(UEBuildBinaryType InBinaryType)
  • 基类直接抛出异常
public virtual string[] GetDebugInfoExtensions(ReadOnlyTargetRules InTarget, UEBuildBinaryType InBinaryType)
  • 基类直接抛出异常
public virtual bool ShouldCompileMonolithicBinary(UnrealTargetPlatform InPlatform)
  • 基类直接返回false
public virtual bool RequiresArchitectureSuffix()
  • 基类直接返回True
public virtual List FinalizeBinaryPaths(FileReference BinaryName, FileReference ProjectFile, ReadOnlyTargetRules Target)
  • 基类直接返回True 装了第一个输入参数的List
public virtual List GetConfigurations(UnrealTargetPlatform InUnrealTargetPlatform, bool bIncludeDebug)
  • 声明了一个List<UnrealTargetConfiguration> Configurations
  • 默认塞入一个UnrealTargetConfiguration.Development
  • 如果第二个参数是True则在0位置插入UnrealTargetConfiguration.Debug
  • 返回Configurations
public virtual bool HasDefaultBuildConfig(UnrealTargetPlatform Platform, DirectoryReference ProjectDirectoryName)
  • 检查默认配置文件,如果项目使用默认构建配置文件则返回True
		public virtual bool HasDefaultBuildConfig(UnrealTargetPlatform Platform, DirectoryReference ProjectDirectoryName)
		{
			string[] BoolKeys = new string[] {
				"bCompileApex", "bCompileICU",
				"bCompileRecast", "bCompileSpeedTree",
				"bCompileWithPluginSupport", "bCompilePhysXVehicle", "bCompileFreeType",
				"bCompileForSize", "bCompileCEF3", "bCompileCustomSQLitePlatform"
			};

			return DoProjectSettingsMatchDefault(Platform, ProjectDirectoryName, "/Script/BuildSettings.BuildSettings",
				BoolKeys, null, null);
		}
public virtual bool RequiresBuild(UnrealTargetPlatform Platform, DirectoryReference ProjectDirectoryName)
  • 基类直接返回false
public virtual void SetUpConfigurationEnvironment(ReadOnlyTargetRules Target, CppCompileEnvironment GlobalCompileEnvironment, LinkEnvironment GlobalLinkEnvironment)
  • 根据一些规则给全局编译环境变量加参数
		public virtual void SetUpConfigurationEnvironment(ReadOnlyTargetRules Target, CppCompileEnvironment GlobalCompileEnvironment, LinkEnvironment GlobalLinkEnvironment)
		{
			if (GlobalCompileEnvironment.bUseDebugCRT)
			{
				GlobalCompileEnvironment.Definitions.Add("_DEBUG=1"); // the engine doesn't use this, but lots of 3rd party stuff does
			}
			else
			{
				GlobalCompileEnvironment.Definitions.Add("NDEBUG=1"); // the engine doesn't use this, but lots of 3rd party stuff does
			}

			switch (Target.Configuration)
			{
				default:
				case UnrealTargetConfiguration.Debug:
					GlobalCompileEnvironment.Definitions.Add("UE_BUILD_DEBUG=1");
					break;
				case UnrealTargetConfiguration.DebugGame:
				// Individual game modules can be switched to be compiled in debug as necessary. By default, everything is compiled in development.
				case UnrealTargetConfiguration.Development:
					GlobalCompileEnvironment.Definitions.Add("UE_BUILD_DEVELOPMENT=1");
					break;
				case UnrealTargetConfiguration.Shipping:
					GlobalCompileEnvironment.Definitions.Add("UE_BUILD_SHIPPING=1");
					break;
				case UnrealTargetConfiguration.Test:
					GlobalCompileEnvironment.Definitions.Add("UE_BUILD_TEST=1");
					break;
			}

			// Create debug info based on the heuristics specified by the user.
			GlobalCompileEnvironment.bCreateDebugInfo =
				!Target.bDisableDebugInfo && ShouldCreateDebugInfo(Target);
			GlobalLinkEnvironment.bCreateDebugInfo = GlobalCompileEnvironment.bCreateDebugInfo;
		}
public string GetExternalBuildMetadata(FileReference ProjectFile)
  • 基类调用GetExternalBuildMetadata(FileReference ProjectFile, StringBuilder Metadata)

子类完全重写该函数,基类为空函数,在此不做解释,

在子类里面会单独拿windows和安卓平台做解析

public virtual void ModifyModuleRulesForOtherPlatform(string ModuleName, ModuleRules Rules, ReadOnlyTargetRules Target)
public abstract bool ShouldCreateDebugInfo(ReadOnlyTargetRules Target);
public abstract UEToolChain CreateToolChain(ReadOnlyTargetRules Target);
public abstract void Deploy(TargetReceipt Receipt);
public virtual void ResetTarget(TargetRules Target)
public virtual void ValidateTarget(TargetRules Target)
public virtual void AddExtraModules(ReadOnlyTargetRules Target, List ExtraModuleNames)
public virtual void ModifyModuleRulesForActivePlatform(string ModuleName, ModuleRules Rules, ReadOnlyTargetRules Target)
public abstract void SetUpEnvironment(ReadOnlyTargetRules Target, CppCompileEnvironment CompileEnvironment, LinkEnvironment LinkEnvironment);
public virtual void GetExternalBuildMetadata(FileReference ProjectFile, StringBuilder Metadata)
public virtual void ModifyBinaryLinkEnvironment( LinkEnvironment BinaryLinkEnvironment, CppCompileEnvironment BinaryCompileEnvironment, ReadOnlyTargetRules Target, UEToolChain ToolChain, IActionGraphBuilder Graph)
public virtual void FindAdditionalBuildProductsToClean(ReadOnlyTargetRules Target, List FilesToDelete, List DirectoriesToDelete)
public abstract bool IsBuildProduct(string FileName, string[] NamePrefixes, string[] NameSuffixes);
public virtual void PostBuildSync(UEBuildTarget Target)
public abstract SDKStatus HasRequiredSDKsInstalled();
public virtual string GetRequiredSDKString()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值