Ivan Mitic
Ivan Mitic

Reputation: 13

Automatic versioning during C code building

I have several HW projects done in C, which I am maintaining for which I would like to add automatic versioning if possible. They include HW written in C for Atxmega (Atmel Studio), dsPIC33 (MPLAB IDE) and Microblaze (Xilinx SDK).

The idea is that a build tool should be able to insert version information automatically before compilation (major, minor, patch and time) into a dedicated macro which I can then use in the code and send out that information when requested (and then get version command via communication interface). Then as soon as someone updates and recompiles the code, the end user will get this information when using the system via this get version command.

I have found out that I can maybe use predefined macros for this. My question: Is there some standard way of doing this which I can utilize across different SDKs and compilers?

My other ideas where writing a python script to edit the code manually before compilation or even modify the hex or elf file after compilation.

Is there a way to connect it somehow with the git and then send out the git commit ID (from a macro) which would also do the job just fine.

Thank you,

Ivan

Upvotes: 1

Views: 580

Answers (1)

Alex Nicolaou
Alex Nicolaou

Reputation: 317

HolyBlackCat's answer is best. So far as I know, pretty much all compilers will accept a -D switch to let you define your version macro but you're surely using gcc anyway. Here's an example with a hardcoded string:

$ cat version.c
#include <stdio.h>

int main(int argc, char **argv) {
    printf("GCC Version: %s\n", __VERSION__);
    printf("Software Version: %s\n", RELEASE_NAME);
}
$ gcc -DRELEASE_NAME='"v0.9 alpha"' version.c 
$ ./a.out
GCC Version: Apple LLVM 12.0.5 (clang-1205.0.22.9)
Software Version: v0.9 alpha

I would recommend writing a short script to construct your version string, and then passing it to the compiler. There are lots of things you might want to check (working tree clean, date, time, build architecture, target architecture) in the script. Since your target is embedded you'll then want to encode that somehow into a small string so there is lots to think about for that step; but passing it into gcc should be no problem (and I'm having a hard time imagining another toolchain for your listed targets).

Upvotes: 1

Related Questions