There is some opensource project, which should be build on windows and on Linux. On Linux it should be built differently, and I want to use LINUX preprocessor constant in C# sources included to .csproj for that, i.e.
#if !LINUX
// windows specific code (some TFS integration)
#else
// linux specific code (absence of TFS integration)
#endif
but the same .csproj is used in both Windows and Linux builds, and I can't just add define to project settings in .csproj like this (because this define will be effective for both platforms):
- <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <DefineConstants>DEBUG;TRACE;LINUX</DefineConstants>
It is possible to create 2 more separate configurations, and pass the configuration name to msbuild from command line. But this violates DRY principle (configurations are duplicated with no need to do that).
It is possible to determine OS authomatically during build, instead of passing this information from outside.
People recommended me to create and include script (for example operating_system.targets, or should it be operating_system.props?) and use it from projects, like this:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" />
+ <Import Project="$(SolutionDir)\operating_system.targets" />
There are several ways to determine operating system, for example http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform
if (Type.GetType("Mono.Runtime") != null)
IsMono = true; // we're on Mono
else
IsMono = false;
int p = (int) Environment.OSVersion.Platform;
if ((p == 4) || (p == 6) || (p == 128)) {
IsUnix = true; // we're on Unix
} else {
IsUnix = false;
}
public static bool IsLinux
{
get {
bool isLinux = System.IO.Path.DirectorySeparatorChar == '/';
return isLinux;
}
}
and there is also __MonoCS__ define - see How can I conditionally compile my C# for Mono vs. Microsoft .NET? (I can't use just __MonoCS__, because it will be defined while compiling with mono compiler on Windows)
So, I want an example of that operating_system.targets file, which will define LINUX constant for C# code when project compiles on linux.