cmake/sdlchecks.cmake
author "Guillermo A. Amaral" <g@maral.me>
Wed, 07 Feb 2018 14:12:26 -0800
changeset 11851 8f295e7acefd
parent 11848 2f157c1ca383
child 11867 4e7f7e186d27
permissions -rw-r--r--
Set rpath instead of changing environment for RPi
Credit goes to Adrian Perez de Castro for the improvement.

Signed-off-by: Guillermo A. Amaral <g@maral.me>
     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       if(LIBSAMPLERATE_SHARED)
   358         if(NOT HAVE_DLOPEN)
   359           message_warn("You must have SDL_LoadObject() support for dynamic libsamplerate loading")
   360         else()
   361           FindLibraryAndSONAME("samplerate")
   362           set(SDL_LIBSAMPLERATE_DYNAMIC "\"${SAMPLERATE_LIB_SONAME}\"")
   363         endif()
   364       else()
   365         list(APPEND EXTRA_LDFLAGS -lsamplerate)
   366       endif()
   367     endif()
   368   endif()
   369 endmacro()
   370 
   371 # Requires:
   372 # - n/a
   373 # Optional:
   374 # - X11_SHARED opt
   375 # - HAVE_DLOPEN opt
   376 macro(CheckX11)
   377   if(VIDEO_X11)
   378     foreach(_LIB X11 Xext Xcursor Xinerama Xi Xrandr Xrender Xss Xxf86vm)
   379         FindLibraryAndSONAME("${_LIB}")
   380     endforeach()
   381 
   382     find_path(X_INCLUDEDIR X11/Xlib.h)
   383     if(X_INCLUDEDIR)
   384       set(X_CFLAGS "-I${X_INCLUDEDIR}")
   385     endif()
   386 
   387     check_include_file(X11/Xcursor/Xcursor.h HAVE_XCURSOR_H)
   388     check_include_file(X11/extensions/Xinerama.h HAVE_XINERAMA_H)
   389     check_include_file(X11/extensions/XInput2.h HAVE_XINPUT_H)
   390     check_include_file(X11/extensions/Xrandr.h HAVE_XRANDR_H)
   391     check_include_file(X11/extensions/Xrender.h HAVE_XRENDER_H)
   392     check_include_file(X11/extensions/scrnsaver.h HAVE_XSS_H)
   393     check_include_file(X11/extensions/shape.h HAVE_XSHAPE_H)
   394     check_include_files("X11/Xlib.h;X11/extensions/xf86vmode.h" HAVE_XF86VM_H)
   395     check_include_files("X11/Xlib.h;X11/Xproto.h;X11/extensions/Xext.h" HAVE_XEXT_H)
   396 
   397     if(X11_LIB)
   398       if(NOT HAVE_XEXT_H)
   399         message_error("Missing Xext.h, maybe you need to install the libxext-dev package?")
   400       endif()
   401 
   402       set(HAVE_VIDEO_X11 TRUE)
   403       set(HAVE_SDL_VIDEO TRUE)
   404 
   405       file(GLOB X11_SOURCES ${SDL2_SOURCE_DIR}/src/video/x11/*.c)
   406       set(SOURCE_FILES ${SOURCE_FILES} ${X11_SOURCES})
   407       set(SDL_VIDEO_DRIVER_X11 1)
   408 
   409       if(APPLE)
   410         set(X11_SHARED OFF)
   411       endif()
   412 
   413       check_function_exists("shmat" HAVE_SHMAT)
   414       if(NOT HAVE_SHMAT)
   415         check_library_exists(ipc shmat "" HAVE_SHMAT)
   416         if(HAVE_SHMAT)
   417           list(APPEND EXTRA_LIBS ipc)
   418         endif()
   419         if(NOT HAVE_SHMAT)
   420           add_definitions(-DNO_SHARED_MEMORY)
   421           set(X_CFLAGS "${X_CFLAGS} -DNO_SHARED_MEMORY")
   422         endif()
   423       endif()
   424 
   425       if(X11_SHARED)
   426         if(NOT HAVE_DLOPEN)
   427           message_warn("You must have SDL_LoadObject() support for dynamic X11 loading")
   428           set(HAVE_X11_SHARED FALSE)
   429         else()
   430           set(HAVE_X11_SHARED TRUE)
   431         endif()
   432         if(HAVE_X11_SHARED)
   433           set(SDL_VIDEO_DRIVER_X11_DYNAMIC "\"${X11_LIB_SONAME}\"")
   434           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "\"${XEXT_LIB_SONAME}\"")
   435         else()
   436           list(APPEND EXTRA_LIBS ${X11_LIB} ${XEXT_LIB})
   437         endif()
   438       endif()
   439 
   440       set(SDL_CFLAGS "${SDL_CFLAGS} ${X_CFLAGS}")
   441 
   442       set(CMAKE_REQUIRED_LIBRARIES ${X11_LIB} ${X11_LIB})
   443       check_c_source_compiles("
   444           #include <X11/Xlib.h>
   445           #include <X11/Xproto.h>
   446           #include <X11/extensions/Xext.h>
   447           #include <X11/extensions/extutil.h>
   448           extern XExtDisplayInfo* XextAddDisplay(XExtensionInfo* a,Display* b,_Xconst char* c,XExtensionHooks* d,int e,XPointer f);
   449           int main(int argc, char **argv) {}" HAVE_CONST_XEXT_ADDDISPLAY)
   450       if(HAVE_CONST_XEXT_ADDDISPLAY)
   451         set(SDL_VIDEO_DRIVER_X11_CONST_PARAM_XEXTADDDISPLAY 1)
   452       endif()
   453 
   454       check_c_source_compiles("
   455           #include <X11/Xlib.h>
   456           int main(int argc, char **argv) {
   457             Display *display;
   458             XEvent event;
   459             XGenericEventCookie *cookie = &event.xcookie;
   460             XNextEvent(display, &event);
   461             XGetEventData(display, cookie);
   462             XFreeEventData(display, cookie); }" HAVE_XGENERICEVENT)
   463       if(HAVE_XGENERICEVENT)
   464         set(SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS 1)
   465       endif()
   466 
   467       check_function_exists(XkbKeycodeToKeysym SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM)
   468 
   469       if(VIDEO_X11_XCURSOR AND HAVE_XCURSOR_H)
   470         set(HAVE_VIDEO_X11_XCURSOR TRUE)
   471         if(HAVE_X11_SHARED AND XCURSOR_LIB)
   472           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "\"${XCURSOR_LIB_SONAME}\"")
   473         else()
   474           list(APPEND EXTRA_LIBS ${XCURSOR_LIB})
   475         endif()
   476         set(SDL_VIDEO_DRIVER_X11_XCURSOR 1)
   477       endif()
   478 
   479       if(VIDEO_X11_XINERAMA AND HAVE_XINERAMA_H)
   480         set(HAVE_VIDEO_X11_XINERAMA TRUE)
   481         if(HAVE_X11_SHARED AND XINERAMA_LIB)
   482           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "\"${XINERAMA_LIB_SONAME}\"")
   483         else()
   484           list(APPEND EXTRA_LIBS ${XINERAMA_LIB})
   485         endif()
   486         set(SDL_VIDEO_DRIVER_X11_XINERAMA 1)
   487       endif()
   488 
   489       if(VIDEO_X11_XINPUT AND HAVE_XINPUT_H)
   490         set(HAVE_VIDEO_X11_XINPUT TRUE)
   491         if(HAVE_X11_SHARED AND XI_LIB)
   492           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "\"${XI_LIB_SONAME}\"")
   493         else()
   494           list(APPEND EXTRA_LIBS ${XI_LIB})
   495         endif()
   496         set(SDL_VIDEO_DRIVER_X11_XINPUT2 1)
   497 
   498         # Check for multitouch
   499         check_c_source_compiles("
   500             #include <X11/Xlib.h>
   501             #include <X11/Xproto.h>
   502             #include <X11/extensions/XInput2.h>
   503             int event_type = XI_TouchBegin;
   504             XITouchClassInfo *t;
   505             Status XIAllowTouchEvents(Display *a,int b,unsigned int c,Window d,int f)
   506             {
   507               return (Status)0;
   508             }
   509             int main(int argc, char **argv) {}" HAVE_XINPUT2_MULTITOUCH)
   510         if(HAVE_XINPUT2_MULTITOUCH)
   511           set(SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH 1)
   512         endif()
   513       endif()
   514 
   515       if(VIDEO_X11_XRANDR AND HAVE_XRANDR_H)
   516         if(HAVE_X11_SHARED AND XRANDR_LIB)
   517           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "\"${XRANDR_LIB_SONAME}\"")
   518         else()
   519           list(APPEND EXTRA_LIBS ${XRANDR_LIB})
   520         endif()
   521         set(SDL_VIDEO_DRIVER_X11_XRANDR 1)
   522         set(HAVE_VIDEO_X11_XRANDR TRUE)
   523       endif()
   524 
   525       if(VIDEO_X11_XSCRNSAVER AND HAVE_XSS_H)
   526         if(HAVE_X11_SHARED AND XSS_LIB)
   527           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "\"${XSS_LIB_SONAME}\"")
   528         else()
   529           list(APPEND EXTRA_LIBS ${XSS_LIB})
   530         endif()
   531         set(SDL_VIDEO_DRIVER_X11_XSCRNSAVER 1)
   532         set(HAVE_VIDEO_X11_XSCRNSAVER TRUE)
   533       endif()
   534 
   535       if(VIDEO_X11_XSHAPE AND HAVE_XSHAPE_H)
   536         set(SDL_VIDEO_DRIVER_X11_XSHAPE 1)
   537         set(HAVE_VIDEO_X11_XSHAPE TRUE)
   538       endif()
   539 
   540       if(VIDEO_X11_XVM AND HAVE_XF86VM_H)
   541         if(HAVE_X11_SHARED AND XXF86VM_LIB)
   542           set(SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "\"${XXF86VM_LIB_SONAME}\"")
   543         else()
   544           list(APPEND EXTRA_LIBS ${XXF86VM_LIB})
   545         endif()
   546         set(SDL_VIDEO_DRIVER_X11_XVIDMODE 1)
   547         set(HAVE_VIDEO_X11_XVM TRUE)
   548       endif()
   549 
   550       set(CMAKE_REQUIRED_LIBRARIES)
   551     endif()
   552   endif()
   553 endmacro()
   554 
   555 # Requires:
   556 # - EGL
   557 # - PkgCheckModules
   558 # Optional:
   559 # - MIR_SHARED opt
   560 # - HAVE_DLOPEN opt
   561 macro(CheckMir)
   562     if(VIDEO_MIR)
   563         find_library(MIR_LIB mirclient mircommon egl)
   564         pkg_check_modules(MIR_TOOLKIT mirclient>=0.26 mircommon)
   565         pkg_check_modules(EGL egl)
   566         pkg_check_modules(XKB xkbcommon)
   567 
   568         if (MIR_LIB AND MIR_TOOLKIT_FOUND AND EGL_FOUND AND XKB_FOUND)
   569             set(HAVE_VIDEO_MIR TRUE)
   570             set(HAVE_SDL_VIDEO TRUE)
   571 
   572             file(GLOB MIR_SOURCES ${SDL2_SOURCE_DIR}/src/video/mir/*.c)
   573             set(SOURCE_FILES ${SOURCE_FILES} ${MIR_SOURCES})
   574             set(SDL_VIDEO_DRIVER_MIR 1)
   575 
   576             list(APPEND EXTRA_CFLAGS ${MIR_TOOLKIT_CFLAGS} ${EGL_CFLAGS} ${XKB_CFLAGS})
   577 
   578             if(MIR_SHARED)
   579                 if(NOT HAVE_DLOPEN)
   580                     message_warn("You must have SDL_LoadObject() support for dynamic Mir loading")
   581                 else()
   582                     FindLibraryAndSONAME(mirclient)
   583                     FindLibraryAndSONAME(xkbcommon)
   584                     set(SDL_VIDEO_DRIVER_MIR_DYNAMIC "\"${MIRCLIENT_LIB_SONAME}\"")
   585                     set(SDL_VIDEO_DRIVER_MIR_DYNAMIC_XKBCOMMON "\"${XKBCOMMON_LIB_SONAME}\"")
   586                     set(HAVE_MIR_SHARED TRUE)
   587                 endif()
   588             else()
   589                 set(EXTRA_LIBS ${MIR_TOOLKIT_LIBRARIES} ${EXTRA_LIBS})
   590             endif()
   591         endif()
   592     endif()
   593 endmacro()
   594 
   595 macro(WaylandProtocolGen _SCANNER _XML _PROTL)
   596     set(_WAYLAND_PROT_C_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-protocol.c")
   597     set(_WAYLAND_PROT_H_CODE "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols/${_PROTL}-client-protocol.h")
   598 
   599     add_custom_command(
   600         OUTPUT "${_WAYLAND_PROT_H_CODE}"
   601         DEPENDS "${_XML}"
   602         COMMAND "${_SCANNER}"
   603         ARGS client-header "${_XML}" "${_WAYLAND_PROT_H_CODE}"
   604     )
   605 
   606     add_custom_command(
   607         OUTPUT "${_WAYLAND_PROT_C_CODE}"
   608         DEPENDS "${_WAYLAND_PROT_H_CODE}"
   609         COMMAND "${_SCANNER}"
   610         ARGS code "${_XML}" "${_WAYLAND_PROT_C_CODE}"
   611     )
   612 
   613     set(SOURCE_FILES ${SOURCE_FILES} "${_WAYLAND_PROT_C_CODE}")
   614 endmacro()
   615 
   616 # Requires:
   617 # - EGL
   618 # - PkgCheckModules
   619 # Optional:
   620 # - WAYLAND_SHARED opt
   621 # - HAVE_DLOPEN opt
   622 macro(CheckWayland)
   623   if(VIDEO_WAYLAND)
   624     pkg_check_modules(WAYLAND wayland-client wayland-scanner wayland-protocols wayland-egl wayland-cursor egl xkbcommon)
   625 
   626     # We have to generate some protocol interface code for some various Wayland features.
   627     if(WAYLAND_FOUND)
   628       execute_process(
   629         COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=pkgdatadir wayland-client
   630         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
   631         RESULT_VARIABLE WAYLAND_CORE_PROTOCOL_DIR_RC
   632         OUTPUT_VARIABLE WAYLAND_CORE_PROTOCOL_DIR
   633         ERROR_QUIET
   634         OUTPUT_STRIP_TRAILING_WHITESPACE
   635       )
   636       if(NOT WAYLAND_CORE_PROTOCOL_DIR_RC EQUAL 0)
   637         set(WAYLAND_FOUND FALSE)
   638       endif()
   639     endif()
   640 
   641     if(WAYLAND_FOUND)
   642       execute_process(
   643         COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=pkgdatadir wayland-protocols
   644         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
   645         RESULT_VARIABLE WAYLAND_PROTOCOLS_DIR_RC
   646         OUTPUT_VARIABLE WAYLAND_PROTOCOLS_DIR
   647         ERROR_QUIET
   648         OUTPUT_STRIP_TRAILING_WHITESPACE
   649       )
   650       if(NOT WAYLAND_PROTOCOLS_DIR_RC EQUAL 0)
   651         set(WAYLAND_FOUND FALSE)
   652       endif()
   653     endif()
   654 
   655     if(WAYLAND_FOUND)
   656       execute_process(
   657         COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=wayland_scanner wayland-scanner
   658         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
   659         RESULT_VARIABLE WAYLAND_SCANNER_RC
   660         OUTPUT_VARIABLE WAYLAND_SCANNER
   661         ERROR_QUIET
   662         OUTPUT_STRIP_TRAILING_WHITESPACE
   663       )
   664       if(NOT WAYLAND_SCANNER_RC EQUAL 0)
   665         set(WAYLAND_FOUND FALSE)
   666       endif()
   667     endif()
   668 
   669     if(WAYLAND_FOUND)
   670       link_directories(
   671           ${WAYLAND_LIBRARY_DIRS}
   672       )
   673       include_directories(
   674           ${WAYLAND_INCLUDE_DIRS}
   675       )
   676       set(HAVE_VIDEO_WAYLAND TRUE)
   677       set(HAVE_SDL_VIDEO TRUE)
   678 
   679       file(GLOB WAYLAND_SOURCES ${SDL2_SOURCE_DIR}/src/video/wayland/*.c)
   680       set(SOURCE_FILES ${SOURCE_FILES} ${WAYLAND_SOURCES})
   681 
   682       # We have to generate some protocol interface code for some unstable Wayland features.
   683       file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols")
   684       include_directories("${CMAKE_CURRENT_BINARY_DIR}/wayland-generated-protocols")
   685 
   686       WaylandProtocolGen("${WAYLAND_SCANNER}" "${WAYLAND_CORE_PROTOCOL_DIR}/wayland.xml" "wayland")
   687 
   688       foreach(_PROTL relative-pointer-unstable-v1 pointer-constraints-unstable-v1, xdg-shell-unstable-v6)
   689         string(REGEX REPLACE "\\-unstable\\-.*$" "" PROTSUBDIR ${_PROTL})
   690         WaylandProtocolGen("${WAYLAND_SCANNER}" "${WAYLAND_PROTOCOLS_DIR}/unstable/${PROTSUBDIR}/${_PROTL}.xml" "${_PROTL}")
   691       endforeach()
   692 
   693       if(VIDEO_WAYLAND_QT_TOUCH)
   694           set(SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH 1)
   695       endif()
   696 
   697       if(WAYLAND_SHARED)
   698         if(NOT HAVE_DLOPEN)
   699           message_warn("You must have SDL_LoadObject() support for dynamic Wayland loading")
   700         else()
   701           FindLibraryAndSONAME(wayland-client)
   702           FindLibraryAndSONAME(wayland-egl)
   703           FindLibraryAndSONAME(wayland-cursor)
   704           FindLibraryAndSONAME(xkbcommon)
   705           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC "\"${WAYLAND_CLIENT_LIB_SONAME}\"")
   706           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL "\"${WAYLAND_EGL_LIB_SONAME}\"")
   707           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR "\"${WAYLAND_CURSOR_LIB_SONAME}\"")
   708           set(SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON "\"${XKBCOMMON_LIB_SONAME}\"")
   709           set(HAVE_WAYLAND_SHARED TRUE)
   710         endif()
   711       else()
   712         set(EXTRA_LIBS ${WAYLAND_LIBRARIES} ${EXTRA_LIBS})
   713       endif()
   714 
   715       set(SDL_VIDEO_DRIVER_WAYLAND 1)
   716     endif()
   717   endif()
   718 endmacro()
   719 
   720 # Requires:
   721 # - n/a
   722 #
   723 macro(CheckCOCOA)
   724   if(VIDEO_COCOA)
   725     if(APPLE) # Apple always has Cocoa.
   726       set(HAVE_VIDEO_COCOA TRUE)
   727     endif()
   728     if(HAVE_VIDEO_COCOA)
   729       file(GLOB COCOA_SOURCES ${SDL2_SOURCE_DIR}/src/video/cocoa/*.m)
   730       set_source_files_properties(${COCOA_SOURCES} PROPERTIES LANGUAGE C)
   731       set(SOURCE_FILES ${SOURCE_FILES} ${COCOA_SOURCES})
   732       set(SDL_VIDEO_DRIVER_COCOA 1)
   733       set(HAVE_SDL_VIDEO TRUE)
   734     endif()
   735   endif()
   736 endmacro()
   737 
   738 # Requires:
   739 # - PkgCheckModules
   740 # Optional:
   741 # - DIRECTFB_SHARED opt
   742 # - HAVE_DLOPEN opt
   743 macro(CheckDirectFB)
   744   if(VIDEO_DIRECTFB)
   745     pkg_check_modules(PKG_DIRECTFB directfb>=1.0.0)
   746     if(PKG_DIRECTFB_FOUND)
   747       set(HAVE_VIDEO_DIRECTFB TRUE)
   748       file(GLOB DIRECTFB_SOURCES ${SDL2_SOURCE_DIR}/src/video/directfb/*.c)
   749       set(SOURCE_FILES ${SOURCE_FILES} ${DIRECTFB_SOURCES})
   750       set(SDL_VIDEO_DRIVER_DIRECTFB 1)
   751       set(SDL_VIDEO_RENDER_DIRECTFB 1)
   752       list(APPEND EXTRA_CFLAGS ${PKG_DIRECTFB_CFLAGS})
   753       if(DIRECTFB_SHARED)
   754         if(NOT HAVE_DLOPEN)
   755           message_warn("You must have SDL_LoadObject() support for dynamic DirectFB loading")
   756         else()
   757           FindLibraryAndSONAME("directfb")
   758           set(SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC "\"${DIRECTFB_LIB_SONAME}\"")
   759           set(HAVE_DIRECTFB_SHARED TRUE)
   760         endif()
   761       else()
   762         list(APPEND EXTRA_LDFLAGS ${PKG_DIRECTFB_LDFLAGS})
   763       endif()
   764       set(HAVE_SDL_VIDEO TRUE)
   765     endif()
   766   endif()
   767 endmacro()
   768 
   769 # Requires:
   770 # - n/a
   771 macro(CheckVivante)
   772   if(VIDEO_VIVANTE)
   773     check_c_source_compiles("
   774         #include <gc_vdk.h>
   775         int main(int argc, char** argv) {}" HAVE_VIDEO_VIVANTE_VDK)
   776     check_c_source_compiles("
   777         #define LINUX
   778         #define EGL_API_FB
   779         #include <EGL/eglvivante.h>
   780         int main(int argc, char** argv) {}" HAVE_VIDEO_VIVANTE_EGL_FB)
   781     if(HAVE_VIDEO_VIVANTE_VDK OR HAVE_VIDEO_VIVANTE_EGL_FB)
   782       set(HAVE_VIDEO_VIVANTE TRUE)
   783       set(HAVE_SDL_VIDEO TRUE)
   784 
   785       file(GLOB VIVANTE_SOURCES ${SDL2_SOURCE_DIR}/src/video/vivante/*.c)
   786       set(SOURCE_FILES ${SOURCE_FILES} ${VIVANTE_SOURCES})
   787       set(SDL_VIDEO_DRIVER_VIVANTE 1)
   788       if(HAVE_VIDEO_VIVANTE_VDK)
   789         set(SDL_VIDEO_DRIVER_VIVANTE_VDK 1)
   790         list(APPEND EXTRA_LIBS VDK VIVANTE)
   791       else()
   792         set(SDL_CFLAGS "${SDL_CFLAGS} -DLINUX -DEGL_API_FB")
   793         list(APPEND EXTRA_LIBS EGL)
   794       endif(HAVE_VIDEO_VIVANTE_VDK)
   795     endif(HAVE_VIDEO_VIVANTE_VDK OR HAVE_VIDEO_VIVANTE_EGL_FB)
   796   endif(VIDEO_VIVANTE)
   797 endmacro(CheckVivante)
   798 
   799 # Requires:
   800 # - nada
   801 macro(CheckOpenGLX11)
   802   if(VIDEO_OPENGL)
   803     check_c_source_compiles("
   804         #include <GL/gl.h>
   805         #include <GL/glx.h>
   806         int main(int argc, char** argv) {}" HAVE_VIDEO_OPENGL)
   807 
   808     if(HAVE_VIDEO_OPENGL)
   809       set(HAVE_VIDEO_OPENGL TRUE)
   810       set(SDL_VIDEO_OPENGL 1)
   811       set(SDL_VIDEO_OPENGL_GLX 1)
   812       set(SDL_VIDEO_RENDER_OGL 1)
   813     endif()
   814   endif()
   815 endmacro()
   816 
   817 # Requires:
   818 # - nada
   819 macro(CheckOpenGLESX11)
   820   if(VIDEO_OPENGLES)
   821     check_c_source_compiles("
   822         #define EGL_API_FB
   823         #include <EGL/egl.h>
   824         int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGL_EGL)
   825     if(HAVE_VIDEO_OPENGL_EGL)
   826         set(SDL_VIDEO_OPENGL_EGL 1)
   827     endif()
   828     check_c_source_compiles("
   829       #include <GLES/gl.h>
   830       #include <GLES/glext.h>
   831       int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGLES_V1)
   832     if(HAVE_VIDEO_OPENGLES_V1)
   833         set(HAVE_VIDEO_OPENGLES TRUE)
   834         set(SDL_VIDEO_OPENGL_ES 1)
   835         set(SDL_VIDEO_RENDER_OGL_ES 1)
   836     endif()
   837     check_c_source_compiles("
   838       #include <GLES2/gl2.h>
   839       #include <GLES2/gl2ext.h>
   840       int main (int argc, char** argv) {}" HAVE_VIDEO_OPENGLES_V2)
   841     if(HAVE_VIDEO_OPENGLES_V2)
   842         set(HAVE_VIDEO_OPENGLES TRUE)
   843         set(SDL_VIDEO_OPENGL_ES2 1)
   844         set(SDL_VIDEO_RENDER_OGL_ES2 1)
   845     endif()
   846 
   847   endif()
   848 endmacro()
   849 
   850 # Requires:
   851 # - nada
   852 # Optional:
   853 # - THREADS opt
   854 # Sets:
   855 # PTHREAD_CFLAGS
   856 # PTHREAD_LIBS
   857 macro(CheckPTHREAD)
   858   if(PTHREADS)
   859     if(ANDROID)
   860       # the android libc provides built-in support for pthreads, so no
   861       # additional linking or compile flags are necessary
   862     elseif(LINUX)
   863       set(PTHREAD_CFLAGS "-D_REENTRANT")
   864       set(PTHREAD_LDFLAGS "-pthread")
   865     elseif(BSDI)
   866       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   867       set(PTHREAD_LDFLAGS "")
   868     elseif(DARWIN)
   869       set(PTHREAD_CFLAGS "-D_THREAD_SAFE")
   870       # causes Carbon.p complaints?
   871       # set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   872       set(PTHREAD_LDFLAGS "")
   873     elseif(FREEBSD)
   874       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   875       set(PTHREAD_LDFLAGS "-pthread")
   876     elseif(NETBSD)
   877       set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE")
   878       set(PTHREAD_LDFLAGS "-lpthread")
   879     elseif(OPENBSD)
   880       set(PTHREAD_CFLAGS "-D_REENTRANT")
   881       set(PTHREAD_LDFLAGS "-pthread")
   882     elseif(SOLARIS)
   883       set(PTHREAD_CFLAGS "-D_REENTRANT")
   884       set(PTHREAD_LDFLAGS "-pthread -lposix4")
   885     elseif(SYSV5)
   886       set(PTHREAD_CFLAGS "-D_REENTRANT -Kthread")
   887       set(PTHREAD_LDFLAGS "")
   888     elseif(AIX)
   889       set(PTHREAD_CFLAGS "-D_REENTRANT -mthreads")
   890       set(PTHREAD_LDFLAGS "-pthread")
   891     elseif(HPUX)
   892       set(PTHREAD_CFLAGS "-D_REENTRANT")
   893       set(PTHREAD_LDFLAGS "-L/usr/lib -pthread")
   894     elseif(HAIKU)
   895       set(PTHREAD_CFLAGS "-D_REENTRANT")
   896       set(PTHREAD_LDFLAGS "")
   897     else()
   898       set(PTHREAD_CFLAGS "-D_REENTRANT")
   899       set(PTHREAD_LDFLAGS "-lpthread")
   900     endif()
   901 
   902     # Run some tests
   903     set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
   904     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${PTHREAD_CFLAGS} ${PTHREAD_LDFLAGS}")
   905     if(CMAKE_CROSSCOMPILING)
   906       set(HAVE_PTHREADS 1)
   907     else()
   908       check_c_source_runs("
   909         #include <pthread.h>
   910         int main(int argc, char** argv) {
   911           pthread_attr_t type;
   912           pthread_attr_init(&type);
   913           return 0;
   914         }" HAVE_PTHREADS)
   915     endif()
   916     if(HAVE_PTHREADS)
   917       set(SDL_THREAD_PTHREAD 1)
   918       list(APPEND EXTRA_CFLAGS ${PTHREAD_CFLAGS})
   919       list(APPEND EXTRA_LDFLAGS ${PTHREAD_LDFLAGS})
   920       set(SDL_CFLAGS "${SDL_CFLAGS} ${PTHREAD_CFLAGS}")
   921       list(APPEND SDL_LIBS ${PTHREAD_LDFLAGS})
   922 
   923       check_c_source_compiles("
   924         #include <pthread.h>
   925         int main(int argc, char **argv) {
   926           pthread_mutexattr_t attr;
   927           pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
   928           return 0;
   929         }" HAVE_RECURSIVE_MUTEXES)
   930       if(HAVE_RECURSIVE_MUTEXES)
   931         set(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 1)
   932       else()
   933         check_c_source_compiles("
   934             #include <pthread.h>
   935             int main(int argc, char **argv) {
   936               pthread_mutexattr_t attr;
   937               pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
   938               return 0;
   939             }" HAVE_RECURSIVE_MUTEXES_NP)
   940         if(HAVE_RECURSIVE_MUTEXES_NP)
   941           set(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP 1)
   942         endif()
   943       endif()
   944 
   945       if(PTHREADS_SEM)
   946         check_c_source_compiles("#include <pthread.h>
   947                                  #include <semaphore.h>
   948                                  int main(int argc, char **argv) { return 0; }" HAVE_PTHREADS_SEM)
   949         if(HAVE_PTHREADS_SEM)
   950           check_c_source_compiles("
   951               #include <pthread.h>
   952               #include <semaphore.h>
   953               int main(int argc, char **argv) {
   954                   sem_timedwait(NULL, NULL);
   955                   return 0;
   956               }" HAVE_SEM_TIMEDWAIT)
   957         endif()
   958       endif()
   959 
   960       check_c_source_compiles("
   961           #include <pthread.h>
   962           #include <pthread_np.h>
   963           int main(int argc, char** argv) { return 0; }" HAVE_PTHREAD_NP_H)
   964       check_function_exists(pthread_setname_np HAVE_PTHREAD_SETNAME_NP)
   965       check_function_exists(pthread_set_name_np HAVE_PTHREAD_SET_NAME_NP)
   966 
   967       set(SOURCE_FILES ${SOURCE_FILES}
   968           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_systhread.c
   969           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_sysmutex.c   # Can be faked, if necessary
   970           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_syscond.c    # Can be faked, if necessary
   971           ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_systls.c
   972           )
   973       if(HAVE_PTHREADS_SEM)
   974         set(SOURCE_FILES ${SOURCE_FILES}
   975             ${SDL2_SOURCE_DIR}/src/thread/pthread/SDL_syssem.c)
   976       else()
   977         set(SOURCE_FILES ${SOURCE_FILES}
   978             ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_syssem.c)
   979       endif()
   980       set(HAVE_SDL_THREADS TRUE)
   981     endif()
   982     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}")
   983   endif()
   984 endmacro()
   985 
   986 # Requires
   987 # - nada
   988 # Optional:
   989 # Sets:
   990 # USB_LIBS
   991 # USB_CFLAGS
   992 macro(CheckUSBHID)
   993   check_library_exists(usbhid hid_init "" LIBUSBHID)
   994   if(LIBUSBHID)
   995     check_include_file(usbhid.h HAVE_USBHID_H)
   996     if(HAVE_USBHID_H)
   997       set(USB_CFLAGS "-DHAVE_USBHID_H")
   998     endif()
   999 
  1000     check_include_file(libusbhid.h HAVE_LIBUSBHID_H)
  1001     if(HAVE_LIBUSBHID_H)
  1002       set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSBHID_H")
  1003     endif()
  1004     set(USB_LIBS ${USB_LIBS} usbhid)
  1005   else()
  1006     check_include_file(usb.h HAVE_USB_H)
  1007     if(HAVE_USB_H)
  1008       set(USB_CFLAGS "-DHAVE_USB_H")
  1009     endif()
  1010     check_include_file(libusb.h HAVE_LIBUSB_H)
  1011     if(HAVE_LIBUSB_H)
  1012       set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSB_H")
  1013     endif()
  1014     check_library_exists(usb hid_init "" LIBUSB)
  1015     if(LIBUSB)
  1016       set(USB_LIBS ${USB_LIBS} usb)
  1017     endif()
  1018   endif()
  1019 
  1020   set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  1021   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${USB_CFLAGS}")
  1022   set(CMAKE_REQUIRED_LIBRARIES "${USB_LIBS}")
  1023   check_c_source_compiles("
  1024        #include <sys/types.h>
  1025         #if defined(HAVE_USB_H)
  1026         #include <usb.h>
  1027         #endif
  1028         #ifdef __DragonFly__
  1029         # include <bus/usb/usb.h>
  1030         # include <bus/usb/usbhid.h>
  1031         #else
  1032         # include <dev/usb/usb.h>
  1033         # include <dev/usb/usbhid.h>
  1034         #endif
  1035         #if defined(HAVE_USBHID_H)
  1036         #include <usbhid.h>
  1037         #elif defined(HAVE_LIBUSB_H)
  1038         #include <libusb.h>
  1039         #elif defined(HAVE_LIBUSBHID_H)
  1040         #include <libusbhid.h>
  1041         #endif
  1042         int main(int argc, char **argv) {
  1043           struct report_desc *repdesc;
  1044           struct usb_ctl_report *repbuf;
  1045           hid_kind_t hidkind;
  1046           return 0;
  1047         }" HAVE_USBHID)
  1048   if(HAVE_USBHID)
  1049     check_c_source_compiles("
  1050           #include <sys/types.h>
  1051           #if defined(HAVE_USB_H)
  1052           #include <usb.h>
  1053           #endif
  1054           #ifdef __DragonFly__
  1055           # include <bus/usb/usb.h>
  1056           # include <bus/usb/usbhid.h>
  1057           #else
  1058           # include <dev/usb/usb.h>
  1059           # include <dev/usb/usbhid.h>
  1060           #endif
  1061           #if defined(HAVE_USBHID_H)
  1062           #include <usbhid.h>
  1063           #elif defined(HAVE_LIBUSB_H)
  1064           #include <libusb.h>
  1065           #elif defined(HAVE_LIBUSBHID_H)
  1066           #include <libusbhid.h>
  1067           #endif
  1068           int main(int argc, char** argv) {
  1069             struct usb_ctl_report buf;
  1070             if (buf.ucr_data) { }
  1071             return 0;
  1072           }" HAVE_USBHID_UCR_DATA)
  1073     if(HAVE_USBHID_UCR_DATA)
  1074       set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_UCR_DATA")
  1075     endif()
  1076 
  1077     check_c_source_compiles("
  1078           #include <sys/types.h>
  1079           #if defined(HAVE_USB_H)
  1080           #include <usb.h>
  1081           #endif
  1082           #ifdef __DragonFly__
  1083           #include <bus/usb/usb.h>
  1084           #include <bus/usb/usbhid.h>
  1085           #else
  1086           #include <dev/usb/usb.h>
  1087           #include <dev/usb/usbhid.h>
  1088           #endif
  1089           #if defined(HAVE_USBHID_H)
  1090           #include <usbhid.h>
  1091           #elif defined(HAVE_LIBUSB_H)
  1092           #include <libusb.h>
  1093           #elif defined(HAVE_LIBUSBHID_H)
  1094           #include <libusbhid.h>
  1095           #endif
  1096           int main(int argc, char **argv) {
  1097             report_desc_t d;
  1098             hid_start_parse(d, 1, 1);
  1099             return 0;
  1100           }" HAVE_USBHID_NEW)
  1101     if(HAVE_USBHID_NEW)
  1102       set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_NEW")
  1103     endif()
  1104 
  1105     check_c_source_compiles("
  1106         #include <machine/joystick.h>
  1107         int main(int argc, char** argv) {
  1108             struct joystick t;
  1109             return 0;
  1110         }" HAVE_MACHINE_JOYSTICK)
  1111     if(HAVE_MACHINE_JOYSTICK)
  1112       set(SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H 1)
  1113     endif()
  1114     set(SDL_JOYSTICK_USBHID 1)
  1115     file(GLOB BSD_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/bsd/*.c)
  1116     set(SOURCE_FILES ${SOURCE_FILES} ${BSD_JOYSTICK_SOURCES})
  1117     list(APPEND EXTRA_CFLAGS ${USB_CFLAGS})
  1118     list(APPEND EXTRA_LIBS ${USB_LIBS})
  1119     set(HAVE_SDL_JOYSTICK TRUE)
  1120 
  1121     set(CMAKE_REQUIRED_LIBRARIES)
  1122     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}")
  1123   endif()
  1124 endmacro()
  1125 
  1126 # Requires:
  1127 # - n/a
  1128 macro(CheckRPI)
  1129   if(VIDEO_RPI)
  1130     pkg_check_modules(VIDEO_RPI bcm_host brcmegl)
  1131     if (NOT VIDEO_RPI_FOUND)
  1132       set(VIDEO_RPI_INCLUDE_DIRS "/opt/vc/include" "/opt/vc/include/interface/vcos/pthreads" "/opt/vc/include/interface/vmcs_host/linux/" )
  1133       set(VIDEO_RPI_LIBRARY_DIRS "/opt/vc/lib" )
  1134       set(VIDEO_RPI_LIBRARIES bcm_host )
  1135       set(VIDEO_RPI_LDFLAGS "-Wl,-rpath,/opt/vc/lib")
  1136     endif()
  1137     listtostr(VIDEO_RPI_INCLUDE_DIRS VIDEO_RPI_INCLUDE_FLAGS "-I")
  1138     listtostr(VIDEO_RPI_LIBRARY_DIRS VIDEO_RPI_LIBRARY_FLAGS "-L")
  1139 
  1140     set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  1141     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${VIDEO_RPI_INCLUDE_FLAGS} ${VIDEO_RPI_LIBRARY_FLAGS}")
  1142     set(CMAKE_REQUIRED_LIBRARIES "${VIDEO_RPI_LIBRARIES}")
  1143     check_c_source_compiles("
  1144         #include <bcm_host.h>
  1145         int main(int argc, char **argv) {}" HAVE_VIDEO_RPI)
  1146     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}")
  1147     set(CMAKE_REQUIRED_LIBRARIES)
  1148 
  1149     if(SDL_VIDEO AND HAVE_VIDEO_RPI)
  1150       set(HAVE_SDL_VIDEO TRUE)
  1151       set(SDL_VIDEO_DRIVER_RPI 1)
  1152       file(GLOB VIDEO_RPI_SOURCES ${SDL2_SOURCE_DIR}/src/video/raspberry/*.c)
  1153       set(SOURCE_FILES ${SOURCE_FILES} ${VIDEO_RPI_SOURCES})
  1154       list(APPEND EXTRA_LIBS ${VIDEO_RPI_LIBRARIES})
  1155       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VIDEO_RPI_INCLUDE_FLAGS} ${VIDEO_RPI_LIBRARY_FLAGS}")
  1156       list(APPEND EXTRA_LDFLAGS ${VIDEO_RPI_LDFLAGS})
  1157     endif(SDL_VIDEO AND HAVE_VIDEO_RPI)
  1158   endif(VIDEO_RPI)
  1159 endmacro(CheckRPI)
  1160 
  1161 # Requires:
  1162 # - EGL
  1163 # - PkgCheckModules
  1164 # Optional:
  1165 # - KMSDRM_SHARED opt
  1166 # - HAVE_DLOPEN opt
  1167 macro(CheckKMSDRM)
  1168   if(VIDEO_KMSDRM)
  1169     pkg_check_modules(KMSDRM libdrm gbm egl)
  1170     if(KMSDRM_FOUND)
  1171       link_directories(
  1172         ${KMSDRM_LIBRARY_DIRS}
  1173       )
  1174       include_directories(
  1175         ${KMSDRM_INCLUDE_DIRS}
  1176       )
  1177       set(HAVE_VIDEO_KMSDRM TRUE)
  1178       set(HAVE_SDL_VIDEO TRUE)
  1179 
  1180       file(GLOB KMSDRM_SOURCES ${SDL2_SOURCE_DIR}/src/video/kmsdrm/*.c)
  1181       set(SOURCE_FILES ${SOURCE_FILES} ${KMSDRM_SOURCES})
  1182 
  1183       list(APPEND EXTRA_CFLAGS ${KMSDRM_CFLAGS})
  1184 
  1185       set(SDL_VIDEO_DRIVER_KMSDRM 1)
  1186 
  1187       if(KMSDRM_SHARED)
  1188         if(NOT HAVE_DLOPEN)
  1189           message_warn("You must have SDL_LoadObject() support for dynamic KMS/DRM loading")
  1190         else()
  1191           FindLibraryAndSONAME(drm)
  1192           FindLibraryAndSONAME(gbm)
  1193           set(SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC "\"${DRM_LIB_SONAME}\"")
  1194           set(SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM "\"${GBM_LIB_SONAME}\"")
  1195           set(HAVE_KMSDRM_SHARED TRUE)
  1196         endif()
  1197       else()
  1198         set(EXTRA_LIBS ${KMSDRM_LIBRARIES} ${EXTRA_LIBS})
  1199       endif()
  1200     endif()
  1201   endif()
  1202 endmacro()