Create tar archive automatically in build process

ghost07
Posts: 36
Joined: Mon Oct 03, 2022 11:47 am

Re: Create tar archive automatically in build process

Postby ghost07 » Fri May 26, 2023 5:08 pm

I've noticed the tar file is actually generated every build.

However "data_file_embed_asm.cmake" script doesn't run every build. That script converts the file into C source file with byte array of that file, and is called by custom command in target_add_binary_data() function located in {IDF_PATH}/tools/cmake/utilities.cmake.

I did a simple test - increment a number in particular file, run build, then check generated tar file in component's directory and generated embedded.tar.S.obj file in build directory. In generated tar file was the up-to-date number (e.g. 5), while in generated .obj file (checked by "hexdump -C -n 2K") was the number from previous build iteration (e.g. 4).

I also added message() to "data_file_embed_asm.cmake" script, and I can see this message every other build. I mean: first build yes, second nope, third yes, fourth no, and so on... Even if I don't modify any file. Maybe some cache has to be cleared?

ghost07
Posts: 36
Joined: Mon Oct 03, 2022 11:47 am

Re: Create tar archive automatically in build process

Postby ghost07 » Mon May 29, 2023 9:09 am

It seems that ninja (build) is the one to blame.

I've manually run cmake with -G "Unix Makefiles" to generate Makefile instead of ninja.build, and with "make" command it generates .S file every time (not every other time like ninja used to do).

Can I somehow switch to Unix Makefiles generator for the project, so I can use VS Code "build" button? Right now that button attempts to execute ninja (which fails, because I ran cmake to generate Makefiles)

ghost07
Posts: 36
Joined: Mon Oct 03, 2022 11:47 am

Re: Create tar archive automatically in build process

Postby ghost07 » Mon May 29, 2023 3:10 pm

Alright, I think I've solved it.

It really had to do with paths. After using absolute path in OUTPUT and DEPENDS, it now generates every time even with ninja build, and the file doesn't need to initially exist, so I can git-ignore generated file and it will still compile when someone else clones the repository.
I thought WORKING_DIRECTORY applies on OUTPUT too, apparently not.

Final CMakeLists.txt:

Code: Select all

idf_component_register(
    SRCS "embedded_vfs.c" "microtar.c"
    INCLUDE_DIRS "include"
)

add_custom_command(
    OUTPUT 
        non-existing.txt # force to execute on every build (https://gitlab.kitware.com/cmake/cmake/-/issues/17071)
        "${COMPONENT_DIR}/embedded.tar"
    WORKING_DIRECTORY ${COMPONENT_DIR}
    COMMAND echo "Creating tar archive..."
    COMMAND tar -cf embedded.tar -C files . --owner dev --group dev --transform "s|^\./||"
    VERBATIM
)

target_add_binary_data(${COMPONENT_LIB} "embedded.tar" BINARY DEPENDS "${COMPONENT_DIR}/embedded.tar"

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

Re: Create tar archive automatically in build process

Postby MicroController » Mon May 29, 2023 7:26 pm

Good to hear you got it working! And thanks for sharing your solution :)

Who is online

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