views:

286

answers:

3

Hi,

I have a C# project in VS2008 which I wish to build for two targets - x86 and the compact framework for dot net (CF.Net) depending on a chosen configuration (say Debug/SmartDeviceDebug etc). How do I go about this? I have an inkling this can be done somehow using the command line and msbuild but I am interested in a solution which works from within VS2008.

Please note for the time being I am using two different projects for the two target types but its a chore to keep the files synchronized between them. Adding files as links also does not help (its not a scalable solution, the names and number of files change too frequently.)

Any and all suggestions will be appreciated.

+3  A: 

I believe the csproj has to be radically different to build for different frameworks. However, there are tricks; for example, you can use wildcards in the csproj. I use this in protobuf-net, which builds on .NET 2.0/3.0/CF 2.0/CF 3.5/Silverlight/Mono - so I know what you mean!

Have a look at my CF 2.0 csproj; in particular:

<ItemGroup>
    <Compile Include="..\protobuf-net\**\*.cs" />
</ItemGroup>

i.e. find all .cs files at any level in the main project.

Marc Gravell
That's usefull ;) thx
Stormenet
I'm going to keep adding them manually, as this trick doesn't auto-generate's the tree with disigner files (they all appear flat)
Stormenet
Ah, fair enough; I just use the other ones to build at the command line, and only maintain the main csproj.
Marc Gravell
A: 

You can't (at least, not that I know of).

First of all, the project references are per project, not per chosen configuration, so you are forced to stick to the cf dll's and so can't use the abilities of the regular framework.

It is a pain in the ass, and I understand your issues because I'm suffering from them to :(

Stormenet
Actually, project references can be per-config. The IDE doesn't support adding such, but it respects them when you build. Just add a "condition" to either a reference or a group, and 'tis done.
Marc Gravell
Isn't the compact framework just a subset of the full framework? In this case, can't a project that targets CF be trivially (or even transparently) re-targeted for the full framework, since it would be restricted to the shares subset?
rwmnau
Well, the compact framework is one big INotImplemented wrapper around the full framework.But yes, they can, that's why we use 2 csproj files. one that targets the files for the compact and one for the full.
Stormenet
+2  A: 

Generally you have two issues - files that are in one type but not the other, and references that differ between the CF and FFx. These problems can be worked around with manual manipulation of the project files, but be forewarned that the VS IDE doesn't appreciate you mucking with these (it doesn't know it should reload the entire project when your configuration changes).

What you can do:

  • start with a CF project (you can start with eitehr, but this is how we do it)
  • Add a new configuration - call it "Desktop" or whatever you'd like
  • Modify the CF solution so it's more obvious (named Debug CF or whatever)
  • Open the project file (*.csproj) with a text editor. You'll find several ItemGroup nodes. The important piece here is that msbuild supports conditionals for this node.

So if you have a node for the project references that looks like this

<ItemGroup>
  <Reference Include="System" />
</ItemGroup>

You can modify it to

<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Desktop|AnyCPU' >
  <Reference Include="System, Version=2.0.0.0, Culture=neutral,
       PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
</ItemGroup>

Note that you have to give it the full name here.

You then can similarly modify the ItemGroup nodes for file inclusion into each project.

Now you've done the easy part. Next you have to massage the PropertyGroup nodes.This is a matter of looking at the CF project file and the FFx project file and putting relevent nodes into conditional parents. Take specific note of things like ProjectTypeGuids and PlatformFamilyName.

All that said, we've found it to be less work to just maintain separate project files for the CF and FFx versions where we need overlap.

ctacke