Help to run cmake before embed_file

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Help to run cmake before embed_file

Postby urbanze » Mon Sep 16, 2024 3:06 pm

Hello guys, I have one http component with a lot of web files (like router page) and I need to compress every file inside /web folder, drag to /web/zip and after this embed to flash with CMake of this component. I already tried many ways to do this but every script tested only execute custom_command after embed_file and not before. What I can do for fix this order of execution? First compress and after embed.

Below are my 2 latest scripts and both compress files only after embed_file or target_add_binary. I need to invert this order! Both examples are shorter than real, embedding only one file to improve reading.

Code: Select all

idf_component_register(SRCS "http.cpp"
                    INCLUDE_DIRS "."
                    REQUIRES esp_http_server
                    EMBED_FILES web/zip/index.html.gz)

message(STATUS "### Compressing WEB files over custom cmake")
set (WEB_DIR ${COMPONENT_DIR}/web)
 
add_custom_target(
    process_http_files
    ALL
    COMMAND ${CMAKE_COMMAND} -E echo "---- Compressing [FW_HTTP]"
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip
    COMMAND gzip -krf -9 ${WEB_DIR}/
    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
    COMMAND ${CMAKE_COMMAND} -E echo "---- Compressed [FW_HTTP] successfully"
)

add_dependencies(${COMPONENT_LIB} process_http_files)

Code: Select all

idf_component_register(
    SRCS "http.cpp"
    INCLUDE_DIRS "."
    REQUIRES esp_http_server
)


externalproject_add(execute_zip
    PREFIX ${COMPONENT_DIR}
    SOURCE_DIR ${COMPONENT_DIR}
    CONFIGURE_COMMAND ""
    BUILD_IN_SOURCE 1
    BUILD_COMMAND ${CMAKE_COMMAND} -E env bash ${COMPONENT_DIR}/zip_all.sh
    INSTALL_COMMAND ""
)

add_dependencies(${COMPONENT_LIB} execute_zip)
target_add_binary_data(${COMPONENT_LIB} "${COMPONENT_DIR}/web/zip/index.html.gz" BINARY DEPENDS execute_zip)

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Help to run cmake before embed_file

Postby MicroController » Wed Sep 18, 2024 8:21 am


User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Help to run cmake before embed_file

Postby urbanze » Fri Sep 20, 2024 2:57 pm

MicroController wrote:
Wed Sep 18, 2024 8:21 am
Very related: https://esp32.com/viewtopic.php?f=13&t= ... 10#p114638
Still in the same order. Embedded -> run custom command.

Code: Select all

idf_component_register(SRCS "http.cpp"
                    INCLUDE_DIRS "."
                    REQUIRES esp_http_server)
 
 
set (WEB_DIR ${COMPONENT_DIR}/web)
 
 
add_custom_command(
    OUTPUT ${WEB_DIR}/zip/index.html.gz
    OUTPUT ${WEB_DIR}/zip/style.css.gz
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip
    COMMAND gzip -krf -9 ${WEB_DIR}/
    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
)
 
add_custom_target(
    process_http_files
    ALL
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip
    COMMAND gzip -krf -9 ${WEB_DIR}/
    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
)
 
add_dependencies(${COMPONENT_LIB} process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/index.html.gz" BINARY DEPENDS "${WEB_DIR}/zip/index.html.gz")
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/style.css.gz" BINARY DEPENDS "${WEB_DIR}/zip/style.css.gz")


I noticed that custom_target run on every compile and custom_command runs firstly on first compile (from scratch). If I remove any one, didnt work well. Buts even in this way, the order of executions still wrong. Anyone have more ideas?

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: Help to run cmake before embed_file

Postby chegewara » Fri Sep 20, 2024 11:30 pm

The DEPENDS argument to target_add_binary_data ensures that the target executes first.
add_custom_target(my_process COMMAND ...)
target_add_binary_data(my_target "my_embed_file.bin" BINARY DEPENDS my_process)
Is not that you should setup it like this?

Code: Select all

add_custom_target(
    process_http_files
    ALL
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip
    COMMAND gzip -krf -9 ${WEB_DIR}/
    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
)
 
add_dependencies(${COMPONENT_LIB} process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/index.html.gz" BINARY DEPENDS process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/style.css.gz" BINARY DEPENDS process_http_files)
The last argument is process name, not the file name (just guessing)

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Help to run cmake before embed_file

Postby urbanze » Mon Sep 23, 2024 3:34 pm

chegewara wrote:
Fri Sep 20, 2024 11:30 pm
The DEPENDS argument to target_add_binary_data ensures that the target executes first.
add_custom_target(my_process COMMAND ...)
target_add_binary_data(my_target "my_embed_file.bin" BINARY DEPENDS my_process)
Is not that you should setup it like this?

Code: Select all

add_custom_target(
    process_http_files
    ALL
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip
    COMMAND gzip -krf -9 ${WEB_DIR}/
    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
)
 
add_dependencies(${COMPONENT_LIB} process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/index.html.gz" BINARY DEPENDS process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/style.css.gz" BINARY DEPENDS process_http_files)
The last argument is process name, not the file name (just guessing)

Code: Select all

idf_component_register(SRCS "http.cpp"
                    INCLUDE_DIRS "."
                    REQUIRES esp_http_server)
 
 
set (WEB_DIR ${COMPONENT_DIR}/web)
 
add_custom_target(
    process_http_files
    ALL
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip
    COMMAND gzip -krf -9 ${WEB_DIR}/
    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
)
 
add_dependencies(${COMPONENT_LIB} process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/index.html.gz" BINARY DEPENDS process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/style.css.gz" BINARY DEPENDS process_http_files)
Still not working to compile from scrath (after fullclean). Error:

Code: Select all

CMake Error in components/fw_http/CMakeLists.txt:
  Cannot find source file:

    /home/ze/xx/xx/components/fw_http/web/zip/index.html.gz

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc


CMake Error in components/fw_http/CMakeLists.txt:
  Cannot find source file:

    /home/ze/xx/xx/components/fw_http/web/zip/style.css.gz

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.
Compiler/cmake still trying to embed before compressing (executing custom_target). Some new idea?

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: Help to run cmake before embed_file

Postby chegewara » Mon Sep 23, 2024 5:19 pm

This may be not very elegant solution, and actually im not sure if it works, but it is building at least

Code: Select all

idf_component_register(SRCS "http.cpp"
                    INCLUDE_DIRS "."
                    REQUIRES esp_http_server)
 
 
set (WEB_DIR ${COMPONENT_DIR}/web)

add_custom_command(
    OUTPUT "${WEB_DIR}/zip/index.html.gz"
    COMMAND COMMAND gzip -krf -9 ${WEB_DIR}/index.html
    VERBATIM
)

add_custom_command(
    OUTPUT "${WEB_DIR}/zip/style.css.gz"
    COMMAND COMMAND gzip -krf -9 ${WEB_DIR}/style.css
    VERBATIM
)

add_custom_target(
    process_http_files
    ALL
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip

    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
    DEPENDS "${WEB_DIR}/zip/index.html.gz"
    DEPENDS "${WEB_DIR}/zip/style.css.gz"
    VERBATIM
    USES_TERMINAL
)

add_dependencies(${COMPONENT_LIB} process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/index.html.gz" BINARY DEPENDS process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/style.css.gz" BINARY DEPENDS process_http_files)



User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Help to run cmake before embed_file

Postby urbanze » Tue Sep 24, 2024 6:43 pm

chegewara wrote:
Mon Sep 23, 2024 5:19 pm
This may be not very elegant solution, and actually im not sure if it works, but it is building at least

Code: Select all

idf_component_register(SRCS "http.cpp"
                    INCLUDE_DIRS "."
                    REQUIRES esp_http_server)
 
 
set (WEB_DIR ${COMPONENT_DIR}/web)

add_custom_command(
    OUTPUT "${WEB_DIR}/zip/index.html.gz"
    COMMAND COMMAND gzip -krf -9 ${WEB_DIR}/index.html
    VERBATIM
)

add_custom_command(
    OUTPUT "${WEB_DIR}/zip/style.css.gz"
    COMMAND COMMAND gzip -krf -9 ${WEB_DIR}/style.css
    VERBATIM
)

add_custom_target(
    process_http_files
    ALL
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${WEB_DIR}/zip
    COMMAND ${CMAKE_COMMAND} -E make_directory ${WEB_DIR}/zip

    COMMAND find ${WEB_DIR} -maxdepth 1 -name "*.gz" -exec mv -t ${WEB_DIR}/zip {} +
    DEPENDS "${WEB_DIR}/zip/index.html.gz"
    DEPENDS "${WEB_DIR}/zip/style.css.gz"
    VERBATIM
    USES_TERMINAL
)

add_dependencies(${COMPONENT_LIB} process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/index.html.gz" BINARY DEPENDS process_http_files)
target_add_binary_data(${COMPONENT_LIB} "${WEB_DIR}/zip/style.css.gz" BINARY DEPENDS process_http_files)


Thanks, but other tests made here already build successfully (from scrath or recompile from some code change), however none worked correctly, I always need to compile 2 times to see changes on webpage, because cmake embed the existing file to .bin and after this, generate new one with new changes.

Someone already done this? I really need this to improve coding speed on my team.

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Help to run cmake before embed_file

Postby MicroController » Wed Sep 25, 2024 7:18 am

Try adding a dependency to the "source"/input files to add_custom_command ("DEPENDS ..."). This should cause the command to be re-run when a "source" file is changed.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Help to run cmake before embed_file

Postby urbanze » Fri Sep 27, 2024 1:02 pm

MicroController wrote:
Wed Sep 25, 2024 7:18 am
Try adding a dependency to the "source"/input files to add_custom_command ("DEPENDS ..."). This should cause the command to be re-run when a "source" file is changed.
Order still wrong :oops: :(

Someone knows how this can be fixed??


Who is online

Users browsing this forum: No registered users and 83 guests