if(NOT ANDROID)
  if(NOT UNIX)
    return()
  endif()
endif()

include(CheckCXXSourceCompiles)
include(CheckLibraryExists)

if(WITH_JEMALLOC)
  find_package(Jemalloc REQUIRED)
  if(NOT JEMALLOC_FOUND)
    message(FATAL_ERROR "did not find jemalloc")
  endif()
  add_definitions(-DUSE_JEMALLOC)
  message(STATUS "using jemalloc")
endif()

add_definitions(-DUNIX)
add_definitions(-DPOSIX)

if (STATIC_LINK_RUNTIME OR STATIC_LINK)
  set(LIBUV_USE_STATIC ON)
endif()


option(NO_SYSTEM_LIBUV "statically compile libuv without even trying to find a system copy" OFF)
add_library(libuv INTERFACE)
if(NOT BUILD_STATIC_DEPS)
  find_package(LibUV 1.28.0)
endif()
if(LibUV_FOUND)
  message(STATUS "using system libuv")
  target_link_libraries(libuv INTERFACE ${LIBUV_LIBRARIES})
  target_include_directories(libuv INTERFACE ${LIBUV_INCLUDE_DIRS})
else()
  message(STATUS "using libuv submodule")
  add_subdirectory(${PROJECT_SOURCE_DIR}/external/libuv)
  target_link_libraries(libuv INTERFACE uv_a)
endif()

if(EMBEDDED_CFG OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  link_libatomic()
endif()

if (${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
  add_definitions(-D_BSD_SOURCE)
  add_definitions(-D_GNU_SOURCE)
  add_definitions(-D_XOPEN_SOURCE=700)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  if (LIBUV_USE_STATIC)
    link_libraries(-lkstat -lsendfile)
  endif()
endif()
