# SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <stenzek@gmail.com>
# SPDX-License-Identifier: CC-BY-NC-ND-4.0 + Packaging Restriction
#
# NOTE: In addition to the terms of CC-BY-NC-ND-4.0, you may not use this file to create
# packages or build recipes without explicit permission from the copyright holder.

add_library(core
  achievements.cpp
  achievements.h
  achievements_private.h
  analog_controller.cpp
  analog_controller.h
  analog_joystick.cpp
  analog_joystick.h
  bios.cpp
  bios.h
  bus.cpp
  bus.h
  cdrom.cpp
  cdrom.h
  cdrom_async_reader.cpp
  cdrom_async_reader.h
  cdrom_subq_replacement.cpp
  cdrom_subq_replacement.h
  cheats.cpp
  cheats.h
  controller.cpp
  controller.h
  controller_helpers.h
  core.cpp
  core.h
  cpu_code_cache.cpp
  cpu_code_cache.h
  cpu_code_cache_private.h
  cpu_core.cpp
  cpu_core.h
  cpu_core_private.h
  cpu_disasm.cpp
  cpu_disasm.h
  cpu_pgxp.cpp
  cpu_pgxp.h
  cpu_types.cpp
  cpu_types.h
  ddgo_controller.cpp
  ddgo_controller.h
  digital_controller.cpp
  digital_controller.h
  discord_presence.cpp
  discord_presence.h
  dma.cpp
  dma.h
  fullscreenui.cpp
  fullscreenui.h
  fullscreenui_achievements.cpp
  fullscreenui_game_list.cpp
  fullscreenui_private.h
  fullscreenui_settings.cpp
  fullscreenui_widgets.cpp
  fullscreenui_widgets.h
  game_database.cpp
  game_database.h
  game_list.cpp
  game_list.h
  gdb_server.cpp
  gdb_server.h
  gpu.cpp
  gpu.h
  gpu_backend.cpp
  gpu_backend.h
  gpu_dump.cpp
  gpu_dump.h
  gpu_helpers.h
  gpu_hw.cpp
  gpu_hw.h
  gpu_hw_shadergen.cpp
  gpu_hw_shadergen.h
  gpu_hw_texture_cache.cpp
  gpu_hw_texture_cache.h
  gpu_sw.cpp
  gpu_sw.h
  gpu_sw_rasterizer.cpp
  gpu_sw_rasterizer.h
  gpu_types.h
  guncon.cpp
  guncon.h
  gte.cpp
  gte.h
  gte_types.h
  host.h
  hotkeys.cpp
  input_types.h
  imgui_overlays.cpp
  imgui_overlays.h
  interrupt_controller.cpp
  interrupt_controller.h
  jogcon.cpp
  jogcon.h
  justifier.cpp
  justifier.h
  mdec.cpp
  mdec.h
  memory_card.cpp
  memory_card.h
  memory_card_image.cpp
  memory_card_image.h
  memory_scanner.cpp
  memory_scanner.h
  mips_encoder.h
  multitap.cpp
  multitap.h
  negcon.cpp
  negcon.h
  negcon_rumble.cpp
  negcon_rumble.h
  pad.cpp
  pad.h
  pcdrv.cpp
  pcdrv.h
  performance_counters.cpp
  performance_counters.h
  pio.cpp
  pio.h
  playstation_mouse.cpp
  playstation_mouse.h
  psf_loader.cpp
  psf_loader.h
  save_state_version.h
  settings.cpp
  settings.h
  shader_cache_version.h
  sio.cpp
  sio.h
  sound_effect_manager.cpp
  sound_effect_manager.h
  spu.cpp
  spu.h
  system.cpp
  system.h
  system_private.h
  timers.cpp
  timers.h
  timing_event.cpp
  timing_event.h
  types.h
  video_presenter.cpp
  video_presenter.h
  video_shadergen.cpp
  video_shadergen.h
  video_thread.cpp
  video_thread.h
  video_thread_commands.h
  video_thread_private.h
)

set(RECOMPILER_SRCS
  cpu_recompiler.cpp
  cpu_recompiler.h
)

target_precompile_headers(core PRIVATE "pch.h")
target_include_directories(core PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_link_libraries(core PUBLIC Threads::Threads common util)
target_link_libraries(core PRIVATE xxhash imgui rapidyaml rcheevos cpuinfo::cpuinfo ZLIB::ZLIB zstd::libzstd_shared speex_resampler_headers)

if(CPU_ARCH_X64)
  target_compile_definitions(core PUBLIC "ENABLE_RECOMPILER=1" "ENABLE_MMAP_FASTMEM=1")
  target_sources(core PRIVATE
    ${RECOMPILER_SRCS}
    cpu_recompiler_x64.cpp
    cpu_recompiler_x64.h
  )
  target_link_libraries(core PRIVATE xbyak)
  if(CMAKE_BUILD_TYPE MATCHES "Debug|Devel")
    target_link_libraries(core PRIVATE zydis)
  endif()
  message(STATUS "Building x64 recompiler.")
endif()
if(CPU_ARCH_ARM32)
  target_compile_definitions(core PUBLIC "ENABLE_RECOMPILER=1")
  target_sources(core PRIVATE
    ${RECOMPILER_SRCS}
    cpu_recompiler_arm32.cpp
    cpu_recompiler_arm32.h
  )
  target_link_libraries(core PRIVATE vixl)
  message(STATUS "Building ARM32 recompiler.")
endif()
if(CPU_ARCH_ARM64)
  target_compile_definitions(core PUBLIC "ENABLE_RECOMPILER=1" "ENABLE_MMAP_FASTMEM=1")
  target_sources(core PRIVATE
    ${RECOMPILER_SRCS}
    cpu_recompiler_arm64.cpp
    cpu_recompiler_arm64.h
  )
  target_link_libraries(core PRIVATE vixl)
  message(STATUS "Building ARM64 recompiler.")
endif()
if(CPU_ARCH_RISCV64)
  target_compile_definitions(core PUBLIC "ENABLE_RECOMPILER=1" "ENABLE_MMAP_FASTMEM=1")
  target_sources(core PRIVATE
    ${RECOMPILER_SRCS}
    cpu_recompiler_riscv64.cpp
    cpu_recompiler_riscv64.h
  )
  target_link_libraries(core PRIVATE biscuit::biscuit riscv-disas)
  message(STATUS "Building RISC-V-64 recompiler.")
endif()
if(CPU_ARCH_LOONGARCH64)
  target_compile_definitions(core PUBLIC "ENABLE_RECOMPILER=1" "ENABLE_MMAP_FASTMEM=1")
  target_sources(core PRIVATE
    ${RECOMPILER_SRCS}
    cpu_recompiler_loongarch64.cpp
    cpu_recompiler_loongarch64.h
  )
  target_link_libraries(core PRIVATE lagoon)
  message(STATUS "Building LoongArch64 recompiler.")
endif()

function(add_core_resources target)
  add_util_resources(${target})

  add_resources(${target} "resources" ${CMAKE_SOURCE_DIR}/data/resources)

  # Copy dynamically-loaded libraries into the bundle.
  bundle_libraries(${target} DiscordRPC::discord-rpc)
endfunction()
