views:

59

answers:

1

I am developing a new module for a large Application in Delphi 2010.

I have organized my sources in a project group of two projects, one to build the full application and one two launch my test suite (which shares some sourcecode with the main application).

During the initalization of a unit, i need to act differently depending on which of the two i was compiling.

unit MySharedUnit
var
  flag : TFlagValues;

implementation
[...]

initialization

if IsTestProject then
  flag := TestValue
else
  flag := ReleaseValue;
end. 

Currently, I use a project defined environment variable (defined in only one of the projects' options) to decide the active project.

My question is:

Is there another or more elegant way to do this, like a builtin #define'd value or so which would not require me to modify the project options by hand when the test application should be run in Release mode?

+7  A: 

Delphi knows conditional compilation like:

initialization
{$IFDEF FULLVERSION}
  flag := ReleaseValue;
{$ELSE}
  flag := TestValue
{$ENDIF}
end. 

You can set FULLVERSION (or any other name) in the project if you like.

With Delphi 2010 you can have a different set of options for Debug and Release versions.

Gamecat
+1, although I prefer to use as the default the release version, and activate test/debug code only when explicitly set. It helps to avoid debug code slip into release version by mistake (because someone typed FULVERSION, for example).
ldsandon
@Idsandon, you can also reverse the check by defining DEBUGVERSION.
Gamecat
If you do this and reference the define (defined in the project file) in another unit, remember to always do a full build of the project. Otherwise the unit may retain the define from the last full build of the other project.
frogb