Skip to content

Instantly share code, notes, and snippets.

@AndreaCatania
Last active April 20, 2024 10:40
Show Gist options
  • Save AndreaCatania/1bb9548ae69531f9359865be250d4db3 to your computer and use it in GitHub Desktop.
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.
// 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