# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: Copyright (C) 2026 Max A. Vilkki a.k.a. codise
#
# SDL3 (zlib license), built static from lib/SDL3 (release-3.4.16).
#
# We use SDL for the window, events, OpenGL context, file dialogs, tray, clipboard and
# power status. Every subsystem beyond that is switched off to keep the binary small.
# The normal variables below win over SDL's option() defaults (policy CMP0077) and only
# live in this directory's scope, so they don't leak into the rest of the build.
#
# Size: about 1.2 MB of a 2.0 MB arm64 release binary (measured 2026-09 with the
# placeholder app). Dead-stripping barely helps here, because SDL's dynamic API table
# (src/dynapi) references every SDL function. SDL only lets you turn that table off by
# editing SDL_dynapi.h, which would mean patching lib/SDL3.

# Library only: no shared build, tests, examples or install rules.
set(SDL_SHARED OFF)
set(SDL_STATIC ON)
set(SDL_TEST_LIBRARY OFF)
set(SDL_TESTS OFF)
set(SDL_EXAMPLES OFF)
set(SDL_INSTALL OFF)
set(SDL_UNINSTALL OFF)

# Subsystems we don't use. VIDEO (with events), POWER (battery-aware animation rate),
# DIALOG and TRAY stay on.
set(SDL_AUDIO OFF)      # miniaudio does audio (DESIGN §2.1)
set(SDL_GPU OFF)        # ImGui renders with OpenGL 3.3 directly
set(SDL_RENDER OFF)     # likewise
set(SDL_CAMERA OFF)
set(SDL_JOYSTICK OFF)   # also covers the gamepad API; SDL_OpenGamepad() etc. still link
set(SDL_HAPTIC OFF)
set(SDL_HIDAPI OFF)
set(SDL_SENSOR OFF)

# Graphics APIs and video drivers we don't use. Desktop OpenGL (SDL_OPENGL) stays on;
# EGL, which Wayland needs for it, doesn't depend on SDL_OPENGLES.
set(SDL_OPENGLES OFF)
set(SDL_VULKAN OFF)
set(SDL_METAL OFF)
set(SDL_OFFSCREEN OFF)  # offscreen (EGL) video driver; the tiny dummy driver stays
set(SDL_KMSDRM OFF)     # console (no X11/Wayland) video driver on Linux

if(APPLE)
    # With subsystems off, a few dozen SDL sources compile to empty objects, and Apple's
    # ranlib warns about each one in universal builds.
    set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols <TARGET>")
endif()

# CMake 4.4 warns that SDL's check_c_source_compiles() passes backslashes through a macro
# (CMP0219, unset by SDL's minimum version). OLD keeps what every CMake before did.
set(CMAKE_POLICY_DEFAULT_CMP0219 OLD)

# SYSTEM keeps SDL's headers out of our warnings; EXCLUDE_FROM_ALL builds only what we
# link.
add_subdirectory("${FLIPSID_LIB_DIR}/SDL3" "${CMAKE_CURRENT_BINARY_DIR}/SDL3"
    SYSTEM EXCLUDE_FROM_ALL)

# On Linux, SDL quietly builds without desktop OpenGL when the GL headers are missing (it
# bundles none), and without X11 or Wayland when their dev packages are, as long as one
# of the two is left. We need a GL 3.3 context on both (DESIGN §2). The package names
# are Debian/Ubuntu's; SDL's docs/README-linux.md lists them for other distros.
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(sdlMissing "")
    foreach(check
            "SDL_VIDEO_OPENGL|OpenGL headers (libgl-dev)"
            "SDL_VIDEO_OPENGL_GLX|GLX headers (libgl-dev)"
            "SDL_VIDEO_OPENGL_EGL|EGL (libegl-dev)"
            "HAVE_X11|X11 (libx11-dev libxext-dev)"
            "HAVE_WAYLAND|Wayland (libwayland-dev libxkbcommon-dev libegl-dev, and pkg-config)")
        string(REPLACE "|" ";" check "${check}")
        list(GET check 0 variable)
        list(GET check 1 what)
        get_directory_property(found DIRECTORY "${FLIPSID_LIB_DIR}/SDL3" DEFINITION ${variable})
        if(NOT found)
            list(APPEND sdlMissing "  ${what}")
        endif()
    endforeach()
    if(sdlMissing)
        list(JOIN sdlMissing "\n" sdlMissingText)
        message(FATAL_ERROR
            "SDL3: configured without parts flipSID needs:\n${sdlMissingText}\n"
            "Install the packages, delete CMakeCache.txt and configure again "
            "(SDL caches what it didn't find).")
    endif()
endif()

flipsid_configure_third_party(SDL3-static)

add_library(ext::SDL3 ALIAS SDL3-static)
