Greetings,
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 at https://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

MSBuild file for .Net 1.0, 1.1, 2.0, Compact Framework 1.0 & 2.0 and Mono
jacoroux
Wow, that's pretty nifty. Note that we do have targets files for building Compact Framework: they ship with Visual Studio 2005, and they support building both v1.1 and v2. I'm not sure where they're installed, however. You should be able to figure it out by creating a new "Smart Device" project then cracking the project file.
Neil
Venu Yankarla
CorkChop
I've made a correction, I think. In the MonoPath condition, you use != instead of ==. Also, in deference to the JAVA_HOME tradition of pointing to the bin directory (for unmodified inclusion in PATH), I changed the sense. Granted, this causes output containing stuff like Mono-1.1.9.3\bin\..\bin and ...\bin\..\lib, but it plays nicely with my existing setup.
I'll take a look when I have time, to see about adding support for the LINQ preview.
Zzeeshan
This looks amazing!
But the link doesn't work :-(
Does anybody have a copy of the file
(Or post it here )
Thanks very very much!
lc247
The link doesn't work can some one post another link
Thanks
Richard Cook - MSFT
Not sure if its the latest version or not.
Sayed Ibrahim Hashimi
www.sedodream.com
Dan Smithtown
I haven't looked at it yet, but you can get the source at svn://sharpdevelop.net/corsavy/trunk/
Steven F
Beautiful!
By any chance, could you add in the Linq/C#3 preview compiler
Since much of it is repetitive, did you create a program to generate this
bkrgr1
Yes, I know. My 1.x platforms comes mostly from the Microsoft.CompactFramework.CSharp.v1 targets file, like Jomo Fisher had already done. The CF support is also mostly a copy of the Microsoft.CompactFramework.*.targets, with some small modifications. Initially, I had imports to them with just a few overrides, but then I found out that VS didn't work correctly with conditional imports, so I just had to copy paste the whole thing. If you look at the file you'll see comments like
<!--Microsoft.CompactFramework.Common-->,
<!--Microsoft.CompactFramework.CSharp:--> and
<!--Microsoft.CompactFramework.CSharp.v1:-->,
that precede the section of code copied from those files. See this thread: http://forums.microsoft.com/msdn/ShowPost.aspx PostID=111059
catalyst
Thanks!
I'm not familiar with Linq yet, and it's not in my current interests for now, so I'll probably won't. But if you look at the generated project file, I think it won't be too difficult for you to combine that with my targets files
Nope, all by hand
Regards,
Gustavo Guerra