Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto increment build number using PlatformIO

I have several micro controller projects for home automation. Each of my nodes have a version number which is manually set in the code. This version number is reported during the startup of the node to inform me which code is running.

Sometimes changing the version number is forgotten after making some changes in the code. So an automatic solution has to be found.

I have some idea about the solution:

  1. create a file (version.h): #define BUILDNO xxx
  2. include it in the relevant c codes
  3. auto increment xxx before every build

Can it be implemented? Or are there any other solutions with similar result?

like image 903
pvoj Avatar asked Dec 13 '22 11:12

pvoj


1 Answers

I have made some research based on answers to my question. PlatformIO can run custom scripts before compile. Here is the process to generate a build number and include it into your project code:

  1. Create a Python script into the project folder: buildscript_versioning.py
FILENAME_BUILDNO = 'versioning'
FILENAME_VERSION_H = 'include/version.h'
version = 'v0.1.'

import datetime

build_no = 0
try:
    with open(FILENAME_BUILDNO) as f:
        build_no = int(f.readline()) + 1
except:
    print('Starting build number from 1..')
    build_no = 1
with open(FILENAME_BUILDNO, 'w+') as f:
    f.write(str(build_no))
    print('Build number: {}'.format(build_no))

hf = """
#ifndef BUILD_NUMBER
  #define BUILD_NUMBER "{}"
#endif
#ifndef VERSION
  #define VERSION "{} - {}"
#endif
#ifndef VERSION_SHORT
  #define VERSION_SHORT "{}"
#endif
""".format(build_no, version+str(build_no), datetime.datetime.now(), version+str(build_no))
with open(FILENAME_VERSION_H, 'w+') as f:
    f.write(hf)
  1. Add a line to platformio.ini:
    extra_scripts = 
        pre:buildscript_versioning.py

Building your project will run the script. 2 files will be created:

  • versioning: a simple text file to store the last build number

  • include/version.h: header file to be included

Now you can add this line to your C code:

#include <version.h>

I started a gitlab repository with some documentation here: https://gitlab.com/pvojnisek/buildnumber-for-platformio/tree/master Further ideas are welcome!

like image 118
pvoj Avatar answered Dec 21 '22 11:12

pvoj