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