generating text files to be embedded

nipil76
Posts: 3
Joined: Sat Aug 20, 2022 7:15 pm

generating text files to be embedded

Postby nipil76 » Thu Oct 06, 2022 3:19 pm

Hello,

I'm using ESP-IDF 4.4.2 in vscode without pio, using ESP-IDF extension, on windows 11
My whole project is public on github : https://github.com/nipil/open-fil-pilote-esp-idf
So far everything is fine and i can build my project in two steps using ESP-IDF terminal :

Code: Select all

# generates autosign.crt and autosign.key from info.txt
cd .\components\ofp-certificates\
python gen.py 
# build firmware and embed autosign.crt and autosign.key
cd -
idf.py.exe build 
The current cmake file in the base folder :

Code: Select all

cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS components/)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(open-fil-pilote-esp-idf)
The current cmake file in the components/ofp-certificate folder

Code: Select all

idf_component_register(
    EMBED_TXTFILES
        autosign.crt
        autosign.key
)
Now i want to automate the python gen.py command so that it is run implicitely during the build process, and eventually depend it on info.txt which is its input file, so that i only run it once.

But i cannot seem to do it, and it is driving me nuts...

Here is the my best attempt so far for the component's CMakeLists.txt :

Code: Select all

idf_component_register(
    EMBED_TXTFILES
        autosign.crt
        autosign.key
)

add_custom_command(
  WORKING_DIRECTORY ${COMPONENT_DIR}
  OUTPUT autosign.crt autosign.key
  COMMAND python gen.py
  DEPENDS info.txt
)

add_custom_target(
  autosign
  DEPENDS autosign.crt autosign.key
)

add_dependencies(
  ${COMPONENT_TARGET}
  autosign
)

set_property(
  DIRECTORY ${COMPONENT_DIR}
  APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES autosign.crt autosign.key
)
No error, except the build system does not take my modification into account :
- the autosign.crt autosign.key files are not generated, and thus cannot be embedded
- the autosign.crt autosign.key are not cleaned either when cleaning the project

What am i missing ?
Any hint will be greatly appreciated !

Thanks in advance
Nicolas

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: generating text files to be embedded

Postby ESP_igrr » Thu Oct 06, 2022 8:39 pm

Hi Nicolas,

The problem you are running into is related to the treatment of file names by add_custom_command CMake function.
According to the docs,
If an output name is a relative path it will be interpreted relative to the build tree directory corresponding to the current source directory.
So CMake is thinking that the command defined by your add_custom_command call will generate two files in the build directory: ${CMAKE_CURRENT_BINARY_DIR}/autosign.crt, ${CMAKE_CURRENT_BINARY_DIR}/autosign.key.

File names passed to idf_component_register are treated as relative to the current source directory. Since the files do not exist and there is no command to generate them, CMake produces this error.

To fix this, specify the absolute path to the generated files when calling add_custom_command:

Code: Select all

add_custom_command(
  WORKING_DIRECTORY ${COMPONENT_DIR}
  OUTPUT ${CMAKE_CURRENT_LIST_DIR}/autosign.crt ${CMAKE_CURRENT_LIST_DIR}/autosign.key
  COMMAND python gen.py
  DEPENDS info.txt
)
and it should work.

(However, you might consider keeping the generated files in the build directory, so that your source directory is always "clean". In that case, specify absolute path ${CMAKE_CURRENT_BINARY_DIR}/autosign.crt in EMBED_TXTFILES, instead.)

nipil76
Posts: 3
Joined: Sat Aug 20, 2022 7:15 pm

Re: generating text files to be embedded

Postby nipil76 » Sat Oct 08, 2022 7:36 am

That is .. .awesome. I didn't notice that line in the documentation about relative path (logically) using the current components' build directory as a base, that makes total sense.

I can confirm that both approach work for my project now :

1) Either outputing the generated files in the source component folder components/ofp-certificates (for developper convenience should he want to analyze it)with an additionnal property so they are removed when cleaning :

Code: Select all

idf_component_register(
    EMBED_TXTFILES
    "autosign.crt"
    "autosign.key"
)

add_custom_command(
    WORKING_DIRECTORY ${COMPONENT_DIR}
    OUTPUT ${COMPONENT_DIR}/autosign.crt ${COMPONENT_DIR}/autosign.key
    COMMAND python gen.py
    DEPENDS info.txt
)

add_custom_target(
    autosign
    DEPENDS autosign.crt autosign.key
)

add_dependencies(
    ${COMPONENT_TARGET} autosign
)

set_property(
    DIRECTORY ${COMPONENT_DIR}
    APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${COMPONENT_DIR}/autosign.crt ${COMPONENT_DIR}/autosign.key
)
2) or outputting them in the binary component folder build/esp-idf/ofp-certificate for cleanliness, this time with no additionnal property is needed for cleaning as the folder is cleaned automatically

Code: Select all

idf_component_register(
    EMBED_TXTFILES
    ${CMAKE_CURRENT_BINARY_DIR}/autosign.crt
    ${CMAKE_CURRENT_BINARY_DIR}/autosign.key
)

add_custom_command(
    WORKING_DIRECTORY ${COMPONENT_DIR}
    OUTPUT autosign.crt autosign.key
    COMMAND python gen.py --infile ${COMPONENT_DIR}/info.txt --outdir=${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS info.txt
)

add_custom_target(
    autosign
    DEPENDS autosign.crt autosign.key
)

add_dependencies(
    ${COMPONENT_TARGET} autosign
)
Thanks a bunch, this closes my case.
Have a nice day
Nicolas

Who is online

Users browsing this forum: MikeMyhre and 132 guests