Using CMake add_custom_command() to perform POST_BUILD action

william.ferguson.au
Posts: 107
Joined: Wed Jan 02, 2019 8:55 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby william.ferguson.au » Wed Jul 24, 2019 12:35 pm

Build directory deleted and/or renamed.

Think I've found it.
I'm using arduino-esp32 (1.0.2). It lists:

Code: Select all

set(COMPONENT_REQUIRES spi_flash mbedtls mdns ethernet)

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby ESP_Angus » Wed Jul 24, 2019 12:49 pm

william.ferguson.au wrote:
Wed Jul 24, 2019 12:35 pm
Build directory deleted and/or renamed.

Think I've found it.
I'm using arduino-esp32 (1.0.2). It lists:

Code: Select all

set(COMPONENT_REQUIRES spi_flash mbedtls mdns ethernet)
Thanks, this is it. Will see about updating Arduino in master (still requires Ethernet), and also about whether this error message can give some clues about which component(s) require the missing one.

You should be able to go back to IDF v3.3 release branch if that's what you were using before, and we'll find a fix for the POST_BUILD step that works in v3.3.

william.ferguson.au
Posts: 107
Joined: Wed Jan 02, 2019 8:55 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby william.ferguson.au » Wed Jul 24, 2019 12:52 pm

Brilliant - Thanks Angus.

william.ferguson.au
Posts: 107
Joined: Wed Jan 02, 2019 8:55 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby william.ferguson.au » Thu Aug 01, 2019 7:27 pm

Hi Angus, any progress on how to get POST_BUILD to work for 3.3?

William

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby ESP_Angus » Fri Aug 02, 2019 5:12 am

Hi William,

Sorry, I looked into this and then forgot to reply on the post.

For IDF v3.3, you can add something to your project CMakeLists.txt like this:

Code: Select all

get_filename_component(idf_project_name ${IDF_PROJECT_EXECUTABLE} NAME_WE)
set(app_bin "${CMAKE_BINARY_DIR}/${idf_project_name}.bin")

add_custom_command(OUTPUT /tmp/app.bin
  DEPENDS "${app_bin}"
  COMMAND "${CMAKE_COMMAND}" -E copy "${app_bin}" /tmp/app.bin
  COMMENT "Copying ${app_bin} to /tmp/app.bin")

add_custom_target(copy_app_binary ALL DEPENDS /tmp/app.bin)
add_dependencies(copy_app_binary app)
EDIT: Fixed code to copy .bin file not ELF file.

This will trigger for the default "all" target (ie cmake --build, or "idf.py build"). If you pass CMake a specific list of targets then copy_app_binary needs to be one of them.

For IDF V4.0, the process is a little bit different and more fiddly because we don't have an IDF build property for the app .bin file path. But this works:

Code: Select all

idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
set(app_bin "${build_dir}/${elf_name}.bin")

add_custom_command(OUTPUT /tmp/app.bin
  DEPENDS "${build_dir}/.bin_timestamp"
  COMMAND "${CMAKE_COMMAND}" -E copy "${app_bin}" /tmp/app.bin
  COMMENT "Copying binary to /tmp/app.bin")

add_custom_target(copy_app_binary ALL DEPENDS /tmp/app.bin)
add_dependencies(copy_app_binary gen_project_binary)
BTW, if you need to support both ways in one project then you can do something like

Code: Select all

if(IDF_VERSION_MAJOR EQUAL 4)
   # V4 logic
else()
   # V3.x logic
endif()

william.ferguson.au
Posts: 107
Joined: Wed Jan 02, 2019 8:55 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby william.ferguson.au » Fri Aug 02, 2019 9:33 pm

Thanks Angus, I can get the command to execute, but I still can't get the correct build output file.

${IDF_PROJECT_EXECUTABLE} resolves to "my-project.elf" but I need "my-project.bin"
Is there another variable that I can/should use?

Where can I find a description/definition of variables like IDF_PROJECT_EXECUTABLE?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby ESP_Angus » Tue Aug 06, 2019 1:20 am

william.ferguson.au wrote:
Fri Aug 02, 2019 9:33 pm
Thanks Angus, I can get the command to execute, but I still can't get the correct build output file.

${IDF_PROJECT_EXECUTABLE} resolves to "my-project.elf" but I need "my-project.bin"
Is there another variable that I can/should use?
Oops, sorry. Updated post to use the .bin file instead.
william.ferguson.au wrote:
Fri Aug 02, 2019 9:33 pm
Where can I find a description/definition of variables like IDF_PROJECT_EXECUTABLE?
It's mentioned here, but in the context of integrating with an existing CMake build system:
https://docs.espressif.com/projects/esp ... e-projects

william.ferguson.au
Posts: 107
Joined: Wed Jan 02, 2019 8:55 am

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby william.ferguson.au » Wed Aug 07, 2019 9:33 am

Thanks Angus.

NB I couldn't get the COMMENT to appear so I ended up adding an extra COMMAND to echo the comment I wanted.

Code: Select all

add_custom_command(
        OUTPUT ${target_bin}
        DEPENDS "${app_bin}"
        COMMAND "${CMAKE_COMMAND}" -E copy "${app_bin}" "${target_bin}"
#       NB I couldn't get comment to work, so I used an extra COMMAND to echo the value
        COMMAND ${CMAKE_COMMAND} -E echo "Copying ${app_bin} to ${target_bin}"
)

meowsqueak
Posts: 151
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby meowsqueak » Sat Nov 23, 2019 4:09 am

ESP_Angus wrote:
Tue Aug 06, 2019 1:20 am
It's mentioned here, but in the context of integrating with an existing CMake build system:
https://docs.espressif.com/projects/esp ... e-projects
Just a note - that link is currently dead (404), yet linked from the current stable docs.

EDIT: ok, that's weird - it returns 404 the first or second time I click the link, but then loads correctly after that. The webserver is doing something strange... I've seen it happen just now with links from the Getting Started page too. I click a subpage, get a 404, then hit refresh and the proper page then loads successfully.

Who is online

Users browsing this forum: No registered users and 122 guests