set(LIBNAME "prismatoid")
set(GODOT_PROJECT_DIR
    "project"
    CACHE STRING "The directory of a Godot project folder")

find_package(Python3 3.4 REQUIRED)

set(PRISM_GODOT_CPP_SOURCE_DIR
    "${CMAKE_CURRENT_LIST_DIR}/godot-cpp"
    CACHE PATH "godot-cpp sources; defaults to the in-tree submodule")
if(NOT EXISTS "${PRISM_GODOT_CPP_SOURCE_DIR}/src")
  if(PRISM_GODOT_CPP_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_LIST_DIR}/godot-cpp")
    find_program(GIT git REQUIRED)
    message(NOTICE "godot-cpp bindings source not found")
    message(NOTICE "initializing/updating the godot-cpp submodule...")
    execute_process(
      COMMAND "${GIT}" submodule update --init godot-cpp
      WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" COMMAND_ERROR_IS_FATAL ANY)
  else()
    message(
      FATAL_ERROR
        "PRISM_GODOT_CPP_SOURCE_DIR='${PRISM_GODOT_CPP_SOURCE_DIR}' contains no src/"
    )
  endif()
endif()
add_subdirectory("${PRISM_GODOT_CPP_SOURCE_DIR}"
                 "${CMAKE_CURRENT_BINARY_DIR}/godot-cpp" SYSTEM)
# godot-cpp links libstdc++ for any Clang on Windows, but the GNU-style driver
# targeting the MSVC runtime, as linting uses, has no libstdc++ to link.
if(WIN32
   AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
   AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU"
   AND NOT MINGW)
  foreach(_property LINK_OPTIONS INTERFACE_LINK_OPTIONS)
    get_target_property(_options godot-cpp ${_property})
    if(_options)
      list(FILTER _options EXCLUDE REGEX "-lstdc\\+\\+")
      set_target_properties(godot-cpp PROPERTIES ${_property} "${_options}")
    endif()
  endforeach()
endif()
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${godot-cpp_SOURCE_DIR}/cmake")
include(GodotCPPModule)
get_target_property(GODOTCPP_SUFFIX godot::cpp GODOTCPP_SUFFIX)
get_target_property(GODOTCPP_PLATFORM godot::cpp GODOTCPP_PLATFORM)
project(
  prismatoid
  VERSION ${PROJECT_VERSION}
  DESCRIPTION "Prism bindings for Godot"
  LANGUAGES C CXX)
add_library(${LIBNAME} SHARED)
target_sources(
  ${LIBNAME}
  PRIVATE src/register_types.cpp src/register_types.h
          src/godot_prism_context.cpp src/godot_prism_context.h
          src/godot_prism_backend.cpp src/godot_prism_backend.h)
target_link_libraries(${LIBNAME} PRIVATE godot-cpp prism::prism)
if(APPLE)
  set_target_properties(${LIBNAME} PROPERTIES LINKER_LANGUAGE CXX)
  target_link_directories(${LIBNAME} PRIVATE
                          ${CMAKE_Swift_IMPLICIT_LINK_DIRECTORIES})
  target_link_libraries(${LIBNAME}
                        PRIVATE ${CMAKE_Swift_IMPLICIT_LINK_LIBRARIES})
endif()
if(WIN32)
  target_compile_definitions(${LIBNAME} PRIVATE TYPED_METHOD_BIND)
  target_compile_definitions(godot-cpp PUBLIC TYPED_METHOD_BIND)
endif()
# The class reference for the editor's help. The editor loads the debug library,
# so the release library has no use for it.
if(NOT GODOTCPP_TARGET STREQUAL "template_release")
  target_doc_sources(
    ${LIBNAME}
    "${CMAKE_CURRENT_LIST_DIR}/doc_classes/GodotPrismBackend.xml;${CMAKE_CURRENT_LIST_DIR}/doc_classes/GodotPrismContext.xml"
  )
  # Generated outside the source tree, where no .clang-tidy applies.
  set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/gen/doc_source.cpp"
                              PROPERTIES SKIP_LINTING ON)
endif()
set_property(TARGET ${LIBNAME} PROPERTY CXX_STANDARD 23)
set_property(TARGET ${LIBNAME} PROPERTY C_STANDARD 18)
if(PRISM_ENABLE_LINTING)
  prism_enable_target_linting(${LIBNAME})
endif()
set_target_properties(
  ${LIBNAME}
  PROPERTIES RUNTIME_OUTPUT_DIRECTORY
             "$<1:${PROJECT_SOURCE_DIR}/bin/${GODOTCPP_PLATFORM}>"
             PREFIX ""
             OUTPUT_NAME "${LIBNAME}${GODOTCPP_SUFFIX}")
set(GODOT_PROJECT_BINARY_DIR
    "${PROJECT_SOURCE_DIR}/${GODOT_PROJECT_DIR}/bin/${GODOTCPP_PLATFORM}")
add_custom_command(
  TARGET ${LIBNAME}
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:${LIBNAME}>"
          "${GODOT_PROJECT_BINARY_DIR}/$<TARGET_FILE_NAME:${LIBNAME}>")
