Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
DelphiTech Partner

Command-line Compilation Of Delphi Projects: Real-life Examples

Command line compilation of Delphi projects Real life examples
command line compilation of delphi projects

Delphi app command-line compilation allows developers to compile a project in the background regime without the necessity to run IDE software. This approach is very useful for automating the compilation process, especially in those cases when you need to compile a lot of projects and/or libraries. Command-line compilation is conducted by means of the MSBuild command. The parameters of this command are considered in detail in the following article:https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Building_a_Project_Using_an_MSBuild_Command.

In our blog post, we offer you to analyze an example of creating a BAT file for compiling a VCL app.

Let’s start with the basics. When you initiate compilation from the IDE, the environment automatically registers environment variables. To compile the project from the command line using MSBuild, it is required to register environment variables. All the necessary settings for this purpose are already available in the rsvars.bat file (located in the bin folder). By default, for Delphi 11, the path to the rsvars.bat file is as follows: C:Program Files (x86)EmbarcaderoStudio22.0bin. Therefore, in the BAT file, before calling MSBuild, you should run the following command:

The rsvars.bat file contains system variables. If you have added your own variables to Delphi (Tools -> Options -> IDE -> Environment Variables -> User System Overrides) and they are used in the settings of your projects, the value of these variables should be indicated manually in your BAT file. For example:

Command line Compilation Of Delphi Projects Real life Examples Enviroment Variables System Override

So, at this step already, we can compile our project from the command line. To do this, you need to call MSBuild with the indication of the path to the .dproj file. If the project is named “MainProject,” the command will look like this:

Instead of the .dproj file, you can indicate the groupproj file, if you need to compile a group of projects. For instance:

MSBuild will compile the projects in the group one by one.

The drawback of this approach is the fact that compilation will occur partially with default parameters and partially with parameters from the .dproj file. There may be a situation when you need to compile the project for the Win64 platform and the Release configuration, while by default, the project file selects the Win32 platform and the Debug configuration. In this scenario, you need to specify additional parameters. Now, we will discuss some of them.

/t:<target name> is a parameter that allows you to inform the compiler what exactly should be done. There are three standard values: clean, make, and build. Clean – means to clean the project, removing generated files such as object code. Make – means to compile the project. Build – means to build the project.

/p:config= is a parameter that allows you to indicate configuration. It can be either a default configuration, such as Debug or Release, or a configuration that you have added to the project. If the configuration name has a space in it, enter the name bounded by double quotes, such as:

/p:platform= allows you to specify your target platforms. Among the possible variants we should name Win32, Win64, Android, iOSDevice64, Linux64.

It means that if it is required to compile an app for Win64 with the Release configuration, the command line will look like this:

But what if you don’t always need exactly that configuration? BAT files provide the possibility to work with variables. As a result, you can ask the user about what needs to be done during script execution. Additionally, BAT files allow you to pass input parameters. For example, it makes sense to pass the following as input parameters: the target platform (Win32, Win64), the configuration (Release, Debug), and what action should be performed (Clean, Make, Build). Below you can see an example of a universal script:

If you run this script without providing input parameters, you will need to enter the values of these parameters during script execution. Let’s assume that the script is saved in a file named RunCompiler.bat. Then, the execution of the script with input parameters will be the following:

At the end of the script, a special variable %ERRORLEVEL% is analyzed. When MSBuild is run, this variable is responsible for the compilation result. If the value of the %ERRORLEVEL% variable is different from zero, it means that an error occurred during the compilation. If the compilation is not successful, the script execution is paused, and you need to press any key.

You can output information about the compilation process to a file using the /l parameter. Here’s an example of a command:

As a result, we have a universal script that can be easily expanded. Based on this script, you can write another script that will use all the latest updates from the Git repository, delete obsolete binary files, and compile new ones.

How To Record Exact Operation Execution Time With Delphi Delphi logo

Do you want to try some of these examples for yourself? Why not download a free trial of the latest version of RAD Studio with Delphi?

This article was written by Embarcadero Tech Partner Softacom. Softacom specialize in all sorts of software development focused on Delphi. Read more about their services on the Softacom website.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES