Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 607
Charles Randall Avatar asked Nov 01 '12 16:11

Charles Randall


People also ask

How do I change the build command in Visual Studio?

Open the project's Property Pages dialog box. For more information, see Set C++ compiler and build properties in Visual Studio. Choose Configuration Properties to enable the Configuration box. In the Configuration box, select the configuration for which you want to specify a custom build tool.

How do I run a custom tool in Visual Studio?

Select the templates that you wish to run in the Solution Explorer in Visual Studio. Note that it is the actual files that you should select - not the folder that contains them. Right click on one of the selected template files and select Run Custom Tool from the context menu.

What is a build step?

A custom build step is a build rule associated with a project. A custom build step can specify a command line to execute, any additional input or output files, and a message to display.


1 Answers

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.

like image 195
SoftwareCarpenter Avatar answered Sep 20 '22 16:09

SoftwareCarpenter