Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add conditional defines in the msbuild command line?

I have the following sample code:

program boohoo;

{$APPTYPE CONSOLE}

{$IFDEF boo}
{$MESSAGE warn 'boo'}
{$ENDIF}

{$IFDEF hoo}
{$MESSAGE warn 'hoo'}
{$ENDIF}

begin
end.

In the project options the conditional boo is defined. I would like to be able to add the conditional hoo as part of my msbuild command line.

I have tried it like this:

msbuild boohoo.dproj /p:Config=Release;DCC_Define="$(DCC_Define);hoo"

The output shows hoo but not boo. When I use verbose output to see the dcc32 command I see

-D$;hoo

Clearly I can do it like this:

msbuild boohoo.dproj /p:Config=Release;DCC_Define="boo;hoo"

but naturally I want to use whatever conditionals are declared in the project options plus what I specify on the command line.

Is there any way for me to specify this property with reference to the value from the underlying configuration?

like image 777
David Heffernan Avatar asked Dec 14 '11 15:12

David Heffernan


2 Answers

Disclaimer: don't use MsBuild myself yet, all taken from the docs and some IDE experimentation

According to MsBuild command line reference ( http://msdn.microsoft.com/en-us/library/ms164311.aspx ):

/property:name=value

Sets or overrides these project-level properties, where name is the property name and value is the property value. Use a semicolon or a comma to separate multiple properties, or specify each property separately. /p is also acceptable. For example: /property:WarningLevel=2;OutputDir=bin\Debug

setting or overriding is all you can do for a property value. Adding to a property value from the project file is either not possible or a case of a hidden feature.

But I guess what you could do is define a custom property in your dproj file with an " " as its default value:

<PropertyGroup>
  <ExtraDefines> </ExtraDefines>
</PropertyGroup>

reference that in your defines statement

<DCC_Define>DUNIT;$(ExtraDefines);$(DCC_Define)</DCC_Define>

which in the IDE should be DUNIT;$(ExtraDefines)

and then specify it on the command line:

msbuild boohoo.dproj /p:Config=Release;ExtraDefines="hoo"

I did test adding the $(ExtraDefines) to the Include options for the project using the IDE. And at least that didn't barf at me, even without having the option defined in the dproj. The commandline the IDE produced from this was:

...rad studio\7.0\bin\dcc32.exe --no-config -B -Q -DDEBUG;DUNIT; -E....

Which seems to indicate that the $(ExtraDefines) got eliminated as it had no value. And that it should be picked up using MSBuild and specififying a value on the command line.

like image 132
Marjan Venema Avatar answered Sep 24 '22 13:09

Marjan Venema


Almost 5 years later, but all answers are not quite elegant ))

Recently, I've faced the same problem

And here is the solution:

Usually, DCC_Define is defined in a .dproj file like this:

<PropertyGroup Condition="'$(Cfg_1)'!=''">
    <DCC_Define>boo;$(DCC_Define)</DCC_Define>

We all have tried to define DCC_Define via /property:DCC_Define=blah-blah

But accordingly to How to: Build the Same Source Files with Different Options:

The property value that is specified on the command line takes precedence over any value that is set for the same property in the project file, and that value in the project file takes precedence over the value in an environment variable.

So, failure (that is the question here!)

BUT! How to: Use Environment Variables in a Build

To use an environment variable in an MSBuild project

  • Reference the environment variable the same way you would a variable declared in your project file. For example, the following code references the BIN_PATH environment variable:

    <FinalOutput>$(BIN_PATH)\MyAssembly.dll</FinalOutput>
    

So, we must define environment variable with the name DCC_Define and values of our ADDITIONAL conditionals

> set DCC_Define=hoo;doo;moo;foo

and then simply run

> msbuild boohoo.dproj /p:Config=Release

DCC32 will get then -Dboo;hoo;doo;moo;foo

like image 21
Alexey Shumkin Avatar answered Sep 23 '22 13:09

Alexey Shumkin