cmake_minimum_required(VERSION 3.24)
project(recomp_image_core VERSION 0.1.0 LANGUAGES C CXX)
include(CTest)
include(FetchContent)
set(BUILD_SHARED_LIBS OFF)
set(RECOMP_IMAGE_CHDR_SOURCE_DIR "" CACHE PATH "Qualified libchdr source override")
# Complete raw frames and decoded-block verification are the shared CD contract.
set(CHDR_WANT_TESTS OFF CACHE BOOL "Build only the CHD storage library" FORCE)
set(CHDR_VERIFY_BLOCK_CRC ON CACHE BOOL "Validate decoded media blocks" FORCE)
set(CHDR_WANT_RAW_DATA_SECTOR ON CACHE BOOL "Preserve complete source sectors" FORCE)
set(CHDR_WANT_SUBCODE ON CACHE BOOL "Preserve source subcode" FORCE)
set(CHDR_FLAC_BACKEND drflac CACHE STRING "Portable in-tree FLAC decoder" FORCE)
if(NOT TARGET chdr-static)
 if(RECOMP_IMAGE_CHDR_SOURCE_DIR)
  add_subdirectory("${RECOMP_IMAGE_CHDR_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/qualified-chdr" EXCLUDE_FROM_ALL)
 else()
  FetchContent_Declare(recomp_image_chdr
    URL "${CMAKE_CURRENT_SOURCE_DIR}/third_party/libchdr-6cde5348eb118da3baf94f75a69577a005a484fd.tar.gz"
    URL_HASH SHA256=a222b1f1c842f988c2aad51e5f56ba94a423ed87ccab71f1abdb757caa7444f5
    DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
  FetchContent_MakeAvailable(recomp_image_chdr)
 endif()
endif()
add_library(recomp_image_core STATIC src/image_reader.cpp src/cue_sheet.cpp src/image_api.cpp src/image_sha256.c)
add_library(recomp-image-core::core ALIAS recomp_image_core)
target_include_directories(recomp_image_core PUBLIC include PRIVATE src)
target_compile_features(recomp_image_core PUBLIC cxx_std_17)
target_link_libraries(recomp_image_core PRIVATE chdr-static)

# Rich native profiles share ownership without reducing their view contracts.
add_library(recomp_image_chd STATIC src/chd_cd.cpp)
add_library(recomp-image-core::chd ALIAS recomp_image_chd)
target_include_directories(recomp_image_chd PUBLIC include)
target_compile_features(recomp_image_chd PUBLIC cxx_std_20)
target_link_libraries(recomp_image_chd PRIVATE chdr-static)
add_library(recomp_image_opera STATIC src/opera_volume.cpp)
add_library(recomp-image-core::opera ALIAS recomp_image_opera)
target_link_libraries(recomp_image_opera PUBLIC recomp_image_chd)
target_compile_features(recomp_image_opera PUBLIC cxx_std_20)
add_library(recomp_image_cue STATIC src/cached_cue.cpp)
add_library(recomp-image-core::cue ALIAS recomp_image_cue)
target_include_directories(recomp_image_cue PUBLIC include)
target_compile_features(recomp_image_cue PUBLIC cxx_std_17)
add_library(recomp_image_xdvdfs INTERFACE)
add_library(recomp-image-core::xdvdfs ALIAS recomp_image_xdvdfs)
target_include_directories(recomp_image_xdvdfs INTERFACE include)
target_compile_features(recomp_image_xdvdfs INTERFACE cxx_std_17)
if(MSVC)
  target_compile_options(recomp_image_core PRIVATE /W4)
else()
  target_compile_options(recomp_image_core PRIVATE -Wall -Wextra -Wpedantic)
endif()
if(BUILD_TESTING)
  foreach(scenario geometry cdda pregap sbi contract)
    add_executable(image_${scenario}_test tests/${scenario}.cpp)
    target_link_libraries(image_${scenario}_test PRIVATE recomp_image_core)
    file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fixtures/${scenario}")
    add_test(NAME image_${scenario} COMMAND image_${scenario}_test)
    set_tests_properties(image_${scenario} PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fixtures/${scenario}")
  endforeach()
  add_executable(image_c_api_test tests/c_api.c)
  target_link_libraries(image_c_api_test PRIVATE recomp_image_core)
  add_test(NAME image_c_api COMMAND image_c_api_test)
  foreach(profile chd_cd cached_cue)
    add_executable(image_${profile}_test tests/${profile}.cpp)
    if(profile STREQUAL "chd_cd")
      target_link_libraries(image_${profile}_test PRIVATE recomp_image_chd)
    else()
      target_link_libraries(image_${profile}_test PRIVATE recomp_image_cue)
    endif()
    file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fixtures/${profile}")
    add_test(NAME image_${profile} COMMAND image_${profile}_test)
    set_tests_properties(image_${profile} PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/fixtures/${profile}" TIMEOUT 0)
  endforeach()
endif()

# Verified SGCDIMG1 sector-archive profile. Drive timing stays with the caller.
add_library(recomp_image_archive STATIC src/sector_archive.c)
add_library(recomp-image-core::archive ALIAS recomp_image_archive)
target_include_directories(recomp_image_archive PUBLIC include)
target_compile_features(recomp_image_archive PUBLIC c_std_11)
option(RECOMP_IMAGE_ARCHIVE_MAP_RESIDENT "Verified mapped archive residency" ON)
target_compile_definitions(recomp_image_archive PRIVATE SEGACD_IMAGE_MAP_RESIDENT=$<BOOL:${RECOMP_IMAGE_ARCHIVE_MAP_RESIDENT}>)
if(WIN32)
  target_link_libraries(recomp_image_archive PRIVATE bcrypt)
else()
  find_package(OpenSSL 1.1 QUIET COMPONENTS Crypto)
  if(TARGET OpenSSL::Crypto)
    target_compile_definitions(recomp_image_archive PRIVATE SEGACD_IMAGE_OPENSSL_SHA=1)
    target_link_libraries(recomp_image_archive PRIVATE OpenSSL::Crypto)
  endif()
endif()
