cmake: export targets

The build system creates two exported targets:
- The executable FluidSynth::fluidsynth
- The library FluidSynth::libfluidsynth

A downstream project using CMake can find and link the library target
directly with cmake (without needing pkg-config) this way:

~~~
project(sample LANGUAGES C)
find_package ( FluidSynth )
if (FluidSynth_FOUND)
  add_executable( sample sample.c )
  target_link_libraries ( sample PRIVATE FluidSynth::libfluidsynth )
endif ()
~~~

After installing fluidsynth in a prefix like "$HOME/Fluidsynth3":
cmake -DCNAKE_PREFIX_PATH="$HOME/Fluidsynth3/;..."

Instead installing, the build directory can be used directly, for
instance:

cmake -DFluidSynth_DIR="$HOME/fluidsynth-2.2.2/build/" ...
This commit is contained in:
Pedro López-Cabanillas 2021-07-25 21:27:48 +02:00
parent ff6b4db503
commit cf5fbac9af
3 changed files with 52 additions and 0 deletions

View File

@ -856,6 +856,29 @@ configure_file ( fluidsynth.pc.in
install ( FILES ${CMAKE_BINARY_DIR}/fluidsynth.pc
DESTINATION ${LIB_INSTALL_DIR}/pkgconfig )
# Exported targets for cmake: find_package(FluidSynth)
# when installed, use CMAKE_PREFIX_PATH=fluidsynth-prefix;...
# to use the build directory directly set the FluidSynth_DIR variable instead.
# targets in the build directory
export(EXPORT FluidSynthTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/FluidSynthTargets.cmake"
NAMESPACE FluidSynth::
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
FluidSynthConfigVersion.cmake
VERSION ${LIB_VERSION_INFO}
COMPATIBILITY SameMajorVersion
)
configure_file(FluidSynthConfig.cmake.in FluidSynthConfig.cmake @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/FluidSynthConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/FluidSynthConfigVersion.cmake"
DESTINATION ${LIB_INSTALL_DIR}/cmake/fluidsynth
)
# Extra targets for Unix build environments
if ( UNIX )
# RPM spec

11
FluidSynthConfig.cmake.in Normal file
View File

@ -0,0 +1,11 @@
# for the find_dependency() macro:
# include(CMakeFindDependencyMacro)
# it has the same syntax as find_package:
# find_dependency(MYDEP REQUIRED)
# define variables for configuration options:
# set(network-enabled @enable-network@)
# finally, include the targets/version files
include("${CMAKE_CURRENT_LIST_DIR}/FluidSynthTargets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/FluidSynthConfigVersion.cmake")

View File

@ -399,12 +399,14 @@ target_link_libraries ( fluidsynth
if ( MACOSX_FRAMEWORK )
install ( TARGETS fluidsynth libfluidsynth
EXPORT FluidSynthTargets
RUNTIME DESTINATION ${BIN_INSTALL_DIR}
FRAMEWORK DESTINATION ${FRAMEWORK_INSTALL_DIR}
ARCHIVE DESTINATION ${FRAMEWORK_INSTALL_DIR}
)
else ( MACOSX_FRAMEWORK )
install ( TARGETS fluidsynth libfluidsynth
EXPORT FluidSynthTargets
RUNTIME DESTINATION ${BIN_INSTALL_DIR}
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
@ -413,6 +415,22 @@ else ( MACOSX_FRAMEWORK )
install ( FILES ${public_main_HEADER} DESTINATION ${INCLUDE_INSTALL_DIR} )
endif ( MACOSX_FRAMEWORK )
# Exported targets.
# build_interface: for the libfluidsynth target when imported from the build directory.
# install_interface: for the target when imported from the installed directory.
target_include_directories(libfluidsynth PUBLIC
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/;${CMAKE_SOURCE_DIR}/include/fluidsynth/;${CMAKE_BINARY_DIR}/include/;${CMAKE_BINARY_DIR}/include/fluidsynth/>"
"$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>"
)
# installation of the exported targets
install(EXPORT FluidSynthTargets
FILE FluidSynthTargets.cmake
NAMESPACE FluidSynth::
DESTINATION ${LIB_INSTALL_DIR}/cmake/fluidsynth
)
# ******* Auto Generated Lookup Tables ******
include(ExternalProject)