Tuesday, 21 October 2014

Custom Build Tools in Visual Studio 2012, 2013 - "Pay no attention to the API behind that curtain!"

So I want to add a custom file type - a new kind of shader I'm going to call a psfx file. I have an exe I've created to build it into binaries. All I need now is to make Visual Studio recognize psfx files, and call "Psfx.exe" to build them into .psfxo files. Simple eh? I used to do this in VS2008:

Now let's try it in VS2012. Let's see what MS has to say:
In earlier releases, a rule file is an XML-based file that has a .rules file name extension. A rule file lets you define custom build rules and incorporate them into the build process of a Visual C++ project. A custom build rule, which can be associated with one or more file name extensions, lets you pass input files to a tool that creates one or more output files.

In this release, custom build rules are represented by three file types, .xml, .props, and .targets, instead of a .rules file. When a .rules file that was created by using an earlier release of Visual C++ is migrated to the current release, equivalent .xml, .props, and .targets files are created and stored in your project together with the original .rules file.

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.


 OK. Here's what I did next. I happened to still have VS 2008 on my machine - I've been meaning to get rid of it for a while now. I made a blank project, and created a new rule - "The Floop Rule". For files with the .floop extension. VS built this beautiful simple, sensible file, FloopFileName.rules:

<visualstudiotoolfile name="Floop Rule File Display Name" version="8.00">
        <custombuildrule commandline="Floop.exe [inputs]" displayname="Floop Rule Display Name" executiondescription="Executing Floop Rule" fileextensions="*.floop" name="Floop Rule" outputs="$(InputName).floopout">
468 bytes of perfection. Then I opened the .vcproj file in Visual Studio 2012, and converted it to a .vcxproj. Now my nice simple .rules file has been converted into FloopRuleFileName.props (879 bytes), FloopRuleFileName.targets (3.27k) and FloopRuleFileName.xml (4.37k). You can tell that this is improved technology, because of the huge file sizes.

Anyway, when I want to create a new rule in future, I can just copy these three files, replacing the following elements:

"Floop Rule File Display Name"

And so can you. To add the rule to a project, you'll need:
<ImportGroup Label="ExtensionSettings">
    <Import Project="FloopRuleFileName.props" />


<importgroup label="ExtensionTargets">
    <import project="FloopRuleFileName.targets">
- insert them as text in the appropriate places - as seen in Temp.vcxproj