NCS / Zephyr / CMake and sourcecode generators

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?

Parents
  • Hi Joost,

    Thank you for contacting DevZone at NordicSemi.

    Your query indeed is more related to the Cmake and less to the NCS, and is also clear from your manual running of scripts.

    I am not sure, but I have following thoughts:
    In Cmake, order of execution, and especially when we are adding / linking / targeting other files is also important.
    As your json.c uses these json_types.h and json_data.h files, maybe it (json.c) is being built earlier before these header files are available?

    Also, CMake documentation mentions that to run a full script, use configure_file() and then specify COMMAND to launch it. Is this relavent to you?

    If you feel suitable, you may send minimal project and the method to reproduce the error you were facing.

    Regards,
    Naeem

Reply
  • Hi Joost,

    Thank you for contacting DevZone at NordicSemi.

    Your query indeed is more related to the Cmake and less to the NCS, and is also clear from your manual running of scripts.

    I am not sure, but I have following thoughts:
    In Cmake, order of execution, and especially when we are adding / linking / targeting other files is also important.
    As your json.c uses these json_types.h and json_data.h files, maybe it (json.c) is being built earlier before these header files are available?

    Also, CMake documentation mentions that to run a full script, use configure_file() and then specify COMMAND to launch it. Is this relavent to you?

    If you feel suitable, you may send minimal project and the method to reproduce the error you were facing.

    Regards,
    Naeem

Children
No Data
Related