cmake_minimum_required(VERSION 3.15.0)
project(touchlab_comm_py)

set(TOUCHLAB_COMM_VERSION 0.2.7)

find_package(Python COMPONENTS Interpreter Development)
add_subdirectory(pybind11)

option(INSTALL_NATIVE "Install into root of the install directory" OFF)

include_directories(
  include
)

pybind11_add_module(touchlab_comm_py src/comm.cpp)
set_property(TARGET touchlab_comm_py PROPERTY CXX_STANDARD 17)
set_property(TARGET touchlab_comm_py PROPERTY CXX_STANDARD_REQUIRED ON)
target_link_libraries(touchlab_comm_py PRIVATE pybind11::module)
if(APPLE)
  # OSX not supported
  message(FATAL_ERROR "Mac is not supported")
elseif(UNIX)
  # If unix
  if(ANDROID)
      target_link_libraries(touchlab_comm_py PRIVATE ${CMAKE_CURRENT_LIST_DIR}/lib/linux-arm64-v8a/libtouchlab_comm_static.a)
      # pybind11_add_module links pybind11::module -> CMake's Python::Module, which
      # deliberately does NOT link libpython (extension symbols normally resolve
      # against the host interpreter at load time). CMake's FindPython only relaxes
      # that for Apple (-undefined dynamic_lookup), SunOS and AIX; it has no Android
      # case, and the NDK toolchain links with -Wl,--no-undefined. So the CPython
      # C-API symbols pybind11 uses (PyErr_Fetch, PyErr_Restore, ...) must be resolved
      # at link time by linking libpython, as required for Android extension modules
      # by PEP 738.
      #
      # find_package(Python) is called above, before add_subdirectory(pybind11), so
      # pybind11 runs in FindPython mode and shares this exact detection. We therefore
      # link Python::Python -- the same FindPython target pybind11::embed itself uses
      # -- so it cannot mismatch the Python pybind11 was built against.
      target_link_libraries(touchlab_comm_py PRIVATE Python::Python)
  elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)$")
      target_link_libraries(touchlab_comm_py PRIVATE ${CMAKE_CURRENT_LIST_DIR}/lib/linux-arm64/libtouchlab_comm_static.a)
  elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64)$")
      target_link_libraries(touchlab_comm_py PRIVATE ${CMAKE_CURRENT_LIST_DIR}/lib/linux-x86/libtouchlab_comm_static.a)
  else()
      message(ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
  endif()
  message(STATUS "Linking linux library")
else()
  # If windows
  message(STATUS "Linking windows library")
  target_link_libraries(touchlab_comm_py PRIVATE ${CMAKE_CURRENT_LIST_DIR}/lib/win/touchlab_comm_static.lib)
endif()
set_property(TARGET touchlab_comm_py PROPERTY POSITION_INDEPENDENT_CODE ON)
target_compile_definitions(touchlab_comm_py PRIVATE VERSION_INFO=${TOUCHLAB_COMM_VERSION})

if (INSTALL_NATIVE)
  install(TARGETS touchlab_comm_py DESTINATION ${PROJECT_NAME})
  install(DIRECTORY examples/
        DESTINATION ${PROJECT_NAME}/examples/
      )
else()
  install(TARGETS touchlab_comm_py DESTINATION lib/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages)
  install(DIRECTORY examples/
        DESTINATION share/${PROJECT_NAME}/examples/
      )
endif()


if (MSVC)
    macro(append_cxx_flag text)
        foreach (flag
            CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
            CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)

            string(APPEND ${flag} " ${text}")

        endforeach()
    endmacro()
    append_cxx_flag("/Zc:preprocessor")
endif()
