Pre-flash and post-flash build steps [un-solved]

cnlohr
Posts: 65
Joined: Sat Dec 03, 2016 5:39 am

Pre-flash and post-flash build steps [un-solved]

Postby cnlohr » Tue Sep 06, 2022 6:05 am

Is there any way to add a pre- and post- flash build step?

For my system, I have to send a special command to the ESP to put it into bootloader mode, and another to get it to actually boot. I can't seem to find any documentation in the IDF for augmenting build steps other than the normal build one. Can anyone point me in a good direction?
Last edited by cnlohr on Sat Jan 21, 2023 12:04 am, edited 2 times in total.

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

Re: Pre-flash and post-flash build steps

Postby ESP_igrr » Tue Sep 06, 2022 8:50 pm

Hi cnlohr,
You can try CMake's add_custom_command with PRE_BUILD and POST_BUILD arguments.
These custom steps can be defined in your project CMakeLists.txt file.

add_custom_command(TARGET flash POST_BUILD
COMMAND your_command_goes_here
)

cnlohr
Posts: 65
Joined: Sat Dec 03, 2016 5:39 am

Re: Pre-flash and post-flash build steps

Postby cnlohr » Wed Sep 07, 2022 9:15 am

This seems to not work.

If I specify the following:

Code: Select all

cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(swadge-esp32)


add_custom_command(TARGET flash POST_BUILD
	COMMAND "echo $IDF_PATH POST_BUILD >> asdf.txt"
)

add_custom_command(TARGET flash PRE_BUILD
	COMMAND "echo $IDF_PATH PRE_BUILD >> asdf.txt"
)
And I perform the following, I get the output...

Code: Select all

idf.py -v -p com7 flash
Checking Python dependencies...
Python requirements from C:\Users\cnlohr\esp\esp-idf\requirements.txt are satisfied.
Executing action: flash
Running ninja in directory c:\projects\super-2023-swadge-fw\build
Executing "ninja -v flash"...
[0/1] C:\Users\cnlohr\.espressif\tools\cmake\3.20.3\bin\cmake.exe --regenerate-during-build -SC:\projects\Super-2023-Swadge-FW -BC:\projects\Super-2023-Swadge-FW\build
-- ccache will be used for faster recompilation
-- Building ESP-IDF components for target esp32s2
-- Project sdkconfig file C:/projects/Super-2023-Swadge-FW/sdkconfig
-- App "swadge-esp32" version: 3cc466a-dirty
-- Adding linker script C:/projects/Super-2023-Swadge-FW/build/esp-idf/esp_system/ld/memory.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_system/ld/esp32s2/sections.ld.in
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.libgcc.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.newlib-funcs.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.newlib-data.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.spiflash.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/esp_rom/esp32s2/ld/esp32s2.rom.newlib-time.ld
-- Adding linker script C:/Users/cnlohr/esp/esp-idf/components/soc/esp32s2/ld/esp32s2.peripherals.ld
-- IPV6 support is disabled so the asio component will not be built
-- IPV6 support is disabled so the coap component will not be built
-- Websocket transport is disabled so the esp_websocket_client component will not be built
-- Components: app_trace app_update asio bootloader bootloader_support bt cbor cmock coap console cxx driver efuse esp-tls esp32s2 esp_adc_cal esp_common esp_eth esp_event esp_gdbstub esp_hid esp_http_client esp_http_server esp_https_ota esp_https_server esp_hw_support esp_ipc esp_lcd esp_local_ctrl esp_netif esp_phy esp_pm esp_ringbuf esp_rom esp_serial_slave_link esp_system esp_timer esp_websocket_client esp_wifi espcoredump esptool_py expat fatfs freertos hal hdw-btn hdw-buzzer hdw-esp-now hdw-i2c hdw-led hdw-mic hdw-nvs hdw-oled hdw-qma6981 hdw-spiffs hdw-temperature hdw-tft hdw-touch heap idf_test ieee802154 jsmn json libsodium log lwip main mbedtls mdns mqtt newlib nghttp nvs_flash openssl openthread partition_table perfmon protobuf-c protocomm pthread sdmmc soc spi_flash spiffs tcp_transport tcpip_adapter tinyusb touch_element ulp unity usb vfs wear_levelling wifi_provisioning wpa_supplicant xtensa
-- Component paths: C:/Users/cnlohr/esp/esp-idf/components/app_trace C:/Users/cnlohr/esp/esp-idf/components/app_update C:/Users/cnlohr/esp/esp-idf/components/asio C:/Users/cnlohr/esp/esp-idf/components/bootloader C:/Users/cnlohr/esp/esp-idf/components/bootloader_support C:/Users/cnlohr/esp/esp-idf/components/bt C:/Users/cnlohr/esp/esp-idf/components/cbor C:/Users/cnlohr/esp/esp-idf/components/cmock C:/Users/cnlohr/esp/esp-idf/components/coap C:/Users/cnlohr/esp/esp-idf/components/console C:/Users/cnlohr/esp/esp-idf/components/cxx C:/Users/cnlohr/esp/esp-idf/components/driver C:/Users/cnlohr/esp/esp-idf/components/efuse C:/Users/cnlohr/esp/esp-idf/components/esp-tls C:/Users/cnlohr/esp/esp-idf/components/esp32s2 C:/Users/cnlohr/esp/esp-idf/components/esp_adc_cal C:/Users/cnlohr/esp/esp-idf/components/esp_common C:/Users/cnlohr/esp/esp-idf/components/esp_eth C:/Users/cnlohr/esp/esp-idf/components/esp_event C:/Users/cnlohr/esp/esp-idf/components/esp_gdbstub C:/Users/cnlohr/esp/esp-idf/components/esp_hid C:/Users/cnlohr/esp/esp-idf/components/esp_http_client C:/Users/cnlohr/esp/esp-idf/components/esp_http_server C:/Users/cnlohr/esp/esp-idf/components/esp_https_ota C:/Users/cnlohr/esp/esp-idf/components/esp_https_server C:/Users/cnlohr/esp/esp-idf/components/esp_hw_support C:/Users/cnlohr/esp/esp-idf/components/esp_ipc C:/Users/cnlohr/esp/esp-idf/components/esp_lcd C:/Users/cnlohr/esp/esp-idf/components/esp_local_ctrl C:/Users/cnlohr/esp/esp-idf/components/esp_netif C:/Users/cnlohr/esp/esp-idf/components/esp_phy C:/Users/cnlohr/esp/esp-idf/components/esp_pm C:/Users/cnlohr/esp/esp-idf/components/esp_ringbuf C:/Users/cnlohr/esp/esp-idf/components/esp_rom C:/Users/cnlohr/esp/esp-idf/components/esp_serial_slave_link C:/Users/cnlohr/esp/esp-idf/components/esp_system C:/Users/cnlohr/esp/esp-idf/components/esp_timer C:/Users/cnlohr/esp/esp-idf/components/esp_websocket_client C:/Users/cnlohr/esp/esp-idf/components/esp_wifi C:/Users/cnlohr/esp/esp-idf/components/espcoredump C:/Users/cnlohr/esp/esp-idf/components/esptool_py C:/Users/cnlohr/esp/esp-idf/components/expat C:/Users/cnlohr/esp/esp-idf/components/fatfs C:/Users/cnlohr/esp/esp-idf/components/freertos C:/Users/cnlohr/esp/esp-idf/components/hal C:/projects/Super-2023-Swadge-FW/components/hdw-btn C:/projects/Super-2023-Swadge-FW/components/hdw-buzzer C:/projects/Super-2023-Swadge-FW/components/hdw-esp-now C:/projects/Super-2023-Swadge-FW/components/hdw-i2c C:/projects/Super-2023-Swadge-FW/components/hdw-led C:/projects/Super-2023-Swadge-FW/components/hdw-mic C:/projects/Super-2023-Swadge-FW/components/hdw-nvs C:/projects/Super-2023-Swadge-FW/components/hdw-oled C:/projects/Super-2023-Swadge-FW/components/hdw-qma6981 C:/projects/Super-2023-Swadge-FW/components/hdw-spiffs C:/projects/Super-2023-Swadge-FW/components/hdw-temperature C:/projects/Super-2023-Swadge-FW/components/hdw-tft C:/projects/Super-2023-Swadge-FW/components/hdw-touch C:/Users/cnlohr/esp/esp-idf/components/heap C:/Users/cnlohr/esp/esp-idf/components/idf_test C:/Users/cnlohr/esp/esp-idf/components/ieee802154 C:/Users/cnlohr/esp/esp-idf/components/jsmn C:/Users/cnlohr/esp/esp-idf/components/json C:/Users/cnlohr/esp/esp-idf/components/libsodium C:/Users/cnlohr/esp/esp-idf/components/log C:/Users/cnlohr/esp/esp-idf/components/lwip C:/projects/Super-2023-Swadge-FW/main C:/Users/cnlohr/esp/esp-idf/components/mbedtls C:/Users/cnlohr/esp/esp-idf/components/mdns C:/Users/cnlohr/esp/esp-idf/components/mqtt C:/Users/cnlohr/esp/esp-idf/components/newlib C:/Users/cnlohr/esp/esp-idf/components/nghttp C:/Users/cnlohr/esp/esp-idf/components/nvs_flash C:/Users/cnlohr/esp/esp-idf/components/openssl C:/Users/cnlohr/esp/esp-idf/components/openthread C:/Users/cnlohr/esp/esp-idf/components/partition_table C:/Users/cnlohr/esp/esp-idf/components/perfmon C:/Users/cnlohr/esp/esp-idf/components/protobuf-c C:/Users/cnlohr/esp/esp-idf/components/protocomm C:/Users/cnlohr/esp/esp-idf/components/pthread C:/Users/cnlohr/esp/esp-idf/components/sdmmc C:/Users/cnlohr/esp/esp-idf/components/soc C:/Users/cnlohr/esp/esp-idf/components/spi_flash C:/Users/cnlohr/esp/esp-idf/components/spiffs C:/Users/cnlohr/esp/esp-idf/components/tcp_transport C:/Users/cnlohr/esp/esp-idf/components/tcpip_adapter C:/projects/Super-2023-Swadge-FW/components/tinyusb C:/Users/cnlohr/esp/esp-idf/components/touch_element C:/Users/cnlohr/esp/esp-idf/components/ulp C:/Users/cnlohr/esp/esp-idf/components/unity C:/Users/cnlohr/esp/esp-idf/components/usb C:/Users/cnlohr/esp/esp-idf/components/vfs C:/Users/cnlohr/esp/esp-idf/components/wear_levelling C:/Users/cnlohr/esp/esp-idf/components/wifi_provisioning C:/projects/Super-2023-Swadge-FW/components/wpa_supplicant C:/Users/cnlohr/esp/esp-idf/components/xtensa
-- Configuring done
-- Generating done
-- Build files have been written to: C:/projects/Super-2023-Swadge-FW/build
ninja: error: unknown target 'flash'
ninja failed with exit code 1
Any recommendations for somewhere to peek next? I've spent about 25 minute son this already.

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

Re: Pre-flash and post-flash build steps

Postby ESP_igrr » Thu Sep 08, 2022 3:05 pm

Sorry for not trying myself the approach I have suggested! After reading the docs I see that the PRE_BUILD / PRE_LINK event will not work for "flash" target, since it is defined with add_custom_target.

Instead, to run some command before the desired target, we can use a dependency.

Here's what seems to work:

Code: Select all

add_custom_target(before_flash
	COMMAND ${CMAKE_COMMAND} -E echo "before_flash"
)
add_dependencies(flash before_flash)
add_custom_command(TARGET flash POST_BUILD
	COMMAND ${CMAKE_COMMAND} -E echo "after_flash"
)
I've tried adding this to the CMakeLists.txt file of examples/get-started/hello_world, and it seems to work correctly. Please give it a try!

cnlohr
Posts: 65
Joined: Sat Dec 03, 2016 5:39 am

Re: Pre-flash and post-flash build steps

Postby cnlohr » Fri Sep 09, 2022 5:36 am

Thank you! This worked wonders.

cnlohr
Posts: 65
Joined: Sat Dec 03, 2016 5:39 am

Re: Pre-flash and post-flash build steps [un-solved]

Postby cnlohr » Sat Jan 21, 2023 12:00 am

I am having an issue with this again. Now when I include the above code, I get this error:

Code: Select all

CMake Error at CMakeLists.txt:20 (add_dependencies):
  Cannot add target-level dependencies to non-existent target "flash".

  The add_dependencies works for top-level logical targets created by the
  add_executable, add_library, or add_custom_target commands.  If you want to
  add file-level dependencies see the DEPENDS option of the add_custom_target
  and add_custom_command commands.


CMake Error at CMakeLists.txt:21 (add_custom_command):
  No TARGET 'flash' has been created in this directory.
For the

Code: Select all

add_dependencies( flash before_flash )
line.

Any ideas what I should do?

Who is online

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