Skip to content

Commit

Permalink
CMake: Get properties from the pkg-config helper.
Browse files Browse the repository at this point in the history
This replaces the `get_linker_flags_from_pkg_config` function with the
`get_target_properties_from_pkg_config` function.

Using raw LDFLAGS was not compatible with MSVC.
  • Loading branch information
FtZPetruska committed Mar 2, 2023
1 parent 652e4a5 commit 22cb4f4
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 85 deletions.
18 changes: 10 additions & 8 deletions README.cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ endif()

The last thing we need to search for is the usage requirements, providing them
makes static linking much easier. If pkg-config is available, we use a helper
function to get the correct set of link flags, otherwise you need to refer to
function to get the correct set of properties, otherwise you need to refer to
the upstream documentation. In the case of libinstpatch, we need `glib-2` and
`libsndfile`, fortunately, we already have Find modules for both libraries:

file [cmake_admin/FindInstPatch.cmake](./cmake_admin/FindInstPatch.cmake#L55), lines 55-69:

```cmake
if(PC_INSTPATCH_FOUND)
get_linker_flags_from_pkg_config("${InstPatch_LIBRARY}" "PC_INSTPATCH"
"_instpatch_link_libraries")
get_target_properties_from_pkg_config("${InstPatch_LIBRARY}" "PC_INSTPATCH"
"_instpatch")
else()
if(NOT TARGET GLib2::gobject-2
OR NOT TARGET GLib2::gthread-2
Expand Down Expand Up @@ -217,17 +217,18 @@ otherwise.

If the library is found, we then create the target and set its properties:

file [cmake_admin/FindInstPatch.cmake](./cmake_admin/FindInstPatch.cmake#L78), lines 78-86:
file [cmake_admin/FindInstPatch.cmake](./cmake_admin/FindInstPatch.cmake#L78), lines 78-87:

```cmake
if(InstPatch_FOUND AND NOT TARGET InstPatch::libinstpatch)
add_library(InstPatch::libinstpatch UNKNOWN IMPORTED)
set_target_properties(
InstPatch::libinstpatch
PROPERTIES IMPORTED_LOCATION "${InstPatch_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_INSTPATCH_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_instpatch_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${InstPatch_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_instpatch_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_instpatch_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_instpatch_link_directories}")
endif()
```

Expand All @@ -237,8 +238,9 @@ Here is a breakdown of the properties:
- `INTERFACE_INCLUDE_DIRECTORES` should be the result of `find_path`;
- `INTERFACE_LINK_LIBRARIES` should be the transitive usage requirements, it
may be omitted if there are none;
- `INTERFACE_COMPILE_OPTIONS` should be set to what pkg-config provided us, if
pkg-config is unavailable it will be ignored.
- `INTERFACE_COMPILE_OPTIONS` and `INTERFACE_LINK_DIRECTORIES` should be set to
what our `get_flags_from_pkg_config` helper provided us, if pkg-config is
unavailable they will be ignored.

Lastly, we call mark a few cache variables as advanced:

Expand Down
17 changes: 9 additions & 8 deletions cmake_admin/FindFLAC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ find_library(

# Handle transitive dependencies
if(PC_FLAC_FOUND)
get_linker_flags_from_pkg_config("${FLAC_LIBRARY}" "PC_FLAC"
"_flac_link_libraries")
get_target_properties_from_pkg_config("${FLAC_LIBRARY}" "PC_FLAC" "_flac")
else()
if(NOT TARGET "Ogg::ogg")
find_package(Ogg QUIET)
Expand All @@ -66,8 +65,8 @@ else()
endif()

if(PC_FLAC++_FOUND)
get_linker_flags_from_pkg_config("${FLAC++_LIBRARY}" "PC_FLAC++"
"_flac++_link_libraries")
get_target_properties_from_pkg_config("${FLAC++_LIBRARY}" "PC_FLAC++"
"_flac++")
else()
set(_flac++_link_libraries "FLAC::FLAC")
endif()
Expand All @@ -84,9 +83,10 @@ if(FLAC_FOUND AND NOT TARGET FLAC::FLAC)
set_target_properties(
FLAC::FLAC
PROPERTIES IMPORTED_LOCATION "${FLAC_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_FLAC_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_flac_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${FLAC_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_flac_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_flac_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_flac_link_directories}")
set(FLAC_FLAC_FOUND TRUE)
endif()

Expand All @@ -95,9 +95,10 @@ if(FLAC_FOUND AND NOT TARGET FLAC::FLAC++)
set_target_properties(
FLAC::FLAC++
PROPERTIES IMPORTED_LOCATION "${FLAC++_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_FLAC++_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_flac++_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${FLAC++_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_flac++_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_flac++_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_flac++_link_directories}")
set(FLAC_FLAC++_FOUND TRUE)
endif()

Expand Down
36 changes: 20 additions & 16 deletions cmake_admin/FindGLib2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ find_package_handle_standard_args(
if(GLib2_glib-2_LIBRARY AND NOT TARGET GLib2::glib-2)
# Handle transitive dependencies
if(PC_GLIB2_FOUND)
get_linker_flags_from_pkg_config("${GLib2_glib-2_LIBRARY}" "PC_GLIB2"
"_glib2_link_libraries")
get_target_properties_from_pkg_config("${GLib2_glib-2_LIBRARY}" "PC_GLIB2"
"_glib2")
else()
find_package(Intl QUIET)
find_package(Iconv QUIET)
Expand Down Expand Up @@ -157,16 +157,17 @@ if(GLib2_glib-2_LIBRARY AND NOT TARGET GLib2::glib-2)
set_target_properties(
GLib2::glib-2
PROPERTIES IMPORTED_LOCATION "${GLib2_glib-2_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_GLIB2_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_glib2_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${GLib2_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${_glib2_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_glib2_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_glib2_link_directories}")
endif()

if(GLib2_gthread-2_LIBRARY AND NOT TARGET GLib2::gthread-2)
# Handle transitive dependencies
if(PC_GTHREAD2_FOUND)
get_linker_flags_from_pkg_config("${GLib2_gthread-2_LIBRARY}" "PC_GTHREAD2"
"_gthread2_link_libraries")
get_target_properties_from_pkg_config("${GLib2_gthread-2_LIBRARY}"
"PC_GTHREAD2" "_gthread2")
else()
set(_gthread2_link_libraries "Threads::Threads" "GLib2::glib-2")
endif()
Expand All @@ -175,16 +176,17 @@ if(GLib2_gthread-2_LIBRARY AND NOT TARGET GLib2::gthread-2)
set_target_properties(
GLib2::gthread-2
PROPERTIES IMPORTED_LOCATION "${GLib2_gthread-2_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_GTHREAD2_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_gthread2_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${GLib2_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${_gthread2_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_gthread2_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_gthread2_link_directories}")
endif()

if(GLib2_gmodule-2_LIBRARY AND NOT TARGET GLib2::gmodule-2)
# Handle transitive dependencies
if(PC_GMODULE2_FOUND)
get_linker_flags_from_pkg_config("${GLib2_gmodule-2_LIBRARY}" "PC_GMODULE2"
"_gmodule2_link_libraries")
get_target_properties_from_pkg_config("${GLib2_gmodule-2_LIBRARY}"
"PC_GMODULE2" "_gmodule2")
else()
set(_gmodule2_link_libraries "GLib2::glib-2")
endif()
Expand All @@ -193,16 +195,17 @@ if(GLib2_gmodule-2_LIBRARY AND NOT TARGET GLib2::gmodule-2)
set_target_properties(
GLib2::gmodule-2
PROPERTIES IMPORTED_LOCATION "${GLib2_gmodule-2_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_GMODULE2_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_gmodule2_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${GLib2_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${_gmodule2_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_gmodule2_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_gmodule2_link_directories}")
endif()

if(GLib2_gobject-2_LIBRARY AND NOT TARGET GLib2::gobject-2)
# Handle transitive dependencies
if(PC_GOBJECT2_FOUND)
get_linker_flags_from_pkg_config("${GLib2_gobject-2_LIBRARY}" "PC_OBJECT2"
"_gobject2_link_libraries")
get_target_properties_from_pkg_config("${GLib2_gobject-2_LIBRARY}"
"PC_OBJECT2" "_gobject2")
else()
find_package(libffi QUIET)
set(_gobject2_link_libraries "libffi" "GLib2::glib-2")
Expand All @@ -212,9 +215,10 @@ if(GLib2_gobject-2_LIBRARY AND NOT TARGET GLib2::gobject-2)
set_target_properties(
GLib2::gobject-2
PROPERTIES IMPORTED_LOCATION "${GLib2_gobject-2_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_GOBJECT2_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_gobject2_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${GLib2_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${_gobject2_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_gobject2_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_gobject2_link_directories}")
endif()

mark_as_advanced(GLib2_INCLUDE_DIR GLib2_glib-2_LIBRARY GLib2_gthread-2_LIBRARY
Expand Down
9 changes: 5 additions & 4 deletions cmake_admin/FindInstPatch.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ endif()

# Handle transitive dependencies
if(PC_INSTPATCH_FOUND)
get_linker_flags_from_pkg_config("${InstPatch_LIBRARY}" "PC_INSTPATCH"
"_instpatch_link_libraries")
get_target_properties_from_pkg_config("${InstPatch_LIBRARY}" "PC_INSTPATCH"
"_instpatch")
else()
if(NOT TARGET GLib2::gobject-2
OR NOT TARGET GLib2::gthread-2
Expand All @@ -80,9 +80,10 @@ if(InstPatch_FOUND AND NOT TARGET InstPatch::libinstpatch)
set_target_properties(
InstPatch::libinstpatch
PROPERTIES IMPORTED_LOCATION "${InstPatch_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_INSTPATCH_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_instpatch_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${InstPatch_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_instpatch_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_instpatch_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_instpatch_link_directories}")
endif()

mark_as_advanced(InstPatch_INCLUDE_DIR InstPatch_LIBRARY)
8 changes: 4 additions & 4 deletions cmake_admin/FindJack.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ find_library(

# Handle transitive dependencies
if(PC_JACK_FOUND)
get_linker_flags_from_pkg_config("${Jack_LIBRARY}" "PC_JACK"
"_jack_link_libraries")
get_target_properties_from_pkg_config("${Jack_LIBRARY}" "PC_JACK" "_jack")
else()
set(_jack_link_libraries "Threads::Threads")
endif()
Expand All @@ -56,9 +55,10 @@ if(Jack_FOUND AND NOT TARGET Jack::Jack)
set_target_properties(
Jack::Jack
PROPERTIES IMPORTED_LOCATION "${Jack_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_JACK_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_jack_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${Jack_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_jack_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_jack_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_jack_link_directories}")
endif()

mark_as_advanced(Jack_INCLUDE_DIR Jack_LIBRARY)
8 changes: 4 additions & 4 deletions cmake_admin/FindLASH.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ endif()

# Handle transitive dependencies
if(PC_LASH_FOUND)
get_linker_flags_from_pkg_config("${LASH_LIBRARY}" "PC_LASH"
"_lash_link_libraries")
get_target_properties_from_pkg_config("${LASH_LIBRARY}" "PC_LASH" "_lash")
else()
set(_lash_link_libraries "Jack::Jack" "Threads::Threads" "ALSA::ALSA" "uuid")
endif()
Expand All @@ -76,9 +75,10 @@ if(LASH_FOUND AND NOT TARGET LASH::LASH)
set_target_properties(
LASH::LASH
PROPERTIES IMPORTED_LOCATION "${LASH_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_LASH_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_lash_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${LASH_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_lash_link_libraries}")
INTERFACE_LINK_LIBRARIES "${_lash_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_lash_link_directories}")
endif()

mark_as_advanced(LASH_INCLUDE_DIR LASH_LIBRARY)
32 changes: 26 additions & 6 deletions cmake_admin/FindMPG123.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ find_library(
NAMES "syn123"
PATHS "${PC_SYN123_LIBDIR}")

# Extract additional flags if pkg-config is available
if(PC_MPG123_FOUND)
get_target_properties_from_pkg_config("${MPG123_libmpg123_LIBRARY}"
"PC_MPG123" "_mpg123")
endif()
if(PC_OUT123_FOUND)
get_target_properties_from_pkg_config("${MPG123_libout123_LIBRARY}"
"PC_OUT123" "_out123")
endif()
if(PC_SYN123_FOUND)
get_target_properties_from_pkg_config("${MPG123_libsyn123_LIBRARY}"
"PC_SYN123" "_syn123")
endif()

# Forward the result to CMake
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
Expand All @@ -71,8 +85,10 @@ if(MPG123_FOUND AND NOT TARGET MPG123::libmpg123)
set_target_properties(
MPG123::libmpg123
PROPERTIES IMPORTED_LOCATION "${MPG123_libmpg123_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_MPG123_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${MPG123_INCLUDE_DIR}")
INTERFACE_COMPILE_OPTIONS "${_mpg123_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${MPG123_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_mpg123_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_mpg123_link_directories}")
set(MPG123_libmpg123_FOUND TRUE)
endif()

Expand All @@ -81,8 +97,10 @@ if(MPG123_FOUND AND NOT TARGET MPG123::libout123)
set_target_properties(
MPG123::libout123
PROPERTIES IMPORTED_LOCATION "${MPG123_libout123_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_OUT123_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${MPG123_INCLUDE_DIR}")
INTERFACE_COMPILE_OPTIONS "${_out123_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${MPG123_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_out123_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_out123_link_directories}")
set(MPG123_libout123_FOUND TRUE)
endif()

Expand All @@ -91,8 +109,10 @@ if(MPG123_FOUND AND NOT TARGET MPG123::libsyn123)
set_target_properties(
MPG123::libsyn123
PROPERTIES IMPORTED_LOCATION "${MPG123_libsyn123_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_SYN123_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${MPG123_INCLUDE_DIR}")
INTERFACE_COMPILE_OPTIONS "${_syn123_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${MPG123_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_syn123_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_syn123_link_directories}")
set(MPG123_libsyn123_FOUND TRUE)
endif()

Expand Down
11 changes: 9 additions & 2 deletions cmake_admin/FindOgg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ find_library(
NAMES "ogg"
PATHS "${PC_OGG_LIBDIR}")

# Extract additional flags if pkg-config is available
if(PC_OGG_FOUND)
get_target_properties_from_pkg_config("${_ogg_library}" "PC_OGG" "_ogg")
endif()

# Forward the result to CMake
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Ogg REQUIRED_VARS "_ogg_library"
Expand All @@ -60,8 +65,10 @@ if(Ogg_FOUND AND NOT TARGET Ogg::ogg)
set_target_properties(
Ogg::ogg
PROPERTIES IMPORTED_LOCATION "${_ogg_library}"
INTERFACE_COMPILE_OPTIONS "${PC_OGG_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${Ogg_INCLUDE_DIR}")
INTERFACE_COMPILE_OPTIONS "${_ogg_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${Ogg_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_ogg_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_ogg_link_directories}")

# Set additional variables for compatibility with upstream config
set(Ogg_INCLUDE_DIRS "${Ogg_INCLUDE_DIR}")
Expand Down
11 changes: 9 additions & 2 deletions cmake_admin/FindOpus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ else()
set(OPUS_VERSION_PATCH)
endif()

# Extract additional flags if pkg-config is available
if(PC_OPUS_FOUND)
get_target_properties_from_pkg_config("${Opus_LIBRARY}" "PC_OPUS" "_opus")
endif()

# Forward the result to CMake
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
Expand All @@ -85,8 +90,10 @@ if(Opus_FOUND AND NOT TARGET Opus::opus)
set_target_properties(
Opus::opus
PROPERTIES IMPORTED_LOCATION "${Opus_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_OPUS_CFLAGS_OTHER}"
INTERFACE_INCLUDE_DIRECTORIES "${_opus_include_dirs}")
INTERFACE_COMPILE_OPTIONS "${_opus_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES "${_opus_include_dirs}"
INTERFACE_LINK_LIBRARIES "${_opus_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_opus_link_directories}")

# Set additional variables for compatibility with upstream config
set(OPUS_FOUND TRUE)
Expand Down
12 changes: 10 additions & 2 deletions cmake_admin/FindPipeWire.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ elseif(PipeWire_INCLUDE_DIR)
endif()
endif()

# Extract additional flags if pkg-config is available
if(PC_PIPEWIRE_FOUND)
get_target_properties_from_pkg_config("${PipeWire_LIBRARY}" "PC_PIPEWIRE"
"_pipewire")
endif()

# Forward the result to CMake
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
Expand All @@ -81,9 +87,11 @@ if(PipeWire_FOUND AND NOT TARGET PipeWire::PipeWire)
set_target_properties(
PipeWire::PipeWire
PROPERTIES IMPORTED_LOCATION "${PipeWire_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${PC_PIPEWIRE_CFLAGS_OTHER}"
INTERFACE_COMPILE_OPTIONS "${_pipewire_compile_options}"
INTERFACE_INCLUDE_DIRECTORIES
"${PipeWire_INCLUDE_DIR};${Spa_INCLUDE_DIR}")
"${PipeWire_INCLUDE_DIR};${Spa_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${_pipewire_link_libraries}"
INTERFACE_LINK_DIRECTORIES "${_pipewire_link_directories}")
endif()

mark_as_advanced(PipeWire_INCLUDE_DIR Spa_INCLUDE_DIR PipeWire_LIBRARY)
Loading

0 comments on commit 22cb4f4

Please sign in to comment.