delphibatch-filebuilddelphi-6

How to create a batch file to build all projects in Delphi with a log result


Using Azure DevOps with Git, I was trying to create a command line to compile and build all the projects (.dpr) we have in the company. With this, we can ensure that everything is working in all PRs and with a clean log (with hints, warnings and errors) we can compare if the commits increase, especially the warnings to alert the developer by pipeline built to analyze this.

So my question is, how to generate a batch that compiles all the projects in Delphi generating a log with the hints, warnings and errors?


Solution

  • The batch below can build all projects (.dpr) in the main path (D:\Projects) showing the results and with a full log (standard Delphi compiler) and a clean log to use for comparing and alerting developers (hints, warnings...)

    @echo off
    setlocal enabledelayedexpansion
    
    :: Sets the folder where the .bat is being executed
    set SCRIPT_PATH=%~dp0
    :: Sets unified log files
    set LOG_FULL="%SCRIPT_PATH%LOG_FULL.txt"
    set LOG_CLEAN="%SCRIPT_PATH%LOG_CLEAN.txt"
    :: Base path where the projects are
    set BASE_PATH=D:\PROJECTS
    :: Sets the compiler output folder for exe/dll
    set OUTPUT_EXE=D:\PROJECTS\EXE
    :: Sets the template res file for projects that do not have a .res
    set DUMMY_RES=D:\PROJECTS\dummy.res
    :: Deletes previous logs (if they exist)
    if exist %LOG_FULL% del %LOG_FULL%
    if exist %LOG_CLEAN% del %LOG_CLEAN%
    
    echo Compiling projects in Delphi 6:
    
    :: Loop to recursively go through all .dpr files
    FOR /R "%BASE_PATH%" %%i IN (*.dpr) DO (
        echo =============================================================================== >> %LOG_FULL%
        echo Compiling project:: %%~nxi em "%%~dpi" >> %LOG_FULL%
        echo =============================================================================== >> %LOG_FULL%
    
        echo   %%~dpi%%~nxi
    
        :: Changes to the project folder
        cd "%%~dpi"
    
        :: Checks if the .res file exists
        if not exist "%%~dpi%%~ni.res" (
            echo Creating .res from the template...
            copy /Y "%DUMMY_RES%" "%%~dpi%%~ni.res" >nul
        )
    
        :: Runs the compiler, redirects the output to the unified log and ignores the output of the executables configured in the project, throwing them all in the configured folder
        dcc32.exe "%%i" -$D- -GD- -B -E"%OUTPUT_EXE%" -N"D:\Dcus" -U"C:\Program Files (x86)\Borland\Delphi6\Lib;C:\Program Files (x86)\Borland\Delphi6\Bin; .... <ALL LIBRARY PATHS> " >> %LOG_FULL% 2>&1
    
        :: Captures the compilation error code
        set COMPILATION_ERROR=!errorlevel!
    
        :: If the compiler (dcc32.exe) gave an error, display a message and exit with an error
        if !COMPILATION_ERROR! neq 0 (
            echo ERROR: Build failed for %%~nxi em "%%~dpi" >> %LOG_FULL%
            echo ERROR: Build failed for %%~nxi. Veja %LOG_FULL%
            exit /b !COMPILATION_ERROR!
        )
    )
    
    :: Remove trailing spaces at the end of lines using PowerShell
    powershell -Command "(Get-Content %LOG_FULL%) -replace '\s+$','' | Set-Content %LOG_FULL%"
    
    :: Filter only error, warning and hint messages
    findstr /I /C:"Warning:" /C:"Hint:" /C:"Error:" /C:"Fatal:" /C:"Fatal" /C:"ERROR:" /C:"======" /C:"Compiling" %LOG_FULL% >> %LOG_CLEAN%
    set FINDSTR_ERROR=!errorlevel!
    
    :: If there were warnings or hints in the log, display the alert
    if !FINDSTR_ERROR! equ 0 (
        echo ALERT: Projects have warnings or hints. See %LOG_CLEAN%
    ) else (
        echo SUCCESS: Build completed without warnings or hints!
    )
    
    echo ====================================
    echo  ALL PROJECTS HAVE BEEN COMPILED
    echo ====================================
    pause