VC MSBuild Extensibility Example

VC MSBuild Extensibility Example

  • Comments 4

Hello, my name is Felix Huang and I am a developer on the Visual C++ team.  Over the last year, I worked on making the build system in Visual Studio 2010 more flexible with MSBuild system as the foundation.  For those who felt the need to use batch scripts to power your builds, I hope that this new system would provide you with that much flexibility while maintaining a similar UI feeling as the previous version of Visual Studio.  Previous article written by Marian Luparu touches more on the benefits of MSBuild.  In this post, I will provide an example augmenting the build system and property page to better suit development life cycle.

Suppose you want to add another switch to an existing tool.  Let’s take /WS:AGGRESSIVE found in link.exe as an example.  We create a file called myfavorite.xml next to a project.  This file describes the structure of the UI property page.  We want to use a Boolean switch inside the Optimization category.  By copying and pasting comments from MSDN, we get the snippet below.

<?xml version="1.0" encoding="utf-8"?>

<ProjectSchemaDefinitions xmlns="clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">

<Rule Name="MyFavoriteOptions" PageTemplate="generic">

  <Rule.DisplayName>

    <sys:String>My Favorite Options</sys:String>

  </Rule.DisplayName>

  <Rule.Categories>

    <Category Name="Optimization">

      <Category.DisplayName>

        <sys:String>Optimization</sys:String>

      </Category.DisplayName>

    </Category>

  </Rule.Categories>

  <Rule.DataSource>

    <DataSource Persistence="ProjectFile" ItemType=""/>

  </Rule.DataSource>

  <BoolProperty Switch="WS:AGGRESSIVE" Name="WSAggressive" Category="Optimization">

    <BoolProperty.DisplayName>

      <sys:String>Aggressively Trim Process Memory</sys:String>

    </BoolProperty.DisplayName>

    <BoolProperty.Description>

      <sys:String>Use the /WS:AGGRESSIVE option to add the WS_AGGRESSIVE attribute to your application's image. The Windows NT 4.0 (and later) loader will recognize this attribute and aggressively trim the working set of the process when it is not active.<sys:String>

    </BoolProperty.Description>

  </BoolProperty>

</Rule>

</ProjectSchemaDefinitions>

Refer to Li Shao’s blog on the topic for writing xml.  Restart Visual Studio to load the changes.  

 

 

Next, we will attach the switch to the target file, thus passing it to the linker during execution.  There are many ways to do this; but let’s use a new feature in MSBuild 4.0 called Before/After targets.  Inside one of your project file (.vcxproj), add the <Target> below to the <Project> scope.  By using the BeforeTargets, it will insert this target into the execution of the build before the link target gets run.  Thus, we are able to modify the value in the Additional Options appending our switch to the end. 

  <ItemGroup>

    <PropertyPageSchema

      Include="$(MSBuildThisFileDirectory)\myfavorite.xml" />

  </ItemGroup>

  <Target Name="WSAggressiveTarget" BeforeTargets="Link">

    <ItemGroup>

      <Link>

        <AdditionalOptions Condition="'$(WSAggressive)'=='true'">%(Link.AdditionOptions) /WS:Aggressive</AdditionalOptions>

      </Link>

    </ItemGroup>

  </Target>  

 

The PropertyPageSchema ItemGroup will let the IDE to know where to look for the xml.  Now your project is ready for building.

 

Thanks for reading.

 

Felix Huang

  • What determines what xml files get included in VS project property pages? Is the filename, "myfavorite.xml" significant? Or does VS 2010 just assume any xml file in the same directory as the project is to be included?

  • The text in the JPEG screenshot is rather hard to read.  Could you repost that in some lossless format and in the original resolution?

  • The first section shows the xml after attaching to the project file.  I wanted to show the picture to draw a connection between the XML and the property page.  Inside of each project file, it contains an <ItemGroup> which includes all the .xml by paths.  The paths are full paths with the help of macros set in environment and set in msbuild.  All the property page, including link and compiler, loads the same way.

  • Save screenshot as PNG and then run "optipng.exe -o7" to optimize it.  That should cut the screen capture down to 1/5 or less of the original size.

    FWIW, print screen, paste into ms paint, save as jpeg is a poor route as ms paint uses a poor quality jpeg codec and saves the jpeg at too low of a quality level.  (please fix ms paint).

Page 1 of 1 (4 items)