cmake/sdlchecks.cmake
author Ryan C. Gordon <icculus@icculus.org>
Tue, 19 Nov 2013 15:25:00 -0500
changeset 8017 d84a30e5cbd5
parent 8016 3746af784462
child 8062 4fc5f66d63cc
permissions -rw-r--r--
Fixed SONAME lookup for runtime loading of shared libraries in CMake project.

Fixes Bugzilla #2249.
     1 macro(FindLibraryAndSONAME _LIB)
     2   string(TOUPPER ${_LIB} _UPPERLNAME)
     3   string(REGEX REPLACE "\\-" "_" _LNAME "${_UPPERLNAME}")
     4 
     5   find_library(${_LNAME}_LIB ${_LIB})
     6   if(${_LNAME}_LIB)
     7     # reduce the library name for shared linking
     8 
     9     get_filename_component(_LIB_REALPATH ${${_LNAME}_LIB} REALPATH)  # resolves symlinks
    10     get_filename_component(_LIB_JUSTNAME ${_LIB_REALPATH} NAME)
    11 
    12     if(APPLE)
    13       string(REGEX REPLACE "(\\.[0-9]*)\\.[0-9\\.]*dylib$" "\\1.dylib" _LIB_REGEXD "${_LIB_JUSTNAME}")
    14     else()
    15       string(REGEX REPLACE "(\\.[0-9]*)\\.[0-9\\.]*$" "\\1" _LIB_REGEXD "${_LIB_JUSTNAME}")
    16     endif()
    17 
    18     SET(_DEBUG_FindSONAME FALSE)
    19     if(_DEBUG_FindSONAME)
    20       message_warn("DYNLIB OUTPUTVAR: ${_LIB} ... ${_LNAME}_LIB")
    21       message_warn("DYNLIB ORIGINAL LIB: ${_LIB} ... ${${_LNAME}_LIB}")
    22       message_warn("DYNLIB REALPATH LIB: ${_LIB} ... ${_LIB_REALPATH}")
    23       message_warn("DYNLIB JUSTNAME LIB: ${_LIB} ... ${_LIB_JUSTNAME}")
    24       message_warn("DYNLIB REGEX'd LIB: ${_LIB} ... ${_LIB_REGEXD}")
    25     endif()
    26 
    27     message(STATUS "dynamic lib${_LIB} -> ${_LIB_REGEXD}")
    28     set(${_LNAME}_LIB_SONAME ${_LIB_REGEXD})
    29   endif()
    30 endmacro()
    31 
    32 macro(CheckDLOPEN)
    33   check_function_exists(dlopen HAVE_DLOPEN)
    34   if(NOT HAVE_DLOPEN)
    35     foreach(_LIBNAME dl tdl)
    36       check_library_exists("${_LIBNAME}" "dlopen" "" DLOPEN_LIB)
    37       if(DLOPEN_LIB)
    38         list(APPEND EXTRA_LIBS ${_LIBNAME})
    39         set(_DLLIB ${_LIBNAME})
    40         set(HAVE_DLOPEN TRUE)
    41         break()
    42       endif(DLOPEN_LIB)
    43     endforeach()
    44   endif()
    45 
    46   if(HAVE_DLOPEN)
    47     if(_DLLIB)
    48       set(CMAKE_REQUIRED_LIBRARIES ${_DLLIB})
    49     endif()
    50     check_c_source_compiles("
    51        #include <dlfcn.h>
    52        int main(int argc, char **argv) {
    53          void *handle = dlopen(\"\", RTLD_NOW);
    54          const char *loaderror = (char *) dlerror();
    55        }" HAVE_DLOPEN)
    56     set(CMAKE_REQUIRED_LIBRARIES)
    57   endif()
    58 
    59   if (HAVE_DLOPEN)
    60     set(SDL_LOADSO_DLOPEN 1)
    61     set(HAVE_SDL_DLOPEN TRUE)
    62     file(GLOB DLOPEN_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/dlopen/*.c)
    63     set(SOURCE_FILES ${SOURCE_FILES} ${DLOPEN_SOURCES})
    64     set(HAVE_SDL_LOADSO TRUE)
    65   endif()
    66 endmacro(CheckDLOPEN)
    67 
    68 # Requires:
    69 # - n/a
    70 macro(CheckOSS)
    71   if(OSS)
    72     set(OSS_HEADER_FILE "sys/soundcard.h")
    73     check_c_source_compiles("
    74         #include <sys/soundcard.h>
    75         int main() { int arg = SNDCTL_DSP_SETFRAGMENT; }" OSS_FOUND)
    76     if(NOT OSS_FOUND)
    77       set(OSS_HEADER_FILE "soundcard.h")
    78       check_c_source_compiles("
    79           #include <soundcard.h>
    80           int main() { int arg = SNDCTL_DSP_SETFRAGMENT; }" OSS_FOUND)
    81     endif(NOT OSS_FOUND)
    82 
    83     if(OSS_FOUND)
    84       set(HAVE_OSS TRUE)
    85       file(GLOB OSS_SOURCES ${SDL2_SOURCE_DIR}/src/audio/dsp/*.c)
    86       if(OSS_HEADER_FILE STREQUAL "soundcard.h")
    87         set(SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H 1)
    88       endif(OSS_HEADER_FILE STREQUAL "soundcard.h")
    89       set(SDL_AUDIO_DRIVER_OSS 1)
    90       set(SOURCE_FILES ${SOURCE_FILES} ${OSS_SOURCES})
    91       if(NETBSD OR OPENBSD)
    92         list(APPEND EXTRA_LIBS ossaudio)
    93       endif(NETBSD OR OPENBSD)
    94       set(HAVE_SDL_AUDIO TRUE)
    95     endif(OSS_FOUND)
    96   endif(OSS)
    97 endmacro(CheckOSS)
    98 
    99 # Requires:
   100 # - n/a
   101 # Optional:
   102 # - ALSA_SHARED opt
   103 # - HAVE_DLOPEN opt
   104 macro(CheckALSA)
   105   if(ALSA)
   106     CHECK_INCLUDE_FILE(alsa/asoundlib.h HAVE_ASOUNDLIB_H)
   107     if(HAVE_ASOUNDLIB_H)
   108       CHECK_LIBRARY_EXISTS(asound snd_pcm_open "" HAVE_LIBASOUND)
   109       set(HAVE_ALSA TRUE)
   110       file(GLOB ALSA_SOURCES ${SDL2_SOURCE_DIR}/src/audio/alsa/*.c)
   111       set(SOURCE_FILES ${SOURCE_FILES} ${ALSA_SOURCES})
   112       set(SDL_AUDIO_DRIVER_ALSA 1)
   113       if(ALSA_SHARED)
   114         if(NOT HAVE_DLOPEN)
   115           message_warn("You must have SDL_LoadObject() support for dynamic ALSA loading")
   116         else()
   117           FindLibraryAndSONAME("asound")
   118           set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${ASOUND_LIB_SONAME}\"")
   119           set(HAVE_ALSA_SHARED TRUE)
   120         endif(NOT HAVE_DLOPEN)
   121       else(ALSA_SHARED)
   122         list(APPEND EXTRA_LIBS asound)
   123       endif(ALSA_SHARED)
   124       set(HAVE_SDL_AUDIO TRUE)
   125     endif(HAVE_ASOUNDLIB_H)
   126   endif(ALSA)
   127 endmacro(CheckALSA)
   128 
   129 # Requires:
   130 # - PkgCheckModules
   131 # Optional:
   132 # - PULSEAUDIO_SHARED opt
   133 # - HAVE_DLOPEN opt
   134 macro(CheckPulseAudio)
   135   if(PULSEAUDIO)
   136     pkg_check_modules(PKG_PULSEAUDIO libpulse-simple)
   137     if(PKG_PULSEAUDIO_FOUND)
   138       set(HAVE_PULSEAUDIO TRUE)
   139       file(GLOB PULSEAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/pulseaudio/*.c)
   140       set(SOURCE_FILES ${SOURCE_FILES} ${PULSEAUDIO_SOURCES})
   141       set(SDL_AUDIO_DRIVER_PULSEAUDIO 1)
   142       list(APPEND EXTRA_CFLAGS ${PKG_PULSEAUDIO_CFLAGS})
   143       if(PULSEAUDIO_SHARED)
   144         if(NOT HAVE_DLOPEN)
   145           message_warn("You must have SDL_LoadObject() support for dynamic PulseAudio loading")
   146         else()
   147           FindLibraryAndSONAME("pulse-simple")
   148           set(SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC "\"${PULSE_SIMPLE_LIB_SONAME}\"")
   149           set(HAVE_PULSEAUDIO_SHARED TRUE)
   150         endif(NOT HAVE_DLOPEN)
   151       else(PULSEAUDIO_SHARED)
   152         list(APPEND EXTRA_LDFLAGS ${PKG_PULSEAUDIO_LDFLAGS})
   153       endif(PULSEAUDIO_SHARED)
   154       set(HAVE_SDL_AUDIO TRUE)
   155     endif(PKG_PULSEAUDIO_FOUND)
   156   endif(PULSEAUDIO)
   157 endmacro(CheckPulseAudio)
   158 
   159 # Requires:
   160 # - PkgCheckModules
   161 # Optional:
   162 # - ESD_SHARED opt
   163 # - HAVE_DLOPEN opt
   164 macro(CheckESD)
   165   if(ESD)
   166     pkg_check_modules(PKG_ESD esound)
   167     if(PKG_ESD_FOUND)
   168       set(HAVE_ESD TRUE)
   169       file(GLOB ESD_SOURCES ${SDL2_SOURCE_DIR}/src/audio/esd/*.c)
   170       set(SOURCE_FILES ${SOURCE_FILES} ${ESD_SOURCES})
   171       set(SDL_AUDIO_DRIVER_ESD 1)
   172       list(APPEND EXTRA_CFLAGS ${PKG_ESD_CFLAGS})
   173       if(ESD_SHARED)
   174         if(NOT HAVE_DLOPEN)
   175           message_warn("You must have SDL_LoadObject() support for dynamic ESD loading")
   176         else()
   177           FindLibraryAndSONAME(esd)
   178           set(SDL_AUDIO_DRIVER_ESD_DYNAMIC "\"${ESD_LIB_SONAME}\"")
   179           set(HAVE_ESD_SHARED TRUE)
   180         endif(NOT HAVE_DLOPEN)
   181       else(ESD_SHARED)
   182         list(APPEND EXTRA_LDFLAGS ${PKG_ESD_LDFLAGS})
   183       endif(ESD_SHARED)
   184       set(HAVE_SDL_AUDIO TRUE)
   185     endif(PKG_ESD_FOUND)
   186   endif(ESD)
   187 endmacro(CheckESD)
   188 
   189 # Requires:
   190 # - n/a
   191 # Optional:
   192 # - ARTS_SHARED opt
   193 # - HAVE_DLOPEN opt
   194 macro(CheckARTS)
   195   if(ARTS)
   196     find_program(ARTS_CONFIG arts-config)
   197     if(ARTS_CONFIG)
   198       execute_process(CMD_ARTSCFLAGS ${ARTS_CONFIG} --cflags
   199         OUTPUT_VARIABLE ARTS_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
   200       list(APPEND EXTRA_CFLAGS ${ARTS_CFLAGS})
   201       execute_process(CMD_ARTSLIBS ${ARTS_CONFIG} --libs
   202         OUTPUT_VARIABLE ARTS_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE)
   203       file(GLOB ARTS_SOURCES ${SDL2_SOURCE_DIR}/src/audio/arts/*.c)
   204       set(SOURCE_FILES ${SOURCE_FILES} ${ARTS_SOURCES})
   205       set(SDL_AUDIO_DRIVER_ARTS 1)
   206       set(HAVE_ARTS TRUE)
   207       if(ARTS_SHARED)
   208         if(NOT HAVE_DLOPEN)
   209           message_warn("You must have SDL_LoadObject() support for dynamic ARTS loading")
   210         else()
   211           # TODO
   212           FindLibraryAndSONAME(artsc)
   213           set(SDL_AUDIO_DRIVER_ARTS_DYNAMIC "\"${ARTSC_LIB_SONAME}\"")
   214           set(HAVE_ARTS_SHARED TRUE)
   215         endif(NOT HAVE_DLOPEN)
   216       else(ARTS_SHARED)
   217         list(APPEND EXTRA_LDFLAGS ${ARTS_LIBS})
   218       endif(ARTS_SHARED)
   219       set(HAVE_SDL_AUDIO TRUE)
   220     endif(ARTS_CONFIG)
   221   endif(ARTS)
   222 endmacro(CheckARTS)
   223 
   224 # Requires:
   225 # - n/a
   226 # Optional:
   227 # - NAS_SHARED opt
   228 # - HAVE_DLOPEN opt
   229 macro(CheckNAS)
   230   if(NAS)
   231     # TODO: set include paths properly, so the NAS headers are found
   232     check_include_file(audio/audiolib.h HAVE_NAS_H)
   233     find_library(D_NAS_LIB audio)
   234     if(HAVE_NAS_H AND D_NAS_LIB)
   235       set(HAVE_NAS TRUE)
   236       file(GLOB NAS_SOURCES ${SDL2_SOURCE_DIR}/src/audio/nas/*.c)
   237       set(SOURCE_FILES ${SOURCE_FILES} ${NAS_SOURCES})
   238       set(SDL_AUDIO_DRIVER_NAS 1)
   239       if(NAS_SHARED)
   240         if(NOT HAVE_DLOPEN)
   241           message_warn("You must have SDL_LoadObject() support for dynamic NAS loading")
   242         else()
   243           FindLibraryAndSONAME("audio")
   244           set(SDL_AUDIO_DRIVER_NAS_DYNAMIC "\"${AUDIO_LIB_SONAME}\"")
   245           set(HAVE_NAS_SHARED TRUE)
   246         endif(NOT HAVE_DLOPEN)
   247       else(NAS_SHARED)
   248         list(APPEND EXTRA_LIBS ${D_NAS_LIB})
   249       endif(NAS_SHARED)
   250       set(HAVE_SDL_AUDIO TRUE)
   251     endif(HAVE_NAS_H AND D_NAS_LIB)
   252   endif(NAS)
   253 endmacro(CheckNAS)
   254 
   255 # Requires:
   256 # - n/a
   257 # Optional:
   258 # - SNDIO_SHARED opt
   259 # - HAVE_DLOPEN opt
   260 macro(CheckSNDIO)
   261   if(SNDIO)
   262     # TODO: set include paths properly, so the sndio headers are found
   263     check_include_file(sndio.h HAVE_SNDIO_H)
   264     find_library(D_SNDIO_LIB sndio)
   265     if(HAVE_SNDIO_H AND D_SNDIO_LIB)
   266       set(HAVE_SNDIO TRUE)
   267       file(GLOB SNDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/sndio/*.c)
   268       set(SOURCE_FILES ${SOURCE_FILES} ${SNDIO_SOURCES})
   269       set(SDL_AUDIO_DRIVER_SNDIO 1)
   270       if(SNDIO_SHARED)
   271         if(NOT HAVE_DLOPEN)
   272           message_warn("You must have SDL_LoadObject() support for dynamic sndio loading")
   273         else()
   274           FindLibraryAndSONAME("sndio")
   275           set(SDL_AUDIO_DRIVER_SNDIO_DYNAMIC "\"${SNDIO_LIB_SONAME}\"")
   276           set(HAVE_SNDIO_SHARED TRUE)
   277         endif(NOT HAVE_DLOPEN)
   278       else(SNDIO_SHARED)
   279         list(APPEND EXTRA_LIBS ${D_SNDIO_LIB})
   280       endif(SNDIO_SHARED)
   281       set(HAVE_SDL_AUDIO TRUE)
   282     endif(HAVE_SNDIO_H AND D_SNDIO_LIB)
   283   endif(SNDIO)
   284 endmacro(CheckSNDIO)
   285 
   286 # Requires:
   287 # - PkgCheckModules
   288 # Optional:
   289 # - FUSIONSOUND_SHARED opt
   290 # - HAVE_DLOPEN opt
   291 macro(CheckFusionSound)
   292   if(FUSIONSOUND)
   293     pkg_check_modules(PKG_FUSIONSOUND fusionsound>=1.0.0)
   294     if(PKG_FUSIONSOUND_FOUND)
   295       set(HAVE_FUSIONSOUND TRUE)
   296       file(GLOB FUSIONSOUND_SOURCES ${SDL2_SOURCE_DIR}/src/audio/fusionsound/*.c)
   297       set(SOURCE_FILES ${SOURCE_FILES} ${FUSIONSOUND_SOURCES})
   298       set(SDL_AUDIO_DRIVER_FUSIONSOUND 1)
   299       list(APPEND EXTRA_CFLAGS ${PKG_FUSIONSOUND_CFLAGS})
   300       if(FUSIONSOUND_SHARED)
   301         if(NOT HAVE_DLOPEN)
   302           message_warn("You must have SDL_LoadObject() support for dynamic FusionSound loading")
   303         else()
   304           FindLibraryAndSONAME("fusionsound")
   305           set(SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "\"${FUSIONSOUND_LIB_SONAME}\"")
   306           set(HAVE_FUSIONSOUND_SHARED TRUE)
   307         endif(NOT HAVE_DLOPEN)
   308       else(FUSIONSOUND_SHARED)
   309         list(APPEND EXTRA_LDFLAGS ${PKG_FUSIONSOUND_LDFLAGS})
   310       endif(FUSIONSOUND_SHARED)
   311       set(HAVE_SDL_AUDIO TRUE)
   312     endif(PKG_FUSIONSOUND_FOUND)
   313   endif(FUSIONSOUND)
   314 endmacro(CheckFusionSound)
   315 
   316 # Requires:
   317 # - n/a
   318 # Optional:
   319 # - X11_SHARED opt
   320 # - HAVE_DLOPEN opt
   321 macro(CheckX11)
   322   if(VIDEO_X11)
   323     foreach(_LIB X11 Xext Xcursor Xinerama Xi Xrandr Xrender Xss Xxf86vm)
   324         FindLibraryAndSONAME("${_LIB}")
   325     endforeach()
   326 
   327     find_path(X_INCLUDEDIR X11/Xlib.h)
   328     if(X_INCLUDEDIR)
   329       set(X_CFLAGS "-I${X_INCLUDEDIR}")
   330     endif()
   331 
   332     check_include_file(X11/Xcursor/Xcursor.h HAVE_XCURSOR_H)
   333     check_include_file(X11/extensions/Xinerama.h HAVE_XINERAMA_H)
   334     check_include_file(X11/extensions/XInput2.h HAVE_XINPUT_H)
   335     check_include_file(X11/extensions/Xrandr.h HAVE_XRANDR_H)
   336     check_include_file(X11/extensions/Xrender.h HAVE_XRENDER_H)
   337     check_include_file(X11/extensions/scrnsaver.h HAVE_XSS_H)
   338     check_include_file(X11/extensions/shape.h HAVE_XSHAPE_H)
   339     check_include_files("X11/Xlib.h;X11/extensions/xf86vmode.h" HAVE_XF86VM_H)
   340     check_include_files("X11/Xlib.h;X11/Xproto.h;X11/extensions/Xext.h" HAVE_XEXT_H)
   341 
   342     if(X11_LIB)
   343       if(NOT HAVE_XEXT_H)
   344         message_error("Missing Xext.h, maybe you need to install the libxext-dev package?")
   345       endif()
   346 
   347       set(HAVE_VIDEO_X11 TRUE)
   348       set(HAVE_SDL_VIDEO TRUE)
   349 
   350       file(GLOB X11_SOURCES ${SDL2_SOURCE_DIR}/src/video/x11/*.c)
   351       set(SOURCE_FILES ${SOURCE_FILES} ${X11_SOURCES})
   352       set(SDL_VIDEO_DRIVER_X11 1)
   353 
   354       if(APPLE)
   355         set(X11_SHARED OFF)
   356       endif(APPLE)
   357 
   358       check_function_exists("shmat" HAVE_SHMAT)
   359       if(NOT HAVE_SHMAT)
   360         check_library_exists(ipc shmat "" HAVE_SHMAT)
   361         if(HAVE_SHMAT)
   362           list(APPEND EXTRA_LIBS ipc)
   363         endif(HAVE_SHMAT)
   364         if(NOT HAVE_SHMAT)
   365           add_definitions(-DNO_SHARED_MEMORY)
   366           set(X_CFLAGS "${X_CFLAGS} -DNO_SHARED_MEMORY")
   367         endif(NOT HAVE_SHMAT)
   368       endif(NOT HAVE_SHMAT)
   369 
   370       if(X11_SHARED)
   371         if(NOT HAVE_DLOPEN)
   372           message_warn("You must have SDL_LoadObject() support for dynamic X11 loading")
   373           set(HAVE_X11_SHARED FALSE)
   374         else(NOT HAVE_DLOPEN)
   375           set(HAVE_X11_SHARED TRUE)
   376         endif()
   377         if(HAVE_X11_SHARED)
   378           set(SDL_VIDEO_DRIVER_X11_DYNAMIC "\"${X11_LIB_SONAME}\"")
   379           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "\"${XEXT_LIB_SONAME}\"")
   380         else(HAVE_X11_SHARED)
   381           list(APPEND EXTRA_LIBS ${X11_LIB} ${XEXT_LIB})
   382         endif(HAVE_X11_SHARED)
   383       endif(X11_SHARED)
   384 
   385       set(SDL_CFLAGS "${SDL_CFLAGS} ${X_CFLAGS}")
   386 
   387       set(CMAKE_REQUIRED_LIBRARIES ${X11_LIB} ${X11_LIB})
   388       check_c_source_compiles("
   389           #include <X11/Xlib.h>
   390           #include <X11/Xproto.h>
   391           #include <X11/extensions/Xext.h>
   392           #include <X11/extensions/extutil.h>
   393           extern XExtDisplayInfo* XextAddDisplay(XExtensionInfo* a,Display* b,_Xconst char* c,XExtensionHooks* d,int e,XPointer f);
   394           int main(int argc, char **argv) {}" HAVE_CONST_XEXT_ADDDISPLAY)
   395       if(HAVE_CONST_XEXT_ADDDISPLAY)
   396         set(SDL_VIDEO_DRIVER_X11_CONST_PARAM_XEXTADDDISPLAY 1)
   397       endif(HAVE_CONST_XEXT_ADDDISPLAY)
   398 
   399       check_c_source_compiles("
   400           #include <X11/Xlib.h>
   401           int main(int argc, char **argv) {
   402             Display *display;
   403             XEvent event;
   404             XGenericEventCookie *cookie = &event.xcookie;
   405             XNextEvent(display, &event);
   406             XGetEventData(display, cookie);
   407             XFreeEventData(display, cookie); }" HAVE_XGENERICEVENT)
   408       if(HAVE_XGENERICEVENT)
   409         set(SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS 1)
   410       endif(HAVE_XGENERICEVENT)
   411 
   412       check_c_source_compiles("
   413           #include <X11/Xlibint.h>
   414           extern int _XData32(Display *dpy,register _Xconst long *data,unsigned len);
   415           int main(int argc, char **argv) {}" HAVE_CONST_XDATA32)
   416       if(HAVE_CONST_XDATA32)
   417         set(SDL_VIDEO_DRIVER_X11_CONST_PARAM_XDATA32 1)
   418       endif(HAVE_CONST_XDATA32)
   419 
   420       check_function_exists(XkbKeycodeToKeysym SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM)
   421 
   422       if(VIDEO_X11_XCURSOR AND HAVE_XCURSOR_H)
   423         set(HAVE_VIDEO_X11_XCURSOR TRUE)
   424         if(HAVE_X11_SHARED AND XCURSOR_LIB)
   425           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "\"${XCURSOR_LIB_SONAME}\"")
   426         else(HAVE_X11_SHARED AND XCURSOR_LIB)
   427           list(APPEND EXTRA_LIBS ${XCURSOR_LIB})
   428         endif(HAVE_X11_SHARED AND XCURSOR_LIB)
   429         set(SDL_VIDEO_DRIVER_X11_XCURSOR 1)
   430       endif(VIDEO_X11_XCURSOR AND HAVE_XCURSOR_H)
   431 
   432       if(VIDEO_X11_XINERAMA AND HAVE_XINERAMA_H)
   433         set(HAVE_VIDEO_X11_XINERAMA TRUE)
   434         if(HAVE_X11_SHARED AND XINERAMA_LIB)
   435           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "\"${XINERAMA_LIB_SONAME}\"")
   436         else(HAVE_X11_SHARED AND XINERAMA_LIB)
   437           list(APPEND EXTRA_LIBS ${XINERAMA_LIB})
   438         endif(HAVE_X11_SHARED AND XINERAMA_LIB)
   439         set(SDL_VIDEO_DRIVER_X11_XINERAMA 1)
   440       endif(VIDEO_X11_XINERAMA AND HAVE_XINERAMA_H)
   441 
   442       if(VIDEO_X11_XINPUT AND HAVE_XINPUT_H)
   443         set(HAVE_VIDEO_X11_XINPUT TRUE)
   444         if(HAVE_X11_SHARED AND XI_LIB)
   445           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "\"${XI_LIB_SONAME}\"")
   446         else(HAVE_X11_SHARED AND XI_LIB)
   447           list(APPEND EXTRA_LIBS ${XI_LIB})
   448         endif(HAVE_X11_SHARED AND XI_LIB)
   449         set(SDL_VIDEO_DRIVER_X11_XINPUT2 1)
   450 
   451         # Check for multitouch
   452         check_c_source_compiles("
   453             #include <X11/Xlib.h>
   454             #include <X11/Xproto.h>
   455             #include <X11/extensions/XInput2.h>
   456             int event_type = XI_TouchBegin;
   457             XITouchClassInfo *t;
   458             Status XIAllowTouchEvents(Display *a,int b,unsigned int c,Window d,int f)
   459             {
   460               return (Status)0;
   461             }
   462             int main(int argc, char **argv) {}" HAVE_XINPUT2_MULTITOUCH)
   463         if(HAVE_XINPUT2_MULTITOUCH)
   464           set(SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH 1)
   465         endif(HAVE_XINPUT2_MULTITOUCH)
   466       endif(VIDEO_X11_XINPUT AND HAVE_XINPUT_H)
   467 
   468       if(VIDEO_X11_XRANDR AND HAVE_XRANDR_H)
   469         if(HAVE_X11_SHARED AND XRANDR_LIB)
   470           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "\"${XRANDR_LIB_SONAME}\"")
   471         else(HAVE_X11_SHARED AND XRANDR_LIB)
   472           list(APPEND EXTRA_LIBS ${XRANDR_LIB})
   473         endif(HAVE_X11_SHARED AND XRANDR_LIB)
   474         set(SDL_VIDEO_DRIVER_X11_XRANDR 1)
   475         set(HAVE_VIDEO_X11_XRANDR TRUE)
   476       endif(VIDEO_X11_XRANDR AND HAVE_XRANDR_H)
   477 
   478       if(VIDEO_X11_XSCRNSAVER AND HAVE_XSS_H)
   479         if(HAVE_X11_SHARED AND XSS_LIB)
   480           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "\"${XSS_LIB_SONAME}\"")
   481         else(HAVE_X11_SHARED AND XSS_LIB)
   482           list(APPEND EXTRA_LIBS ${XSS_LIB})
   483         endif(HAVE_X11_SHARED AND XSS_LIB)
   484         set(SDL_VIDEO_DRIVER_X11_XSCRNSAVER 1)
   485         set(HAVE_VIDEO_X11_XSCRNSAVER TRUE)
   486       endif(VIDEO_X11_XSCRNSAVER AND HAVE_XSS_H)
   487 
   488       if(VIDEO_X11_XSHAPE AND HAVE_XSHAPE_H)
   489         set(SDL_VIDEO_DRIVER_X11_XSHAPE 1)
   490         set(HAVE_VIDEO_X11_XSHAPE TRUE)
   491       endif(VIDEO_X11_XSHAPE AND HAVE_XSHAPE_H)
   492 
   493       if(VIDEO_X11_XVM AND HAVE_XF86VM_H)
   494         if(HAVE_X11_SHARED AND XF86VMODE_LIB)
   495           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "\"${XF86VMODE_LIB_SONAME}\"")
   496         else(HAVE_X11_SHARED AND XF86VMODE_LIB)
   497           list(APPEND EXTRA_LIBS ${XF86VMODE_LIB})
   498         endif(HAVE_X11_SHARED AND XF86VMODE_LIB)
   499         set(SDL_VIDEO_DRIVER_X11_XVIDMODE 1)
   500         set(HAVE_VIDEO_X11_XVM TRUE)
   501       endif(VIDEO_X11_XVM AND HAVE_XF86VM_H)
   502 
   503       set(CMAKE_REQUIRED_LIBRARIES)
   504     endif(X11_LIB)
   505   endif(VIDEO_X11)
   506 endmacro(CheckX11)
   507 
   508 # Requires:
   509 # - n/a
   510 #
   511 macro(CheckCOCOA)
   512   if(VIDEO_COCOA)
   513     check_objc_source_compiles("
   514         #import <Cocoa/Cocoa.h>
   515         int main (int argc, char** argv) {}" HAVE_VIDEO_COCOA)
   516     if(HAVE_VIDEO_COCOA)
   517       file(GLOB COCOA_SOURCES ${SDL2_SOURCE_DIR}/src/video/cocoa/*.m)
   518       set_source_files_properties(${COCOA_SOURCES} PROPERTIES LANGUAGE C)
   519       set(SOURCE_FILES ${SOURCE_FILES} ${COCOA_SOURCES})
   520       set(SDL_VIDEO_DRIVER_COCOA 1)
   521       set(HAVE_SDL_VIDEO TRUE)
   522     endif(HAVE_VIDEO_COCOA)
   523   endif(VIDEO_COCOA)
   524 endmacro(CheckCOCOA)
   525 
   526 # Requires:
   527 # - PkgCheckModules
   528 # Optional:
   529 # - DIRECTFB_SHARED opt
   530 # - HAVE_DLOPEN opt
   531 macro(CheckDirectFB)
   532   if(VIDEO_DIRECTFB)
   533     pkg_check_modules(PKG_DIRECTFB directfb>=1.0.0)
   534     if(PKG_DIRECTFB_FOUND)
   535       set(HAVE_VIDEO_DIRECTFB TRUE)
   536       file(GLOB DIRECTFB_SOURCES ${SDL2_SOURCE_DIR}/src/video/directfb/*.c)
   537       set(SOURCE_FILES ${SOURCE_FILES} ${DIRECTFB_SOURCES})
   538       set(SDL_VIDEO_DRIVER_DIRECTFB 1)
   539       set(SDL_VIDEO_RENDER_DIRECTFB 1)
   540       list(APPEND EXTRA_CFLAGS ${PKG_DIRECTFB_CFLAGS})
   541       if(DIRECTFB_SHARED)
   542         if(NOT HAVE_DLOPEN)
   543           message_warn("You must have SDL_LoadObject() support for dynamic DirectFB loading")
   544         else()
   545           FindLibraryAndSONAME("directfb")
   546           set(SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC "\"${DIRECTFB_LIB_SONAME}\"")
   547           set(HAVE_DIRECTFB_SHARED TRUE)
   548         endif(NOT HAVE_DLOPEN)
   549       else(DIRECTFB_SHARED)
   550         list(APPEND EXTRA_LDFLAGS ${PKG_DIRECTFB_LDFLAGS})
   551       endif(DIRECTFB_SHARED)
   552       set(HAVE_SDL_VIDEO TRUE)
   553     endif(PKG_DIRECTFB_FOUND)
   554   endif(VIDEO_DIRECTFB)
   555 endmacro(CheckDirectFB)
   556 
   557 # Requires:
   558 # - nada
   559 macro(CheckOpenGLX11)
   560   if(VIDEO_OPENGL)
   561     check_c_source_compiles("
   562         #include <GL/gl.h>
   563         #include <GL/glx.h>
   564         int main(int argc, char** argv) {}" HAVE_VIDEO_OPENGL)
   565 
   566     if(HAVE_VIDEO_OPENGL)
   567       set(HAVE_VIDEO_OPENGL TRUE)
   568       set(SDL_VIDEO_OPENGL 1)
   569       set(SDL_VIDEO_OPENGL_GLX 1)
   570       set(SDL_VIDEO_RENDER_OGL 1)
   571       list(APPEND EXTRA_LIBS GL)
   572     endif(HAVE_VIDEO_OPENGL)
   573   endif(VIDEO_OPENGL)
   574 endmacro(CheckOpenGLX11)
   575 
   576 # Requires:
   577 # - nada
   578 macro(CheckOpenGLESX11)
   579   if(VIDEO_OPENGLES)
   580     check_c_source_compiles("
   581         #include <EGL/egl.h>
   582         int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGL_EGL)
   583     if(HAVE_VIDEO_OPENGL_EGL)
   584         set(SDL_VIDEO_OPENGL_EGL 1)
   585     endif(HAVE_VIDEO_OPENGL_EGL) 
   586     check_c_source_compiles("
   587       #include <GLES/gl.h>
   588       #include <GLES/glext.h>
   589       int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGLES_V1)
   590     if(HAVE_VIDEO_OPENGLES_V1)
   591         set(HAVE_VIDEO_OPENGLES TRUE)
   592         set(SDL_VIDEO_OPENGL_ES 1)
   593         set(SDL_VIDEO_RENDER_OGL_ES 1)
   594     endif(HAVE_VIDEO_OPENGLES_V1)
   595     check_c_source_compiles("
   596       #include <GLES2/gl2.h>
   597       #include <GLES2/gl2ext.h>
   598       int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGLES_V2)
   599     if(HAVE_VIDEO_OPENGLES_V2)
   600         set(HAVE_VIDEO_OPENGLES TRUE)
   601         set(SDL_VIDEO_OPENGL_ES2 1)
   602         set(SDL_VIDEO_RENDER_OGL_ES2 1)
   603     endif(HAVE_VIDEO_OPENGLES_V2)
   604 
   605   endif(VIDEO_OPENGLES)
   606 endmacro(CheckOpenGLESX11)
   607 
   608 # Rquires:
   609 # - nada
   610 # Optional:
   611 # - THREADS opt
   612 # Sets:
   613 # PTHREAD_CFLAGS
   614 # PTHREAD_LIBS
   615 macro(CheckPTHREAD)
   616   if(PTHREADS)
   617     if(LINUX)
   618       set(PTHREAD_CFLAGS "-D_REENTRANT")
   619       set(PTHREAD_LDFLAGS "-pthread")
   620     elseif(BSDI)
   621       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   622       set(PTHREAD_LDFLAGS "")
   623     elseif(DARWIN)
   624       set(PTHREAD_CFLAGS "-D_THREAD_SAFE")
   625       # causes Carbon.p complaints?
   626       # set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   627       set(PTHREAD_LDFLAGS "")
   628     elseif(FREEBSD)
   629       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   630       set(PTHREAD_LDFLAGS "-pthread")
   631     elseif(NETBSD)
   632       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   633       set(PTHREAD_LDFLAGS "-lpthread")
   634     elseif(OPENBSD)
   635       set(PTHREAD_CFLAGS "-D_REENTRANT")
   636       set(PTHREAD_LDFLAGS "-pthread")
   637     elseif(SOLARIS)
   638       set(PTHREAD_CFLAGS "-D_REENTRANT")
   639       set(PTHREAD_LDFLAGS "-pthread -lposix4")
   640     elseif(SYSV5)
   641       set(PTHREAD_CFLAGS "-D_REENTRANT -Kthread")
   642       set(PTHREAD_LDFLAGS "")
   643     elseif(AIX)
   644       set(PTHREAD_CFLAGS "-D_REENTRANT -mthreads")
   645       set(PTHREAD_LDFLAGS "-pthread")
   646     elseif(HPUX)
   647       set(PTHREAD_CFLAGS "-D_REENTRANT")
   648       set(PTHREAD_LDFLAGS "-L/usr/lib -pthread")
   649     elseif(HAIKU)
   650       set(PTHREAD_CFLAGS "-D_REENTRANT")
   651       set(PTHREAD_LDFLAGS "")
   652     else()
   653       set(PTHREAD_CFLAGS "-D_REENTRANT")
   654       set(PTHREAD_LDFLAGS "-lpthread")
   655     endif(LINUX)
   656 
   657     # Run some tests
   658     set(CMAKE_REQUIRED_FLAGS "${PTHREAD_CFLAGS} ${PTHREAD_LDFLAGS}")
   659     check_c_source_runs("
   660         #include <pthread.h>
   661         int main(int argc, char** argv) {
   662           pthread_attr_t type;
   663           pthread_attr_init(&type);
   664           return 0;
   665         }" HAVE_PTHREADS)
   666     if(HAVE_PTHREADS)
   667       set(SDL_THREAD_PTHREAD 1)
   668       list(APPEND EXTRA_CFLAGS ${PTHREAD_CFLAGS})
   669       list(APPEND EXTRA_LDFLAGS ${PTHREAD_LDFLAGS})
   670       set(SDL_CFLAGS "${SDL_CFLAGS} ${PTHREAD_CFLAGS}")
   671       list(APPEND SDL_LIBS ${PTHREAD_LDFLAGS})
   672 
   673       check_c_source_compiles("
   674         #include <pthread.h>
   675         int main(int argc, char **argv) {
   676           pthread_mutexattr_t attr;
   677           pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
   678           return 0;
   679         }" HAVE_RECURSIVE_MUTEXES)
   680       if(HAVE_RECURSIVE_MUTEXES)
   681         set(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 1)
   682       else(HAVE_RECURSIVE_MUTEXES)
   683         check_c_source_compiles("
   684             #include <pthread.h>
   685             int main(int argc, char **argv) {
   686               pthread_mutexattr_t attr;
   687               pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
   688               return 0;
   689             }" HAVE_RECURSIVE_MUTEXES_NP)
   690         if(HAVE_RECURSIVE_MUTEXES_NP)
   691           set(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP 1)
   692         endif(HAVE_RECURSIVE_MUTEXES_NP)
   693       endif(HAVE_RECURSIVE_MUTEXES)
   694 
   695       if(PTHREADS_SEM)
   696         check_c_source_compiles("#include <pthread.h>
   697                                  #include <semaphore.h>
   698                                  int main(int argc, char **argv) { return 0; }" HAVE_PTHREADS_SEM)
   699         if(HAVE_PTHREADS_SEM)
   700           check_c_source_compiles("
   701               #include <pthread.h>
   702               #include <semaphore.h>
   703               int main(int argc, char **argv) {
   704                   sem_timedwait(NULL, NULL);
   705                   return 0;
   706               }" HAVE_SEM_TIMEDWAIT)
   707         endif(HAVE_PTHREADS_SEM)
   708       endif(PTHREADS_SEM)
   709 
   710       check_c_source_compiles("
   711           #include <pthread.h>
   712           int main(int argc, char** argv) {
   713               pthread_spin_trylock(NULL);
   714               return 0;
   715           }" HAVE_PTHREAD_SPINLOCK)
   716 
   717       check_c_source_compiles("
   718           #include <pthread.h>
   719           #include <pthread_np.h>
   720           int main(int argc, char** argv) { return 0; }" HAVE_PTHREAD_NP_H)
   721       check_function_exists(pthread_setname_np HAVE_PTHREAD_setNAME_NP)
   722       check_function_exists(pthread_set_name_np HAVE_PTHREAD_set_NAME_NP)
   723       set(CMAKE_REQUIRED_FLAGS)
   724 
   725       set(SOURCE_FILES ${SOURCE_FILES}
   726           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_systhread.c
   727           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_sysmutex.c   # Can be faked, if necessary
   728           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_syscond.c    # Can be faked, if necessary
   729           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_systls.c
   730           )
   731       if(HAVE_PTHREADS_SEM)
   732         set(SOURCE_FILES ${SOURCE_FILES}
   733             ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_syssem.c)
   734       else(HAVE_PTHREADS_SEM)
   735         set(SOURCE_FILES ${SOURCE_FILES}
   736             ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_syssem.c)
   737       endif(HAVE_PTHREADS_SEM)
   738       set(HAVE_SDL_THREADS TRUE)
   739     endif(HAVE_PTHREADS)
   740   endif(PTHREADS)
   741 endmacro(CheckPTHREAD)
   742 
   743 # Requires
   744 # - nada
   745 # Optional:
   746 # Sets:
   747 # USB_LIBS
   748 # USB_CFLAGS
   749 macro(CheckUSBHID)
   750   check_library_exists(usbhid hid_init "" LIBUSBHID)
   751   if(LIBUSBHID)
   752     check_include_file(usbhid.h HAVE_USBHID_H)
   753     if(HAVE_USBHID_H)
   754       set(USB_CFLAGS "-DHAVE_USBHID_H")
   755     endif(HAVE_USBHID_H)
   756 
   757     check_include_file(libusbhid.h HAVE_LIBUSBHID_H)
   758     if(HAVE_LIBUSBHID_H)
   759       set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSBHID_H")
   760     endif(HAVE_LIBUSBHID_H)
   761     set(USB_LIBS ${USB_LIBS} usbhid)
   762   else(LIBUSBHID)
   763     check_include_file(usb.h HAVE_USB_H)
   764     if(HAVE_USB_H)
   765       set(USB_CFLAGS "-DHAVE_USB_H")
   766     endif(HAVE_USB_H)
   767     check_include_file(libusb.h HAVE_LIBUSB_H)
   768     if(HAVE_LIBUSB_H)
   769       set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSB_H")
   770     endif(HAVE_LIBUSB_H)
   771     check_library_exists(usb hid_init "" LIBUSB)
   772     if(LIBUSB)
   773       set(USB_LIBS ${USB_LIBS} usb)
   774     endif(LIBUSB)
   775   endif(LIBUSBHID)
   776 
   777   set(CMAKE_REQUIRED_FLAGS "${USB_CFLAGS}")
   778   set(CMAKE_REQUIRED_LIBRARIES "${USB_LIBS}")
   779   check_c_source_compiles("
   780        #include <sys/types.h>
   781         #if defined(HAVE_USB_H)
   782         #include <usb.h>
   783         #endif
   784         #ifdef __DragonFly__
   785         # include <bus/usb/usb.h>
   786         # include <bus/usb/usbhid.h>
   787         #else
   788         # include <dev/usb/usb.h>
   789         # include <dev/usb/usbhid.h>
   790         #endif
   791         #if defined(HAVE_USBHID_H)
   792         #include <usbhid.h>
   793         #elif defined(HAVE_LIBUSB_H)
   794         #include <libusb.h>
   795         #elif defined(HAVE_LIBUSBHID_H)
   796         #include <libusbhid.h>
   797         #endif
   798         int main(int argc, char **argv) {
   799           struct report_desc *repdesc;
   800           struct usb_ctl_report *repbuf;
   801           hid_kind_t hidkind;
   802           return 0;
   803         }" HAVE_USBHID)
   804   if(HAVE_USBHID)
   805     check_c_source_compiles("
   806           #include <sys/types.h>
   807           #if defined(HAVE_USB_H)
   808           #include <usb.h>
   809           #endif
   810           #ifdef __DragonFly__
   811           # include <bus/usb/usb.h>
   812           # include <bus/usb/usbhid.h>
   813           #else
   814           # include <dev/usb/usb.h>
   815           # include <dev/usb/usbhid.h>
   816           #endif
   817           #if defined(HAVE_USBHID_H)
   818           #include <usbhid.h>
   819           #elif defined(HAVE_LIBUSB_H)
   820           #include <libusb.h>
   821           #elif defined(HAVE_LIBUSBHID_H)
   822           #include <libusbhid.h>
   823           #endif
   824           int main(int argc, char** argv) {
   825             struct usb_ctl_report buf;
   826             if (buf.ucr_data) { }
   827             return 0;
   828           }" HAVE_USBHID_UCR_DATA)
   829     if(HAVE_USBHID_UCR_DATA)
   830       set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_UCR_DATA")
   831     endif(HAVE_USBHID_UCR_DATA)
   832 
   833     check_c_source_compiles("
   834           #include <sys/types.h>
   835           #if defined(HAVE_USB_H)
   836           #include <usb.h>
   837           #endif
   838           #ifdef __DragonFly__
   839           #include <bus/usb/usb.h>
   840           #include <bus/usb/usbhid.h>
   841           #else
   842           #include <dev/usb/usb.h>
   843           #include <dev/usb/usbhid.h>
   844           #endif
   845           #if defined(HAVE_USBHID_H)
   846           #include <usbhid.h>
   847           #elif defined(HAVE_LIBUSB_H)
   848           #include <libusb.h>
   849           #elif defined(HAVE_LIBUSBHID_H)
   850           #include <libusbhid.h>
   851           #endif
   852           int main(int argc, char **argv) {
   853             report_desc_t d;
   854             hid_start_parse(d, 1, 1);
   855             return 0;
   856           }" HAVE_USBHID_NEW)
   857     if(HAVE_USBHID_NEW)
   858       set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_NEW")
   859     endif(HAVE_USBHID_NEW)
   860 
   861     check_c_source_compiles("
   862         #include <machine/joystick.h>
   863         int main(int argc, char** argv) {
   864             struct joystick t;
   865             return 0;
   866         }" HAVE_MACHINE_JOYSTICK)
   867     if(HAVE_MACHINE_JOYSTICK)
   868       set(SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H 1)
   869     endif(HAVE_MACHINE_JOYSTICK)
   870     set(SDL_JOYSTICK_USBHID 1)
   871     file(GLOB BSD_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/bsd/*.c)
   872     set(SOURCE_FILES ${SOURCE_FILES} ${BSD_JOYSTICK_SOURCES})
   873     list(APPEND EXTRA_CFLAGS ${USB_CFLAGS})
   874     list(APPEND EXTRA_LIBS ${USB_LIBS})
   875     set(HAVE_SDL_JOYSTICK TRUE)
   876 
   877     set(CMAKE_REQUIRED_LIBRARIES)
   878     set(CMAKE_REQUIRED_FLAGS)
   879   endif(HAVE_USBHID)
   880 endmacro(CheckUSBHID)