How to enable postsharp with a compilation symbol

Questions about integration in Visual Studio, MSBuild or other IDEs or build tools.

How to enable postsharp with a compilation symbol

Postby christianacca on Mon Aug 25, 2008 6:25 pm

Hi Gael,
I've been trying without much success for about a day to modify the build process so as to conditionally enable postsharp via a compilation symbol. Basically I want PostSharp to be disabled by default unless a symbol is supplied either individually by a project:

<DefineConstants>TRACE;DEBUG;IncludePostSharp</DefineConstants>

or for the whole solution when building using the command line:

MSBuild.exe ..\src\Sickness.sln /p:CmdLineDefineConstants=IncludePostSharp

My motivating goal is to optimise build times for a solution when compiling in the Visual Studio IDE. My solution is to disable PostSharp when working in the IDE for those projects where aspects are only being weaved for such things as logging/tracing.

Complicating my solution, is the requirement that symbols passed via the command line should be appended to any compilation symbols defined by the project file itself. The solution I arrived at here was to override the 'BeforeCompile' target in each csproj file, thusly:

<Target Name="BeforeCompile">
<Message Text="DefineConstants Initial Value: $(DefineConstants)" />
<CreateProperty Condition="$(CmdLineDefineConstants)!=''" Value="$(DefineConstants);$(CmdLineDefineConstants)">
<Output TaskParameter="Value" PropertyName="DefineConstants" />
</CreateProperty>
<Message Text="DefineConstants Final Value: $(DefineConstants)" />
</Target>

Now onto the problem that I can't seem to solve no matter what I try - modifying the 'PostSharp-1.0.targets' file so that PostSharp is enabled only if IncludePostSharp symbol is detected. I have got a partial solution that works so long as the csproj file defines the 'IncludePostSharp' symbol. However, when executing the build with the command line above, the PostSharp target does not get executed. To be honest, I just tried adapting the behaviour of 'SkipPostSharp' to achieve what I wanted. I'm not even sure whether this is the correct thing to do.

I think my problem is lack of MsBuild knowledge! I would really appreciate if you can see what I'm doing wrong with the my hacks to 'PostSharp-1.0.targets' file or maybe suggest a better solution. I attach my modified 'PostSharp-1.0.targets' file.

Thanks in advance
Christian
You do not have the required permissions to view the files attached to this post.
christianacca
 
Posts: 27
Joined: Sat Jun 28, 2008 12:59 pm
First Name: Christian
Last Name: Crowhurst

Re: How to enable postsharp with a compilation symbol

Postby gfraiteur on Mon Aug 25, 2008 6:33 pm

Well... you can skip PostSharp easily, just by defining the C#/VB constant SkipPostSharp or by defining the MSBUild property of the same name...

-gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 816
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: Coding Glove

Re: How to enable postsharp with a compilation symbol

Postby christianacca on Mon Aug 25, 2008 6:49 pm

Gael,
I maybe missing something really obvious here so please correct me if I'm off track. If I define the SkipPostSharp symbol in each project that has "non-essential" aspects that would certainly optimise the build times within Visual Studio. However, when it comes time to build the same solution from the command line (directly or indirectly via CruiseControl), there's no way I can see of "switching on" PostSharp for all projects without manually removing the SkipPostSharp symbol from the csproj files.

C
christianacca
 
Posts: 27
Joined: Sat Jun 28, 2008 12:59 pm
First Name: Christian
Last Name: Crowhurst

Re: How to enable postsharp with a compilation symbol

Postby christianacca on Wed Aug 27, 2008 12:52 am

Hi Gael,
I managed to find a solution to my problem, although I reckon there's a better solution that you could suggest!

The problem was down to the order in which the targets were being invoked.

As I mentioned previously, I have overriden the 'BeforeCompile' target in each csproj file in order to append additional symbols to those that were defined by the project itself. I was then calling MsBuild with a command line that supplied my new 'IncludePostSharp' symbol. The problem was, the target 'BeforeCompile' that actually appends my new symbol to the DefineConstants property was being executed AFTER the target 'PostSharpInspectConstants'. As a result when 'PostSharpInspectConstants' target ran my new symbol had yet to be added to the DefineConstants property and as such the PostSharp target was skipped.

The change was thus:

<!-- Introduces PostSharp in the chain of compilation targets -->
<PropertyGroup>
<CompileDependsOn>
ResolveReferences;
ResolveKeySource;
BeforeCompile;
PostSharpInspectConstants;
PostSharpInspectReferences;
PostSharpDefineConstant;
_TimeStampBeforeCompile;
CoreCompile;
_TimeStampAfterCompile;
AfterCompile;
PostSharp
</CompileDependsOn>
<BuildDependsOn>
$(BuildDependsOn);
PostSharpVerify
</BuildDependsOn>
</PropertyGroup>

Like I said, I'm sure there is a better solution. I particularly don't like the fact that I'm having to maintain a modified 'PostSharp-1.0.targets' file.

If you have a moment, it would nice to hear a better alternative :-)

Christian
christianacca
 
Posts: 27
Joined: Sat Jun 28, 2008 12:59 pm
First Name: Christian
Last Name: Crowhurst

Re: How to enable postsharp with a compilation symbol

Postby gfraiteur on Wed Aug 27, 2008 9:12 am

From MSBuild doc:

When building inside Visual Studio, the property $(BuildingInsideVisualStudio) is set to true. This can be used in your project or .targets files to cause the build to behave differently.


So you could edit your csproj to define the SkipPostSharp property is BuildingInsideVisualStudio is true.

-gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 816
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: Coding Glove

Re: How to enable postsharp with a compilation symbol

Postby christianacca on Wed Aug 27, 2008 10:11 am

See I knew you would have a better solution!

At least it was a learning experience for me - from 5% MsBuild knowledge, I now have 10% :-)

Thanks for the help
Christian
christianacca
 
Posts: 27
Joined: Sat Jun 28, 2008 12:59 pm
First Name: Christian
Last Name: Crowhurst


Return to Integration with IDE and Build Scripts