option(BUILD_ATHENA "Build Athena libraries from source" ON)
if (WIN32 OR APPLE)
  # 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 (WIN32)
    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(boo EXCLUDE_FROM_ALL)
add_subdirectory(nod EXCLUDE_FROM_ALL)
# amuse must come after athena/atdna, boo and nod
add_subdirectory(amuse 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)

add_library(nativefiledialog STATIC EXCLUDE_FROM_ALL nativefiledialog/src/nfd_common.c)
target_include_directories(nativefiledialog PUBLIC nativefiledialog/src/include)
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  target_sources(nativefiledialog PRIVATE nativefiledialog/src/nfd_gtk.c)
  target_compile_definitions(nativefiledialog INTERFACE NATIVEFILEDIALOG_SUPPORTED)

  find_package(PkgConfig REQUIRED)
  pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
  target_link_libraries(nativefiledialog PRIVATE ${GTK3_LIBRARIES})
  target_compile_options(nativefiledialog PRIVATE ${GTK3_CFLAGS_OTHER} -Wno-format-truncation -Wno-stringop-truncation)
  target_include_directories(nativefiledialog PRIVATE ${GTK3_INCLUDE_DIRS})
  target_link_directories(nativefiledialog PRIVATE ${GTK3_LIBRARY_DIRS})
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  target_sources(nativefiledialog PRIVATE nativefiledialog/src/nfd_cocoa.m)
  target_compile_definitions(nativefiledialog INTERFACE NATIVEFILEDIALOG_SUPPORTED)
  # TODO link flags
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
  target_sources(nativefiledialog PRIVATE nativefiledialog/src/nfd_win.cpp)
  target_compile_definitions(nativefiledialog 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)
