This question is all about massaging CMake I guess, but I cannot get any clear answers from the CMake documentation. Here is my problem:
I have a NCS based project that features a JSON interface, driven by decorated fields from a structure. I wrote a script (using bash, grep, sed and awk) that takes information from the structure definition for the decorated fields, does some wizardry and generates 2 include files that in turn generate macro's that can be used to generate code to make JSON. I can manually run the scripts and generate the definitions, but I want the build system to check their dependencies and run the script automatically. And there, everything falls apart.
TL;DR? In short:
- the input of the generator is in "config.h"
- there is a shellscript named "generator.sh" that generates "json_types.h" and "json_data.h" from said "config.h"
- there is a sourcefile named "json.c" that includes these generated "json_types.h" and "json_data.h" files
In the CMakeLists.txt of the json module, I tried:
add_custom_command( OUTPUT json_data.h json_types.h COMMAND bash generate.sh config.h DEPENDS generate.sh config.h )
But the tools still complain about there being a dependency from json.c on the non-existent "json_data.h" and "json_types.h". It does NOT run the custom command.
In a desperate attempt, I forced the script to run through an add_custom_target with the magical ALL keyword. This always runs the generator, even if the config.h file is not changed. Not good because "west flash" will now always generate and rebuild, even when previously I did a "west build".
How do I define the dependencies correctly? How do I make it run only when needed because the generated files either do not exist or are outdated?