cmake_minimum_required(VERSION 3.18)
project(sing_tun C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
find_package(pybind11 CONFIG REQUIRED)
file(TO_CMAKE_PATH "${SING_TUN_GO_BUILD_DIR}" SING_TUN_GO_BUILD_DIR)
foreach(value SING_TUN_GO_BUILD_DIR SING_TUN_VERSION SING_TUN_UPSTREAM_VERSION SING_TUN_UPSTREAM_COMMIT)
  if("${${value}}" STREQUAL "")
    message(FATAL_ERROR "${value} must be supplied")
  endif()
endforeach()
pybind11_add_module(_native src/sing_tun.cpp)
target_compile_definitions(_native PRIVATE SING_TUN_VERSION="${SING_TUN_VERSION}" SING_TUN_UPSTREAM_VERSION="${SING_TUN_UPSTREAM_VERSION}" SING_TUN_UPSTREAM_COMMIT="${SING_TUN_UPSTREAM_COMMIT}")
target_include_directories(_native PRIVATE "${SING_TUN_GO_BUILD_DIR}")
target_link_libraries(_native PRIVATE "${SING_TUN_GO_BUILD_DIR}/sing_tun.a")
if(WIN32)
  if(NOT MINGW)
    message(FATAL_ERROR "Go c-archive requires MinGW or LLVM-MinGW on Windows")
  endif()
  target_compile_options(_native PRIVATE "-Wa,-mbig-obj")
  target_link_options(_native PRIVATE "-static-libgcc" "-static-libstdc++" "-Wl,-Bstatic,--whole-archive" "-lwinpthread" "-Wl,--no-whole-archive")
  target_link_libraries(_native PRIVATE ws2_32 winmm ntdll bcrypt)
else()
  find_package(Threads REQUIRED)
  target_link_libraries(_native PRIVATE Threads::Threads resolv ${CMAKE_DL_LIBS})
  if(APPLE)
    target_link_libraries(_native PRIVATE "-framework CoreFoundation" "-framework Security")
  endif()
endif()
