C++ – Dynamically determine all solutions, projects and configurations on a build/trigger event and call them all with MSBuild from command line/script

cmsbuildvisual-studio-2008

We're still a little early in setting up our build automation and just using a bat file for now for Win32 C++ solutions. We have about 4 solutions and each has a couple vcproj files.

Each time a new solution or configuration is added I have to update the bat file to reflect the new solution or configuration to call with MSBuild.

I thought perhaps it would be easier to write a tool that parses all the sln files in a given path (and its subtree) and then parse all project files it references for configurations and then call all the builds that way.

Is there an easy way to do this?

Really this is 2 questions:

  1. How can I tell MSBuild just to build all solutions inside a subtree? (I can do a search on them – that is a simple tool I think to write)

  2. How can I tell MSBuild to build all configurations of a solution/vcproj?

We're using MSBuild, bat files, vc2008, c++

Best Answer

Being a PowerShell fan the following reformatted one-liner might be of help:

Get-ChildItem -Recurse -Include *.sln | 
ForEach-Object {  
    $Solution = $_.FullName
    Get-Content $_ | 
    ForEach-Object { 
        if($_ -match '\{[^\}]+[^=]+= ([^\{\s]*)$') { 
            $matches[1] 
        }
    } | Sort-Object -Unique | 
    ForEach-Object { 
        $config =  ([string]$_).Split([char]'|')
        & "$env:windir\Microsoft.NET\Framework\v3.5\msbuild.exe" $Solution /p:Configuration="$($config[0])" /p:Platform="$($config[1])" 
    }
}

This script can be saved as a ps1 file or pasted as a function in your profile. To explain what it does:

  1. find all .sln files in and below the current directory
  2. parse the sln extracting the configuration and platform values
  3. for each unique configuration platform combination call msbuild with the given solution

--edit: Forgot Split-String is part of PSCX, it's better to use [string] I hope this helps. If you still would like to use MsBuild, it does support a recursive form of the file glob operator via the ".\**\*.sln" syntax see here for details. MsBuild also provides the MsBuild task which can be used to build a set of solutions. I do not know how you can get all 'known' configurations from a solution easily in msbuild. Therefore I choose the PowerShell route.

Related Topic