Incremental build number (manual versioning scheme)

martins
Posts: 51
Joined: Tue Aug 24, 2021 8:58 am

Incremental build number (manual versioning scheme)

Postby martins » Tue Sep 13, 2022 8:30 am

This is just a quick tip if you want to use simple manual versioning of your project. Both legacy make and Cmake IDF projects does support use of version.txt file where you can directly write whatever text up to 32 characters and it gets automatically added to output binary on each build (including incremental). So you can also write your version number to this file in following format: <major>.<minor>.<bugfix>+<buildNo>. Now the question would be how to increment the <buildNo> automatically.


Cmake build

Executing bash commands in cmake seems bit too messy to me so I rather used more native functionality of cmake. I would expect it to be quite platform-independent (only tested on Ubuntu tho). This is what I ended up with:

1. Create version.txt file in your project's root folder and fill in your initial version number (1.0.0+0 for exaple)
2. Add the code below to CmakeLists.txt in project's root before the include($ENV{IDF_PATH}/tools/cmake/project.cmake) line.
3. Thats it, you can build as ussual.
4. version.txt should now have 1.0.0+1 written in it and same number should be in the main output binary.

  1. # Read version from file:
  2. file(READ "version.txt" VERSION_STRING)
  3. # Remove whitespace characters (like newline):
  4. string(STRIP ${VERSION_STRING} VERSION_STRING)
  5. # Match anything before build metadata (+ character) and save it as VER_NUM (usual regex would be: /^(.*?)\+/ but this is cmake):
  6. string(REGEX MATCHALL "^(\.*)\\+" VER_NUM ${VERSION_STRING} )
  7. # Match last number and save it as BUILD_NUM:
  8. string(REGEX MATCHALL "([0-9]+)$" BUILD_NUM ${VERSION_STRING} )
  9. # Increase build number:
  10. math(EXPR BUILD_NUM "${BUILD_NUM}+1")
  11. # Concatenate back to one string:
  12. string(CONCAT VERSION_STRING ${VER_NUM} ${BUILD_NUM})
  13. # Print info and save to file:
  14. #message(NOTICE "New build number: ${VERSION_STRING}")
  15. file(WRITE "version.txt" ${VERSION_STRING})

Legacy make build

Original version where I simply used bash command line tools (like cut). Only tested on Linux based OS, other ones may not be compatilbe in this exact form. Each run of make ver will increment the number even if source code files did not change at all.

1. Create version.txt file in your project's root folder and fill in your initial version number (1.0.0+0 for exaple)
2. Add the code below to Makefile in project's root BEFORE the include $(IDF_PATH)/make/project.mk line:
3. Add ver to your build command, ie.: make ver all and run it
4. version.txt should now have 1.0.0+1 written in it and same number should be in the main output binary.

  1. VERSION_FILE=version.txt
  2. ver:
  3. # Parses the version number from VERSION_FILE by + character and adds 1 to last value.
  4. # Expected formatting: <major>.<minor>.<bugfix>+<buildNo>
  5.     @echo $$(cut -d "+" -f 1 $(VERSION_FILE))+$$(($$(cut -d "+" -f 2 $(VERSION_FILE)) + 1)) > $(VERSION_FILE)

Edit: Changed to comply with semantic versioning. In this example the build number would be included in build metadata, which is marked by + character per semantic versioning, and would be the ONLY part of build metadata. Preceding parts shoud not matter (regex match anything before + and can include pre-release versions too).

Who is online

Users browsing this forum: No registered users and 47 guests