MSBuild file for .Net 1.0, 1.1, 2.0, Compact Framework 1.0 & 2.0 and Mono

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 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

[5340 byte] By [ovatsus] at [2007-12-17]
# 1

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?

KeithFarmer at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 2

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

NeilEnnsMSFT at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 3
Keith Farmer wrote:

Beautiful!

Thanks!


By any chance, could you add in the Linq/C#3 preview compiler?

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


Since much of it is repetitive, did you create a program to generate this?

Nope, all by hand

Regards,
Gustavo Guerra

ovatsus at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 4
Neil Enns MSFT wrote:

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

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

ovatsus at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 5
I've also discovered that the SharpDevelop team has done something like this for SharpDevelop2. It's probably much more elaborate than my work.

I haven't looked at it yet, but you can get the source at svn://sharpdevelop.net/corsavy/trunk/

ovatsus at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 6
Finally installed and played with it -- very cool.

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.

KeithFarmer at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 7
I was able to get the targets file working in VS2005 C#, is there a VS2005 Visual Basic targets file available?
DaveRodegeb at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 8

The link doesn't work? can some one post another link?

Thanks

Cyonix at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 9

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!

SkySigal at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...
# 10
I've posted a copy that I had from a while ago at: http://www.sedodream.com/content/binary/Ovatsus.CSharp.targets.txt

Not sure if its the latest version or not.

Sayed Ibrahim Hashimi
www.sedodream.com

SayedIbrahimHashimi at 2007-9-9 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...

Visual Studio

Site Classified