How to add build of non-UE-linked executable to UE build?

I’ve got a viable workaround to my previous question about the working directory of Linux UE4Editor. I’ve found that the process abstraction layer for Unix doesn’t support setting the working directory, as the vfork/exec path it takes does not permit an intervening chdir.

To solve this problem, I’ve written a trivial program called setcwd. It’s about 5 lines long and simply sets the working directory as appropriate, then execvs the actual program. I’ve also patched the UE source code to prepend this new binary to CreateProc calls that include the OptionalWorkingDirectory. This all works great, and now my source control plugin is working again!

The question is: how can I add setcwd to be built alongside the UE4 engine?

It does not, and really should not, link to any UE4 code at all. I just want UE4 to build it (with my own makefile if necessary), and then copy the binary into the appropriate Binaries directory. I can obviously do this manually. But I would like to submit this patch to Epic for inclusion in the mainline version of UE4, as it adds critical functionality that would be of benefit on all Unix-style platforms.

You need to do it in build scripts. Look in to ThirdParty folder as it has ton of examples of build scripts that builds non-UE stuff as modules and there own *.build.cs. Generally you could include it to your plugin as extra module, but im not sure if it gonna be past thru marketplace validation or else you want to release this plugin on github. If you really plan to submit pull request then that module probably will need to land in Programs or maybe ThirdParty

I didn’t actually write the original plugin. That’s not relevant here. Sorry if I threw you off with that.

My patch modifies Engine/Source/Runtime/Core/Private/Unix/UnixPlatformProcess.cpp and adds a unix-portable shim program that should build and function on all posix-style systems without modification, and is necessary on all of them. Ideally, this shim program should be built in the same environment as the UE4 engine, so I would prefer not to package it as a binary.

I’ll check out ThirdParty again. I’m having trouble finding a module that builds a program that doesn’t link back into UE4 code. Could you provide an example of a ThirdParty tool that builds a raw executable?

The patch and shim program correct a deficiency in the documented, expected behavior of the UE4 engine. So I’m really seeking information on how to properly integrate this build into the core engine build.

Would it be better to submit a pull request on github with an incomplete solution and seek knowledge there to complete the patch?

Your patch really need to be solid to be accepted and you need to explain full reasoning behind it, you need to convince Epic to include it. So you need to include everything including program… which i’m not sure if Epic will like this kind of tactic.

You right there only Libery builds didn’t notice that sorry.

I lurked a little bit in Programs (it actually seems to be more fitting to search there then ThirdParty) and i found example of one program that does not seem to be library and don’t call out any of UE4 modules and this is Windows/BootstrapPackagedGame has build script and only single library reference.

Based on that i assume thet UBT works with program by searching for cpp file of same name as the “module” (which really is not a module) and that file should have the Main function containing main code, rest is pure cpp and any configuration needed you do in build script as in any other module (search ModuleRules.cs and TargetRules.cs in UBT source code for all possible configuration you can do with). So maybe try searching that way.

Thank you for the clue! I’m certain this will point me in the right direction.

Your patch really need to be solid to be accepted and you need to explain full reasoning behind it, you need to convince Epic to include it.

Of course! But we are professionals doing active native game development on Linux+SteamVR. The experience is overall very good, and so I would like to contribute back this fix so that UE4 can have even better Linux support.

This is my craft and my profession, and part of insuring my patch is solid is conforming to established project practices. Which is why I’m asking, and why I appreciate your help. :slight_smile:

So you need to include everything including program… which i’m not sure if Epic will like this kind of tactic.

The docs for CreateProc says it should take an optional working directory. This argument is silently ignored on unix targets, which breaks external tool integration that works with path names generated by external tools and expects them to be generated relative to the working directory they specified in the CreateProc method call. It also simply doesn’t do what it says it will do, which shouldn’t be allowed to stand.

Epic may not accept the patch (it’s certainly their right), but there is no other way to set the working directory of an arbitrary child process created with posix_spawn(). The traditional approach would be fork(); chdir(); exec();, but UE4 uses posix_spawn to avoid the pagetable copy. Which makes sense if a giant program like UE4Editor is spawning a tiny program like UnrealHeaderTool.

This approach has very little overhead: the shim program execvs the target program in place without an additional fork, replacing itself after setting the working directory. The rest of the environment, which is set by UE4 and CreateProcess is inherited by the child process. It’s about as elegant as you can get to solve this problem on this platform.

Thank you!