Run a custom command before build

I would like to insert a custom build step (e.g. running a batch file) before the compilation begins. How would I go about adding a custom build step to the build process.

Sorry for the duplicate, it said it had failed.

UE4 has it own building system called UBT and uses c# build script which you can find in source, you might try to plug yourself there

Yeah, I’ve been looking through the ModuleRule.cs to see if there was a custom commands list. I was hoping to not have to make any changes to the UBT itself since I’d have thought this was a fairly standard requirement to be able to hook custom rules in to the pipeline.

Inspired by 's comment I have come up with a very hacky solution and would very much like to see the proper way of doing it.

I realised that the constructor from the projects ModuleRules must be executed so I just bunged a Process.Start into there.

There are a few caveats:

Firstly, this is called twice in a build, so if your operation is time consuming this could be a problem.

Secondly, if you launch a batch file the working directory will be [Unreal Engine Location]\Engine\Source. To get the path, I came up with another awful hack of a solution:

StackTrace st = new StackTrace(new StackFrame(true));
StackFrame sf = st.GetFrame(0);
string fileName = sf.GetFileName();
DirectoryInfo ContaingingDirectory = new DirectoryInfo(Path.GetDirectoryName(fileName));

DirectoryInfo CorrectDirectory = ContainginDirectory.Parent.Parent;

The CorrectDirectory is the root directory of your project.

You can use PreBuildSteps and PostBuildSteps string arrays in your *.Target.cs file.

// Specifies a list of steps which should be executed before/after this target is built, in the context of the host platform's shell.
// The following variables will be expanded before execution:
// $(EngineDir), $(ProjectDir), $(TargetName), $(TargetPlatform), $(TargetConfiguration), $(TargetType), $(ProjectFile).

Emphasis on “in the context of the host platform’s shell”.