enable_language(OBJCXX)
# The shared pad / lifecycle adapters (DualSense layout, runtime platform),
# also used by the PS5 port.
if(NOT TARGET outrun_ps5_platform)
    add_library(outrun_ps5_platform STATIC ../src/input/dual_sense.cpp ../src/input/runtime_adapter.cpp)
    target_link_libraries(outrun_ps5_platform PUBLIC outrun_native_runtime)
    target_compile_features(outrun_ps5_platform PUBLIC cxx_std_17)
    target_compile_options(outrun_ps5_platform PRIVATE -Wall -Wextra -Wpedantic -ffp-contract=off -fno-fast-math)
endif()
if(TARGET test_switch_audio)
    # Compile the unchanged Switch audio regression on macOS, using only a
    # test-local allocation shim. No production target gets this include path.
    target_include_directories(test_switch_audio BEFORE PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/tests/support")
endif()
function(or2_mac_pair_framework_options target)
    get_target_property(raw_options ${target} INTERFACE_LINK_OPTIONS)
    if(NOT raw_options)
        return()
    endif()
    set(paired_options)
    while(raw_options)
        list(POP_FRONT raw_options flag)
        if(flag STREQUAL "-framework")
            list(POP_FRONT raw_options framework)
            list(APPEND paired_options "SHELL:-framework ${framework}")
        else()
            list(APPEND paired_options "${flag}")
        endif()
    endwhile()
    set_property(TARGET ${target} PROPERTY INTERFACE_LINK_OPTIONS "${paired_options}")
endfunction()
if(TARGET PkgConfig::FFMPEG)
    or2_mac_pair_framework_options(PkgConfig::FFMPEG)
endif()
find_library(OR2_METAL Metal REQUIRED)
find_library(OR2_QUARTZCORE QuartzCore REQUIRED)
find_library(OR2_COCOA Cocoa REQUIRED)
find_package(Python3 COMPONENTS Interpreter REQUIRED)
set(OR2_METAL_SHADERS "${CMAKE_CURRENT_BINARY_DIR}/generated/pc_metal_shaders.hpp")
add_custom_command(OUTPUT "${OR2_METAL_SHADERS}"
    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_metal_shaders.py" "${OR2_METAL_SHADERS}"
    DEPENDS tools/generate_metal_shaders.py tools/generate_metal_vertices.py ../src/platform/pc_shader_programs_generated.cpp ../src/platform/pc_shader_programs.hpp ../switch/shaders/pc_ffp_fsh.glsl ../switch/shaders/pc_ps11_uber_fsh.glsl ../switch/shaders/pc_ps14_uber_fsh.glsl)
add_library(outrun_mac_metal STATIC source/metal_display.mm source/mac_paths.mm source/display_rate.mm source/metal_d3d9.mm source/metal_vertices.cpp "${OR2_METAL_SHADERS}")
target_compile_features(outrun_mac_metal PUBLIC cxx_std_17)
target_include_directories(outrun_mac_metal PUBLIC source)
target_include_directories(outrun_mac_metal PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_compile_options(outrun_mac_metal PRIVATE -fobjc-arc -Wall -Wextra -Wpedantic)
target_link_libraries(outrun_mac_metal PUBLIC outrun_native_runtime ${OR2_METAL} ${OR2_QUARTZCORE} ${OR2_COCOA})
# Application icon (tools/make_icons.ps1), in Contents/Resources of each bundle.
set_source_files_properties(OutRun.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
add_executable(OutRunMac MACOSX_BUNDLE source/main.mm OutRun.icns)
target_compile_options(OutRunMac PRIVATE -fobjc-arc)
target_link_libraries(OutRunMac PRIVATE outrun_mac_metal outrun_native_runtime)
set_target_properties(OutRunMac PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER "org.outrun.mac" MACOSX_BUNDLE_BUNDLE_NAME "OutRunMac" MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist.in")
add_executable(test_mac_metal tests/test_metal.mm)
target_compile_options(test_mac_metal PRIVATE -fobjc-arc)
target_link_libraries(test_mac_metal PRIVATE outrun_mac_metal)
add_test(NAME mac_metal_pixels COMMAND test_mac_metal)
set_tests_properties(mac_metal_pixels PROPERTIES LABELS "mac;metal")
add_executable(test_mac_metal_d3d9 tests/test_metal_d3d9.cpp)
target_link_libraries(test_mac_metal_d3d9 PRIVATE outrun_mac_metal)
add_test(NAME mac_metal_d3d9_differential COMMAND test_mac_metal_d3d9)
set_tests_properties(mac_metal_d3d9_differential PROPERTIES LABELS "mac;metal;d3d9")
option(OR2_MAC_SDL "Enable SDL2 input/audio services" OFF)
option(OR2_MAC_MEDIA "Enable FFmpeg/Vorbis decoders" OFF)
if(OR2_MAC_SDL)
    find_package(SDL2 CONFIG REQUIRED)
    add_library(outrun_mac_media STATIC source/sdl_services.cpp)
    target_include_directories(outrun_mac_media PUBLIC source)
    target_compile_features(outrun_mac_media PUBLIC cxx_std_17)
    target_link_libraries(outrun_mac_media PUBLIC SDL2::SDL2 outrun_ps5_platform)
    target_link_libraries(OutRunMac PRIVATE outrun_mac_media)
    target_compile_definitions(OutRunMac PRIVATE OR2_MAC_SDL=1)
    add_executable(OutRunMacGame MACOSX_BUNDLE OutRun.icns source/main.cpp source/setup_display.cpp ${PROJECT_SOURCE_DIR}/src/runtime/game_host.cpp source/platform_api.cpp source/renderer.cpp source/sdl_backend.cpp source/audio.cpp)
    target_compile_features(OutRunMacGame PRIVATE cxx_std_17)
    target_compile_definitions(OutRunMacGame PRIVATE OR2_MAC_HOST=1)
    target_include_directories(OutRunMacGame PRIVATE source)
    target_link_libraries(OutRunMacGame PRIVATE outrun_mac_metal outrun_ps5_platform SDL2::SDL2)
    set_target_properties(OutRunMacGame PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER "org.outrun.mac.game" MACOSX_BUNDLE_BUNDLE_NAME "OutRunMac" MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/GameInfo.plist.in")
    add_executable(test_mac_game_display tests/test_game_display.cpp source/sdl_backend.cpp source/audio.cpp)
    target_include_directories(test_mac_game_display PRIVATE source)
    target_link_libraries(test_mac_game_display PRIVATE SDL2::SDL2 outrun_native_runtime)
    add_test(NAME mac_game_metal_display COMMAND test_mac_game_display)
    set_tests_properties(mac_game_metal_display PROPERTIES ENVIRONMENT "SDL_AUDIODRIVER=dummy" LABELS "mac;metal;window")
    add_executable(test_mac_sdl tests/test_sdl.cpp)
    target_link_libraries(test_mac_sdl PRIVATE outrun_mac_media)
    add_test(NAME mac_sdl_services COMMAND test_mac_sdl)
    set_tests_properties(mac_sdl_services PROPERTIES ENVIRONMENT "SDL_AUDIODRIVER=dummy;SDL_VIDEODRIVER=dummy" LABELS "mac;audio;input")
endif()
if(OR2_MAC_MEDIA)
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(OR2_MAC_CODECS REQUIRED IMPORTED_TARGET libavformat libavcodec libswscale libswresample libavutil vorbisfile vorbis ogg)
    # Keep each Apple framework flag paired with its name when CMake
    # deduplicates pkg-config options on a subsequent configuration.
    set(OR2_MAC_RAW_OPTIONS ${OR2_MAC_CODECS_LDFLAGS_OTHER})
    set(OR2_MAC_LINK_OPTIONS)
    while(OR2_MAC_RAW_OPTIONS)
        list(POP_FRONT OR2_MAC_RAW_OPTIONS OR2_MAC_FLAG)
        if(OR2_MAC_FLAG STREQUAL "-framework")
            list(POP_FRONT OR2_MAC_RAW_OPTIONS OR2_MAC_FRAMEWORK)
            list(APPEND OR2_MAC_LINK_OPTIONS "SHELL:-framework ${OR2_MAC_FRAMEWORK}")
        else()
            list(APPEND OR2_MAC_LINK_OPTIONS "${OR2_MAC_FLAG}")
        endif()
    endwhile()
    set_property(TARGET PkgConfig::OR2_MAC_CODECS PROPERTY INTERFACE_LINK_OPTIONS "${OR2_MAC_LINK_OPTIONS}")
    target_compile_definitions(outrun_native_runtime PRIVATE OR2_HAVE_VORBIS=1 OUTRUN_WITH_FFMPEG=1)
    target_link_libraries(outrun_native_runtime PUBLIC PkgConfig::OR2_MAC_CODECS)
    add_executable(test_mac_codecs tests/test_codecs.cpp)
    target_link_libraries(test_mac_codecs PRIVATE outrun_native_runtime PkgConfig::OR2_MAC_CODECS)
    add_test(NAME mac_codec_availability COMMAND test_mac_codecs)
    set_tests_properties(mac_codec_availability PROPERTIES LABELS "mac;media")
endif()
# Tests that use the engine read the EXE-owned tables from the player's EXE image (top-level OR2_EXE).
if(OR2_EXE)
    get_property(_or2_mac_tests DIRECTORY PROPERTY TESTS)
    foreach(_t IN LISTS _or2_mac_tests)
        set_property(TEST ${_t} APPEND PROPERTY ENVIRONMENT "OR2_EXE=${OR2_EXE}")
    endforeach()
endif()
