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.

User avatar
dmarks_ls
Posts: 6
Joined: Fri Feb 10, 2023 4:56 pm

Re: Using CMake add_custom_command() to perform POST_BUILD action

Postby dmarks_ls » Mon May 06, 2024 10:48 pm

Just wanted to add to this thread, it's exactly what I needed. We have a source code library that we don't distribute directly to customers; instead we compile an archive and include that with the application source. For Eclipse-based toolchains, this is fairly simple to manage by making two projects, one produces a lib.a file and uses a post-build step to copy into the lib directory of the adjacent application project.

ESP-IDF is a bit more complicated, but I was able to figure it out by adapting the IDF v4 example (I'm using IDF v5.2). What I did was make a "hello world" example, and I create a "mylibrary" extra component in which I have all of our library source code, library headers, and configuration headers. When I build the project, it does make a functional "hello world" executable for ESP32, but I'm not interested in that. Instead, I grab the lib[mylibrary].a file from build/esp-idf/[mylibrary] (build/esp-idf is where all IDF and extra components get built into libraries) and copy it into a lib directory in the real ESP32 application project that sits alongside.

Here's my script, which sits at the tail end of my CMakeFiles.txt in the "hello world" library generator project:

Code: Select all

idf_build_get_property(build_dir BUILD_DIR)
set(lib_name "lslibs_source")
set(lib_file "lib${lib_name}.a")
set(lib_path "${build_dir}/esp-idf/${lib_name}")
set(out_path "${build_dir}/../../my_esp32_application_project/lslibs_include/lib")

add_custom_command(OUTPUT ${out_path}/${lib_file}
  DEPENDS "${lib_path}/${lib_file}"
  COMMAND "${CMAKE_COMMAND}" -E copy "${lib_path}/${lib_file}" "${out_path}/${lib_file}"
  COMMENT "Copying binary to ${out_path}/${lib_file}")

add_custom_target(copy_app_binary ALL DEPENDS ${out_path}/${lib_file})
add_dependencies(copy_app_binary gen_project_binary)
And there we go. It's executed as the very last step of the build. Hope this helps someone.

Dana M.

Who is online

Users browsing this forum: Bing [Bot] and 234 guests