CMakeLists.txt
author Sam Lantinga <slouken@libsdl.org>
Mon, 12 Jun 2017 16:35:34 -0700
changeset 11095 5cf754a84eb7
parent 11089 84a49d9b3e58
child 11138 c936a84f05ba
permissions -rw-r--r--
Fixed bug 3670 - CMake IOS haptic error
     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.5)
     6 project(SDL2 C)
     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(CheckCSourceRuns)
    24 include(CheckCCompilerFlag)
    25 include(CheckTypeSize)
    26 include(CheckStructHasMember)
    27 include(CMakeDependentOption)
    28 include(FindPkgConfig)
    29 include(GNUInstallDirs)
    30 set(CMAKE_MODULE_PATH "${SDL2_SOURCE_DIR}/cmake")
    31 include(${SDL2_SOURCE_DIR}/cmake/macros.cmake)
    32 include(${SDL2_SOURCE_DIR}/cmake/sdlchecks.cmake)
    33 
    34 # General settings
    35 # Edit include/SDL_version.h and change the version, then:
    36 #   SDL_MICRO_VERSION += 1;
    37 #   SDL_INTERFACE_AGE += 1;
    38 #   SDL_BINARY_AGE += 1;
    39 # if any functions have been added, set SDL_INTERFACE_AGE to 0.
    40 # if backwards compatibility has been broken,
    41 # set SDL_BINARY_AGE and SDL_INTERFACE_AGE to 0.
    42 set(SDL_MAJOR_VERSION 2)
    43 set(SDL_MINOR_VERSION 0)
    44 set(SDL_MICRO_VERSION 5)
    45 set(SDL_INTERFACE_AGE 1)
    46 set(SDL_BINARY_AGE 5)
    47 set(SDL_VERSION "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}.${SDL_MICRO_VERSION}")
    48 
    49 # Calculate a libtool-like version number
    50 math(EXPR LT_CURRENT "${SDL_MICRO_VERSION} - ${SDL_INTERFACE_AGE}")
    51 math(EXPR LT_AGE "${SDL_BINARY_AGE} - ${SDL_INTERFACE_AGE}")
    52 math(EXPR LT_MAJOR "${LT_CURRENT}- ${LT_AGE}")
    53 set(LT_REVISION "${SDL_INTERFACE_AGE}")
    54 set(LT_RELEASE "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}")
    55 set(LT_VERSION "${LT_MAJOR}.${LT_AGE}.${LT_REVISION}")
    56 
    57 message(STATUS "${LT_VERSION} :: ${LT_AGE} :: ${LT_REVISION} :: ${LT_CURRENT} :: ${LT_RELEASE}")
    58 
    59 # General settings & flags
    60 set(LIBRARY_OUTPUT_DIRECTORY "build")
    61 # Check for 64 or 32 bit
    62 set(SIZEOF_VOIDP ${CMAKE_SIZEOF_VOID_P})
    63 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    64   set(ARCH_64 TRUE)
    65   set(PROCESSOR_ARCH "x64")
    66 else()
    67   set(ARCH_64 FALSE)
    68   set(PROCESSOR_ARCH "x86")
    69 endif()
    70 set(LIBNAME SDL2)
    71 if(NOT LIBTYPE)
    72   set(LIBTYPE SHARED)
    73 endif()
    74 
    75 # Get the platform
    76 if(WIN32)
    77   if(NOT WINDOWS)
    78     set(WINDOWS TRUE)
    79   endif()
    80 elseif(UNIX AND NOT APPLE)
    81   if(CMAKE_SYSTEM_NAME MATCHES ".*Linux")
    82     set(LINUX TRUE)
    83   elseif(CMAKE_SYSTEM_NAME MATCHES "kFreeBSD.*")
    84     set(FREEBSD TRUE)
    85   elseif(CMAKE_SYSTEM_NAME MATCHES "kNetBSD.*|NetBSD.*")
    86     set(NETBSD TRUE)
    87   elseif(CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*")
    88     set(OPENBSD TRUE)
    89   elseif(CMAKE_SYSTEM_NAME MATCHES ".*GNU.*")
    90     set(GNU TRUE)
    91   elseif(CMAKE_SYSTEM_NAME MATCHES ".*BSDI.*")
    92     set(BSDI TRUE)
    93   elseif(CMAKE_SYSTEM_NAME MATCHES "DragonFly.*|FreeBSD")
    94     set(FREEBSD TRUE)
    95   elseif(CMAKE_SYSTEM_NAME MATCHES "SYSV5.*")
    96     set(SYSV5 TRUE)
    97   elseif(CMAKE_SYSTEM_NAME MATCHES "Solaris.*")
    98     set(SOLARIS TRUE)
    99   elseif(CMAKE_SYSTEM_NAME MATCHES "HP-UX.*")
   100     set(HPUX TRUE)
   101   elseif(CMAKE_SYSTEM_NAME MATCHES "AIX.*")
   102     set(AIX TRUE)
   103   elseif(CMAKE_SYSTEM_NAME MATCHES "Minix.*")
   104     set(MINIX TRUE)
   105   endif()
   106 elseif(APPLE)
   107   if(CMAKE_SYSTEM_NAME MATCHES ".*Darwin.*")
   108     set(DARWIN TRUE)
   109   elseif(CMAKE_SYSTEM_NAME MATCHES ".*MacOS.*")
   110     set(MACOSX TRUE)
   111   endif()
   112   # TODO: iOS?
   113 elseif(CMAKE_SYSTEM_NAME MATCHES "BeOS.*")
   114   message_error("BeOS support has been removed as of SDL 2.0.2.")
   115 elseif(CMAKE_SYSTEM_NAME MATCHES "Haiku.*")
   116   set(HAIKU TRUE)
   117 endif()
   118 
   119 # Don't mistake osx for unix
   120 if(UNIX AND NOT APPLE)
   121   set(UNIX_SYS ON)
   122 else()
   123   set(UNIX_SYS OFF)
   124 endif()
   125 
   126 if(UNIX OR APPLE)
   127   set(UNIX_OR_MAC_SYS ON)
   128 else()
   129   set(UNIX_OR_MAC_SYS OFF)
   130 endif()
   131 
   132 if (UNIX_OR_MAC_SYS AND NOT EMSCRIPTEN) # JavaScript does not yet have threading support, so disable pthreads when building for Emscripten.
   133   set(SDL_PTHREADS_ENABLED_BY_DEFAULT ON)
   134 else()
   135   set(SDL_PTHREADS_ENABLED_BY_DEFAULT OFF)
   136 endif()
   137 
   138 # Default option knobs
   139 if(APPLE OR ARCH_64)
   140   if(NOT "${CMAKE_OSX_ARCHITECTURES}" MATCHES "arm")
   141     set(OPT_DEF_SSEMATH ON)
   142   endif()
   143 endif()
   144 if(UNIX OR MINGW OR MSYS)
   145   set(OPT_DEF_LIBC ON)
   146 endif()
   147 
   148 # Compiler info
   149 if(CMAKE_COMPILER_IS_GNUCC)
   150   set(USE_GCC TRUE)
   151   set(OPT_DEF_ASM TRUE)
   152 elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
   153   set(USE_CLANG TRUE)
   154   set(OPT_DEF_ASM TRUE)
   155 elseif(MSVC_VERSION GREATER 1400) # VisualStudio 8.0+
   156   set(OPT_DEF_ASM TRUE)
   157   #set(CMAKE_C_FLAGS "/ZI /WX- /
   158 else()
   159   set(OPT_DEF_ASM FALSE)
   160 endif()
   161 
   162 # Default flags, if not set otherwise
   163 if("$ENV{CFLAGS}" STREQUAL "")
   164   if(CMAKE_BUILD_TYPE STREQUAL "")
   165     if(USE_GCC OR USE_CLANG)
   166       set(CMAKE_C_FLAGS "-g -O3")
   167     endif()
   168   endif()
   169 else()
   170   set(CMAKE_C_FLAGS "$ENV{CFLAGS}")
   171   list(APPEND EXTRA_CFLAGS "$ENV{CFLAGS}")
   172 endif()
   173 if(NOT ("$ENV{CFLAGS}" STREQUAL "")) # Hackish, but does the trick on Win32
   174   list(APPEND EXTRA_LDFLAGS "$ENV{LDFLAGS}")
   175 endif()
   176 
   177 if(MSVC)
   178   option(FORCE_STATIC_VCRT "Force /MT for static VC runtimes" OFF)
   179   if(FORCE_STATIC_VCRT)
   180     foreach(flag_var
   181         CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
   182         CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
   183       if(${flag_var} MATCHES "/MD")
   184         string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
   185       endif()
   186     endforeach()
   187   endif()
   188 
   189   # Make sure /RTC1 is disabled, otherwise it will use functions from the CRT
   190   foreach(flag_var
   191       CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
   192       CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
   193     string(REGEX REPLACE "/RTC(su|[1su])" "" ${flag_var} "${${flag_var}}")
   194   endforeach(flag_var)
   195 endif()
   196 
   197 # Those are used for pkg-config and friends, so that the SDL2.pc, sdl2-config,
   198 # etc. are created correctly.
   199 set(SDL_LIBS "-lSDL2")
   200 set(SDL_CFLAGS "")
   201 
   202 # Emscripten toolchain has a nonempty default value for this, and the checks 
   203 # in this file need to change that, so remember the original value, and 
   204 # restore back to that afterwards. For check_function_exists() to work in
   205 # Emscripten, this value must be at its default value.
   206 set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
   207 
   208 if(CYGWIN)
   209   # We build SDL on cygwin without the UNIX emulation layer
   210   include_directories("-I/usr/include/mingw")
   211   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -mno-cygwin")
   212   check_c_source_compiles("int main(int argc, char **argv) {}"
   213     HAVE_GCC_NO_CYGWIN)
   214   set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   215   if(HAVE_GCC_NO_CYGWIN)
   216     list(APPEND EXTRA_LDFLAGS "-mno-cygwin")
   217     list(APPEND SDL_LIBS "-mno-cygwin")
   218   endif()
   219   set(SDL_CFLAGS "${SDL_CFLAGS} -I/usr/include/mingw")
   220 endif()
   221 
   222 add_definitions(-DUSING_GENERATED_CONFIG_H)
   223 # General includes
   224 include_directories(${SDL2_BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)
   225 
   226 # All these ENABLED_BY_DEFAULT vars will default to ON if not specified, so
   227 #  you only need to have a platform override them if they are disabling.
   228 set(OPT_DEF_ASM TRUE)
   229 if(EMSCRIPTEN)
   230   # Set up default values for the currently supported set of subsystems:
   231   # Emscripten/Javascript does not have assembly support, a dynamic library 
   232   # loading architecture, low-level CPU inspection or multithreading.
   233   set(OPT_DEF_ASM FALSE)
   234   set(SDL_SHARED_ENABLED_BY_DEFAULT OFF)
   235   set(SDL_ATOMIC_ENABLED_BY_DEFAULT OFF)
   236   set(SDL_THREADS_ENABLED_BY_DEFAULT OFF)
   237   set(SDL_LOADSO_ENABLED_BY_DEFAULT OFF)
   238   set(SDL_CPUINFO_ENABLED_BY_DEFAULT OFF)
   239   set(SDL_DLOPEN_ENABLED_BY_DEFAULT OFF)
   240 endif()
   241 
   242 if (NOT DEFINED SDL_SHARED_ENABLED_BY_DEFAULT)
   243     set(SDL_SHARED_ENABLED_BY_DEFAULT ON)
   244 endif()
   245 
   246 set(SDL_SUBSYSTEMS
   247     Atomic Audio Video Render Events Joystick Haptic Power Threads Timers
   248     File Loadso CPUinfo Filesystem Dlopen)
   249 foreach(_SUB ${SDL_SUBSYSTEMS})
   250   string(TOUPPER ${_SUB} _OPT)
   251   if (NOT DEFINED SDL_${_OPT}_ENABLED_BY_DEFAULT)
   252     set(SDL_${_OPT}_ENABLED_BY_DEFAULT ON)
   253   endif()
   254   option(SDL_${_OPT} "Enable the ${_SUB} subsystem" ${SDL_${_OPT}_ENABLED_BY_DEFAULT})
   255 endforeach()
   256 
   257 option_string(ASSERTIONS "Enable internal sanity checks (auto/disabled/release/enabled/paranoid)" "auto")
   258 #set_option(DEPENDENCY_TRACKING "Use gcc -MMD -MT dependency tracking" ON)
   259 set_option(LIBC                "Use the system C library" ${OPT_DEF_LIBC})
   260 set_option(GCC_ATOMICS         "Use gcc builtin atomics" ${USE_GCC})
   261 set_option(ASSEMBLY            "Enable assembly routines" ${OPT_DEF_ASM})
   262 set_option(SSEMATH             "Allow GCC to use SSE floating point math" ${OPT_DEF_SSEMATH})
   263 set_option(MMX                 "Use MMX assembly routines" ${OPT_DEF_ASM})
   264 set_option(3DNOW               "Use 3Dnow! MMX assembly routines" ${OPT_DEF_ASM})
   265 set_option(SSE                 "Use SSE assembly routines" ${OPT_DEF_ASM})
   266 set_option(SSE2                "Use SSE2 assembly routines" ${OPT_DEF_SSEMATH})
   267 set_option(SSE3                "Use SSE3 assembly routines" ${OPT_DEF_SSEMATH})
   268 set_option(ALTIVEC             "Use Altivec assembly routines" ${OPT_DEF_ASM})
   269 set_option(DISKAUDIO           "Support the disk writer audio driver" ON)
   270 set_option(DUMMYAUDIO          "Support the dummy audio driver" ON)
   271 set_option(VIDEO_DIRECTFB      "Use DirectFB video driver" OFF)
   272 dep_option(DIRECTFB_SHARED     "Dynamically load directfb support" ON "VIDEO_DIRECTFB" OFF)
   273 set_option(VIDEO_DUMMY         "Use dummy video driver" ON)
   274 set_option(VIDEO_OPENGL        "Include OpenGL support" ON)
   275 set_option(VIDEO_OPENGLES      "Include OpenGL ES support" ON)
   276 set_option(PTHREADS            "Use POSIX threads for multi-threading" ${SDL_PTHREADS_ENABLED_BY_DEFAULT})
   277 dep_option(PTHREADS_SEM        "Use pthread semaphores" ON "PTHREADS" OFF)
   278 set_option(SDL_DLOPEN          "Use dlopen for shared object loading" ${SDL_DLOPEN_ENABLED_BY_DEFAULT})
   279 set_option(OSS                 "Support the OSS audio API" ${UNIX_SYS})
   280 set_option(ALSA                "Support the ALSA audio API" ${UNIX_SYS})
   281 dep_option(ALSA_SHARED         "Dynamically load ALSA audio support" ON "ALSA" OFF)
   282 set_option(JACK                "Support the JACK audio API" ${UNIX_SYS})
   283 dep_option(JACK_SHARED         "Dynamically load JACK audio support" ON "JACK" OFF)
   284 set_option(ESD                 "Support the Enlightened Sound Daemon" ${UNIX_SYS})
   285 dep_option(ESD_SHARED          "Dynamically load ESD audio support" ON "ESD" OFF)
   286 set_option(PULSEAUDIO          "Use PulseAudio" ${UNIX_SYS})
   287 dep_option(PULSEAUDIO_SHARED   "Dynamically load PulseAudio support" ON "PULSEAUDIO" OFF)
   288 set_option(ARTS                "Support the Analog Real Time Synthesizer" ${UNIX_SYS})
   289 dep_option(ARTS_SHARED         "Dynamically load aRts audio support" ON "ARTS" OFF)
   290 set_option(NAS                 "Support the NAS audio API" ${UNIX_SYS})
   291 set_option(NAS_SHARED          "Dynamically load NAS audio API" ${UNIX_SYS})
   292 set_option(SNDIO               "Support the sndio audio API" ${UNIX_SYS})
   293 set_option(FUSIONSOUND         "Use FusionSound audio driver" OFF)
   294 dep_option(FUSIONSOUND_SHARED  "Dynamically load fusionsound audio support" ON "FUSIONSOUND" OFF)
   295 set_option(LIBSAMPLERATE       "Use libsamplerate for audio rate conversion" ${UNIX_SYS})
   296 dep_option(LIBSAMPLERATE_SHARED "Dynamically load libsamplerate" ON "LIBSAMPLERATE" OFF)
   297 set_option(RPATH               "Use an rpath when linking SDL" ${UNIX_SYS})
   298 set_option(CLOCK_GETTIME       "Use clock_gettime() instead of gettimeofday()" OFF)
   299 set_option(INPUT_TSLIB         "Use the Touchscreen library for input" ${UNIX_SYS})
   300 set_option(VIDEO_X11           "Use X11 video driver" ${UNIX_SYS})
   301 set_option(VIDEO_WAYLAND       "Use Wayland video driver" ${UNIX_SYS})
   302 dep_option(WAYLAND_SHARED      "Dynamically load Wayland support" ON "VIDEO_WAYLAND" OFF)
   303 dep_option(VIDEO_WAYLAND_QT_TOUCH  "QtWayland server support for Wayland video driver" ON "VIDEO_WAYLAND" OFF)
   304 set_option(VIDEO_MIR           "Use Mir video driver" ${UNIX_SYS})
   305 dep_option(MIR_SHARED          "Dynamically load Mir support" ON "VIDEO_MIR" OFF)
   306 set_option(VIDEO_RPI           "Use Raspberry Pi video driver" ${UNIX_SYS})
   307 dep_option(X11_SHARED          "Dynamically load X11 support" ON "VIDEO_X11" OFF)
   308 set(SDL_X11_OPTIONS Xcursor Xinerama XInput Xrandr Xscrnsaver XShape Xvm)
   309 foreach(_SUB ${SDL_X11_OPTIONS})
   310   string(TOUPPER "VIDEO_X11_${_SUB}" _OPT)
   311   dep_option(${_OPT}           "Enable ${_SUB} support" ON "VIDEO_X11" OFF)
   312 endforeach()
   313 set_option(VIDEO_COCOA         "Use Cocoa video driver" ${APPLE})
   314 set_option(DIRECTX             "Use DirectX for Windows audio/video" ${WINDOWS})
   315 set_option(RENDER_D3D          "Enable the Direct3D render driver" ${WINDOWS})
   316 set_option(VIDEO_VIVANTE       "Use Vivante EGL video driver" ${UNIX_SYS})
   317 
   318 # TODO: We should (should we?) respect cmake's ${BUILD_SHARED_LIBS} flag here
   319 # The options below are for compatibility to configure's default behaviour.
   320 set(SDL_SHARED ${SDL_SHARED_ENABLED_BY_DEFAULT} CACHE BOOL "Build a shared version of the library")
   321 set(SDL_STATIC ON CACHE BOOL "Build a static version of the library")
   322 
   323 dep_option(SDL_STATIC_PIC      "Static version of the library should be built with Position Independent Code" OFF "SDL_STATIC" OFF)
   324 
   325 # General source files
   326 file(GLOB SOURCE_FILES
   327   ${SDL2_SOURCE_DIR}/src/*.c
   328   ${SDL2_SOURCE_DIR}/src/atomic/*.c
   329   ${SDL2_SOURCE_DIR}/src/audio/*.c
   330   ${SDL2_SOURCE_DIR}/src/cpuinfo/*.c
   331   ${SDL2_SOURCE_DIR}/src/dynapi/*.c
   332   ${SDL2_SOURCE_DIR}/src/events/*.c
   333   ${SDL2_SOURCE_DIR}/src/file/*.c
   334   ${SDL2_SOURCE_DIR}/src/libm/*.c
   335   ${SDL2_SOURCE_DIR}/src/render/*.c
   336   ${SDL2_SOURCE_DIR}/src/render/*/*.c
   337   ${SDL2_SOURCE_DIR}/src/stdlib/*.c
   338   ${SDL2_SOURCE_DIR}/src/thread/*.c
   339   ${SDL2_SOURCE_DIR}/src/timer/*.c
   340   ${SDL2_SOURCE_DIR}/src/video/*.c)
   341 
   342 
   343 if(ASSERTIONS STREQUAL "auto")
   344   # Do nada - use optimization settings to determine the assertion level
   345 elseif(ASSERTIONS STREQUAL "disabled")
   346   set(SDL_DEFAULT_ASSERT_LEVEL 0)
   347 elseif(ASSERTIONS STREQUAL "release")
   348   set(SDL_DEFAULT_ASSERT_LEVEL 1)
   349 elseif(ASSERTIONS STREQUAL "enabled")
   350   set(SDL_DEFAULT_ASSERT_LEVEL 2)
   351 elseif(ASSERTIONS STREQUAL "paranoid")
   352   set(SDL_DEFAULT_ASSERT_LEVEL 3)
   353 else()
   354   message_error("unknown assertion level")
   355 endif()
   356 set(HAVE_ASSERTIONS ${ASSERTIONS})
   357 
   358 # Compiler option evaluation
   359 if(USE_GCC OR USE_CLANG)
   360   # Check for -Wall first, so later things can override pieces of it.
   361   check_c_compiler_flag(-Wall HAVE_GCC_WALL)
   362   if(HAVE_GCC_WALL)
   363     list(APPEND EXTRA_CFLAGS "-Wall")
   364     if(HAIKU)
   365       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-multichar")
   366     endif()
   367   endif()
   368 
   369   check_c_compiler_flag(-Wdeclaration-after-statement HAVE_GCC_WDECLARATION_AFTER_STATEMENT)
   370   if(HAVE_GCC_WDECLARATION_AFTER_STATEMENT)
   371     check_c_compiler_flag(-Werror=declaration-after-statement HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
   372     if(HAVE_GCC_WERROR_DECLARATION_AFTER_STATEMENT)
   373       list(APPEND EXTRA_CFLAGS "-Werror=declaration-after-statement")
   374     endif()
   375     list(APPEND EXTRA_CFLAGS "-Wdeclaration-after-statement")
   376   endif()
   377 
   378   if(DEPENDENCY_TRACKING)
   379     check_c_source_compiles("
   380         #if !defined(__GNUC__) || __GNUC__ < 3
   381         #error Dependency tracking requires GCC 3.0 or newer
   382         #endif
   383         int main(int argc, char **argv) { }" HAVE_DEPENDENCY_TRACKING)
   384   endif()
   385 
   386   if(GCC_ATOMICS)
   387     check_c_source_compiles("int main(int argc, char **argv) {
   388         int a;
   389         void *x, *y, *z;
   390         __sync_lock_test_and_set(&a, 4);
   391         __sync_lock_test_and_set(&x, y);
   392         __sync_fetch_and_add(&a, 1);
   393         __sync_bool_compare_and_swap(&a, 5, 10);
   394         __sync_bool_compare_and_swap(&x, y, z); }" HAVE_GCC_ATOMICS)
   395     if(NOT HAVE_GCC_ATOMICS)
   396       check_c_source_compiles("int main(int argc, char **argv) {
   397           int a;
   398           __sync_lock_test_and_set(&a, 1);
   399           __sync_lock_release(&a); }" HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
   400     endif()
   401   endif()
   402 
   403   set(CMAKE_REQUIRED_FLAGS "-mpreferred-stack-boundary=2")
   404   check_c_source_compiles("int x = 0; int main(int argc, char **argv) {}"
   405     HAVE_GCC_PREFERRED_STACK_BOUNDARY)
   406   set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   407 
   408   set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden -Werror")
   409   check_c_source_compiles("
   410       #if !defined(__GNUC__) || __GNUC__ < 4
   411       #error SDL only uses visibility attributes in GCC 4 or newer
   412       #endif
   413       int main(int argc, char **argv) {}" HAVE_GCC_FVISIBILITY)
   414   if(HAVE_GCC_FVISIBILITY)
   415     list(APPEND EXTRA_CFLAGS "-fvisibility=hidden")
   416   endif()
   417   set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   418 
   419   check_c_compiler_flag(-Wshadow HAVE_GCC_WSHADOW)
   420   if(HAVE_GCC_WSHADOW)
   421     list(APPEND EXTRA_CFLAGS "-Wshadow")
   422   endif()
   423 
   424   set(CMAKE_REQUIRED_FLAGS "-Wl,--no-undefined")
   425   check_c_compiler_flag("" HAVE_NO_UNDEFINED)
   426   set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   427   if(HAVE_NO_UNDEFINED)
   428     list(APPEND EXTRA_LDFLAGS "-Wl,--no-undefined")
   429   endif()
   430 endif()
   431 
   432 if(ASSEMBLY)
   433   if(USE_GCC OR USE_CLANG)
   434     set(SDL_ASSEMBLY_ROUTINES 1)
   435     # TODO: Those all seem to be quite GCC specific - needs to be
   436     # reworked for better compiler support
   437     set(HAVE_ASSEMBLY TRUE)
   438     if(MMX)
   439       set(CMAKE_REQUIRED_FLAGS "-mmmx")
   440       check_c_source_compiles("
   441           #ifdef __MINGW32__
   442           #include <_mingw.h>
   443           #ifdef __MINGW64_VERSION_MAJOR
   444           #include <intrin.h>
   445           #else
   446           #include <mmintrin.h>
   447           #endif
   448           #else
   449           #include <mmintrin.h>
   450           #endif
   451           #ifndef __MMX__
   452           #error Assembler CPP flag not enabled
   453           #endif
   454           int main(int argc, char **argv) { }" HAVE_MMX)
   455       if(HAVE_MMX)
   456         list(APPEND EXTRA_CFLAGS "-mmmx")
   457       endif()
   458       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   459     endif()
   460 
   461     if(3DNOW)
   462       set(CMAKE_REQUIRED_FLAGS "-m3dnow")
   463       check_c_source_compiles("
   464           #include <mm3dnow.h>
   465           #ifndef __3dNOW__
   466           #error Assembler CPP flag not enabled
   467           #endif
   468           int main(int argc, char **argv) {
   469             void *p = 0;
   470             _m_prefetch(p);
   471           }" HAVE_3DNOW)
   472       if(HAVE_3DNOW)
   473         list(APPEND EXTRA_CFLAGS "-m3dnow")
   474       endif()
   475       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   476     endif()
   477 
   478     if(SSE)
   479       set(CMAKE_REQUIRED_FLAGS "-msse")
   480       check_c_source_compiles("
   481           #ifdef __MINGW32__
   482           #include <_mingw.h>
   483           #ifdef __MINGW64_VERSION_MAJOR
   484           #include <intrin.h>
   485           #else
   486           #include <xmmintrin.h>
   487           #endif
   488           #else
   489           #include <xmmintrin.h>
   490           #endif
   491           #ifndef __SSE__
   492           #error Assembler CPP flag not enabled
   493           #endif
   494           int main(int argc, char **argv) { }" HAVE_SSE)
   495       if(HAVE_SSE)
   496         list(APPEND EXTRA_CFLAGS "-msse")
   497       endif()
   498       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   499     endif()
   500 
   501     if(SSE2)
   502       set(CMAKE_REQUIRED_FLAGS "-msse2")
   503       check_c_source_compiles("
   504           #ifdef __MINGW32__
   505           #include <_mingw.h>
   506           #ifdef __MINGW64_VERSION_MAJOR
   507           #include <intrin.h>
   508           #else
   509           #include <emmintrin.h>
   510           #endif
   511           #else
   512           #include <emmintrin.h>
   513           #endif
   514           #ifndef __SSE2__
   515           #error Assembler CPP flag not enabled
   516           #endif
   517           int main(int argc, char **argv) { }" HAVE_SSE2)
   518       if(HAVE_SSE2)
   519         list(APPEND EXTRA_CFLAGS "-msse2")
   520       endif()
   521       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   522     endif()
   523 
   524     if(SSE3)
   525       set(CMAKE_REQUIRED_FLAGS "-msse3")
   526       check_c_source_compiles("
   527           #ifdef __MINGW32__
   528           #include <_mingw.h>
   529           #ifdef __MINGW64_VERSION_MAJOR
   530           #include <intrin.h>
   531           #else
   532           #include <pmmintrin.h>
   533           #endif
   534           #else
   535           #include <pmmintrin.h>
   536           #endif
   537           #ifndef __SSE3__
   538           #error Assembler CPP flag not enabled
   539           #endif
   540           int main(int argc, char **argv) { }" HAVE_SSE3)
   541       if(HAVE_SSE3)
   542         list(APPEND EXTRA_CFLAGS "-msse3")
   543       endif()
   544       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   545     endif()
   546 
   547     if(NOT SSEMATH)
   548       if(SSE OR SSE2 OR SSE3)
   549         if(USE_GCC)
   550           check_c_compiler_flag(-mfpmath=387 HAVE_FP_387)
   551           if(HAVE_FP_387)
   552             list(APPEND EXTRA_CFLAGS "-mfpmath=387")
   553           endif()
   554         endif()
   555         set(HAVE_SSEMATH TRUE)
   556       endif()
   557     endif()
   558 
   559     if(ALTIVEC)
   560       set(CMAKE_REQUIRED_FLAGS "-maltivec")
   561       check_c_source_compiles("
   562           #include <altivec.h>
   563           vector unsigned int vzero() {
   564               return vec_splat_u32(0);
   565           }
   566           int main(int argc, char **argv) { }" HAVE_ALTIVEC_H_HDR)
   567       check_c_source_compiles("
   568           vector unsigned int vzero() {
   569               return vec_splat_u32(0);
   570           }
   571           int main(int argc, char **argv) { }" HAVE_ALTIVEC)
   572       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
   573       if(HAVE_ALTIVEC OR HAVE_ALTIVEC_H_HDR)
   574         set(HAVE_ALTIVEC TRUE) # if only HAVE_ALTIVEC_H_HDR is set
   575         list(APPEND EXTRA_CFLAGS "-maltivec")
   576         set(SDL_ALTIVEC_BLITTERS 1)
   577         if(HAVE_ALTIVEC_H_HDR)
   578           set(HAVE_ALTIVEC_H 1)
   579         endif()
   580       endif()
   581     endif()
   582   elseif(MSVC_VERSION GREATER 1500)
   583     # TODO: SDL_cpuinfo.h needs to support the user's configuration wish
   584     # for MSVC - right now it is always activated
   585     if(NOT ARCH_64)
   586       set(HAVE_MMX TRUE)
   587       set(HAVE_3DNOW TRUE)
   588     endif()
   589     set(HAVE_SSE TRUE)
   590     set(HAVE_SSE2 TRUE)
   591     set(HAVE_SSE3 TRUE)
   592     set(SDL_ASSEMBLY_ROUTINES 1)
   593   endif()
   594 # TODO:
   595 #else()
   596 #  if(USE_GCC OR USE_CLANG)
   597 #    list(APPEND EXTRA_CFLAGS "-mno-sse" "-mno-sse2" "-mno-sse3" "-mno-mmx")
   598 #  endif()
   599 endif()
   600 
   601 # TODO: Can't deactivate on FreeBSD? w/o LIBC, SDL_stdinc.h can't define
   602 # anything.
   603 if(LIBC)
   604   if(WINDOWS AND NOT MINGW)
   605     set(HAVE_LIBC TRUE)
   606     foreach(_HEADER stdio.h string.h ctype.h math.h)
   607       string(TOUPPER "HAVE_${_HEADER}" _UPPER)
   608       string(REPLACE "." "_" _HAVE_H ${_UPPER})
   609       set(${_HAVE_H} 1)
   610     endforeach()
   611     set(HAVE_SIGNAL_H 1)
   612     foreach(_FN
   613             malloc calloc realloc free qsort abs memset memcpy memmove memcmp
   614             strlen _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa
   615             _ultoa strtol strtoul strtoll strtod atoi atof strcmp strncmp
   616             _stricmp _strnicmp sscanf atan atan2 acos asin ceil copysign cos
   617             cosf fabs floor log pow scalbn sin sinf sqrt sqrtf tan tanf)
   618       string(TOUPPER ${_FN} _UPPER)
   619       set(HAVE_${_UPPER} 1)
   620     endforeach()
   621     if(NOT CYGWIN AND NOT MINGW)
   622       set(HAVE_ALLOCA 1)
   623     endif()
   624     set(HAVE_M_PI 1)
   625     add_definitions(-D_USE_MATH_DEFINES) # needed for M_PI
   626     set(STDC_HEADERS 1)
   627   else()
   628     set(HAVE_LIBC TRUE)
   629     check_include_file(sys/types.h HAVE_SYS_TYPES_H)
   630     foreach(_HEADER
   631             stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h
   632             strings.h inttypes.h stdint.h ctype.h math.h iconv.h signal.h)
   633       string(TOUPPER "HAVE_${_HEADER}" _UPPER)
   634       string(REPLACE "." "_" _HAVE_H ${_UPPER})
   635       check_include_file("${_HEADER}" ${_HAVE_H})
   636     endforeach()
   637 
   638     check_include_files("dlfcn.h;stdint.h;stddef.h;inttypes.h;stdlib.h;strings.h;string.h;float.h" STDC_HEADERS)
   639     check_type_size("size_t" SIZEOF_SIZE_T)
   640     check_symbol_exists(M_PI math.h HAVE_M_PI)
   641     # TODO: refine the mprotect check
   642     check_c_source_compiles("#include <sys/types.h>
   643                              #include <sys/mman.h>
   644                              int main() { }" HAVE_MPROTECT)
   645     foreach(_FN
   646             strtod malloc calloc realloc free getenv setenv putenv unsetenv
   647             qsort abs bcopy memset memcpy memmove memcmp strlen strlcpy strlcat
   648             strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa
   649             _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull
   650             atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp
   651             vsscanf vsnprintf fseeko fseeko64 sigaction setjmp
   652             nanosleep sysconf sysctlbyname getauxval
   653             )
   654       string(TOUPPER ${_FN} _UPPER)
   655       set(_HAVEVAR "HAVE_${_UPPER}")
   656       check_function_exists("${_FN}" ${_HAVEVAR})
   657     endforeach()
   658 
   659     check_library_exists(m pow "" HAVE_LIBM)
   660     if(HAVE_LIBM)
   661       set(CMAKE_REQUIRED_LIBRARIES m)
   662       foreach(_FN
   663               atan atan2 ceil copysign cos cosf fabs floor log pow scalbn sin
   664               sinf sqrt sqrtf tan tanf acos asin)
   665         string(TOUPPER ${_FN} _UPPER)
   666         set(_HAVEVAR "HAVE_${_UPPER}")
   667         check_function_exists("${_FN}" ${_HAVEVAR})
   668       endforeach()
   669       set(CMAKE_REQUIRED_LIBRARIES)
   670       list(APPEND EXTRA_LIBS m)
   671     endif()
   672 
   673     check_library_exists(iconv iconv_open "" HAVE_LIBICONV)
   674     if(HAVE_LIBICONV)
   675       list(APPEND EXTRA_LIBS iconv)
   676       set(HAVE_ICONV 1)
   677     endif()
   678 
   679     if(NOT APPLE)
   680       check_include_file(alloca.h HAVE_ALLOCA_H)
   681       check_function_exists(alloca HAVE_ALLOCA)
   682     else()
   683       set(HAVE_ALLOCA_H 1)
   684       set(HAVE_ALLOCA 1)
   685     endif()
   686 
   687     check_struct_has_member("struct sigaction" "sa_sigaction" "signal.h" HAVE_SA_SIGACTION)
   688   endif()
   689 else()
   690   if(WINDOWS)
   691     set(HAVE_STDARG_H 1)
   692     set(HAVE_STDDEF_H 1)
   693   endif()
   694 endif()
   695 
   696 
   697 # Enable/disable various subsystems of the SDL library
   698 foreach(_SUB ${SDL_SUBSYSTEMS})
   699   string(TOUPPER ${_SUB} _OPT)
   700   if(NOT SDL_${_OPT})
   701     set(SDL_${_OPT}_DISABLED 1)
   702   endif()
   703 endforeach()
   704 if(SDL_JOYSTICK)
   705   file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/*.c)
   706   set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
   707 endif()
   708 if(SDL_HAPTIC)
   709   if(NOT SDL_JOYSTICK)
   710     # Haptic requires some private functions from the joystick subsystem.
   711     message_error("SDL_HAPTIC requires SDL_JOYSTICK, which is not enabled")
   712   endif()
   713   file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/*.c)
   714   set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
   715 endif()
   716 if(SDL_POWER)
   717   file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/*.c)
   718   set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
   719 endif()
   720 # TODO: in configure.in, the test for LOADSO and SDL_DLOPEN is a bit weird:
   721 # if LOADSO is not wanted, SDL_LOADSO_DISABLED is set
   722 # If however on Unix or APPLE dlopen() is detected via CheckDLOPEN(),
   723 # SDL_LOADSO_DISABLED will not be set, regardless of the LOADSO settings
   724 
   725 # General SDL subsystem options, valid for all platforms
   726 if(SDL_AUDIO)
   727   # CheckDummyAudio/CheckDiskAudio - valid for all platforms
   728   if(DUMMYAUDIO)
   729     set(SDL_AUDIO_DRIVER_DUMMY 1)
   730     file(GLOB DUMMYAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/dummy/*.c)
   731     set(SOURCE_FILES ${SOURCE_FILES} ${DUMMYAUDIO_SOURCES})
   732     set(HAVE_DUMMYAUDIO TRUE)
   733   endif()
   734   if(DISKAUDIO)
   735     set(SDL_AUDIO_DRIVER_DISK 1)
   736     file(GLOB DISKAUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/disk/*.c)
   737     set(SOURCE_FILES ${SOURCE_FILES} ${DISKAUDIO_SOURCES})
   738     set(HAVE_DISKAUDIO TRUE)
   739   endif()
   740 endif()
   741 
   742 if(SDL_DLOPEN)
   743   # Relevant for Unix/Darwin only
   744   if(UNIX OR APPLE)
   745     CheckDLOPEN()
   746   endif()
   747 endif()
   748 
   749 if(SDL_VIDEO)
   750   if(VIDEO_DUMMY)
   751     set(SDL_VIDEO_DRIVER_DUMMY 1)
   752     file(GLOB VIDEO_DUMMY_SOURCES ${SDL2_SOURCE_DIR}/src/video/dummy/*.c)
   753     set(SOURCE_FILES ${SOURCE_FILES} ${VIDEO_DUMMY_SOURCES})
   754     set(HAVE_VIDEO_DUMMY TRUE)
   755     set(HAVE_SDL_VIDEO TRUE)
   756   endif()
   757 endif()
   758 
   759 if(ANDROID)
   760   file(GLOB ANDROID_CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/android/*.c)
   761   set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_CORE_SOURCES})
   762 
   763   # SDL_spinlock.c Needs to be compiled in ARM mode.
   764   # There seems to be no better way currently to set the ARM mode.
   765   # see: https://issuetracker.google.com/issues/62264618
   766   # Another option would be to set ARM mode to all compiled files
   767   check_c_compiler_flag(-marm HAVE_ARM_MODE)
   768   if(HAVE_ARM_MODE)
   769     set_source_files_properties(${SDL2_SOURCE_DIR}/src/atomic/SDL_spinlock.c PROPERTIES COMPILE_FLAGS -marm)
   770   endif()
   771 
   772   file(GLOB ANDROID_MAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/android/*.c)
   773   set(SDLMAIN_SOURCES ${SDLMAIN_SOURCES} ${ANDROID_MAIN_SOURCES})
   774 
   775   if(SDL_AUDIO)
   776     set(SDL_AUDIO_DRIVER_ANDROID 1)
   777     file(GLOB ANDROID_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/android/*.c)
   778     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_AUDIO_SOURCES})
   779     set(HAVE_SDL_AUDIO TRUE)
   780   endif()
   781   if(SDL_FILESYSTEM)
   782     set(SDL_FILESYSTEM_ANDROID 1)
   783     file(GLOB ANDROID_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/android/*.c)
   784     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_FILESYSTEM_SOURCES})
   785     set(HAVE_SDL_FILESYSTEM TRUE)
   786   endif()
   787   if(SDL_JOYSTICK)
   788     set(SDL_JOYSTICK_ANDROID 1)
   789     file(GLOB ANDROID_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/android/*.c)
   790     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_JOYSTICK_SOURCES})
   791     set(HAVE_SDL_JOYSTICK TRUE)
   792   endif()
   793   if(SDL_POWER)
   794     set(SDL_POWER_ANDROID 1)
   795     file(GLOB ANDROID_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/android/*.c)
   796     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_POWER_SOURCES})
   797     set(HAVE_SDL_POWER TRUE)
   798   endif()
   799   if(SDL_VIDEO)
   800     set(SDL_VIDEO_DRIVER_ANDROID 1)
   801     file(GLOB ANDROID_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/android/*.c)
   802     set(SOURCE_FILES ${SOURCE_FILES} ${ANDROID_VIDEO_SOURCES})
   803     set(HAVE_SDL_VIDEO TRUE)
   804 
   805     # Core stuff
   806     find_library(ANDROID_DL_LIBRARY dl)
   807     find_library(ANDROID_LOG_LIBRARY log)
   808     find_library(ANDROID_LIBRARY_LIBRARY android)
   809     list(APPEND EXTRA_LIBS ${ANDROID_DL_LIBRARY} ${ANDROID_LOG_LIBRARY} ${ANDROID_LIBRARY_LIBRARY})
   810     add_definitions(-DGL_GLEXT_PROTOTYPES)
   811 
   812     #enable gles
   813     if(VIDEO_OPENGLES)
   814       set(SDL_VIDEO_OPENGL_EGL 1)
   815       set(HAVE_VIDEO_OPENGLES TRUE)
   816       set(SDL_VIDEO_OPENGL_ES2 1)
   817       set(SDL_VIDEO_RENDER_OGL_ES2 1)
   818 
   819       find_library(OpenGLES1_LIBRARY GLESv1_CM)
   820       find_library(OpenGLES2_LIBRARY GLESv2)
   821       list(APPEND EXTRA_LIBS ${OpenGLES1_LIBRARY} ${OpenGLES2_LIBRARY})
   822     endif()
   823   endif()
   824   list(APPEND EXTRA_LDFLAGS "-Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit")
   825 endif()
   826 
   827 # Platform-specific options and settings
   828 if(EMSCRIPTEN)
   829   # Hide noisy warnings that intend to aid mostly during initial stages of porting a new
   830   # project. Uncomment at will for verbose cross-compiling -I/../ path info.
   831   add_definitions(-Wno-warn-absolute-paths)
   832   if(SDL_AUDIO)
   833     set(SDL_AUDIO_DRIVER_EMSCRIPTEN 1)
   834     file(GLOB EM_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/emscripten/*.c)
   835     set(SOURCE_FILES ${SOURCE_FILES} ${EM_AUDIO_SOURCES})
   836     set(HAVE_SDL_AUDIO TRUE)
   837   endif()
   838   if(SDL_FILESYSTEM)
   839     set(SDL_FILESYSTEM_EMSCRIPTEN 1)
   840     file(GLOB EM_FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/emscripten/*.c)
   841     set(SOURCE_FILES ${SOURCE_FILES} ${EM_FILESYSTEM_SOURCES})
   842     set(HAVE_SDL_FILESYSTEM TRUE)
   843   endif()
   844   if(SDL_JOYSTICK)
   845     set(SDL_JOYSTICK_EMSCRIPTEN 1)
   846     file(GLOB EM_JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/emscripten/*.c)
   847     set(SOURCE_FILES ${SOURCE_FILES} ${EM_JOYSTICK_SOURCES})
   848     set(HAVE_SDL_JOYSTICK TRUE)
   849   endif()
   850   if(SDL_POWER)
   851     set(SDL_POWER_EMSCRIPTEN 1)
   852     file(GLOB EM_POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/emscripten/*.c)
   853     set(SOURCE_FILES ${SOURCE_FILES} ${EM_POWER_SOURCES})
   854     set(HAVE_SDL_POWER TRUE)
   855   endif()
   856   if(SDL_TIMERS)
   857     set(SDL_TIMER_UNIX 1)
   858     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
   859     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
   860     set(HAVE_SDL_TIMERS TRUE)
   861 
   862     if(CLOCK_GETTIME)
   863       set(HAVE_CLOCK_GETTIME 1)
   864     endif()
   865   endif()
   866   if(SDL_VIDEO)
   867     set(SDL_VIDEO_DRIVER_EMSCRIPTEN 1)
   868     file(GLOB EM_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/emscripten/*.c)
   869     set(SOURCE_FILES ${SOURCE_FILES} ${EM_VIDEO_SOURCES})
   870     set(HAVE_SDL_VIDEO TRUE)
   871 
   872     #enable gles
   873     if(VIDEO_OPENGLES)
   874       set(SDL_VIDEO_OPENGL_EGL 1)
   875       set(HAVE_VIDEO_OPENGLES TRUE)
   876       set(SDL_VIDEO_OPENGL_ES2 1)
   877       set(SDL_VIDEO_RENDER_OGL_ES2 1)
   878     endif()
   879   endif()
   880 elseif(UNIX AND NOT APPLE AND NOT ANDROID)
   881   if(SDL_AUDIO)
   882     if(SYSV5 OR SOLARIS OR HPUX)
   883         set(SDL_AUDIO_DRIVER_SUNAUDIO 1)
   884         file(GLOB SUN_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/sun/*.c)
   885         set(SOURCE_FILES ${SOURCE_FILES} ${SUN_AUDIO_SOURCES})
   886         set(HAVE_SDL_AUDIO TRUE)
   887     elseif(NETBSD)
   888         set(SDL_AUDIO_DRIVER_NETBSD 1)
   889         file(GLOB NETBSD_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/netbsd/*.c)
   890         set(SOURCE_FILES ${SOURCE_FILES} ${NETBSD_AUDIO_SOURCES})
   891         set(HAVE_SDL_AUDIO TRUE)
   892     elseif(AIX)
   893         set(SDL_AUDIO_DRIVER_PAUDIO 1)
   894         file(GLOB AIX_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/paudio/*.c)
   895         set(SOURCE_FILES ${SOURCE_FILES} ${AIX_AUDIO_SOURCES})
   896         set(HAVE_SDL_AUDIO TRUE)
   897     endif()
   898     CheckOSS()
   899     CheckALSA()
   900     CheckJACK()
   901     CheckPulseAudio()
   902     CheckESD()
   903     CheckARTS()
   904     CheckNAS()
   905     CheckSNDIO()
   906     CheckFusionSound()
   907     CheckLibSampleRate()
   908   endif()
   909 
   910   if(SDL_VIDEO)
   911     # Need to check for Raspberry PI first and add platform specific compiler flags, otherwise the test for GLES fails!
   912     CheckRPI()
   913     CheckX11()
   914     CheckMir()
   915     CheckDirectFB()
   916     CheckOpenGLX11()
   917     CheckOpenGLESX11()
   918     CheckWayland()
   919     CheckVivante()
   920   endif()
   921 
   922   if(LINUX)
   923     check_c_source_compiles("
   924         #include <linux/input.h>
   925         #ifndef EVIOCGNAME
   926         #error EVIOCGNAME() ioctl not available
   927         #endif
   928         int main(int argc, char** argv) {}" HAVE_INPUT_EVENTS)
   929 
   930     check_c_source_compiles("
   931         #include <linux/kd.h>
   932         #include <linux/keyboard.h>
   933 
   934         int main(int argc, char **argv) 
   935         {
   936             struct kbentry kbe;
   937             kbe.kb_table = KG_CTRL;
   938             ioctl(0, KDGKBENT, &kbe);
   939         }" HAVE_INPUT_KD)
   940 
   941     file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/linux/*.c)
   942     set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
   943 
   944     if(HAVE_INPUT_EVENTS)
   945       set(SDL_INPUT_LINUXEV 1)
   946     endif()
   947 
   948     if(SDL_HAPTIC AND HAVE_INPUT_EVENTS)
   949       set(SDL_HAPTIC_LINUX 1)
   950       file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/linux/*.c)
   951       set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
   952       set(HAVE_SDL_HAPTIC TRUE)
   953     endif()
   954 
   955     if(HAVE_INPUT_KD)
   956       set(SDL_INPUT_LINUXKD 1)
   957     endif()
   958 
   959     check_include_file("libudev.h" HAVE_LIBUDEV_H)
   960 
   961     if(PKG_CONFIG_FOUND)
   962       pkg_search_module(DBUS dbus-1 dbus)
   963       if(DBUS_FOUND)
   964         set(HAVE_DBUS_DBUS_H TRUE)
   965         include_directories(${DBUS_INCLUDE_DIRS})
   966         list(APPEND EXTRA_LIBS ${DBUS_LIBRARIES})
   967       endif()
   968 
   969       pkg_search_module(IBUS ibus-1.0 ibus)
   970       if(IBUS_FOUND)
   971         set(HAVE_IBUS_IBUS_H TRUE)
   972         include_directories(${IBUS_INCLUDE_DIRS})
   973         list(APPEND EXTRA_LIBS ${IBUS_LIBRARIES})
   974       endif()
   975     endif()
   976 
   977     check_include_file("fcitx/frontend.h" HAVE_FCITX_FRONTEND_H)
   978 
   979   endif()
   980 
   981   if(INPUT_TSLIB)
   982     check_c_source_compiles("
   983         #include \"tslib.h\"
   984         int main(int argc, char** argv) { }" HAVE_INPUT_TSLIB)
   985     if(HAVE_INPUT_TSLIB)
   986       set(SDL_INPUT_TSLIB 1)
   987       list(APPEND EXTRA_LIBS ts)
   988     endif()
   989   endif()
   990 
   991   if(SDL_JOYSTICK)
   992     CheckUSBHID()   # seems to be BSD specific - limit the test to BSD only?
   993     if(LINUX AND NOT ANDROID)
   994       set(SDL_JOYSTICK_LINUX 1)
   995       file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/linux/*.c)
   996       set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
   997       set(HAVE_SDL_JOYSTICK TRUE)
   998     endif()
   999   endif()
  1000 
  1001   CheckPTHREAD()
  1002 
  1003   if(CLOCK_GETTIME)
  1004     check_library_exists(rt clock_gettime "" FOUND_CLOCK_GETTIME)
  1005     if(FOUND_CLOCK_GETTIME)
  1006       list(APPEND EXTRA_LIBS rt)
  1007       set(HAVE_CLOCK_GETTIME 1)
  1008     else()
  1009       check_library_exists(c clock_gettime "" FOUND_CLOCK_GETTIME)
  1010       if(FOUND_CLOCK_GETTIME)
  1011         set(HAVE_CLOCK_GETTIME 1)
  1012       endif()
  1013     endif()
  1014   endif()
  1015 
  1016   check_include_file(linux/version.h HAVE_LINUX_VERSION_H)
  1017   if(HAVE_LINUX_VERSION_H)
  1018     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LINUX_VERSION_H")
  1019   endif()
  1020 
  1021   if(SDL_POWER)
  1022     if(LINUX)
  1023       set(SDL_POWER_LINUX 1)
  1024       file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/linux/*.c)
  1025       set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
  1026       set(HAVE_SDL_POWER TRUE)
  1027     endif()
  1028   endif()
  1029 
  1030   if(SDL_FILESYSTEM)
  1031     set(SDL_FILESYSTEM_UNIX 1)
  1032     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/unix/*.c)
  1033     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1034     set(HAVE_SDL_FILESYSTEM TRUE)
  1035   endif()
  1036 
  1037   if(SDL_TIMERS)
  1038     set(SDL_TIMER_UNIX 1)
  1039     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
  1040     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1041     set(HAVE_SDL_TIMERS TRUE)
  1042   endif()
  1043 
  1044   if(RPATH)
  1045     set(SDL_RLD_FLAGS "")
  1046     if(BSDI OR FREEBSD OR LINUX OR NETBSD)
  1047       set(CMAKE_REQUIRED_FLAGS "-Wl,--enable-new-dtags")
  1048       check_c_compiler_flag("" HAVE_ENABLE_NEW_DTAGS)
  1049       set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
  1050       if(HAVE_ENABLE_NEW_DTAGS)
  1051         set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir} -Wl,--enable-new-dtags")
  1052       else()
  1053         set(SDL_RLD_FLAGS "-Wl,-rpath,\${libdir}")
  1054       endif()
  1055     elseif(SOLARIS)
  1056       set(SDL_RLD_FLAGS "-R\${libdir}")
  1057     endif()
  1058     set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
  1059     set(HAVE_RPATH TRUE)
  1060   endif()
  1061 
  1062 elseif(WINDOWS)
  1063   find_program(WINDRES windres)
  1064 
  1065   check_c_source_compiles("
  1066     #include <windows.h>
  1067     int main(int argc, char **argv) { }" HAVE_WIN32_CC)
  1068 
  1069   file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/windows/*.c)
  1070   set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
  1071 
  1072   if(MSVC)
  1073     # Prevent codegen that would use the VC runtime libraries.
  1074     add_definitions(/GS-)
  1075     if(NOT ARCH_64)
  1076       add_definitions(/arch:SSE)
  1077     endif()
  1078   endif()
  1079 
  1080   # Check for DirectX
  1081   if(DIRECTX)
  1082     if(DEFINED MSVC_VERSION AND NOT ${MSVC_VERSION} LESS 1700)
  1083         set(USE_WINSDK_DIRECTX TRUE)
  1084     endif()
  1085     if(NOT CMAKE_COMPILER_IS_MINGW AND NOT USE_WINSDK_DIRECTX)
  1086       if("$ENV{DXSDK_DIR}" STREQUAL "")
  1087         message_error("DIRECTX requires the \$DXSDK_DIR environment variable to be set")
  1088       endif()
  1089       set(CMAKE_REQUIRED_FLAGS "/I\"$ENV{DXSDK_DIR}\\Include\"")
  1090     endif()
  1091 
  1092     if(HAVE_WIN32_CC)
  1093       # xinput.h may need windows.h, but doesn't include it itself.
  1094       check_c_source_compiles("
  1095         #include <windows.h>
  1096         #include <xinput.h>
  1097         int main(int argc, char **argv) { }" HAVE_XINPUT_H)
  1098     else()
  1099       check_include_file(xinput.h HAVE_XINPUT_H)
  1100     endif()
  1101 
  1102     check_include_file(d3d9.h HAVE_D3D_H)
  1103     check_include_file(d3d11_1.h HAVE_D3D11_H)
  1104     check_include_file(ddraw.h HAVE_DDRAW_H)
  1105     check_include_file(dsound.h HAVE_DSOUND_H)
  1106     check_include_file(dinput.h HAVE_DINPUT_H)
  1107     check_include_file(xaudio2.h HAVE_XAUDIO2_H)
  1108     check_include_file(audioclient.h HAVE_AUDIOCLIENT_H)
  1109     check_include_file(dxgi.h HAVE_DXGI_H)
  1110     if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H OR HAVE_XAUDIO2_H)
  1111       set(HAVE_DIRECTX TRUE)
  1112       if(NOT CMAKE_COMPILER_IS_MINGW AND NOT USE_WINSDK_DIRECTX)
  1113       # TODO: change $ENV{DXSDL_DIR} to get the path from the include checks
  1114         link_directories($ENV{DXSDK_DIR}\\lib\\${PROCESSOR_ARCH})
  1115         include_directories($ENV{DXSDK_DIR}\\Include)
  1116       endif()
  1117     endif()
  1118     set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
  1119   endif()
  1120 
  1121   if(SDL_AUDIO)
  1122     set(SDL_AUDIO_DRIVER_WINMM 1)
  1123     file(GLOB WINMM_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/winmm/*.c)
  1124     set(SOURCE_FILES ${SOURCE_FILES} ${WINMM_AUDIO_SOURCES})
  1125     set(HAVE_SDL_AUDIO TRUE)
  1126 
  1127     if(HAVE_DSOUND_H)
  1128       set(SDL_AUDIO_DRIVER_DSOUND 1)
  1129       file(GLOB DSOUND_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/directsound/*.c)
  1130       set(SOURCE_FILES ${SOURCE_FILES} ${DSOUND_AUDIO_SOURCES})
  1131     endif()
  1132 
  1133     if(HAVE_XAUDIO2_H)
  1134       set(SDL_AUDIO_DRIVER_XAUDIO2 1)
  1135       file(GLOB XAUDIO2_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/xaudio2/*.c)
  1136       set(SOURCE_FILES ${SOURCE_FILES} ${XAUDIO2_AUDIO_SOURCES})
  1137     endif()
  1138 
  1139     if(HAVE_AUDIOCLIENT_H)
  1140       set(SDL_AUDIO_DRIVER_WASAPI 1)
  1141       file(GLOB WASAPI_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/wasapi/*.c)
  1142       set(SOURCE_FILES ${SOURCE_FILES} ${WASAPI_AUDIO_SOURCES})
  1143     endif()
  1144   endif()
  1145 
  1146   if(SDL_VIDEO)
  1147     # requires SDL_LOADSO on Windows (IME, DX, etc.)
  1148     if(NOT SDL_LOADSO)
  1149       message_error("SDL_VIDEO requires SDL_LOADSO, which is not enabled")
  1150     endif()
  1151     set(SDL_VIDEO_DRIVER_WINDOWS 1)
  1152     file(GLOB WIN_VIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/windows/*.c)
  1153     set(SOURCE_FILES ${SOURCE_FILES} ${WIN_VIDEO_SOURCES})
  1154 
  1155     if(RENDER_D3D AND HAVE_D3D_H)
  1156       set(SDL_VIDEO_RENDER_D3D 1)
  1157       set(HAVE_RENDER_D3D TRUE)
  1158     endif()
  1159     if(RENDER_D3D AND HAVE_D3D11_H)
  1160       set(SDL_VIDEO_RENDER_D3D11 1)
  1161       set(HAVE_RENDER_D3D TRUE)
  1162     endif()
  1163     set(HAVE_SDL_VIDEO TRUE)
  1164   endif()
  1165 
  1166   if(SDL_THREADS)
  1167     set(SDL_THREAD_WINDOWS 1)
  1168     set(SOURCE_FILES ${SOURCE_FILES}
  1169       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_sysmutex.c
  1170       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_syssem.c
  1171       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_systhread.c
  1172       ${SDL2_SOURCE_DIR}/src/thread/windows/SDL_systls.c
  1173       ${SDL2_SOURCE_DIR}/src/thread/generic/SDL_syscond.c)
  1174     set(HAVE_SDL_THREADS TRUE)
  1175   endif()
  1176 
  1177   if(SDL_POWER)
  1178     set(SDL_POWER_WINDOWS 1)
  1179     set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/power/windows/SDL_syspower.c)
  1180     set(HAVE_SDL_POWER TRUE)
  1181   endif()
  1182 
  1183   if(SDL_FILESYSTEM)
  1184     set(SDL_FILESYSTEM_WINDOWS 1)
  1185     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/windows/*.c)
  1186     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1187     set(HAVE_SDL_FILESYSTEM TRUE)
  1188   endif()
  1189 
  1190   # Libraries for Win32 native and MinGW
  1191   list(APPEND EXTRA_LIBS user32 gdi32 winmm imm32 ole32 oleaut32 version uuid)
  1192 
  1193   # TODO: in configure.in the check for timers is set on
  1194   # cygwin | mingw32* - does this include mingw32CE?
  1195   if(SDL_TIMERS)
  1196     set(SDL_TIMER_WINDOWS 1)
  1197     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/windows/*.c)
  1198     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1199     set(HAVE_SDL_TIMERS TRUE)
  1200   endif()
  1201 
  1202   if(SDL_LOADSO)
  1203     set(SDL_LOADSO_WINDOWS 1)
  1204     file(GLOB LOADSO_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/windows/*.c)
  1205     set(SOURCE_FILES ${SOURCE_FILES} ${LOADSO_SOURCES})
  1206     set(HAVE_SDL_LOADSO TRUE)
  1207   endif()
  1208 
  1209   file(GLOB CORE_SOURCES ${SDL2_SOURCE_DIR}/src/core/windows/*.c)
  1210   set(SOURCE_FILES ${SOURCE_FILES} ${CORE_SOURCES})
  1211 
  1212   if(SDL_VIDEO)
  1213     if(VIDEO_OPENGL)
  1214       set(SDL_VIDEO_OPENGL 1)
  1215       set(SDL_VIDEO_OPENGL_WGL 1)
  1216       set(SDL_VIDEO_RENDER_OGL 1)
  1217       set(HAVE_VIDEO_OPENGL TRUE)
  1218     endif()
  1219 
  1220     if(VIDEO_OPENGLES)
  1221       set(SDL_VIDEO_OPENGL_EGL 1)
  1222       set(SDL_VIDEO_OPENGL_ES2 1)
  1223       set(SDL_VIDEO_RENDER_OGL_ES2 1)
  1224       set(HAVE_VIDEO_OPENGLES TRUE)
  1225     endif()
  1226   endif()
  1227 
  1228   if(SDL_JOYSTICK)
  1229     file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/windows/*.c)
  1230     set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
  1231     if(HAVE_DINPUT_H)
  1232       set(SDL_JOYSTICK_DINPUT 1)
  1233       list(APPEND EXTRA_LIBS dinput8)
  1234       if(CMAKE_COMPILER_IS_MINGW)
  1235         list(APPEND EXTRA_LIBS dxerr8)
  1236       elseif (NOT USE_WINSDK_DIRECTX)
  1237         list(APPEND EXTRA_LIBS dxerr)
  1238       endif()
  1239     endif()
  1240     if(HAVE_XINPUT_H)
  1241       set(SDL_JOYSTICK_XINPUT 1)
  1242     endif()
  1243     if(NOT HAVE_DINPUT_H AND NOT HAVE_XINPUT_H)
  1244       set(SDL_JOYSTICK_WINMM 1)
  1245     endif()
  1246     set(HAVE_SDL_JOYSTICK TRUE)
  1247 
  1248     if(SDL_HAPTIC)
  1249       if(HAVE_DINPUT_H OR HAVE_XINPUT_H)
  1250         file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/windows/*.c)
  1251         if(HAVE_DINPUT_H)
  1252           set(SDL_HAPTIC_DINPUT 1)
  1253         endif()
  1254         if(HAVE_XINPUT_H)
  1255           set(SDL_HAPTIC_XINPUT 1)
  1256         endif()
  1257       else()
  1258         file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
  1259         set(SDL_HAPTIC_DUMMY 1)
  1260       endif()
  1261       set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
  1262       set(HAVE_SDL_HAPTIC TRUE)
  1263     endif()
  1264   endif()
  1265 
  1266   file(GLOB VERSION_SOURCES ${SDL2_SOURCE_DIR}/src/main/windows/*.rc)
  1267   file(GLOB SDLMAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/windows/*.c)
  1268   if(MINGW OR CYGWIN)
  1269     list(APPEND EXTRA_LIBS mingw32)
  1270     list(APPEND EXTRA_LDFLAGS "-mwindows")
  1271     set(SDL_CFLAGS "${SDL_CFLAGS} -Dmain=SDL_main")
  1272     list(APPEND SDL_LIBS "-lmingw32" "-lSDL2main" "-mwindows")
  1273   endif()
  1274 elseif(APPLE)
  1275   # TODO: rework this for proper MacOS X, iOS and Darwin support
  1276 
  1277   # Requires the darwin file implementation
  1278   if(SDL_FILE)
  1279     file(GLOB EXTRA_SOURCES ${SDL2_SOURCE_DIR}/src/file/cocoa/*.m)
  1280     set(SOURCE_FILES ${EXTRA_SOURCES} ${SOURCE_FILES})
  1281     set_source_files_properties(${EXTRA_SOURCES} PROPERTIES LANGUAGE C)
  1282     set(HAVE_SDL_FILE TRUE)
  1283     set(SDL_FRAMEWORK_COCOA 1)
  1284     set(SDL_FRAMEWORK_COREVIDEO 1)
  1285   else()
  1286     message_error("SDL_FILE must be enabled to build on MacOS X")
  1287   endif()
  1288 
  1289   if(SDL_AUDIO)
  1290     set(SDL_AUDIO_DRIVER_COREAUDIO 1)
  1291     file(GLOB AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/coreaudio/*.m)
  1292     set(SOURCE_FILES ${SOURCE_FILES} ${AUDIO_SOURCES})
  1293     set(HAVE_SDL_AUDIO TRUE)
  1294     set(SDL_FRAMEWORK_COREAUDIO 1)
  1295     set(SDL_FRAMEWORK_AUDIOTOOLBOX 1)
  1296   endif()
  1297 
  1298   if(SDL_JOYSTICK)
  1299     set(SDL_JOYSTICK_IOKIT 1)
  1300     if (IOS)
  1301       file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/iphoneos/*.m)
  1302     else()
  1303       file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/darwin/*.c)
  1304     endif()
  1305     set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
  1306     set(HAVE_SDL_JOYSTICK TRUE)
  1307     set(SDL_FRAMEWORK_IOKIT 1)
  1308     set(SDL_FRAMEWORK_FF 1)
  1309   endif()
  1310 
  1311   if(SDL_HAPTIC)
  1312     set(SDL_HAPTIC_IOKIT 1)
  1313     if (IOS)
  1314       file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
  1315       set(SDL_HAPTIC_DUMMY 1)
  1316     else()
  1317       file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/darwin/*.c)
  1318     endif()
  1319     set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
  1320     set(HAVE_SDL_HAPTIC TRUE)
  1321     set(SDL_FRAMEWORK_IOKIT 1)
  1322     set(SDL_FRAMEWORK_FF 1)
  1323     if(NOT SDL_JOYSTICK)
  1324       message(FATAL_ERROR "SDL_HAPTIC requires SDL_JOYSTICK to be enabled")
  1325     endif()
  1326   endif()
  1327 
  1328   if(SDL_POWER)
  1329     set(SDL_POWER_MACOSX 1)
  1330     if (IOS)
  1331       file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/uikit/*.m)
  1332     else()
  1333       file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/macosx/*.c)
  1334     endif()
  1335     set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES})
  1336     set(HAVE_SDL_POWER TRUE)
  1337     set(SDL_FRAMEWORK_CARBON 1)
  1338     set(SDL_FRAMEWORK_IOKIT 1)
  1339   endif()
  1340 
  1341   if(SDL_TIMERS)
  1342     set(SDL_TIMER_UNIX 1)
  1343     file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/unix/*.c)
  1344     set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1345     set(HAVE_SDL_TIMERS TRUE)
  1346   endif(SDL_TIMERS)
  1347 
  1348   if(SDL_FILESYSTEM)
  1349     set(SDL_FILESYSTEM_COCOA 1)
  1350     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/cocoa/*.m)
  1351     set_source_files_properties(${FILESYSTEM_SOURCES} PROPERTIES LANGUAGE C)
  1352     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1353     set(HAVE_SDL_FILESYSTEM TRUE)
  1354   endif()
  1355 
  1356   # Actually load the frameworks at the end so we don't duplicate include.
  1357   if(SDL_FRAMEWORK_COREVIDEO)
  1358     find_library(COREVIDEO CoreVideo)
  1359     list(APPEND EXTRA_LIBS ${COREVIDEO})
  1360   endif()
  1361   if(SDL_FRAMEWORK_COCOA)
  1362     find_library(COCOA_LIBRARY Cocoa)
  1363     list(APPEND EXTRA_LIBS ${COCOA_LIBRARY})
  1364   endif()
  1365   if(SDL_FRAMEWORK_IOKIT)
  1366     find_library(IOKIT IOKit)
  1367     list(APPEND EXTRA_LIBS ${IOKIT})
  1368   endif()
  1369   if(SDL_FRAMEWORK_FF)
  1370     find_library(FORCEFEEDBACK ForceFeedback)
  1371     list(APPEND EXTRA_LIBS ${FORCEFEEDBACK})
  1372   endif()
  1373   if(SDL_FRAMEWORK_CARBON)
  1374     find_library(CARBON_LIBRARY Carbon)
  1375     list(APPEND EXTRA_LIBS ${CARBON_LIBRARY})
  1376   endif()
  1377   if(SDL_FRAMEWORK_COREAUDIO)
  1378     find_library(COREAUDIO CoreAudio)
  1379     list(APPEND EXTRA_LIBS ${COREAUDIO})
  1380   endif()
  1381   if(SDL_FRAMEWORK_AUDIOTOOLBOX)
  1382     find_library(AUDIOTOOLBOX AudioToolbox)
  1383     list(APPEND EXTRA_LIBS ${AUDIOTOOLBOX})
  1384   endif()
  1385 
  1386   # iOS hack needed - http://code.google.com/p/ios-cmake/ ?
  1387   if(SDL_VIDEO)
  1388     if (IOS)
  1389       set(SDL_VIDEO_DRIVER_UIKIT 1)
  1390       file(GLOB UIKITVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/uikit/*.m)
  1391       set(SOURCE_FILES ${SOURCE_FILES} ${UIKITVIDEO_SOURCES})
  1392     else()
  1393       CheckCOCOA()
  1394       if(VIDEO_OPENGL)
  1395         set(SDL_VIDEO_OPENGL 1)
  1396         set(SDL_VIDEO_OPENGL_CGL 1)
  1397         set(SDL_VIDEO_RENDER_OGL 1)
  1398         set(HAVE_VIDEO_OPENGL TRUE)
  1399       endif()
  1400     endif()
  1401   endif()
  1402 
  1403   CheckPTHREAD()
  1404 elseif(HAIKU)
  1405   if(SDL_VIDEO)
  1406     set(SDL_VIDEO_DRIVER_HAIKU 1)
  1407     file(GLOB HAIKUVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/haiku/*.c)
  1408     set(SOURCE_FILES ${SOURCE_FILES} ${HAIKUVIDEO_SOURCES})
  1409     set(HAVE_SDL_VIDEO TRUE)
  1410 
  1411     set(SDL_FILESYSTEM_HAIKU 1)
  1412     file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/haiku/*.cc)
  1413     set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1414     set(HAVE_SDL_FILESYSTEM TRUE)
  1415 
  1416     if(SDL_TIMERS)
  1417       set(SDL_TIMER_HAIKU 1)
  1418       file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/haiku/*.c)
  1419       set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1420       set(HAVE_SDL_TIMERS TRUE)
  1421     endif(SDL_TIMERS)
  1422 
  1423     if(VIDEO_OPENGL)
  1424       # TODO: Use FIND_PACKAGE(OpenGL) instead
  1425       set(SDL_VIDEO_OPENGL 1)
  1426       set(SDL_VIDEO_OPENGL_BGL 1)
  1427       set(SDL_VIDEO_RENDER_OGL 1)
  1428       list(APPEND EXTRA_LIBS GL)
  1429       set(HAVE_VIDEO_OPENGL TRUE)
  1430     endif()
  1431   endif()
  1432 
  1433   CheckPTHREAD()
  1434 endif()
  1435 
  1436 # Dummies
  1437 # configure.in does it differently:
  1438 # if not have X
  1439 #   if enable_X {  SDL_X_DISABLED = 1 }
  1440 #   [add dummy sources]
  1441 # so it always adds a dummy, without checking, if it was actually requested.
  1442 # This leads to missing internal references on building, since the
  1443 # src/X/*.c does not get included.
  1444 if(NOT HAVE_SDL_JOYSTICK)
  1445   set(SDL_JOYSTICK_DISABLED 1)
  1446   if(SDL_JOYSTICK AND NOT APPLE) # results in unresolved symbols on OSX
  1447 
  1448     file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/dummy/*.c)
  1449     set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES})
  1450   endif()
  1451 endif()
  1452 if(NOT HAVE_SDL_HAPTIC)
  1453   set(SDL_HAPTIC_DISABLED 1)
  1454   file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c)
  1455   set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES})
  1456 endif()
  1457 if(NOT HAVE_SDL_LOADSO)
  1458   set(SDL_LOADSO_DISABLED 1)
  1459   file(GLOB LOADSO_SOURCES ${SDL2_SOURCE_DIR}/src/loadso/dummy/*.c)
  1460   set(SOURCE_FILES ${SOURCE_FILES} ${LOADSO_SOURCES})
  1461 endif()
  1462 if(NOT HAVE_SDL_FILESYSTEM)
  1463   set(SDL_FILESYSTEM_DISABLED 1)
  1464   file(GLOB FILESYSTEM_SOURCES ${SDL2_SOURCE_DIR}/src/filesystem/dummy/*.c)
  1465   set(SOURCE_FILES ${SOURCE_FILES} ${FILESYSTEM_SOURCES})
  1466 endif()
  1467 
  1468 # We always need to have threads and timers around
  1469 if(NOT HAVE_SDL_THREADS)
  1470   set(SDL_THREADS_DISABLED 1)
  1471   file(GLOB THREADS_SOURCES ${SDL2_SOURCE_DIR}/src/thread/generic/*.c)
  1472   set(SOURCE_FILES ${SOURCE_FILES} ${THREADS_SOURCES})
  1473 endif()
  1474 if(NOT HAVE_SDL_TIMERS)
  1475   set(SDL_TIMERS_DISABLED 1)
  1476   file(GLOB TIMER_SOURCES ${SDL2_SOURCE_DIR}/src/timer/dummy/*.c)
  1477   set(SOURCE_FILES ${SOURCE_FILES} ${TIMER_SOURCES})
  1478 endif()
  1479 
  1480 if(NOT SDLMAIN_SOURCES)
  1481   file(GLOB SDLMAIN_SOURCES ${SDL2_SOURCE_DIR}/src/main/dummy/*.c)
  1482 endif()
  1483 
  1484 # Append the -MMD -MT flags
  1485 # if(DEPENDENCY_TRACKING)
  1486 #   if(COMPILER_IS_GNUCC)
  1487 #     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -MMD -MT \$@")
  1488 #   endif()
  1489 # endif()
  1490 
  1491 configure_file("${SDL2_SOURCE_DIR}/include/SDL_config.h.cmake"
  1492   "${SDL2_BINARY_DIR}/include/SDL_config.h")
  1493 
  1494 # Prepare the flags and remove duplicates
  1495 if(EXTRA_LDFLAGS)
  1496   list(REMOVE_DUPLICATES EXTRA_LDFLAGS)
  1497 endif()
  1498 if(EXTRA_LIBS)
  1499   list(REMOVE_DUPLICATES EXTRA_LIBS)
  1500 endif()
  1501 if(EXTRA_CFLAGS)
  1502   list(REMOVE_DUPLICATES EXTRA_CFLAGS)
  1503 endif()
  1504 listtostr(EXTRA_CFLAGS _EXTRA_CFLAGS)
  1505 set(EXTRA_CFLAGS ${_EXTRA_CFLAGS})
  1506 
  1507 # Compat helpers for the configuration files
  1508 if(NOT WINDOWS OR CYGWIN)
  1509   # TODO: we need a Windows script, too
  1510   execute_process(COMMAND sh ${SDL2_SOURCE_DIR}/build-scripts/updaterev.sh)
  1511 
  1512   set(prefix ${CMAKE_INSTALL_PREFIX})
  1513   set(exec_prefix "\${prefix}")
  1514   set(libdir "\${exec_prefix}/lib${LIB_SUFFIX}")
  1515   set(bindir "\${exec_prefix}/bin")
  1516   set(includedir "\${prefix}/include")
  1517   if(SDL_STATIC)
  1518     set(ENABLE_STATIC_TRUE "")
  1519     set(ENABLE_STATIC_FALSE "#")
  1520   else()
  1521     set(ENABLE_STATIC_TRUE "#")
  1522     set(ENABLE_STATIC_FALSE "")
  1523   endif()
  1524   if(SDL_SHARED)
  1525     set(ENABLE_SHARED_TRUE "")
  1526     set(ENABLE_SHARED_FALSE "#")
  1527   else()
  1528     set(ENABLE_SHARED_TRUE "#")
  1529     set(ENABLE_SHARED_FALSE "")
  1530   endif()
  1531 
  1532   # Clean up the different lists
  1533   listtostr(EXTRA_LIBS _EXTRA_LIBS "-l")
  1534   set(SDL_STATIC_LIBS ${SDL_LIBS} ${EXTRA_LDFLAGS} ${_EXTRA_LIBS})
  1535   list(REMOVE_DUPLICATES SDL_STATIC_LIBS)
  1536   listtostr(SDL_STATIC_LIBS _SDL_STATIC_LIBS)
  1537   set(SDL_STATIC_LIBS ${_SDL_STATIC_LIBS})
  1538   listtostr(SDL_LIBS _SDL_LIBS)
  1539   set(SDL_LIBS ${_SDL_LIBS})
  1540 
  1541   # MESSAGE(STATUS "SDL_LIBS: ${SDL_LIBS}")
  1542   # MESSAGE(STATUS "SDL_STATIC_LIBS: ${SDL_STATIC_LIBS}")
  1543 
  1544   configure_file("${SDL2_SOURCE_DIR}/sdl2.pc.in"
  1545     "${SDL2_BINARY_DIR}/sdl2.pc" @ONLY)
  1546   configure_file("${SDL2_SOURCE_DIR}/sdl2-config.in"
  1547     "${SDL2_BINARY_DIR}/sdl2-config")
  1548   configure_file("${SDL2_SOURCE_DIR}/sdl2-config.in"
  1549     "${SDL2_BINARY_DIR}/sdl2-config" @ONLY)
  1550   configure_file("${SDL2_SOURCE_DIR}/SDL2.spec.in"
  1551     "${SDL2_BINARY_DIR}/SDL2.spec" @ONLY)
  1552 endif()
  1553 
  1554 ##### Info output #####
  1555 message(STATUS "")
  1556 message(STATUS "SDL2 was configured with the following options:")
  1557 message(STATUS "")
  1558 message(STATUS "Platform: ${CMAKE_SYSTEM}")
  1559 message(STATUS "64-bit:   ${ARCH_64}")
  1560 message(STATUS "Compiler: ${CMAKE_C_COMPILER}")
  1561 message(STATUS "")
  1562 message(STATUS "Subsystems:")
  1563 foreach(_SUB ${SDL_SUBSYSTEMS})
  1564   string(TOUPPER ${_SUB} _OPT)
  1565   message_bool_option(${_SUB} SDL_${_OPT})
  1566 endforeach()
  1567 message(STATUS "")
  1568 message(STATUS "Options:")
  1569 list(SORT ALLOPTIONS)
  1570 foreach(_OPT ${ALLOPTIONS})
  1571   # Longest option is VIDEO_X11_XSCREENSAVER = 22 characters
  1572   # Get the padding
  1573   string(LENGTH ${_OPT} _OPTLEN)
  1574   math(EXPR _PADLEN "23 - ${_OPTLEN}")
  1575   string(RANDOM LENGTH ${_PADLEN} ALPHABET " " _PADDING)
  1576   message_tested_option(${_OPT} ${_PADDING})
  1577 endforeach()
  1578 message(STATUS "")
  1579 message(STATUS " CFLAGS:        ${CMAKE_C_FLAGS}")
  1580 message(STATUS " EXTRA_CFLAGS:  ${EXTRA_CFLAGS}")
  1581 message(STATUS " EXTRA_LDFLAGS: ${EXTRA_LDFLAGS}")
  1582 message(STATUS " EXTRA_LIBS:    ${EXTRA_LIBS}")
  1583 message(STATUS "")
  1584 message(STATUS " Build Shared Library: ${SDL_SHARED}")
  1585 message(STATUS " Build Static Library: ${SDL_STATIC}")
  1586 if(SDL_STATIC)
  1587     message(STATUS " Build Static Library with Position Independent Code: ${SDL_STATIC_PIC}")
  1588 endif()
  1589 message(STATUS "")
  1590 if(UNIX)
  1591   message(STATUS "If something was not detected, although the libraries")
  1592   message(STATUS "were installed, then make sure you have set the")
  1593   message(STATUS "CFLAGS and LDFLAGS environment variables correctly.")
  1594   message(STATUS "")
  1595 endif()
  1596 
  1597 # Ensure that the extra cflags are used at compile time
  1598 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
  1599 
  1600 # Always build SDLmain
  1601 add_library(SDL2main STATIC ${SDLMAIN_SOURCES})
  1602 set(_INSTALL_LIBS "SDL2main")
  1603 
  1604 if(SDL_SHARED)
  1605   add_library(SDL2 SHARED ${SOURCE_FILES} ${VERSION_SOURCES})
  1606   if(UNIX AND NOT ANDROID)
  1607     set_target_properties(SDL2 PROPERTIES
  1608       VERSION ${LT_VERSION}
  1609       SOVERSION ${LT_REVISION}
  1610       OUTPUT_NAME "SDL2-${LT_RELEASE}")
  1611   else()
  1612     set_target_properties(SDL2 PROPERTIES
  1613       VERSION ${SDL_VERSION}
  1614       SOVERSION ${LT_REVISION}
  1615       OUTPUT_NAME "SDL2")
  1616   endif()
  1617   if(MSVC AND NOT LIBC)
  1618     # Don't try to link with the default set of libraries.
  1619     set_target_properties(SDL2 PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
  1620     set_target_properties(SDL2 PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
  1621     set_target_properties(SDL2 PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
  1622   endif()
  1623   set(_INSTALL_LIBS "SDL2" ${_INSTALL_LIBS})
  1624   target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
  1625 endif()
  1626 
  1627 if(SDL_STATIC)
  1628   set (BUILD_SHARED_LIBS FALSE)
  1629   add_library(SDL2-static STATIC ${SOURCE_FILES})
  1630   set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
  1631   set_target_properties(SDL2-static PROPERTIES POSITION_INDEPENDENT_CODE ${SDL_STATIC_PIC})
  1632   if(MSVC AND NOT LIBC)
  1633     set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
  1634     set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB")
  1635     set_target_properties(SDL2-static PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB")
  1636   endif()
  1637   # TODO: Win32 platforms keep the same suffix .lib for import and static
  1638   # libraries - do we need to consider this?
  1639   set(_INSTALL_LIBS "SDL2-static" ${_INSTALL_LIBS})
  1640   target_link_libraries(SDL2-static ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
  1641 endif()
  1642 
  1643 ##### Installation targets #####
  1644 install(TARGETS ${_INSTALL_LIBS}
  1645   LIBRARY DESTINATION "lib${LIB_SUFFIX}"
  1646   ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
  1647   RUNTIME DESTINATION bin)
  1648 
  1649 file(GLOB INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/*.h)
  1650 file(GLOB BIN_INCLUDE_FILES ${SDL2_BINARY_DIR}/include/*.h)
  1651 foreach(_FNAME ${BIN_INCLUDE_FILES})
  1652   get_filename_component(_INCNAME ${_FNAME} NAME)
  1653   list(REMOVE_ITEM INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/${_INCNAME})
  1654 endforeach()
  1655 list(APPEND INCLUDE_FILES ${BIN_INCLUDE_FILES})
  1656 install(FILES ${INCLUDE_FILES} DESTINATION include/SDL2)
  1657 
  1658 if(NOT (WINDOWS OR CYGWIN))
  1659   if(SDL_SHARED)
  1660     if (APPLE)
  1661         set(SOEXT "dylib")
  1662     else()
  1663         set(SOEXT "so")
  1664     endif()
  1665     install(CODE "
  1666       execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
  1667       \"libSDL2-2.0.${SOEXT}\" \"libSDL2.${SOEXT}\")")
  1668     install(FILES ${SDL2_BINARY_DIR}/libSDL2.${SOEXT} DESTINATION "lib${LIB_SUFFIX}")
  1669   endif()
  1670   if(FREEBSD)
  1671     # FreeBSD uses ${PREFIX}/libdata/pkgconfig
  1672     install(FILES ${SDL2_BINARY_DIR}/sdl2.pc DESTINATION "libdata/pkgconfig")
  1673   else()
  1674     install(FILES ${SDL2_BINARY_DIR}/sdl2.pc
  1675       DESTINATION "lib${LIB_SUFFIX}/pkgconfig")
  1676   endif()
  1677   install(PROGRAMS ${SDL2_BINARY_DIR}/sdl2-config DESTINATION bin)
  1678   # TODO: what about the .spec file? Is it only needed for RPM creation?
  1679   install(FILES "${SDL2_SOURCE_DIR}/sdl2.m4" DESTINATION "${CMAKE_INSTALL_FULL_DATAROOTDIR}/aclocal")
  1680 endif()
  1681 
  1682 ##### Uninstall target #####
  1683 
  1684 configure_file(
  1685     "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
  1686     "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
  1687     IMMEDIATE @ONLY)
  1688 
  1689 add_custom_target(uninstall
  1690     COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)