I need to install a library that my project depends on. To make my package more independent I moved the installation of it to cmake. I did it successfully with ExternalProject_Add with a setup similar to the one shown below.
include(ExternalProject)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/include)
ExternalProject_Add(
library-build
.
.
.
)
add_library(library STATIC IMPORTED GLOBAL)
add_dependencies(library library-build)
set_target_properties(
library
PROPERTIES
IMPORTED_LOCATION
${CMAKE_CURRENT_BINARY_DIR}/library-build/lib/library.so)
set_target_properties(
library
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${CMAKE_CURRENT_BINARY_DIR}/library-build/include)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/lib
DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/include
DESTINATION ${CMAKE_INSTALL_PREFIX})
add_library(${PROJECT_NAME} SHARED src/file.cpp)
target_link_libraries(${PROJECT_NAME} library)
The problem I’m encountering is build time, especially when built on arm64. It’s a bit faster when building for a second time but it can still take a few minutes - a bit annoying during development.
What I was thinking of doing is exporting the library installed with ExternalProject_Add
so it can be later found with find_package()
. Then installation step could be skipped:
find_library(LIBRARY_INSTALLED library)
if(NOT LIBRARY_INSTALLED)
include(ExternalProject)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/library-build/include)
ExternalProject_Add(
library-build
.
.
.
)
I’ve tried using install()
to export the library, but it can’t be done with IMPORTED
libraries. My question is whether it is possible to export this type of library (that needs to be linked to the .so
file) or create a target that can be exported so the library can be found using find_package
. Maybe there is another solution or workaround to achieve my goal?