Skip to content

Instantly share code, notes, and snippets.

@f-steff
Last active November 7, 2023 22:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save f-steff/97eceddb617312e0252a75445bfabbf8 to your computer and use it in GitHub Desktop.
Save f-steff/97eceddb617312e0252a75445bfabbf8 to your computer and use it in GitHub Desktop.
Use the Keil µVision compiler from the windows command line.
@echo off
setlocal
:: KeiluVisionBuilder.cmd
:: Written by Flemming Steffensen, 2019.
:: Free for use and abuse by anyone.
:: ======================
:: Configuration
set WaitForLicenseTimeout=60
set BuildAttemptsMax=10
set "ProjectFileName=bootloader.uvprojx"
set "ProjectPath=MyProject\bootloader\"
set "Compiler=C:\Keil_v5\UV4\UV4.exe"
set "OutFolder=out_585\"
:: ======================
:: Do not edit below this line
set BuildAttempt=0
pushd %ProjectPath%
:PerformBuild
echo:
echo:Performing Keil Build...
if exist build.log del build.log
if exist %OutFolder%*.build_log.htm del %OutFolder%*.build_log.htm
start /wait %Compiler% -j0 -b %ProjectFileName% -o build.log
set ReportedError=%ERRORLEVEL%
:: Scan build.log to determine if the license is locked.
find /c "Error: *** All Flex licenses are in use! ***" build.log >nul
if %errorlevel% equ 0 (
set /a BuildAttempt=%BuildAttempt% + 1
echo:Error: *** All Flex licenses are in use!
if %BuildAttempt% equ %BuildAttemptsMax% goto NoLicenseAvailable
echo:Retrying ^(%BuildAttempt% of %BuildAttemptsMax%^) in %WaitForLicenseTimeout% seconds
waitfor SignalNeverComming /T %WaitForLicenseTimeout% >nul 2>&1
goto PerformBuild
)
:: Scan alternative build.log to determine if the license is locked.
find /c "Failed to check out a license" %OutFolder%*.build_log.htm >nul
if %errorlevel% equ 0 (
set /a BuildAttempt=%BuildAttempt% + 1
echo:Error: Failed to check out a license
if %BuildAttempt% equ %BuildAttemptsMax% goto NoLicenseAvailable
echo:Retrying ^(%BuildAttempt% of %BuildAttemptsMax%^) in %WaitForLicenseTimeout% seconds
waitfor SignalNeverComming /T %WaitForLicenseTimeout% >nul 2>&1
goto PerformBuild
)
goto NoLicenseProblem
:NoLicenseAvailable
echo:Error: After %BuildAttempt% attempts, the Flex license still appear to be unavailable. Failing the build!
echo:
popd
exit /b 1
:NoLicenseProblem
:: Parse exit codes
set KnownErrors=0 1 2 3 11 12 13 15 20 41
echo:Kiel compiler exited with error code %ReportedError%
for %%a in (%KnownErrors%) do (
if [%ReportedError%] equ [%%a] goto Error%ReportedError%
)
goto UnknownError
:Error0
echo Compilation successful
goto ExitButContinueJob
:Error1
echo Warnings were found
goto ExitButContinueJob
:Error2
echo Errors were found
goto ExitCritical
:Error3
echo Error 3 = Fatal Errors
goto ExitCritical
:Error11
echo Error 11 = Cannot open project file for writing
goto ExitCritical
:Error12
echo Error 12 = Device with given name in not found in database
goto ExitCritical
:Error13
echo Error 13 = Error writing project file
goto ExitCritical
:Error15
echo Error 15 = Error reading import XML file
goto ExitCritical
:Error20
echo Error 20 = Can not convert the project file.
goto ExitCritical
:Error41
echo Error 41 = Can not create the logfile requested using the -l switch.
goto ExitCritical
:UnknownError
echo Error %ReportedError% = Unknown error
goto ExitCritical
:ExitCritical
echo:
if [%ReportedError%] neq 0 exit /b %ReportedError%
:ExitButContinueJob
echo:
popd
exit /b 0

Command line compiling with the Keil µVision compiler (at least version 4 and 5)

Compiling your Kiel µVision projects from the commandline should be easy, and out of the box it is resonable simple. However the Commandline interface is very limited and it has basically not changed since 2011.

The KeiluVisionBuilder.cmd batch script attempts to handle the following situations:

  • Suppress tge compiler Windows GUI for error reporting; which is especially a probelem when using the compiler on a build server.
  • Operates on all the output streams from the compiler; Strangely the Keil uVision compiler creates up to two sets of output: Either none, one, the other or both output files are created.
  • When the Flex license server is used, a number of conflicts can occour, so in this script you can:
    • Define a number of times you want to retry a build
    • Set the interval between attempted builds.
    • Successfully recover if the license is suddenly withdrawn during a build.

Originally published in this Stack Overflow answer.

@handsometree
Copy link

Dear author, I come across your script while searching for method to build Keil uVision 5 project automatically from build pipeline. May I ask how the action of "Checking-out Flexnet license before build, checking-in afterwards" is handled in the script? There seems to be no explicit statement for license actions. The way in which the script is currently written seems to suggest that, the license is automatically booked/checked-out when the uVision executable is invoked from the command line. Is this the case? Your kind confirmation would be highly appreciated!

@f-steff
Copy link
Author

f-steff commented Jul 14, 2022

@handsometree

Sorry for this very late response. Apparently, I do not receive notifications for comments on this gist. Strange!

I hope you have solved your problem since then, but for completeness:

Yes, the Flexnet license is automatically checked-out and returned when the uVision executable is invoked from the command line.
You are required to configure it in Tools.ini using a syntax similar to this:

[UV2]
...
FLEX_VARIANT=mdk_pro
FLEX=portNumber@hostServer
FLEX_USE=1
...

The URL portNumber@hostServer needs to be adapted to point to your FlexNet license server.
FLEX_VARIANT needs to be set to either mdk_pro, mdk_std or mdk_cm_std, depending on the exact license.

Do you have a writeup on how you build Keil uVision 5 project automatically from build pipeline?
I'd be interested!

@francoisftex
Copy link

@f-steff thanks for the script. Greatly useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment