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 8017d84a30e5cbd5
parent 8016 3746af784462
child 8018 cff03048babd
Fixed SONAME lookup for runtime loading of shared libraries in CMake project.

Fixes Bugzilla #2249.
cmake/sdlchecks.cmake
     1.1 --- a/cmake/sdlchecks.cmake	Tue Nov 19 15:00:39 2013 -0500
     1.2 +++ b/cmake/sdlchecks.cmake	Tue Nov 19 15:25:00 2013 -0500
     1.3 @@ -1,3 +1,34 @@
     1.4 +macro(FindLibraryAndSONAME _LIB)
     1.5 +  string(TOUPPER ${_LIB} _UPPERLNAME)
     1.6 +  string(REGEX REPLACE "\\-" "_" _LNAME "${_UPPERLNAME}")
     1.7 +
     1.8 +  find_library(${_LNAME}_LIB ${_LIB})
     1.9 +  if(${_LNAME}_LIB)
    1.10 +    # reduce the library name for shared linking
    1.11 +
    1.12 +    get_filename_component(_LIB_REALPATH ${${_LNAME}_LIB} REALPATH)  # resolves symlinks
    1.13 +    get_filename_component(_LIB_JUSTNAME ${_LIB_REALPATH} NAME)
    1.14 +
    1.15 +    if(APPLE)
    1.16 +      string(REGEX REPLACE "(\\.[0-9]*)\\.[0-9\\.]*dylib$" "\\1.dylib" _LIB_REGEXD "${_LIB_JUSTNAME}")
    1.17 +    else()
    1.18 +      string(REGEX REPLACE "(\\.[0-9]*)\\.[0-9\\.]*$" "\\1" _LIB_REGEXD "${_LIB_JUSTNAME}")
    1.19 +    endif()
    1.20 +
    1.21 +    SET(_DEBUG_FindSONAME FALSE)
    1.22 +    if(_DEBUG_FindSONAME)
    1.23 +      message_warn("DYNLIB OUTPUTVAR: ${_LIB} ... ${_LNAME}_LIB")
    1.24 +      message_warn("DYNLIB ORIGINAL LIB: ${_LIB} ... ${${_LNAME}_LIB}")
    1.25 +      message_warn("DYNLIB REALPATH LIB: ${_LIB} ... ${_LIB_REALPATH}")
    1.26 +      message_warn("DYNLIB JUSTNAME LIB: ${_LIB} ... ${_LIB_JUSTNAME}")
    1.27 +      message_warn("DYNLIB REGEX'd LIB: ${_LIB} ... ${_LIB_REGEXD}")
    1.28 +    endif()
    1.29 +
    1.30 +    message(STATUS "dynamic lib${_LIB} -> ${_LIB_REGEXD}")
    1.31 +    set(${_LNAME}_LIB_SONAME ${_LIB_REGEXD})
    1.32 +  endif()
    1.33 +endmacro()
    1.34 +
    1.35  macro(CheckDLOPEN)
    1.36    check_function_exists(dlopen HAVE_DLOPEN)
    1.37    if(NOT HAVE_DLOPEN)
    1.38 @@ -83,9 +114,8 @@
    1.39          if(NOT HAVE_DLOPEN)
    1.40            message_warn("You must have SDL_LoadObject() support for dynamic ALSA loading")
    1.41          else()
    1.42 -          find_library(_ALSA_LIB asound)
    1.43 -          get_filename_component(F_ALSA_LIB ${_ALSA_LIB} NAME)
    1.44 -          set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${F_ALSA_LIB}\"")
    1.45 +          FindLibraryAndSONAME("asound")
    1.46 +          set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${ASOUND_LIB_SONAME}\"")
    1.47            set(HAVE_ALSA_SHARED TRUE)
    1.48          endif(NOT HAVE_DLOPEN)
    1.49        else(ALSA_SHARED)
    1.50 @@ -114,9 +144,8 @@
    1.51          if(NOT HAVE_DLOPEN)
    1.52            message_warn("You must have SDL_LoadObject() support for dynamic PulseAudio loading")
    1.53          else()
    1.54 -          find_library(D_PULSE_LIB pulse-simple)
    1.55 -          get_filename_component(F_PULSE_LIB ${D_PULSE_LIB} NAME)
    1.56 -          set(SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC "\"${F_PULSE_LIB}\"")
    1.57 +          FindLibraryAndSONAME("pulse-simple")
    1.58 +          set(SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC "\"${PULSE_SIMPLE_LIB_SONAME}\"")
    1.59            set(HAVE_PULSEAUDIO_SHARED TRUE)
    1.60          endif(NOT HAVE_DLOPEN)
    1.61        else(PULSEAUDIO_SHARED)
    1.62 @@ -145,9 +174,8 @@
    1.63          if(NOT HAVE_DLOPEN)
    1.64            message_warn("You must have SDL_LoadObject() support for dynamic ESD loading")
    1.65          else()
    1.66 -          find_library(D_ESD_LIB esd)
    1.67 -          get_filename_component(F_ESD_LIB ${D_ESD_LIB} NAME)
    1.68 -          set(SDL_AUDIO_DRIVER_ESD_DYNAMIC "\"${F_ESD_LIB}\"")
    1.69 +          FindLibraryAndSONAME(esd)
    1.70 +          set(SDL_AUDIO_DRIVER_ESD_DYNAMIC "\"${ESD_LIB_SONAME}\"")
    1.71            set(HAVE_ESD_SHARED TRUE)
    1.72          endif(NOT HAVE_DLOPEN)
    1.73        else(ESD_SHARED)
    1.74 @@ -181,9 +209,8 @@
    1.75            message_warn("You must have SDL_LoadObject() support for dynamic ARTS loading")
    1.76          else()
    1.77            # TODO
    1.78 -          find_library(D_ARTS_LIB artsc)
    1.79 -          get_filename_component(F_ARTS_LIB ${D_ARTS_LIB} NAME)
    1.80 -          set(SDL_AUDIO_DRIVER_ARTS_DYNAMIC "\"${F_ARTS_LIB}\"")
    1.81 +          FindLibraryAndSONAME(artsc)
    1.82 +          set(SDL_AUDIO_DRIVER_ARTS_DYNAMIC "\"${ARTSC_LIB_SONAME}\"")
    1.83            set(HAVE_ARTS_SHARED TRUE)
    1.84          endif(NOT HAVE_DLOPEN)
    1.85        else(ARTS_SHARED)
    1.86 @@ -213,8 +240,8 @@
    1.87          if(NOT HAVE_DLOPEN)
    1.88            message_warn("You must have SDL_LoadObject() support for dynamic NAS loading")
    1.89          else()
    1.90 -          get_filename_component(F_NAS_LIB ${D_NAS_LIB} NAME)
    1.91 -          set(SDL_AUDIO_DRIVER_NAS_DYNAMIC "\"${F_NAS_LIB}\"")
    1.92 +          FindLibraryAndSONAME("audio")
    1.93 +          set(SDL_AUDIO_DRIVER_NAS_DYNAMIC "\"${AUDIO_LIB_SONAME}\"")
    1.94            set(HAVE_NAS_SHARED TRUE)
    1.95          endif(NOT HAVE_DLOPEN)
    1.96        else(NAS_SHARED)
    1.97 @@ -234,7 +261,7 @@
    1.98    if(SNDIO)
    1.99      # TODO: set include paths properly, so the sndio headers are found
   1.100      check_include_file(sndio.h HAVE_SNDIO_H)
   1.101 -    find_library(D_SNDIO_LIB audio)
   1.102 +    find_library(D_SNDIO_LIB sndio)
   1.103      if(HAVE_SNDIO_H AND D_SNDIO_LIB)
   1.104        set(HAVE_SNDIO TRUE)
   1.105        file(GLOB SNDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/sndio/*.c)
   1.106 @@ -244,8 +271,8 @@
   1.107          if(NOT HAVE_DLOPEN)
   1.108            message_warn("You must have SDL_LoadObject() support for dynamic sndio loading")
   1.109          else()
   1.110 -          get_filename_component(F_SNDIO_LIB ${D_SNDIO_LIB} NAME)
   1.111 -          set(SDL_AUDIO_DRIVER_SNDIO_DYNAMIC "\"${F_SNDIO_LIB}\"")
   1.112 +          FindLibraryAndSONAME("sndio")
   1.113 +          set(SDL_AUDIO_DRIVER_SNDIO_DYNAMIC "\"${SNDIO_LIB_SONAME}\"")
   1.114            set(HAVE_SNDIO_SHARED TRUE)
   1.115          endif(NOT HAVE_DLOPEN)
   1.116        else(SNDIO_SHARED)
   1.117 @@ -274,9 +301,8 @@
   1.118          if(NOT HAVE_DLOPEN)
   1.119            message_warn("You must have SDL_LoadObject() support for dynamic FusionSound loading")
   1.120          else()
   1.121 -          find_library(D_FUSIONSOUND_LIB fusionsound)
   1.122 -          get_filename_component(F_FUSIONSOUND_LIB ${D_FUSIONSOUND_LIB} NAME)
   1.123 -          set(SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "\"${F_FUSIONSOUND_LIB}\"")
   1.124 +          FindLibraryAndSONAME("fusionsound")
   1.125 +          set(SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "\"${FUSIONSOUND_LIB_SONAME}\"")
   1.126            set(HAVE_FUSIONSOUND_SHARED TRUE)
   1.127          endif(NOT HAVE_DLOPEN)
   1.128        else(FUSIONSOUND_SHARED)
   1.129 @@ -295,13 +321,7 @@
   1.130  macro(CheckX11)
   1.131    if(VIDEO_X11)
   1.132      foreach(_LIB X11 Xext Xcursor Xinerama Xi Xrandr Xrender Xss Xxf86vm)
   1.133 -      string(TOUPPER ${_LIB} _LNAME)
   1.134 -      find_library(${_LNAME}_LIB ${_LIB})
   1.135 -      if(${_LNAME}_LIB)
   1.136 -        # reduce the library name for shared linking
   1.137 -        get_filename_component(_TMP ${${_LNAME}_LIB} NAME)
   1.138 -        set(${_LNAME}_LIB ${_TMP})
   1.139 -      endif()
   1.140 +        FindLibraryAndSONAME("${_LIB}")
   1.141      endforeach()
   1.142  
   1.143      find_path(X_INCLUDEDIR X11/Xlib.h)
   1.144 @@ -355,8 +375,8 @@
   1.145            set(HAVE_X11_SHARED TRUE)
   1.146          endif()
   1.147          if(HAVE_X11_SHARED)
   1.148 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC "\"${X11_LIB}\"")
   1.149 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "\"${XEXT_LIB}\"")
   1.150 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC "\"${X11_LIB_SONAME}\"")
   1.151 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "\"${XEXT_LIB_SONAME}\"")
   1.152          else(HAVE_X11_SHARED)
   1.153            list(APPEND EXTRA_LIBS ${X11_LIB} ${XEXT_LIB})
   1.154          endif(HAVE_X11_SHARED)
   1.155 @@ -402,7 +422,7 @@
   1.156        if(VIDEO_X11_XCURSOR AND HAVE_XCURSOR_H)
   1.157          set(HAVE_VIDEO_X11_XCURSOR TRUE)
   1.158          if(HAVE_X11_SHARED AND XCURSOR_LIB)
   1.159 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "\"${XCURSOR_LIB}\"")
   1.160 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "\"${XCURSOR_LIB_SONAME}\"")
   1.161          else(HAVE_X11_SHARED AND XCURSOR_LIB)
   1.162            list(APPEND EXTRA_LIBS ${XCURSOR_LIB})
   1.163          endif(HAVE_X11_SHARED AND XCURSOR_LIB)
   1.164 @@ -412,7 +432,7 @@
   1.165        if(VIDEO_X11_XINERAMA AND HAVE_XINERAMA_H)
   1.166          set(HAVE_VIDEO_X11_XINERAMA TRUE)
   1.167          if(HAVE_X11_SHARED AND XINERAMA_LIB)
   1.168 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "\"${XINERAMA_LIB}\"")
   1.169 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "\"${XINERAMA_LIB_SONAME}\"")
   1.170          else(HAVE_X11_SHARED AND XINERAMA_LIB)
   1.171            list(APPEND EXTRA_LIBS ${XINERAMA_LIB})
   1.172          endif(HAVE_X11_SHARED AND XINERAMA_LIB)
   1.173 @@ -422,7 +442,7 @@
   1.174        if(VIDEO_X11_XINPUT AND HAVE_XINPUT_H)
   1.175          set(HAVE_VIDEO_X11_XINPUT TRUE)
   1.176          if(HAVE_X11_SHARED AND XI_LIB)
   1.177 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "\"${XI_LIB}\"")
   1.178 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "\"${XI_LIB_SONAME}\"")
   1.179          else(HAVE_X11_SHARED AND XI_LIB)
   1.180            list(APPEND EXTRA_LIBS ${XI_LIB})
   1.181          endif(HAVE_X11_SHARED AND XI_LIB)
   1.182 @@ -447,7 +467,7 @@
   1.183  
   1.184        if(VIDEO_X11_XRANDR AND HAVE_XRANDR_H)
   1.185          if(HAVE_X11_SHARED AND XRANDR_LIB)
   1.186 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "\"${XRANDR_LIB}\"")
   1.187 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "\"${XRANDR_LIB_SONAME}\"")
   1.188          else(HAVE_X11_SHARED AND XRANDR_LIB)
   1.189            list(APPEND EXTRA_LIBS ${XRANDR_LIB})
   1.190          endif(HAVE_X11_SHARED AND XRANDR_LIB)
   1.191 @@ -457,7 +477,7 @@
   1.192  
   1.193        if(VIDEO_X11_XSCRNSAVER AND HAVE_XSS_H)
   1.194          if(HAVE_X11_SHARED AND XSS_LIB)
   1.195 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "\"${XSS_LIB}\"")
   1.196 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "\"${XSS_LIB_SONAME}\"")
   1.197          else(HAVE_X11_SHARED AND XSS_LIB)
   1.198            list(APPEND EXTRA_LIBS ${XSS_LIB})
   1.199          endif(HAVE_X11_SHARED AND XSS_LIB)
   1.200 @@ -472,7 +492,7 @@
   1.201  
   1.202        if(VIDEO_X11_XVM AND HAVE_XF86VM_H)
   1.203          if(HAVE_X11_SHARED AND XF86VMODE_LIB)
   1.204 -          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "\"${XF86VMODE_LIB}\"")
   1.205 +          set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "\"${XF86VMODE_LIB_SONAME}\"")
   1.206          else(HAVE_X11_SHARED AND XF86VMODE_LIB)
   1.207            list(APPEND EXTRA_LIBS ${XF86VMODE_LIB})
   1.208          endif(HAVE_X11_SHARED AND XF86VMODE_LIB)
   1.209 @@ -522,9 +542,8 @@
   1.210          if(NOT HAVE_DLOPEN)
   1.211            message_warn("You must have SDL_LoadObject() support for dynamic DirectFB loading")
   1.212          else()
   1.213 -          find_library(D_DIRECTFB_LIB directfb)
   1.214 -          get_filename_component(F_DIRECTFB_LIB ${D_DIRECTFB_LIB} NAME)
   1.215 -          set(SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC "\"${F_DIRECTFB_LIB}\"")
   1.216 +          FindLibraryAndSONAME("directfb")
   1.217 +          set(SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC "\"${DIRECTFB_LIB_SONAME}\"")
   1.218            set(HAVE_DIRECTFB_SHARED TRUE)
   1.219          endif(NOT HAVE_DLOPEN)
   1.220        else(DIRECTFB_SHARED)