1

How do I change the order of my post compilers in visual studio?

Specifically, I would like to make the code contracts post compililation happen AFTER Postsharp. By default it's the other way around.

Ev.
  • 7,109
  • 14
  • 53
  • 87

2 Answers2

2

Maybe this points you to the right direction:

PostSharp is executed by Program Files\PostSharp 2.1\PostSharp.targets

which is included by Program Files\MSBuild\v4.0\Custom.After.Microsoft.Common.targets

which is included by Windows\Microsoft.NET\Framework\v4.0\Microsoft.Common.targets

which is included by Windows\Microsoft.NET\Framework\v4.0\Microsoft.CSharp.targets

which is included by YourProject.csproj

There must be something like

<Import Project="$(ContractsPath)\Microsoft.Contract.targets" />

somewhere around all previous files. Find it and put it after line

<Import Project="C:\Program Files (x86)\PostSharp 2.1\PostSharp.targets" Condition=" '$(DontImportPostSharp)' == '' AND Exists('C:\Program Files (x86)\PostSharp 2.1\PostSharp.targets')" />

in Custom.After.Microsoft.Common.targets and try a compilation.

Note: Replace versions and directories to match your enviroment and installation path.

jlvaquero
  • 8,571
  • 1
  • 29
  • 45
2

First of all, it's not recommended to execute PostSharp rewriter before CodeContracts rewriter, because CodeContracts rewriter may not understand the input assembly after it has been changed by PostSharp.

Below is the explanation of how the order of the build steps is determined if you still want to manipulate it.

PostSharp inserts itself into a build process using standard MSBuild extension points - "DependsOn" properties, as documented on MSDN: How to: Extend the Visual Studio Build Process.

In particular the overridden property "CompileDependsOn" inserts PostSharp post-compiler after the compilation step. You can find this piece in the PostSharp.targets file:

<CompileDependsOn>
  PostSharp30TimestampBeforeCompile;
  $(CompileDependsOn);
  PostSharp30TimestampAfterCompile;
  PostSharp30
</CompileDependsOn>

To debug the whole build process you can enable diagnostic verbosity of MSBuild in "Tools" -> "Options" -> "Projects and Solutions" -> "Build and Run". Look for the string "CompileDependsOn =" in the build output to see the order of build steps. CodeContractInstrument is going to be before PostSharp30 in this list.

You can manipulate the value of "CompileDependsOn" property in your build/project file to change the order of tasks.

AlexD
  • 5,011
  • 2
  • 23
  • 34