David Heffernan
David Heffernan

Reputation: 612914

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?

Upvotes: 19

Views: 4834

Answers (5)

Justin
Justin

Reputation: 985

I just tried the following and it worked, so don't know whether Microsoft has changed it:

msbuild "myApp.dproj" /t:build /property:DCC_Define="boo"

remember to add the double quote "", otherwise it won't work

Upvotes: 3

Alexey Shumkin
Alexey Shumkin

Reputation: 419

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

Upvotes: 20

pyxidata
pyxidata

Reputation: 131

Another way is to create a wrapper project file like this:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Full" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Full">
  <CreateProperty Value="$(DCC_Define);$(ExtraDefines)">
    <Output TaskParameter="Value" PropertyName="DCC_Define"/>
  </CreateProperty>
</Target>
<Import Project="example.dproj" />
</Project>

and invoke like this:

msbuild.exe "/t:Clean;Full;Build" "/p:config=Debug" /p:ExtraDefines=SOME_DEFINE wrapper.proj

It is certainly less elegant but you don't have to modify the .dproj file.

Upvotes: 1

kludg
kludg

Reputation: 27493

Straightforward solution is to create a new build configuration (say, boohooRelease), add both boo and hoo conditional defines to it and compile as msbuild boohoo.dproj /p:Config=boohooRelease. Not exactly what you are trying to do, but it works.

Upvotes: 2

Marjan Venema
Marjan Venema

Reputation: 19346

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.

Upvotes: 23

Related Questions