cmake_minimum_required(VERSION 3.25)
project(MigPython LANGUAGES CXX)
set(engine "${CMAKE_CURRENT_SOURCE_DIR}/../_engine")
if(NOT EXISTS "${engine}/CMakeLists.txt")
    get_filename_component(engine "${CMAKE_CURRENT_SOURCE_DIR}/../../.." ABSOLUTE)
endif()
if(NOT EXISTS "${engine}/src/c-api/CMakeLists.txt")
    message(FATAL_ERROR "Build from tools/package-python.py or a complete MIG source distribution.")
endif()
set(MIG_BUILD_CONFIGURATOR OFF CACHE BOOL "" FORCE)
set(MIG_BUILD_CONTROLLER OFF CACHE BOOL "" FORCE)
set(MIG_BUILD_NATIVE_RUNTIME OFF CACHE BOOL "" FORCE)
set(MIG_BUILD_FACE OFF CACHE BOOL "" FORCE)
set(MIG_BUILD_HANDS ON CACHE BOOL "" FORCE)
set(MIG_BUILD_FORMAT ON CACHE BOOL "" FORCE)
set(MIG_BUILD_C_API ON CACHE BOOL "" FORCE)
set(MIG_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(MIG_INSTALL OFF CACHE BOOL "" FORCE)
if(EXISTS "${engine}/vendor/include/nlohmann/json.hpp")
    set(MIG_NATIVE_DEPS "${engine}/vendor" CACHE PATH "Bundled JSON headers" FORCE)
elseif(WIN32)
    set(MIG_NATIVE_DEPS "${engine}/build/native-deps" CACHE PATH "JSON headers")
else()
    set(MIG_NATIVE_DEPS "${engine}/build/native-linux-deps" CACHE PATH "JSON headers")
endif()
if(MSVC)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
add_subdirectory("${engine}" engine)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_link_options(mig-c PRIVATE -static-libstdc++ -static-libgcc)
    string(REGEX MATCH "^[0-9]+" compiler_major "${CMAKE_CXX_COMPILER_VERSION}")
    set(gcc_notice "/usr/share/doc/gcc-${compiler_major}-base/copyright")
    if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR NOT EXISTS "${gcc_notice}")
        message(FATAL_ERROR "Linux wheels require GCC and its gcc-N-base copyright notice.")
    endif()
    install(FILES "${gcc_notice}" DESTINATION mig/_native/licenses RENAME GCC-runtime-copyright)
    install(FILES /usr/share/common-licenses/GPL-3 DESTINATION mig/_native/licenses)
endif()
if(WIN32)
    install(FILES "$<TARGET_FILE:mig-c>" DESTINATION mig/_native RENAME mig-c.dll)
else()
    install(FILES "$<TARGET_FILE:mig-c>" DESTINATION mig/_native RENAME libmig-c.so.1)
endif()
if(EXISTS "${MIG_NATIVE_DEPS}/nlohmann-LICENSE")
    install(FILES "${MIG_NATIVE_DEPS}/nlohmann-LICENSE" DESTINATION mig/_native/licenses)
endif()
