Step by step, how do I add a custom build step for every file of a given type in Visual Studio 2010?

Charles Randall picture Charles Randall · Nov 1, 2012 · Viewed 19.3k times · Source

The Problem

  1. I have a bunch of custom files in a visual studio 2010 project.
  2. I need to run a tool on those custom files when they change.
  3. The tool generates .h files included by existing .cpp files.
  4. I would like the tool to run as part of the build process.
  5. It needs to run from within visual studio, but ideally could also run as part of an msbuild process.

Existing sub-standard solutions

On each file, every time I add it, I can go to the file settings, set the Item Type to Custom Build Tool, and then in the Custom Build Tool dialogue, set the command line parameters, set the (hardcoded) outputs, and set the (hardcoded) additional dependencies.

Obviously, this is a poor solution because it's a non-trivial and error-prone amount of work every time you add a new file of the custom type.

The desired solution

I set up visual studio that for whatever file of extension .foo, it runs tool bar.exe, when the .foo file changes, before it compiles the CPP code.

Things I have attempted

Based on this: http://msdn.microsoft.com/en-us/library/3e889s84(v=vs.100).aspx I attempted to set up a .targets and .xml file. However, I can't find out how to create a new Item Type such that it shows up in visual studio, nor can I figure out how to make it magically apply to every file of type .foo. On top of that, I haven't figured how where any of that links to anything that describes the act of calling my bar.exe tool.

I've attempted to search through all of visual studio's xml/targets files in order to track existing Item Types and determine how they are translated into actual build actions. It always ends at the wall of ItemType, where there's a bunch described in ProjectItemsSchema, but I can't find where they are implemented, or how I might implement them myself.

MSDN's documentation has been a complete failure, and I think I've read every single potentially related page ten times, to no avail.

In visual studio 2008, I had this kind of thing working with a .rules file that looked like this:

<?xml version="1.0" encoding="utf-8"?>
<VisualStudioToolFile
Name="Foo Build Rules"
Version="8.00"
>
<Rules>
    <CustomBuildRule
        Name="FooBuild"
        DisplayName="FooGen"
        CommandLine="..\..\tools\FooGen.exe [inputs]"
        Outputs="$(InputName).h"
        AdditionalDependencies="*.foo"
        FileExtensions="*.foo"
        ExecutionDescription="Generating Foos..."
        >
        <Properties>
        </Properties>
    </CustomBuildRule>
</Rules>
</VisualStudioToolFile>

Yet this functionality has been deprecated in 2010, and I have been unable to figure out how to replicate it with the new system.

Help?

Answer

SoftwareCarpenter picture SoftwareCarpenter · Nov 11, 2012

Start by adding a custom ItemGroup to your project file.

Like below:

<ItemGroup>
    <CustomBuild Include="faq.txt">
      <Message>Copying readme...</Message>
      <Command>copy %(Identity) $(OutDir)%(Identity)</Command>
      <Outputs>$(OutDir)%(Identity)</Outputs>
    </CustomBuild>
  </ItemGroup>

Next create a custom target and define where in the build process you want to run your custom targets in the build process.

The below links explain more in detail and step by step.

Hope this helps.

Walk-through: Using MSBuild to Create a Visual C++ Project

How to: Add Custom Build Tools to MSBuild Projects

Note:In the current release, the IDE does not support the creation of new rules. For that reason, the easiest way to use a rule file from a project that was created by using an earlier release of Visual C++ is to migrate the project to the current release.