CMakeLists.txt
author Brandon Schaefer
Tue, 24 Sep 2019 16:36:48 -0400
changeset 13089 9692ad570003
parent 13086 144fc4e62b72
child 13102 f54a77124843
permissions -rw-r--r--
offscreen: Add new video driver backend Offscreen

The Offscreen video driver is intended to be used for headless rendering
as well as allows for multiple GPUs to be used for headless rendering

Currently only supports EGL (OpenGL / ES) or Framebuffers
Adds a hint to specifiy which EGL device to use: SDL_HINT_EGL_DEVICE
Adds testoffscreen.c which can be used to test the backend out
Disabled by default for now
     1 if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
     2   message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the SDL source code and call cmake from there")
     3 endif()
     4 
     5 cmake_minimum_required(VERSION 2.8.11)
     6 project(SDL2 C CXX)
     7 
     8 # !!! FIXME: this should probably do "MACOSX_RPATH ON" as a target property
     9 # !!! FIXME:  for the SDL2 shared library (so you get an
    10 # !!! FIXME:  install_name ("soname") of "@rpath/libSDL-whatever.dylib"
    11 # !!! FIXME:  instead of "/usr/local/lib/libSDL-whatever.dylib"), but I'm
    12 # !!! FIXME:  punting for now and leaving the existing behavior. Until this
    13 # !!! FIXME:  properly resolved, this line silences a warning in CMake 3.0+.
    14 # !!! FIXME:  remove it and this comment entirely once the problem is
    15 # !!! FIXME:  properly resolved.
    16 #cmake_policy(SET CMP0042 OLD)
    17 
    18 include(CheckFunctionExists)
    19 include(CheckLibraryExists)
    20 include(CheckIncludeFiles)
    21 include(CheckIncludeFile)
    22 include(CheckSymbolExists)
    23 include(CheckCSourceCompiles)
    24 include(CheckCSourceRuns)
    25 include(CheckCCompilerFlag)
    26 include(CheckTypeSize)
    27 include(CheckStructHasMember)
    28 include(CMakeDependentOption)
    29 include(FindPkgConfig)
    30 include(GNUInstallDirs)
    31 set(CMAKE_MODULE_PATH "${SDL2_SOURCE_DIR}/cmake")
    32 include(${SDL2_SOURCE_DIR}/cmake/macros.cmake)
    33 include(${SDL2_SOURCE_DIR}/cmake/sdlchecks.cmake)
    34 
    35 # General settings
    36 # Edit include/SDL_version.h and change the version, then:
    37 #   SDL_MICRO_VERSION += 1;
    38 #   SDL_INTERFACE_AGE += 1;
    39 #   SDL_BINARY_AGE += 1;
    40 # if any functions have been added, set SDL_INTERFACE_AGE to 0.
    41 # if backwards compatibility has been broken,
    42 # set SDL_BINARY_AGE and SDL_INTERFACE_AGE to 0.
    43 set(SDL_MAJOR_VERSION 2)
    44 set(SDL_MINOR_VERSION 0)
    45 set(SDL_MICRO_VERSION 11)
    46 set(SDL_INTERFACE_AGE 0)
    47 set(SDL_BINARY_AGE 11)
    48 set(SDL_VERSION "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}.${SDL_MICRO_VERSION}")
    49 # the following should match the versions in Xcode project file:
    50 set(DYLIB_CURRENT_VERSION 10.0.0)
    51 set(DYLIB_COMPATIBILITY_VERSION 1.0.0)
    52 
    53 # Set defaults preventing destination file conflicts
    54 set(SDL_CMAKE_DEBUG_POSTFIX "d"
    55     CACHE STRING "Name suffix for debug builds")
    56 
    57 mark_as_advanced(CMAKE_IMPORT_LIBRARY_SUFFIX SDL_CMAKE_DEBUG_POSTFIX)
    58 
    59 # Calculate a libtool-like version number
    60 math(EXPR LT_CURRENT "${SDL_MICRO_VERSION} - ${SDL_INTERFACE_AGE}")
    61 math(EXPR LT_AGE "${SDL_BINARY_AGE} - ${SDL_INTERFACE_AGE}")
    62 math(EXPR LT_MAJOR "${LT_CURRENT}- ${LT_AGE}")
    63 set(LT_REVISION "${SDL_INTERFACE_AGE}")
    64 set(LT_RELEASE "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}")
    65 set(LT_VERSION "${LT_MAJOR}.${LT_AGE}.${LT_REVISION}")
    66 
    67 #message(STATUS "${LT_VERSION} :: ${LT_AGE} :: ${LT_REVISION} :: ${LT_CURRENT} :: ${LT_RELEASE}")
    68 
    69 # General settings & flags
    70 set(LIBRARY_OUTPUT_DIRECTORY "build")
    71 # Check for 64 or 32 bit
    72 set(SIZEOF_VOIDP ${CMAKE_SIZEOF_VOID_P})
    73 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    74   set(ARCH_64 TRUE)
    75   set(PROCESSOR_ARCH "x64")
    76 else()
    77   set(ARCH_64 FALSE)
    78   set(PROCESSOR_ARCH "x86")
    79 endif()
    80 set(LIBNAME SDL2)
    81 if(NOT LIBTYPE)
    82   set(LIBTYPE SHARED)
    83 endif()
    84 
    85 # Get the platform
    86 if(WIN32)
    87   if(NOT WINDOWS)
    88     set(WINDOWS TRUE)
    89   endif()
    90 elseif(UNIX AND NOT APPLE)
    91   if(CMAKE_SYSTEM_NAME MATCHES ".*Linux")
    92     set(LINUX TRUE)
    93   elseif(CMAKE_SYSTEM_NAME MATCHES "kFreeBSD.*")
    94     set(FREEBSD TRUE)
    95   elseif(CMAKE_SYSTEM_NAME MATCHES "kNetBSD.*|NetBSD.*")
    96     set(NETBSD TRUE)
    97   elseif(CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*")
    98     set(OPENBSD TRUE)
    99   elseif(CMAKE_SYSTEM_NAME MATCHES ".*GNU.*")
   100     set(GNU TRUE)
   101   elseif(CMAKE_SYSTEM_NAME MATCHES ".*BSDI.*")
   102     set(BSDI TRUE)
   103   elseif(CMAKE_SYSTEM_NAME MATCHES "DragonFly.*|FreeBSD")
   104     set(FREEBSD TRUE)
   105   elseif(CMAKE_SYSTEM_NAME MATCHES "SYSV5.*")
   106     set(SYSV5 TRUE)
   107   elseif(CMAKE_SYSTEM_NAME MATCHES "Solaris.*")
   108     set(SOLARIS TRUE)
   109   elseif(CMAKE_SYSTEM_NAME MATCHES "HP-UX.*")
   110     set(HPUX TRUE)
   111   elseif(CMAKE_SYSTEM_NAME MATCHES "AIX.*")
   112     set(AIX TRUE)
   113   elseif(CMAKE_SYSTEM_NAME MATCHES "Minix.*")
   114     set(MINIX TRUE)
   115   endif()
   116 elseif(APPLE)
   117   if(CMAKE_SYSTEM_NAME MATCHES ".*Darwin.*")
   118     set(DARWIN TRUE)
   119   elseif(CMAKE_SYSTEM_NAME MATCHES ".*MacOS.*")
   120     set(MACOSX TRUE)
   121   elseif(CMAKE_SYSTEM_NAME MATCHES ".*tvOS.*")
   122     set(TVOS TRUE)
   123   endif()
   124   # TODO: iOS?
   125 elseif(CMAKE_SYSTEM_NAME MATCHES "BeOS.*")
   126   message_error("BeOS support has been removed as of SDL 2.0.2.")
   127 elseif(CMAKE_SYSTEM_NAME MATCHES "Haiku.*")
   128   set(HAIKU TRUE)
   129 endif()
   130 
   131 # Don't mistake osx for unix
   132 if(UNIX AND NOT APPLE)
   133   set(UNIX_SYS ON)
   134 else()
   135   set(UNIX_SYS OFF)
   136 endif()
   137 
   138 if(UNIX OR APPLE)
   139   set(UNIX_OR_MAC_SYS ON)
   140 else()
   141   set(UNIX_OR_MAC_SYS OFF)
   142 endif()
   143 
   144 if (UNIX_OR_MAC_SYS AND NOT EMSCRIPTEN) # JavaScript does not yet have threading support, so disable pthreads when building for Emscripten.
   145   set(SDL_PTHREADS_ENABLED_BY_DEFAULT ON)
   146 else()
   147   set(SDL_PTHREADS_ENABLED_BY_DEFAULT OFF)
   148 endif()
   149 
   150 # Default option knobs
   151 if(APPLE OR ARCH_64)
   152   if(NOT "${CMAKE_OSX_ARCHITECTURES}" MATCHES "arm")
   153     set(OPT_DEF_SSEMATH ON)
   154   endif()
   155 endif()
   156 if(UNIX OR MINGW OR MSYS)
   157   set(OPT_DEF_LIBC ON)
   158 endif()
   159 
   160 # The hidraw support doesn't catch Xbox, PS4 and Nintendo controllers,
   161 #  so we'll just use libusb when it's available. libusb does not support iOS,
   162 #  so we default to yes on iOS.
   163 #  TODO: Windows can support libusb, the hid.c file just depends on Unix APIs
   164 if(WINDOWS OR IOS OR TVOS OR ANDROID)
   165   set(HIDAPI_SKIP_LIBUSB TRUE)
   166 else()
   167   set(HIDAPI_SKIP_LIBUSB FALSE)
   168 endif()
   169 if (HIDAPI_SKIP_LIBUSB)
   170   set(OPT_DEF_HIDAPI ON)
   171 endif()
   172 
   173 # On the other hand, *BSD specifically uses libusb only, so we make a special
   174 #  case just for them.
   175 if(FREEBSD OR NETBSD OR OPENBSD OR BSDI)
   176   set(HIDAPI_ONLY_LIBUSB TRUE)
   177 else()
   178   set(HIDAPI_ONLY_LIBUSB FALSE)
   179 endif()
   180 
   181 # Compiler info
   182 if(CMAKE_COMPILER_IS_GNUCC)
   183   set(USE_GCC TRUE)
   184   set(OPT_DEF_ASM TRUE)
   185 elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
   186   set(USE_CLANG TRUE)
   187   set(OPT_DEF_ASM TRUE)
   188 elseif(MSVC_VERSION GREATER 1400) # VisualStudio 8.0+
   189   set(OPT_DEF_ASM TRUE)
   190   #set(CMAKE_C_FLAGS "/ZI /WX- /
   191 else()
   192   set(OPT_DEF_ASM FALSE)
   193 endif()
   194 
   195 if(USE_GCC OR USE_CLANG)
   196   set(OPT_DEF_GCC_ATOMICS ON)
   197 endif()
   198 
   199 # Default flags, if not set otherwise
   200 if("$ENV{CFLAGS}" STREQUAL "")
   201   if(CMAKE_BUILD_TYPE STREQUAL "")
   202     if(USE_GCC OR USE_CLANG)
   203       set(CMAKE_C_FLAGS "-g -O3")
   204     endif()
   205   endif()
   206 else()
   207   set(CMAKE_C_FLAGS "$ENV{CFLAGS}")
   208   list(APPEND EXTRA_CFLAGS "$ENV{CFLAGS}")
   209 endif()
   210 if(NOT ("$ENV{CFLAGS}" STREQUAL "")) # Hackish, but does the trick on Win32
   211   list(APPEND EXTRA_LDFLAGS "$ENV{LDFLAGS}")
   212 endif()
   213 
   214 if(MSVC)
   215   option(FORCE_STATIC_VCRT "Force /MT for static VC runtimes" OFF)
   216   if(FORCE_STATIC_VCRT)
   217     foreach(flag_var
   218         CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
   219         CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
   220       if(${flag_var} MATCHES "/MD")
   221         string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
   222       endif()
   223     endforeach()
   224   endif()
   225 
   226   # Make sure /RTC1 is disabled, otherwise it will use functions from the CRT
   227   foreach(flag_var
   228       CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
   229       CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
   230     string(REGEX REPLACE "/RTC(su|[1su])" "" ${flag_var} "${${flag_var}}")
   231   endforeach(flag_var)
   232 endif()
   233 
   234 # Those are used for pkg-config and friends, so that the SDL2.pc, sdl2-config,
   235 # etc. are created correctly.
   236 set(SDL_LIBS "-lSDL2")
   237 set(SDL_CFLAGS "")
   238 
   239 # When building shared lib for Windows with MinGW,
   240 # avoid the DLL having a "lib" prefix
   241 if(WINDOWS)
   242   set(CMAKE_SHARED_LIBRARY_PREFIX "")
   243 endif()
   244 
   245 # Emscripten toolchain has a nonempty default value for this, and the checks
   246 # in this file need to change that, so remember the original value, and
   247 # restore back to that afterwards. For check_function_exists() to work in
   248 # Emscripten, this value must be at its default value.
   249 set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
   250 
   251 if(CYGWIN)
   252   # We build SDL on cygwin without the UNIX emulation layer
   253   include_directories("-I/usr/include/mingw")
   254   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mno-cygwin")
   255   check_c_source_compiles("int main(int argc, char **argv) {}"
   256     HAVE_GCC_NO_CYGWIN)
   257   set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   258   if(HAVE_GCC_NO_CYGWIN)
   259     list(APPEND EXTRA_LDFLAGS "-mno-cygwin")
   260     list(APPEND SDL_LIBS "-mno-cygwin")
   261   endif()
   262   set(SDL_CFLAGS "${SDL_CFLAGS} -I/usr/include/mingw")
   263 endif()
   264 
   265 add_definitions(-DUSING_GENERATED_CONFIG_H)
   266 # General includes
   267 include_directories(${SDL2_BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)
   268 if(USE_GCC OR USE_CLANG)
   269   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -idirafter ${SDL2_SOURCE_DIR}/src/video/khronos")
   270 else()
   271   include_directories(${SDL2_SOURCE_DIR}/src/video/khronos)
   272 endif()
   273 
   274 # All these ENABLED_BY_DEFAULT vars will default to ON if not specified, so
   275 #  you only need to have a platform override them if they are disabling.
   276 set(OPT_DEF_ASM TRUE)
   277 if(EMSCRIPTEN)
   278   # Set up default values for the currently supported set of subsystems:
   279   # Emscripten/Javascript does not have assembly support, a dynamic library
   280   # loading architecture, low-level CPU inspection or multithreading.
   281   set(OPT_DEF_ASM FALSE)
   282   set(SDL_SHARED_ENABLED_BY_DEFAULT OFF)
   283   set(SDL_ATOMIC_ENABLED_BY_DEFAULT OFF)
   284   set(SDL_THREADS_ENABLED_BY_DEFAULT OFF)
   285   set(SDL_LOADSO_ENABLED_BY_DEFAULT OFF)
   286   set(SDL_CPUINFO_ENABLED_BY_DEFAULT OFF)
   287   set(SDL_DLOPEN_ENABLED_BY_DEFAULT OFF)
   288 endif()
   289 
   290 # When defined, respect CMake's BUILD_SHARED_LIBS setting:
   291 set(SDL_STATIC_ENABLED_BY_DEFAULT ON)
   292 if (NOT DEFINED SDL_SHARED_ENABLED_BY_DEFAULT)
   293   # ...unless decided already (as for EMSCRIPTEN)
   294 
   295   set(SDL_SHARED_ENABLED_BY_DEFAULT OFF)
   296 
   297   if (NOT DEFINED BUILD_SHARED_LIBS)
   298     # No preference? Build both, just like the AC/AM configure
   299     set(SDL_SHARED_ENABLED_BY_DEFAULT ON)
   300 
   301   elseif (BUILD_SHARED_LIBS)
   302     # In this case, we assume the user wants a shared lib and don't build
   303     # the static one
   304     set(SDL_SHARED_ENABLED_BY_DEFAULT ON)
   305     set(SDL_STATIC_ENABLED_BY_DEFAULT OFF)
   306   endif()
   307 endif()
   308 
   309 set(SDL_SUBSYSTEMS
   310     Atomic Audio Video Render Events Joystick Haptic Power Threads Timers
   311     File Loadso CPUinfo Filesystem Dlopen Sensor)
   312 foreach(_SUB ${SDL_SUBSYSTEMS})
   313   string(TOUPPER ${_SUB} _OPT)
   314   if (NOT DEFINED SDL_${_OPT}_ENABLED_BY_DEFAULT)
   315     set(SDL_${_OPT}_ENABLED_BY_DEFAULT ON)
   316   endif()
   317   option(SDL_${_OPT} "Enable the ${_SUB} subsystem" ${SDL_${_OPT}_ENABLED_BY_DEFAULT})
   318 endforeach()
   319 
   320 option_string(ASSERTIONS "Enable internal sanity checks (auto/disabled/release/enabled/paranoid)" "auto")
   321 #set_option(DEPENDENCY_TRACKING "Use gcc -MMD -MT dependency tracking" ON)
   322 set_option(LIBC                "Use the system C library" ${OPT_DEF_LIBC})
   323 set_option(GCC_ATOMICS         "Use gcc builtin atomics" ${OPT_DEF_GCC_ATOMICS})
   324 set_option(ASSEMBLY            "Enable assembly routines" ${OPT_DEF_ASM})
   325 set_option(SSEMATH             "Allow GCC to use SSE floating point math" ${OPT_DEF_SSEMATH})
   326 set_option(MMX                 "Use MMX assembly routines" ${OPT_DEF_ASM})
   327 set_option(3DNOW               "Use 3Dnow! MMX assembly routines" ${OPT_DEF_ASM})
   328 set_option(SSE                 "Use SSE assembly routines" ${OPT_DEF_ASM})
   329 set_option(SSE2                "Use SSE2 assembly routines" ${OPT_DEF_SSEMATH})
   330 set_option(SSE3                "Use SSE3 assembly routines" ${OPT_DEF_SSEMATH})
   331 set_option(ALTIVEC             "Use Altivec assembly routines" ${OPT_DEF_ASM})
   332 set_option(DISKAUDIO           "Support the disk writer audio driver" ON)
   333 set_option(DUMMYAUDIO          "Support the dummy audio driver" ON)
   334 set_option(VIDEO_DIRECTFB      "Use DirectFB video driver" OFF)
   335 dep_option(DIRECTFB_SHARED     "Dynamically load directfb support" ON "VIDEO_DIRECTFB" OFF)
   336 set_option(VIDEO_DUMMY         "Use dummy video driver" ON)
   337 set_option(VIDEO_OPENGL        "Include OpenGL support" ON)
   338 set_option(VIDEO_OPENGLES      "Include OpenGL ES support" ON)
   339 set_option(PTHREADS            "Use POSIX threads for multi-threading" ${SDL_PTHREADS_ENABLED_BY_DEFAULT})
   340 dep_option(PTHREADS_SEM        "Use pthread semaphores" ON "PTHREADS" OFF)
   341 set_option(SDL_DLOPEN          "Use dlopen for shared object loading" ${SDL_DLOPEN_ENABLED_BY_DEFAULT})
   342 set_option(OSS                 "Support the OSS audio API" ${UNIX_SYS})
   343 set_option(ALSA                "Support the ALSA audio API" ${UNIX_SYS})
   344 dep_option(ALSA_SHARED         "Dynamically load ALSA audio support" ON "ALSA" OFF)
   345 set_option(JACK                "Support the JACK audio API" ${UNIX_SYS})
   346 dep_option(JACK_SHARED         "Dynamically load JACK audio support" ON "JACK" OFF)
   347 set_option(ESD                 "Support the Enlightened Sound Daemon" ${UNIX_SYS})
   348 dep_option(ESD_SHARED          "Dynamically load ESD audio support" ON "ESD" OFF)
   349 set_option(PULSEAUDIO          "Use PulseAudio" ${UNIX_SYS})
   350 dep_option(PULSEAUDIO_SHARED   "Dynamically load PulseAudio support" ON "PULSEAUDIO" OFF)
   351 set_option(ARTS                "Support the Analog Real Time Synthesizer" ${UNIX_SYS})
   352 dep_option(ARTS_SHARED         "Dynamically load aRts audio support" ON "ARTS" OFF)
   353 set_option(NAS                 "Support the NAS audio API" ${UNIX_SYS})
   354 set_option(NAS_SHARED          "Dynamically load NAS audio API" ${UNIX_SYS})
   355 set_option(SNDIO               "Support the sndio audio API" ${UNIX_SYS})
   356 set_option(FUSIONSOUND         "Use FusionSound audio driver" OFF)
   357 dep_option(FUSIONSOUND_SHARED  "Dynamically load fusionsound audio support" ON "FUSIONSOUND" OFF)
   358 set_option(LIBSAMPLERATE       "Use libsamplerate for audio rate conversion" ${UNIX_SYS})
   359 dep_option(LIBSAMPLERATE_SHARED "Dynamically load libsamplerate" ON "LIBSAMPLERATE" OFF)
   360 set_option(RPATH               "Use an rpath when linking SDL" ${UNIX_SYS})
   361 set_option(CLOCK_GETTIME       "Use clock_gettime() instead of gettimeofday()" OFF)
   362 set_option(INPUT_TSLIB         "Use the Touchscreen library for input" ${UNIX_SYS})
   363 set_option(VIDEO_X11           "Use X11 video driver" ${UNIX_SYS})
   364 set_option(VIDEO_WAYLAND       "Use Wayland video driver" ${UNIX_SYS})
   365 dep_option(WAYLAND_SHARED      "Dynamically load Wayland support" ON "VIDEO_WAYLAND" OFF)
   366 dep_option(VIDEO_WAYLAND_QT_TOUCH  "QtWayland server support for Wayland video driver" ON "VIDEO_WAYLAND" OFF)
   367 set_option(VIDEO_RPI           "Use Raspberry Pi video driver" ${UNIX_SYS})
   368 dep_option(X11_SHARED          "Dynamically load X11 support" ON "VIDEO_X11" OFF)
   369 set(SDL_X11_OPTIONS Xcursor Xinerama XInput Xrandr Xscrnsaver XShape Xvm)
   370 foreach(_SUB ${SDL_X11_OPTIONS})
   371   string(TOUPPER "VIDEO_X11_${_SUB}" _OPT)
   372   dep_option(${_OPT}           "Enable ${_SUB} support" ON "VIDEO_X11" OFF)
   373 endforeach()
   374 set_option(VIDEO_COCOA         "Use Cocoa video driver" ${APPLE})
   375 set_option(DIRECTX             "Use DirectX for Windows audio/video" ${WINDOWS})
   376 set_option(WASAPI              "Use the Windows WASAPI audio driver" ${WINDOWS})
   377 set_option(RENDER_D3D          "Enable the Direct3D render driver" ${WINDOWS})
   378 set_option(RENDER_METAL        "Enable the Metal render driver" ${APPLE})
   379 set_option(VIDEO_VIVANTE       "Use Vivante EGL video driver" ${UNIX_SYS})
   380 dep_option(VIDEO_VULKAN        "Enable Vulkan support" ON "ANDROID OR APPLE OR LINUX OR WINDOWS" OFF)
   381 set_option(VIDEO_METAL         "Enable Metal support" ${APPLE})
   382 set_option(VIDEO_KMSDRM        "Use KMS DRM video driver" ${UNIX_SYS})
   383 dep_option(KMSDRM_SHARED       "Dynamically load KMS DRM support" ON "VIDEO_KMSDRM" OFF)
   384 set_option(VIDEO_OFFSCREEN     "Use offscreen video driver" OFF)
   385 option_string(BACKGROUNDING_SIGNAL "number to use for magic backgrounding signal or 'OFF'" "OFF")
   386 option_string(FOREGROUNDING_SIGNAL "number to use for magic foregrounding signal or 'OFF'" "OFF")
   387 set_option(HIDAPI              "Use HIDAPI for low level joystick drivers" ${OPT_DEF_HIDAPI})
   388 
   389 set(SDL_SHARED ${SDL_SHARED_ENABLED_BY_DEFAULT} CACHE BOOL "Build a shared version of the library")
   390 set(SDL_STATIC ${SDL_STATIC_ENABLED_BY_DEFAULT} CACHE BOOL "Build a static version of the library")
   391 
   392 dep_option(SDL_STATIC_PIC      "Static version of the library should be built with Position Independent Code" OFF "SDL_STATIC" OFF)
   393 set_option(SDL_TEST            "Build the test directory" OFF)
   394 
   395 # General source files
   396 file(GLOB SOURCE_FILES
   397   ${SDL2_SOURCE_DIR}/src/*.c
   398   ${SDL2_SOURCE_DIR}/src/atomic/*.c
   399   ${SDL2_SOURCE_DIR}/src/audio/*.c
   400   ${SDL2_SOURCE_DIR}/src/cpuinfo/*.c
   401   ${SDL2_SOURCE_DIR}/src/dynapi/*.c
   402   ${SDL2_SOURCE_DIR}/src/events/*.c
   403   ${SDL2_SOURCE_DIR}/src/file/*.c
   404   ${SDL2_SOURCE_DIR}/src/libm/*.c
   405   ${SDL2_SOURCE_DIR}/src/render/*.c
   406   ${SDL2_SOURCE_DIR}/src/render/*/*.c
   407   ${SDL2_SOURCE_DIR}/src/stdlib/*.c
   408   ${SDL2_SOURCE_DIR}/src/thread/*.c
   409   ${SDL2_SOURCE_DIR}/src/timer/*.c
   410   ${SDL2_SOURCE_DIR}/src/video/*.c
   411   ${SDL2_SOURCE_DIR}/src/video/yuv2rgb/*.c)
   412 
   413 
   414 if(ASSERTIONS STREQUAL "auto")
   415   # Do nada - use optimization settings to determine the assertion level
   416 elseif(ASSERTIONS STREQUAL "disabled")
   417   set(SDL_DEFAULT_ASSERT_LEVEL 0)
   418 elseif(ASSERTIONS STREQUAL "release")
   419   set(SDL_DEFAULT_ASSERT_LEVEL 1)
   420 elseif(ASSERTIONS STREQUAL "enabled")
   421   set(SDL_DEFAULT_ASSERT_LEVEL 2)
   422 elseif(ASSERTIONS STREQUAL "paranoid")
   423   set(SDL_DEFAULT_ASSERT_LEVEL 3)
   424 else()
   425   message_error("unknown assertion level")
   426 endif()
   427 set(HAVE_ASSERTIONS ${ASSERTIONS})
   428 
   429 if(NOT BACKGROUNDING_SIGNAL STREQUAL "OFF")
   430   add_definitions("-DSDL_BACKGROUNDING_SIGNAL=${BACKGROUNDING_SIGNAL}")
   431 endif()
   432 
   433 if(NOT FOREGROUNDING_SIGNAL STREQUAL "OFF")
   434   add_definitions("-DSDL_FOREGROUNDING_SIGNAL=${FOREGROUNDING_SIGNAL}")
   435 endif()
   436 
   437 # Compiler option evaluation
   438 if(USE_GCC OR USE_CLANG)
   439   # Check for -Wall first, so later things can override pieces of it.
   440   check_c_compiler_flag(-Wall HAVE_GCC_WALL)
   441   if(HAVE_GCC_WALL)
   442     list(APPEND EXTRA_CFLAGS "-Wall")
   443     if(HAIKU)
   444       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-multichar")
   445     endif()
   446   endif()
   447 
   448   check_c_compiler_flag(-fno-strict-aliasing HAVE_GCC_NO_STRICT_ALIASING)
   449   if(HAVE_GCC_NO_STRICT_ALIASING)
   450     list(APPEND EXTRA_CFLAGS "-fno-strict-aliasing")
   451   endif()
   452 
   453   check_c_compiler_flag(-Wdeclaration-after-statement HAVE_GCC_WDECLARATION_AFTER_STATEMENT)
   454   if(HAVE_GCC_WDECLARATION_AFTER_STATEMENT)
   455     check_c_compiler_flag(-Werror=declaration-after-statement HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
   456     if(HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
   457       list(APPEND EXTRA_CFLAGS "-Werror=declaration-after-statement")
   458     endif()
   459     list(APPEND EXTRA_CFLAGS "-Wdeclaration-after-statement")
   460   endif()
   461 
   462   if(DEPENDENCY_TRACKING)
   463     check_c_source_compiles("
   464         #if !defined(__GNUC__) || __GNUC__ < 3
   465         #error Dependency tracking requires GCC 3.0 or newer
   466         #endif
   467         int main(int argc, char **argv) { }" HAVE_DEPENDENCY_TRACKING)
   468   endif()
   469 
   470   if(GCC_ATOMICS)
   471     check_c_source_compiles("int main(int argc, char **argv) {
   472         int a;
   473         void *x, *y, *z;
   474         __sync_lock_test_and_set(&a, 4);
   475         __sync_lock_test_and_set(&x, y);
   476         __sync_fetch_and_add(&a, 1);
   477         __sync_bool_compare_and_swap(&a, 5, 10);
   478         __sync_bool_compare_and_swap(&x, y, z); }" HAVE_GCC_ATOMICS)
   479     if(NOT HAVE_GCC_ATOMICS)
   480       check_c_source_compiles("int main(int argc, char **argv) {
   481           int a;
   482           __sync_lock_test_and_set(&a, 1);
   483           __sync_lock_release(&a); }" HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
   484     endif()
   485   endif()
   486 
   487   set(CMAKE_REQUIRED_FLAGS "-mpreferred-stack-boundary=2")
   488   check_c_source_compiles("int x = 0; int main(int argc, char **argv) {}"
   489     HAVE_GCC_PREFERRED_STACK_BOUNDARY)
   490   set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   491 
   492   set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden -Werror")
   493   check_c_source_compiles("
   494       #if !defined(__GNUC__) || __GNUC__ < 4
   495       #error SDL only uses visibility attributes in GCC 4 or newer
   496       #endif
   497       int main(int argc, char **argv) {}" HAVE_GCC_FVISIBILITY)
   498   if(HAVE_GCC_FVISIBILITY)
   499     list(APPEND EXTRA_CFLAGS "-fvisibility=hidden")
   500   endif()
   501   set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   502 
   503   check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
   504   if(HAVE_GCC_WSHADOW)
   505     list(APPEND EXTRA_CFLAGS "-Wshadow")
   506   endif()
   507 
   508   if(APPLE)
   509     list(APPEND EXTRA_LDFLAGS "-Wl,-undefined,error")
   510     list(APPEND EXTRA_LDFLAGS "-Wl,-compatibility_version,${DYLIB_COMPATIBILITY_VERSION}")
   511     list(APPEND EXTRA_LDFLAGS "-Wl,-current_version,${DYLIB_CURRENT_VERSION}")
   512   else()
   513     set(CMAKE_REQUIRED_FLAGS "-Wl,--no-undefined")
   514     check_c_compiler_flag("" HAVE_NO_UNDEFINED)
   515     set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   516     if(HAVE_NO_UNDEFINED)
   517       list(APPEND EXTRA_LDFLAGS "-Wl,--no-undefined")
   518     endif()
   519   endif()
   520 endif()
   521 
   522 if(ASSEMBLY)
   523   if(USE_GCC OR USE_CLANG)
   524     set(SDL_ASSEMBLY_ROUTINES 1)
   525     # TODO: Those all seem to be quite GCC specific - needs to be
   526     # reworked for better compiler support
   527     set(HAVE_ASSEMBLY TRUE)
   528     if(MMX)
   529       set(CMAKE_REQUIRED_FLAGS "-mmmx")
   530       check_c_source_compiles("
   531           #ifdef __MINGW32__
   532           #include <_mingw.h>
   533           #ifdef __MINGW64_VERSION_MAJOR
   534           #include <intrin.h>
   535           #else
   536           #include <mmintrin.h>
   537           #endif
   538           #else
   539           #include <mmintrin.h>
   540           #endif
   541           #ifndef __MMX__
   542           #error Assembler CPP flag not enabled
   543           #endif
   544           int main(int argc, char **argv) { }" HAVE_MMX)
   545       if(HAVE_MMX)
   546         list(APPEND EXTRA_CFLAGS "-mmmx")
   547       endif()
   548       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   549     endif()
   550 
   551     if(3DNOW)
   552       set(CMAKE_REQUIRED_FLAGS "-m3dnow")
   553       check_c_source_compiles("
   554           #include <mm3dnow.h>
   555           #ifndef __3dNOW__
   556           #error Assembler CPP flag not enabled
   557           #endif
   558           int main(int argc, char **argv) {
   559             void *p = 0;
   560             _m_prefetch(p);
   561           }" HAVE_3DNOW)
   562       if(HAVE_3DNOW)
   563         list(APPEND EXTRA_CFLAGS "-m3dnow")
   564       endif()
   565       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   566     endif()
   567 
   568     if(SSE)
   569       set(CMAKE_REQUIRED_FLAGS "-msse")
   570       check_c_source_compiles("
   571           #ifdef __MINGW32__
   572           #include <_mingw.h>
   573           #ifdef __MINGW64_VERSION_MAJOR
   574           #include <intrin.h>
   575           #else
   576           #include <xmmintrin.h>
   577           #endif
   578           #else
   579           #include <xmmintrin.h>
   580           #endif
   581           #ifndef __SSE__
   582           #error Assembler CPP flag not enabled
   583           #endif
   584           int main(int argc, char **argv) { }" HAVE_SSE)
   585       if(HAVE_SSE)
   586         list(APPEND EXTRA_CFLAGS "-msse")
   587       endif()
   588       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   589     endif()
   590 
   591     if(SSE2)
   592       set(CMAKE_REQUIRED_FLAGS "-msse2")
   593       check_c_source_compiles("
   594           #ifdef __MINGW32__
   595           #include <_mingw.h>
   596           #ifdef __MINGW64_VERSION_MAJOR
   597           #include <intrin.h>
   598           #else
   599           #include <emmintrin.h>
   600           #endif
   601           #else
   602           #include <emmintrin.h>
   603           #endif
   604           #ifndef __SSE2__
   605           #error Assembler CPP flag not enabled
   606           #endif
   607           int main(int argc, char **argv) { }" HAVE_SSE2)
   608       if(HAVE_SSE2)
   609         list(APPEND EXTRA_CFLAGS "-msse2")
   610       endif()
   611       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   612     endif()
   613 
   614     if(SSE3)
   615       set(CMAKE_REQUIRED_FLAGS "-msse3")
   616       check_c_source_compiles("
   617           #ifdef __MINGW32__
   618           #include <_mingw.h>
   619           #ifdef __MINGW64_VERSION_MAJOR
   620           #include <intrin.h>
   621           #else
   622           #include <pmmintrin.h>
   623           #endif
   624           #else
   625           #include <pmmintrin.h>
   626           #endif
   627           #ifndef __SSE3__
   628           #error Assembler CPP flag not enabled
   629           #endif
   630           int main(int argc, char **argv) { }" HAVE_SSE3)
   631       if(HAVE_SSE3)
   632         list(APPEND EXTRA_CFLAGS "-msse3")
   633       endif()
   634       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   635     endif()
   636 
   637     if(NOT SSEMATH)
   638       if(SSE OR SSE2 OR SSE3)
   639         if(USE_GCC)
   640           check_c_compiler_flag(-mfpmath=387 HAVE_FP_387)
   641           if(HAVE_FP_387)
   642             list(APPEND EXTRA_CFLAGS "-mfpmath=387")
   643           endif()
   644         endif()
   645         set(HAVE_SSEMATH TRUE)
   646       endif()
   647     endif()
   648 
   649     check_include_file("immintrin.h" HAVE_IMMINTRIN_H)
   650 
   651     if(ALTIVEC)
   652       set(CMAKE_REQUIRED_FLAGS "-maltivec")
   653       check_c_source_compiles("
   654           #include <altivec.h>
   655           vector unsigned int vzero() {
   656               return vec_splat_u32(0);
   657           }
   658           int main(int argc, char **argv) { }" HAVE_ALTIVEC_H_HDR)
   659       check_c_source_compiles("
   660           vector unsigned int vzero() {
   661               return vec_splat_u32(0);
   662           }
   663           int main(int argc, char **argv) { }" HAVE_ALTIVEC)
   664       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   665       if(HAVE_ALTIVEC OR HAVE_ALTIVEC_H_HDR)
   666         set(HAVE_ALTIVEC TRUE) # if only HAVE_ALTIVEC_H_HDR is set
   667         list(APPEND EXTRA_CFLAGS "-maltivec")
   668         set(SDL_ALTIVEC_BLITTERS 1)
   669         if(HAVE_ALTIVEC_H_HDR)
   670           set(HAVE_ALTIVEC_H 1)
   671         endif()
   672       endif()
   673     endif()
   674   elseif(MSVC_VERSION GREATER 1500)
   675     # TODO: SDL_cpuinfo.h needs to support the user's configuration wish
   676     # for MSVC - right now it is always activated
   677     if(NOT ARCH_64)
   678       set(HAVE_MMX TRUE)
   679       set(HAVE_3DNOW TRUE)
   680     endif()
   681     set(HAVE_SSE TRUE)
   682     set(HAVE_SSE2 TRUE)
   683     set(HAVE_SSE3 TRUE)
   684     set(SDL_ASSEMBLY_ROUTINES 1)
   685   endif()
   686 # TODO:
   687 #else()
   688 #  if(USE_GCC OR USE_CLANG)
   689 #    list(APPEND EXTRA_CFLAGS "-mno-sse" "-mno-sse2" "-mno-sse3" "-mno-mmx")
   690 #  endif()
   691 endif()
   692 
   693 # TODO: Can't deactivate on FreeBSD? w/o LIBC, SDL_stdinc.h can't define
   694 # anything.
   695 if(LIBC)
   696   if(WINDOWS AND NOT MINGW)
   697     set(HAVE_LIBC TRUE)
   698     foreach(_HEADER stdio.h string.h wchar.h ctype.h math.h limits.h)
   699       string(TOUPPER "HAVE_${_HEADER}" _UPPER)
   700       string(REPLACE "." "_" _HAVE_H ${_UPPER})
   701       set(${_HAVE_H} 1)
   702     endforeach()
   703     set(HAVE_SIGNAL_H 1)
   704     foreach(_FN
   705             malloc calloc realloc free qsort abs memset memcpy memmove memcmp
   706             wcslen wcscmp
   707             strlen _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa
   708             _ultoa strtol strtoul strtoll strtod atoi atof strcmp strncmp
   709             _stricmp _strnicmp sscanf
   710             acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf
   711             copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf
   712             log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf)
   713       string(TOUPPER ${_FN} _UPPER)
   714       set(HAVE_${_UPPER} 1)
   715     endforeach()
   716     if(NOT CYGWIN AND NOT MINGW)
   717       set(HAVE_ALLOCA 1)
   718     endif()
   719     set(HAVE_M_PI 1)
   720     add_definitions(-D_USE_MATH_DEFINES) # needed for M_PI
   721     set(STDC_HEADERS 1)
   722   else()
   723     set(HAVE_LIBC TRUE)
   724     check_include_file(sys/types.h HAVE_SYS_TYPES_H)
   725     foreach(_HEADER
   726             stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h limits.h
   727             strings.h wchar.h inttypes.h stdint.h ctype.h math.h iconv.h signal.h libunwind.h)
   728       string(TOUPPER "HAVE_${_HEADER}" _UPPER)
   729       string(REPLACE "." "_" _HAVE_H ${_UPPER})
   730       check_include_file("${_HEADER}" ${_HAVE_H})
   731     endforeach()
   732 
   733     check_include_files("dlfcn.h;stdint.h;stddef.h;inttypes.h;stdlib.h;strings.h;string.h;float.h" STDC_HEADERS)
   734     check_type_size("size_t" SIZEOF_SIZE_T)
   735     check_symbol_exists(M_PI math.h HAVE_M_PI)
   736     # TODO: refine the mprotect check
   737     check_c_source_compiles("#include <sys/types.h>
   738                              #include <sys/mman.h>
   739                              int main() { }" HAVE_MPROTECT)
   740     foreach(_FN
   741             strtod malloc calloc realloc free getenv setenv putenv unsetenv
   742             qsort abs bcopy memset memcpy memmove memcmp strlen strlcpy strlcat
   743             _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa
   744             _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull
   745             atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp
   746             vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp
   747             nanosleep sysconf sysctlbyname getauxval poll _Exit
   748             )
   749       string(TOUPPER ${_FN} _UPPER)
   750       set(_HAVEVAR "HAVE_${_UPPER}")
   751       check_function_exists("${_FN}" ${_HAVEVAR})
   752     endforeach()
   753 
   754     check_library_exists(m pow "" HAVE_LIBM)
   755     if(HAVE_LIBM)
   756       set(CMAKE_REQUIRED_LIBRARIES m)
   757       foreach(_FN
   758               atan atan2 ceil copysign cos cosf fabs floor log pow scalbn sin
   759               sinf sqrt sqrtf tan tanf acos asin)
   760         string(TOUPPER ${_FN} _UPPER)
   761         set(_HAVEVAR "HAVE_${_UPPER}")
   762         check_function_exists("${_FN}" ${_HAVEVAR})
   763       endforeach()
   764       set(CMAKE_REQUIRED_LIBRARIES)
   765       list(APPEND EXTRA_LIBS m)
   766     endif()
   767 
   768     check_library_exists(iconv iconv_open "" HAVE_LIBICONV)
   769     if(HAVE_LIBICONV)
   770       list(APPEND EXTRA_LIBS iconv)
   771       set(HAVE_ICONV 1)
   772     endif()
   773 
   774     if(NOT APPLE)
   775       check_include_file(alloca.h HAVE_ALLOCA_H)
   776       check_function_exists(alloca HAVE_ALLOCA)
   777     else()
   778       set(HAVE_ALLOCA_H 1)
   779       set(HAVE_ALLOCA 1)
   780     endif()
   781 
   782     check_struct_has_member("struct sigaction" "sa_sigaction" "signal.h" HAVE_SA_SIGACTION)
   783   endif()
   784 else()
   785   if(WINDOWS)
   786     set(HAVE_STDARG_H 1)
   787     set(HAVE_STDDEF_H 1)
   788   endif()
   789 endif()
   790 
   791 
   792 # Enable/disable various subsystems of the SDL library
   793 foreach(_SUB ${SDL_SUBSYSTEMS})
   794   string(TOUPPER ${_SUB} _OPT)
   795   if(NOT SDL_${_OPT})
   796     set(SDL_${_OPT}_DISABLED 1)
   797   endif()
   798 endforeach()
   799 if(SDL_JOYSTICK)
   800   file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/*.c)
   801   set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
   802 endif()
   803 if(SDL_HAPTIC)
   804   if(NOT SDL_JOYSTICK)
   805     # Haptic requires some private functions from the joystick subsystem.
   806     message_error("SDL_HAPTIC requires SDL_JOYSTICK, which is not enabled")
   807   endif()
   808   file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/*.c)
   809   set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
   810 endif()
   811 if(SDL_SENSOR)
   812   file(GLOB SENSOR_SOURCES ${SDL2_SOURCE_DIR}/src/sensor/*.c)
   813   set(SOURCE_FILES ${SOURCE_FILES} ${SENSOR_SOURCES})
   814 endif()
   815 if(SDL_POWER)
   816   file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/*.c)
   817   set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
   818 endif()
   819 # TODO: in configure.ac, the test for LOADSO and SDL_DLOPEN is a bit weird:
   820 # if LOADSO is not wanted, SDL_LOADSO_DISABLED is set
   821 # If however on Unix or APPLE dlopen() is detected via CheckDLOPEN(),
   822 # SDL_LOADSO_DISABLED will not be set, regardless of the LOADSO settings
   823 
   824 # General SDL subsystem options, valid for all platforms
   825 if(SDL_AUDIO)
   826   # CheckDummyAudio/CheckDiskAudio - valid for all platforms
   827   if(DUMMYAUDIO)
   828     set(SDL_AUDIO_DRIVER_DUMMY 1)
   829     file(GLOB DUMMYAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/dummy/*.c)
   830     set(SOURCE_FILES ${SOURCE_FILES} ${DUMMYAUDIO_SOURCES})
   831     set(HAVE_DUMMYAUDIO TRUE)
   832   endif()
   833   if(DISKAUDIO)
   834     set(SDL_AUDIO_DRIVER_DISK 1)
   835     file(GLOB DISKAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/disk/*.c)
   836     set(SOURCE_FILES ${SOURCE_FILES} ${DISKAUDIO_SOURCES})
   837     set(HAVE_DISKAUDIO TRUE)
   838   endif()
   839 endif()
   840 
   841 if(SDL_DLOPEN)
   842   # Relevant for Unix/Darwin only
   843   if(UNIX OR APPLE)
   844     CheckDLOPEN()
   845   endif()
   846 endif()
   847 
   848 if(SDL_VIDEO)
   849   if(VIDEO_DUMMY)
   850     set(SDL_VIDEO_DRIVER_DUMMY 1)
   851     file(GLOB VIDEO_DUMMY_SOURCES ${SDL2_SOURCE_DIR}/src/video/dummy/*.c)
   852     set(SOURCE_FILES ${SOURCE_FILES} ${VIDEO_DUMMY_SOURCES})
   853     set(HAVE_VIDEO_DUMMY TRUE)
   854     set(HAVE_SDL_VIDEO TRUE)
   855   endif()
   856   if(VIDEO_OFFSCREEN)
   857     set(SDL_VIDEO_DRIVER_OFFSCREEN 1)
   858     file(GLOB VIDEO_OFFSCREEN_SOURCES ${SDL2_SOURCE_DIR}/src/video/offscreen/*.c)
   859     set(SOURCE_FILES ${SOURCE_FILES} ${VIDEO_OFFSCREEN_SOURCES})
   860     set(HAVE_VIDEO_OFFSCREEN TRUE)
   861     set(HAVE_SDL_VIDEO TRUE)
   862   endif()
   863 endif()
   864 
   865 # Platform-specific options and settings
   866 if(ANDROID)
   867   file(GLOB ANDROID_CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/android/*.c)
   868   set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_CORE_SOURCES})
   869 
   870   # SDL_spinlock.c Needs to be compiled in ARM mode.
   871   # There seems to be no better way currently to set the ARM mode.
   872   # see: https://issuetracker.google.com/issues/62264618
   873   # Another option would be to set ARM mode to all compiled files
   874   check_c_compiler_flag(-marm HAVE_ARM_MODE)
   875   if(HAVE_ARM_MODE)
   876     set_source_files_properties(${SDL2_SOURCE_DIR}/src/atomic/SDL_spinlock.c PROPERTIES COMPILE_FLAGS -marm)
   877   endif()
   878 
   879   file(GLOB ANDROID_MAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/android/*.c)
   880   set(SDLMAIN_SOURCES ${SDLMAIN_SOURCES} ${ANDROID_MAIN_SOURCES})
   881 
   882   if(SDL_AUDIO)
   883     set(SDL_AUDIO_DRIVER_ANDROID 1)
   884     file(GLOB ANDROID_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/android/*.c)
   885     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_AUDIO_SOURCES})
   886     set(HAVE_SDL_AUDIO TRUE)
   887   endif()
   888   if(SDL_FILESYSTEM)
   889     set(SDL_FILESYSTEM_ANDROID 1)
   890     file(GLOB ANDROID_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/android/*.c)
   891     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_FILESYSTEM_SOURCES})
   892     set(HAVE_SDL_FILESYSTEM TRUE)
   893   endif()
   894   if(SDL_HAPTIC)
   895     set(SDL_HAPTIC_ANDROID 1)
   896     file(GLOB ANDROID_HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/android/*.c)
   897     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_HAPTIC_SOURCES})
   898     set(HAVE_SDL_HAPTIC TRUE)
   899   endif()
   900   if(SDL_JOYSTICK)
   901     CheckHIDAPI()
   902     set(SDL_JOYSTICK_ANDROID 1)
   903     file(GLOB ANDROID_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/android/*.c ${SDL2_SOURCE_DIR}/src/joystick/steam/*.c)
   904     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_JOYSTICK_SOURCES})
   905     set(HAVE_SDL_JOYSTICK TRUE)
   906   endif()
   907   if(SDL_LOADSO)
   908     set(SDL_LOADSO_DLOPEN 1)
   909     file(GLOB LOADSO_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/dlopen/*.c)
   910     set(SOURCE_FILES ${SOURCE_FILES} ${LOADSO_SOURCES})
   911     set(HAVE_SDL_LOADSO TRUE)
   912   endif()
   913   if(SDL_POWER)
   914     set(SDL_POWER_ANDROID 1)
   915     file(GLOB ANDROID_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/android/*.c)
   916     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_POWER_SOURCES})
   917     set(HAVE_SDL_POWER TRUE)
   918   endif()
   919   if(SDL_TIMERS)
   920     set(SDL_TIMER_UNIX 1)
   921     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
   922     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
   923     set(HAVE_SDL_TIMERS TRUE)
   924   endif()
   925   if(SDL_SENSOR)
   926     set(SDL_SENSOR_ANDROID 1)
   927     set(HAVE_SDL_SENSORS TRUE)
   928     file(GLOB ANDROID_SENSOR_SOURCES ${SDL2_SOURCE_DIR}/src/sensor/android/*.c)
   929     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_SENSOR_SOURCES})
   930   endif()
   931   if(SDL_VIDEO)
   932     set(SDL_VIDEO_DRIVER_ANDROID 1)
   933     file(GLOB ANDROID_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/android/*.c)
   934     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_VIDEO_SOURCES})
   935     set(HAVE_SDL_VIDEO TRUE)
   936 
   937     # Core stuff
   938     # find_library(ANDROID_DL_LIBRARY dl)
   939     # FIXME failing dlopen https://github.com/android-ndk/ndk/issues/929
   940     find_library(ANDROID_DL_LIBRARY NAMES libdl.so dl)
   941     find_library(ANDROID_LOG_LIBRARY log)
   942     find_library(ANDROID_LIBRARY_LIBRARY android)
   943     list(APPEND EXTRA_LIBS ${ANDROID_DL_LIBRARY} ${ANDROID_LOG_LIBRARY} ${ANDROID_LIBRARY_LIBRARY})
   944     add_definitions(-DGL_GLEXT_PROTOTYPES)
   945 
   946     if (HAVE_HIDAPI)
   947         list(APPEND EXTRA_LIBS hidapi)
   948     endif()
   949 
   950     #enable gles
   951     if(VIDEO_OPENGLES)
   952       set(SDL_VIDEO_OPENGL_EGL 1)
   953       set(HAVE_VIDEO_OPENGLES TRUE)
   954       set(SDL_VIDEO_OPENGL_ES 1)
   955       set(SDL_VIDEO_RENDER_OGL_ES 1)
   956       set(SDL_VIDEO_OPENGL_ES2 1)
   957       set(SDL_VIDEO_RENDER_OGL_ES2 1)
   958 
   959       find_library(OpenGLES1_LIBRARY GLESv1_CM)
   960       find_library(OpenGLES2_LIBRARY GLESv2)
   961       list(APPEND EXTRA_LIBS ${OpenGLES1_LIBRARY} ${OpenGLES2_LIBRARY})
   962     endif()
   963 
   964     CHECK_C_SOURCE_COMPILES("
   965     #if defined(__ARM_ARCH) && __ARM_ARCH < 7
   966     #error Vulkan doesn't work on this configuration
   967     #endif
   968     int main()
   969     {
   970         return 0;
   971     }
   972     " VULKAN_PASSED_ANDROID_CHECKS)
   973     if(NOT VULKAN_PASSED_ANDROID_CHECKS)
   974       set(VIDEO_VULKAN OFF)
   975       message(STATUS "Vulkan doesn't work on this configuration")
   976     endif()
   977   endif()
   978 
   979   CheckPTHREAD()
   980 
   981 elseif(EMSCRIPTEN)
   982   # Hide noisy warnings that intend to aid mostly during initial stages of porting a new
   983   # project. Uncomment at will for verbose cross-compiling -I/../ path info.
   984   add_definitions(-Wno-warn-absolute-paths)
   985   if(SDL_AUDIO)
   986     set(SDL_AUDIO_DRIVER_EMSCRIPTEN 1)
   987     file(GLOB EM_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/emscripten/*.c)
   988     set(SOURCE_FILES ${SOURCE_FILES} ${EM_AUDIO_SOURCES})
   989     set(HAVE_SDL_AUDIO TRUE)
   990   endif()
   991   if(SDL_FILESYSTEM)
   992     set(SDL_FILESYSTEM_EMSCRIPTEN 1)
   993     file(GLOB EM_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/emscripten/*.c)
   994     set(SOURCE_FILES ${SOURCE_FILES} ${EM_FILESYSTEM_SOURCES})
   995     set(HAVE_SDL_FILESYSTEM TRUE)
   996   endif()
   997   if(SDL_JOYSTICK)
   998     set(SDL_JOYSTICK_EMSCRIPTEN 1)
   999     file(GLOB EM_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/emscripten/*.c)
  1000     set(SOURCE_FILES ${SOURCE_FILES} ${EM_JOYSTICK_SOURCES})
  1001     set(HAVE_SDL_JOYSTICK TRUE)
  1002   endif()
  1003   if(SDL_POWER)
  1004     set(SDL_POWER_EMSCRIPTEN 1)
  1005     file(GLOB EM_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/emscripten/*.c)
  1006     set(SOURCE_FILES ${SOURCE_FILES} ${EM_POWER_SOURCES})
  1007     set(HAVE_SDL_POWER TRUE)
  1008   endif()
  1009   if(SDL_TIMERS)
  1010     set(SDL_TIMER_UNIX 1)
  1011     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
  1012     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1013     set(HAVE_SDL_TIMERS TRUE)
  1014 
  1015     if(CLOCK_GETTIME)
  1016       set(HAVE_CLOCK_GETTIME 1)
  1017     endif()
  1018   endif()
  1019   if(SDL_VIDEO)
  1020     set(SDL_VIDEO_DRIVER_EMSCRIPTEN 1)
  1021     file(GLOB EM_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/emscripten/*.c)
  1022     set(SOURCE_FILES ${SOURCE_FILES} ${EM_VIDEO_SOURCES})
  1023     set(HAVE_SDL_VIDEO TRUE)
  1024 
  1025     #enable gles
  1026     if(VIDEO_OPENGLES)
  1027       set(SDL_VIDEO_OPENGL_EGL 1)
  1028       set(HAVE_VIDEO_OPENGLES TRUE)
  1029       set(SDL_VIDEO_OPENGL_ES2 1)
  1030       set(SDL_VIDEO_RENDER_OGL_ES2 1)
  1031     endif()
  1032   endif()
  1033 
  1034 elseif(UNIX AND NOT APPLE AND NOT ANDROID)
  1035   if(SDL_AUDIO)
  1036     if(SYSV5 OR SOLARIS OR HPUX)
  1037         set(SDL_AUDIO_DRIVER_SUNAUDIO 1)
  1038         file(GLOB SUN_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/sun/*.c)
  1039         set(SOURCE_FILES ${SOURCE_FILES} ${SUN_AUDIO_SOURCES})
  1040         set(HAVE_SDL_AUDIO TRUE)
  1041     elseif(NETBSD)
  1042         set(SDL_AUDIO_DRIVER_NETBSD 1)
  1043         file(GLOB NETBSD_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/netbsd/*.c)
  1044         set(SOURCE_FILES ${SOURCE_FILES} ${NETBSD_AUDIO_SOURCES})
  1045         set(HAVE_SDL_AUDIO TRUE)
  1046     elseif(AIX)
  1047         set(SDL_AUDIO_DRIVER_PAUDIO 1)
  1048         file(GLOB AIX_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/paudio/*.c)
  1049         set(SOURCE_FILES ${SOURCE_FILES} ${AIX_AUDIO_SOURCES})
  1050         set(HAVE_SDL_AUDIO TRUE)
  1051     endif()
  1052     CheckOSS()
  1053     CheckALSA()
  1054     CheckJACK()
  1055     CheckPulseAudio()
  1056     CheckESD()
  1057     CheckARTS()
  1058     CheckNAS()
  1059     CheckSNDIO()
  1060     CheckFusionSound()
  1061     CheckLibSampleRate()
  1062   endif()
  1063 
  1064   if(SDL_VIDEO)
  1065     # Need to check for Raspberry PI first and add platform specific compiler flags, otherwise the test for GLES fails!
  1066     CheckRPI()
  1067     CheckX11()
  1068     CheckDirectFB()
  1069     CheckOpenGLX11()
  1070     CheckOpenGLESX11()
  1071     CheckWayland()
  1072     CheckVivante()
  1073     CheckKMSDRM()
  1074   endif()
  1075 
  1076   if(UNIX)
  1077     file(GLOB CORE_UNIX_SOURCES ${SDL2_SOURCE_DIR}/src/core/unix/*.c)
  1078     set(SOURCE_FILES ${SOURCE_FILES} ${CORE_UNIX_SOURCES})
  1079   endif()
  1080 
  1081   if(LINUX)
  1082     check_c_source_compiles("
  1083         #include <linux/input.h>
  1084         #ifndef EVIOCGNAME
  1085         #error EVIOCGNAME() ioctl not available
  1086         #endif
  1087         int main(int argc, char** argv) {}" HAVE_INPUT_EVENTS)
  1088 
  1089     check_c_source_compiles("
  1090         #include <linux/kd.h>
  1091         #include <linux/keyboard.h>
  1092 
  1093         int main(int argc, char **argv)
  1094         {
  1095             struct kbentry kbe;
  1096             kbe.kb_table = KG_CTRL;
  1097             ioctl(0, KDGKBENT, &kbe);
  1098         }" HAVE_INPUT_KD)
  1099 
  1100     file(GLOB CORE_LINUX_SOURCES ${SDL2_SOURCE_DIR}/src/core/linux/*.c)
  1101     set(SOURCE_FILES ${SOURCE_FILES} ${CORE_LINUX_SOURCES})
  1102 
  1103     if(HAVE_INPUT_EVENTS)
  1104       set(SDL_INPUT_LINUXEV 1)
  1105     endif()
  1106 
  1107     if(SDL_HAPTIC AND HAVE_INPUT_EVENTS)
  1108       set(SDL_HAPTIC_LINUX 1)
  1109       file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/linux/*.c)
  1110       set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
  1111       set(HAVE_SDL_HAPTIC TRUE)
  1112     endif()
  1113 
  1114     if(HAVE_INPUT_KD)
  1115       set(SDL_INPUT_LINUXKD 1)
  1116     endif()
  1117 
  1118     check_include_file("libudev.h" HAVE_LIBUDEV_H)
  1119 
  1120     if(PKG_CONFIG_FOUND)
  1121       pkg_search_module(DBUS dbus-1 dbus)
  1122       if(DBUS_FOUND)
  1123         set(HAVE_DBUS_DBUS_H TRUE)
  1124         include_directories(${DBUS_INCLUDE_DIRS})
  1125         list(APPEND EXTRA_LIBS ${DBUS_LIBRARIES})
  1126       endif()
  1127 
  1128       pkg_search_module(IBUS ibus-1.0 ibus)
  1129       if(IBUS_FOUND)
  1130         set(HAVE_IBUS_IBUS_H TRUE)
  1131         include_directories(${IBUS_INCLUDE_DIRS})
  1132         list(APPEND EXTRA_LIBS ${IBUS_LIBRARIES})
  1133         add_definitions(-DSDL_USE_IME)
  1134       endif()
  1135       if(HAVE_LIBUNWIND_H)
  1136         # We've already found the header, so REQUIRE the lib to be present
  1137         pkg_search_module(UNWIND REQUIRED libunwind)
  1138         pkg_search_module(UNWIND_GENERIC REQUIRED libunwind-generic)
  1139         list(APPEND EXTRA_LIBS ${UNWIND_LIBRARIES} ${UNWIND_GENERIC_LIBRARIES})
  1140       endif()
  1141     endif()
  1142 
  1143     check_include_file("fcitx/frontend.h" HAVE_FCITX_FRONTEND_H)
  1144   endif()
  1145 
  1146   if(INPUT_TSLIB)
  1147     check_c_source_compiles("
  1148         #include \"tslib.h\"
  1149         int main(int argc, char** argv) { }" HAVE_INPUT_TSLIB)
  1150     if(HAVE_INPUT_TSLIB)
  1151       set(SDL_INPUT_TSLIB 1)
  1152       list(APPEND EXTRA_LIBS ts)
  1153     endif()
  1154   endif()
  1155 
  1156   if(SDL_JOYSTICK)
  1157     if(FREEBSD OR NETBSD OR OPENBSD OR BSDI)
  1158       CheckUSBHID()
  1159     endif()
  1160     CheckHIDAPI()
  1161     if(LINUX AND NOT ANDROID)
  1162       set(SDL_JOYSTICK_LINUX 1)
  1163       file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/linux/*.c ${SDL2_SOURCE_DIR}/src/joystick/steam/*.c)
  1164       set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
  1165       set(HAVE_SDL_JOYSTICK TRUE)
  1166     endif()
  1167   endif()
  1168 
  1169   CheckPTHREAD()
  1170 
  1171   if(CLOCK_GETTIME)
  1172     check_library_exists(rt clock_gettime "" FOUND_CLOCK_GETTIME)
  1173     if(FOUND_CLOCK_GETTIME)
  1174       list(APPEND EXTRA_LIBS rt)
  1175       set(HAVE_CLOCK_GETTIME 1)
  1176     else()
  1177       check_library_exists(c clock_gettime "" FOUND_CLOCK_GETTIME)
  1178       if(FOUND_CLOCK_GETTIME)
  1179         set(HAVE_CLOCK_GETTIME 1)
  1180       endif()
  1181     endif()
  1182   endif()
  1183 
  1184   check_include_file(linux/version.h HAVE_LINUX_VERSION_H)
  1185   if(HAVE_LINUX_VERSION_H)
  1186     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LINUX_VERSION_H")
  1187   endif()
  1188 
  1189   if(SDL_POWER)
  1190     if(LINUX)
  1191       set(SDL_POWER_LINUX 1)
  1192       file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/linux/*.c)
  1193       set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
  1194       set(HAVE_SDL_POWER TRUE)
  1195     endif()
  1196   endif()
  1197 
  1198   if(SDL_FILESYSTEM)
  1199     set(SDL_FILESYSTEM_UNIX 1)
  1200     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/unix/*.c)
  1201     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1202     set(HAVE_SDL_FILESYSTEM TRUE)
  1203   endif()
  1204 
  1205   if(SDL_TIMERS)
  1206     set(SDL_TIMER_UNIX 1)
  1207     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
  1208     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1209     set(HAVE_SDL_TIMERS TRUE)
  1210   endif()
  1211 
  1212   if(RPATH)
  1213     set(SDL_RLD_FLAGS "")
  1214     if(BSDI OR FREEBSD OR LINUX OR NETBSD)
  1215       set(CMAKE_REQUIRED_FLAGS "-Wl,--enable-new-dtags")
  1216       check_c_compiler_flag("" HAVE_ENABLE_NEW_DTAGS)
  1217       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
  1218       if(HAVE_ENABLE_NEW_DTAGS)
  1219         set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir} -Wl,--enable-new-dtags")
  1220       else()
  1221         set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir}")
  1222       endif()
  1223     elseif(SOLARIS)
  1224       set(SDL_RLD_FLAGS "-R\${libdir}")
  1225     endif()
  1226     set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
  1227     set(HAVE_RPATH TRUE)
  1228   endif()
  1229 
  1230 elseif(WINDOWS)
  1231   find_program(WINDRES windres)
  1232 
  1233   check_c_source_compiles("
  1234     #include <windows.h>
  1235     int main(int argc, char **argv) { }" HAVE_WIN32_CC)
  1236 
  1237   file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/windows/*.c)
  1238   set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
  1239 
  1240   if(MSVC)
  1241     # Prevent codegen that would use the VC runtime libraries.
  1242     set_property(DIRECTORY . APPEND PROPERTY COMPILE_OPTIONS "/GS-")
  1243     if(NOT ARCH_64)
  1244       set_property(DIRECTORY . APPEND PROPERTY COMPILE_OPTIONS "/arch:SSE")
  1245     endif()
  1246   endif()
  1247 
  1248   # Check for DirectX
  1249   if(DIRECTX)
  1250     if(DEFINED MSVC_VERSION AND NOT ${MSVC_VERSION} LESS 1700)
  1251         set(USE_WINSDK_DIRECTX TRUE)
  1252     endif()
  1253     if(NOT CMAKE_COMPILER_IS_MINGW AND NOT USE_WINSDK_DIRECTX)
  1254       if("$ENV{DXSDK_DIR}" STREQUAL "")
  1255         message_error("DIRECTX requires the \$DXSDK_DIR environment variable to be set")
  1256       endif()
  1257       set(CMAKE_REQUIRED_FLAGS "/I\"$ENV{DXSDK_DIR}\\Include\"")
  1258     endif()
  1259 
  1260     if(HAVE_WIN32_CC)
  1261       # xinput.h may need windows.h, but doesn't include it itself.
  1262       check_c_source_compiles("
  1263         #include <windows.h>
  1264         #include <xinput.h>
  1265         int main(int argc, char **argv) { }" HAVE_XINPUT_H)
  1266       check_c_source_compiles("
  1267         #include <windows.h>
  1268         #include <xinput.h>
  1269         XINPUT_GAMEPAD_EX x1;
  1270         int main(int argc, char **argv) { }" HAVE_XINPUT_GAMEPAD_EX)
  1271       check_c_source_compiles("
  1272         #include <windows.h>
  1273         #include <xinput.h>
  1274         XINPUT_STATE_EX s1;
  1275         int main(int argc, char **argv) { }" HAVE_XINPUT_STATE_EX)
  1276     else()
  1277       check_include_file(xinput.h HAVE_XINPUT_H)
  1278     endif()
  1279 
  1280     check_include_file(d3d9.h HAVE_D3D_H)
  1281     check_include_file(d3d11_1.h HAVE_D3D11_H)
  1282     check_include_file(ddraw.h HAVE_DDRAW_H)
  1283     check_include_file(dsound.h HAVE_DSOUND_H)
  1284     check_include_file(dinput.h HAVE_DINPUT_H)
  1285     check_include_file(dxgi.h HAVE_DXGI_H)
  1286     if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H)
  1287       set(HAVE_DIRECTX TRUE)
  1288       if(NOT CMAKE_COMPILER_IS_MINGW AND NOT USE_WINSDK_DIRECTX)
  1289       # TODO: change $ENV{DXSDL_DIR} to get the path from the include checks
  1290         link_directories($ENV{DXSDK_DIR}\\lib\\${PROCESSOR_ARCH})
  1291         include_directories($ENV{DXSDK_DIR}\\Include)
  1292       endif()
  1293     endif()
  1294     set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
  1295   endif()
  1296 
  1297   # headers needed elsewhere ...
  1298   check_include_file(mmdeviceapi.h HAVE_MMDEVICEAPI_H)
  1299   check_include_file(audioclient.h HAVE_AUDIOCLIENT_H)
  1300   check_include_file(endpointvolume.h HAVE_ENDPOINTVOLUME_H)
  1301 
  1302   if(SDL_AUDIO)
  1303     set(SDL_AUDIO_DRIVER_WINMM 1)
  1304     file(GLOB WINMM_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/winmm/*.c)
  1305     set(SOURCE_FILES ${SOURCE_FILES} ${WINMM_AUDIO_SOURCES})
  1306     set(HAVE_SDL_AUDIO TRUE)
  1307 
  1308     if(HAVE_DSOUND_H)
  1309       set(SDL_AUDIO_DRIVER_DSOUND 1)
  1310       file(GLOB DSOUND_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/directsound/*.c)
  1311       set(SOURCE_FILES ${SOURCE_FILES} ${DSOUND_AUDIO_SOURCES})
  1312     endif()
  1313 
  1314     if(WASAPI AND HAVE_AUDIOCLIENT_H AND HAVE_MMDEVICEAPI_H)
  1315       set(SDL_AUDIO_DRIVER_WASAPI 1)
  1316       file(GLOB WASAPI_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/wasapi/*.c)
  1317       set(SOURCE_FILES ${SOURCE_FILES} ${WASAPI_AUDIO_SOURCES})
  1318     endif()
  1319   endif()
  1320 
  1321   if(SDL_VIDEO)
  1322     # requires SDL_LOADSO on Windows (IME, DX, etc.)
  1323     if(NOT SDL_LOADSO)
  1324       message_error("SDL_VIDEO requires SDL_LOADSO, which is not enabled")
  1325     endif()
  1326     set(SDL_VIDEO_DRIVER_WINDOWS 1)
  1327     file(GLOB WIN_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/windows/*.c)
  1328     set(SOURCE_FILES ${SOURCE_FILES} ${WIN_VIDEO_SOURCES})
  1329 
  1330     if(RENDER_D3D AND HAVE_D3D_H)
  1331       set(SDL_VIDEO_RENDER_D3D 1)
  1332       set(HAVE_RENDER_D3D TRUE)
  1333     endif()
  1334     if(RENDER_D3D AND HAVE_D3D11_H)
  1335       set(SDL_VIDEO_RENDER_D3D11 1)
  1336       set(HAVE_RENDER_D3D TRUE)
  1337     endif()
  1338     set(HAVE_SDL_VIDEO TRUE)
  1339   endif()
  1340 
  1341   if(SDL_THREADS)
  1342     set(SDL_THREAD_WINDOWS 1)
  1343     set(SOURCE_FILES ${SOURCE_FILES}
  1344       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_sysmutex.c
  1345       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_syssem.c
  1346       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_systhread.c
  1347       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_systls.c
  1348       ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_syscond.c)
  1349     set(HAVE_SDL_THREADS TRUE)
  1350   endif()
  1351 
  1352   if(SDL_POWER)
  1353     set(SDL_POWER_WINDOWS 1)
  1354     set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/power/windows/SDL_syspower.c)
  1355     set(HAVE_SDL_POWER TRUE)
  1356   endif()
  1357 
  1358   if(SDL_FILESYSTEM)
  1359     set(SDL_FILESYSTEM_WINDOWS 1)
  1360     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/windows/*.c)
  1361     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1362     set(HAVE_SDL_FILESYSTEM TRUE)
  1363   endif()
  1364 
  1365   # Libraries for Win32 native and MinGW
  1366   list(APPEND EXTRA_LIBS user32 gdi32 winmm imm32 ole32 oleaut32 version uuid advapi32 setupapi shell32)
  1367 
  1368   # TODO: in configure.ac the check for timers is set on
  1369   # cygwin | mingw32* - does this include mingw32CE?
  1370   if(SDL_TIMERS)
  1371     set(SDL_TIMER_WINDOWS 1)
  1372     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/windows/*.c)
  1373     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1374     set(HAVE_SDL_TIMERS TRUE)
  1375   endif()
  1376 
  1377   if(SDL_LOADSO)
  1378     set(SDL_LOADSO_WINDOWS 1)
  1379     file(GLOB LOADSO_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/windows/*.c)
  1380     set(SOURCE_FILES ${SOURCE_FILES} ${LOADSO_SOURCES})
  1381     set(HAVE_SDL_LOADSO TRUE)
  1382   endif()
  1383 
  1384   file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/windows/*.c)
  1385   set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
  1386 
  1387   if(SDL_VIDEO)
  1388     if(VIDEO_OPENGL)
  1389       set(SDL_VIDEO_OPENGL 1)
  1390       set(SDL_VIDEO_OPENGL_WGL 1)
  1391       set(SDL_VIDEO_RENDER_OGL 1)
  1392       set(HAVE_VIDEO_OPENGL TRUE)
  1393     endif()
  1394 
  1395     if(VIDEO_OPENGLES)
  1396       set(SDL_VIDEO_OPENGL_EGL 1)
  1397       set(SDL_VIDEO_OPENGL_ES2 1)
  1398       set(SDL_VIDEO_RENDER_OGL_ES2 1)
  1399       set(HAVE_VIDEO_OPENGLES TRUE)
  1400     endif()
  1401   endif()
  1402 
  1403   if(SDL_JOYSTICK)
  1404     CheckHIDAPI()
  1405     # TODO: Remove this hid.c block when SDL_hidapi.c is supported on Windows!
  1406     if(HAVE_HIDAPI)
  1407       set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/windows/hid.c)
  1408     endif()
  1409     file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/windows/*.c)
  1410     set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
  1411     if(HAVE_DINPUT_H)
  1412       set(SDL_JOYSTICK_DINPUT 1)
  1413       list(APPEND EXTRA_LIBS dinput8)
  1414       if(CMAKE_COMPILER_IS_MINGW)
  1415         list(APPEND EXTRA_LIBS dxerr8)
  1416       elseif (NOT USE_WINSDK_DIRECTX)
  1417         list(APPEND EXTRA_LIBS dxerr)
  1418       endif()
  1419     endif()
  1420     if(HAVE_XINPUT_H)
  1421       set(SDL_JOYSTICK_XINPUT 1)
  1422     endif()
  1423     if(NOT HAVE_DINPUT_H AND NOT HAVE_XINPUT_H)
  1424       set(SDL_JOYSTICK_WINMM 1)
  1425     endif()
  1426     set(HAVE_SDL_JOYSTICK TRUE)
  1427 
  1428     if(SDL_HAPTIC)
  1429       if(HAVE_DINPUT_H OR HAVE_XINPUT_H)
  1430         file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/windows/*.c)
  1431         if(HAVE_DINPUT_H)
  1432           set(SDL_HAPTIC_DINPUT 1)
  1433         endif()
  1434         if(HAVE_XINPUT_H)
  1435           set(SDL_HAPTIC_XINPUT 1)
  1436         endif()
  1437       else()
  1438         file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
  1439         set(SDL_HAPTIC_DUMMY 1)
  1440       endif()
  1441       set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
  1442       set(HAVE_SDL_HAPTIC TRUE)
  1443     endif()
  1444   endif()
  1445 
  1446   file(GLOB VERSION_SOURCES ${SDL2_SOURCE_DIR}/src/main/windows/*.rc)
  1447   file(GLOB SDLMAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/windows/*.c)
  1448   if(MINGW OR CYGWIN)
  1449     list(APPEND EXTRA_LIBS mingw32)
  1450     list(APPEND EXTRA_LDFLAGS "-mwindows")
  1451     set(SDL_CFLAGS "${SDL_CFLAGS} -Dmain=SDL_main")
  1452     list(APPEND SDL_LIBS "-lmingw32" "-lSDL2main" "-mwindows")
  1453   endif()
  1454 
  1455 elseif(APPLE)
  1456   # TODO: rework this all for proper MacOS X, iOS and Darwin support
  1457 
  1458   # We always need these libs on macOS at the moment.
  1459   # !!! FIXME: we need Carbon for some very old API calls in
  1460   # !!! FIXME:  src/video/cocoa/SDL_cocoakeyboard.c, but we should figure out
  1461   # !!! FIXME:  how to dump those.
  1462   if(DARWIN OR MACOSX)
  1463     set(SDL_FRAMEWORK_COCOA 1)
  1464     set(SDL_FRAMEWORK_CARBON 1)
  1465   endif()
  1466   set(SDL_FRAMEWORK_FOUNDATION 1)
  1467 
  1468   # Requires the darwin file implementation
  1469   if(SDL_FILE)
  1470     file(GLOB EXTRA_SOURCES ${SDL2_SOURCE_DIR}/src/file/cocoa/*.m)
  1471     set(SOURCE_FILES ${EXTRA_SOURCES} ${SOURCE_FILES})
  1472     # !!! FIXME: modern CMake doesn't need "LANGUAGE C" for Objective-C.
  1473     set_source_files_properties(${EXTRA_SOURCES} PROPERTIES LANGUAGE C)
  1474     set(HAVE_SDL_FILE TRUE)
  1475     # !!! FIXME: why is COREVIDEO inside this if() block?
  1476     set(SDL_FRAMEWORK_COREVIDEO 1)
  1477   else()
  1478     message_error("SDL_FILE must be enabled to build on MacOS X")
  1479   endif()
  1480 
  1481   if(SDL_AUDIO)
  1482     set(SDL_AUDIO_DRIVER_COREAUDIO 1)
  1483     file(GLOB AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/coreaudio/*.m)
  1484     # !!! FIXME: modern CMake doesn't need "LANGUAGE C" for Objective-C.
  1485     set_source_files_properties(${AUDIO_SOURCES} PROPERTIES LANGUAGE C)
  1486     set(SOURCE_FILES ${SOURCE_FILES} ${AUDIO_SOURCES})
  1487     set(HAVE_SDL_AUDIO TRUE)
  1488     set(SDL_FRAMEWORK_COREAUDIO 1)
  1489     set(SDL_FRAMEWORK_AUDIOTOOLBOX 1)
  1490     set(SDL_FRAMEWORK_AVFOUNDATION 1)
  1491   endif()
  1492 
  1493   if(SDL_JOYSTICK)
  1494     CheckHIDAPI()
  1495     if(HAVE_HIDAPI)
  1496       if(IOS OR TVOS)
  1497         set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/ios/hid.m)
  1498         set(SDL_FRAMEWORK_COREBLUETOOTH 1)
  1499       endif()
  1500     endif()
  1501     if(IOS OR TVOS)
  1502       file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/iphoneos/*.m ${SDL2_SOURCE_DIR}/src/joystick/steam/*.c)
  1503       set(SDL_JOYSTICK_MFI 1)
  1504       if(IOS)
  1505         set(SDL_FRAMEWORK_COREMOTION 1)
  1506       endif()
  1507       set(SDL_FRAMEWORK_GAMECONTROLLER 1)
  1508       set(HAVE_SDL_SENSORS 1)
  1509     else()
  1510       file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/darwin/*.c)
  1511       set(SDL_JOYSTICK_IOKIT 1)
  1512       set(SDL_FRAMEWORK_IOKIT 1)
  1513       set(SDL_FRAMEWORK_FF 1)
  1514     endif()
  1515     set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
  1516     set(HAVE_SDL_JOYSTICK TRUE)
  1517   endif()
  1518 
  1519   if(SDL_HAPTIC)
  1520     if (IOS OR TVOS)
  1521       file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
  1522       set(SDL_HAPTIC_DUMMY 1)
  1523     else()
  1524       file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/darwin/*.c)
  1525       set(SDL_HAPTIC_IOKIT 1)
  1526       set(SDL_FRAMEWORK_IOKIT 1)
  1527       set(SDL_FRAMEWORK_FF 1)
  1528     endif()
  1529     set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
  1530     set(HAVE_SDL_HAPTIC TRUE)
  1531     if(NOT SDL_JOYSTICK)
  1532       message(FATAL_ERROR "SDL_HAPTIC requires SDL_JOYSTICK to be enabled")
  1533     endif()
  1534   endif()
  1535 
  1536   if(SDL_POWER)
  1537     if (IOS OR TVOS)
  1538       file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/uikit/*.m)
  1539       set(SDL_POWER_UIKIT 1)
  1540     else()
  1541       file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/macosx/*.c)
  1542       set(SDL_POWER_MACOSX 1)
  1543       set(SDL_FRAMEWORK_IOKIT 1)
  1544     endif()
  1545     set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
  1546     set(HAVE_SDL_POWER TRUE)
  1547   endif()
  1548 
  1549   if(SDL_TIMERS)
  1550     set(SDL_TIMER_UNIX 1)
  1551     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
  1552     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1553     set(HAVE_SDL_TIMERS TRUE)
  1554   endif(SDL_TIMERS)
  1555 
  1556   if(SDL_FILESYSTEM)
  1557     set(SDL_FILESYSTEM_COCOA 1)
  1558     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/cocoa/*.m)
  1559     # !!! FIXME: modern CMake doesn't need "LANGUAGE C" for Objective-C.
  1560     set_source_files_properties(${FILESYSTEM_SOURCES} PROPERTIES LANGUAGE C)
  1561     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1562     set(HAVE_SDL_FILESYSTEM TRUE)
  1563   endif()
  1564 
  1565   if(SDL_SENSOR)
  1566     if(IOS)
  1567       set(SDL_SENSOR_COREMOTION 1)
  1568       set(HAVE_SDL_SENSORS TRUE)
  1569       file(GLOB SENSOR_SOURCES ${SDL2_SOURCE_DIR}/src/sensor/coremotion/*.m)
  1570       set(SOURCE_FILES ${SOURCE_FILES} ${SENSOR_SOURCES})
  1571     endif()
  1572   endif()
  1573 
  1574   # iOS hack needed - http://code.google.com/p/ios-cmake/ ?
  1575   if(SDL_VIDEO)
  1576     if (IOS OR TVOS)
  1577       set(SDL_VIDEO_DRIVER_UIKIT 1)
  1578       set(SDL_FRAMEWORK_COREGRAPHICS 1)
  1579       set(SDL_FRAMEWORK_QUARTZCORE 1)
  1580       set(SDL_FRAMEWORK_UIKIT 1)
  1581       set(SDL_IPHONE_KEYBOARD 1)
  1582       set(SDL_IPHONE_LAUNCHSCREEN 1)
  1583       file(GLOB UIKITVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/uikit/*.m)
  1584       set(SOURCE_FILES ${SOURCE_FILES} ${UIKITVIDEO_SOURCES})
  1585     else()
  1586       CheckCOCOA()
  1587       if(VIDEO_OPENGL)
  1588         set(SDL_VIDEO_OPENGL 1)
  1589         set(SDL_VIDEO_OPENGL_CGL 1)
  1590         set(SDL_VIDEO_RENDER_OGL 1)
  1591         set(HAVE_VIDEO_OPENGL TRUE)
  1592       endif()
  1593     endif()
  1594 
  1595     if(VIDEO_OPENGLES)
  1596       if(IOS OR TVOS)
  1597         set(SDL_FRAMEWORK_OPENGLES 1)
  1598         set(SDL_VIDEO_OPENGL_ES 1)
  1599         set(SDL_VIDEO_RENDER_OGL_ES 1)
  1600       else()
  1601         set(SDL_VIDEO_OPENGL_EGL 1)
  1602       endif()
  1603       set(SDL_VIDEO_OPENGL_ES2 1)
  1604       set(SDL_VIDEO_RENDER_OGL_ES2 1)
  1605       set(HAVE_VIDEO_OPENGLES TRUE)
  1606     endif()
  1607 
  1608     if(VIDEO_VULKAN OR VIDEO_METAL OR RENDER_METAL)
  1609       set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  1610       set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -x objective-c")
  1611       check_c_source_compiles("
  1612         #include <AvailabilityMacros.h>
  1613         #import <Metal/Metal.h>
  1614         #import <QuartzCore/CAMetalLayer.h>
  1615 
  1616         #if TARGET_OS_SIMULATOR || (!TARGET_CPU_X86_64 && !TARGET_CPU_ARM64)
  1617         #error Metal doesn't work on this configuration
  1618         #endif
  1619         int main()
  1620         {
  1621             return 0;
  1622         }
  1623         " HAVE_FRAMEWORK_METAL)
  1624       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
  1625       if(HAVE_FRAMEWORK_METAL)
  1626         set(SDL_FRAMEWORK_METAL 1)
  1627         set(SDL_FRAMEWORK_QUARTZCORE 1)
  1628       else()
  1629         set(VIDEO_VULKAN 0)
  1630         set(VIDEO_METAL 0)
  1631         set(RENDER_METAL 0)
  1632       endif()
  1633     endif()
  1634 
  1635     if(VIDEO_METAL)
  1636       set(SDL_VIDEO_METAL 1)
  1637       set(HAVE_VIDEO_METAL TRUE)
  1638     endif()
  1639 
  1640     if(RENDER_METAL)
  1641       file(GLOB RENDER_METAL_SOURCES ${SDL2_SOURCE_DIR}/src/render/metal/*.m)
  1642       set(SOURCE_FILES ${SOURCE_FILES} ${RENDER_METAL_SOURCES})
  1643       set(SDL_VIDEO_RENDER_METAL 1)
  1644       set(HAVE_RENDER_METAL TRUE)
  1645     endif()
  1646   endif()
  1647 
  1648   # Actually load the frameworks at the end so we don't duplicate include.
  1649   if(SDL_FRAMEWORK_COREVIDEO)
  1650     find_library(COREVIDEO CoreVideo)
  1651     list(APPEND EXTRA_LIBS ${COREVIDEO})
  1652   endif()
  1653   if(SDL_FRAMEWORK_COCOA)
  1654     find_library(COCOA_LIBRARY Cocoa)
  1655     list(APPEND EXTRA_LIBS ${COCOA_LIBRARY})
  1656   endif()
  1657   if(SDL_FRAMEWORK_IOKIT)
  1658     find_library(IOKIT IOKit)
  1659     list(APPEND EXTRA_LIBS ${IOKIT})
  1660   endif()
  1661   if(SDL_FRAMEWORK_FF)
  1662     find_library(FORCEFEEDBACK ForceFeedback)
  1663     list(APPEND EXTRA_LIBS ${FORCEFEEDBACK})
  1664   endif()
  1665   if(SDL_FRAMEWORK_CARBON)
  1666     find_library(CARBON_LIBRARY Carbon)
  1667     list(APPEND EXTRA_LIBS ${CARBON_LIBRARY})
  1668   endif()
  1669   if(SDL_FRAMEWORK_COREAUDIO)
  1670     find_library(COREAUDIO CoreAudio)
  1671     list(APPEND EXTRA_LIBS ${COREAUDIO})
  1672   endif()
  1673   if(SDL_FRAMEWORK_AUDIOTOOLBOX)
  1674     find_library(AUDIOTOOLBOX AudioToolbox)
  1675     list(APPEND EXTRA_LIBS ${AUDIOTOOLBOX})
  1676   endif()
  1677   if(SDL_FRAMEWORK_AVFOUNDATION)
  1678     find_library(AVFOUNDATION AVFoundation)
  1679     list(APPEND EXTRA_LIBS ${AVFOUNDATION})
  1680   endif()
  1681   if(SDL_FRAMEWORK_COREBLUETOOTH)
  1682     find_library(COREBLUETOOTH CoreBluetooth)
  1683     list(APPEND EXTRA_LIBS ${COREBLUETOOTH})
  1684   endif()
  1685   if(SDL_FRAMEWORK_COREGRAPHICS)
  1686     find_library(COREGRAPHICS CoreGraphics)
  1687     list(APPEND EXTRA_LIBS ${COREGRAPHICS})
  1688   endif()
  1689   if(SDL_FRAMEWORK_COREMOTION)
  1690     find_library(COREMOTION CoreMotion)
  1691     list(APPEND EXTRA_LIBS ${COREMOTION})
  1692   endif()
  1693   if(SDL_FRAMEWORK_FOUNDATION)
  1694     find_library(FOUNDATION Foundation)
  1695     list(APPEND EXTRA_LIBS ${FOUNDATION})
  1696   endif()
  1697   if(SDL_FRAMEWORK_GAMECONTROLLER)
  1698     find_library(GAMECONTROLLER GameController)
  1699     list(APPEND EXTRA_LIBS ${GAMECONTROLLER})
  1700   endif()
  1701   if(SDL_FRAMEWORK_METAL)
  1702     if(IOS OR TVOS)
  1703       find_library(METAL Metal)
  1704       list(APPEND EXTRA_LIBS ${METAL})
  1705     else()
  1706       list(APPEND EXTRA_LDFLAGS "-Wl,-weak_framework,Metal")
  1707     endif()
  1708   endif()
  1709   if(SDL_FRAMEWORK_OPENGLES)
  1710     find_library(OPENGLES OpenGLES)
  1711     list(APPEND EXTRA_LIBS ${OPENGLES})
  1712   endif()
  1713   if(SDL_FRAMEWORK_QUARTZCORE)
  1714     if(IOS OR TVOS)
  1715       find_library(QUARTZCORE QuartzCore)
  1716       list(APPEND EXTRA_LIBS ${QUARTZCORE})
  1717     else()
  1718       list(APPEND EXTRA_LDFLAGS "-Wl,-weak_framework,QuartzCore")
  1719     endif()
  1720   endif()
  1721   if(SDL_FRAMEWORK_UIKIT)
  1722     find_library(UIKIT UIKit)
  1723     list(APPEND EXTRA_LIBS ${UIKIT})
  1724   endif()
  1725 
  1726   CheckPTHREAD()
  1727 
  1728 elseif(HAIKU)
  1729   if(SDL_VIDEO)
  1730     set(SDL_VIDEO_DRIVER_HAIKU 1)
  1731     file(GLOB HAIKUVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/haiku/*.c)
  1732     set(SOURCE_FILES ${SOURCE_FILES} ${HAIKUVIDEO_SOURCES})
  1733     set(HAVE_SDL_VIDEO TRUE)
  1734 
  1735     set(SDL_FILESYSTEM_HAIKU 1)
  1736     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/haiku/*.cc)
  1737     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1738     set(HAVE_SDL_FILESYSTEM TRUE)
  1739 
  1740     if(SDL_TIMERS)
  1741       set(SDL_TIMER_HAIKU 1)
  1742       file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/haiku/*.c)
  1743       set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1744       set(HAVE_SDL_TIMERS TRUE)
  1745     endif(SDL_TIMERS)
  1746 
  1747     if(VIDEO_OPENGL)
  1748       # TODO: Use FIND_PACKAGE(OpenGL) instead
  1749       set(SDL_VIDEO_OPENGL 1)
  1750       set(SDL_VIDEO_OPENGL_BGL 1)
  1751       set(SDL_VIDEO_RENDER_OGL 1)
  1752       list(APPEND EXTRA_LIBS GL)
  1753       set(HAVE_VIDEO_OPENGL TRUE)
  1754     endif()
  1755   endif()
  1756 
  1757   CheckPTHREAD()
  1758 endif()
  1759 
  1760 if(VIDEO_VULKAN)
  1761   set(SDL_VIDEO_VULKAN 1)
  1762   set(HAVE_VIDEO_VULKAN TRUE)
  1763 endif()
  1764 
  1765 # Dummies
  1766 # configure.ac does it differently:
  1767 # if not have X
  1768 #   if enable_X {  SDL_X_DISABLED = 1 }
  1769 #   [add dummy sources]
  1770 # so it always adds a dummy, without checking, if it was actually requested.
  1771 # This leads to missing internal references on building, since the
  1772 # src/X/*.c does not get included.
  1773 if(NOT HAVE_SDL_JOYSTICK)
  1774   set(SDL_JOYSTICK_DUMMY 1)
  1775   if(SDL_JOYSTICK AND NOT APPLE) # results in unresolved symbols on OSX
  1776 
  1777     file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/dummy/*.c)
  1778     set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
  1779   endif()
  1780 endif()
  1781 if(NOT HAVE_SDL_HAPTIC)
  1782   set(SDL_HAPTIC_DUMMY 1)
  1783   file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
  1784   set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
  1785 endif()
  1786 if(NOT HAVE_SDL_SENSORS)
  1787   set(SDL_SENSOR_DUMMY 1)
  1788   file(GLOB SENSORS_SOURCES ${SDL2_SOURCE_DIR}/src/sensor/dummy/*.c)
  1789   set(SOURCE_FILES ${SOURCE_FILES} ${SENSORS_SOURCES})
  1790 endif()
  1791 if(NOT HAVE_SDL_LOADSO)
  1792   set(SDL_LOADSO_DISABLED 1)
  1793   file(GLOB LOADSO_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/dummy/*.c)
  1794   set(SOURCE_FILES ${SOURCE_FILES} ${LOADSO_SOURCES})
  1795 endif()
  1796 if(NOT HAVE_SDL_FILESYSTEM)
  1797   set(SDL_FILESYSTEM_DISABLED 1)
  1798   file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/dummy/*.c)
  1799   set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1800 endif()
  1801 
  1802 # We always need to have threads and timers around
  1803 if(NOT HAVE_SDL_THREADS)
  1804   set(SDL_THREADS_DISABLED 1)
  1805   file(GLOB THREADS_SOURCES ${SDL2_SOURCE_DIR}/src/thread/generic/*.c)
  1806   set(SOURCE_FILES ${SOURCE_FILES} ${THREADS_SOURCES})
  1807 endif()
  1808 if(NOT HAVE_SDL_TIMERS)
  1809   set(SDL_TIMERS_DISABLED 1)
  1810   file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/dummy/*.c)
  1811   set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1812 endif()
  1813 
  1814 if(NOT SDLMAIN_SOURCES)
  1815   file(GLOB SDLMAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/dummy/*.c)
  1816 endif()
  1817 
  1818 # Append the -MMD -MT flags
  1819 # if(DEPENDENCY_TRACKING)
  1820 #   if(COMPILER_IS_GNUCC)
  1821 #     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -MMD -MT \$@")
  1822 #   endif()
  1823 # endif()
  1824 
  1825 configure_file("${SDL2_SOURCE_DIR}/include/SDL_config.h.cmake"
  1826   "${SDL2_BINARY_DIR}/include/SDL_config.h")
  1827 
  1828 # Prepare the flags and remove duplicates
  1829 if(EXTRA_LDFLAGS)
  1830   list(REMOVE_DUPLICATES EXTRA_LDFLAGS)
  1831 endif()
  1832 if(EXTRA_LIBS)
  1833   list(REMOVE_DUPLICATES EXTRA_LIBS)
  1834 endif()
  1835 if(EXTRA_CFLAGS)
  1836   list(REMOVE_DUPLICATES EXTRA_CFLAGS)
  1837 endif()
  1838 listtostr(EXTRA_CFLAGS _EXTRA_CFLAGS)
  1839 set(EXTRA_CFLAGS ${_EXTRA_CFLAGS})
  1840 
  1841 # Compat helpers for the configuration files
  1842 if(NOT WINDOWS OR CYGWIN)
  1843   # TODO: we need a Windows script, too
  1844   execute_process(COMMAND sh ${SDL2_SOURCE_DIR}/build-scripts/updaterev.sh)
  1845 
  1846   set(prefix ${CMAKE_INSTALL_PREFIX})
  1847   set(exec_prefix "\${prefix}")
  1848   set(libdir "\${exec_prefix}/lib${LIB_SUFFIX}")
  1849   set(bindir "\${exec_prefix}/bin")
  1850   set(includedir "\${prefix}/include")
  1851   if(SDL_STATIC)
  1852     set(ENABLE_STATIC_TRUE "")
  1853     set(ENABLE_STATIC_FALSE "#")
  1854   else()
  1855     set(ENABLE_STATIC_TRUE "#")
  1856     set(ENABLE_STATIC_FALSE "")
  1857   endif()
  1858   if(SDL_SHARED)
  1859     set(ENABLE_SHARED_TRUE "")
  1860     set(ENABLE_SHARED_FALSE "#")
  1861   else()
  1862     set(ENABLE_SHARED_TRUE "#")
  1863     set(ENABLE_SHARED_FALSE "")
  1864   endif()
  1865 
  1866   # Clean up the different lists
  1867   listtostr(EXTRA_LIBS _EXTRA_LIBS "-l")
  1868   set(SDL_STATIC_LIBS ${SDL_LIBS} ${EXTRA_LDFLAGS} ${_EXTRA_LIBS})
  1869   list(REMOVE_DUPLICATES SDL_STATIC_LIBS)
  1870   listtostr(SDL_STATIC_LIBS _SDL_STATIC_LIBS)
  1871   set(SDL_STATIC_LIBS ${_SDL_STATIC_LIBS})
  1872   listtostr(SDL_LIBS _SDL_LIBS)
  1873   set(SDL_LIBS ${_SDL_LIBS})
  1874 
  1875   # MESSAGE(STATUS "SDL_LIBS: ${SDL_LIBS}")
  1876   # MESSAGE(STATUS "SDL_STATIC_LIBS: ${SDL_STATIC_LIBS}")
  1877 
  1878   configure_file("${SDL2_SOURCE_DIR}/sdl2.pc.in"
  1879     "${SDL2_BINARY_DIR}/sdl2.pc" @ONLY)
  1880   configure_file("${SDL2_SOURCE_DIR}/sdl2-config.in"
  1881     "${SDL2_BINARY_DIR}/sdl2-config")
  1882   configure_file("${SDL2_SOURCE_DIR}/sdl2-config.in"
  1883     "${SDL2_BINARY_DIR}/sdl2-config" @ONLY)
  1884   configure_file("${SDL2_SOURCE_DIR}/SDL2.spec.in"
  1885     "${SDL2_BINARY_DIR}/SDL2.spec" @ONLY)
  1886 endif()
  1887 
  1888 ##### Info output #####
  1889 message(STATUS "")
  1890 message(STATUS "SDL2 was configured with the following options:")
  1891 message(STATUS "")
  1892 message(STATUS "Platform: ${CMAKE_SYSTEM}")
  1893 message(STATUS "64-bit:   ${ARCH_64}")
  1894 message(STATUS "Compiler: ${CMAKE_C_COMPILER}")
  1895 message(STATUS "")
  1896 message(STATUS "Subsystems:")
  1897 foreach(_SUB ${SDL_SUBSYSTEMS})
  1898   string(TOUPPER ${_SUB} _OPT)
  1899   message_bool_option(${_SUB} SDL_${_OPT})
  1900 endforeach()
  1901 message(STATUS "")
  1902 message(STATUS "Options:")
  1903 list(SORT ALLOPTIONS)
  1904 foreach(_OPT ${ALLOPTIONS})
  1905   # Longest option is VIDEO_X11_XSCREENSAVER = 22 characters
  1906   # Get the padding
  1907   string(LENGTH ${_OPT} _OPTLEN)
  1908   math(EXPR _PADLEN "23 - ${_OPTLEN}")
  1909   string(RANDOM LENGTH ${_PADLEN} ALPHABET " " _PADDING)
  1910   message_tested_option(${_OPT} ${_PADDING})
  1911 endforeach()
  1912 message(STATUS "")
  1913 message(STATUS " CFLAGS:        ${CMAKE_C_FLAGS}")
  1914 message(STATUS " EXTRA_CFLAGS:  ${EXTRA_CFLAGS}")
  1915 message(STATUS " EXTRA_LDFLAGS: ${EXTRA_LDFLAGS}")
  1916 message(STATUS " EXTRA_LIBS:    ${EXTRA_LIBS}")
  1917 message(STATUS "")
  1918 message(STATUS " Build Shared Library: ${SDL_SHARED}")
  1919 message(STATUS " Build Static Library: ${SDL_STATIC}")
  1920 if(SDL_STATIC)
  1921     message(STATUS " Build Static Library with Position Independent Code: ${SDL_STATIC_PIC}")
  1922 endif()
  1923 message(STATUS "")
  1924 if(UNIX)
  1925   message(STATUS "If something was not detected, although the libraries")
  1926   message(STATUS "were installed, then make sure you have set the")
  1927   message(STATUS "CFLAGS and LDFLAGS environment variables correctly.")
  1928   message(STATUS "")
  1929 endif()
  1930 
  1931 # Ensure that the extra cflags are used at compile time
  1932 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
  1933 
  1934 # Always build SDLmain
  1935 add_library(SDL2main STATIC ${SDLMAIN_SOURCES})
  1936 target_include_directories(SDL2main PUBLIC "$<BUILD_INTERFACE:${SDL2_SOURCE_DIR}/include>" $<INSTALL_INTERFACE:include/SDL2>)
  1937 set(_INSTALL_LIBS "SDL2main")
  1938 if (NOT ANDROID)
  1939   set_target_properties(SDL2main PROPERTIES DEBUG_POSTFIX ${SDL_CMAKE_DEBUG_POSTFIX})
  1940 endif()
  1941 
  1942 if (ANDROID AND HAVE_HIDAPI)
  1943   set(_INSTALL_LIBS ${_INSTALL_LIBS} "hidapi")
  1944 endif()
  1945 
  1946 if(SDL_SHARED)
  1947   add_library(SDL2 SHARED ${SOURCE_FILES} ${VERSION_SOURCES})
  1948   if(APPLE)
  1949     set_target_properties(SDL2 PROPERTIES
  1950       MACOSX_RPATH 1
  1951       OUTPUT_NAME "SDL2-${LT_RELEASE}")
  1952   elseif(UNIX AND NOT ANDROID)
  1953     set_target_properties(SDL2 PROPERTIES
  1954       VERSION ${LT_VERSION}
  1955       SOVERSION ${LT_REVISION}
  1956       OUTPUT_NAME "SDL2-${LT_RELEASE}")
  1957   else()
  1958     set_target_properties(SDL2 PROPERTIES
  1959       VERSION ${SDL_VERSION}
  1960       SOVERSION ${LT_REVISION}
  1961       OUTPUT_NAME "SDL2")
  1962   endif()
  1963   if(MSVC AND NOT LIBC)
  1964     # Don't try to link with the default set of libraries.
  1965     set_target_properties(SDL2 PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
  1966     set_target_properties(SDL2 PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
  1967     set_target_properties(SDL2 PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
  1968   endif()
  1969   set(_INSTALL_LIBS "SDL2" ${_INSTALL_LIBS})
  1970   target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
  1971   target_include_directories(SDL2 PUBLIC "$<BUILD_INTERFACE:${SDL2_SOURCE_DIR}/include>" $<INSTALL_INTERFACE:include/SDL2>)
  1972   if (NOT ANDROID)
  1973     set_target_properties(SDL2 PROPERTIES DEBUG_POSTFIX ${SDL_CMAKE_DEBUG_POSTFIX})
  1974   endif()
  1975   if(IOS OR TVOS)
  1976     set_property(TARGET SDL2 APPEND_STRING PROPERTY COMPILE_FLAGS "-fobjc-arc")
  1977     target_compile_definitions(SDL2 PRIVATE IOS_DYLIB=1)
  1978   endif()
  1979 endif()
  1980 
  1981 if(ANDROID)
  1982   if(HAVE_HIDAPI)
  1983     add_library(hidapi SHARED ${SDL2_SOURCE_DIR}/src/hidapi/android/hid.cpp)
  1984   endif()
  1985 
  1986   if(MSVC AND NOT LIBC)
  1987     # Don't try to link with the default set of libraries.
  1988     set_target_properties(hidapi PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
  1989     set_target_properties(hidapi PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
  1990     set_target_properties(hidapi PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
  1991   endif()
  1992   if(HAVE_HIDAPI)
  1993     target_link_libraries(hidapi log)
  1994   endif()
  1995 endif()
  1996 
  1997 if(SDL_STATIC)
  1998   set (BUILD_SHARED_LIBS FALSE)
  1999   add_library(SDL2-static STATIC ${SOURCE_FILES})
  2000   if (NOT SDL_SHARED OR NOT WIN32)
  2001     set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
  2002     # Note: Apparently, OUTPUT_NAME must really be unique; even when
  2003     # CMAKE_IMPORT_LIBRARY_SUFFIX or the like are given. Otherwise
  2004     # the static build may race with the import lib and one will get
  2005     # clobbered, when the suffix is realized via subsequent rename.
  2006   endif()
  2007   set_target_properties(SDL2-static PROPERTIES POSITION_INDEPENDENT_CODE ${SDL_STATIC_PIC})
  2008   if(MSVC AND NOT LIBC)
  2009     set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
  2010     set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
  2011     set_target_properties(SDL2-static PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
  2012   endif()
  2013   # TODO: Win32 platforms keep the same suffix .lib for import and static
  2014   # libraries - do we need to consider this?
  2015   set(_INSTALL_LIBS "SDL2-static" ${_INSTALL_LIBS})
  2016   target_link_libraries(SDL2-static ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
  2017   target_include_directories(SDL2-static PUBLIC "$<BUILD_INTERFACE:${SDL2_SOURCE_DIR}/include>" $<INSTALL_INTERFACE:include/SDL2>)
  2018   if (NOT ANDROID)
  2019     set_target_properties(SDL2-static PROPERTIES DEBUG_POSTFIX ${SDL_CMAKE_DEBUG_POSTFIX})
  2020   endif()
  2021   if(IOS OR TVOS)
  2022     set_property(TARGET SDL2-static APPEND_STRING PROPERTY COMPILE_FLAGS "-fobjc-arc")
  2023   endif()
  2024 endif()
  2025 
  2026 ##### Tests #####
  2027 
  2028 if(SDL_TEST)
  2029   file(GLOB TEST_SOURCES ${SDL2_SOURCE_DIR}/src/test/*.c)
  2030   add_library(SDL2_test STATIC ${TEST_SOURCES})
  2031 
  2032   add_subdirectory(test)
  2033 endif()
  2034 
  2035 ##### Installation targets #####
  2036 install(TARGETS ${_INSTALL_LIBS} EXPORT SDL2Targets
  2037   LIBRARY DESTINATION "lib${LIB_SUFFIX}"
  2038   ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
  2039   RUNTIME DESTINATION bin)
  2040 
  2041 ##### Export files #####
  2042 if (WINDOWS)
  2043   set(PKG_PREFIX "cmake")
  2044 else ()
  2045   set(PKG_PREFIX "lib/cmake/SDL2")
  2046 endif ()
  2047 
  2048 include(CMakePackageConfigHelpers)
  2049 write_basic_package_version_file("${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake"
  2050   VERSION ${SDL_VERSION}
  2051   COMPATIBILITY AnyNewerVersion
  2052 )
  2053 
  2054 install(EXPORT SDL2Targets
  2055   FILE SDL2Targets.cmake
  2056   NAMESPACE SDL2::
  2057   DESTINATION ${PKG_PREFIX}
  2058 )
  2059 install(
  2060   FILES
  2061     ${CMAKE_CURRENT_SOURCE_DIR}/SDL2Config.cmake
  2062     ${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake
  2063   DESTINATION ${PKG_PREFIX}
  2064   COMPONENT Devel
  2065 )
  2066 
  2067 file(GLOB INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/*.h)
  2068 file(GLOB BIN_INCLUDE_FILES ${SDL2_BINARY_DIR}/include/*.h)
  2069 foreach(_FNAME ${BIN_INCLUDE_FILES})
  2070   get_filename_component(_INCNAME ${_FNAME} NAME)
  2071   list(REMOVE_ITEM INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/${_INCNAME})
  2072 endforeach()
  2073 list(APPEND INCLUDE_FILES ${BIN_INCLUDE_FILES})
  2074 install(FILES ${INCLUDE_FILES} DESTINATION include/SDL2)
  2075 
  2076 string(TOUPPER "${CMAKE_BUILD_TYPE}" UPPER_BUILD_TYPE)
  2077 if (UPPER_BUILD_TYPE MATCHES DEBUG)
  2078   set(SOPOSTFIX "${SDL_CMAKE_DEBUG_POSTFIX}")
  2079 else()
  2080   set(SOPOSTFIX "")
  2081 endif()
  2082 
  2083 if(NOT (WINDOWS OR CYGWIN))
  2084   if(SDL_SHARED)
  2085     set(SOEXT ${CMAKE_SHARED_LIBRARY_SUFFIX}) # ".so", ".dylib", etc.
  2086     get_target_property(SONAME SDL2 OUTPUT_NAME)
  2087     if(NOT ANDROID)
  2088         install(CODE "
  2089           execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
  2090             \"lib${SONAME}${SOPOSTFIX}${SOEXT}\" \"libSDL2${SOPOSTFIX}${SOEXT}\"
  2091             WORKING_DIRECTORY \"${SDL2_BINARY_DIR}\")")
  2092         install(FILES ${SDL2_BINARY_DIR}/libSDL2${SOPOSTFIX}${SOEXT} DESTINATION "lib${LIB_SUFFIX}")
  2093     endif()
  2094   endif()
  2095   if(FREEBSD)
  2096     # FreeBSD uses ${PREFIX}/libdata/pkgconfig
  2097     install(FILES ${SDL2_BINARY_DIR}/sdl2.pc DESTINATION "libdata/pkgconfig")
  2098   else()
  2099     install(FILES ${SDL2_BINARY_DIR}/sdl2.pc
  2100       DESTINATION "lib${LIB_SUFFIX}/pkgconfig")
  2101   endif()
  2102   install(PROGRAMS ${SDL2_BINARY_DIR}/sdl2-config DESTINATION bin)
  2103   # TODO: what about the .spec file? Is it only needed for RPM creation?
  2104   install(FILES "${SDL2_SOURCE_DIR}/sdl2.m4" DESTINATION "${CMAKE_INSTALL_FULL_DATAROOTDIR}/aclocal")
  2105 endif()
  2106 
  2107 ##### Uninstall target #####
  2108 
  2109 if(NOT TARGET uninstall)
  2110   configure_file(
  2111       "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
  2112       "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
  2113       IMMEDIATE @ONLY)
  2114 
  2115   add_custom_target(uninstall
  2116       COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
  2117 endif()