4

I'm working on automating our software's build / deploy process. One major piece of this is executing msbuild to compile multiple Delphi projects.

Following numerous resources such as this one, I can do it successfully from the RAD Studio Command Line (which is simply calling rsvars.bat to set some environment variables). However, when trying to automate both this batch file and the msbuild command from Delphi, I cannot figure out how to proceed.

The key of the issue is that the batch file and the actual msbuild command are two entirely separate commands - although they need to be run together under the same environment. I found this question somewhat related, but I don't see a clear answer for my scenario.

How can I execute msbuild from Delphi while first preparing it with the environment variables as found in rsvars.bat?

1
  • 1
    If it were me I would port rsvars to Delphi and create msbuild process directly Commented Jul 14, 2016 at 19:49

2 Answers 2

9

As this answer showed, you can run cmd.exe itself with command-line parameters to execute commands.

Per the cmd.exe documentation:

Syntax

cmd [/c|/k] [/s] [/q] [/d] [/a|/u] [/t:{<B><F>|<F>}] [/e:{on|off}] [/f:{on|off}] [/v:{on|off}] [<String>]

Parameters

/c
Carries out the command specified by String and then stops.
...
<String>
Specifies the command you want to carry out.
...

Remarks

  • Using multiple commands

    To use multiple commands for <String>, separate them by the command separator && and enclose them in quotation marks. For example:

    "<Command>&&<Command>&&<Command>"
    
  • Processing quotation marks

    If you specify /c or /k, cmd processes the remainder of String, and quotation marks are preserved only if all of the following conditions are met:

    • You do not use /s.

    • You use exactly one set of quotation marks.

    • You do not use any special characters within the quotation marks (for example: & < > ( ) @ ^ | ).

    • You use one or more white-space characters within the quotation marks.

    • The String within quotation marks is the name of an executable file.

    If the previous conditions are not met, String is processed by examining the first character to verify whether it is an opening quotation mark. If the first character is an opening quotation mark, it is stripped along with the closing quotation mark. Any text following the closing quotation marks is preserved.

So try using CreateProcess() or ShellExecute/Ex() to run an instance of cmd.exe with these parameters:

cmd.exe /C ""<path>\rsvars.bat" && msbuild "<path>\project" <msbuild parameters> ..."

Alternatively, you can have your app load rsvars.bat and parse out the values it defines (or just define the values in your own code), and then execute msbuild using CreateProcess(), passing the desired environment variables to its lpEnvironment parameter. This way, you do not need to actually execute rsvars.bat at all.

Sign up to request clarification or add additional context in comments.

7 Comments

Are the quotes supposed to be wrapped around all parameters? I think that's what's throwing me off, because some of the parameters themselves have their own quotes around the full path/filename values.
@JerryDodge: Per the documentation I posted, the entire <String> needs to be in quotes in order to use &&, and the individual commands can have their own quoted parameters as well. I updated my answer with a larger quote of the documentation.
Thank you, that did the trick. However now I'm pursuing how to completely ignore the rsvars.bat file altogether, and call msbuild directly.
Well, you can't ignore it, since it does define some important environment variables that Delphi projects actually use. But you don't need to execute rsvars.bat itself to give those variables to msbuild, you can pass them directly to CreateProcess() instead.
That's what I meant, ignore the actual file itself, and just implement the same thing directly in my application. I also re-worded my question so that I'm asking for any alternative possibility besides including rsvars.bat.
|
5

Another way which works is to put both commands together into another new batch file. This also allows you to build multiple projects using the same environment. For example DoBuild.bat with the following contents:

call "C:\Program Files (x86)\Embarcadero\Studio\17.0\bin\rsvars.bat"
msbuild "<path>\MyProject.dproj"
msbuild "<path>\MyOtherProject.dproj"
msbuild "<path>\YetAnotherProject.dproj"

Then, just execute your batch file:

Cmd.exe /K "<path>\DoBuild.bat"

1 Comment

If you use /C, cmd.exe terminates after the command is finished. If you use /K instead, it keeps running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.