
cmake_minimum_required(VERSION 3.20)
project(AEROGAUGE_NA_E_77626171_PC LANGUAGES C CXX)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

include(FetchContent)
if(WIN32)
  FetchContent_Declare(SDL2 URL https://github.com/libsdl-org/SDL/releases/download/release-2.30.3/SDL2-devel-2.30.3-VC.zip)
  FetchContent_MakeAvailable(SDL2)
endif()

set(RT64_STATIC TRUE)
set(RT64_SDL_WINDOW_VULKAN TRUE)
add_compile_definitions(HLSL_CPU NOMINMAX __PRFCHWINTRIN_H)

# Shader compiler settings. RT64 sets these only inside its own folder, but
# RecompFrontend's UI shaders need them too (without them its shader rule
# has no compiler and produces empty files).
if(WIN32)
  set(DXC "C:/Users/gallo/AppData/Local/N64RS/runtime/rt64/src/contrib/dxc/bin/x64/dxc.exe")
endif()
set(DXC_COMMON_OPTS "-IC:/Users/gallo/AppData/Local/N64RS/runtime/rt64/src")
set(DXC_DXIL_OPTS "-Wno-ignored-attributes")
set(DXC_SPV_OPTS "-spirv" "-fspv-target-env=vulkan1.0" "-fvk-use-dx-layout")
set(DXC_PS_OPTS "${DXC_COMMON_OPTS}" "-E" "PSMain" "-T ps_6_0")
set(DXC_VS_OPTS "${DXC_COMMON_OPTS}" "-E" "VSMain" "-T vs_6_0" "-fvk-invert-y")

if(NOT EXISTS "C:/Users/gallo/AppData/Local/N64RS/runtime/rt64/CMakeLists.txt")
  message(FATAL_ERROR "RT64 is missing. Run CREATE RUNTIME BASE again; the studio clones RT64 too.")
endif()
add_subdirectory("C:/Users/gallo/AppData/Local/N64RS/runtime/rt64" "${CMAKE_BINARY_DIR}/rt64")
add_subdirectory("C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime" "${CMAKE_BINARY_DIR}/n64runtime")
set(RECOMP_FRONTEND_N64MODERNRUNTIME_PATH "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime" CACHE PATH "" FORCE)
set(RECOMP_FRONTEND_RT64_PATH "C:/Users/gallo/AppData/Local/N64RS/runtime/rt64" CACHE PATH "" FORCE)
add_subdirectory("C:/Users/gallo/AppData/Local/N64RS/runtime/RecompFrontend" "${CMAKE_BINARY_DIR}/frontend")

file(GLOB RECOMP_C "${CMAKE_SOURCE_DIR}/../generated/RecompiledFuncs_Discovery/*.c")
file(GLOB RECOMP_CPP "${CMAKE_SOURCE_DIR}/../generated/RecompiledFuncs_Discovery/*.cpp")
# Rust builds (config/rust.on): the game code comes from the Rust crate in
# the project's rust folder instead of the funcs_*.c files
if(N64RS_RUST_LIB)
  list(FILTER RECOMP_C EXCLUDE REGEX "/funcs_[0-9]+[.]c$")
endif()
add_library(RecompiledFuncs STATIC ${RECOMP_C} ${RECOMP_CPP})
if(N64RS_RUST_LIB)
  target_link_libraries(RecompiledFuncs PUBLIC "${N64RS_RUST_LIB}" kernel32 advapi32 ntdll userenv ws2_32 dbghelp bcrypt synchronization)
endif()
target_include_directories(RecompiledFuncs PRIVATE
  "${CMAKE_SOURCE_DIR}/../generated/RecompiledFuncs_Discovery"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime/N64Recomp/include"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime/ultramodern/include"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime/librecomp/include")
# Same flags the official recomp projects use (clang / clang-cl)
target_compile_options(RecompiledFuncs PRIVATE -march=nehalem -fno-strict-aliasing -fms-extensions -Wno-everything)
# TRACE_ENTRY() for diagnostic builds (config/trace.on)
if(EXISTS "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime/librecomp/include/trace.h")
  if(MSVC)
    target_compile_options(RecompiledFuncs PRIVATE /FItrace.h)
  else()
    target_compile_options(RecompiledFuncs PRIVATE -include trace.h)
  endif()
endif()

add_executable(AEROGAUGE_NA_E_77626171_PC src/universal_host.cpp)
target_include_directories(AEROGAUGE_NA_E_77626171_PC PRIVATE
  "${CMAKE_SOURCE_DIR}/../generated/RecompiledFuncs_Discovery"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime/N64Recomp/include"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime/ultramodern/include"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/N64ModernRuntime/librecomp/include"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/RecompFrontend/recompui/include"
  "C:/Users/gallo/AppData/Local/N64RS/runtime/RecompFrontend/recompui/src"
  "${sdl2_SOURCE_DIR}/include")
target_link_directories(AEROGAUGE_NA_E_77626171_PC PRIVATE "${sdl2_SOURCE_DIR}/lib/x64")
target_compile_options(AEROGAUGE_NA_E_77626171_PC PRIVATE -march=nehalem -fno-strict-aliasing -fms-extensions)
if(MSVC)
  # identical-code folding would merge recompiled functions that share bodies
  target_link_options(AEROGAUGE_NA_E_77626171_PC PRIVATE /OPT:NOICF)
  # a normal Windows app: no console window next to the game
  target_link_options(AEROGAUGE_NA_E_77626171_PC PRIVATE /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup)
  # symbol map next to the exe: turns crash addresses in play.log into function names
  target_link_options(AEROGAUGE_NA_E_77626171_PC PRIVATE "/MAP:$<TARGET_FILE_DIR:AEROGAUGE_NA_E_77626171_PC>/AEROGAUGE_NA_E_77626171_PC.map" /DEBUG)
endif()
target_link_libraries(AEROGAUGE_NA_E_77626171_PC PRIVATE RecompiledFuncs librecomp ultramodern recompui recompinput rt64 SDL2 Winmm.lib)
if(WIN32)
  add_custom_command(TARGET AEROGAUGE_NA_E_77626171_PC POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different "${sdl2_SOURCE_DIR}/lib/x64/SDL2.dll" $<TARGET_FILE_DIR:AEROGAUGE_NA_E_77626171_PC>)
endif()
