6

Is there anyway to use compiler constants in Build Events in Visual Studio - VB.NET? (especially in Post-Build events)

Scenario

If TEST_EDITION=TRUE is defined I want to run an executable during the Post-Build event so if it's FALSE then I'll run something else.

This can be used for creating different installers for different editions.

P.S. Before someone suggests: No I don't want to use nant, msbuild or something like that

dr. evil
  • 26,944
  • 33
  • 131
  • 201
  • It would help if you'd explain more about what you're trying to do. – Jon Skeet Feb 27 '10 at 13:10
  • I'm trying to write a conditional build-event based on the compiler constant? Wasn't that clear? -but I added an example to clear that up. – dr. evil Feb 27 '10 at 13:17

3 Answers3

12

Yes, the $(DefineConstants) macro is available and can be tested in a build event. For example, Project + Compile, Advanced Compile Options, Custom constants = Test can be tested like this:

if /i "$(DefineConstants)" NEQ "TEST" goto skiptest
echo Setting up for test environment
:skiptest

More complicated custom constants like Test=TRUE or compound constants need to be parsed differently. Admittedly I quickly gave up trying to figure out how to use the horrid FOR command.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Not sure about Visual Basic's syntax, but the following trick can be used by C++: the file global_inc.bat reads as:

SET PARAMETER=TRUE

This could be input by a batch script which was called in post-build event. The C++ code used the file as follows:

#define PARAMETER const int parameter
#define SET /**/
#include "global_inc.bat"
;
#undef PARAMETER

The postbuild step looked like this:

call global_inc.bat
if "%PARAMETER%" == "TRUE" echo True

Another possibility would be for the prebuild step to generate a .vb file, as well as a configuration file used in postbuild step.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • I assume this is your workaround for this limitation, because I don't really need to compile anything. All I want to do this run an executable only when a constant is TRUE during the post-build event. – dr. evil Feb 27 '10 at 13:20
  • changed the example to be closer to the question. the answer by nobugz seems me a better solution however – Vlad Feb 27 '10 at 14:08
0

Have you tried the MsBuild PostEvents? this is an extract of a .csproj... but the same applies to vbproj files

  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
    <Copy SourceFiles="$(OutputPath)$(AssemblyName).dll" DestinationFolder="$(BinariesFolder)" ContinueOnError="true" />
  </Target>

You can use it with the TaskExec Target, which allows you to run a batch file or an executable.

<Target Name="DoSomething">
    <Exec Command="D:\DoSomething.exe"/>
</Target>
Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103