Mike Webb
Mike Webb

Reputation: 9013

Is it possible to use conditional compilation symbols in VS build events?

Say for instance I have a Visual Studio project with a configuration called "MyConfig" and I have the compilation symbol MY_CONFIG_SYMBOL defined.

Is there a macro or command to see if MY_CONFIG_SYMBOL is defined in the pre/post build events? Something like #if MY_CONFIG_SYMBOL, but for the build event?

Upvotes: 12

Views: 5919

Answers (3)

Mike Webb
Mike Webb

Reputation: 9013

I finally found an answer. The following works perfectly:

if "$(DefineConstants.Contains('DEBUG'))" == "True" <command>

This works for any constants defined in the build, but note that the constant is case-sensitive ('DEBUG' != 'Debug').

Upvotes: 14

Erik Eidt
Erik Eidt

Reputation: 26766

Well, this is not a solution, just trying to advance state by sharing some experiments. (I've yet to find a way to test conditional compilation symbols.)

This as a way to consolidate switching debug on and off:

<#@ include file="debug.incl" #>`

some text1
<# if ( xdebug ) { #>
    foo = bas;
<# } #>
more text

Where debug.incl contains:

<# 
bool xdebug = true;
#>

The conditional (if) in the first file is able to see the value of xdebug, so output is altered based on the setting of xdebug in debug.incl.

Sadly, however, the output files are not rebuilt on changes to debug.incl, despite the obvious include of it. And even a clean & rebuild doesn't seem to trigger generation, so some separate build construct is need for that...

(I did try debug.tt instead of debug.incl to no avail, switch to .incl so that debug.cs wasn't created by debug.tt.)


This didn't work very well as it doesn't see conditional compilation symbols, though does actually switch on the template debug attribute!

<#
#if DEBUG 
bool xdebug = true;
#else
bool xdebug = false;
#endif
#>

some text1
<# if ( xdebug ) { #>
    foo = bas;
<# } #>
more text

with <#@ template debug="true" #> vs. <# template debug=false #> you get the conditional output or not, respectively.

Upvotes: 0

Ken Cenerelli
Ken Cenerelli

Reputation: 520

If you mean conditional builds based on the build types (Debug or Release) then yes. Check out these threads:

Conditional Post-build event in Visual Studio 2008

How to run Visual Studio post-build events for debug build only

Upvotes: 0

Related Questions