summaryrefslogtreecommitdiff
path: root/src/CMakeLists.txt
diff options
context:
space:
mode:
authorAlbrecht Schlosser <albrechts.fltk@online.de>2024-04-19 20:45:32 +0200
committerAlbrecht Schlosser <albrechts.fltk@online.de>2024-04-19 20:45:32 +0200
commita9d3d3e3fbefc80278392a88c0a81a5bbb97eb51 (patch)
tree80ec985bef2f9c46cf78a110abf490f64af08dac /src/CMakeLists.txt
parent2eb5d175fd46dbfd567a440774c362e8d3c67a90 (diff)
CMake: fix propagation of CMake targets to user projects (#954)
Unfortunately commit 5417ea5f1f33f62e2511bda74477da05dce900a3 broke simple user projects by propagating unknown CMake target names to linker requirements of user projects. This commit tries to fix this w/o breaking the intentions of PR #954.
Diffstat (limited to 'src/CMakeLists.txt')
-rw-r--r--src/CMakeLists.txt64
1 files changed, 45 insertions, 19 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b01ee09ab..e4f2306cd 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -630,13 +630,33 @@ if(WIN32)
endif()
#######################################################################
-
+#
# Prepare optional libs for shared and static FLTK libraries.
-# Note: OPTIONAL_LIBS is a CMake 'list' and may contain CMake targets,
-# i.e. it is only used for CMake stuff: target_link_libraries().
-# FIXME: make this all more consistent (targets rather than libs).
+#
+# Note: 'OPTIONAL_LIBS' is a CMake 'list' but must not contain arbitrary
+# CMake targets because these targets would be propagated to
+# consumer projects. The macro below simplifies adding link
+# libraries of such targets to 'OPTIONAL_LIBS'.
+#
+# This macro appends interface targets to 'OPTIONAL_LIBS'.
+# Input:
+# 'targets' may be a CMake list of targets or a single target.
+# It must be quoted if multiple targets are to be added in
+# one call (see examples below).
+#
+#######################################################################
-set(OPTIONAL_LIBS) # init
+macro(append_optional_libs targets)
+ foreach(_target ${targets})
+ get_target_property(_link_libraries ${_target} INTERFACE_LINK_LIBRARIES)
+ list(APPEND OPTIONAL_LIBS ${_link_libraries})
+ list(APPEND OPTIONAL_LIBS )
+ endforeach()
+ unset(_target)
+ unset(_link_libraries)
+endmacro()
+
+set(OPTIONAL_LIBS)
if(LIB_dl)
list(APPEND OPTIONAL_LIBS ${LIB_dl})
@@ -674,11 +694,11 @@ if(HAVE_XRENDER)
endif(HAVE_XRENDER)
if(USE_PANGO)
- ### FIXME ### This needs to use the PKG_* variables directly
- list(APPEND OPTIONAL_LIBS ${HAVE_LIB_PANGO} PkgConfig::PANGOCAIRO)
- list(APPEND OPTIONAL_LIBS PkgConfig::CAIRO ${HAVE_LIB_GOBJECT})
+ list(APPEND OPTIONAL_LIBS ${HAVE_LIB_PANGO})
+ append_optional_libs(PkgConfig::PANGOCAIRO)
+ list(APPEND OPTIONAL_LIBS ${HAVE_LIB_GOBJECT})
if(USE_PANGOXFT)
- list(APPEND OPTIONAL_LIBS PkgConfig::PANGOXFT)
+ append_optional_libs(PkgConfig::PANGOXFT)
endif(USE_PANGOXFT)
endif(USE_PANGO)
@@ -753,23 +773,29 @@ if(UNIX AND FLTK_BACKEND_WAYLAND)
endif()
if(FLTK_USE_GL)
- list(APPEND OPTIONAL_LIBS PkgConfig::WLD_EGL PkgConfig::PKG_EGL)
+ append_optional_libs("PkgConfig::WLD_EGL;PkgConfig::PKG_EGL")
endif(FLTK_USE_GL)
+
if(USE_SYSTEM_LIBDECOR)
- list(APPEND OPTIONAL_LIBS PkgConfig::SYSTEM_LIBDECOR)
+ append_optional_libs(PkgConfig::SYSTEM_LIBDECOR)
elseif(GTK_FOUND AND FLTK_USE_LIBDECOR_GTK)
- list(APPEND OPTIONAL_LIBS PkgConfig::GTK)
- endif(USE_SYSTEM_LIBDECOR)
- list(APPEND OPTIONAL_LIBS PkgConfig::WLDCURSOR PkgConfig::WLDCLIENT PkgConfig::XKBCOMMON -ldl)
+ append_optional_libs(PkgConfig::GTK)
+ endif()
+
+ append_optional_libs("PkgConfig::WLDCURSOR;PkgConfig::WLDCLIENT;PkgConfig::XKBCOMMON")
+
if(DBUS_FOUND)
- list(APPEND OPTIONAL_LIBS PkgConfig::DBUS)
- endif(DBUS_FOUND)
+ append_optional_libs(PkgConfig::DBUS)
+ endif()
+
endif(UNIX AND FLTK_BACKEND_WAYLAND)
+list(REMOVE_DUPLICATES OPTIONAL_LIBS)
+
#######################################################################
fl_add_library(fltk STATIC "${STATIC_FILES}")
-target_link_libraries(fltk PUBLIC ${OPTIONAL_LIBS})
+target_link_libraries(fltk PRIVATE ${OPTIONAL_LIBS})
#######################################################################
@@ -819,7 +845,7 @@ endif(FLTK_USE_GL)
if(FLTK_BUILD_SHARED_LIBS AND NOT MSVC)
fl_add_library(fltk SHARED "${SHARED_FILES}")
- target_link_libraries(fltk-shared PUBLIC ${OPTIONAL_LIBS})
+ target_link_libraries(fltk-shared PRIVATE ${OPTIONAL_LIBS})
###################################################################
@@ -888,7 +914,7 @@ if(FLTK_BUILD_SHARED_LIBS AND MSVC)
endif(OPENGL_FOUND)
fl_add_library(fltk SHARED "${SOURCES}")
- target_link_libraries(fltk-shared PUBLIC ${OPTIONAL_LIBS})
+ target_link_libraries(fltk-shared PRIVATE ${OPTIONAL_LIBS})
if(FLTK_USE_BUNDLED_JPEG)
target_link_libraries(fltk-shared PUBLIC fltk::jpeg-shared)