Can I build multiple configurations of a project within one solution configuration?
Asked Answered
S

2

12

I would like to build the same project twice in the same solution configuration, varying some #define flags to toggle features. Both binaries will be deployed with different names.

The solutions that I know could work:

  1. Add a solution configuration - But I will then need to build the solution twice, which I would prefer to avoid. Both project configurations will always be built.

  2. Copy the project - But then I have the overhead of maintaining a new project when I only want to maintain a different configuration.

  3. Batch build - I avoid using batch build as I use both devenv for local development and msbuild for continuous integration.

Any other ideas or suggestions?

Selfliquidating answered 7/12, 2012 at 18:33 Comment(0)
G
7

Just figured out a way to do what you asked for. Create one msbuild file (I named mine multiple.proj) and add the script below.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Choose>
    <When Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
      <ItemGroup>
        <ProjectToBuild Include="$(MSBuildProjectName).csproj">
          <Properties>Configuration=Release</Properties>
        </ProjectToBuild>
      </ItemGroup>
    </When>
  </Choose>
  <Target Name="BeforeBuild">
    <Message Text="Building configuration $(Configuration)..." />
  </Target>
  <Target Name="AfterBuild">
    <MSBuild Projects="@(ProjectToBuild)"/>
  </Target>
</Project>

</type>
</this>

Import the script on your projects (csproj or vbproj):

<Import Project="..\multiple.proj" />

This script tells msbuild to build again your project with another configuration as an AfterBuild event. I used Debug/Release to make the example, but you can easily change the script to support other configurations, or make the decision to build again based on other variables.

Be careful because you're running two builds at once, so build errors can be harder to understand.

Hope this helps.

Gravettian answered 17/1, 2013 at 20:40 Comment(0)
B
0

You could do it, but perhaps a simpler and more natural alternative is to add a new project which duplicates the original, and have the new project define its own pre-processor macros which are picked up by the #ifdef statements. That way, both projects can still share the same source files, but it is clear what each project does differently, and the build process is not "hidden" away in a post-build step.

Beaufort answered 31/5, 2024 at 9:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.