Export third part library built with ExternalProject_Add

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?

It’s probably better for that ExternalProject code to just always be found using find_package and to have it provide its own -config.cmake package file. You can then find_dependency that package to get its targets.

Note that add_library and ExternalProject_add in the same project are like oil-and-water as CMake just doesn’t have insight into the EP build (as you can see from having to just “guess” LOCATION bits). It might work better to have a standalone “superbuild” that builds your dependency and your project using ExternalProject_add.

The problem is that this third-part library doesn’t provide -config.cmake file. That’s why I’m creating target and link it to .so file. I was trying to install that target but it can’t be done with IMPORTED libraries.

Later I’d like to have superbuild but for now I’m trying to figure out how to export and find that library.

I would encourage you to work with upstream to do so. If they’re not willing, a FindDep.cmake module is probably best.

Your comment set me on the right path. I figured out that the library that I’m installing is actually build with pkg-config and I can use PkgConfig and pkg_check_modules to find it.
Thanks for help :grin: