Welcome, Guest. Please Login or Register
Arduino IDE for Visual Studio
 
 
When Logging a Support Issue in the Forum, please ensure you have also:-
  • Enabled vMicro > Compiler > Show Build Properties and Verbose
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
Support requests without the output above may be impossible to answer, so please help us to help you
 
  HomeSearch Tutorials & Wiki visualmicro.com LoginHelpRegister  
 
Page Index Toggle Pages: 1
Send Topic Print
Automatic build version in built code (Read 1304 times)
Acuario
Member
***
Offline



Posts: 104
Automatic build version in built code
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
Back to top
 
 
IP Logged
 
Acuario
Member
***
Offline



Posts: 104
RE: Automatic build version in built code (SOLUTION)
Reply #1 - 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 


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"
 



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
Back to top
« Last Edit: Dec 24th, 2018 at 3:30pm by Visual Micro »  
 
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10149
United Kingdom
Re: Automatic build version in built code
Reply #2 - Dec 24th, 2018 at 3:32pm
 
Excellent thank you!
Back to top
 
WWW  
IP Logged
 
Acuario
Member
***
Offline



Posts: 104
Re: Automatic build version in built code
Reply #3 - 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" 



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.
Back to top
 
 
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10149
United Kingdom
Re: Automatic build version in built code
Reply #4 - Jun 10th, 2019 at 2:00pm
 
Thanks, sadly, the quotes cause an issue with some platforms. Sorry forgot to add to the release notes.
Back to top
 
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print