How to add a custom build target to a Visual C ++ 2010 project?

There are many tutorials to help you simulate the VS2008 "Custom Build Step" in VS2010 with MSBuild. However, I would like my build to be smarter and use MSBuild. I wrote a small MSBuild task that calls the ANTLR parser generator. This build task works flawlessly when I run it in a simple MSBuild file. However, when I try to add my task to a C ++ project, I have problems. Essentially, I added this to the top of my project file (right after the <project> element):

  <UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar" AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" /> <Target Name="BeforeBuild" Inputs="ConfigurationParser.g" Outputs="ConfigurationParserParser.h;ConfigurationParserParser.cpp;ConfigurationParserLexer.h;ConfigurationParserLexer.cpp"> <AntlrGrammar AntlrLocation="$(MSBuildProjectDirectory)Antlr.jar" Grammar="ConfigurationParser.g" RenameToCpp="true" /> </Target> 

However, my target is not called before assembly.

How can I add my task to a C ++ assembly?

+4
source share
1 answer

Before reading this answer, you probably want to see:

The old way to extend MSBuild and the one mentioned in the manual that I have, in fact, is based on overriding the default goals set by Microsoft. The new way, as indicated in the second link above, is to define your own custom target and use the "BeforeTargets" and "AfterTargets" properties to make your target work before or after the intended target.

In my specific case, I needed the ANTLR Grammars task to run before the CLCompile target, which actually creates C ++ files, because the ANTLR Grammars task creates .cpp files. Therefore, the XML looks like this:

 <Project ... <!-- Other things put in by VS2010 ... this is the bottom of the file --> <UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar" AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" /> <Target Name="AntlrGrammars" Inputs="Configuration.g" Outputs="ConfigurationParser.h;ConfigurationParser.cpp;ConfigurationLexer.h;ConfigurationLexer.cpp" BeforeTargets="ClCompile"> <AntlrGrammar AntlrLocation="$(MSBuildProjectDirectory)\Antlr.jar" Grammar="Configuration.g" RenameToCpp="true" /> </Target> <ImportGroup Label="ExtensionTargets"> </ImportGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> </Project> 

As to why this is superior to PreBuildEvent and / or PostBuildEvent; it's smart enough not to rebuild .cpps when the grammar itself is not updated. You will get something like:

  1> AntlrGrammars:
 1> Skipping target "AntlrGrammars" because all output files are up-to-date with respect to the input files.
 1> ClCompile:
 1> All outputs are up-to-date.
 1> All outputs are up-to-date.

It also raises a Visual Studio-independent complaint every time you run the program necessary to restore it, for example, for simple steps before and after the build.

Hope this helps someone - forever made me freak to understand.

+8
source

Source: https://habr.com/ru/post/1315994/


All Articles