]> granicus.if.org Git - json-c/commitdiff
Specify dependent libraries, including -lbsd, in a more consistent way so linking...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Mon, 30 May 2022 15:30:11 +0000 (15:30 +0000)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Mon, 30 May 2022 15:30:11 +0000 (15:30 +0000)
Use target_link_libraries, plus fill in Libs.private in json-c.pc so pkg-config --static --libs works appropriately.
Also, only link against libbsd when arc4random is actually found there.

CMakeLists.txt

index 37a9f97a98763b83453397ebf0664b0a1b3918a5..89cff3f7a4a216ece6b6b1863a64b5dda3bf0252 100644 (file)
@@ -173,10 +173,12 @@ check_symbol_exists(arc4random      "stdlib.h" HAVE_ARC4RANDOM)
 if (NOT HAVE_ARC4RANDOM AND DISABLE_EXTRA_LIBS STREQUAL "OFF")
     check_include_file(bsd/stdlib.h HAVE_BSD_STDLIB_H)
     if (HAVE_BSD_STDLIB_H)
-               list(APPEND CMAKE_REQUIRED_LIBRARIES "-lbsd")
-               link_libraries(bsd)
+               list(APPEND CMAKE_REQUIRED_LIBRARIES "bsd")
                unset(HAVE_ARC4RANDOM CACHE)
         check_symbol_exists(arc4random   "bsd/stdlib.h" HAVE_ARC4RANDOM)
+        if (NOT HAVE_ARC4RANDOM)
+                       list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "bsd")
+        endif()
     endif()
 endif()
 
@@ -455,6 +457,8 @@ target_include_directories(${PROJECT_NAME}
         $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
 )
 
+target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
+
 # Allow to build static and shared libraries at the same time
 if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
     set(STATIC_LIB ${PROJECT_NAME}-static)
@@ -468,6 +472,8 @@ if (BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS)
             $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
     )
 
+       target_link_libraries(${PROJECT_NAME}-static PUBLIC ${CMAKE_REQUIRED_LIBRARIES})
+
     # rename the static library
     if (NOT MSVC)
     set_target_properties(${STATIC_LIB} PROPERTIES
@@ -521,6 +527,13 @@ if (UNIX OR MINGW OR CYGWIN)
     SET(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
     SET(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
     SET(VERSION ${PROJECT_VERSION})
+
+       # Linking against the static json-c requires
+       # dependent packages to include additional libs:
+       SET(LIBS_LIST ${CMAKE_REQUIRED_LIBRARIES})
+       list(TRANSFORM LIBS_LIST PREPEND "-l")
+       string(REPLACE ";" " " LIBS "${LIBS_LIST}")
+
     configure_file(json-c.pc.in json-c.pc @ONLY)
     set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
     install(FILES ${PROJECT_BINARY_DIR}/json-c.pc DESTINATION "${INSTALL_PKGCONFIG_DIR}")