# SPDX-FileCopyrightText: 2025-2026 Jérôme SONRIER <jsid@emor3j.fr.eu.org>
# SPDX-License-Identifier: GPL-2.0-or-later

cmake_minimum_required(VERSION 3.18)

project(taylet
  VERSION 0.7.2
  DESCRIPTION "Player for .AY (ZX Spectrum / Amstrad CPC) music files"
  LANGUAGES C)

include(GNUInstallDirs)
include(CheckIncludeFile)

# The version above is the only place where it is written: the program, the
# KDE application and the man pages take it from here. The date of the man
# pages is written by hand when a version is released, not taken from the
# clock, so that two builds of the same sources give the same files.
set(TAYLET_MAN_DATE "November 2026")

# Fill in the date and the version of a man page (doc/<n>.1.in), then install
# it.
function(taylet_install_man name)
  configure_file(doc/${name}.1.in ${PROJECT_BINARY_DIR}/doc/${name}.1 @ONLY)
  install(FILES ${PROJECT_BINARY_DIR}/doc/${name}.1
    DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
endfunction()

# ---------------------------------------------------------------------------
# Options
#
# Every executable is chosen explicitly: nothing is enabled or disabled
# behind your back because a library happens to be (or not to be) installed.
# If you ask for a program whose dependency is missing, the configuration
# fails.
# ---------------------------------------------------------------------------

option(TAYLET_BUILD_CLI "Build taylet, the command-line player" ON)
option(TAYLET_BUILD_SDL "Build taylet-sdl, the player using SDL2 (needs SDL2)" OFF)
option(TAYLET_BUILD_KDE
  "Build taylet-kde, the Qt/KDE player (needs Qt 6 and KDE Frameworks 6)" OFF)

# Application identifier of taylet-kde (desktop file, AppStream, D-Bus name):
# the reverse of a domain name belonging to the project.
set(TAYLET_APP_ID "org.eu.fr.emor3j.taylet" CACHE STRING
  "Application identifier of taylet-kde")

# Audio output driver for the command-line player (taylet).
#   none    - no audio device: raw PCM on stdout only (`-s'), no dependency
#   oss     - OSS (/dev/dsp)
#   openbsd - OpenBSD native audio (/dev/sound)
# taylet-sdl always uses SDL2 and ignores this setting.
set(TAYLET_DRIVER "none" CACHE STRING
  "Audio output driver for the command-line player (none, oss, openbsd)")
set_property(CACHE TAYLET_DRIVER PROPERTY STRINGS none oss openbsd)

option(TAYLET_WARNINGS "Enable compiler warnings (-Wall)" ON)
option(TAYLET_BUILD_TESTS "Build the tests (run them with ctest)" ON)

# Only when the user (or a packaging tool) did not choose a build type.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# The core and the other players are C; the Qt/KDE front-end is C++.
if(TAYLET_BUILD_KDE)
  enable_language(CXX)
endif()

# ---------------------------------------------------------------------------
# Driver selection for the command-line player
# ---------------------------------------------------------------------------

if(TAYLET_BUILD_CLI)
  if(TAYLET_DRIVER STREQUAL "none")
    set(TAYLET_DRIVER_SOURCE src/drivers/drv-stdout.c)
    set(TAYLET_DRIVER_DEFINE "")
  elseif(TAYLET_DRIVER STREQUAL "oss")
    check_include_file(sys/soundcard.h HAVE_SYS_SOUNDCARD_H)
    if(NOT HAVE_SYS_SOUNDCARD_H)
      message(FATAL_ERROR
        "TAYLET_DRIVER=oss requires <sys/soundcard.h>, which was not found")
    endif()
    set(TAYLET_DRIVER_SOURCE src/drivers/drv-oss.c)
    set(TAYLET_DRIVER_DEFINE DRIVER_OSS)
  elseif(TAYLET_DRIVER STREQUAL "openbsd")
    set(TAYLET_DRIVER_SOURCE src/drivers/drv-obsd.c)
    set(TAYLET_DRIVER_DEFINE DRIVER_OPENBSD)
  else()
    message(FATAL_ERROR
      "Invalid TAYLET_DRIVER '${TAYLET_DRIVER}' (expected none, oss or openbsd)")
  endif()
endif()

# ---------------------------------------------------------------------------
# Emulation core (Z80 + AY/beeper sound), as an internal static library.
#
# z80ops.c, cbops.c and edops.c are #include'd by z80.c; they must not be
# compiled on their own, so they are only listed to be tracked as sources.
#
# The core calls the audio driver through src/core/driver.h; the functions
# are resolved at link time from the executable, which provides the driver
# of its choice (src/drivers, src/sdl). The core must not include anything
# from src/cli, src/drivers or src/sdl: they are not in its include path on
# purpose.
#
# C11 is needed for <stdatomic.h> (the player is controlled from other
# threads).
# ---------------------------------------------------------------------------

add_library(taylet-core STATIC
  src/core/z80.c
  src/core/sound.c
  src/core/machine.c
  src/core/player.c
  src/core/ay.c)
set_source_files_properties(
  src/core/z80ops.c src/core/cbops.c src/core/edops.c
  PROPERTIES HEADER_FILE_ONLY TRUE)
target_sources(taylet-core PRIVATE
  src/core/z80ops.c src/core/cbops.c src/core/edops.c)
target_include_directories(taylet-core PUBLIC src/core)
set_target_properties(taylet-core PROPERTIES
  C_STANDARD 11
  POSITION_INDEPENDENT_CODE ON)

if(TAYLET_WARNINGS AND CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
  target_compile_options(taylet-core PRIVATE -Wall)
endif()

# ---------------------------------------------------------------------------
# Players: the command-line front-end (src/cli/main.c: options, play list,
# signals) linked with the audio driver of the executable.
#
#   taylet_add_player(<name> [SOURCES <driver sources>]
#                     [DEFINITIONS <defs>] [LIBRARIES <libs>])
# ---------------------------------------------------------------------------

function(taylet_add_player target)
  cmake_parse_arguments(PARSE_ARGV 1 P "" "" "SOURCES;DEFINITIONS;LIBRARIES")

  add_executable(${target} src/cli/main.c ${P_SOURCES})
  target_link_libraries(${target} PRIVATE taylet-core ${P_LIBRARIES})
  set_target_properties(${target} PROPERTIES C_STANDARD 11)
  target_compile_definitions(${target} PRIVATE
    TAYLET_VERSION="${PROJECT_VERSION}" ${P_DEFINITIONS})
  if(TAYLET_WARNINGS AND CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
    target_compile_options(${target} PRIVATE -Wall)
  endif()
  install(TARGETS ${target} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endfunction()

# taylet: no dependency, driver chosen with TAYLET_DRIVER.
if(TAYLET_BUILD_CLI)
  taylet_add_player(taylet
    SOURCES ${TAYLET_DRIVER_SOURCE}
    DEFINITIONS ${TAYLET_DRIVER_DEFINE})
  taylet_install_man(taylet)
  message(STATUS "taylet: building taylet (audio driver: ${TAYLET_DRIVER})")
endif()

# taylet-sdl: audio through SDL2.
if(TAYLET_BUILD_SDL)
  find_package(SDL2 REQUIRED CONFIG)
  taylet_add_player(taylet-sdl
    SOURCES src/sdl/drv-sdl.c
    LIBRARIES SDL2::SDL2)
  taylet_install_man(taylet-sdl)
  message(STATUS "taylet: building taylet-sdl (SDL2 ${SDL2_VERSION})")
endif()

# taylet-kde: Qt/KDE front-end. src/qt is the part which needs Qt only (audio
# output, controller of the core); src/kde is the KDE user interface.
if(TAYLET_BUILD_KDE)
  add_subdirectory(src/qt)
  add_subdirectory(src/kde)
  taylet_install_man(taylet-kde)
  message(STATUS "taylet: building taylet-kde (${TAYLET_APP_ID})")
endif()

# ---------------------------------------------------------------------------
# Tests
#
# The core test is linked with taylet-core and a fake audio driver only:
# it proves that the core does not depend on any front-end.
# ---------------------------------------------------------------------------

if(TAYLET_BUILD_TESTS)
  enable_testing()
  add_subdirectory(tests)
endif()
