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

3

12

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?

Malchus answered 30/8, 2012 at 15:23 Comment(2)
Any luck with finding an answer?Jester
@Jester I finally did find an answer. See below.Malchus
M
14

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').

Malchus answered 16/3, 2016 at 16:21 Comment(0)
B
0

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

Bruis answered 30/8, 2012 at 21:15 Comment(1)
No, I mean using the Conditional Compilation symbols defined in Debug or Release. For the Debug configuration the main Conditional Compilation symbol, usually defined by default, is DEBUG. You can use this in code, but can you use it in a pre or post build eventMalchus
S
0

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.

Sillsby answered 4/10, 2012 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.