
if(APPLE)
  add_executable(session-router session_router.swift)
  target_compile_options(session-router BEFORE PRIVATE -target x86_64-apple-macos${CMAKE_OSX_DEPLOYMENT_TARGET})
else()
  add_executable(session-router session_router.cpp)
endif()
target_link_libraries(session-router PRIVATE libsessionrouter CLI11)
enable_lto(session-router)

add_executable(session-router-cntrl session-router-cntrl.cpp utils.cpp)
target_link_libraries(session-router-cntrl PRIVATE session-router-base CLI11)
enable_lto(session-router-cntrl)

add_executable(session-router-config session-router-config.cpp)
target_link_libraries(session-router-config PRIVATE session-router-conf CLI11)
enable_lto(session-router-config)

set(exetargets session-router session-router-cntrl session-router-config)
enable_lto(${exetargets})

set(should_install ON)
set(SETCAP)

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  option(WITH_SETCAP "use setcap when installing" ON)
  if(WITH_SETCAP)
    find_program(SETCAP NAMES setcap HINTS /sbin /usr/sbin)
    if(SETCAP)
      message(STATUS "Found setcap binary: ${SETCAP}")
    else()
      message(WARNING "cannot find setcap binary you will not be able use the install targets unless you use -DWITH_SETCAP=OFF")
      set(should_install OFF)
    endif()
  endif()
endif()

# cmake interface library for bunch of cmake hacks to fix final link order
add_library(hax_and_shims_for_cmake INTERFACE)
if(WIN32)
  target_link_libraries(hax_and_shims_for_cmake INTERFACE uvw oxenmq::oxenmq -lws2_32 -lshlwapi -ldbghelp -luser32 -liphlpapi -lpsapi -luserenv)
endif()

foreach(exe ${exetargets})
  if(WIN32)
    target_sources(${exe} PRIVATE ${CMAKE_BINARY_DIR}/${exe}.rc)
    target_link_libraries(${exe} PRIVATE -static-libstdc++ -static-libgcc --static -Wl,--pic-executable,-e,mainCRTStartup,--subsystem,console:5.00)
  elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
    target_link_directories(${exe} PRIVATE /usr/local/lib)
  endif()
  if(SROUTER_STRIP)
    add_custom_command(TARGET ${exe}
      POST_BUILD
      COMMAND ${CMAKE_OBJCOPY} ARGS --only-keep-debug $<TARGET_FILE:${exe}> $<TARGET_FILE:${exe}>.debug
      COMMAND ${CMAKE_STRIP} ARGS --strip-all $<TARGET_FILE:${exe}>)
  endif()
  target_include_directories(${exe} PUBLIC "..")
  if(should_install)
    if(APPLE)
      install(TARGETS ${exe}
        BUNDLE DESTINATION "${PROJECT_BINARY_DIR}"
        RUNTIME DESTINATION "."
        COMPONENT session-router)
    else()
      install(TARGETS ${exe} RUNTIME DESTINATION bin COMPONENT session-router)
    endif()
  endif()
endforeach()

if(WITH_SETCAP)
  install(CODE "execute_process(COMMAND ${SETCAP} cap_net_admin,cap_net_bind_service=+eip ${CMAKE_INSTALL_PREFIX}/bin/session-router)")
endif()

if(SROUTER_STRIP)
  add_custom_target(symbols ALL
    COMMAND ${CMAKE_COMMAND} -E tar cJf ${CMAKE_CURRENT_BINARY_DIR}/debug-symbols.tar.xz $<TARGET_FILE:session-router>.debug
    DEPENDS session-router)
endif()

set_target_properties(${exetargets} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
