Arduino IDE 2.x bug recipe.hook

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}

Hi @fl0mll. What happens if you try running a simple command in the hook. For example:

recipe.hooks.sketch.prebuild.0.pattern=cmd /c "echo hello"

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.

Is it possible to see the output in the console or something? On my tool I redirect the output to a file.

Yes, but only if you have the verbose compilation output enabled:

  1. Select File > Preferences from the Arduino IDE menus.
  2. Check the box next to "Show verbose output during: ☐ compilation".
  3. Click the OK button.

Here is how it looks from my experiment:

sing board 'foono' from platform in folder: C:\Users\per\Documents\Arduino\hardware\InoFooPlatform\avr
Using core 'foo' from platform in folder: C:\Users\per\Documents\Arduino\hardware\InoFooPlatform\avr
Detecting libraries used...
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_FOONO -DARDUINO_ARCH_AVR "-IC:\\Users\\per\\Documents\\Arduino\\hardware\\InoFooPlatform\\avr\\cores\\foo" "-IC:\\Users\\per\\Documents\\Arduino\\hardware\\InoFooPlatform\\avr\\variants\\foo" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\sketch\\sketch_sep21a.ino.cpp" -o nul
Using cached library dependencies for file: C:\Users\per\AppData\Local\Temp\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\sketch\foo.cpp
Generating function prototypes...
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_FOONO -DARDUINO_ARCH_AVR "-IC:\\Users\\per\\Documents\\Arduino\\hardware\\InoFooPlatform\\avr\\cores\\foo" "-IC:\\Users\\per\\Documents\\Arduino\\hardware\\InoFooPlatform\\avr\\variants\\foo" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\sketch\\sketch_sep21a.ino.cpp" -o "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\preproc\\ctags_target_for_gcc_minus_e.cpp"
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\builtin\\tools\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
cmd /c "echo hello"
hello
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_FOONO -DARDUINO_ARCH_AVR "-IC:\\Users\\per\\Documents\\Arduino\\hardware\\InoFooPlatform\\avr\\cores\\foo" "-IC:\\Users\\per\\Documents\\Arduino\\hardware\\InoFooPlatform\\avr\\variants\\foo" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\sketch\\sketch_sep21a.ino.cpp" -o "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\sketch\\sketch_sep21a.ino.cpp.o"
Using previously compiled file: C:\Users\per\AppData\Local\Temp\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\sketch\foo.cpp.o
Compiling libraries...
Compiling core...
Using precompiled core: C:\Users\per\AppData\Local\Temp\arduino-core-cache\core_InoFooPlatform_avr_foono_04c676c6685cb7766ffc96039427e4dd.a
Linking everything together...
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6/sketch_sep21a.ino.elf" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\sketch\\foo.cpp.o" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6\\sketch\\sketch_sep21a.ino.cpp.o" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6/..\\arduino-core-cache\\core_InoFooPlatform_avr_foono_04c676c6685cb7766ffc96039427e4dd.a" "-LC:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6" -lm
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6/sketch_sep21a.ino.elf" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6/sketch_sep21a.ino.eep"
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -R .eeprom "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6/sketch_sep21a.ino.elf" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6/sketch_sep21a.ino.hex"

"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-size" -A "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5B885C69C85D2CEE337505ED4E6DBFE6/sketch_sep21a.ino.elf"
Sketch uses 138 bytes (0%) of program storage space. Maximum is 32256 bytes.
Global variables use 0 bytes (0%) of dynamic memory, leaving 2048 bytes for local variables. Maximum is 2048 bytes.

The rest of your output will look different, but you can see that the command ran only once in my output:

cmd /c "echo hello"
hello

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.

grafik

No click on the build button is required. The indexing process runs infinite....

Is it possible that the "Building sketch" and indexing process retrigger the prebuild steps?

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.

https://github.com/arduino/arduino-cli/issues/new/choose

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 think the property idea is a good solution. Is it possible to use a other property as a workaround for now? I find no documentation about this.

I will submit a feature request for this now and link it here.

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.

General documentation is available here:

https://arduino.github.io/arduino-cli/latest/platform-specification/

I took a look on the documentation but I have no idea to build a workaround for now. I created a feature request and will wait for a response now.

Same here. As I mentioned in my previous reply, the other forum members might have an idea if you provide more details about how your system works.

Thanks. I saw that:

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?

1 Like

I do not know how this should work with postbuild. After the sketch build the binary is already finished.

If I understand right and I think I do.

1st the prebuild
2nd the build (all libs and then the sketch)
3rd then postbuild - everything what you want to do with the binary files or something.

My prebuild.exe increment a number in buildnumber.h file and this must be compiled into the sketch.

Do you use a other solution?

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.