Visual Micro
https://www.visualmicro.com/forums/YaBB.pl
>> >> Automatic build version in built code
https://www.visualmicro.com/forums/YaBB.pl?num=1545471409

Message started by Acuario on Dec 22nd, 2018 at 9:36am

Title: Automatic build version in built code
Post by Acuario on Dec 22nd, 2018 at 9:36am
Hi,

it would be a nice feature to be able to automatically include a build number in built bin files that can then be used in the Sketch.

For example:
A file 'BuildVersion.h' included in the project that contains:
#define VERSION 3.1

Each time the project is built this number gets automatically (if desired by a setting)  incremented

The version can then be used as desired in the sketch.

I guess this could be implemented using a pre or post build script.

Acuario

Title: RE: Automatic build version in built code (SOLUTION)
Post by Acuario on Dec 23rd, 2018 at 7:42am
For anyone who wants this feature, here is a way to achieve at least a basic way of doing it.

1. Create a file in your project called version.h
2. Put the following in version.h

Code (]#define VERSION 1[/code):
3. Create a version.bat file in your project and mark it as Exclude from project
4. Add the following to version.bat:
[code]@echo off
setlocal EnableDelayedExpansion
set file=%1

for /F "tokens=1-3 delims=N " %%a in (%file%) do (
   for %%A in (%%a %%b %%c) do set "i=%%A"
)
set /a i=i+1
echo #define VERSION %i% > %file%


5. Add the following to your Board.txt

Code (]# Increment version before build
recipe.hooks.sketch.prebuild.1.pattern=cmd.exe /c "{build.project_path}\version.bat {build.project_path}\version.h"
[/code):

6. Add #Include "version.h" in your sketch

The version number will increment by 1 every time you do a build, irrespective if it succeeds or fails.

To use the version just use the defined VERSION in your code for example:
[code]Serial.printf("Startup Version:%d\n", VERSION);


Acuario

Title: Re: Automatic build version in built code
Post by Visual Micro on Dec 24th, 2018 at 3:32pm
Excellent thank you!

Title: Re: Automatic build version in built code
Post by Acuario on Jun 10th, 2019 at 8:48am
I just upgraded to the latest version of VS and it broke my auto update.

It appears something has changed in the build process and now the quotes on the command string are no longer required.

To fix this change the code in step 5 from this:

Code (]# Increment version before build
recipe.hooks.sketch.prebuild.1.pattern=cmd.exe /c "{build.project_path}\version.bat {build.project_path}\version.h"[/code):

to this:
[code]# Increment version before build
recipe.hooks.sketch.prebuild.1.pattern=cmd.exe /c {build.project_path}\version.bat {build.project_path}\version.h


The version increment then works correctly.

Title: Re: Automatic build version in built code
Post by Visual Micro on Jun 10th, 2019 at 2:00pm
Thanks, sadly, the quotes cause an issue with some platforms. Sorry forgot to add to the release notes.

Visual Micro » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2021. All Rights Reserved.