I use the latest Arduino IDE 2.x and integrated a automatic build number increment. I realize that the recipe hook runs in a infinite loop. I press the build button and the increment process do not stop.....
platform.txt file
# Automatic build number
recipe.hooks.sketch.prebuild.0.pattern="C:\Users\User\Documents\Arduino\prebuild.exe" {build.path} {build.source.path} {build.project_name}
This might give an idea about whether the problem is specific to the command you are running to generate the build number, or whether it is a universal problem that occurs no matter what the command is.
I tried the simple command in the hook and didn't get an infinite loop like you experienced.
Ok thank you. I see the output only once. But I see a problem. Maybe the problem is by the indexing process.On the left botton corner the text flashing on / off.
Building sketch
indexing 0/1
and after this step the buildnumber increments again.
No click on the build button is required. The indexing process runs infinite....
Yes, they do. The language server that provides the features like autocomplete and "Go to Definition" has to rebuild the compilation database for the sketch program after every change and that executes this command.
Ironically, the execution of the "prebuild" hooks was added after a report from the community that it was required by the popular 3rd party ESP32 and STM32 boards platforms:
Note that only the "prebuild" hooks are executed during the compilation database generation. "postbuild" hooks are not executed. Is there any way you could use a post-build hook for this command?
OK this is a problem. The buildnumber must be increment before the sketch builds. If I use a post build step then the result is that the complied binary is one build number behind.
I also use the ESP32. I will check the plattform.txt file but currently I have no idea to fix this problem with post build step.
I think the best way to provide for this use case would be the addition of a property that indicates whether it is a normal compilation or the compilation database generation. You could pass that property to your prebuild.exe tool via an argument and then the tool's code (or a wrapper script/command if the tool code can't be modified) could determine whether or not to increment the build number based on the value of the argument.
Something similar was done for identification of when the recipe.preproc.macros command is ran for library discovery:
My suggestion is that you submit a feature request to the Arduino CLI issue tracker.
Obviously that won't solve your problem immediately. It may be that the other forum members have some ideas about how to work around this issue. I think they might be better equipped to do so if you provide more information about how this build number system works. Is the tool writing the build number to a header file or something?
I looked to see whether any properties had identifying values when running a compilation with the --only-compilation-database flag vs without and didn't find anything.
The postbuild hook is the way I've gone and this seems to work. It seems to me this is only the difference between ++Build and Build++. One 'empty' build would put you on track again. Would that not be good enough?
I use this autoinc-semver but use the postbuild hook instead of the prebuild. Sure enough it will increment the build count after the build but in my view this is better. Autoinc-semver creates a version.h file in the project and this will now be updated to always to show the next build number. This seems more sensible.
I've been forced down this route because of the prebuild hook issue but the solution actually seems to be an improvement in my view.
Sure the postbuild is better than no build number. A workaround is to rename the define in NEXT_BUILDNUMBER or something else…..in combination with postbuild.