Build cuda files for plugin (using UBT)

Hey,

I’m planning to use cuda in one of my plugins. If I prebuild a static lib I already can use the cuda methods.
In the next step I want to compile the cuda .cu files while (re-)building the plugin.

For this I need to change the compiler to nvcc and also add some statements to the vcx project files (visual studio). Is this possible without changing the Engine source code in “Engine\Source\Programs\UnrealBuildTool\ProjectFiles\VisualStudio\VC*.cs”?

If yes, how can I add this custom build steps to my *build.cs files?

Greets,

It a bit tricky because you can do in configuration in target build script not module build script and target is individual for each project you make as well as program. the config target rules varbales are:

	/// <summary>
	/// List of scripts to run before building
	/// </summary>
	FileReference[] PreBuildStepScripts;

	/// <summary>
	/// List of scripts to run after building
	/// </summary>
	FileReference[] PostBuildStepScripts;

So you might need to try adding C# code to execute proper scripting or try to make program module which allows to create sperate target script. You could also try to build using plugin it self, i mean those files are loaded in runtime by plug in anyway isn’t it, so why not make it build it before and potentially also build on packaging?

Regardless what you do, if you plan to release this plugin in UE4 market place i recommend you to review requirements as they are quite strict and not all solutions will work here

You might be able to achieve this using a prebuild step. Here is someone talking about adding pre/post build steps into the build process: Pre/Post build step in UBT - Programming & Scripting - Unreal Engine Forums

If it is done in a prebuild step you should be able to call any shell code including executing executable programs. If you had something very complex you could create your cuda build chain as a separate C# program and call it as a pre-build step.

I have not actually tried to customize the tool chain in this way so you will probably have to experiment more but this might be a road you can go down to achieve this.

Hey, I think in the end it is similar to adding these prebuild steps to the .uproject file (as Brian mentioned) - it’s easier.
I tried it and it works so far, but if I want to use some unreal methods / header files I need to modify the UBT.
In the first shot I will work in this way, but maybe in the near future, it is necessary to think about modifying the UBT.

I am not sure this answer will work as I just have a theory that it should work.

As mentioned adding a script to FileReference[] PreBuildStepScripts that will initiate build of your CUDA VS project and you can then move the necessary libs and dlls alone to your unreal project referred directories.

For initiating build you can use command using MSBuild.exe
Refer MSBuild doc

For executing the build command use {EngineDir}\Engine\Source\Programs\UnrealBuildTool\System\ExternalExecution.cs

		/// <summary>
		/// Run an external native executable (and capture the output), given the executable path and the commandline.
		/// </summary>
		public static int RunExternalNativeExecutable(string ExePath, string Commandline)

Please let me know if this works,I am also trying to achieve something similar to this and your response will be of much help to me :slight_smile:

I have chosen the solution to define these steps in the *.uplugin file. It’s not the best solution, but for now this is enough. If I need to work more on it I will let you know in this post.

Best,