How do I create a conditional compile symbol without needing to create a new build configuration to use it?

63 Views Asked by At

For full context: I'm building an app in .NET MAUI (for Android, iOS, and Windows). On Windows, I want to have multiple different flavours, for example: a build that can go to the Microsoft Store, one that can go to Steam, and one that can go to Epic Games. Each of these stores has their own SDKs and bespoke code and I would like to build them differently (e.g. packaged for the Windows Store, unpackaged for Steam/Epic).

I want to define a preprocessor directive/conditional compile symbol/constant that allows me to only run specific code for the target flavour. For example, I'd like to be able to have custom code that only runs on the Steam build:

#if STEAM
// custom Steam code
#endif

Then, I want to be able to (in a build pipeline), build a Steam version in Release mode. I want to do the same for all the flavours I have. However, I'm really not sure how to utilise these constants without needing to create a build configuration for each. I want to avoid the situation where I have many duplicated build configs and a messy .csproj with copy pasted settings. I don't want to have:

  • Debug
  • Release
  • DebugSteam
  • ReleaseSteam
  • DebugEpic
  • ReleaseEpic
  • DebugItch
  • ReleaseItch
  • etc.

I think I'd be prone to making a mistake and it feels wrong to have to switch between these.

At the moment, I just have all the code + dependencies in the Windows project. And I'm commenting/uncommenting bits to test out but its not really viable when it to long term maintainability or for pipelines.

My goal would be to have a "Debug" and a "Release" mode configuration only. Then, on my pipeline I can choose to build between Android/iOS/Windows (which is working today). I then want to further configure on Windows whether I'm targeting Windows Store/Steam/Epic and include the correct additional code and correct NuGet packages.

0

There are 0 best solutions below