MSBuild file for .Net 1.0, 1.1, 2.0, Compact Framework 1.0 & 2.0 and Mono
Intro:
I'm currently working in a library that has to target both .Net 2.0 and .Net
Compact Framework 2.0.
My initial approach was to have two projects for each assembly. The source
code would be all the same, with #if's testing for the compact or the full
framework. One of the projects would have all the files, and the other all
the files from the first linked.
As the number of source files and assemblies growed, this turned into
problematic.
At some time, I also wanted to test if my code worked in mono, even if it
wasn't required.
So I thought of using msbuild to customize my build. I started googling, and
found msbuild files from Jomo Fisher and Robert McLaws that would target
previous versions of .Net. And I also found msbuild files from Mike Hull and
Andy Waddell that would target Mono. But nothing to target Compact Framework
So, a week's work of researching the docs, posting on miscrosoft msbuild
forum and other newsgroups, and I've come up with a msbuild file that does
all that I wanted.
I'm sharing it here so everyone can use it and also to get improvements for
it.
You can get it athttps://ovatsus.ath.cx/Ovatsus.CSharp.targets
Usage Instructions:
-Copy the Ovatsus.CSharp.targets to C:\Program Files\MSBuild
-Replace the <Import> element from your project file with <Import
Project="$(MSBuildExtensionsPath)\Ovatsus.CSharp.targets"/>
After that you have 11 new Platforms:
.NET 1.0
.NET 1.1
.NET 2.0
.NET CF 1.0 (Pocket PC 2003)
.NET CF 1.0 (Smartphone 2003)
.NET CF 2.0 (Pocket PC 2003)
.NET CF 2.0 (Windows CE 5.0)
.NET 1.1 (Mono Compiler & Libraries)
.NET 2.0 (Mono Compiler & Libraries)
.NET 1.1 (Mono Libraries)
.NET 2.0 (Mono Libraries)
The existing Any CPU platform remains, and if you build with that platform
selected you will build for all targets sequentially. It continues to be the
default platform so if you just invoke msbuild.exe from the project
directory, you'll build everything.
The difference between the "Mono Libraries" platforms and "Mono Compiler &
Libraries" platforms is that in the first, the compiler is still csc and not
the mono mcs or gmcs. These targets exists so you can debug inside VS.
There are also defined some conditional compilation symbols. They are,
respectively for the above platforms:
NET_1_0
NET_1_1
NET_2_0
NET_CF;NET_CF_1_0;PocketPC
NET_CF;NET_CF_1_0;Smartphone
NET_CF;NET_CF_2_0;PocketPC
NET_CF;NET_CF_2_0;WindowsCE
NET_1_1;Mono
NET_2_0;Mono
NET_1_1;Mono
NET_2_0;Mono
All works fine from msbuild.exe. From withing VS there are a few things
worth notice:
1-You can't deploy to the compact framework, and you can design cf forms
visually, but VS won't know your form is for the CF, and will probably set
properties that don't exist in CF. You can always create a specific CF
project, that just uses all code, including forms, from the multi-platform
project
2-If you have project references, VS will only find the referenced project
assemblies if you set both projects to the same Platform, and that platform
can't be AnyCPU. What you can do is build for all platforms in the
referenced project, unload it and then build for any platform you like
(including all at once - AnyCPU) in the referencee project. In this case VS
will work correctly. Note that from msbuild.exe it all works correctly.
Also, you can override the platform names before including
Ovatsus.CSharp.targets by overriding the following properties:
NET_1_0
NET_1_1
NET_2_0
NET_CF_1_0_PocketPC
NET_CF_1_0_Smartphone
NET_CF_2_0_PocketPC
NET_CF_2_0_WinCE
Mono_1_1_Full
Mono_2_0_Full
Mono_1_1_LibOnly
Mono_2_0_LibOnly
You can also change the conditional compilation symbols, by overriding these
properties:
NET_1_0_Constant
NET_1_1_Constant
NET_2_0_Constant
NET_CF_1_0_Constant
NET_CF_2_0_Constant
NET_CF_Constant
Mono_Constant
I don't suport overriding the symbols PocketPC, Smartphone or WindowCE,
because these names already existed in VS when building for CF.
You can also override these two properties:
BinDir
ObjDir
By default, they are setted to bin and obj, respectively. You can override
them to something else, if you want. This is usefull if you have the source
code in cvs or svn, for example, and don't want to have to mess with
settings the ignore directories. In that case, you can set those properties
to ..\..\bin and ..\..\obj, for example, so it's out of your working copy.
When building inside VS, it will stubbornly create the obj directories below
your project directory, but as it only contains more empty subdirectories,
that's less of a problem.
You can also override the property MonoPath, that's hardcoded to C:\Program
Files\Mono-1.1.9.2, or set an environment variable with that name. Please
don't surround it with ".
Best regards,
Gustavo Guerra

