Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

msbuild - What happened to BeforeBuild and other targets in VS2012?

I'm trying to get some pre-build steps to work in a C++ project in Visual Studio 2012 but they do not get invoked (while I'm pretty sure the same techniques were OK in Visual Studio 2010). Command line builds behave exactly the same.

This is the end of the project file; the file was generated using Visual Studio and then I just added the last couple of lines:

<Import Project="$(VCTargetsPath)Microsoft.Cpp.targets" />
<Target Name="BeforeBuild">
  <Message Text="### BeforeBuild ###" />
</Target>
<Target Name="BeforeCompile">
  <Message Text="### BeforeCompile ###" />
</Target>
<Target Name="AfterBuild">
  <Message Text="### AfterBuild ###" />
</Target>

and here's the output:

Project "d:empemp.vcxproj" on node 1 (default targets).
InitializeBuildStatus:
  Creating "Debugemp.unsuccessfulbuild" because "AlwaysCreate" was specified.
AfterBuild:
  AfterBuild
FinalizeBuildStatus:
  Deleting file "Debugemp.unsuccessfulbuild".
  Touching "Debugemp.lastbuildstate".

So only AfterBuild is considered and the others are ignored. Looking into this I found this PropertyGroup in Microsoft.BuildSteps.targets:

<BuildDependsOn>
  _PrepareForBuild;
  $(BuildSteps);
  AfterBuild;
  FinalizeBuildStatus;
</BuildDependsOn>

Shouldn't this also have BeforeBuild and the BuildEvent targets? Or is something wrong with my MSBuild install causing it to use this BuildSteps.targets file instead of something else?

Solution

As Alexey points out, using Before/AfterTarget provides a usable workaround. You just have to take care of which targets to use, but this is easy by looking at the BuildSteps file. This seems to work fine for now:

<Target Name="BeforeBuild" BeforeTargets="PrepareForBuild">
  <Message Text="### BeforeBuild ###" />
</Target>
<Target Name="BeforeCompile" BeforeTargets="BuildCompile">
  <Message Text="### BeforeCompile ###" />
</Target>
<Target Name="AfterBuild" AfterTargets="Build">
  <Message Text="### AfterBuild ###" />
</Target>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have same msbuild targets as you described, so I think your msbuild installation is fine.

Looks like they decide to make some cleanup to targets and dependencies ( not sure if this issue related to VS version, VS just using same targets, provided by msbuild). BeforeBuild and other targets still exists in Microsoft.common.targets. I suppose it just reserved for .NET projects (I never played with C++ ones, so I don't know, how to build a pipeline there).

Anyway whether it works or not on previous versions, your problem can be solved much easier - just use new attributes BeforeTargetsAfterTargets for MSBuild 4.0 and hook your targets directly on whatever you want:

<Import Project="$(VCTargetsPath)Microsoft.Cpp.targets" />
<Target Name="BeforeBuild" BeforeTargets="Build">
     <Message Text="### BeforeBuild ###" Importance="high" />
</Target>
<Target Name="BeforeCompile" BeforeTargets="Compile">
    <Message Text="### BeforeCompile ###" Importance="high" />
</Target>
<Target Name="AfterBuild" AfterTargets="Build">
    <Message Text="### AfterBuild ###" Importance="high" />
</Target>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...