cancel
Showing results for 
Search instead for 
Did you mean: 

C-MACRO in Pre-Build script

FMass.1
Associate III

Hi, for my STM32CubeIDE projects it would be convenient to have a macro whose value increases every time I compile the project; As if it were a counter of the times I pressed the build button, to be used as an ID to identify the specific build.
For example, in my code I would like to put a printf at the beginning that prints this number to understand which build the executable being executed belongs to.

On other IDEs there is a way to make a kind of pre-build script that is executed before compiling the project, the question is: is it possible to write a pre-build script also in STM32CubeIDE? If yes, how? In what programming language? Can I read and write C-MACROs from these scripts?

Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

Yes I have one example, but not with CubeIDE.

I have "version.h" with all the usual major/minor numbers. Then I made a small c file. "getversion.c" :

//getversion.c

#include "Inc\version.h"
//#define FW_VERSION_SUFFIX     -beta
//#define FW_VERSION_MAJOR 5
//..... etc
// #define STRINGIFY(s) #s
//#define VERSIONIZE(x, y, z, w) STRINGIFY(x) "." STRINGIFY(y) "." STRINGIFY(z) STRINGIFY(w)

//#define MY_FW_VERSION   VERSIONIZE(FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_REVISION, FW_VERSION_SUFFIX)

MY_FW_VERSION

 

Then, in the pre-build script I run the compiler on getversion.c  and get the actual version string like "5.2.0-beta":

iccarm --preprocess=ns $scripts_dir/getver.tmp $scripts_dir/getversion.c" > /dev/null

 

From that I generate various things to handle the build outputs.

But I don't modify the version automatically. Instead I edit version.h manually and assign the version number agreed with the customer.

You can use pre-build script to parse your previous version number and increment it before build.

 

View solution in original post

7 REPLIES 7
bbee
Associate III

Use the Pre-build or Post-build steps in your project properties under 'C/C++ Build > Settings > Build Steps' to call any script you want. Unfortunately there is no build in feature to support build numbers.

Andrew Neil
Evangelist III

I have a timestamp.c file:

 

/* Public  variables ---------------------------------------------------------*/
const char build_date[] = __DATE__;
const char build_time[] = __TIME__;

 

So those strings can be printed as required - to show when the code was built.

I use this Pre-build step to ensure that the file is always recompiled:

AndrewNeil_1-1713514435169.png

 

Slight downside with this is that the timestamp file will always be rebuilt - even if no other files have changed.

I haven't found a better way in Eclipse to specify "include in every build" - like other tools have.

 

 

 

Pavel A.
Evangelist III

You can do this in a usual "sh" or "dash" shell script. CubeIDE for Windows comes with set of common posix utilities. On Linux, OS/X you have the native bash and more.

Pre-build and post-build commands are set up per project, in the project settings.

Can I read and write C-MACROs from these scripts?

C-MACRO? if you mean, preprocessing C-like expressions with defines and includes - yes, you can run the C compiler and produce preprocessor output.

 

 

I think that would suffer the same issue as my "timestamp":  ie, the "build number" would get updated every time you press the 'Build' button - whether or not there was anything else in the rest of the project that would have been built?

The 'Pre-build' seems to come before the evaluation of whether there is actually anything that needs building?


@Pavel A. wrote:

C-MACRO? if you mean, preprocessing C-like expressions with defines and includes - yes, you can run the C compiler and produce preprocessor output.

 

Ok, this is exactly what I need to do, but how do I start the compiler before compiling? Do you have any examples of how to do this?

Pavel A.
Evangelist III

Yes I have one example, but not with CubeIDE.

I have "version.h" with all the usual major/minor numbers. Then I made a small c file. "getversion.c" :

//getversion.c

#include "Inc\version.h"
//#define FW_VERSION_SUFFIX     -beta
//#define FW_VERSION_MAJOR 5
//..... etc
// #define STRINGIFY(s) #s
//#define VERSIONIZE(x, y, z, w) STRINGIFY(x) "." STRINGIFY(y) "." STRINGIFY(z) STRINGIFY(w)

//#define MY_FW_VERSION   VERSIONIZE(FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_REVISION, FW_VERSION_SUFFIX)

MY_FW_VERSION

 

Then, in the pre-build script I run the compiler on getversion.c  and get the actual version string like "5.2.0-beta":

iccarm --preprocess=ns $scripts_dir/getver.tmp $scripts_dir/getversion.c" > /dev/null

 

From that I generate various things to handle the build outputs.

But I don't modify the version automatically. Instead I edit version.h manually and assign the version number agreed with the customer.

You can use pre-build script to parse your previous version number and increment it before build.

 


@Andrew Neil wrote:

I think that would suffer the same issue as my "timestamp":  ie, the "build number" would get updated every time you press the 'Build' button - whether or not there was anything else in the rest of the project that would have been built?

The 'Pre-build' seems to come before the evaluation of whether there is actually anything that needs building?


Emphasis added because this is precisely the situation I'm trying to solve.  Starting a debug session, for example, automatically triggers an incremental build (this behavior can be changed, but I want it to do that).  I don't especially want my build numbers incrementing just because I started a new debug session without changing any code.  

I see that it's possible to change the arguments passed to make; is it possible to have user modifications to the makefile that will persist across clean/build cycles?  I suspect make itself is clever enough to "do this only if you did that".