I need to add a meson project (specifically jose) to my project where I am using cmake.
This post: CMake: How to build external projects and include their targets is trying to acheive a similar goal but now that time has pass the recommendation is to use FetchContent. However, that does seem to only apply to other cmake projects, not "foreign" projects. At least, I failed to see how to run meson in the examples.
So. I am trying to use ExternalProject_Add, which works perfectly (configure and eventually build jose). But conceptually there is an issue since I also need to add the jose library to target_link_libraries. However, the library is not yet built when cmake configures the whole project.
I think I understand that I need to have jose built during the global cmake configuration.
I try to use execute_process and ExternalProject_Add_Step but failed at making them to work properly. Here are the details
ExternalProject_Add(
jose
UPDATE_COMMAND ""
PATCH_COMMAND ""
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/jose
# Configure
CONFIGURE_HANDLED_BY_BUILD true
CONFIGURE_COMMAND meson setup ${CMAKE_CURRENT_SOURCE_DIR}/jose --prefix=${INSTALL_DIR} --pkg-config-path ${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/x64-linux/lib/pkgconfig
BUILD_COMMAND ninja
INSTALL_COMMAND ""
TEST_COMMAND ""
)
set(jose_BINARY_DIR "${BINARY_DIR}")
ExternalProject_Add_Step(jose build
COMMENT "Building jose project..."
COMMAND ${CMAKE_COMMAND} --build ${jose_BINARY_DIR}
RESULT_VARIABLE jose_build_result
OUTPUT_QUIET
DEPENDEES download update
WORKING_DIRECTORY ${jose_BINARY_DIR}
)
add_dependencies(${CMAKE_PROJECT_NAME} jose)
There are 2 problems:
- target_link_libraries is still trying to look for josé before it is being built
- if I remove josé from target_link_libraries (for testing) the ExternalProject_Add_Step fails with a message indicating that add_custom_command is trying to add a duplicate custom rule (in jose-build.rule).
My questions
- Am I on the right track with ExternalProject_Add_Step
- If so, how to make it executed before target_link_libraries is evaluated