Monday, April 23, 2012

Bit specific builds in Visual Studio 2010.

Using Conditioned Tags

The Visual Studio 2010 UI does not have options to handle conditional references based on what kind of configuration and platform you are running. For example, you may have a project reference that needs to change depending on whether you are doing a 32 or 64 bit build.

Fortunately, the project file syntax does support conditional references. I was able to find this out on stack overflow.

In our case, we have our DLL's in source control under the project's bin directory. For some reason I kept getting the 64-bit version of the DLL and then found out it was because the Content tags also needed to be conditioned.

Example

Lets say you have a project (MyProject) with two DLL's, 32bit.dll and 64bit.dll, that are referenced by the project and kept under the bin directory for the project:

  • MyProject\bin\x86\32bit.dll
  • MyProject\bin\x64\64bit.dll

You will need to hand edit the MyProject\MyProject.vbproj file and add these entries to it (or change it if they are already in there). The first two entries are for the project references and the last two are for their inclusion as a content item in the project.

   <Reference Include="32bit" Condition="'$(Platform)' == 'x86'">
      <HintPath>bin\x86\32bit.dll</HintPath>
   </Reference>  
   <Reference Include="64bit" Condition="'$(Platform)' == 'x64'">
      <HintPath>bin\x64\64bit.dll</HintPath>
   </Reference>
   ...
   <ItemGroup>    
      <Content Include="bin\x86\32bit.dll" Condition="'$(Platform)' == 'x86'"/>
      <Content Include="bin\x64\64bit.dll" Condition="'$(Platform)' == 'x64'"/>
   </ItemGroup>

Now, whenever the platform is changed to x86, it will only use the x86 conditioned tags and likewise when changed to x64. This can also be applied to other conditions too, check the links below for a complete breakdown of the MSBuild schema and conditions.