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:
1 |
call "C:Program Files (x86)EmbarcaderoStudio22.0binrsvars.bat" |
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:
1 |
@SET MyMainProjectBin=C:WorkMainProjectBin |
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:
1 |
MSBuild ".MainProject.dproj" |
Instead of the .dproj file, you can indicate the groupproj file, if you need to compile a group of projects. For instance:
1 |
MSBuild ".GroupOfProjects.groupproj" |
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:
1 |
/p:config ="My config" |
/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:
1 |
MSBuild ".MainProject.dproj" /t:Build /p:Config=Release /p:platform=Win64 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
@echo off :: We parse 3 params, they must be: :: BuildMode - Build, Clean, Make :: ConfigMode - Debug, Release :: PlatformMode - Win32, Win64 :: Check, maybe user want to see some help info if [%1%]==[?] goto info if [%1%]==[/?] goto info :: Parse all external params and set their values into variables FOR %%A IN (%*) DO ( FOR /f "tokens=1,2 delims=:" %%G IN ("%%A") DO set %%G=%%H ) :CheckParams :: Validate platform IF NOT [%PlatformMode%]==[Win64] ( IF not [%PlatformMode%]==[Win32] ( goto inputPlatform ) ) :: Validate Config IF NOT [%ConfigMode%]==[Debug] ( IF not [%ConfigMode%]==[Release] ( goto inputConfigMode ) ) IF NOT [%BuildMode%]==[Build] ( IF not [%BuildMode%]==[Clean] ( IF not [%BuildMode%]==[Make] ( goto inputBuildMode ) ) ) :: If we are here - then all params are valid. Skip all and start to compile. goto DoCompile :info echo Params: echo BuildMode - Build, Clean, Make echo ConfigMode - Debug, Release echo PlatformMode - Win32, Win64 echo Example: echo RunCompiler.bat PlatformMode:Win32 ConfigMode:Debug BuildMode:Build goto end :inputPlatform echo Select Platform mode: echo 1: Win32 echo 2: Win64 SET /P PlatformMode="PlatformMode [1|2|Win32|Win64]>" if /i [%PlatformMode%]==[Win32] goto CheckParams if /i [%PlatformMode%]==[Win64] goto CheckParams if /i [%PlatformMode%]==[1] ( SET PlatformMode=Win32 goto CheckParams ) if /i [%PlatformMode%]==[2] ( SET PlatformMode=Win64 goto CheckParams ) GOTO inputPlatform :inputConfigMode echo Select ConfigMode mode: echo 1: Debug echo 2: Release SET /P ConfigMode="ConfigMode [1|2|D|R]>" if /i [%ConfigMode%]==[Debug] goto CheckParams if /i [%ConfigMode%]==[Release] goto CheckParams if /i [%ConfigMode%]==[1] ( SET ConfigMode=Debug goto CheckParams ) if /i [%ConfigMode%]==[2] ( SET ConfigMode=Release goto CheckParams ) if /i [%ConfigMode%]==[D] ( SET ConfigMode=Debug goto CheckParams ) if /i [%ConfigMode%]==[R] ( SET ConfigMode=Release goto CheckParams ) GOTO inputConfigMode :inputBuildMode echo Select BuildMode mode: echo 1: Build echo 2: Clean echo 3: Make SET /P BuildMode="BuildMode [1|2|3|B|C|M]>" if /i [%BuildMode%]==[Build] goto CheckParams if /i [%BuildMode%]==[Clean] goto CheckParams if /i [%BuildMode%]==[Make] goto CheckParams if /i [%BuildMode%]==[1] ( SET BuildMode=Build goto CheckParams ) if /i [%BuildMode%]==[2] ( SET BuildMode=Clean goto CheckParams ) if /i [%BuildMode%]==[3] ( SET BuildMode=Make goto CheckParams ) if /i [%BuildMode%]==[B] ( SET BuildMode=Build goto CheckParams ) if /i [%BuildMode%]==[C] ( SET BuildMode=Clean goto CheckParams ) if /i [%BuildMode%]==[M] ( SET BuildMode=Make goto CheckParams ) :DoCompile REM NOTE THAT DUE TO THE WAY THE BLOG WORKS YOU NEED TO REM CHANGE THE SLASHES (AND REMOVE THESE TWO COMMENT LINES) call "c:/Program Files (x86)/Embarcadero/Studio/22.0/bin/rsvars.bat" echo Start %BuildMode% %PlatformMode% %ConfigMode% for "MainProject" MSBuild ".MainProject.dproj" /t:%BuildMode% /p:Config=%ConfigMode% /p:platform=%PlatformMode% if not [%ERRORLEVEL%]==[0] ( goto fail ) else ( goto end ) :fail echo Build Failed!!! Params: %BuildMode% %PlatformMode% %ConfigMode% pause exit /b :end |
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:
1 |
RunCompiler.bat PlatformMode:Win64 ConfigMode:Debug BuildMode:Build |
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:
1 |
MSBuild ".MainProject.dproj" /nologo /t:%BuildMode% /l:FileLogger,Microsoft.Build.Engine;logfile=MainProject_%PlatformMode%_%ConfigMode%.log;verbosity=quiet /p:Config=%ConfigMode% /p:platform=%PlatformMode% |
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.
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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition