Author Topic: IAR - Putting a #define into the compiled hex filename  (Read 2507 times)

0 Members and 1 Guest are viewing this topic.

Offline PsiTopic starter

  • Super Contributor
  • ***
  • Posts: 9930
  • Country: nz
IAR - Putting a #define into the compiled hex filename
« on: February 18, 2018, 08:59:09 pm »
Has anyone managed to get IAR to compile a hex with a #define string the compiled filename.

etc..
In code have
#define VERSION  123

And 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.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: IAR - Putting a #define into the compiled hex filename
« Reply #1 on: February 18, 2018, 09:23:33 pm »
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: [Select]
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 symbol VERSION in the source code without specific #define.

In the command line batch file you can the use the same environment variable $VERSION to 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: [Select]
echo "#ifndef VERSION_H" > version.h
echo "#define VERSION_H" >> version.h
echo "#define VERSION $VERSION" >> version.h
echo "#endif /* VERSION_H */" >> version.h
 

Online ajb

  • Super Contributor
  • ***
  • Posts: 2600
  • Country: us
Re: IAR - Putting a #define into the compiled hex filename
« Reply #2 on: February 18, 2018, 09:48:05 pm »
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. 
 

Offline filssavi

  • Frequent Contributor
  • **
  • Posts: 433
Re: IAR - Putting a #define into the compiled hex filename
« Reply #3 on: February 18, 2018, 10:05:51 pm »
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 program


Now 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 accordingly

You 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
« Last Edit: February 18, 2018, 10:09:15 pm by filssavi »
 

Offline PsiTopic starter

  • Super Contributor
  • ***
  • Posts: 9930
  • Country: nz
Re: IAR - Putting a #define into the compiled hex filename
« Reply #4 on: February 19, 2018, 02:37:02 am »
Thanks guys,

Just thought i'd ask.

Sounds like a post-build command line app/batch is the way to go.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online mikerj

  • Super Contributor
  • ***
  • Posts: 3238
  • Country: gb
Re: IAR - Putting a #define into the compiled hex filename
« Reply #5 on: February 19, 2018, 12:43:36 pm »
Thanks guys,

Just thought i'd ask.

Sounds like a post-build command line app/batch is the way to go.

That'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 file

Code: [Select]
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_MAJOR
set MINOR_NAME=FIRMWARE_REV_MINOR
set PATCH_NAME=FIRMWARE_REV_PATCH

REM 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=%%~i

REM 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=%%~i

REM 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
 

Offline josip

  • Regular Contributor
  • *
  • Posts: 151
  • Country: hr
Re: IAR - Putting a #define into the compiled hex filename
« Reply #6 on: February 20, 2018, 08:16:28 am »
Has anyone managed to get IAR to compile a hex with a #define string the compiled filename.

etc..
In code have
#define VERSION  123

And 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.

I 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf