# pthread requires special checks and flags to be added
find_package(Threads)
if(THREADS_HAVE_PTHREAD_ARG)
  if(CZMQ_BUILD_SHARED)
    target_compile_options(czmq PUBLIC "-pthread")
  endif()
  if(CZMQ_BUILD_STATIC)
    target_compile_options(czmq-static PUBLIC "-pthread")
  endif()
endif()
if(CMAKE_THREAD_LIBS_INIT)
  if(CZMQ_BUILD_SHARED)
    target_link_libraries(czmq PUBLIC "${CMAKE_THREAD_LIBS_INIT}")
  endif()
  if(CZMQ_BUILD_STATIC)
    target_link_libraries(czmq-static PUBLIC "${CMAKE_THREAD_LIBS_INIT}")
  endif()
endif()

# at least C99 is required for zdir which uses ‘for’ loop initial declarations
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")

if(NOT CMAKE_CROSSCOMPILING AND UNIX)
  # prevent Unknown CMake command "check_c_source_runs".
  include(CheckCSourceRuns)

  message(STATUS "Checking whether SOCK_CLOEXEC is supported")
  check_c_source_runs(
    "
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char *argv [])
{
    int s = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
    return(s == -1);
}
"
    CZMQ_HAVE_SOCK_CLOEXEC)

  if(CZMQ_HAVE_SOCK_CLOEXEC)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCZMQ_HAVE_SOCK_CLOEXEC=1")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCZMQ_HAVE_SOCK_CLOEXEC=1")
  endif()
endif()

configure_file("${PROJECT_BINARY_DIR}/platform.h.in" "${PROJECT_BINARY_DIR}/platform.h")
