2010-07-16 83 views
4

有很多的指南可以帮助你用MSBuild在VS2010中模仿VS2008的“自定义构建步骤”。但是,我希望我的构建更智能,并使用MSBuild。我写了调用ANTLR解析器生成器的a little MSBuild task。当我在一个简单的测试MSBuild文件中运行它时,该构建任务完美无缺地工作。但是,当我尝试将我的任务添加到C++项目时,我遇到了问题。从本质上讲,我已将此添加到我的项目文件的顶部(在<project>元素后右):如何将自定义构建目标添加到Visual C++ 2010项目?

<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> 

然而,我的目标没有被构建之前调用。

如何将我的任务添加到C++构建中?

回答

8

阅读这个答案之前,你可能会想看看:

延伸的MSBuild的老办法,并通过参考书我提到的一个基本上基于微软提供的覆盖默认空目标。正如上面第二个链接中指定的那样,新方法是定义您自己的任意目标,并使用“BeforeTargets”和“AfterTargets”属性强制目标在预期目标之前或之后运行。

在我的具体情况下,我需要ANTLR语法任务在CLCompile目标之前运行,因为ANTLR语法任务会生成.cpp文件,所以它实际上会生成C++文件。因此,XML看起来像这样:

<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> 

至于为什么这是优于PreBuildEvent和/或PostBuildEvent;当语法本身没有更新时,这足够聪明,不会重新生成.cpps。你会得到这样的:

 
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. 

这也沉默Visual Studio的没完没了的埋怨每次运行,它需要重建的东西,喜欢它与普通不前和后生成步骤程序的时间。

希望这可以帮助别人 - 永远带我frickin弄清楚。

+1

谢谢!这也让我永远也想到了。 – 2012-07-09 21:45:36

相关问题