ProjectDefinitions / Precompiler Definitions in Target.cs and Building via Command Line

Hi,

I am compiling our project via RunUAT:

call %UE_5.3%/RunUAT.bat BuildCookRun -noP4 -utf8output -cook -project="%cd%/ProjectName.uproject" -platform=Win64 -stage -archive -package -build -pak -iostore -compressed -prereqs -archivedirectory="%cd%/PackagedBuilds/EGSWindows" -clientconfig=Shipping -nodebuginfo -target=ProjectNameEGS

I am specifying a custom target that points to a custom Target.cs file for Epic Game Store that looks like this:

public class MyGameEGSTarget : TargetRules
{
	public MyGameEGSTarget( TargetInfo Target) : base(Target)
	{
        Type = TargetType.Game;
        DefaultBuildSettings = BuildSettingsVersion.V2;
        ExtraModuleNames.AddRange(new string[] { "MyGame" });

        IncludeOrderVersion = EngineIncludeOrderVersion.Latest;

        CustomConfig = "EGS";


        ProjectDefinitions.Add("PLATFORM_EGS=1");
    }
}

I am using the precompiler check like this in C++:


#if PLATFORM_EGS
	LoginEGS(Identity, UserIndex);
#elif PLATFORM_STEAM
	LoginSteam(Identity, UserIndex);
#endif

Still, when compiling the target, RunUAT shows the following error:

[171/932] Compile [x64] CreateSessionRequest.cpp
...\Private\Services\CreateSessionRequest.cpp(27): error C4668: 'PLATFORM_EGS' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
...\Private\Services\CreateSessionRequest.cpp(29): error C4668: 'PLATFORM_STEAM' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'

Why is this happening?

I noticed that the output log shows that among other targets, the following is being built: -Target="MyGameEditor Win64 Development

Does this mean that effectively the precompiler settings from the MyGameEditor.target.cs are being used? Adding the PLATFORM_EOS=1 flag in MyGameEditor.target.cs fixes the error, but that is not what I want, since I need different targets defined per the CLI in order to build for all platforms.

Or am I building two targets (Editor and EGS)? If yes, is there a way to remove the Editor target from the build?

Thanks in advance

Okay I have been able to fix this and it took me a few hours, so I am leaving my findings here in case anyone comes across a similar issue.

First of all, there is the RunUAT BuildCookRun parameter -NoCompileEditor, which will skip the -Target=“MyGameEditor Win64 Development” that is passed to the UnrealBuildTool.dll automatically. However doing that resulted in an error that the plugin VisualStudioTools could no longer be found or similar. I am no longer able to reproduce this error and currently I can use the -NoCompileEditor token without issues. It could be that it is required to build the editor and right now it only works because I did not clean before building.

However the -NoCompileEditor flag is not needed. It seems that without -NoCompileEditor the .cpp files are compiled twice, once for the Editor target and once for MyGameEGS target. The problem was in my code, where I used this:

#if PLATFORM_EGS
	LoginEGS(Identity, UserIndex);
#elif PLATFORM_STEAM
	LoginSteam(Identity, UserIndex);
#endif

This would always result in the compile error I posted earlier, because my editor target does not define any of these. The correct way would be to check if any of them is actually defined like so:

#ifdef PLATFORM_EGS
	LoginEGS(Identity, UserIndex);
#elif defined(PLATFORM_STEAM)
	LoginSteam(Identity, UserIndex);
#endif

This way, the compiler will not complain, even if none of them are defined. Furthermore the MyGameEGS target compiles correctly with the PLATFORM_EGS flag being defined from the MyGameEGS.Target.cs file, even though the Editor target is compiled alongside without the PLATFORM_EGS Flag.