Warning, /graphics/glaxnimate/cmake/modules/FindSDL2.cmake is written in an unsupported language. File is not indexed.

0001 # Copyright 2016-2020 Mattia Basaglia
0002 #
0003 # This program is free software: you can redistribute it and/or modify
0004 # it under the terms of the GNU General Public License as published by
0005 # the Free Software Foundation, either version 3 of the License, or
0006 # (at your option) any later version.
0007 #
0008 # This program is distributed in the hope that it will be useful,
0009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0011 # GNU General Public License for more details.
0012 #
0013 # You should have received a copy of the GNU General Public License
0014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
0015 #
0016 # =====
0017 # Finds the SDL2 library and various components
0018 #
0019 # Defines the following variable
0020 # SDL2_FOUND        Whether the ibrary has been found
0021 # SDL2_LIBRARIES    A list of linker options
0022 # SDL2_INCLUDE_DIRS A list of paths to include
0023 # SDL2_VERSION      Version string in the form "major.minor.patch"
0024 #
0025 # It supports finding SDL2 components, a component is a library that follows the
0026 # following pattern:
0027 # Header in SDL2/SDL_(name).h
0028 # Library as libSDL2_(name)
0029 #
0030 # Some example of these components are SDL_image and SDL_ttf
0031 # (which would be called "image" and "ttf" respectively)
0032 #
0033 # The component "main" links to SDLMain.
0034 
0035 # Like find_path() buth tailored to work with SDL2 include paths
0036 function(sdl2_find_include_path output_var header_name)
0037     find_path(${output_var}_tmp ${header_name}
0038         HINTS
0039         ENV SDL2DIR
0040         PATH_SUFFIXES include/SDL2 include
0041         PATHS
0042         ~/Library/Frameworks
0043         /Library/Frameworks
0044         /usr/local/include/SDL2
0045         /usr/include/SDL2
0046         /sw # Fink
0047         /opt/local # DarwinPorts
0048         /opt/csw # Blastwave
0049         /opt
0050     )
0051     set(${output_var} ${${output_var}_tmp} PARENT_SCOPE)
0052 endfunction()
0053 
0054 # Like find_library() but tailored to work with SDL2 libraries
0055 function(sdl2_find_library output_var lib_name)
0056     find_library(${output_var}_tmp
0057         NAMES ${lib_name}
0058         HINTS
0059         ENV SDL2DIR
0060         PATH_SUFFIXES lib64 lib
0061         PATHS
0062         /sw
0063         /opt/local
0064         /opt/csw
0065         /opt
0066     )
0067     set(${output_var} ${${output_var}_tmp} PARENT_SCOPE)
0068 endfunction()
0069 
0070 # Helper function to parse version macros from a header
0071 # Arguments:
0072 #   header_path:    Full path to the header file to parse
0073 #   macro_prefix:   Prefix of the _MAJOR_VERSION macro and friends
0074 #   output_var:     Variable to store the version insto
0075 function(sdl2_parse_version header_path macro_prefix output_var)
0076     if(EXISTS ${header_path})
0077         file(STRINGS ${header_path} SDL2_COMP_VERSION_MAJOR_LINE REGEX "^#define[ \t]+${macro_prefix}_MAJOR_VERSION[ \t]+[0-9]+$")
0078         file(STRINGS ${header_path} SDL2_COMP_VERSION_MINOR_LINE REGEX "^#define[ \t]+${macro_prefix}_MINOR_VERSION[ \t]+[0-9]+$")
0079         file(STRINGS ${header_path} SDL2_COMP_VERSION_PATCH_LINE REGEX "^#define[ \t]+${macro_prefix}_PATCHLEVEL[ \t]+[0-9]+$")
0080         string(REGEX REPLACE "^#define[ \t]+${macro_prefix}_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_COMP_VERSION_MAJOR "${SDL2_COMP_VERSION_MAJOR_LINE}")
0081         string(REGEX REPLACE "^#define[ \t]+${macro_prefix}_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_COMP_VERSION_MINOR "${SDL2_COMP_VERSION_MINOR_LINE}")
0082         string(REGEX REPLACE "^#define[ \t]+${macro_prefix}_PATCHLEVEL[ \t]+([0-9]+)$"    "\\1" SDL2_COMP_VERSION_PATCH "${SDL2_COMP_VERSION_PATCH_LINE}")
0083         set(${output_var} ${SDL2_COMP_VERSION_MAJOR}.${SDL2_COMP_VERSION_MINOR}.${SDL2_COMP_VERSION_PATCH} PARENT_SCOPE)
0084     endif()
0085 endfunction()
0086 
0087 # Helper function to find SDL2 components
0088 # Outputs the given variables (all uppercase):
0089 #   SDL2_(ID)_FOUND         (Boolean, whether the component has been found)
0090 #   SDL2_(ID)_VERSION       (String, the detected version as "major.minor.patch")
0091 #   SDL2_(ID)_INCLUDE_DIRS  (Cached, List of paths to include)
0092 #   SDL2_(ID)_LIBRARIES     (Cached, List of libraries to include)
0093 function(sdl2_find_component SDL2_COMPONENT_ID)
0094     set(SDL2_VARPREFIX "SDL2_${SDL2_COMPONENT_ID}")
0095     set(SDL2_COMPONENT_HEADER "SDL_${SDL2_COMPONENT_ID}.h")
0096 
0097     sdl2_find_include_path(${SDL2_VARPREFIX}_INCLUDE_DIR ${SDL2_COMPONENT_HEADER})
0098     sdl2_find_library(${SDL2_VARPREFIX}_LIBRARY "SDL2_${SDL2_COMPONENT_ID}")
0099 
0100     set(SDL2_COMPONENT_HEADER_PATH "${${SDL2_VARPREFIX}_INCLUDE_DIR}/${SDL2_COMPONENT_HEADER}")
0101 
0102     if(${SDL2_VARPREFIX}_INCLUDE_DIR)
0103         string(TOUPPER "SDL_${SDL2_COMPONENT_ID}" SDL2_COMPONENT_MACRO)
0104         sdl2_parse_version(${SDL2_COMPONENT_HEADER_PATH} ${SDL2_COMPONENT_MACRO} "${SDL2_VARPREFIX}_VERSION")
0105         set("${SDL2_VARPREFIX}_VERSION" ${${SDL2_VARPREFIX}_VERSION} PARENT_SCOPE)
0106     endif()
0107 
0108     if(${SDL2_VARPREFIX}_LIBRARY AND ${SDL2_VARPREFIX}_INCLUDE_DIR)
0109         set("${SDL2_VARPREFIX}_LIBRARIES" ${${SDL2_VARPREFIX}_LIBRARY} PARENT_SCOPE)
0110         set("${SDL2_VARPREFIX}_INCLUDE_DIRS" ${${SDL2_VARPREFIX}_INCLUDE_DIR} PARENT_SCOPE)
0111         set("${SDL2_VARPREFIX}_FOUND" ON PARENT_SCOPE)
0112     else()
0113         set("${SDL2_VARPREFIX}_FOUND" OFF PARENT_SCOPE)
0114     endif()
0115 endfunction()
0116 
0117 # Find the library proper
0118 set(SDL2_INCLUDE_DIRS "")
0119 sdl2_find_include_path(SDL2_INCLUDE_DIR SDL.h)
0120 list(APPEND SDL2_INCLUDE_DIRS ${SDL2_INCLUDE_DIR})
0121 sdl2_parse_version("${SDL2_INCLUDE_DIR}/SDL_version.h" "SDL" SDL2_VERSION)
0122 
0123 set(SDL2_LIBRARIES "")
0124 sdl2_find_library(SDL2_LIBRARY SDL2)
0125 
0126 # Link extra libraries
0127 if(SDL2_LIBRARY)
0128     list(APPEND SDL2_LIBRARIES ${SDL2_LIBRARY})
0129 
0130     # On OS X, SDL needs to link against the Cocoa framework,
0131     # on other systems, it needs to link against the threading library
0132     if(NOT APPLE)
0133         find_package(Threads)
0134         list(APPEND SDL2_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
0135     else()
0136         list(APPEND SDL2_LIBRARIES "-framework Cocoa")
0137     endif()
0138 
0139     # MinGW needs a few more libraries
0140     if(MINGW)
0141         list(APPEND SDL2_LIBRARIES "mingw32" "mwindows")
0142     endif()
0143 
0144 endif()
0145 
0146 # Search for required and optional components
0147 if(SDL2_FIND_COMPONENTS)
0148     message(STATUS "Searching SDL components:")
0149 
0150     foreach(component ${SDL2_FIND_COMPONENTS})
0151         # main is a bit special, it doesn't have a header and doesn't follow
0152         # the library naming convention
0153         # SDL2_main_FOUND is for find_package_handle_standard_args
0154         if(${component} STREQUAL "main")
0155             sdl2_find_library(SDL2MAIN_LIBRARY SDL2main)
0156             if(SDL2_LIBRARIES)
0157                 list(APPEND SDL2_LIBRARIES ${SDL2MAIN_LIBRARY})
0158                 set(SDL2_main_FOUND ON)
0159             else()
0160                 set(SDL2_main_FOUND OFF)
0161             endif()
0162             unset(SDL2MAIN_LIBRARY)
0163         # Other components use a helper function
0164         else()
0165             sdl2_find_component(${component})
0166             if(SDL2_${component}_FOUND)
0167                 message(STATUS "  ${component} found ${SDL2_${component}_VERSION}")
0168                 list(APPEND SDL2_LIBRARIES ${SDL2_${component}_LIBRARIES})
0169                 list(APPEND SDL2_INCLUDE_DIRS ${SDL2_${component}_INCLUDE_DIR})
0170             else()
0171                 message(STATUS "  ${component} not found")
0172             endif()
0173         endif()
0174     endforeach()
0175 endif()
0176 
0177 
0178 # Handle common functionality for Find* scripts
0179 include(FindPackageHandleStandardArgs)
0180 find_package_handle_standard_args(
0181     SDL2
0182     REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
0183     VERSION_VAR SDL2_VERSION
0184     HANDLE_COMPONENTS
0185 )
0186 
0187 # Set up cached variables
0188 if(SDL2_FOUND)
0189     set(SDL2_LIBRARIES ${SDL2_LIBRARIES} CACHE STRING "Linker flags for SDL2")
0190     list(REMOVE_DUPLICATES SDL2_INCLUDE_DIRS)
0191     set(SDL2_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS} CACHE STRING "Search paths for SDL2")
0192 endif()
0193 
0194 mark_as_advanced(SDL2_LIBRARIES SDL2_INCLUDE_DIRS)
0195 
0196 # Clean temporaries
0197 unset(SDL2_INCLUDE_DIR)
0198 unset(SDL2_LIBRARY)