IAR - Putting a #define into the compiled hex filename - Page 1
Forum section: electronics / general

author: Psi
author_rank: Super Contributor
timestamp: February 18, 2018, 08:59:09 pm
content: Has anyone managed to get IAR to compile a hex with a #define string the compiled filename.etc..In code have#define VERSION 123And i want IAR to automatically generate"my hexfile V123.hex"I've had a play around and not been able to find a way to do this.I found a few people suggesting a couple of ways but none worked.I just thought id ask before taking the time to write a command line program to read the firmware header and rename the file post build.

author: Kalvin
author_rank: Super Contributor
timestamp: February 18, 2018, 09:23:33 pm
content: Unfortunately I do not know IAR, but typically you can define symbols as command line parameters which are then available to the C preprocessor.In the (Linux) command line batch file you can create an environment variable containing the version number as follows:Code:VERSION="123"In Windows environment you should be able to create environment variables with quite similar syntax.Then you can define the command line argument for the compiler-DVERSION=$VERSION.Now you can access the symbolVERSIONin the source code without specific #define.In the command line batch file you can the use the same environment variable$VERSIONto create the hex-file with the same version information in its name.If the compiler doesn't accept the command line arguments, you can always create a header file containing the version information:Code:echo "#ifndef VERSION_H" > version.hecho "#define VERSION_H" >> version.hecho "#define VERSION $VERSION" >> version.hecho "#endif /* VERSION_H */" >> version.h

author: ajb
author_rank: Super Contributor
timestamp: February 18, 2018, 09:48:05 pm
content: I don't know if IAR has the facility to use C symbols in output file name settings, but as a general solution, you can usually define external commands that can be run as pre- and post-build steps. At the simplest, you can use a batch file or similar to copy and rename the output file, or for more complex tasks you can invoke a python program that parses your C files and does whatever you want based on that information.

author: filssavi
author_rank: Frequent Contributor
timestamp: February 18, 2018, 10:05:51 pm
content: I’m not too familiar with IAR (I have used it just casually)But my gut’s feeling is that you will have a really hard time having a compiler do that, that is because usually you handle versioning in other ways, mainly with your source code management platform of choice (be it git SCN etc) and handling versions of the program is not the compiler’s job and thus output file names are quite unimportant.Now the way Iwould do what you ask is with by using a real build system (make, Cmake, etc) with that you can easily decide what the object files are called and where you put them, you can also pass symbols to the compiler from the makefile and that way you could check the version from your programNow if you really must use a #define you could use a simple regexp grep in the makefile to extract the version string form the files and subsequently use it to name the output files accordinglyYou can also most probably do this from the IDE but I find that usually this advanced build system options are really well hidden by many menus and sub menus and generally not that well documented

author: Psi
author_rank: Super Contributor
timestamp: February 19, 2018, 02:37:02 am
content: Thanks guys,Just thought i'd ask.Sounds like a post-build command line app/batch is the way to go.

author: mikerj
author_rank: Super Contributor
timestamp: February 19, 2018, 12:43:36 pm
content: Quote from: Psi on February 19, 2018, 02:37:02 amThat's the way I've always done things to include a version/build number into a filename. It's surprisingly simple to parse a file and extract a value via a batch fileCode:REM ******************FIRMWARE VERSION EXTRACTION**********************REM ** This code extracts the application version information from **REM ** a header file defined in %VERFILE% **REM *******************************************************************REM Set the 'descriptors' to search for.set MAJOR_NAME=FIRMWARE_REV_MAJORset MINOR_NAME=FIRMWARE_REV_MINORset PATCH_NAME=FIRMWARE_REV_PATCHREM Extract major version number from header file.for /F "usebackq tokens=3" %%i in (`findstr /L /C:"#define %MAJOR_NAME% " %VERFILE%`) do set MAJOR_VER=%%~iREM Extract minor version number from header file.for /F "usebackq tokens=3" %%i in (`findstr /L /C:"#define %MINOR_NAME% " %VERFILE%`) do set MINOR_VER=%%~iREM Extract patch version number from header file.for /F "usebackq tokens=3" %%i in (`findstr /L /C:"#define %PATCH_NAME% " %VERFILE%`) do set PATCH_VER=%%~i

author: josip
author_rank: Regular Contributor
timestamp: February 20, 2018, 08:16:28 am
content: Quote from: Psi on February 18, 2018, 08:59:09 pmI am using IAR, and even macros are powerful, there are things that can't be done by macros. I have made my own program for injecting custom staff (that can't be done by macros) in original source code, before build. When I have stable firmware version, than I use my program for packing it for distribution. More firmwares are generated from one source file, AES encrypted, and ziped, with version number present in filename.