Fixed SONAME lookup for runtime loading of shared libraries in CMake project.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 19 Nov 2013 15:25:00 -0500
changeset 8017 d84a30e5cbd5
parent 8016 3746af784462
child 8018 cff03048babd
Fixed SONAME lookup for runtime loading of shared libraries in CMake project. Fixes Bugzilla #2249.
cmake/sdlchecks.cmake
--- a/cmake/sdlchecks.cmake	Tue Nov 19 15:00:39 2013 -0500
+++ b/cmake/sdlchecks.cmake	Tue Nov 19 15:25:00 2013 -0500
@@ -1,3 +1,34 @@
+macro(FindLibraryAndSONAME _LIB)
+  string(TOUPPER ${_LIB} _UPPERLNAME)
+  string(REGEX REPLACE "\\-" "_" _LNAME "${_UPPERLNAME}")
+
+  find_library(${_LNAME}_LIB ${_LIB})
+  if(${_LNAME}_LIB)
+    # reduce the library name for shared linking
+
+    get_filename_component(_LIB_REALPATH ${${_LNAME}_LIB} REALPATH)  # resolves symlinks
+    get_filename_component(_LIB_JUSTNAME ${_LIB_REALPATH} NAME)
+
+    if(APPLE)
+      string(REGEX REPLACE "(\\.[0-9]*)\\.[0-9\\.]*dylib$" "\\1.dylib" _LIB_REGEXD "${_LIB_JUSTNAME}")
+    else()
+      string(REGEX REPLACE "(\\.[0-9]*)\\.[0-9\\.]*$" "\\1" _LIB_REGEXD "${_LIB_JUSTNAME}")
+    endif()
+
+    SET(_DEBUG_FindSONAME FALSE)
+    if(_DEBUG_FindSONAME)
+      message_warn("DYNLIB OUTPUTVAR: ${_LIB} ... ${_LNAME}_LIB")
+      message_warn("DYNLIB ORIGINAL LIB: ${_LIB} ... ${${_LNAME}_LIB}")
+      message_warn("DYNLIB REALPATH LIB: ${_LIB} ... ${_LIB_REALPATH}")
+      message_warn("DYNLIB JUSTNAME LIB: ${_LIB} ... ${_LIB_JUSTNAME}")
+      message_warn("DYNLIB REGEX'd LIB: ${_LIB} ... ${_LIB_REGEXD}")
+    endif()
+
+    message(STATUS "dynamic lib${_LIB} -> ${_LIB_REGEXD}")
+    set(${_LNAME}_LIB_SONAME ${_LIB_REGEXD})
+  endif()
+endmacro()
+
 macro(CheckDLOPEN)
   check_function_exists(dlopen HAVE_DLOPEN)
   if(NOT HAVE_DLOPEN)
@@ -83,9 +114,8 @@
         if(NOT HAVE_DLOPEN)
           message_warn("You must have SDL_LoadObject() support for dynamic ALSA loading")
         else()
-          find_library(_ALSA_LIB asound)
-          get_filename_component(F_ALSA_LIB ${_ALSA_LIB} NAME)
-          set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${F_ALSA_LIB}\"")
+          FindLibraryAndSONAME("asound")
+          set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${ASOUND_LIB_SONAME}\"")
           set(HAVE_ALSA_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(ALSA_SHARED)
@@ -114,9 +144,8 @@
         if(NOT HAVE_DLOPEN)
           message_warn("You must have SDL_LoadObject() support for dynamic PulseAudio loading")
         else()
-          find_library(D_PULSE_LIB pulse-simple)
-          get_filename_component(F_PULSE_LIB ${D_PULSE_LIB} NAME)
-          set(SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC "\"${F_PULSE_LIB}\"")
+          FindLibraryAndSONAME("pulse-simple")
+          set(SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC "\"${PULSE_SIMPLE_LIB_SONAME}\"")
           set(HAVE_PULSEAUDIO_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(PULSEAUDIO_SHARED)
@@ -145,9 +174,8 @@
         if(NOT HAVE_DLOPEN)
           message_warn("You must have SDL_LoadObject() support for dynamic ESD loading")
         else()
-          find_library(D_ESD_LIB esd)
-          get_filename_component(F_ESD_LIB ${D_ESD_LIB} NAME)
-          set(SDL_AUDIO_DRIVER_ESD_DYNAMIC "\"${F_ESD_LIB}\"")
+          FindLibraryAndSONAME(esd)
+          set(SDL_AUDIO_DRIVER_ESD_DYNAMIC "\"${ESD_LIB_SONAME}\"")
           set(HAVE_ESD_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(ESD_SHARED)
@@ -181,9 +209,8 @@
           message_warn("You must have SDL_LoadObject() support for dynamic ARTS loading")
         else()
           # TODO
-          find_library(D_ARTS_LIB artsc)
-          get_filename_component(F_ARTS_LIB ${D_ARTS_LIB} NAME)
-          set(SDL_AUDIO_DRIVER_ARTS_DYNAMIC "\"${F_ARTS_LIB}\"")
+          FindLibraryAndSONAME(artsc)
+          set(SDL_AUDIO_DRIVER_ARTS_DYNAMIC "\"${ARTSC_LIB_SONAME}\"")
           set(HAVE_ARTS_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(ARTS_SHARED)
@@ -213,8 +240,8 @@
         if(NOT HAVE_DLOPEN)
           message_warn("You must have SDL_LoadObject() support for dynamic NAS loading")
         else()
-          get_filename_component(F_NAS_LIB ${D_NAS_LIB} NAME)
-          set(SDL_AUDIO_DRIVER_NAS_DYNAMIC "\"${F_NAS_LIB}\"")
+          FindLibraryAndSONAME("audio")
+          set(SDL_AUDIO_DRIVER_NAS_DYNAMIC "\"${AUDIO_LIB_SONAME}\"")
           set(HAVE_NAS_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(NAS_SHARED)
@@ -234,7 +261,7 @@
   if(SNDIO)
     # TODO: set include paths properly, so the sndio headers are found
     check_include_file(sndio.h HAVE_SNDIO_H)
-    find_library(D_SNDIO_LIB audio)
+    find_library(D_SNDIO_LIB sndio)
     if(HAVE_SNDIO_H AND D_SNDIO_LIB)
       set(HAVE_SNDIO TRUE)
       file(GLOB SNDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/sndio/*.c)
@@ -244,8 +271,8 @@
         if(NOT HAVE_DLOPEN)
           message_warn("You must have SDL_LoadObject() support for dynamic sndio loading")
         else()
-          get_filename_component(F_SNDIO_LIB ${D_SNDIO_LIB} NAME)
-          set(SDL_AUDIO_DRIVER_SNDIO_DYNAMIC "\"${F_SNDIO_LIB}\"")
+          FindLibraryAndSONAME("sndio")
+          set(SDL_AUDIO_DRIVER_SNDIO_DYNAMIC "\"${SNDIO_LIB_SONAME}\"")
           set(HAVE_SNDIO_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(SNDIO_SHARED)
@@ -274,9 +301,8 @@
         if(NOT HAVE_DLOPEN)
           message_warn("You must have SDL_LoadObject() support for dynamic FusionSound loading")
         else()
-          find_library(D_FUSIONSOUND_LIB fusionsound)
-          get_filename_component(F_FUSIONSOUND_LIB ${D_FUSIONSOUND_LIB} NAME)
-          set(SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "\"${F_FUSIONSOUND_LIB}\"")
+          FindLibraryAndSONAME("fusionsound")
+          set(SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "\"${FUSIONSOUND_LIB_SONAME}\"")
           set(HAVE_FUSIONSOUND_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(FUSIONSOUND_SHARED)
@@ -295,13 +321,7 @@
 macro(CheckX11)
   if(VIDEO_X11)
     foreach(_LIB X11 Xext Xcursor Xinerama Xi Xrandr Xrender Xss Xxf86vm)
-      string(TOUPPER ${_LIB} _LNAME)
-      find_library(${_LNAME}_LIB ${_LIB})
-      if(${_LNAME}_LIB)
-        # reduce the library name for shared linking
-        get_filename_component(_TMP ${${_LNAME}_LIB} NAME)
-        set(${_LNAME}_LIB ${_TMP})
-      endif()
+        FindLibraryAndSONAME("${_LIB}")
     endforeach()
 
     find_path(X_INCLUDEDIR X11/Xlib.h)
@@ -355,8 +375,8 @@
           set(HAVE_X11_SHARED TRUE)
         endif()
         if(HAVE_X11_SHARED)
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC "\"${X11_LIB}\"")
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "\"${XEXT_LIB}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC "\"${X11_LIB_SONAME}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "\"${XEXT_LIB_SONAME}\"")
         else(HAVE_X11_SHARED)
           list(APPEND EXTRA_LIBS ${X11_LIB} ${XEXT_LIB})
         endif(HAVE_X11_SHARED)
@@ -402,7 +422,7 @@
       if(VIDEO_X11_XCURSOR AND HAVE_XCURSOR_H)
         set(HAVE_VIDEO_X11_XCURSOR TRUE)
         if(HAVE_X11_SHARED AND XCURSOR_LIB)
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "\"${XCURSOR_LIB}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "\"${XCURSOR_LIB_SONAME}\"")
         else(HAVE_X11_SHARED AND XCURSOR_LIB)
           list(APPEND EXTRA_LIBS ${XCURSOR_LIB})
         endif(HAVE_X11_SHARED AND XCURSOR_LIB)
@@ -412,7 +432,7 @@
       if(VIDEO_X11_XINERAMA AND HAVE_XINERAMA_H)
         set(HAVE_VIDEO_X11_XINERAMA TRUE)
         if(HAVE_X11_SHARED AND XINERAMA_LIB)
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "\"${XINERAMA_LIB}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "\"${XINERAMA_LIB_SONAME}\"")
         else(HAVE_X11_SHARED AND XINERAMA_LIB)
           list(APPEND EXTRA_LIBS ${XINERAMA_LIB})
         endif(HAVE_X11_SHARED AND XINERAMA_LIB)
@@ -422,7 +442,7 @@
       if(VIDEO_X11_XINPUT AND HAVE_XINPUT_H)
         set(HAVE_VIDEO_X11_XINPUT TRUE)
         if(HAVE_X11_SHARED AND XI_LIB)
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "\"${XI_LIB}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "\"${XI_LIB_SONAME}\"")
         else(HAVE_X11_SHARED AND XI_LIB)
           list(APPEND EXTRA_LIBS ${XI_LIB})
         endif(HAVE_X11_SHARED AND XI_LIB)
@@ -447,7 +467,7 @@
 
       if(VIDEO_X11_XRANDR AND HAVE_XRANDR_H)
         if(HAVE_X11_SHARED AND XRANDR_LIB)
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "\"${XRANDR_LIB}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "\"${XRANDR_LIB_SONAME}\"")
         else(HAVE_X11_SHARED AND XRANDR_LIB)
           list(APPEND EXTRA_LIBS ${XRANDR_LIB})
         endif(HAVE_X11_SHARED AND XRANDR_LIB)
@@ -457,7 +477,7 @@
 
       if(VIDEO_X11_XSCRNSAVER AND HAVE_XSS_H)
         if(HAVE_X11_SHARED AND XSS_LIB)
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "\"${XSS_LIB}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "\"${XSS_LIB_SONAME}\"")
         else(HAVE_X11_SHARED AND XSS_LIB)
           list(APPEND EXTRA_LIBS ${XSS_LIB})
         endif(HAVE_X11_SHARED AND XSS_LIB)
@@ -472,7 +492,7 @@
 
       if(VIDEO_X11_XVM AND HAVE_XF86VM_H)
         if(HAVE_X11_SHARED AND XF86VMODE_LIB)
-          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "\"${XF86VMODE_LIB}\"")
+          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "\"${XF86VMODE_LIB_SONAME}\"")
         else(HAVE_X11_SHARED AND XF86VMODE_LIB)
           list(APPEND EXTRA_LIBS ${XF86VMODE_LIB})
         endif(HAVE_X11_SHARED AND XF86VMODE_LIB)
@@ -522,9 +542,8 @@
         if(NOT HAVE_DLOPEN)
           message_warn("You must have SDL_LoadObject() support for dynamic DirectFB loading")
         else()
-          find_library(D_DIRECTFB_LIB directfb)
-          get_filename_component(F_DIRECTFB_LIB ${D_DIRECTFB_LIB} NAME)
-          set(SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC "\"${F_DIRECTFB_LIB}\"")
+          FindLibraryAndSONAME("directfb")
+          set(SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC "\"${DIRECTFB_LIB_SONAME}\"")
           set(HAVE_DIRECTFB_SHARED TRUE)
         endif(NOT HAVE_DLOPEN)
       else(DIRECTFB_SHARED)