安装Mac+Unreal源码引擎


重点内容:
  • Brew安装
  • SVN or git
  • xcode
  • Visual Studio for Mac安装时记得勾选dotnet选项
  • sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
  • xcode设置自动生成info.plist
  • 各种文件权限不足chmod +x filename
  • 如果是UE4修改[-Werror,-Wunused-but-set-variable]错误
  • 如果是UE4修改libwebsockets HTTP重定义错误
  • Werror,-Wordered-compare-function-pointers错误

  1. 安装Homebrew
    通过如下命令进行安装:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
当然也可以找其他文章看看如何进行安装。等待安装成功即可,中途可能需要输入电脑密码和确认什么的。

  1. 安装SVN
    如果不常用SVN,这里也可以不处理。用如下命令进行安装:./brew install svn。需要cd到对应的目录,当然也可以设置path。
cd /opt/homebrew
./ brew install svn
    设置Path的方法:
/etc/profile
/etc/paths
~/.bash_profile
~/.bash_login
~/.profile              # 当前用户设置
~/.bashrc 
这些文件都可以设置,前两个是系统Path
通过vim ~/.bash_profile设置Path(该文件可能不存在,直接vim保存即可)

export brewHome=/opt/homebrew
export PATH=$PATH:$brewHome/bin:$brewHome/opt/svn/bin
在终端输入source ~/.bash_profile可以让PATH立即生效

  1. 下载Unreal源码
    你可以去Epic的仓库下载或是其他地方。我们这里直接从内网svn下载。
Svn checkout [svn地址] [保存目录]

  1. 终端执行Unreal源码中的Setup.sh
    首先给Setup.sh执行权限chmod +x Setup.sh。运行之后发现 Engine/Build/BatchFiles/Mac/GitDependencies.sh: Permission denied错误。
同样需要执行权限。
重新执行./Setup.sh。获得以下错误
/Users/lv/Unreal/Tools/UE4/Engine/Build/BatchFiles/Mac/SetupMono.sh: line 51: /Users/lv/Unreal/Tools/UE4/Engine/Build/BatchFiles/Mac/../../../Binaries/ThirdParty/Mono/Mac/bin/mono: Bad CPU type in executable
这是由于M1用的arm架构导致的。需要安装Rosetta2转译。在Mac终端使用softwareupdate —install-rosetta进行安装,期间会提示输入A同意安装。

  1. UE源码安装-文件下载
    通过第4步的修正,基本可以开始安装了执行Setup.sh或者Setup.command进行文件下载


  1. 生成XCode工程
    与上面步骤一样,执行GenerateProjectFiles.command或GenerateProjectFiles.sh
一开始就出错了

Running bundled mono, version: Mono JIT compiler version 5.16.0.220 (2018-06/bb3ae37d71a Fri Nov 16 17:12:11 EST 2018)
2023-02-22 19:27:48.968 defaults[10652:108162]
The domain/default pair of (com.epicgames.ue4, MonoAOT) does not exist
Triggered an exception while looking for SDK directory in Xcode.app
System.IO.DirectoryNotFoundException: Could not find a part of the path '/Library/Developer/CommandLineTools/Platforms/MacOSX.platform/Developer/SDKs'.
  at System.IO.__Error.WinIOError (System.Int32 errorCode, System.String maybeFullPath) [0x000f7] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at System.IO.FileSystemEnumerableIterator`1[TSource].HandleError (System.Int32 hr, System.String path) [0x00006] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at System.IO.FileSystemEnumerableIterator`1[TSource].CommonInit () [0x00054] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at System.IO.FileSystemEnumerableIterator`1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler`1[TSource] resultHandler, System.Boolean checkHost) [0x000d6] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at System.IO.FileSystemEnumerableFactory.CreateFileNameIterator (System.String path, System.String originalUserPath, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) [0x00009] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at System.IO.Directory.InternalGetFileDirectoryNames (System.String path, System.String userPathOriginal, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) [0x00000] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at System.IO.Directory.InternalGetDirectories (System.String path, System.String searchPattern, System.IO.SearchOption searchOption) [0x00000] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at System.IO.Directory.GetDirectories (System.String path) [0x0000e] in <98fac219bd4e453693d76fda7bd96ab0>:0
  at UnrealBuildTool.AppleToolChainSettings.SelectSDK (System.String BaseSDKDir, System.String OSPrefix, System.String& PlatformSDKVersion, System.Boolean bVerbose) [0x00018] in <5e468606cc944616a3935494a9a68c85>:0
ERROR: Invalid SDK MacOSX.sdk, not found in /Library/Developer/CommandLineTools/Platforms/MacOSX.platform/Developer/SDKs
可能是我们没有安装XCode的原因,先下载安装XCode。安装完成并第一次启动XCode,依旧提示同样的错误。
那是由于Xcode安装之后与Unreal需要的路径不一致。通过一下方法创建程序链接。
可能还有其他文件最好在/Applications/Xcode.app/contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/目录下检查一次

sudo mkdir -p /Library/Developer/CommandLineTools/Platforms/MacOSX.platform/Developer/SDKs

//将正确的的文件路径链接到如上目录下
sudo ln -s /Applications/Xcode.app/contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk MacOSX.sdk
sudo ln -s /Applications/Xcode.app/contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk MacOSX10.14.sdk
接着新的问题又来了。(RunMono.sh没有权限)

ERROR: Failed to start local process for action ("ApplicationName='/Users/lv/Unreal/Tools/UE4/Engine/Build/BatchFiles/Mac/RunMono.sh', CommandLine='"/Users/lv/Unreal/Tools/UE4/Engine/Binaries/DotNET/IOS/IPhonePackager.exe" signing_match Engine -bundlename com.YourCompany.UE4Game', CurrentDirectory='/Users/lv/Unreal/Tools/UE4/Engine', Native error= Access denied"): /Users/lv/Unreal/Tools/UE4/Engine/Build/BatchFiles/Mac/RunMono.sh "/Users/lv/Unreal/Tools/UE4/Engine/Binaries/DotNET/IOS/IPhonePackager.exe" signing_match Engine -bundlename com.YourCompany.UE4Game
到此,我们可以看到XCode工程已经生成完毕了!赶紧双击打开它吧!



  1. XCode打开UE4工程后,提示缺少Info.plist导致编译失败
    展开详细信息如下:

Cannot code sign because the target does not have an Info.plist file and one is not being generated automatically. Apply an Info.plist file to the target using the INFOPLIST_FILE build setting or generate one automatically by setting the GENERATE_INFOPLIST_FILE build setting to YES (recommended).
该错误是在XCode13之后才出现的。按下图将自动生成info.plist改为true




  1. 开始编译ShaderCompileWorker(set but not used [-Werror,-Wunused-but-set-variable])
又是一堆的报错主要是变量申明。
Showing All Messages
/Users/lv/Unreal/Tools/UE4/Engine/Source/Runtime/Core/Private/Async/TaskGraph.cpp:1019:8: variable 'bDidStall' set but not used [-Werror,-Wunused-but-set-variable]
网上的朋友提供了两种方法。 试了试。至少在编译Mac版本的源码时解决不了问题。
A. 找到项目中的 .target.cs 和 Editor.target.cs在其中加入以下两行
bOverrideBuildEnvironment = true;
AdditionalCompilerArguments = "-Wno-unused-but-set-variable";
B. 修改Unreal引擎文件(适合用windows进行远程编译的人)
找到Engine/Source/Programs/UnrealBuildTool 目录,双击 UnrealBuildTool.csproj 打开工程(不要求源码版,Launcer版本也可),找到打开MacToolChain.cs
删掉-Wall参数。

C. 另外还有一种方法,找到打开MacToolChain.cs

Result += " -c";前面增加下面的一堆参数。
Result += " -Wno-deprecated";
Result += " -Wno-deprecated-declarations";
Result += " -Wno-int-to-void-pointer-cast";
Result += " -Wno-non-literal-null-conversion";
Result += " -Wno-overloaded-virtual";
Result += " -Wno-pointer-to-int-cast";
Result += " -Wno-pragma-once-outside-header";
Result += " -Wno-unused-but-set-variable";
Result += " -Wno-unused-function";
Result += " -Wno-unused-result";
Result += " -Wno-unused-variable";
这几种方法都可以试试,在编译Mac版本的UE时,有效的方法就是删除-Wall参数,或者设置下面的参数。
if (CompileEnvironment.bEnableUndefinedIdentifierWarnings)
{
    Result += " -Wundef" + (CompileEnvironment.bUndefinedIdentifierWarningsAsErrors ? "" : " -Wno-error=undef");
}
//copy from ue5.1.1
if (GetClangVersion() >= new Version(13, 0, 0))
{
Result += " -Wno-unused-but-set-variable";
Result += " -Wno-unused-but-set-parameter";
Result += " -Wno-ordered-compare-function-pointers";
}
if (GetClangVersion() >= new Version(14, 0, 0))
{
Result += " -Wno-bitwise-instead-of-logical";
}

Result += " -c";
或者直接改为下面这样
if (CompileEnvironment.bEnableUndefinedIdentifierWarnings)
{
    Result += " -Wundef" + (CompileEnvironment.bUndefinedIdentifierWarningsAsErrors ? "" : " -Wno-error=undef");
}
Result += " -Wno-unused-but-set-variable";
Result += " -Wno-unused-but-set-parameter";
Result += " -Wno-ordered-compare-function-pointers";
Result += " -Wno-bitwise-instead-of-logical";

Result += " -c";
  1. 编译警告---Werror,-Wordered-compare-function-pointers
Showing All Messages
/Users/lv/Unreal/Tools/UE4/Engine/Plugins/Media/ElectraPlayer/Source/ElectraPlayerRuntime/Private/Runtime/3rdParty/FastDelegate/FastDelegate.h:602:30: ordered comparison of function pointers ('Electra::fastdelegate::DelegateMemento::GenericFuncPtr' (aka 'void (*)()') and 'Electra::fastdelegate::DelegateMemento::GenericFuncPtr') [-Werror,-Wordered-compare-function-pointers]
用了一个比较变态的办法,该问题是直接比较两个指针导致。将指针转位整型即可。FastDelegate.h
return m_pStaticFunction < right.m_pStaticFunction改为return (uint64)m_pStaticFunction < (uint64)right.m_pStaticFunction

*还有一种方法同样是添加编译参数,这种方法在UE5中可以看到。
-Wno-ordered-compare-function-pointers

  1. -Werror, -Wuninitialized-const-reference错误
与前面的-Werror,-Wunused-but-set-variable一样,删除-Wall参数。

  1. System/SourceFileWorkingSet.cs(92,3): error CS0246: The type or namespace name 'DirectoryReference' could not be found (are you missing a using directive or an assembly reference?)
    一堆的找不到C#类的错误。
    到这里,发现提示的代码都是Mono的相关代码,首先就怀疑是不是没有安装dotnet导致。所以,首先取官网下载了dotnet5.0.
重新开始编译之后,任然是相同的提示。
    没有办法,就尝试安装官方的git release分支的5.1.1源码版本。这次更糟糕,生成xcode工程的步骤已经出错。详情可以看下一个问题。其实也没有抱有太大期望。
    接下来有尝试安装Epic 启动器里的发布版本。可以正常安装,但启动时会遇到下一节提到的找不到xcode的错误。解决完之后,官方发布版本可以正常打开并创建工程了。
    抱着试试看的心态,重新执行UE5.1.1源码的GenerateProjectFile.command。居然也成功了。

    那是否编译4.26源码也没问题了呢?执行UE4.26.2源码的GenerateProjectFile.command。也成功了。那就开始build吧。
    在之后还出现过该问题,可以重新编译UnrealBuildTool。
    



  • 安装版UE5.1.1启动时提示找不到xcode
执行下面命令查看xcode路径xcode-select -p输出的结果如果不是/Applications/Xcode.app/Contents/Developer执行下面命令sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

  • 安装UE5.1.1源码版
安装UE5.1.1源码版本,执行GenerateProjectFiles.command时,提示SDK不完整
与上面的错误一样,需指定xcode的路径

Some Platforms were skipped due to invalid SDK setup: Mac, IOS, Android, TVOS.
See the log file for detailed information

/Library/Developer/CommandLineTools
Generating data for project indexing...   
Exception while generating include data for UnrealEditor: Platform Mac is not a valid platform to build. Check that the SDK is installed properly.
Exception while generating include data for BaseTextureBuildWorker: Platform Mac is not a valid platform to build. Check that the SDK is installed properly.
  1. libWebsockets HTTP状态重定义
在xcode14里肯定会有该问题,其他早期的版本不清楚。
修改方法:
enum http_status {
改为
enum class http_status {
当然还有一种修改方法,参照官方的提交记录



  1. 找不到libwebsockets.a
先不管那么多了,从发布版本的目录中复制一个过来。

  1. 通过上面的一番折腾--直接编译UE5.1.1没出现任何问题。