I found the answer using the following links:
It turns out that the full setup is quite complicated, so I will limit my answer to the answer to my two first questions with minimalist steps:
To add a custom build tool to MSVC
In the project file ( vcxproj ), specify a new assembly definition (there may be a way to include it in the system area, but I have not studied it yet):
<ImportGroup Label="ExtensionTargets"> <Import Project="mybuild.targets" /> </ImportGroup>
This can go right before closing </Project> .
Create a text file called mybuild.targets in the same folder as the vcxproj file.
Paste the following into mybuild.targets :
<?xml version="1.0" encoding="utf-8"?> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <PropertyPageSchema Include="$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml" /> </ItemGroup> </Project>
This target file refers to an xml file with more detailed information ( $(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml , which in my example refers to mybuild.xml). Create mybuild.xml .
Paste the following into mybuild.xml :
<?xml version="1.0" encoding="utf-8"?> <ProjectSchemaDefinitions xmlns="http://schemas.microsoft.com/build/2009/properties" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <ItemType Name="MYBUILD" DisplayName="My Custom Build" /> </ProjectSchemaDefinitions>
What we just did: the project file now includes a new target definition ( mybuild.targets ). Usually this file stores more information on how to perform the actual build, but here it contains only the include file in the mybuild.xml file. This mybuild.xml file contains a new item type definition with the internal name MYBUILD and the display name My Custom Build . When the project is reopened in Visual Studio, a new element type will be available in the Element Type list. When you select it, the type of this element is set to MYBUILD , and since there is currently no rule for this element, it will simply be ignored from the assembly.
To associate a file extension with a custom MSVC item type
This is done in two steps:
Associate the file extension with the content type. This is done by adding an entry to mybuild.xml as follows:
<FileExtension Name="*.myext" ContentType="MYBUILD" />
Associate a content type with an item type. This is done by adding an entry to mybuild.xml as follows:
<ContentType Name="MYBUILD" DisplayName="My Custom Build" ItemType="MYBUILD" />
At this point, mybuild.xml looks like this:
<?xml version="1.0" encoding="utf-8"?> <ProjectSchemaDefinitions xmlns="http://schemas.microsoft.com/build/2009/properties" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <ItemType Name="MYBUILD" DisplayName="My Custom Build" /> <FileExtension Name="*.hh" ContentType="MYBUILD" /> <ContentType Name="MYBUILD" DisplayName="My Custom Build" ItemType="MYBUILD" /> </ProjectSchemaDefinitions>
What we just did: Visual Studio now knows that the .myext extension contains data of type MYBUILD . He also knows that files with contents of type MYBUILD are elements of type MYBUILD . After the project is reopened in Visual Studio, when adding files with the extension .myext , Visual Studio will automatically set the item type to My Custom Assembly for these files.
Running a custom tool for a custom MSVC item
At this point, we have a file extension associated with a custom item type. We need to associate this type of element with a set of assembly rules.
Associate an item type with an assembly purpose. In mybuild.targets add the following to the same group of elements as our PropertyPageSchema:
<AvailableItemName Include="MYBUILD"> <Targets>_MYBUILD</Targets> </AvailableItemName>
So now it looks like this:
<ItemGroup> <PropertyPageSchema Include="$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml" /> <AvailableItemName Include="MYBUILD"> <Targets>_MYBUILD</Targets> </AvailableItemName> </ItemGroup>
Define a custom goal. Here you must map all the properties and variables that should be used to configure the build task. In the next step, we will use the build task, which uses the command line template, so in our target setup we will configure the actual command line. This is directly below the ItemGroup defined in the previous step:
<Target Name="_MYBUILD"> <MYBUILD CommandLineTemplate="explorer $(IntDir)"></MYBUILD> </Target>
Our command line will simply open the explorer window.
Declare a custom assembly task. This happens immediately after Target from the previous step:
<UsingTask TaskName="MYBUILD" TaskFactory="XamlTaskFactory" AssemblyName="Microsoft.Build.Tasks.v4.0"> <Task>$(MSBuildThisFileDirectory)$(MSBuildThisFileName).xml</Task> </UsingTask>
Here we refer to the task that will be defined in our mybuild.xml file.
In mybuild.xml add the following rule:
<Rule Name="MYBUILD" PageTemplate="tool" DisplayName="My Custom Build" Order="200"/>
What we just did: we matched the type of custom item with the custom target and the custom build task that opens the explorer window. When we create the .myext files, we can now expect Visual Studio to appear in the explorer window.
Build setup
There are ways to add configuration fields to the properties window, map them to variables, and use them to configure the task. This is not discussed here, but is discussed in the links provided above.