cmake_minimum_required(VERSION 3.21)
project(Player VERSION 0.11.0 LANGUAGES C CXX RC)

if(NOT MSVC)
    message(FATAL_ERROR "Player builds with MSVC only")
endif()

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Static CRT so the exe needs no runtime DLLs.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
# C++/WinRT would otherwise embed source file paths in the exe for its error reports.
add_compile_definitions(WINRT_NO_SOURCE_LOCATION)

add_compile_definitions(UNICODE _UNICODE WIN32_LEAN_AND_MEAN NOMINMAX _WIN32_WINNT=0x0A00 WINVER=0x0A00)
file(TO_NATIVE_PATH "${CMAKE_SOURCE_DIR}" HSP_SOURCE_DIR)
add_compile_options(
    "$<$<COMPILE_LANGUAGE:C,CXX>:/W4;/utf-8;/permissive-;/Zc:__cplusplus>"
    "$<$<AND:$<CONFIG:Release>,$<COMPILE_LANGUAGE:C,CXX>>:/O2;/Gy;/Gw>"
    # Whole-program optimization for everything except miniaudio: under /GL the linker generated one of
    # dr_flac's functions two different ways from run to run (about one link in ten), which broke
    # byte-identical rebuilds.
    "$<$<AND:$<CONFIG:Release>,$<COMPILE_LANGUAGE:C,CXX>,$<NOT:$<STREQUAL:$<TARGET_PROPERTY:NAME>,ma>>>:/GL>"
    # Control Flow Guard.
    "$<$<COMPILE_LANGUAGE:C,CXX>:/guard:cf>"
    # Reproducible Release builds: no build time in the objects or the exe, and the source folder mapped
    # to a fixed name (anonymous namespaces are named from the source path), so the same source and the
    # same tools give the same bytes wherever they are built.
    "$<$<AND:$<CONFIG:Release>,$<COMPILE_LANGUAGE:C,CXX>>:/Brepro;/experimental:deterministic;/pathmap:${HSP_SOURCE_DIR}=HSP>"
)
add_link_options("$<$<CONFIG:Release>:/LTCG;/OPT:REF;/OPT:ICF;/Brepro>" /guard:cf /CETCOMPAT)

# miniaudio implementation unit, shared by the app and the tests.
add_library(ma STATIC src/miniaudio_impl.c)
target_include_directories(ma PUBLIC src third_party/miniaudio)

# Engine, workers and playlist, shared by the app and the tests.
add_library(core STATIC src/audio.cpp src/loader.cpp src/playlist.cpp src/radio.cpp src/scanner.cpp src/settings.cpp
    src/stream.cpp src/tags.cpp src/mp4.cpp src/asf.cpp src/adts.cpp src/mf.cpp src/http.cpp src/podcast.cpp src/cd.cpp src/booklog.cpp)
target_link_libraries(core PUBLIC ma shlwapi shell32 ole32 winhttp mfplat mfreadwrite mfuuid xmllite)

add_executable(HolySnailPlayer WIN32
    src/main.cpp
    src/app.cpp
    src/ui.cpp
    src/smtc.cpp
    src/theme.cpp
    res/player.rc
)
target_link_libraries(HolySnailPlayer PRIVATE core comctl32 ole32 shell32 uxtheme dwmapi runtimeobject delayimp)
target_include_directories(HolySnailPlayer PRIVATE res)
# The manifest comes from player.rc. Static imports resolve from System32 only. The WinRT imports
# (SMTC, started after the first paint) load on first use, so they don't count toward startup.
target_link_options(HolySnailPlayer PRIVATE /MANIFEST:NO /DEPENDENTLOADFLAG:0x800
    /DELAYLOAD:api-ms-win-core-winrt-l1-1-0.dll /DELAYLOAD:api-ms-win-core-winrt-error-l1-1-1.dll
    /DELAYLOAD:oleaut32.dll /DELAYLOAD:winhttp.dll /DELAYLOAD:mfplat.dll /DELAYLOAD:mfreadwrite.dll /DELAYLOAD:xmllite.dll /DELAYLOAD:dwmapi.dll)

add_executable(Player_tests
    tests/test_main.cpp
    tests/fixtures.cpp
    tests/test_miniaudio.cpp
    tests/test_audio.cpp
    tests/test_loader.cpp
    tests/test_playlist.cpp
    tests/test_scanner.cpp
    tests/test_tags.cpp
    tests/test_settings.cpp
    tests/test_radio.cpp
    tests/test_mp4.cpp
    tests/test_podcast.cpp
    tests/test_cd.cpp
    tests/test_booklog.cpp
    tests/test_formats.cpp
)
target_link_libraries(Player_tests PRIVATE core)

# Optional helper programs for testing (audio session probe, AAC encoder for test files). They are
# built only when the tools folder is present; the source download doesn't include it.
if(EXISTS "${CMAKE_SOURCE_DIR}/tools/audio_probe.cpp")
    add_executable(Player_audio_probe tools/audio_probe.cpp)
    target_link_libraries(Player_audio_probe PRIVATE ole32)
    add_executable(Player_make_mf tools/make_mf.cpp)
    target_link_libraries(Player_make_mf PRIVATE mf mfplat mfreadwrite mfuuid ole32)
endif()
