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

1

28

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?

Spade answered 1/11, 2012 at 16:40 Comment(4)
Have you checked this? msdn.microsoft.com/en-us/library/xfsbz6cw(v=vs.100).aspxFollett
Those are what I was using in 2008. They no longer work in 2008.Spade
I suppose you must have read this one too: blogs.msdn.com/b/vcblog/archive/2010/04/21/… . What caught my eyes is that according to this, you need a .props file too, maybe that's what you're missing. Haven't tried it myself though....Follett
Even now in Visual Studio 2022 this still seems to be a non-trivial taskGalan
M
8

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.

Mesoblast answered 11/11, 2012 at 21:7 Comment(5)
In this example, the CustomBuild step only applies to one file, faq.txt. The OP wanted a CustomBuild step that runs on multiple files like *.foo.Tomkins
Just to follow up, CustomBuild can also be used with wildcards, so using <CustomBuild Include="$(MSBuildProjectDirectory)\**\*.foo"> works as expected. Thank you.Tomkins
Wild cards aren't recommended - learn.microsoft.com/en-us/cpp/build/reference/… If a .vcxproj project file that includes these constructs gets loaded in the IDE, the project may seem to work at first. However, issues are likely as soon as the project is modified by Visual Studio and then saved on disk. You may experience random crashes and undefined behavior.Faletti
@MarkIngram good to know. Is this related too a specific version of VS ? Since MSBUILD used to build everything maybe VS independent. Am sure there has too be way too use wildcard chars. It’s like using // or \\ instead of just one like / or \ in pathsMesoblast
@MarkIngram actually as long as the wildcard is within a itemgroup or within a target it will not effect the ide.Mesoblast

© 2022 - 2024 — McMap. All rights reserved.