From 3dd87e70507f8c2832c47ed399386bee5eb06366 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Tue, 30 Sep 2014 23:13:24 -0400 Subject: [PATCH] handle SDL2 in a more cross-platform way --- CMakeLists.txt | 26 ++++---- cmake/FindSDL2.cmake | 148 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 15 deletions(-) create mode 100644 cmake/FindSDL2.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f6a270..57846c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,16 @@ project(freespace2) -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") -include(CheckCXXCompilerFlag) -include(CheckCXXSourceCompiles) -include(CheckTypeSize) -include(FindPkgConfig) -pkg_search_module(SDL2 REQUIRED sdl2 >= 2.0.1) +find_package(SDL2 "2.0.1" REQUIRED) +find_package(OpenGL REQUIRED) +find_package(OpenAL REQUIRED) -find_package(OpenGL) -find_package(OpenAL) find_package(wxWidgets COMPONENTS core base gl) - include(${wxWidgets_USE_FILE}) @@ -60,7 +56,7 @@ endif() include_directories( ${PROJECT_SOURCE_DIR}/include - ${SDL2_INCLUDE_DIRS} + ${SDL2_INCLUDE_DIR} ${OPENAL_INCLUDE_DIR} ${wxWidgets_INCLUDE_DIRS} ) @@ -109,7 +105,7 @@ add_executable(${FS_BINARY} target_link_libraries( ${FS_BINARY} code - ${SDL2_LIBRARIES} + ${SDL2_LIBRARY} ${OPENGL_LIBRARIES} ${OPENAL_LIBRARY} ) @@ -135,7 +131,7 @@ add_executable(ac target_link_libraries( ac code - ${SDL2_LIBRARIES} + ${SDL2_LIBRARY} ${OPENGL_LIBRARIES} ${OPENAL_LIBRARY} ) @@ -170,7 +166,7 @@ add_executable(nebedit target_link_libraries( nebedit code - ${SDL2_LIBRARIES} + ${SDL2_LIBRARY} ${OPENGL_LIBRARIES} ${OPENAL_LIBRARY} ${wxWidgets_LIBRARIES} @@ -189,7 +185,7 @@ add_executable(pofview target_link_libraries( pofview code - ${SDL2_LIBRARIES} + ${SDL2_LIBRARY} ${OPENGL_LIBRARIES} ${OPENAL_LIBRARY} ${wxWidgets_LIBRARIES} @@ -230,7 +226,7 @@ add_custom_command(TARGET fonttool target_link_libraries( fonttool code - ${SDL2_LIBRARIES} + ${SDL2_LIBRARY} ${OPENGL_LIBRARIES} ${OPENAL_LIBRARY} ) diff --git a/cmake/FindSDL2.cmake b/cmake/FindSDL2.cmake new file mode 100644 index 0000000..6559715 --- /dev/null +++ b/cmake/FindSDL2.cmake @@ -0,0 +1,148 @@ +# - Locate SDL2 library (modified from CMake's FindSDL.cmake) +# This module defines +# SDL2_LIBRARY, the name of the library to link against +# SDL2_FOUND, if false, do not try to link to SDL +# SDL2_INCLUDE_DIR, where to find SDL.h +# SDL2_VERSION_STRING, human-readable string containing the version of SDL2 +# +# This module responds to the the flag: +# SDL2_BUILDING_LIBRARY +# If this is defined, then no SDL_main will be linked in because +# only applications need main(). +# Otherwise, it is assumed you are building an application and this +# module will attempt to locate and set the the proper link flags +# as part of the returned SDL2_LIBRARY variable. +# +# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration +# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library +# (SDL2.dll, libsdl2.so, SDL2.framework, etc). +# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again. +# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value +# as appropriate. These values are used to generate the final SDL2_LIBRARY +# variable, but when these values are unset, SDL2_LIBRARY does not get created. +# +# +# $SDL2DIR is an environment variable that would +# correspond to the ./configure --prefix=$SDL2DIR +# used in building SDL. +# + +#============================================================================= +# Copyright 2014 Justin Jacobs +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= + +find_path(SDL2_INCLUDE_DIR SDL.h + HINTS + ENV SDL2DIR + PATH_SUFFIXES include/SDL2 include +) + +find_library(SDL2_LIBRARY_TEMP + NAMES SDL2 + HINTS + ENV SDL2DIR + PATH_SUFFIXES lib +) + +if(NOT SDL2_BUILDING_LIBRARY) + if(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") + # Non-OS X framework versions expect you to also dynamically link to + # SDLmain. This is mainly for Windows and OS X. Other (Unix) platforms + # seem to provide SDLmain for compatibility even though they don't + # necessarily need it. + find_library(SDL2MAIN_LIBRARY + NAMES SDL2main + HINTS + ENV SDL2DIR + PATH_SUFFIXES lib + PATHS + /sw + /opt/local + /opt/csw + /opt + ) + endif() +endif() + +# SDL2 may require threads on your system. +# The Apple build may not need an explicit flag because one of the +# frameworks may already provide it. +# But for non-OSX systems, I will use the CMake Threads package. +if(NOT APPLE) + find_package(Threads) +endif() + +# MinGW needs an additional library, mwindows +# It's total link flags should look like -lmingw32 -lSDLmain -lSDL -lmwindows +# (Actually on second look, I think it only needs one of the m* libraries.) +if(MINGW) + set(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW") +endif() + +if(SDL2_LIBRARY_TEMP) + # For SDLmain + if(SDL2MAIN_LIBRARY AND NOT SDL2_BUILDING_LIBRARY) + list(FIND SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" _SDL2_MAIN_INDEX) + if(_SDL2_MAIN_INDEX EQUAL -1) + set(SDL2_LIBRARY_TEMP "${SDL2MAIN_LIBRARY}" ${SDL2_LIBRARY_TEMP}) + endif() + unset(_SDL2_MAIN_INDEX) + endif() + + # For OS X, SDL uses Cocoa as a backend so it must link to Cocoa. + # CMake doesn't display the -framework Cocoa string in the UI even + # though it actually is there if I modify a pre-used variable. + # I think it has something to do with the CACHE STRING. + # So I use a temporary variable until the end so I can set the + # "real" variable in one-shot. + if(APPLE) + set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa") + endif() + + # For threads, as mentioned Apple doesn't need this. + # In fact, there seems to be a problem if I used the Threads package + # and try using this line, so I'm just skipping it entirely for OS X. + if(NOT APPLE) + set(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT}) + endif() + + # For MinGW library + if(MINGW) + set(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP}) + endif() + + # Set the final string here so the GUI reflects the final state. + set(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found") + # Set the temp variable to INTERNAL so it is not seen in the CMake GUI + set(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "") +endif() + +if(SDL2_INCLUDE_DIR AND EXISTS "${SDL2_INCLUDE_DIR}/SDL_version.h") + file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$") + file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$") + file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$") + string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MAJOR "${SDL2_VERSION_MAJOR_LINE}") + string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MINOR "${SDL2_VERSION_MINOR_LINE}") + string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_PATCH "${SDL2_VERSION_PATCH_LINE}") + set(SDL2_VERSION_STRING ${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH}) + unset(SDL2_VERSION_MAJOR_LINE) + unset(SDL2_VERSION_MINOR_LINE) + unset(SDL2_VERSION_PATCH_LINE) + unset(SDL2_VERSION_MAJOR) + unset(SDL2_VERSION_MINOR) + unset(SDL2_VERSION_PATCH) +endif() + +include(FindPackageHandleStandardArgs) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 + REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR + VERSION_VAR SDL2_VERSION_STRING) + -- 2.39.2