macro(add_to_alloptions _NEWNAME)
  list(APPEND ALLOPTIONS ${_NEWNAME})
endmacro()

macro(set_option _NAME _DESC)
  add_to_alloptions(${_NAME})
  if(${ARGC} EQUAL 3)
    set(_DEFLT ${ARGV2})
  else()
    set(_DEFLT OFF)
  endif()
  option(${_NAME} ${_DESC} ${_DEFLT})
endmacro()

macro(dep_option _NAME _DESC _DEFLT _DEPTEST _FAILDFLT)
  add_to_alloptions("${_NAME}")
  cmake_dependent_option("${_NAME}" "${_DESC}" "${_DEFLT}" "${_DEPTEST}" "${_FAILDFLT}")
endmacro()

macro(option_string _NAME _DESC _VALUE)
  add_to_alloptions(${_NAME})
  set(${_NAME} ${_VALUE} CACHE STRING "${_DESC}")
  set(HAVE_${_NAME} ${_VALUE})
ENDMACRO()

macro(message_bool_option _NAME _VALUE)
  set(_PAD "\t")
  if(${ARGC} EQUAL 3)
    set(_PAD ${ARGV2})
  endif()
  if(${_VALUE})
    message(STATUS "  ${_NAME}:${_PAD}ON")
  else()
    message(STATUS "  ${_NAME}:${_PAD}OFF")
  endif()
endmacro()

macro(message_tested_option _NAME)
  set(_REQVALUE ${${_NAME}})
  set(_PAD " ")
  if(${ARGC} EQUAL 2)
    set(_PAD ${ARGV1})
  endif()
  string(SUBSTRING "${_NAME}" 0 4 _NAMESTART)
  if(_NAMESTART STREQUAL "SDL_")
    string(SUBSTRING "${_NAME}" 4 -1 _STRIPPEDNAME)
  else()
    set(_STRIPPEDNAME "${_NAME}")
  endif()
  if(NOT HAVE_${_STRIPPEDNAME})
    set(HAVE_${_STRIPPEDNAME} OFF)
  elseif("${HAVE_${_STRIPPEDNAME}}" MATCHES "1|TRUE|YES|Y")
    set(HAVE_${_STRIPPEDNAME} ON)
  endif()
  message(STATUS "  ${_NAME}${_PAD}(Wanted: ${_REQVALUE}): ${HAVE_${_STRIPPEDNAME}}")
endmacro()

function(find_stringlength_longest_item inList outLength)
  set(maxLength 0)
  foreach(item IN LISTS ${inList})
    string(LENGTH "${item}" slen)
    if(slen GREATER maxLength)
      set(maxLength ${slen})
    endif()
  endforeach()
  set("${outLength}" ${maxLength} PARENT_SCOPE)
endfunction()

function(message_dictlist inList)
  find_stringlength_longest_item(${inList} maxLength)
  foreach(name IN LISTS ${inList})
    # Get the padding
    string(LENGTH ${name} nameLength)
    math(EXPR padLength "(${maxLength} + 1) - ${nameLength}")
    string(RANDOM LENGTH ${padLength} ALPHABET " " padding)
    message_tested_option(${name} ${padding})
  endforeach()
endfunction()

if(APPLE)
  include(CheckOBJCSourceCompiles)
  enable_language(OBJC)
endif()

if(CMAKE_VERSION VERSION_LESS 3.18)
  function(check_linker_flag LANG FLAG VAR)
    cmake_push_check_state()
    list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${FLAG} )
    if(LANG STREQUAL "C")
      include(CheckCSourceCompiles)
      check_c_source_compiles("int main(int argc,char*argv[]){(void)argc;(void)argv;return 0;}" ${VAR} FAIL_REGEX "warning")
    elseif(LANG STREQUAL "CXX")
      include(CheckCXXSourceCompiles)
      check_cxx_source_compiles("int main(int argc,char*argv[]){(void)argc;(void)argv;return 0;}" ${VAR} FAIL_REGEX "warning")
    else()
      message(FATAL_ERROR "Unsupported language: ${LANG}")
    endif()
    cmake_pop_check_state()
  endfunction()
else()
  cmake_policy(SET CMP0057 NEW)  # Support new if() IN_LIST operator. (used inside check_linker_flag, used in CMake 3.18)
  include(CheckLinkerFlag)
endif()

if(APPLE)
  check_language(OBJC)
  if(NOT CMAKE_OBJC_COMPILER)
    message(WARNING "Cannot find working OBJC compiler.")
  endif()
endif()

function(SDL_PrintSummary)
  ##### Info output #####
  message(STATUS "")
  message(STATUS "SDL3 was configured with the following options:")
  message(STATUS "")
  message(STATUS "Platform: ${CMAKE_SYSTEM}")
  message(STATUS "64-bit:   ${ARCH_64}")
  message(STATUS "Compiler: ${CMAKE_C_COMPILER}")
  message(STATUS "Revision: ${SDL_REVISION}")
  message(STATUS "Vendor:   ${SDL_VENDOR_INFO}")
  message(STATUS "")
  message(STATUS "Subsystems:")

  find_stringlength_longest_item(SDL_SUBSYSTEMS maxLength)
  foreach(_SUB IN LISTS SDL_SUBSYSTEMS)
    string(LENGTH ${_SUB} _SUBLEN)
    math(EXPR _PADLEN "(${maxLength} + 1) - ${_SUBLEN}")
    string(RANDOM LENGTH ${_PADLEN} ALPHABET " " _PADDING)
    string(TOUPPER ${_SUB} _OPT)
    message_bool_option(${_SUB} SDL_${_OPT} ${_PADDING})
  endforeach()
  message(STATUS "")
  message(STATUS "Options:")
  list(SORT ALLOPTIONS)
  message_dictlist(ALLOPTIONS)
  message(STATUS "")
  message(STATUS " Build Shared Library: ${SDL_SHARED}")
  message(STATUS " Build Static Library: ${SDL_STATIC}")
  if(SDL_STATIC)
    message(STATUS " Build Static Library with Position Independent Code: ${SDL_STATIC_PIC}")
  endif()
  if(APPLE)
    message(STATUS " Build libraries as Apple Framework: ${SDL_FRAMEWORK}")
  endif()
  message(STATUS "")
  if(UNIX)
    message(STATUS "If something was not detected, although the libraries")
    message(STATUS "were installed, then make sure you have set the")
    message(STATUS "CMAKE_C_FLAGS and CMAKE_PREFIX_PATH CMake variables correctly.")
    message(STATUS "")
  endif()

  if(WARN_ABOUT_ARM_SIMD_ASM_MIT)
    message(STATUS "SDL is being built with ARM SIMD optimizations, which")
    message(STATUS "uses code licensed under the MIT license. If this is a")
    message(STATUS "problem, please disable that code by rerunning CMake with:")
    message(STATUS "")
    message(STATUS "    -DSDL_ARMSIMD=OFF")
    message(STATUS "")
  endif()

  if(WARN_ABOUT_ARM_NEON_ASM_MIT)
    message(STATUS "SDL is being built with ARM NEON optimizations, which")
    message(STATUS "uses code licensed under the MIT license. If this is a")
    message(STATUS "problem, please disable that code by rerunning CMake with:")
    message(STATUS "")
    message(STATUS "    -DSDL_ARMNEON=OFF")
    message(STATUS "")
  endif()
endfunction()
