Go Down

Topic: Build process - add custom action during execution (e.g. fill build date, ... (Read 859 times) previous topic - next topic

alex_73

Hi!

I am searching a nice way to add custom steps to the build process of the Arduino IDE.
My actual intent for now is to change the code before it gets compiled to insert
  • compile/build date
  • some unique id - e.g. to be used for randomSeed

via some sed/awk/m4/bash here document/whatever...

Is there a way to add such a step? In a way that does not get overwritten on updates? Possibly dedicated to one project only?

Thanks a lot, Alex.

pert

The IDE does have a nice Pre and post build hooks feature that will likely be useful to you:
https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification#pre-and-post-build-hooks-since-ide-165

However, that doesn't get you around the problem of your changes to a standard hardware package being lost on an update. There is a feature where you can make your changes in a file named platform.local.txt instead of platform.txt. However, that file is located in the same folder as platform.txt so you would still need to have a copy of your platform.local.txt file stored somewhere safe and copy it back into the hardware package after every update.

The true solution to the update issue is to create your own custom hardware package. This is actually quite simple because you can reference resources from another hardware package. Your custom package could be as simple as a platform.txt and a boards.txt file. Unfortunately, the one resource you can't reference is bootloaders so if you want support for Tools > Burn Bootloader then you would need to place copies of the bootloader files in your package. Information on referencing resources here:
https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification#referencing-another-core-variant-or-tool

It sounds like an interesting project. Let me know if you have any questions.

alex_73

Thanks for the useful hints!

I played a bit the last days and analyzed the options I found.

The easiest and most limiting is to use predefined C preprocessor macros like __DATE__ or __TIME__. __TIME__ might be an option for a base to "randomly" seed the random number generator. (E.g. XOR the characters together or any other weird operation :-)

As for the hooks provided by the build process there is one major issue that I see right now.
Before even the first hook (recipe.hooks.sketch.prebuild) is called, the IDE already runs arduino-builder to
  • dump-prefs
  • detecting libraries used
  • generate function prototypes

This is too late to make any useful changes to the code in question or even add a file to include in the build - the arduino code is already copied to the build location in temp directory. So changes to the sources are not considered until the next build and missing files are reported already by the previous steps. (I tried to generate a header file with constants and include it in the main ino file.)

Also the arduino IDE does not detect file changes on disk on open files in the ide (although one can work around this with "Tools->Fix Encoding and Reload" menu item).

From my POV I miss a hook which is called before anything with the code happens, especially the copy to the build location to be able to inject some self generated magic.

As of referencing the hooks in platform.local.txt I'd say that it would be a great benefit if the build process would check for default scripts in the source directory (as only(?) c, cpp, h, s files are copied to temp) and call those when existing. So each project could have its own hooks implemented if required and not all projects of an architecture/board.

alex_73

For now I will stick with the following setup.

As I am a cygwin user, I'll use this environment to run a bash script doing some magic to the already copied and pre processed source files from the sketch.

Extend arduino-x.y.z/hardware/platform.txt to include paths to bash, shell script location (in my sketchbook folder) and finally the call of the shell script with required parameters:
Code: [Select]

local.cygwin.bash=c:\cygwin64\bin\bash
local.sketchbook.location=c:\Work\Arduino
local.hooks.location={local.sketchbook.location}\Arduino_IDE_Hooks

recipe.hooks.sketch.prebuild.100.pattern={local.cygwin.bash} {local.hooks.location}/hook.sketch.prebuild.sh --build.path "{build.path}" --build.project_name "{build.project_name}" -v

# Fail target for testing.
#recipe.hooks.sketch.prebuild.999.pattern=fail.999


This then will call hook.sketch.prebuild.sh.
As of now it is capable of replacing {{MY_VARIABLE}} with some generated content - here a random number from 0 to 32767 in two flavors:
On every occurrence a different one or the same on each occurrence (just for the sake of testing).

Comments and suggestions welcome.

Go Up