Last active
January 23, 2025 14:36
-
-
Save AndreaCatania/1bb9548ae69531f9359865be250d4db3 to your computer and use it in GitHub Desktop.
Utility used to execute console commands (like the one to compile CMake projects) during the compilation steps of Unreal Engine.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Utility used to execute console commands (like the one to compile CMake projects) during the compilation steps of Unreal Engine. | |
public class MBuildUtils | |
{ | |
public string MModulePath = ""; | |
// Taken from UE4Cmake - Kudos to `caseymcc` | |
private Tuple<string, string> GetExecuteCommandSync() | |
{ | |
string cmd = ""; | |
string options = ""; | |
if((BuildHostPlatform.Current.Platform == UnrealTargetPlatform.Win64) | |
#if !UE_5_0_OR_LATER | |
|| (BuildHostPlatform.Current.Platform == UnrealTargetPlatform.Win32) | |
#endif//!UE_5_0_OR_LATER | |
) | |
{ | |
cmd="cmd.exe"; | |
options="/c "; | |
} | |
else if(IsUnixPlatform(BuildHostPlatform.Current.Platform)) | |
{ | |
cmd="bash"; | |
options="-c "; | |
} | |
return Tuple.Create(cmd, options); | |
} | |
public int ExecuteCommandSync(string command) | |
{ | |
var cmdInfo = GetExecuteCommandSync(); | |
if(IsUnixPlatform(BuildHostPlatform.Current.Platform)) | |
{ | |
command=" \""+command.Replace("\"", "\\\"")+" \""; | |
} | |
Console.WriteLine("Calling: "+cmdInfo.Item1+" "+cmdInfo.Item2+command); | |
var processInfo = new ProcessStartInfo(cmdInfo.Item1, cmdInfo.Item2+command) | |
{ | |
CreateNoWindow=true, | |
UseShellExecute=false, | |
RedirectStandardError=true, | |
RedirectStandardOutput=true, | |
WorkingDirectory=MModulePath | |
}; | |
StringBuilder outputString = new StringBuilder(); | |
Process p = Process.Start(processInfo); | |
p.OutputDataReceived+=(sender, args) => {outputString.Append(args.Data); Console.WriteLine(args.Data);}; | |
p.ErrorDataReceived+=(sender, args) => {outputString.Append(args.Data); Console.WriteLine(args.Data);}; | |
p.BeginOutputReadLine(); | |
p.BeginErrorReadLine(); | |
p.WaitForExit(); | |
if(p.ExitCode != 0) | |
{ | |
Console.WriteLine(outputString); | |
} | |
return p.ExitCode; | |
} | |
private bool IsUnixPlatform(UnrealTargetPlatform platform) { | |
return platform == UnrealTargetPlatform.Linux || platform == UnrealTargetPlatform.Mac; | |
} | |
public static string GetCMake() | |
{ | |
string program = "cmake"; | |
if((BuildHostPlatform.Current.Platform == UnrealTargetPlatform.Win64) | |
#if !UE_5_0_OR_LATER | |
|| (BuildHostPlatform.Current.Platform == UnrealTargetPlatform.Win32) | |
#endif//!UE_5_0_OR_LATER | |
) | |
{ | |
program+=".exe"; | |
} | |
return program; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment