Recompile UE4 PhysX libraries (Linux, Windows etc)

I require complete instructions to recompile the UE4 PhysX libraries on Linux, Windows etc.

Which set of named commands need to be executed, and with what specific parameters?

I specifically need to compile older ~UE4.14.3 PhysX libraries for a newer Linux system (Ubuntu 20.04+). The distributed UE4.14.3 PhysX libraries for Linux were compiled for an older version of glibc with the clang ffast-math flag and thus reference obsolete *_finite math functions (e.g. atan2f_finite etc). All versions of glibc past ~2019 do not support these functions; which comprises a major Linux backwards compatibility failure.

There are some informal instructions for compiling PhysX on Windows that reference the old unrealengine wiki, but neither specify how to generate PhysX.sln;

General recommendations;

  • All UE4 third party libraries require complete compilation guides (should not rely on distributed versions of these libraries for long-term support).
  • All UE4 scripts in UnrealEngine\Engine\Build\BatchFiles require complete usage documentation.

Thank you for your support

See Linux Part 2b/Windows Part 2 below for a method to recompile the PhysX library for Linux/Windows respectively (UE4.14.3).

I resolved the lost libm *_finite function support issue by recompiling the third party PhysX library without the cclang -ffast-math flag (see https://github.com/EpicGames/UnrealEngine/commit/2b6178f2cbd66dbf6a4c550ddc894064d6239add).

I also found a method to recompile the PhysX library for Windows, although it requires additional environment variables for this UE version (see Windows Part 2 below); Build PhysX For UE4 On Windows | Noah Zuo's Blog

Linux

Linux Part 1 (Download UE4.14.3 source):

cd /USERFOLDER
git clone https://github.com/EpicGames/UnrealEngine.git
cd UnrealEngine
git checkout tags/4.14.3-release
apply fix4 (`Package clang-3.9 is not available, but is referred to by another package`);
	download and extract https://releases.llvm.org/3.9.1/clang+llvm-3.9.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz
	cd clang+llvm-3.9.1-x86_64-linux-gnu-ubuntu-16.04
	sudo cp -r * /usr/local/
	sudo ln -s /usr/local/bin/clang++ /usr/local/bin/clang++-3.9
apply fix2 (`Failed to download 'http://cdn.unrealengine.com/dependencies/..': The remote server returned an error`);
	go to https://github.com/EpicGames/UnrealEngine/releases/tag/4.14.3-release
	download Commit.gitdeps.xml 
	cp Commit.gitdeps.xml /USERFOLDER/UnrealEngine/Engine/Build/Commit.gitdeps.xml
./Setup.sh [download dependencies]	
./GenerateProjectFiles.sh

Linux Part 2a (patch UE4.14.3 PhysX library for Linux UB20.04):

note instances of finite math functions in original PhysX library (these will be removed);
	cd /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX
	grep -r '\-finite' *
cd /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX
nedit APEX_1.4/compiler/cmake/Linux/CMakeLists.txt PhysX_3.4/Source/compiler/cmake/Linux/CMakeLists.txt PxShared/src/compiler/cmake/Linux/CMakeLists.txt &
remove all instances; " -ffast-math"
	verify removed all Linux instances;
	cd /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX
	grep -r ffast\-math *

Linux Part 2b (recompile UE4.14.3 PhysX library for Linux):

Install dependencies (minimal);
	sudo apt-get install cmake-qt-gui
	sudo apt-get install nedit
mkdir /USERFOLDER/PhysX414
nedit /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Externals/CMakeModules/Linux/LinuxCrossToolchain.x86_64-unknown-linux-gnu.cmake
	#MESSAGE("LINUX_ROOT environment variable not defined!")
	SET(CMAKE_C_COMPILER   /usr/local/bin/clang)
	SET(CMAKE_CXX_COMPILER   /usr/local/bin/clang++)
export CMAKE_MODULE_PATH=/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Externals/CMakeModules
export GW_DEPS_ROOT=/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX
mkdir /USERFOLDER/PhysX414/Lib
mkdir /USERFOLDER/PhysX414/cmakeworking-PhysX
mkdir /USERFOLDER/PhysX414/cmakeworking-APEX
Repeat for each sub library (PhysX, APEX):
	cmake-gui
	source code; 
		/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/PhysX_3.4/Source/compiler/cmake/Linux
		OR: /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/APEX_1.4/compiler/cmake/Linux
	binaries; 
		/USERFOLDER/PhysX414/cmakeworking-PhysX
		OR: /USERFOLDER/PhysX414/cmakeworking-APEX
	Configure 
		Unix Makefiles
		Specify toolchain file for cross-compiling
		/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Externals/CMakeModules/Linux/LinuxCrossToolchain.x86_64-unknown-linux-gnu.cmake
		Finish
	[will generate an error; PxFoundation.cmake:35  - "STREQUAL" "debug"]
	set var in GUI list; CMAKE_BUILD_TYPE	release
	Configure
	Generate
cp /USERFOLDER/PhysX414/cmakeworking-PhysX/*.a /USERFOLDER/PhysX414/Lib
cp /USERFOLDER/PhysX414/cmakeworking-PhysX/pxshared_bin/*.a /USERFOLDER/PhysX414/Lib
cp /USERFOLDER/PhysX414/cmakeworking-APEX/*.a /USERFOLDER/PhysX414/Lib

Linux Part 2c (verify recompiled UE4.14.3 PhysX library for Linux)

verify removed all instances;
	cd /USERFOLDER/PhysX414/Lib
	grep -r '\-finite' *
The only remaining instances should pertain to strings rather than *-finite function references;
	Binary file libNvParameterized.a matches
	Binary file libNvParameterizedPROFILE.a matches
	APEX_1.4/NvParameterized/src/NvParameterized.cpp:		NV_PARAM_TRAITS_WARNING(mParameterizedTraits, "%s: setting non-finite floating point value", _longName); \
	APEX_1.4/NvParameterized/src/NvParameterized.cpp:			NV_PARAM_TRAITS_WARNING(mParameterizedTraits, "%s[%d]: setting non-finite floating point value", _longName, (int)_i); \

Linux Part 3 (compile UE4.14.3 with updated PhysX library):

cd /USERFOLDER
reapply fix1 (`Package clang-3.9 is not available, but is referred to by another package`);
	nedit /USERFOLDER/UnrealEngine/Engine/Build/BatchFiles/Linux/Setup.sh
	remove `clang-3.9` reference from `DEPS` `assume the latest, this is going to be a moving target`
apply fix2 (already done);
apply fix3 (`The <sys/sysctl.h> header is deprecated and will be removed`);
	nedit /USERFOLDER/UnrealEngine/Engine/Source/Runtime/Core/Public/Linux/LinuxSystemIncludes.h &
	//#include <sys/sysctl.h>
apply fix4 (already done)
apply fix5 (`undefined symbol: __atan2f_finite`);
	rm /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Lib/Linux/x86_64-unknown-linux-gnu/*.a
	cp /USERFOLDER/PhysX414/Lib/*.a /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Lib/Linux/x86_64-unknown-linux-gnu/
make ARGS="-clean"
make
cd Engine/Binaries/Linux/
./UE4Editor

Windows

Windows Part 1 (Download UE4.14.3 source):

Open git command line
cd X:/USERFOLDER
git clone https://github.com/EpicGames/UnrealEngine.git
cd UnrealEngine
git checkout tags/4.14.3-release
apply fix2 (`Failed to download 'http://cdn.unrealengine.com/dependencies/..': The remote server returned an error`);
	go to https://github.com/EpicGames/UnrealEngine/releases/tag/4.14.3-release
	download Commit.gitdeps.xml 
	copy Commit.gitdeps.xml X:/USERFOLDER/UnrealEngine/Engine/Build/Commit.gitdeps.xml
Setup.bat [download dependencies]	
GenerateProjectFiles.bat

Windows Part 2 (recompile UE4.14.3 PhysX library for Windows):

Install dependencies (minimal);
	https://cmake.org/download/ (e.g. https://github.com/Kitware/CMake/releases/download/v3.27.1/cmake-3.27.1-windows-x86_64.msi)
	text editor (e.g. TextPad)
mkdir X:/USERFOLDER/PhysX414/Lib
mkdir X:/USERFOLDER/PhysX414/cmakeworking-PhysX
mkdir X:/USERFOLDER/PhysX414/cmakeworking-APEX
Repeat for each sub library (PhysX, APEX):
	open cmake gui
	source code; 
		X:/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/PhysX_3.4/Source/compiler/cmake/windows
		OR: X:/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/APEX_1.4/compiler/cmake/windows
	binaries; 
		X:/USERFOLDER/PhysX414/cmakeworking-PhysX
		OR: X:/USERFOLDER/PhysX414/cmakeworking-APEX
	Configure 
		Generator: Visual Studio 14 2015
		optional platform: x64
		use default native compilers
		Finish
	Environment - Add Entries
		GW_DEPS_ROOT X:\USERFOLDER\UnrealEngine\Engine\Source\ThirdParty\PhysX
		CMAKE_MODULE_PATH X:\USERFOLDER\UnrealEngine\Engine\Source\ThirdParty\PhysX\Externals\CMakeModules
	Configure
	Generate
	Open X:/USERFOLDER/PhysX414/cmakeworking-PhysX/PhysX.sln generated Visual Studio .sln file
	OR: Open X:/USERFOLDER/PhysX414/cmakeworking-APEX/Apex.sln generated Visual Studio .sln file
	select build type [release]
	build
copy X:/USERFOLDER/PhysX414/cmakeworking-PhysX/release/*.lib X:/USERFOLDER/PhysX414/Lib
copy X:/USERFOLDER/PhysX414/cmakeworking-PhysX/pxshared_bin/release/*.lib X:/USERFOLDER/PhysX414/Lib
copy X:/USERFOLDER/PhysX414/cmakeworking-APEX/release/*.lib X:/USERFOLDER/PhysX414/Lib

Windows Part 3 (compile UE4.14.3 with updated PhysX library):

rm X:/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Lib/Win64/VS2015/*.lib
cp X:/USERFOLDER/PhysX414/Lib/*.lib X:/USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Lib/Win64/VS2015/
Open X:/USERFOLDER/UnrealEngine/UE4.sln
select build type [Development/..]
build
cd Engine/Binaries/Win64/
open UE4Editor.exe

Note Linux UE4.14.3 currently throws an unrelated Error at ~50% startup; “appError called: Assertion failed: Assertion failed: ReaderPos + Num <= ReaderSize”

See Windows Part 2b/Linux Part 2b below for a more robust method to recompile the PhysX library for Windows/Linux respectively (UE4.14.3).

This is a better method using RunUAT.bat (from PhysX Source Guide | Unreal Engine Community Wiki / [URGENT] Default Movement Component causing major dedicated server performance issues (Linux Only))

Windows

Windows Part 1 (Download UE4.14.3 source):

Open git command line
cd X:/USERFOLDER
git clone https://github.com/EpicGames/UnrealEngine.git
cd UnrealEngine
git checkout tags/4.14.3-release
apply fix2 (`Failed to download 'http://cdn.unrealengine.com/dependencies/..': The remote server returned an error`);
	go to https://github.com/EpicGames/UnrealEngine/releases/tag/4.14.3-release
	download Commit.gitdeps.xml 
	copy Commit.gitdeps.xml X:/USERFOLDER/UnrealEngine/Engine/Build/Commit.gitdeps.xml
Install Linux cross-compile toolchain for UE4.14.3;
	https://docs.unrealengine.com/4.26/en-US/SharingAndReleasing/Linux/GettingStarted/	
	https://cdn.unrealengine.com/CrossToolchain_Linux/v8_clang-3.9.0-centos7.exe
Setup.bat [download dependencies]	
GenerateProjectFiles.bat

Windows Part 2a (patch UE4.14.3 PhysX library for Linux UB20.04):

Open;
	X:\USERFOLDER\UnrealEngine\Engine\Source\ThirdParty\PhysX\PxShared\src\compiler\cmake\Linux\CMakeLists.txt
	X:\USERFOLDER\UnrealEngine\Engine\Source\ThirdParty\PhysX\PhysX_3.4\Source\compiler\cmake\Linux\CMakeLists.txt
	X:\USERFOLDER\UnrealEngine\Engine\Source\ThirdParty\PhysX\APEX_1.4\compiler\cmake\Linux\CMakeLists.txt
remove all instances; " -ffast-math"

Windows Part 2b (recompile UE4.14.3 PhysX library for Windows):

apply fix6 (`Program.Main: ERROR: AutomationTool terminated with exception: System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified.. UnrealEngine\Engine\Source\Programs\AutomationTool\AutomationUtils\ProcessUtils.cs :line 797`);
	open X:/USERFOLDER/UnrealEngine/Engine/Source/Programs/AutomationTool/Scripts/BuildPhysX.Automation.cs
	//[RequireP4]	(comment out line)
cd X:/USERFOLDER/UnrealEngine/Engine/Build/BatchFiles
call RunUAT.bat BuildPhysX -TargetPlatforms=Win64 -TargetConfigs=profile+release+checked -TargetWindowsCompilers=VisualStudio2015 -SkipCreateChangelist

Windows Part 3 (compile UE4.14.3 with updated PhysX library):

Open X:/USERFOLDER/UnrealEngine/UE4.sln
select build type [Development/..]
build
cd Engine/Binaries/Win64/
open UE4Editor.exe

Linux

Linux Part 1 (Download UE4.14.3 source):

cd /USERFOLDER
git clone https://github.com/EpicGames/UnrealEngine.git
cd UnrealEngine
git checkout tags/4.14.3-release
apply fix1 (`Package clang-3.9 is not available, but is referred to by another package`);
	nedit /USERFOLDER/UnrealEngine/Engine/Build/BatchFiles/Linux/Setup.sh
	remove `clang-3.9` reference from `DEPS` `assume the latest, this is going to be a moving target`
apply fix4 (`Package clang-3.9 is not available, but is referred to by another package`);
	download and extract https://releases.llvm.org/3.9.0/clang+llvm-3.9.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz
	cd clang+llvm-3.9.0-x86_64-linux-gnu-ubuntu-16.04
	sudo cp -r * /usr/local/
	sudo ln -s /usr/local/bin/clang++ /usr/local/bin/clang++-3.9
apply fix2 (`Failed to download 'http://cdn.unrealengine.com/dependencies/..': The remote server returned an error`);
	go to https://github.com/EpicGames/UnrealEngine/releases/tag/4.14.3-release
	download Commit.gitdeps.xml 
	cp Commit.gitdeps.xml /USERFOLDER/UnrealEngine/Engine/Build/Commit.gitdeps.xml
./Setup.sh [download dependencies]	
./GenerateProjectFiles.sh

Linux Part 2b (recompile UE4.14.3 PhysX library for Linux) [using Windows]:

Switch to Windows UE4.14.3 source folder
Add an environment variable named LINUX_ROOT, the value of which is the absolute path to your toolchain (without a trailing backslash): 
	Control Panel->System->Advanced system settings->Advanced->Environment variables->New
		LINUX_ROOT
		C:\UnrealToolchains\v8_clang-3.9.0-centos7\x86_64-unknown-linux-gnu
apply fix6 (already done)
cd X:/USERFOLDER/UnrealEngine/Engine/Build/BatchFiles
call RunUAT.bat BuildPhysX -SkipCreateChangelist -SkipSubmit -TargetLibs=PhysX+APEX -TargetPlatforms=Linux -TargetConfigs=checked+profile+release
copy all X:\USERFOLDER\UnrealEngine\Engine\Source\ThirdParty\PhysX\Lib\Linux\x86_64-unknown-linux-gnu to Linux

Linux Part 3 (compile UE4.14.3 with updated PhysX library):

cd /USERFOLDER
apply fix1 (already done)
apply fix2 (already done);
apply fix3 (`The <sys/sysctl.h> header is deprecated and will be removed`);
	nedit /USERFOLDER/UnrealEngine/Engine/Source/Runtime/Core/Public/Linux/LinuxSystemIncludes.h &
	//#include <sys/sysctl.h>
apply fix4 (already done)
apply fix5 (`undefined symbol: __atan2f_finite`);
	rm /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Lib/Linux/x86_64-unknown-linux-gnu/*.a
	copy recompiled libraries X:\USERFOLDER\UnrealEngine\Engine\Source\ThirdParty\PhysX\Lib\Linux\x86_64-unknown-linux-gnu\*.a to /USERFOLDER/UnrealEngine/Engine/Source/ThirdParty/PhysX/Lib/Linux/x86_64-unknown-linux-gnu/
make ARGS="-clean"
make
cd Engine/Binaries/Linux/
./UE4Editor

For reference, the previous method I provided to recompile PhysX for Linux UE4.14.3 (using cmake-gui) throws an error at ~90% startup; in nvidia::apex::ApexScene::destroy (this=<optimised out>) mModuleScenes.back()->release(). If you wish to further debug the previous method;

  • change all clang 3.9.1 to clang 3.9.0 (fix appError called: Assertion failed: Assertion failed: ReaderPos + Num <= ReaderSize)
  • move fix1 before fix4
  • execute make for each sub library (PhysX, APEX)
  • Repeat for each build type; profile or checked or release (see set var in GUI list; CMAKE_BUILD_TYPE)
  • enable PhysX library debugging;
    • open /USERFOLDER/UnrealEngine/Engine/Saved/UnrealBuildTool/BuildConfiguration.xml
    • add inside <BuildConfiguration>: <bDebugBuildsActuallyUseDebugCRT>true</bDebugBuildsActuallyUseDebugCRT>
  • build UE4Editor for debugging;
    make ARGS="-clean"
    make UE4Editor-Linux-Debug
    cd Engine/Binaries/Linux/
    gdb -ex run --args UE4Editor-Linux-Debug