option(BUILD_ATHENA "Build Athena libraries from source" ON)
if (CMAKE_HOST_SYSTEM_NAME STREQUAL Windows OR CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
  # Default to binary atdna on Windows & macOS
  option(BUILD_ATDNA "Build atdna utility from source (requires LLVM)" OFF)
else ()
  option(BUILD_ATDNA "Build atdna utility from source (requires LLVM)" ON)
endif ()

if (NOT BUILD_ATDNA)
  set(ATHENA_BASE_URL "https://github.com/libAthena/athena/releases/download/latest")
  if (CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
    set(ATHENA_EXT 7z)
  else ()
    set(ATHENA_EXT tar.gz)
  endif ()
  include(FetchContent)
  FetchContent_Declare(athena-host
      URL "${ATHENA_BASE_URL}/athena-${HOST_PLATFORM_NAME}-${ATHENA_HOST_ARCH}.${ATHENA_EXT}")
  message(STATUS "Fetching atdna host binary")
  FetchContent_Populate(athena-host)
  include(${athena-host_SOURCE_DIR}/lib/cmake/atdna/atdnaConfig.cmake)
  set(atdna_DIR "${athena-host_SOURCE_DIR}/lib/cmake/atdna" PARENT_SCOPE)
endif ()

if (BUILD_ATHENA OR BUILD_ATDNA)
  add_subdirectory(athena EXCLUDE_FROM_ALL)
else ()
  if (ATHENA_ARCH STREQUAL ATHENA_HOST_ARCH)
    set(athena_SOURCE_DIR "${athena-host_SOURCE_DIR}")
  else ()
    FetchContent_Declare(athena
        URL "${ATHENA_BASE_URL}/athena-${HOST_PLATFORM_NAME}-${ATHENA_ARCH}.tar.gz")
    FetchContent_Populate(athena)
  endif ()
  include(${athena_SOURCE_DIR}/lib/cmake/athena/AthenaConfig.cmake)
  include(${athena_SOURCE_DIR}/lib/cmake/lzokay/lzokayConfig.cmake)
  add_library(lzokay ALIAS lzokay::lzokay)
  include(athena/atdna/atdnaHelpers.cmake)
endif ()

# Enable MoltenVK
#if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
#  set(DAWN_ENABLE_VULKAN ON CACHE BOOL "Enable compilation of the Vulkan backend" FORCE)
#endif()
set(AURORA_NATIVE_MATRIX ON CACHE BOOL "Assume OpenGL-layout matrices, disables transposing" FORCE)
add_subdirectory(aurora)

# boo must come after SDL2
add_subdirectory(nod EXCLUDE_FROM_ALL)

if (NOT GEKKO AND NOT NX AND NOT IOS AND NOT TVOS)
  set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/discord-rpc)
  if (NOT CMAKE_INSTALL_LIBDIR)
    set(CMAKE_INSTALL_LIBDIR ${CMAKE_BINARY_DIR}/fake-prefix)
  endif ()
  add_subdirectory(discord-rpc/src EXCLUDE_FROM_ALL)
  target_include_directories(discord-rpc PRIVATE rapidjson/include PUBLIC discord-rpc/include)
  if (APPLE)
    # remove their nasty hack
    get_target_property(DISCORD_LINK_LIBRARIES discord-rpc INTERFACE_LINK_LIBRARIES)
    list(REMOVE_ITEM DISCORD_LINK_LIBRARIES "-framework AppKit, -mmacosx-version-min=10.10")
    set_target_properties(discord-rpc PROPERTIES INTERFACE_LINK_LIBRARIES "${DISCORD_LINK_LIBRARIES}")
  endif ()
  if (UNIX)
    # remove another nasty hack
    get_target_property(DISCORD_COMPILE_OPTIONS discord-rpc COMPILE_OPTIONS)
    list(REMOVE_ITEM DISCORD_COMPILE_OPTIONS "-g")
    set_target_properties(discord-rpc PROPERTIES COMPILE_OPTIONS "${DISCORD_COMPILE_OPTIONS}")
  endif ()
endif ()

add_subdirectory(jbus EXCLUDE_FROM_ALL)
add_subdirectory(kabufuda EXCLUDE_FROM_ALL)

option(NFD_PORTAL "Use xdg-desktop-portal instead of GTK" ON)
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  add_subdirectory(nativefiledialog-extended)
  target_compile_definitions(nfd INTERFACE NATIVEFILEDIALOG_SUPPORTED)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  add_subdirectory(nativefiledialog-extended)
  target_compile_definitions(nfd INTERFACE NATIVEFILEDIALOG_SUPPORTED)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
  add_subdirectory(nativefiledialog-extended)
  target_compile_definitions(nfd INTERFACE NATIVEFILEDIALOG_SUPPORTED)
else ()
  message(WARNING "nativefiledialog unsupported for ${CMAKE_SYSTEM_NAME}")
endif ()

option(OPTICK_ENABLED "Enable profiling with Optick" OFF)
set(OPTICK_USE_VULKAN ${DAWN_ENABLE_VULKAN} CACHE BOOL "Built-in support for Vulkan" FORCE)
set(OPTICK_INSTALL_TARGETS OFF CACHE BOOL "Should optick be installed? Set to OFF if you use add_subdirectory to include Optick." FORCE)
add_subdirectory(optick)
if (NOT MSVC)
  target_compile_options(OptickCore PRIVATE -Wno-implicit-fallthrough)
endif ()

add_subdirectory(libjpeg-turbo EXCLUDE_FROM_ALL)
add_subdirectory(zeus EXCLUDE_FROM_ALL)
