cmake/sdlchecks.cmake
author Sam Lantinga <slouken@libsdl.org>
Wed, 23 May 2018 17:15:35 -0700
changeset 11999 f3fc4b3d54f6
parent 11871 c207c3ab46ee
child 12030 c0c4ba22a685
permissions -rw-r--r--
Better fix for axis sorting with some Android controllers
     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()
    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()
    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()
    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()
    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()
    94       set(HAVE_SDL_AUDIO TRUE)
    95     endif()
    96   endif()
    97 endmacro()
    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_recover "" HAVE_LIBASOUND)
   109     endif()
   110     if(HAVE_LIBASOUND)
   111       set(HAVE_ALSA TRUE)
   112       file(GLOB ALSA_SOURCES ${SDL2_SOURCE_DIR}/src/audio/alsa/*.c)
   113       set(SOURCE_FILES ${SOURCE_FILES} ${ALSA_SOURCES})
   114       set(SDL_AUDIO_DRIVER_ALSA 1)
   115       if(ALSA_SHARED)
   116         if(NOT HAVE_DLOPEN)
   117           message_warn("You must have SDL_LoadObject() support for dynamic ALSA loading")
   118         else()
   119           FindLibraryAndSONAME("asound")
   120           set(SDL_AUDIO_DRIVER_ALSA_DYNAMIC "\"${ASOUND_LIB_SONAME}\"")
   121           set(HAVE_ALSA_SHARED TRUE)
   122         endif()
   123       else()
   124         list(APPEND EXTRA_LIBS asound)
   125       endif()
   126       set(HAVE_SDL_AUDIO TRUE)
   127     endif()
   128   endif()
   129 endmacro()
   130 
   131 # Requires:
   132 # - PkgCheckModules
   133 # Optional:
   134 # - PULSEAUDIO_SHARED opt
   135 # - HAVE_DLOPEN opt
   136 macro(CheckPulseAudio)
   137   if(PULSEAUDIO)
   138     pkg_check_modules(PKG_PULSEAUDIO libpulse-simple)
   139     if(PKG_PULSEAUDIO_FOUND)
   140       set(HAVE_PULSEAUDIO TRUE)
   141       file(GLOB PULSEAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/pulseaudio/*.c)
   142       set(SOURCE_FILES ${SOURCE_FILES} ${PULSEAUDIO_SOURCES})
   143       set(SDL_AUDIO_DRIVER_PULSEAUDIO 1)
   144       list(APPEND EXTRA_CFLAGS ${PKG_PULSEAUDIO_CFLAGS})
   145       if(PULSEAUDIO_SHARED)
   146         if(NOT HAVE_DLOPEN)
   147           message_warn("You must have SDL_LoadObject() support for dynamic PulseAudio loading")
   148         else()
   149           FindLibraryAndSONAME("pulse-simple")
   150           set(SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC "\"${PULSE_SIMPLE_LIB_SONAME}\"")
   151           set(HAVE_PULSEAUDIO_SHARED TRUE)
   152         endif()
   153       else()
   154         list(APPEND EXTRA_LDFLAGS ${PKG_PULSEAUDIO_LDFLAGS})
   155       endif()
   156       set(HAVE_SDL_AUDIO TRUE)
   157     endif()
   158   endif()
   159 endmacro()
   160 
   161 # Requires:
   162 # - PkgCheckModules
   163 # Optional:
   164 # - JACK_SHARED opt
   165 # - HAVE_DLOPEN opt
   166 macro(CheckJACK)
   167   if(JACK)
   168     pkg_check_modules(PKG_JACK jack)
   169     if(PKG_JACK_FOUND)
   170       set(HAVE_JACK TRUE)
   171       file(GLOB JACK_SOURCES ${SDL2_SOURCE_DIR}/src/audio/jack/*.c)
   172       set(SOURCE_FILES ${SOURCE_FILES} ${JACK_SOURCES})
   173       set(SDL_AUDIO_DRIVER_JACK 1)
   174       list(APPEND EXTRA_CFLAGS ${PKG_JACK_CFLAGS})
   175       if(JACK_SHARED)
   176         if(NOT HAVE_DLOPEN)
   177           message_warn("You must have SDL_LoadObject() support for dynamic JACK audio loading")
   178         else()
   179           FindLibraryAndSONAME("jack")
   180           set(SDL_AUDIO_DRIVER_JACK_DYNAMIC "\"${JACK_LIB_SONAME}\"")
   181           set(HAVE_JACK_SHARED TRUE)
   182         endif()
   183       else()
   184         list(APPEND EXTRA_LDFLAGS ${PKG_JACK_LDFLAGS})
   185       endif()
   186       set(HAVE_SDL_AUDIO TRUE)
   187     endif()
   188   endif()
   189 endmacro()
   190 
   191 # Requires:
   192 # - PkgCheckModules
   193 # Optional:
   194 # - ESD_SHARED opt
   195 # - HAVE_DLOPEN opt
   196 macro(CheckESD)
   197   if(ESD)
   198     pkg_check_modules(PKG_ESD esound)
   199     if(PKG_ESD_FOUND)
   200       set(HAVE_ESD TRUE)
   201       file(GLOB ESD_SOURCES ${SDL2_SOURCE_DIR}/src/audio/esd/*.c)
   202       set(SOURCE_FILES ${SOURCE_FILES} ${ESD_SOURCES})
   203       set(SDL_AUDIO_DRIVER_ESD 1)
   204       list(APPEND EXTRA_CFLAGS ${PKG_ESD_CFLAGS})
   205       if(ESD_SHARED)
   206         if(NOT HAVE_DLOPEN)
   207           message_warn("You must have SDL_LoadObject() support for dynamic ESD loading")
   208         else()
   209           FindLibraryAndSONAME(esd)
   210           set(SDL_AUDIO_DRIVER_ESD_DYNAMIC "\"${ESD_LIB_SONAME}\"")
   211           set(HAVE_ESD_SHARED TRUE)
   212         endif()
   213       else()
   214         list(APPEND EXTRA_LDFLAGS ${PKG_ESD_LDFLAGS})
   215       endif()
   216       set(HAVE_SDL_AUDIO TRUE)
   217     endif()
   218   endif()
   219 endmacro()
   220 
   221 # Requires:
   222 # - n/a
   223 # Optional:
   224 # - ARTS_SHARED opt
   225 # - HAVE_DLOPEN opt
   226 macro(CheckARTS)
   227   if(ARTS)
   228     find_program(ARTS_CONFIG arts-config)
   229     if(ARTS_CONFIG)
   230       execute_process(CMD_ARTSCFLAGS ${ARTS_CONFIG} --cflags
   231         OUTPUT_VARIABLE ARTS_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
   232       list(APPEND EXTRA_CFLAGS ${ARTS_CFLAGS})
   233       execute_process(CMD_ARTSLIBS ${ARTS_CONFIG} --libs
   234         OUTPUT_VARIABLE ARTS_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE)
   235       file(GLOB ARTS_SOURCES ${SDL2_SOURCE_DIR}/src/audio/arts/*.c)
   236       set(SOURCE_FILES ${SOURCE_FILES} ${ARTS_SOURCES})
   237       set(SDL_AUDIO_DRIVER_ARTS 1)
   238       set(HAVE_ARTS TRUE)
   239       if(ARTS_SHARED)
   240         if(NOT HAVE_DLOPEN)
   241           message_warn("You must have SDL_LoadObject() support for dynamic ARTS loading")
   242         else()
   243           # TODO
   244           FindLibraryAndSONAME(artsc)
   245           set(SDL_AUDIO_DRIVER_ARTS_DYNAMIC "\"${ARTSC_LIB_SONAME}\"")
   246           set(HAVE_ARTS_SHARED TRUE)
   247         endif()
   248       else()
   249         list(APPEND EXTRA_LDFLAGS ${ARTS_LIBS})
   250       endif()
   251       set(HAVE_SDL_AUDIO TRUE)
   252     endif()
   253   endif()
   254 endmacro()
   255 
   256 # Requires:
   257 # - n/a
   258 # Optional:
   259 # - NAS_SHARED opt
   260 # - HAVE_DLOPEN opt
   261 macro(CheckNAS)
   262   if(NAS)
   263     # TODO: set include paths properly, so the NAS headers are found
   264     check_include_file(audio/audiolib.h HAVE_NAS_H)
   265     find_library(D_NAS_LIB audio)
   266     if(HAVE_NAS_H AND D_NAS_LIB)
   267       set(HAVE_NAS TRUE)
   268       file(GLOB NAS_SOURCES ${SDL2_SOURCE_DIR}/src/audio/nas/*.c)
   269       set(SOURCE_FILES ${SOURCE_FILES} ${NAS_SOURCES})
   270       set(SDL_AUDIO_DRIVER_NAS 1)
   271       if(NAS_SHARED)
   272         if(NOT HAVE_DLOPEN)
   273           message_warn("You must have SDL_LoadObject() support for dynamic NAS loading")
   274         else()
   275           FindLibraryAndSONAME("audio")
   276           set(SDL_AUDIO_DRIVER_NAS_DYNAMIC "\"${AUDIO_LIB_SONAME}\"")
   277           set(HAVE_NAS_SHARED TRUE)
   278         endif()
   279       else()
   280         list(APPEND EXTRA_LIBS ${D_NAS_LIB})
   281       endif()
   282       set(HAVE_SDL_AUDIO TRUE)
   283     endif()
   284   endif()
   285 endmacro()
   286 
   287 # Requires:
   288 # - n/a
   289 # Optional:
   290 # - SNDIO_SHARED opt
   291 # - HAVE_DLOPEN opt
   292 macro(CheckSNDIO)
   293   if(SNDIO)
   294     # TODO: set include paths properly, so the sndio headers are found
   295     check_include_file(sndio.h HAVE_SNDIO_H)
   296     find_library(D_SNDIO_LIB sndio)
   297     if(HAVE_SNDIO_H AND D_SNDIO_LIB)
   298       set(HAVE_SNDIO TRUE)
   299       file(GLOB SNDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/sndio/*.c)
   300       set(SOURCE_FILES ${SOURCE_FILES} ${SNDIO_SOURCES})
   301       set(SDL_AUDIO_DRIVER_SNDIO 1)
   302       if(SNDIO_SHARED)
   303         if(NOT HAVE_DLOPEN)
   304           message_warn("You must have SDL_LoadObject() support for dynamic sndio loading")
   305         else()
   306           FindLibraryAndSONAME("sndio")
   307           set(SDL_AUDIO_DRIVER_SNDIO_DYNAMIC "\"${SNDIO_LIB_SONAME}\"")
   308           set(HAVE_SNDIO_SHARED TRUE)
   309         endif()
   310       else()
   311         list(APPEND EXTRA_LIBS ${D_SNDIO_LIB})
   312       endif()
   313       set(HAVE_SDL_AUDIO TRUE)
   314     endif()
   315   endif()
   316 endmacro()
   317 
   318 # Requires:
   319 # - PkgCheckModules
   320 # Optional:
   321 # - FUSIONSOUND_SHARED opt
   322 # - HAVE_DLOPEN opt
   323 macro(CheckFusionSound)
   324   if(FUSIONSOUND)
   325     pkg_check_modules(PKG_FUSIONSOUND fusionsound>=1.0.0)
   326     if(PKG_FUSIONSOUND_FOUND)
   327       set(HAVE_FUSIONSOUND TRUE)
   328       file(GLOB FUSIONSOUND_SOURCES ${SDL2_SOURCE_DIR}/src/audio/fusionsound/*.c)
   329       set(SOURCE_FILES ${SOURCE_FILES} ${FUSIONSOUND_SOURCES})
   330       set(SDL_AUDIO_DRIVER_FUSIONSOUND 1)
   331       list(APPEND EXTRA_CFLAGS ${PKG_FUSIONSOUND_CFLAGS})
   332       if(FUSIONSOUND_SHARED)
   333         if(NOT HAVE_DLOPEN)
   334           message_warn("You must have SDL_LoadObject() support for dynamic FusionSound loading")
   335         else()
   336           FindLibraryAndSONAME("fusionsound")
   337           set(SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "\"${FUSIONSOUND_LIB_SONAME}\"")
   338           set(HAVE_FUSIONSOUND_SHARED TRUE)
   339         endif()
   340       else()
   341         list(APPEND EXTRA_LDFLAGS ${PKG_FUSIONSOUND_LDFLAGS})
   342       endif()
   343       set(HAVE_SDL_AUDIO TRUE)
   344     endif()
   345   endif()
   346 endmacro()
   347 
   348 # Requires:
   349 # - LIBSAMPLERATE
   350 # Optional:
   351 # - LIBSAMPLERATE_SHARED opt
   352 # - HAVE_DLOPEN opt
   353 macro(CheckLibSampleRate)
   354   if(LIBSAMPLERATE)
   355     check_include_file(samplerate.h HAVE_LIBSAMPLERATE_H)
   356     if(HAVE_LIBSAMPLERATE_H)
   357       set(HAVE_LIBSAMPLERATE TRUE)
   358       if(LIBSAMPLERATE_SHARED)
   359         if(NOT HAVE_DLOPEN)
   360           message_warn("You must have SDL_LoadObject() support for dynamic libsamplerate loading")
   361         else()
   362           FindLibraryAndSONAME("samplerate")
   363           set(SDL_LIBSAMPLERATE_DYNAMIC "\"${SAMPLERATE_LIB_SONAME}\"")
   364           set(HAVE_LIBSAMPLERATE_SHARED TRUE)
   365         endif()
   366       else()
   367         list(APPEND EXTRA_LDFLAGS -lsamplerate)
   368       endif()
   369     endif()
   370   endif()
   371 endmacro()
   372 
   373 # Requires:
   374 # - n/a
   375 # Optional:
   376 # - X11_SHARED opt
   377 # - HAVE_DLOPEN opt
   378 macro(CheckX11)
   379   if(VIDEO_X11)
   380     foreach(_LIB X11 Xext Xcursor Xinerama Xi Xrandr Xrender Xss Xxf86vm)
   381         FindLibraryAndSONAME("${_LIB}")
   382     endforeach()
   383 
   384     find_path(X_INCLUDEDIR X11/Xlib.h)
   385     if(X_INCLUDEDIR)
   386       set(X_CFLAGS "-I${X_INCLUDEDIR}")
   387     endif()
   388 
   389     check_include_file(X11/Xcursor/Xcursor.h HAVE_XCURSOR_H)
   390     check_include_file(X11/extensions/Xinerama.h HAVE_XINERAMA_H)
   391     check_include_file(X11/extensions/XInput2.h HAVE_XINPUT_H)
   392     check_include_file(X11/extensions/Xrandr.h HAVE_XRANDR_H)
   393     check_include_file(X11/extensions/Xrender.h HAVE_XRENDER_H)
   394     check_include_file(X11/extensions/scrnsaver.h HAVE_XSS_H)
   395     check_include_file(X11/extensions/shape.h HAVE_XSHAPE_H)
   396     check_include_files("X11/Xlib.h;X11/extensions/xf86vmode.h" HAVE_XF86VM_H)
   397     check_include_files("X11/Xlib.h;X11/Xproto.h;X11/extensions/Xext.h" HAVE_XEXT_H)
   398 
   399     if(X11_LIB)
   400       if(NOT HAVE_XEXT_H)
   401         message_error("Missing Xext.h, maybe you need to install the libxext-dev package?")
   402       endif()
   403 
   404       set(HAVE_VIDEO_X11 TRUE)
   405       set(HAVE_SDL_VIDEO TRUE)
   406 
   407       file(GLOB X11_SOURCES ${SDL2_SOURCE_DIR}/src/video/x11/*.c)
   408       set(SOURCE_FILES ${SOURCE_FILES} ${X11_SOURCES})
   409       set(SDL_VIDEO_DRIVER_X11 1)
   410 
   411       if(APPLE)
   412         set(X11_SHARED OFF)
   413       endif()
   414 
   415       check_function_exists("shmat" HAVE_SHMAT)
   416       if(NOT HAVE_SHMAT)
   417         check_library_exists(ipc shmat "" HAVE_SHMAT)
   418         if(HAVE_SHMAT)
   419           list(APPEND EXTRA_LIBS ipc)
   420         endif()
   421         if(NOT HAVE_SHMAT)
   422           add_definitions(-DNO_SHARED_MEMORY)
   423           set(X_CFLAGS "${X_CFLAGS} -DNO_SHARED_MEMORY")
   424         endif()
   425       endif()
   426 
   427       if(X11_SHARED)
   428         if(NOT HAVE_DLOPEN)
   429           message_warn("You must have SDL_LoadObject() support for dynamic X11 loading")
   430           set(HAVE_X11_SHARED FALSE)
   431         else()
   432           set(HAVE_X11_SHARED TRUE)
   433         endif()
   434         if(HAVE_X11_SHARED)
   435           set(SDL_VIDEO_DRIVER_X11_DYNAMIC "\"${X11_LIB_SONAME}\"")
   436           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "\"${XEXT_LIB_SONAME}\"")
   437         else()
   438           list(APPEND EXTRA_LIBS ${X11_LIB} ${XEXT_LIB})
   439         endif()
   440       endif()
   441 
   442       set(SDL_CFLAGS "${SDL_CFLAGS} ${X_CFLAGS}")
   443 
   444       set(CMAKE_REQUIRED_LIBRARIES ${X11_LIB} ${X11_LIB})
   445       check_c_source_compiles("
   446           #include <X11/Xlib.h>
   447           #include <X11/Xproto.h>
   448           #include <X11/extensions/Xext.h>
   449           #include <X11/extensions/extutil.h>
   450           extern XExtDisplayInfo* XextAddDisplay(XExtensionInfo* a,Display* b,_Xconst char* c,XExtensionHooks* d,int e,XPointer f);
   451           int main(int argc, char **argv) {}" HAVE_CONST_XEXT_ADDDISPLAY)
   452       if(HAVE_CONST_XEXT_ADDDISPLAY)
   453         set(SDL_VIDEO_DRIVER_X11_CONST_PARAM_XEXTADDDISPLAY 1)
   454       endif()
   455 
   456       check_c_source_compiles("
   457           #include <X11/Xlib.h>
   458           int main(int argc, char **argv) {
   459             Display *display;
   460             XEvent event;
   461             XGenericEventCookie *cookie = &event.xcookie;
   462             XNextEvent(display, &event);
   463             XGetEventData(display, cookie);
   464             XFreeEventData(display, cookie); }" HAVE_XGENERICEVENT)
   465       if(HAVE_XGENERICEVENT)
   466         set(SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS 1)
   467       endif()
   468 
   469       check_function_exists(XkbKeycodeToKeysym SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM)
   470 
   471       if(VIDEO_X11_XCURSOR AND HAVE_XCURSOR_H)
   472         set(HAVE_VIDEO_X11_XCURSOR TRUE)
   473         if(HAVE_X11_SHARED AND XCURSOR_LIB)
   474           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "\"${XCURSOR_LIB_SONAME}\"")
   475         else()
   476           list(APPEND EXTRA_LIBS ${XCURSOR_LIB})
   477         endif()
   478         set(SDL_VIDEO_DRIVER_X11_XCURSOR 1)
   479       endif()
   480 
   481       if(VIDEO_X11_XINERAMA AND HAVE_XINERAMA_H)
   482         set(HAVE_VIDEO_X11_XINERAMA TRUE)
   483         if(HAVE_X11_SHARED AND XINERAMA_LIB)
   484           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "\"${XINERAMA_LIB_SONAME}\"")
   485         else()
   486           list(APPEND EXTRA_LIBS ${XINERAMA_LIB})
   487         endif()
   488         set(SDL_VIDEO_DRIVER_X11_XINERAMA 1)
   489       endif()
   490 
   491       if(VIDEO_X11_XINPUT AND HAVE_XINPUT_H)
   492         set(HAVE_VIDEO_X11_XINPUT TRUE)
   493         if(HAVE_X11_SHARED AND XI_LIB)
   494           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "\"${XI_LIB_SONAME}\"")
   495         else()
   496           list(APPEND EXTRA_LIBS ${XI_LIB})
   497         endif()
   498         set(SDL_VIDEO_DRIVER_X11_XINPUT2 1)
   499 
   500         # Check for multitouch
   501         check_c_source_compiles("
   502             #include <X11/Xlib.h>
   503             #include <X11/Xproto.h>
   504             #include <X11/extensions/XInput2.h>
   505             int event_type = XI_TouchBegin;
   506             XITouchClassInfo *t;
   507             Status XIAllowTouchEvents(Display *a,int b,unsigned int c,Window d,int f)
   508             {
   509               return (Status)0;
   510             }
   511             int main(int argc, char **argv) {}" HAVE_XINPUT2_MULTITOUCH)
   512         if(HAVE_XINPUT2_MULTITOUCH)
   513           set(SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH 1)
   514         endif()
   515       endif()
   516 
   517       if(VIDEO_X11_XRANDR AND HAVE_XRANDR_H)
   518         if(HAVE_X11_SHARED AND XRANDR_LIB)
   519           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "\"${XRANDR_LIB_SONAME}\"")
   520         else()
   521           list(APPEND EXTRA_LIBS ${XRANDR_LIB})
   522         endif()
   523         set(SDL_VIDEO_DRIVER_X11_XRANDR 1)
   524         set(HAVE_VIDEO_X11_XRANDR TRUE)
   525       endif()
   526 
   527       if(VIDEO_X11_XSCRNSAVER AND HAVE_XSS_H)
   528         if(HAVE_X11_SHARED AND XSS_LIB)
   529           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "\"${XSS_LIB_SONAME}\"")
   530         else()
   531           list(APPEND EXTRA_LIBS ${XSS_LIB})
   532         endif()
   533         set(SDL_VIDEO_DRIVER_X11_XSCRNSAVER 1)
   534         set(HAVE_VIDEO_X11_XSCRNSAVER TRUE)
   535       endif()
   536 
   537       if(VIDEO_X11_XSHAPE AND HAVE_XSHAPE_H)
   538         set(SDL_VIDEO_DRIVER_X11_XSHAPE 1)
   539         set(HAVE_VIDEO_X11_XSHAPE TRUE)
   540       endif()
   541 
   542       if(VIDEO_X11_XVM AND HAVE_XF86VM_H)
   543         if(HAVE_X11_SHARED AND XXF86VM_LIB)
   544           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "\"${XXF86VM_LIB_SONAME}\"")
   545         else()
   546           list(APPEND EXTRA_LIBS ${XXF86VM_LIB})
   547         endif()
   548         set(SDL_VIDEO_DRIVER_X11_XVIDMODE 1)
   549         set(HAVE_VIDEO_X11_XVM TRUE)
   550       endif()
   551 
   552       set(CMAKE_REQUIRED_LIBRARIES)
   553     endif()
   554   endif()
   555 endmacro()
   556 
   557 # Requires:
   558 # - EGL
   559 # - PkgCheckModules
   560 # Optional:
   561 # - MIR_SHARED opt
   562 # - HAVE_DLOPEN opt
   563 macro(CheckMir)
   564     if(VIDEO_MIR)
   565         find_library(MIR_LIB mirclient mircommon egl)
   566         pkg_check_modules(MIR_TOOLKIT mirclient>=0.26 mircommon)
   567         pkg_check_modules(EGL egl)
   568         pkg_check_modules(XKB xkbcommon)
   569 
   570         if (MIR_LIB AND MIR_TOOLKIT_FOUND AND EGL_FOUND AND XKB_FOUND)
   571             set(HAVE_VIDEO_MIR TRUE)
   572             set(HAVE_SDL_VIDEO TRUE)
   573 
   574             file(GLOB MIR_SOURCES ${SDL2_SOURCE_DIR}/src/video/mir/*.c)
   575             set(SOURCE_FILES ${SOURCE_FILES} ${MIR_SOURCES})
   576             set(SDL_VIDEO_DRIVER_MIR 1)
   577 
   578             list(APPEND EXTRA_CFLAGS ${MIR_TOOLKIT_CFLAGS} ${EGL_CFLAGS} ${XKB_CFLAGS})
   579 
   580             if(MIR_SHARED)
   581                 if(NOT HAVE_DLOPEN)
   582                     message_warn("You must have SDL_LoadObject() support for dynamic Mir loading")
   583                 else()
   584                     FindLibraryAndSONAME(mirclient)
   585                     FindLibraryAndSONAME(xkbcommon)
   586                     set(SDL_VIDEO_DRIVER_MIR_DYNAMIC "\"${MIRCLIENT_LIB_SONAME}\"")
   587                     set(SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON "\"${XKBCOMMON_LIB_SONAME}\"")
   588                     set(HAVE_MIR_SHARED TRUE)
   589                 endif()
   590             else()
   591                 set(EXTRA_LIBS ${MIR_TOOLKIT_LIBRARIES} ${EXTRA_LIBS})
   592             endif()
   593         endif()
   594     endif()
   595 endmacro()
   596 
   597 macro(WaylandProtocolGen _SCANNER _XML _PROTL)
   598     set(_WAYLAND_PROT_C_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-protocol.c")
   599     set(_WAYLAND_PROT_H_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-client-protocol.h")
   600 
   601     add_custom_command(
   602         OUTPUT "${_WAYLAND_PROT_H_CODE}"
   603         DEPENDS "${_XML}"
   604         COMMAND "${_SCANNER}"
   605         ARGS client-header "${_XML}" "${_WAYLAND_PROT_H_CODE}"
   606     )
   607 
   608     add_custom_command(
   609         OUTPUT "${_WAYLAND_PROT_C_CODE}"
   610         DEPENDS "${_WAYLAND_PROT_H_CODE}"
   611         COMMAND "${_SCANNER}"
   612         ARGS code "${_XML}" "${_WAYLAND_PROT_C_CODE}"
   613     )
   614 
   615     set(SOURCE_FILES ${SOURCE_FILES} "${_WAYLAND_PROT_C_CODE}")
   616 endmacro()
   617 
   618 # Requires:
   619 # - EGL
   620 # - PkgCheckModules
   621 # Optional:
   622 # - WAYLAND_SHARED opt
   623 # - HAVE_DLOPEN opt
   624 macro(CheckWayland)
   625   if(VIDEO_WAYLAND)
   626     pkg_check_modules(WAYLAND wayland-client wayland-scanner wayland-protocols wayland-egl wayland-cursor egl xkbcommon)
   627 
   628     # We have to generate some protocol interface code for some various Wayland features.
   629     if(WAYLAND_FOUND)
   630       execute_process(
   631         COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=pkgdatadir wayland-client
   632         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
   633         RESULT_VARIABLE WAYLAND_CORE_PROTOCOL_DIR_RC
   634         OUTPUT_VARIABLE WAYLAND_CORE_PROTOCOL_DIR
   635         ERROR_QUIET
   636         OUTPUT_STRIP_TRAILING_WHITESPACE
   637       )
   638       if(NOT WAYLAND_CORE_PROTOCOL_DIR_RC EQUAL 0)
   639         set(WAYLAND_FOUND FALSE)
   640       endif()
   641     endif()
   642 
   643     if(WAYLAND_FOUND)
   644       execute_process(
   645         COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=pkgdatadir wayland-protocols
   646         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
   647         RESULT_VARIABLE WAYLAND_PROTOCOLS_DIR_RC
   648         OUTPUT_VARIABLE WAYLAND_PROTOCOLS_DIR
   649         ERROR_QUIET
   650         OUTPUT_STRIP_TRAILING_WHITESPACE
   651       )
   652       if(NOT WAYLAND_PROTOCOLS_DIR_RC EQUAL 0)
   653         set(WAYLAND_FOUND FALSE)
   654       endif()
   655     endif()
   656 
   657     if(WAYLAND_FOUND)
   658       execute_process(
   659         COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=wayland_scanner wayland-scanner
   660         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
   661         RESULT_VARIABLE WAYLAND_SCANNER_RC
   662         OUTPUT_VARIABLE WAYLAND_SCANNER
   663         ERROR_QUIET
   664         OUTPUT_STRIP_TRAILING_WHITESPACE
   665       )
   666       if(NOT WAYLAND_SCANNER_RC EQUAL 0)
   667         set(WAYLAND_FOUND FALSE)
   668       endif()
   669     endif()
   670 
   671     if(WAYLAND_FOUND)
   672       link_directories(
   673           ${WAYLAND_LIBRARY_DIRS}
   674       )
   675       include_directories(
   676           ${WAYLAND_INCLUDE_DIRS}
   677       )
   678       set(HAVE_VIDEO_WAYLAND TRUE)
   679       set(HAVE_SDL_VIDEO TRUE)
   680 
   681       file(GLOB WAYLAND_SOURCES ${SDL2_SOURCE_DIR}/src/video/wayland/*.c)
   682       set(SOURCE_FILES ${SOURCE_FILES} ${WAYLAND_SOURCES})
   683 
   684       # We have to generate some protocol interface code for some unstable Wayland features.
   685       file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols")
   686       include_directories("${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols")
   687 
   688       WaylandProtocolGen("${WAYLAND_SCANNER}" "${WAYLAND_CORE_PROTOCOL_DIR}/wayland.xml" "wayland")
   689 
   690       foreach(_PROTL relative-pointer-unstable-v1 pointer-constraints-unstable-v1 xdg-shell-unstable-v6)
   691         string(REGEX REPLACE "\\-unstable\\-.*$" "" PROTSUBDIR ${_PROTL})
   692         WaylandProtocolGen("${WAYLAND_SCANNER}" "${WAYLAND_PROTOCOLS_DIR}/unstable/${PROTSUBDIR}/${_PROTL}.xml" "${_PROTL}")
   693       endforeach()
   694 
   695       if(VIDEO_WAYLAND_QT_TOUCH)
   696           set(SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH 1)
   697       endif()
   698 
   699       if(WAYLAND_SHARED)
   700         if(NOT HAVE_DLOPEN)
   701           message_warn("You must have SDL_LoadObject() support for dynamic Wayland loading")
   702         else()
   703           FindLibraryAndSONAME(wayland-client)
   704           FindLibraryAndSONAME(wayland-egl)
   705           FindLibraryAndSONAME(wayland-cursor)
   706           FindLibraryAndSONAME(xkbcommon)
   707           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC "\"${WAYLAND_CLIENT_LIB_SONAME}\"")
   708           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL "\"${WAYLAND_EGL_LIB_SONAME}\"")
   709           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR "\"${WAYLAND_CURSOR_LIB_SONAME}\"")
   710           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON "\"${XKBCOMMON_LIB_SONAME}\"")
   711           set(HAVE_WAYLAND_SHARED TRUE)
   712         endif()
   713       else()
   714         set(EXTRA_LIBS ${WAYLAND_LIBRARIES} ${EXTRA_LIBS})
   715       endif()
   716 
   717       set(SDL_VIDEO_DRIVER_WAYLAND 1)
   718     endif()
   719   endif()
   720 endmacro()
   721 
   722 # Requires:
   723 # - n/a
   724 #
   725 macro(CheckCOCOA)
   726   if(VIDEO_COCOA)
   727     if(APPLE) # Apple always has Cocoa.
   728       set(HAVE_VIDEO_COCOA TRUE)
   729     endif()
   730     if(HAVE_VIDEO_COCOA)
   731       file(GLOB COCOA_SOURCES ${SDL2_SOURCE_DIR}/src/video/cocoa/*.m)
   732       set_source_files_properties(${COCOA_SOURCES} PROPERTIES LANGUAGE C)
   733       set(SOURCE_FILES ${SOURCE_FILES} ${COCOA_SOURCES})
   734       set(SDL_VIDEO_DRIVER_COCOA 1)
   735       set(HAVE_SDL_VIDEO TRUE)
   736     endif()
   737   endif()
   738 endmacro()
   739 
   740 # Requires:
   741 # - PkgCheckModules
   742 # Optional:
   743 # - DIRECTFB_SHARED opt
   744 # - HAVE_DLOPEN opt
   745 macro(CheckDirectFB)
   746   if(VIDEO_DIRECTFB)
   747     pkg_check_modules(PKG_DIRECTFB directfb>=1.0.0)
   748     if(PKG_DIRECTFB_FOUND)
   749       set(HAVE_VIDEO_DIRECTFB TRUE)
   750       file(GLOB DIRECTFB_SOURCES ${SDL2_SOURCE_DIR}/src/video/directfb/*.c)
   751       set(SOURCE_FILES ${SOURCE_FILES} ${DIRECTFB_SOURCES})
   752       set(SDL_VIDEO_DRIVER_DIRECTFB 1)
   753       set(SDL_VIDEO_RENDER_DIRECTFB 1)
   754       list(APPEND EXTRA_CFLAGS ${PKG_DIRECTFB_CFLAGS})
   755       if(DIRECTFB_SHARED)
   756         if(NOT HAVE_DLOPEN)
   757           message_warn("You must have SDL_LoadObject() support for dynamic DirectFB loading")
   758         else()
   759           FindLibraryAndSONAME("directfb")
   760           set(SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC "\"${DIRECTFB_LIB_SONAME}\"")
   761           set(HAVE_DIRECTFB_SHARED TRUE)
   762         endif()
   763       else()
   764         list(APPEND EXTRA_LDFLAGS ${PKG_DIRECTFB_LDFLAGS})
   765       endif()
   766       set(HAVE_SDL_VIDEO TRUE)
   767     endif()
   768   endif()
   769 endmacro()
   770 
   771 # Requires:
   772 # - n/a
   773 macro(CheckVivante)
   774   if(VIDEO_VIVANTE)
   775     check_c_source_compiles("
   776         #include <gc_vdk.h>
   777         int main(int argc, char** argv) {}" HAVE_VIDEO_VIVANTE_VDK)
   778     check_c_source_compiles("
   779         #define LINUX
   780         #define EGL_API_FB
   781         #include <EGL/eglvivante.h>
   782         int main(int argc, char** argv) {}" HAVE_VIDEO_VIVANTE_EGL_FB)
   783     if(HAVE_VIDEO_VIVANTE_VDK OR HAVE_VIDEO_VIVANTE_EGL_FB)
   784       set(HAVE_VIDEO_VIVANTE TRUE)
   785       set(HAVE_SDL_VIDEO TRUE)
   786 
   787       file(GLOB VIVANTE_SOURCES ${SDL2_SOURCE_DIR}/src/video/vivante/*.c)
   788       set(SOURCE_FILES ${SOURCE_FILES} ${VIVANTE_SOURCES})
   789       set(SDL_VIDEO_DRIVER_VIVANTE 1)
   790       if(HAVE_VIDEO_VIVANTE_VDK)
   791         set(SDL_VIDEO_DRIVER_VIVANTE_VDK 1)
   792         list(APPEND EXTRA_LIBS VDK VIVANTE)
   793       else()
   794         set(SDL_CFLAGS "${SDL_CFLAGS} -DLINUX -DEGL_API_FB")
   795         list(APPEND EXTRA_LIBS EGL)
   796       endif(HAVE_VIDEO_VIVANTE_VDK)
   797     endif(HAVE_VIDEO_VIVANTE_VDK OR HAVE_VIDEO_VIVANTE_EGL_FB)
   798   endif(VIDEO_VIVANTE)
   799 endmacro(CheckVivante)
   800 
   801 # Requires:
   802 # - nada
   803 macro(CheckOpenGLX11)
   804   if(VIDEO_OPENGL)
   805     check_c_source_compiles("
   806         #include <GL/gl.h>
   807         #include <GL/glx.h>
   808         int main(int argc, char** argv) {}" HAVE_VIDEO_OPENGL)
   809 
   810     if(HAVE_VIDEO_OPENGL)
   811       set(HAVE_VIDEO_OPENGL TRUE)
   812       set(SDL_VIDEO_OPENGL 1)
   813       set(SDL_VIDEO_OPENGL_GLX 1)
   814       set(SDL_VIDEO_RENDER_OGL 1)
   815     endif()
   816   endif()
   817 endmacro()
   818 
   819 # Requires:
   820 # - nada
   821 macro(CheckOpenGLESX11)
   822   if(VIDEO_OPENGLES)
   823     check_c_source_compiles("
   824         #define EGL_API_FB
   825         #include <EGL/egl.h>
   826         int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGL_EGL)
   827     if(HAVE_VIDEO_OPENGL_EGL)
   828         set(SDL_VIDEO_OPENGL_EGL 1)
   829     endif()
   830     check_c_source_compiles("
   831       #include <GLES/gl.h>
   832       #include <GLES/glext.h>
   833       int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGLES_V1)
   834     if(HAVE_VIDEO_OPENGLES_V1)
   835         set(HAVE_VIDEO_OPENGLES TRUE)
   836         set(SDL_VIDEO_OPENGL_ES 1)
   837         set(SDL_VIDEO_RENDER_OGL_ES 1)
   838     endif()
   839     check_c_source_compiles("
   840       #include <GLES2/gl2.h>
   841       #include <GLES2/gl2ext.h>
   842       int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGLES_V2)
   843     if(HAVE_VIDEO_OPENGLES_V2)
   844         set(HAVE_VIDEO_OPENGLES TRUE)
   845         set(SDL_VIDEO_OPENGL_ES2 1)
   846         set(SDL_VIDEO_RENDER_OGL_ES2 1)
   847     endif()
   848 
   849   endif()
   850 endmacro()
   851 
   852 # Requires:
   853 # - nada
   854 # Optional:
   855 # - THREADS opt
   856 # Sets:
   857 # PTHREAD_CFLAGS
   858 # PTHREAD_LIBS
   859 macro(CheckPTHREAD)
   860   if(PTHREADS)
   861     if(ANDROID)
   862       # the android libc provides built-in support for pthreads, so no
   863       # additional linking or compile flags are necessary
   864     elseif(LINUX)
   865       set(PTHREAD_CFLAGS "-D_REENTRANT")
   866       set(PTHREAD_LDFLAGS "-pthread")
   867     elseif(BSDI)
   868       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   869       set(PTHREAD_LDFLAGS "")
   870     elseif(DARWIN)
   871       set(PTHREAD_CFLAGS "-D_THREAD_SAFE")
   872       # causes Carbon.p complaints?
   873       # set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   874       set(PTHREAD_LDFLAGS "")
   875     elseif(FREEBSD)
   876       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   877       set(PTHREAD_LDFLAGS "-pthread")
   878     elseif(NETBSD)
   879       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   880       set(PTHREAD_LDFLAGS "-lpthread")
   881     elseif(OPENBSD)
   882       set(PTHREAD_CFLAGS "-D_REENTRANT")
   883       set(PTHREAD_LDFLAGS "-pthread")
   884     elseif(SOLARIS)
   885       set(PTHREAD_CFLAGS "-D_REENTRANT")
   886       set(PTHREAD_LDFLAGS "-pthread -lposix4")
   887     elseif(SYSV5)
   888       set(PTHREAD_CFLAGS "-D_REENTRANT -Kthread")
   889       set(PTHREAD_LDFLAGS "")
   890     elseif(AIX)
   891       set(PTHREAD_CFLAGS "-D_REENTRANT -mthreads")
   892       set(PTHREAD_LDFLAGS "-pthread")
   893     elseif(HPUX)
   894       set(PTHREAD_CFLAGS "-D_REENTRANT")
   895       set(PTHREAD_LDFLAGS "-L/usr/lib -pthread")
   896     elseif(HAIKU)
   897       set(PTHREAD_CFLAGS "-D_REENTRANT")
   898       set(PTHREAD_LDFLAGS "")
   899     else()
   900       set(PTHREAD_CFLAGS "-D_REENTRANT")
   901       set(PTHREAD_LDFLAGS "-lpthread")
   902     endif()
   903 
   904     # Run some tests
   905     set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
   906     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${PTHREAD_CFLAGS} ${PTHREAD_LDFLAGS}")
   907     if(CMAKE_CROSSCOMPILING)
   908       set(HAVE_PTHREADS 1)
   909     else()
   910       check_c_source_runs("
   911         #include <pthread.h>
   912         int main(int argc, char** argv) {
   913           pthread_attr_t type;
   914           pthread_attr_init(&type);
   915           return 0;
   916         }" HAVE_PTHREADS)
   917     endif()
   918     if(HAVE_PTHREADS)
   919       set(SDL_THREAD_PTHREAD 1)
   920       list(APPEND EXTRA_CFLAGS ${PTHREAD_CFLAGS})
   921       list(APPEND EXTRA_LDFLAGS ${PTHREAD_LDFLAGS})
   922       set(SDL_CFLAGS "${SDL_CFLAGS} ${PTHREAD_CFLAGS}")
   923       list(APPEND SDL_LIBS ${PTHREAD_LDFLAGS})
   924 
   925       check_c_source_compiles("
   926         #include <pthread.h>
   927         int main(int argc, char **argv) {
   928           pthread_mutexattr_t attr;
   929           pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
   930           return 0;
   931         }" HAVE_RECURSIVE_MUTEXES)
   932       if(HAVE_RECURSIVE_MUTEXES)
   933         set(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 1)
   934       else()
   935         check_c_source_compiles("
   936             #include <pthread.h>
   937             int main(int argc, char **argv) {
   938               pthread_mutexattr_t attr;
   939               pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
   940               return 0;
   941             }" HAVE_RECURSIVE_MUTEXES_NP)
   942         if(HAVE_RECURSIVE_MUTEXES_NP)
   943           set(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP 1)
   944         endif()
   945       endif()
   946 
   947       if(PTHREADS_SEM)
   948         check_c_source_compiles("#include <pthread.h>
   949                                  #include <semaphore.h>
   950                                  int main(int argc, char **argv) { return 0; }" HAVE_PTHREADS_SEM)
   951         if(HAVE_PTHREADS_SEM)
   952           check_c_source_compiles("
   953               #include <pthread.h>
   954               #include <semaphore.h>
   955               int main(int argc, char **argv) {
   956                   sem_timedwait(NULL, NULL);
   957                   return 0;
   958               }" HAVE_SEM_TIMEDWAIT)
   959         endif()
   960       endif()
   961 
   962       check_c_source_compiles("
   963           #include <pthread.h>
   964           #include <pthread_np.h>
   965           int main(int argc, char** argv) { return 0; }" HAVE_PTHREAD_NP_H)
   966       check_function_exists(pthread_setname_np HAVE_PTHREAD_SETNAME_NP)
   967       check_function_exists(pthread_set_name_np HAVE_PTHREAD_SET_NAME_NP)
   968 
   969       set(SOURCE_FILES ${SOURCE_FILES}
   970           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_systhread.c
   971           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_sysmutex.c   # Can be faked, if necessary
   972           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_syscond.c    # Can be faked, if necessary
   973           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_systls.c
   974           )
   975       if(HAVE_PTHREADS_SEM)
   976         set(SOURCE_FILES ${SOURCE_FILES}
   977             ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_syssem.c)
   978       else()
   979         set(SOURCE_FILES ${SOURCE_FILES}
   980             ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_syssem.c)
   981       endif()
   982       set(HAVE_SDL_THREADS TRUE)
   983     endif()
   984     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}")
   985   endif()
   986 endmacro()
   987 
   988 # Requires
   989 # - nada
   990 # Optional:
   991 # Sets:
   992 # USB_LIBS
   993 # USB_CFLAGS
   994 macro(CheckUSBHID)
   995   check_library_exists(usbhid hid_init "" LIBUSBHID)
   996   if(LIBUSBHID)
   997     check_include_file(usbhid.h HAVE_USBHID_H)
   998     if(HAVE_USBHID_H)
   999       set(USB_CFLAGS "-DHAVE_USBHID_H")
  1000     endif()
  1001 
  1002     check_include_file(libusbhid.h HAVE_LIBUSBHID_H)
  1003     if(HAVE_LIBUSBHID_H)
  1004       set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSBHID_H")
  1005     endif()
  1006     set(USB_LIBS ${USB_LIBS} usbhid)
  1007   else()
  1008     check_include_file(usb.h HAVE_USB_H)
  1009     if(HAVE_USB_H)
  1010       set(USB_CFLAGS "-DHAVE_USB_H")
  1011     endif()
  1012     check_include_file(libusb.h HAVE_LIBUSB_H)
  1013     if(HAVE_LIBUSB_H)
  1014       set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSB_H")
  1015     endif()
  1016     check_library_exists(usb hid_init "" LIBUSB)
  1017     if(LIBUSB)
  1018       set(USB_LIBS ${USB_LIBS} usb)
  1019     endif()
  1020   endif()
  1021 
  1022   set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  1023   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${USB_CFLAGS}")
  1024   set(CMAKE_REQUIRED_LIBRARIES "${USB_LIBS}")
  1025   check_c_source_compiles("
  1026        #include <sys/types.h>
  1027         #if defined(HAVE_USB_H)
  1028         #include <usb.h>
  1029         #endif
  1030         #ifdef __DragonFly__
  1031         # include <bus/usb/usb.h>
  1032         # include <bus/usb/usbhid.h>
  1033         #else
  1034         # include <dev/usb/usb.h>
  1035         # include <dev/usb/usbhid.h>
  1036         #endif
  1037         #if defined(HAVE_USBHID_H)
  1038         #include <usbhid.h>
  1039         #elif defined(HAVE_LIBUSB_H)
  1040         #include <libusb.h>
  1041         #elif defined(HAVE_LIBUSBHID_H)
  1042         #include <libusbhid.h>
  1043         #endif
  1044         int main(int argc, char **argv) {
  1045           struct report_desc *repdesc;
  1046           struct usb_ctl_report *repbuf;
  1047           hid_kind_t hidkind;
  1048           return 0;
  1049         }" HAVE_USBHID)
  1050   if(HAVE_USBHID)
  1051     check_c_source_compiles("
  1052           #include <sys/types.h>
  1053           #if defined(HAVE_USB_H)
  1054           #include <usb.h>
  1055           #endif
  1056           #ifdef __DragonFly__
  1057           # include <bus/usb/usb.h>
  1058           # include <bus/usb/usbhid.h>
  1059           #else
  1060           # include <dev/usb/usb.h>
  1061           # include <dev/usb/usbhid.h>
  1062           #endif
  1063           #if defined(HAVE_USBHID_H)
  1064           #include <usbhid.h>
  1065           #elif defined(HAVE_LIBUSB_H)
  1066           #include <libusb.h>
  1067           #elif defined(HAVE_LIBUSBHID_H)
  1068           #include <libusbhid.h>
  1069           #endif
  1070           int main(int argc, char** argv) {
  1071             struct usb_ctl_report buf;
  1072             if (buf.ucr_data) { }
  1073             return 0;
  1074           }" HAVE_USBHID_UCR_DATA)
  1075     if(HAVE_USBHID_UCR_DATA)
  1076       set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_UCR_DATA")
  1077     endif()
  1078 
  1079     check_c_source_compiles("
  1080           #include <sys/types.h>
  1081           #if defined(HAVE_USB_H)
  1082           #include <usb.h>
  1083           #endif
  1084           #ifdef __DragonFly__
  1085           #include <bus/usb/usb.h>
  1086           #include <bus/usb/usbhid.h>
  1087           #else
  1088           #include <dev/usb/usb.h>
  1089           #include <dev/usb/usbhid.h>
  1090           #endif
  1091           #if defined(HAVE_USBHID_H)
  1092           #include <usbhid.h>
  1093           #elif defined(HAVE_LIBUSB_H)
  1094           #include <libusb.h>
  1095           #elif defined(HAVE_LIBUSBHID_H)
  1096           #include <libusbhid.h>
  1097           #endif
  1098           int main(int argc, char **argv) {
  1099             report_desc_t d;
  1100             hid_start_parse(d, 1, 1);
  1101             return 0;
  1102           }" HAVE_USBHID_NEW)
  1103     if(HAVE_USBHID_NEW)
  1104       set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_NEW")
  1105     endif()
  1106 
  1107     check_c_source_compiles("
  1108         #include <machine/joystick.h>
  1109         int main(int argc, char** argv) {
  1110             struct joystick t;
  1111             return 0;
  1112         }" HAVE_MACHINE_JOYSTICK)
  1113     if(HAVE_MACHINE_JOYSTICK)
  1114       set(SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H 1)
  1115     endif()
  1116     set(SDL_JOYSTICK_USBHID 1)
  1117     file(GLOB BSD_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/bsd/*.c)
  1118     set(SOURCE_FILES ${SOURCE_FILES} ${BSD_JOYSTICK_SOURCES})
  1119     list(APPEND EXTRA_CFLAGS ${USB_CFLAGS})
  1120     list(APPEND EXTRA_LIBS ${USB_LIBS})
  1121     set(HAVE_SDL_JOYSTICK TRUE)
  1122 
  1123     set(CMAKE_REQUIRED_LIBRARIES)
  1124     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}")
  1125   endif()
  1126 endmacro()
  1127 
  1128 # Requires:
  1129 # - n/a
  1130 macro(CheckRPI)
  1131   if(VIDEO_RPI)
  1132     pkg_check_modules(VIDEO_RPI bcm_host brcmegl)
  1133     if (NOT VIDEO_RPI_FOUND)
  1134       set(VIDEO_RPI_INCLUDE_DIRS "/opt/vc/include" "/opt/vc/include/interface/vcos/pthreads" "/opt/vc/include/interface/vmcs_host/linux/" )
  1135       set(VIDEO_RPI_LIBRARY_DIRS "/opt/vc/lib" )
  1136       set(VIDEO_RPI_LIBRARIES bcm_host )
  1137       set(VIDEO_RPI_LDFLAGS "-Wl,-rpath,/opt/vc/lib")
  1138     endif()
  1139     listtostr(VIDEO_RPI_INCLUDE_DIRS VIDEO_RPI_INCLUDE_FLAGS "-I")
  1140     listtostr(VIDEO_RPI_LIBRARY_DIRS VIDEO_RPI_LIBRARY_FLAGS "-L")
  1141 
  1142     set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  1143     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${VIDEO_RPI_INCLUDE_FLAGS} ${VIDEO_RPI_LIBRARY_FLAGS}")
  1144     set(CMAKE_REQUIRED_LIBRARIES "${VIDEO_RPI_LIBRARIES}")
  1145     check_c_source_compiles("
  1146         #include <bcm_host.h>
  1147         int main(int argc, char **argv) {}" HAVE_VIDEO_RPI)
  1148     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}")
  1149     set(CMAKE_REQUIRED_LIBRARIES)
  1150 
  1151     if(SDL_VIDEO AND HAVE_VIDEO_RPI)
  1152       set(HAVE_SDL_VIDEO TRUE)
  1153       set(SDL_VIDEO_DRIVER_RPI 1)
  1154       file(GLOB VIDEO_RPI_SOURCES ${SDL2_SOURCE_DIR}/src/video/raspberry/*.c)
  1155       set(SOURCE_FILES ${SOURCE_FILES} ${VIDEO_RPI_SOURCES})
  1156       list(APPEND EXTRA_LIBS ${VIDEO_RPI_LIBRARIES})
  1157       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VIDEO_RPI_INCLUDE_FLAGS} ${VIDEO_RPI_LIBRARY_FLAGS}")
  1158       list(APPEND EXTRA_LDFLAGS ${VIDEO_RPI_LDFLAGS})
  1159     endif(SDL_VIDEO AND HAVE_VIDEO_RPI)
  1160   endif(VIDEO_RPI)
  1161 endmacro(CheckRPI)
  1162 
  1163 # Requires:
  1164 # - EGL
  1165 # - PkgCheckModules
  1166 # Optional:
  1167 # - KMSDRM_SHARED opt
  1168 # - HAVE_DLOPEN opt
  1169 macro(CheckKMSDRM)
  1170   if(VIDEO_KMSDRM)
  1171     pkg_check_modules(KMSDRM libdrm gbm egl)
  1172     if(KMSDRM_FOUND)
  1173       link_directories(
  1174         ${KMSDRM_LIBRARY_DIRS}
  1175       )
  1176       include_directories(
  1177         ${KMSDRM_INCLUDE_DIRS}
  1178       )
  1179       set(HAVE_VIDEO_KMSDRM TRUE)
  1180       set(HAVE_SDL_VIDEO TRUE)
  1181 
  1182       file(GLOB KMSDRM_SOURCES ${SDL2_SOURCE_DIR}/src/video/kmsdrm/*.c)
  1183       set(SOURCE_FILES ${SOURCE_FILES} ${KMSDRM_SOURCES})
  1184 
  1185       list(APPEND EXTRA_CFLAGS ${KMSDRM_CFLAGS})
  1186 
  1187       set(SDL_VIDEO_DRIVER_KMSDRM 1)
  1188 
  1189       if(KMSDRM_SHARED)
  1190         if(NOT HAVE_DLOPEN)
  1191           message_warn("You must have SDL_LoadObject() support for dynamic KMS/DRM loading")
  1192         else()
  1193           FindLibraryAndSONAME(drm)
  1194           FindLibraryAndSONAME(gbm)
  1195           set(SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC "\"${DRM_LIB_SONAME}\"")
  1196           set(SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM "\"${GBM_LIB_SONAME}\"")
  1197           set(HAVE_KMSDRM_SHARED TRUE)
  1198         endif()
  1199       else()
  1200         set(EXTRA_LIBS ${KMSDRM_LIBRARIES} ${EXTRA_LIBS})
  1201       endif()
  1202     endif()
  1203   endif()
  1204 endmacro()