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.