CMakeLists.txt 43.2 KB
Newer Older
1 2
# CMake build script for ZeroMQ

3 4 5
cmake_minimum_required (VERSION 2.8.12)
project (ZeroMQ)

6
list (INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}")
7 8
set (ZMQ_CMAKE_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/Modules)
list (APPEND CMAKE_MODULE_PATH ${ZMQ_CMAKE_MODULES_DIR})
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# Apply CMP0042: MACOSX_RPATH is enabled by default
cmake_policy (SET CMP0042 NEW)

include(GNUInstallDirs)
if (${CMAKE_SYSTEM_NAME} STREQUAL Darwin)
  # Find more information: https://cmake.org/Wiki/CMake_RPATH_handling

  # Add an install rpath if it is not a system directory
  list (FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" isSystemDir)
  if ("${isSystemDir}" STREQUAL "-1")
    set (CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
  endif ()

  # Add linker search paths pointing to external dependencies
  set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif ()

27 28 29
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=gnu++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX11)
30
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
31 32 33 34
endif()
include(CheckCCompilerFlag)
CHECK_C_COMPILER_FLAG("-std=gnu11" COMPILER_SUPPORTS_C11)
if(COMPILER_SUPPORTS_C11)
35
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
36 37
endif()

38 39 40
# Will be used to add flags to pkg-config useful when apps want to statically link
set (pkg_config_libs_private "")

41 42 43 44
option (WITH_OPENPGM "Build with support for OpenPGM" OFF)
option (WITH_VMCI "Build with support for VMware VMCI socket" OFF)

if (APPLE)
45
    option (ZMQ_BUILD_FRAMEWORK "Build as OS X framework" OFF)
46 47 48 49 50 51 52 53 54 55 56 57
endif ()

# Select curve encryption library, defaults to tweetnacl
# To use libsodium instead, use --with-libsodium (must be installed)
# To disable curve, use --disable-curve

option (WITH_LIBSODIUM "Use libsodium instead of built-in tweetnacl" OFF)
option (ENABLE_CURVE "Enable CURVE security" ON)

if (NOT ENABLE_CURVE)
    message (STATUS "CURVE security is disabled")

58
elseif (WITH_LIBSODIUM)
59 60 61 62
    find_package (Sodium)
    if (SODIUM_FOUND)
        message (STATUS "Using libsodium for CURVE security")
        include_directories (${SODIUM_INCLUDE_DIRS})
63
        set (ZMQ_USE_LIBSODIUM 1)
64
        set (ZMQ_HAVE_CURVE 1)
65
        set (pkg_config_libs_private "${pkg_config_libs_private} -lsodium")
66 67 68 69 70 71 72
    else ()
        message (FATAL_ERROR
            "libsodium is not installed. Install it, then run CMake again")
    endif ()

else ()
    message (STATUS "Using tweetnacl for CURVE security")
73 74 75
    list (APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/src/tweetnacl.c)
    set (ZMQ_USE_TWEETNACL 1)
    set (ZMQ_HAVE_CURVE 1)
76 77
endif ()

78 79 80 81 82 83 84 85 86 87 88 89 90 91
set(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

if (EXISTS "${SOURCE_DIR}/.git")
    OPTION (ENABLE_DRAFTS "Build and install draft classes and methods" ON)
else ()
    OPTION (ENABLE_DRAFTS "Build and install draft classes and methods" OFF)
endif ()
IF (ENABLE_DRAFTS)
    ADD_DEFINITIONS (-DZMQ_BUILD_DRAFT_API)
    set (pkg_config_defines "-DZMQ_BUILD_DRAFT_API=1")
ELSE (ENABLE_DRAFTS)
    set (pkg_config_defines "")
ENDIF (ENABLE_DRAFTS)

92 93 94 95 96
option (WITH_MILITANT "Enable militant assertions" OFF)
if (WITH_MILITANT)
    add_definitions(-DZMQ_ACT_MILITANT)
endif()

97 98 99 100
set (API_POLLER "" CACHE STRING "Choose polling system for zmq_poll(er)_*. valid values are
                            poll or select [default=poll unless POLLER=select]")

set (POLLER "" CACHE STRING "Choose polling system for I/O threads. valid values are
101
                            kqueue, epoll, devpoll, pollset, poll or select [default=autodetect]")
102

103 104 105
include (CheckFunctionExists)
include (CheckTypeSize)

106 107 108 109 110 111 112 113 114
if (NOT MSVC)
    if (POLLER STREQUAL "")
        set (CMAKE_REQUIRED_INCLUDES sys/event.h)
        check_function_exists (kqueue HAVE_KQUEUE)
        set (CMAKE_REQUIRED_INCLUDES)
        if (HAVE_KQUEUE)
            set (POLLER "kqueue")
        endif()
    endif ()
115

116 117 118 119 120 121 122 123 124 125
    if (POLLER STREQUAL "")
        set (CMAKE_REQUIRED_INCLUDES sys/epoll.h)
        check_function_exists (epoll_create HAVE_EPOLL)
        set (CMAKE_REQUIRED_INCLUDES)
        if (HAVE_EPOLL)
            set (POLLER "epoll")
            check_function_exists (epoll_create1 HAVE_EPOLL_CLOEXEC)
            if (HAVE_EPOLL_CLOEXEC)
                set (ZMQ_IOTHREAD_POLLER_USE_EPOLL_CLOEXEC 1)
            endif ()
126
        endif ()
127 128
    endif ()

129 130 131 132 133 134 135
    if (POLLER STREQUAL "")
        set (CMAKE_REQUIRED_INCLUDES sys/devpoll.h)
        check_type_size ("struct pollfd" DEVPOLL)
        set (CMAKE_REQUIRED_INCLUDES)
        if (HAVE_DEVPOLL)
            set (POLLER "devpoll")
        endif ()
136 137
    endif ()

138 139 140 141 142 143 144 145
    if (POLLER STREQUAL "")
        set (CMAKE_REQUIRED_INCLUDES sys/pollset.h)
        check_function_exists (pollset_create HAVE_POLLSET)
        set (CMAKE_REQUIRED_INCLUDES)
        if (HAVE_POLLSET)
            set (POLLER "pollset")
        endif()
    endif ()
146

147 148 149 150 151 152 153
    if (POLLER STREQUAL "")
        set (CMAKE_REQUIRED_INCLUDES poll.h)
        check_function_exists (poll HAVE_POLL)
        set (CMAKE_REQUIRED_INCLUDES)
        if (HAVE_POLL)
            set (POLLER "poll")
        endif ()
154
    endif ()
155
endif()
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
if (POLLER STREQUAL "")
    if (WIN32)
        set (CMAKE_REQUIRED_INCLUDES winsock2.h)
        set (HAVE_SELECT 1)
    else ()
        set (CMAKE_REQUIRED_INCLUDES sys/select.h)
        check_function_exists (select HAVE_SELECT)
        set (CMAKE_REQUIRED_INCLUDES)
    endif ()
    if (HAVE_SELECT)
        set (POLLER "select")
    else ()
        message (FATAL_ERROR
            "Could not autodetect polling method")
    endif ()
endif ()
173

174 175 176
if (POLLER STREQUAL "kqueue"
 OR POLLER STREQUAL "epoll"
 OR POLLER STREQUAL "devpoll"
177
 OR POLLER STREQUAL "pollset"
178 179
 OR POLLER STREQUAL "poll"
 OR POLLER STREQUAL "select")
180
    message (STATUS "Using polling method in I/O threads: ${POLLER}")
181
    string (TOUPPER ${POLLER} UPPER_POLLER)
182
    set (ZMQ_IOTHREAD_POLLER_USE_${UPPER_POLLER} 1)
183 184 185
else ()
    message (FATAL_ERROR "Invalid polling method")
endif ()
186

187 188 189 190 191
if (POLLER STREQUAL "epoll" AND WIN32)
    message (STATUS "Including wepoll")
    list (APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/external/wepoll/wepoll.c ${CMAKE_CURRENT_SOURCE_DIR}/external/wepoll/wepoll.h)
endif()

192 193 194 195 196 197 198 199 200 201 202 203
if (API_POLLER STREQUAL "")
	if (POLLER STREQUAL "select")
		set (API_POLLER "select")
	else()
		set (API_POLLER "poll")
	endif()
endif()

message (STATUS "Using polling method in zmq_poll(er)_* API: ${API_POLLER}")
    string (TOUPPER ${API_POLLER} UPPER_API_POLLER)
set (ZMQ_POLL_BASED_ON_${UPPER_API_POLLER} 1)

204
include (TestZMQVersion)
205 206 207
if (NOT CMAKE_CROSSCOMPILING)
  include (ZMQSourceRunChecks)
endif ()
208 209 210 211 212 213 214 215
include (CheckIncludeFiles)
include (CheckLibraryExists)
include (CheckCCompilerFlag)
include (CheckCXXCompilerFlag)
include (CheckCSourceCompiles)
include (CheckCSourceRuns)
include (CMakeDependentOption)
include (CheckCXXSymbolExists)
216
include (CheckSymbolExists)
217

218
check_include_files (windows.h ZMQ_HAVE_WINDOWS)
219
if(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore" AND CMAKE_SYSTEM_VERSION STREQUAL "10.0")
220 221 222
  SET(ZMQ_HAVE_WINDOWS_UWP ON)
  ADD_DEFINITIONS(-D_WIN32_WINNT=_WIN32_WINNT_WIN10)
endif()
223 224 225 226 227 228 229 230
if (NOT MSVC)
    check_include_files (ifaddrs.h ZMQ_HAVE_IFADDRS)
    check_include_files (sys/uio.h ZMQ_HAVE_UIO)
    check_include_files (sys/eventfd.h ZMQ_HAVE_EVENTFD)
    if (ZMQ_HAVE_EVENTFD AND NOT CMAKE_CROSSCOMPILING)
      zmq_check_efd_cloexec ()
    endif ()
endif()
231

232 233 234
if (ZMQ_HAVE_WINDOWS)
  # Cannot use check_library_exists because the symbol is always declared as char(*)(void)
  set(CMAKE_REQUIRED_LIBRARIES "ws2_32.lib")
235
  check_symbol_exists (WSAStartup "winsock2.h" HAVE_WS2_32)
236 237

  set(CMAKE_REQUIRED_LIBRARIES "rpcrt4.lib")
238
  check_symbol_exists (UuidCreateSequential "rpc.h" HAVE_RPCRT4)
239 240

  set(CMAKE_REQUIRED_LIBRARIES "iphlpapi.lib")
241
  check_symbol_exists (GetAdaptersAddresses "winsock2.h;iphlpapi.h" HAVE_IPHLAPI)
242 243 244 245 246 247 248 249

  set(CMAKE_REQUIRED_LIBRARIES "")
  # TODO: This not the symbol we're looking for. What is the symbol?
  check_library_exists (ws2 fopen "" HAVE_WS2)
else()
  check_cxx_symbol_exists (SO_PEERCRED sys/socket.h ZMQ_HAVE_SO_PEERCRED)
  check_cxx_symbol_exists (LOCAL_PEERCRED sys/socket.h ZMQ_HAVE_LOCAL_PEERCRED)
endif()
250

251
find_library (RT_LIBRARY rt)
252

253
find_package (Threads)
Matt Arsenault's avatar
Matt Arsenault committed
254

255

256 257 258 259 260 261 262 263
if (WIN32 AND NOT CYGWIN)
  if (NOT HAVE_WS2_32 AND NOT HAVE_WS2)
    message (FATAL_ERROR "Cannot link to ws2_32 or ws2")
  endif ()

  if (NOT HAVE_RPCRT4)
    message (FATAL_ERROR "Cannot link to rpcrt4")
  endif ()
264

265 266 267 268
  if (NOT HAVE_IPHLAPI)
    message (FATAL_ERROR "Cannot link to iphlapi")
  endif ()
endif ()
269

270 271 272 273
if (NOT MSVC)
    set (CMAKE_REQUIRED_LIBRARIES rt)
    check_function_exists (clock_gettime HAVE_CLOCK_GETTIME)
    set (CMAKE_REQUIRED_LIBRARIES)
274

275 276 277
    set (CMAKE_REQUIRED_INCLUDES unistd.h)
    check_function_exists (fork HAVE_FORK)
    set (CMAKE_REQUIRED_INCLUDES)
Frank's avatar
Frank committed
278

279 280 281
    set (CMAKE_REQUIRED_INCLUDES sys/time.h)
    check_function_exists (gethrtime HAVE_GETHRTIME)
    set (CMAKE_REQUIRED_INCLUDES)
282

283 284 285
    set (CMAKE_REQUIRED_INCLUDES stdlib.h)
    check_function_exists (mkdtemp HAVE_MKDTEMP)
    set (CMAKE_REQUIRED_INCLUDES)
286

287 288 289 290
    set (CMAKE_REQUIRED_INCLUDES sys/socket.h)
    check_function_exists (accept4 HAVE_ACCEPT4)
    set (CMAKE_REQUIRED_INCLUDES)
endif()
291

292
add_definitions (-D_REENTRANT -D_THREAD_SAFE)
293
add_definitions (-DZMQ_CUSTOM_PLATFORM_HPP)
294

295
option (ENABLE_EVENTFD "Enable/disable eventfd" ZMQ_HAVE_EVENTFD)
296

297 298
macro (zmq_check_cxx_flag_prepend flag)
  check_cxx_compiler_flag ("${flag}" HAVE_FLAG_${flag})
299

300 301 302 303
  if (HAVE_FLAG_${flag})
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
  endif ()
endmacro ()
304

305
OPTION (ENABLE_ANALYSIS "Build with static analysis (make take very long)" OFF)
306

307
if (MSVC)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  if (ENABLE_ANALYSIS)  
    zmq_check_cxx_flag_prepend ("/W4")

    zmq_check_cxx_flag_prepend ("/analyze")
    
    # C++11/14/17-specific, but maybe possible via conditional defines
    zmq_check_cxx_flag_prepend ("/wd26440") # Function '...' can be declared 'noexcept' 
    zmq_check_cxx_flag_prepend ("/wd26432") # If you define or delete any default operation in the type '...', define or delete them all
    zmq_check_cxx_flag_prepend ("/wd26439") # This kind of function may not throw. Declare it 'noexcept' 
    zmq_check_cxx_flag_prepend ("/wd26447") # The function is declared 'noexcept' but calls function '...' which may throw exceptions
    zmq_check_cxx_flag_prepend ("/wd26433") # Function '...' should be marked with 'override'
    zmq_check_cxx_flag_prepend ("/wd26409") # Avoid calling new and delete explicitly, use std::make_unique<T> instead
    # Requires GSL
    zmq_check_cxx_flag_prepend ("/wd26429") # Symbol '...' is never tested for nullness, it can be marked as not_null
    zmq_check_cxx_flag_prepend ("/wd26446") # Prefer to use gsl::at()
    zmq_check_cxx_flag_prepend ("/wd26481") # Don't use pointer arithmetic. Use span instead
    zmq_check_cxx_flag_prepend ("/wd26472") # Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narow
    zmq_check_cxx_flag_prepend ("/wd26448") # Consider using gsl::finally if final action is intended
    zmq_check_cxx_flag_prepend ("/wd26400") # Do not assign the result of an allocation or a function call with an owner<T> return value to a raw pointer, use owner<T> instead    
    zmq_check_cxx_flag_prepend ("/wd26485") # Expression '...': No array to pointer decay (bounds.3)
  else()
    zmq_check_cxx_flag_prepend ("/W3")
  endif()
331

332 333 334 335 336 337 338 339
  if (MSVC_IDE)
    set (MSVC_TOOLSET "-${CMAKE_VS_PLATFORM_TOOLSET}")
  else ()
    set (MSVC_TOOLSET "")
  endif ()
else ()
  zmq_check_cxx_flag_prepend ("-Wall")
endif ()
340

341 342 343
if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  zmq_check_cxx_flag_prepend ("-Wextra")
endif ()
344

345 346
option (LIBZMQ_PEDANTIC "" ON)
option (LIBZMQ_WERROR "" OFF)
347

348 349 350 351
#   TODO: why is -Wno-long-long defined differently than in configure.ac?
if (NOT MSVC)
    zmq_check_cxx_flag_prepend ("-Wno-long-long")
    zmq_check_cxx_flag_prepend ("-Wno-uninitialized")
352

353 354
    if (LIBZMQ_PEDANTIC)
      zmq_check_cxx_flag_prepend ("-pedantic")
355

356 357 358 359 360 361 362 363 364
      if (${CMAKE_CXX_COMPILER_ID} MATCHES "Intel")
        zmq_check_cxx_flag_prepend ("-strict-ansi")
      endif ()

      if (${CMAKE_CXX_COMPILER_ID} MATCHES "SunPro")
        zmq_check_cxx_flag_prepend ("-compat=5")
      endif ()
    endif ()
endif()
365

366
if (LIBZMQ_WERROR)
367 368 369 370 371 372 373 374
    if(MSVC)
        zmq_check_cxx_flag_prepend ("/WX")
    else()
        zmq_check_cxx_flag_prepend ("-Werror")
        if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
            zmq_check_cxx_flag_prepend ("-errwarn=%all")
        endif()
    endif()
375
endif ()
376

377 378 379
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^sparc")
  zmq_check_cxx_flag_prepend ("-mcpu=v9")
endif ()
380

381 382 383
if (${CMAKE_CXX_COMPILER_ID} MATCHES "SunPro")
  zmq_check_cxx_flag_prepend ("-features=zla")
endif ()
384

385

386 387 388
if (CMAKE_SYSTEM_NAME MATCHES "SunOS" OR CMAKE_SYSTEM_NAME MATCHES "NetBSD")
  message (STATUS "Checking whether atomic operations can be used")
  check_c_source_compiles (
389 390 391
  "
   #include <atomic.h>

392
    int main ()
393 394
    {
      uint32_t value;
395
      atomic_cas_32 (&value, 0, 0);
396 397 398 399 400
      return 0;
    }
    "
    HAVE_ATOMIC_H)

401 402 403 404
  if (NOT HAVE_ATOMIC_H)
    set (ZMQ_FORCE_MUTEXES 1)
  endif ()
endif ()
405 406 407


#-----------------------------------------------------------------------------
408
if (NOT CMAKE_CROSSCOMPILING AND NOT MSVC)
409
  zmq_check_sock_cloexec ()
410
  zmq_check_o_cloexec ()
411
  zmq_check_so_bindtodevice ()
412 413 414 415 416 417 418
  zmq_check_so_keepalive ()
  zmq_check_tcp_keepcnt ()
  zmq_check_tcp_keepidle ()
  zmq_check_tcp_keepintvl ()
  zmq_check_tcp_keepalive ()
  zmq_check_tcp_tipc ()
  zmq_check_pthread_setname ()
419
  zmq_check_pthread_setaffinity ()
420
  zmq_check_getrandom ()
421
endif ()
422

423
if (    CMAKE_SYSTEM_NAME MATCHES "Linux"
424 425 426
    OR CMAKE_SYSTEM_NAME MATCHES "GNU/kFreeBSD"
    OR CMAKE_SYSTEM_NAME MATCHES "GNU/Hurd"
    OR CYGWIN)
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
  add_definitions (-D_GNU_SOURCE)
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  add_definitions (-D__BSD_VISIBLE)
elseif (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
  add_definitions (-D_NETBSD_SOURCE)
elseif (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
  add_definitions (-D_OPENBSD_SOURCE)
elseif (CMAKE_SYSTEM_NAME MATCHES "SunOS")
  add_definitions (-D_PTHREADS)
elseif (CMAKE_SYSTEM_NAME MATCHES "HP-UX")
  add_definitions (-D_POSIX_C_SOURCE=200112L)
  zmq_check_cxx_flag_prepend (-Ae)
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
  add_definitions (-D_DARWIN_C_SOURCE)
endif ()

set (CMAKE_PYTHON_VERSION 2.7 2.6 2.5 2.4)
find_package (PythonInterp)
find_package (AsciiDoc)

cmake_dependent_option (WITH_DOC "Build Reference Guide documentation (requires DocBook)" ON
448
                       "PYTHONINTERP_FOUND;ASCIIDOC_FOUND" OFF)
449

450 451 452 453 454 455 456 457
if (MSVC)
  if (WITH_OPENPGM)
    #   set (OPENPGM_ROOT "" CACHE PATH "Location of OpenPGM")
    set (OPENPGM_VERSION_MAJOR 5)
    set (OPENPGM_VERSION_MINOR 2)
    set (OPENPGM_VERSION_MICRO 122)
    if (CMAKE_CL_64)
      find_path (OPENPGM_ROOT include/pgm/pgm.h
458 459 460
                PATHS
                "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Miru\\OpenPGM ${OPENPGM_VERSION_MAJOR}.${OPENPGM_VERSION_MINOR}.${OPENPGM_VERSION_MICRO}]"
                NO_DEFAULT_PATH
461 462 463 464
               )
      message (STATUS "OpenPGM x64 detected - ${OPENPGM_ROOT}")
    else ()
      find_path (OPENPGM_ROOT include/pgm/pgm.h
465 466 467 468
                PATHS
                "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Miru\\OpenPGM ${OPENPGM_VERSION_MAJOR}.${OPENPGM_VERSION_MINOR}.${OPENPGM_VERSION_MICRO}]"
                "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Miru\\OpenPGM ${OPENPGM_VERSION_MAJOR}.${OPENPGM_VERSION_MINOR}.${OPENPGM_VERSION_MICRO}]"
                NO_DEFAULT_PATH
469 470 471 472 473 474
               )
      message (STATUS "OpenPGM x86 detected - ${OPENPGM_ROOT}")
    endif (CMAKE_CL_64)
    set (OPENPGM_INCLUDE_DIRS ${OPENPGM_ROOT}/include)
    set (OPENPGM_LIBRARY_DIRS ${OPENPGM_ROOT}/lib)
    set (OPENPGM_LIBRARIES
475 476
      optimized libpgm${MSVC_TOOLSET}-mt-${OPENPGM_VERSION_MAJOR}_${OPENPGM_VERSION_MINOR}_${OPENPGM_VERSION_MICRO}.lib
      debug libpgm${MSVC_TOOLSET}-mt-gd-${OPENPGM_VERSION_MAJOR}_${OPENPGM_VERSION_MINOR}_${OPENPGM_VERSION_MICRO}.lib)
477 478 479
  endif ()
else ()
  if (WITH_OPENPGM)
480
    #  message (FATAL_ERROR "WITH_OPENPGM not implemented")
481

482 483 484
    if (NOT OPENPGM_PKGCONFIG_NAME)
      SET (OPENPGM_PKGCONFIG_NAME "openpgm-5.2")
    endif(NOT OPENPGM_PKGCONFIG_NAME)
485 486

    SET (OPENPGM_PKGCONFIG_NAME ${OPENPGM_PKGCONFIG_NAME} CACHE STRING
487 488
      "Name pkg-config shall use to find openpgm libraries and include paths"
      FORCE )
489

490 491 492 493 494 495 496 497 498 499
    find_package(PkgConfig)
    pkg_check_modules (OPENPGM  ${OPENPGM_PKGCONFIG_NAME})

    if (OPENPGM_FOUND)
        message (STATUS ${OPENPGM_PKGCONFIG_NAME}" found")
    else ()
        message (FATAL_ERROR
            ${OPENPGM_PKGCONFIG_NAME}"  not found. openpgm is searchd via `pkg-config ${OPENPGM_PKGCONFIG_NAME}`. Consider providing a valid OPENPGM_PKGCONFIG_NAME")
    endif ()

500
    # DSO symbol visibility for openpgm
501
    if (HAVE_FLAG_VISIBILITY_HIDDEN)
502

503
    elseif (HAVE_FLAG_LDSCOPE_HIDDEN)
504

505 506 507
    endif ()
  endif ()
endif ()
508

509 510 511 512

#-----------------------------------------------------------------------------
# force off-tree build

513 514
if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
message (FATAL_ERROR "CMake generation is not allowed within the source directory!
515 516
Remove the CMakeCache.txt file and try again from another folder, e.g.:

517
   rm CMakeCache.txt
518 519 520 521
   mkdir cmake-make
   cd cmake-make
   cmake ..
")
522
endif ()
523 524 525 526

#-----------------------------------------------------------------------------
# default to Release build

527
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
528 529
  # CMAKE_BUILD_TYPE is not used for multi-configuration generators like Visual Studio/XCode
  # which instead use CMAKE_CONFIGURATION_TYPES
530
  set (CMAKE_BUILD_TYPE Release CACHE STRING
531 532
      "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
      FORCE)
533
endif ()
534

535 536
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/bin)
set (LIBRARY_OUTPUT_PATH  ${CMAKE_CURRENT_BINARY_DIR}/lib)
Doron Somech's avatar
Doron Somech committed
537

538 539 540
#-----------------------------------------------------------------------------
# platform specifics

xantares's avatar
xantares committed
541
if (WIN32)
542 543
    #   Socket limit is 16K (can be raised arbitrarily)
    add_definitions (-DFD_SETSIZE=16384)
544
    add_definitions (-D_CRT_SECURE_NO_WARNINGS)
545
    add_definitions (-D_WINSOCK_DEPRECATED_NO_WARNINGS)
xantares's avatar
xantares committed
546
endif ()
547

548
if (MSVC)
549
  # Parallel make.
550
  set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
551
  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
552

553 554 555
  # Compile the static lib with debug information included
  string (REGEX REPLACE "/Z." "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")

556 557
  # Optimization flags.
  # http://msdn.microsoft.com/en-us/magazine/cc301698.aspx
558 559 560 561 562 563 564
  if (NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL")
    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LTCG")
    set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LTCG")
    set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /LTCG")
  endif ()
endif ()
565 566


567 568 569
#-----------------------------------------------------------------------------
# source files

570
set (cxx-sources
571
        precompiled.cpp
572
        address.cpp
somdoron's avatar
somdoron committed
573
        client.cpp
574 575
        clock.cpp
        ctx.cpp
576
        curve_mechanism_base.cpp
577 578
        curve_client.cpp
        curve_server.cpp
579 580
        dealer.cpp
        devpoll.cpp
581
        dgram.cpp
582 583 584 585 586 587 588 589 590 591 592 593 594
        dist.cpp
        epoll.cpp
        err.cpp
        fq.cpp
        io_object.cpp
        io_thread.cpp
        ip.cpp
        ipc_address.cpp
        ipc_connecter.cpp
        ipc_listener.cpp
        kqueue.cpp
        lb.cpp
        mailbox.cpp
595
        mailbox_safe.cpp
596
        mechanism.cpp
597
        mechanism_base.cpp
Frank's avatar
Frank committed
598
        metadata.cpp
599 600 601 602 603
        msg.cpp
        mtrie.cpp
        object.cpp
        options.cpp
        own.cpp
604
        null_mechanism.cpp
605 606 607 608 609
        pair.cpp
        pgm_receiver.cpp
        pgm_sender.cpp
        pgm_socket.cpp
        pipe.cpp
610 611
        plain_client.cpp
        plain_server.cpp
612 613
        poll.cpp
        poller_base.cpp
614
        pollset.cpp
615 616 617 618 619 620 621 622 623 624 625 626
        proxy.cpp
        pub.cpp
        pull.cpp
        push.cpp
        random.cpp
        raw_encoder.cpp
        raw_decoder.cpp
        reaper.cpp
        rep.cpp
        req.cpp
        router.cpp
        select.cpp
somdoron's avatar
somdoron committed
627
        server.cpp
628 629 630
        session_base.cpp
        signaler.cpp
        socket_base.cpp
Richard Newton's avatar
Richard Newton committed
631 632
        socks.cpp
        socks_connecter.cpp
Richard Newton's avatar
Richard Newton committed
633
        stream.cpp
634 635 636 637 638 639 640 641 642 643
        stream_engine.cpp
        sub.cpp
        tcp.cpp
        tcp_address.cpp
        tcp_connecter.cpp
        tcp_listener.cpp
        thread.cpp
        trie.cpp
        v1_decoder.cpp
        v1_encoder.cpp
644 645
        v2_decoder.cpp
        v2_encoder.cpp
646 647 648
        xpub.cpp
        xsub.cpp
        zmq.cpp
Jens Auer's avatar
Jens Auer committed
649
        zmq_utils.cpp
650
        decoder_allocators.cpp
651
        socket_poller.cpp
somdoron's avatar
somdoron committed
652
        timers.cpp
somdoron's avatar
somdoron committed
653 654
        config.hpp
        radio.cpp
655 656
        dish.cpp
        udp_engine.cpp
somdoron's avatar
somdoron committed
657 658
        udp_address.cpp
        scatter.cpp
659
        gather.cpp
660
	ip_resolver.cpp
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
		zap_client.cpp
		# at least for VS, the header files must also be listed
		address.hpp
		array.hpp
		atomic_counter.hpp
		atomic_ptr.hpp
		blob.hpp
		client.hpp
		clock.hpp
		command.hpp
		condition_variable.hpp
		config.hpp
		ctx.hpp
		curve_client.hpp
		curve_client_tools.hpp
		curve_mechanism_base.hpp
		curve_server.hpp
		dbuffer.hpp
		dealer.hpp
		decoder.hpp
		decoder_allocators.hpp
		devpoll.hpp
		dgram.hpp
		dish.hpp
		dist.hpp
		encoder.hpp
		epoll.hpp
		err.hpp
		fd.hpp
		fq.hpp
		gather.hpp
692 693
		generic_mtrie.hpp
		generic_mtrie_impl.hpp
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
		gssapi_client.hpp
		gssapi_mechanism_base.hpp
		gssapi_server.hpp
		i_decoder.hpp
		i_encoder.hpp
		i_engine.hpp
		i_mailbox.hpp
		i_poll_events.hpp
		io_object.hpp
		io_thread.hpp
		ip.hpp
		ipc_address.hpp
		ipc_connecter.hpp
		ipc_listener.hpp
		kqueue.hpp
		lb.hpp
		likely.hpp
		macros.hpp
		mailbox.hpp
		mailbox_safe.hpp
		mechanism.hpp
		mechanism_base.hpp
		metadata.hpp
		msg.hpp
		mtrie.hpp
		mutex.hpp
		norm_engine.hpp
		null_mechanism.hpp
		object.hpp
		options.hpp
		own.hpp
		pair.hpp
		pgm_receiver.hpp
		pgm_sender.hpp
		pgm_socket.hpp
		pipe.hpp
		plain_client.hpp
		plain_server.hpp
		poll.hpp
		poller.hpp
		poller_base.hpp
		pollset.hpp
		precompiled.hpp
		proxy.hpp
		pub.hpp
		pull.hpp
		push.hpp
		radio.hpp
		random.hpp
		raw_decoder.hpp
		raw_encoder.hpp
		reaper.hpp
		rep.hpp
		req.hpp
		router.hpp
		scatter.hpp
		select.hpp
		server.hpp
		session_base.hpp
		signaler.hpp
		socket_base.hpp
		socket_poller.hpp
		socks.hpp
		socks_connecter.hpp
		stdint.hpp
		stream.hpp
		stream_engine.hpp
		sub.hpp
		tcp.hpp
		tcp_address.hpp
		tcp_connecter.hpp
		tcp_listener.hpp
		thread.hpp
		timers.hpp
		tipc_address.hpp
		tipc_connecter.hpp
		tipc_listener.hpp
		trie.hpp
		udp_address.hpp
		udp_engine.hpp
		v1_decoder.hpp
		v1_encoder.hpp
		v2_decoder.hpp
		v2_encoder.hpp
		v2_protocol.hpp
		vmci.hpp
		vmci_address.hpp
		vmci_connecter.hpp
		vmci_listener.hpp
		windows.hpp
		wire.hpp
		xpub.hpp
		xsub.hpp
		ypipe.hpp
		ypipe_base.hpp
		ypipe_conflate.hpp
		yqueue.hpp
		zap_client.hpp
		)
Matt Arsenault's avatar
Matt Arsenault committed
793

794
if (MINGW)
Matt Arsenault's avatar
Matt Arsenault committed
795
  # Generate the right type when using -m32 or -m64
796 797 798 799
  macro (set_rc_arch rc_target)
    set (CMAKE_RC_COMPILER_INIT windres)
    enable_language (RC)
    set (CMAKE_RC_COMPILE_OBJECT
Maarten Ditzel's avatar
Maarten Ditzel committed
800
        "<CMAKE_RC_COMPILER> <FLAGS> -O coff --target=${rc_target} <DEFINES> -i <SOURCE> -o <OBJECT>")
801
  endmacro ()
Matt Arsenault's avatar
Matt Arsenault committed
802

803 804
  if (NOT CMAKE_SYSTEM_PROCESSOR)
    set (CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
805 806
  endif ()

807
  if (    CMAKE_SYSTEM_PROCESSOR MATCHES "i386"
808 809 810
      OR CMAKE_SYSTEM_PROCESSOR MATCHES "i486"
      OR CMAKE_SYSTEM_PROCESSOR MATCHES "i586"
      OR CMAKE_SYSTEM_PROCESSOR MATCHES "i686"
Matt Arsenault's avatar
Matt Arsenault committed
811
     # This also happens on x86_64 systems...what a worthless variable
812 813 814
      OR CMAKE_SYSTEM_PROCESSOR MATCHES "x86"
      OR CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64"
      OR CMAKE_SYSTEM_PROCESSOR MATCHES "amd64")
Matt Arsenault's avatar
Matt Arsenault committed
815

816 817 818 819 820 821 822
    if (CMAKE_SIZEOF_VOID_P EQUAL 8)
      set_rc_arch ("pe-x86-64")
    else ()
      set_rc_arch ("pe-i386")
    endif ()
  endif ()
endif ()
Matt Arsenault's avatar
Matt Arsenault committed
823

Min RK's avatar
Min RK committed
824 825
set (public_headers include/zmq.h
                    include/zmq_utils.h)
Matt Arsenault's avatar
Matt Arsenault committed
826

827
set (readme-docs AUTHORS
Matt Arsenault's avatar
Matt Arsenault committed
828 829
                COPYING
                COPYING.LESSER
830
                NEWS)
831 832 833 834

#-----------------------------------------------------------------------------
# optional modules

835 836 837 838 839 840
if (WITH_OPENPGM)
  add_definitions (-DZMQ_HAVE_OPENPGM)
  include_directories (${OPENPGM_INCLUDE_DIRS})
  link_directories (${OPENPGM_LIBRARY_DIRS})
  set (OPTIONAL_LIBRARIES ${OPENPGM_LIBRARIES})
endif (WITH_OPENPGM)
841

842 843 844 845 846
if (WITH_VMCI)
    add_definitions (-DZMQ_HAVE_VMCI)
    include_directories (${VMCI_INCLUDE_DIRS})
    list (APPEND cxx-sources vmci_address.cpp vmci_connecter.cpp vmci_listener.cpp vmci.cpp)
endif (WITH_VMCI)
Ilya Kulakov's avatar
Ilya Kulakov committed
847

848 849
if (ZMQ_HAVE_TIPC)
    add_definitions (-DZMQ_HAVE_TIPC)
850
    list (APPEND cxx-sources tipc_address.cpp tipc_connecter.cpp tipc_listener.cpp)
851 852
endif (ZMQ_HAVE_TIPC)

853 854 855
#-----------------------------------------------------------------------------
# source generators

856 857 858
foreach (source ${cxx-sources})
  list (APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/src/${source})
endforeach ()
859

860
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/src/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
Frank's avatar
Frank committed
861

862 863 864
#   Delete any src/platform.hpp left by configure
file (REMOVE ${CMAKE_CURRENT_SOURCE_DIR}/src/platform.hpp)

865 866
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/platform.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/platform.hpp)
list (APPEND sources ${CMAKE_CURRENT_BINARY_DIR}/platform.hpp)
867

868 869
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/src/libzmq.pc.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/libzmq.pc @ONLY)
set (zmq-pkgconfig ${CMAKE_CURRENT_BINARY_DIR}/libzmq.pc)
870

871 872 873
if (NOT ZMQ_BUILD_FRAMEWORK)
  install (FILES ${CMAKE_CURRENT_BINARY_DIR}/libzmq.pc DESTINATION lib/pkgconfig)
endif ()
874

875 876 877 878 879 880
if (MSVC)
  if (CMAKE_CL_64)
    set (nsis-template ${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/NSIS.template64.in)
  else ()
    set (nsis-template ${CMAKE_CURRENT_SOURCE_DIR}/builds/cmake/NSIS.template32.in)
  endif ()
881

882
  add_custom_command (
Matt Arsenault's avatar
Matt Arsenault committed
883 884 885 886 887 888 889
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/NSIS.template.in
    COMMAND ${CMAKE_COMMAND}
    ARGS -E
    copy
    ${nsis-template}
    ${CMAKE_CURRENT_BINARY_DIR}/NSIS.template.in
    DEPENDS ${nsis-template})
890 891 892 893 894 895
endif ()

file (MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc)
file (GLOB docs RELATIVE ${CMAKE_CURRENT_BINARY_DIR}/ "${CMAKE_CURRENT_SOURCE_DIR}/doc/*.txt")
set (html-docs)
foreach (txt ${docs})
896
  string (REGEX REPLACE ".*/(.*)\\.txt" "\\1.html" html ${txt})
897 898 899
  set (src ${txt})
  set (dst doc/${html})
  if (WITH_DOC)
Yann Diorcet's avatar
Yann Diorcet committed
900 901 902 903 904 905 906 907 908 909 910 911
    add_custom_command (
      OUTPUT  ${dst}
      COMMAND ${ASCIIDOC_EXECUTABLE}
      -d manpage
      -b xhtml11
      -f ${CMAKE_CURRENT_SOURCE_DIR}/doc/asciidoc.conf
      -azmq_version=${ZMQ_VERSION}
      -o ${dst}
      ${src}
      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${src}
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
      COMMENT "Generating ${html}")
912 913 914
    list (APPEND html-docs ${CMAKE_CURRENT_BINARY_DIR}/${dst})
  endif ()
endforeach ()
915

916 917
if (ZMQ_BUILD_FRAMEWORK)
  add_custom_command (
918
    TARGET libzmq
919 920 921 922
    POST_BUILD
    COMMAND ${CMAKE_COMMAND}
    ARGS -E make_directory "${CMAKE_LIBRARY_OUTPUT_PATH}/ZeroMQ.framework/Versions/${ZMQ_VERSION}/MacOS"
    COMMENT "Perf tools")
923
endif ()
924

925 926
if (MSVC)
    # default for all sources is to use precompiled headers
927 928 929
    foreach(source ${sources})    
        # C and C++ can not use the same precompiled header
        if (${soruce} MATCHES ".cpp$" AND NOT ${source} STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/src/precompiled.cpp")
Yann Diorcet's avatar
Yann Diorcet committed
930 931 932 933 934 935
            set_source_files_properties(${source}
                PROPERTIES
                COMPILE_FLAGS "/Yuprecompiled.hpp"
                OBJECT_DEPENDS precompiled.hpp
                )
        endif()
936 937 938 939 940
    endforeach()
    # create precompiled header
    set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/precompiled.cpp
        PROPERTIES
        COMPILE_FLAGS "/Ycprecompiled.hpp"
Yann Diorcet's avatar
Yann Diorcet committed
941
        OBJECT_OUTPUTS precompiled.hpp
942 943 944
        )
endif ()

945 946 947

#-----------------------------------------------------------------------------
# output
948 949 950 951 952 953
option(BUILD_SHARED "Whether or not to build the shared object"  ON)
option(BUILD_STATIC "Whether or not to build the static archive" ON)

list(APPEND target_outputs "")

if (BUILD_SHARED)
954
  list(APPEND target_outputs "libzmq")
955 956 957
endif()

if (BUILD_STATIC)
958
  list(APPEND target_outputs "libzmq-static")
959
endif()
960

961
if (MSVC)
962 963 964
  # Suppress linker warnings caused by #ifdef omission
  # of file content.
  set( CMAKE_STATIC_LINKER_FLAGS /ignore:4221 )
965 966
  set (PDB_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
  set (PDB_NAME "libzmq${MSVC_TOOLSET}-mt-gd-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}")
967 968 969 970 971 972
  function(enable_vs_guideline_checker target)
    set_target_properties(${target} PROPERTIES
        VS_GLOBAL_EnableCppCoreCheck true
        VS_GLOBAL_CodeAnalysisRuleSet CppCoreCheckRules.ruleset
        VS_GLOBAL_RunCodeAnalysis true)
  endfunction()
973
  if (BUILD_SHARED)
974
    add_library (libzmq SHARED ${sources} ${public_headers} ${html-docs} ${readme-docs} ${CMAKE_CURRENT_BINARY_DIR}/NSIS.template.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
975 976 977
    if(ENABLE_ANALYSIS)
        enable_vs_guideline_checker (libzmq)
    endif()
978
    set_target_properties (libzmq PROPERTIES
979 980 981
      PUBLIC_HEADER "${public_headers}"
      RELEASE_POSTFIX "${MSVC_TOOLSET}-mt-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
      RELWITHDEBINFO_POSTFIX "${MSVC_TOOLSET}-mt-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
982
      MINSIZEREL_POSTFIX "${MSVC_TOOLSET}-mt-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
983 984 985 986 987
      DEBUG_POSTFIX "${MSVC_TOOLSET}-mt-gd-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
      RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
      COMPILE_DEFINITIONS "DLL_EXPORT"
      OUTPUT_NAME "libzmq")
  endif()
988

989
  if (BUILD_STATIC)
990
    add_library (libzmq-static STATIC ${sources} ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
991
    set_target_properties (libzmq-static PROPERTIES
992 993 994
      PUBLIC_HEADER "${public_headers}"
      RELEASE_POSTFIX "${MSVC_TOOLSET}-mt-s-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
      RELWITHDEBINFO_POSTFIX "${MSVC_TOOLSET}-mt-s-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
995
      MINSIZEREL_POSTFIX "${MSVC_TOOLSET}-mt-s-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
996 997 998 999
      DEBUG_POSTFIX "${MSVC_TOOLSET}-mt-sgd-${ZMQ_VERSION_MAJOR}_${ZMQ_VERSION_MINOR}_${ZMQ_VERSION_PATCH}"
      COMPILE_FLAGS "/DZMQ_STATIC"
      OUTPUT_NAME "libzmq")
  endif()
1000
else ()
1001 1002
  # avoid building everything twice for shared + static
  # only on *nix, as Windows needs different preprocessor defines in static builds
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
  if (NOT MINGW)
    add_library (objects OBJECT ${sources})
    set_property(TARGET objects PROPERTY POSITION_INDEPENDENT_CODE ON)
    target_include_directories (objects
      PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
        $<INSTALL_INTERFACE:include>
    )
  endif ()
1013

1014
  if (BUILD_SHARED)
1015 1016 1017 1018 1019
    if (MINGW)
      add_library (libzmq SHARED ${sources} ${public_headers} ${html-docs} ${readme-docs} ${zmq-pkgconfig} ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
    else ()
      add_library (libzmq SHARED $<TARGET_OBJECTS:objects> ${public_headers} ${html-docs} ${readme-docs} ${zmq-pkgconfig} ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
    endif ()
Sylvain Corlay's avatar
Sylvain Corlay committed
1020
    # NOTE: the SOVERSION and VERSION MUST be the same as the one generated by libtool! It is NOT the same as the version of the package.
1021
    set_target_properties (libzmq PROPERTIES
1022 1023
                          COMPILE_DEFINITIONS "DLL_EXPORT"
                          PUBLIC_HEADER "${public_headers}"
1024
                          VERSION "5.1.6"
Sylvain Corlay's avatar
Sylvain Corlay committed
1025
                          SOVERSION "5"
Yann Diorcet's avatar
Yann Diorcet committed
1026 1027
                          OUTPUT_NAME "zmq"
                          PREFIX "lib")
1028
    if (ZMQ_BUILD_FRAMEWORK)
1029
      set_target_properties (libzmq PROPERTIES
Doron Somech's avatar
Doron Somech committed
1030 1031 1032
                            FRAMEWORK TRUE
                            MACOSX_FRAMEWORK_IDENTIFIER "org.zeromq.libzmq"
                            MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${ZMQ_VERSION}
1033
                            MACOSX_FRAMEWORK_BUNDLE_VERSION ${ZMQ_VERSION})
1034
      set_source_files_properties (${html-docs} PROPERTIES
Doron Somech's avatar
Doron Somech committed
1035
                                  MACOSX_PACKAGE_LOCATION doc)
1036
      set_source_files_properties (${readme-docs} PROPERTIES
Doron Somech's avatar
Doron Somech committed
1037
                                  MACOSX_PACKAGE_LOCATION etc)
1038
      set_source_files_properties (${zmq-pkgconfig} PROPERTIES
Doron Somech's avatar
Doron Somech committed
1039
                                  MACOSX_PACKAGE_LOCATION lib/pkgconfig)
1040
    endif ()
1041 1042 1043
  endif()

  if (BUILD_STATIC)
1044 1045 1046 1047 1048
    if (MINGW)
      add_library (libzmq-static STATIC ${sources} ${public_headers} ${html-docs} ${readme-docs} ${zmq-pkgconfig} ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
    else ()
      add_library (libzmq-static STATIC $<TARGET_OBJECTS:objects> ${public_headers} ${html-docs} ${readme-docs} ${zmq-pkgconfig} ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
    endif ()
1049
    set_target_properties (libzmq-static PROPERTIES
1050
      PUBLIC_HEADER "${public_headers}"
Yann Diorcet's avatar
Yann Diorcet committed
1051 1052
      OUTPUT_NAME "zmq"
      PREFIX "lib")
1053
  endif()
1054

1055 1056 1057 1058 1059
endif ()

if (BUILD_STATIC)
  target_compile_definitions(libzmq-static
    PUBLIC ZMQ_STATIC
1060
  )
1061
endif ()
1062

1063
foreach (target ${target_outputs})
1064 1065 1066 1067 1068 1069 1070 1071
  target_include_directories (${target}
    PUBLIC
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
      $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
      $<INSTALL_INTERFACE:include>
  )
endforeach()

1072
if (BUILD_SHARED)
1073
  target_link_libraries (libzmq ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
1074

1075
  if (SODIUM_FOUND)
1076
    target_link_libraries (libzmq ${SODIUM_LIBRARIES})
1077 1078 1079 1080
	# On Solaris, libsodium depends on libssp
    if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
      target_link_libraries (libzmq ssp)
    endif ()
1081
  endif ()
1082

1083
  if (HAVE_WS2_32)
1084
    target_link_libraries (libzmq ws2_32)
1085
  elseif (HAVE_WS2)
1086
    target_link_libraries (libzmq ws2)
1087
  endif ()
1088

1089
  if (HAVE_RPCRT4)
1090
    target_link_libraries (libzmq rpcrt4)
1091
  endif ()
1092

1093
  if (HAVE_IPHLAPI)
1094
    target_link_libraries (libzmq iphlpapi)
1095
  endif ()
1096

1097
  if (RT_LIBRARY)
1098
    target_link_libraries (libzmq -lrt)
1099
  endif ()
1100
endif ()
1101

1102 1103
if (BUILD_STATIC)
  target_link_libraries (libzmq-static ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
1104

1105 1106 1107 1108 1109 1110 1111
  if (SODIUM_FOUND)
    target_link_libraries (libzmq-static ${SODIUM_LIBRARIES})
    # On Solaris, libsodium depends on libssp
    if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
      target_link_libraries (libzmq-static ssp)
    endif ()
  endif ()
1112

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
  if (HAVE_WS2_32)
    target_link_libraries (libzmq-static ws2_32)
  elseif (HAVE_WS2)
    target_link_libraries (libzmq-static ws2)
  endif ()

  if (HAVE_RPCRT4)
    target_link_libraries (libzmq-static rpcrt4)
  endif ()

  if (HAVE_IPHLAPI)
    target_link_libraries (libzmq-static iphlpapi)
  endif ()

  if (RT_LIBRARY)
    target_link_libraries (libzmq-static -lrt)
  endif ()
endif ()

1132 1133 1134 1135 1136 1137 1138
if (BUILD_SHARED)
  set (perf-tools local_lat
                 remote_lat
                 local_thr
                 remote_thr
                 inproc_lat
                 inproc_thr)
1139

1140 1141 1142 1143 1144
  if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") # Why?
    option (WITH_PERF_TOOL "Build with perf-tools" ON)
  else ()
    option (WITH_PERF_TOOL "Build with perf-tools" OFF)
  endif ()
1145

1146 1147 1148
  if (WITH_PERF_TOOL)
    foreach (perf-tool ${perf-tools})
      add_executable (${perf-tool} perf/${perf-tool}.cpp)
1149
      target_link_libraries (${perf-tool} libzmq)
1150 1151 1152 1153

      if (ZMQ_BUILD_FRAMEWORK)
        # Copy perf-tools binaries into Framework
        add_custom_command (
1154
          TARGET libzmq ${perf-tool}
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
          POST_BUILD
          COMMAND ${CMAKE_COMMAND}
          ARGS -E copy "$<TARGET_FILE:${perf-tool}>" "${LIBRARY_OUTPUT_PATH}/ZeroMQ.framework/Versions/${ZMQ_VERSION_STRING}/MacOS/${perf-tool}"
          VERBATIM
          COMMENT "Perf tools")
      else ()
        install (TARGETS ${perf-tool}
                RUNTIME DESTINATION bin
                COMPONENT PerfTools)
      endif ()
    endforeach ()
  endif ()
elseif (WITH_PERF_TOOL)
  message(FATAL_ERROR "Shared library disabled - perf-tools unavailable.")
1169
endif ()
1170

1171 1172 1173
#-----------------------------------------------------------------------------
# tests

1174 1175 1176
option(BUILD_TESTS "Whether or not to build the tests" ON)

set (ZMQ_BUILD_TESTS ${BUILD_TESTS} CACHE BOOL "Build the tests for ZeroMQ")
1177

1178 1179 1180
if (ZMQ_BUILD_TESTS)
  enable_testing () # Enable testing only works in root scope
  ADD_SUBDIRECTORY (tests)
1181 1182 1183 1184 1185
  if (BUILD_STATIC)
    add_subdirectory (unittests)
  else ()
    message (WARNING "Not building unit tests, since BUILD_STATIC is not enabled")
  endif ()
1186
endif ()
1187

1188 1189
#-----------------------------------------------------------------------------
# installer
1190

1191
if (MSVC)
1192
  install (TARGETS ${target_outputs}
1193
          EXPORT ${PROJECT_NAME}-targets
1194 1195
          ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
          LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
1196
          RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
1197
          PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
Doron Somech's avatar
Doron Somech committed
1198
          COMPONENT SDK)
1199 1200 1201 1202 1203 1204
  if (MSVC_IDE)
    install (FILES ${PDB_OUTPUT_DIRECTORY}/\${CMAKE_INSTALL_CONFIG_NAME}/${PDB_NAME}.pdb DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT SDK OPTIONAL)
  else()
    install (FILES ${PDB_OUTPUT_DIRECTORY}/${PDB_NAME}.pdb DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT SDK OPTIONAL)
  endif()
  if (BUILD_SHARED)
1205
    install (TARGETS libzmq
1206 1207
            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
            PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
Doron Somech's avatar
Doron Somech committed
1208
            COMPONENT Runtime)
1209 1210
  endif ()
else ()
1211
  install (TARGETS ${target_outputs}
1212
          EXPORT ${PROJECT_NAME}-targets
1213 1214 1215
          RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
          ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
          LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Doron Somech's avatar
Doron Somech committed
1216
          FRAMEWORK DESTINATION "Library/Frameworks"
1217
          PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
1218
endif ()
1219

1220
# install (FILES ${public_headers}
Doron Somech's avatar
Doron Somech committed
1221 1222
#          DESTINATION include
#          COMPONENT SDK)
1223

1224 1225 1226
#if (NOT ZMQ_BUILD_FRAMEWORK)
#  file (GLOB private_headers "${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp")
#  install (FILES ${sources} ${private_headers} DESTINATION src/zmq
xantares's avatar
xantares committed
1227
#          COMPONENT SourceCode)
1228
#endif ()
1229

1230 1231
foreach (readme ${readme-docs})
  configure_file (${CMAKE_CURRENT_SOURCE_DIR}/${readme} ${CMAKE_CURRENT_BINARY_DIR}/${readme}.txt)
1232

1233
  if (NOT ZMQ_BUILD_FRAMEWORK)
1234
    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${readme}.txt DESTINATION share/zmq)
1235 1236
  endif ()
endforeach ()
1237

1238 1239 1240 1241 1242
if (WITH_DOC)
  if (NOT ZMQ_BUILD_FRAMEWORK)
    install (FILES ${html-docs} DESTINATION doc/zmq COMPONENT RefGuide)
  endif ()
endif ()
Doron Somech's avatar
Doron Somech committed
1243

1244 1245
include(CMakePackageConfigHelpers)

1246 1247 1248 1249 1250 1251
if (WIN32)
  set(ZEROMQ_CMAKECONFIG_INSTALL_DIR "CMake" CACHE STRING "install path for ZeroMQConfig.cmake")
else()
  # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
  set(ZEROMQ_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for ZeroMQConfig.cmake")
endif()
1252

1253 1254 1255 1256
if (NOT CMAKE_VERSION VERSION_LESS 3.0)
  export(EXPORT ${PROJECT_NAME}-targets
         FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
endif()
1257
configure_package_config_file(builds/cmake/${PROJECT_NAME}Config.cmake.in
1258 1259 1260 1261 1262
                              "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
                              INSTALL_DESTINATION ${ZEROMQ_CMAKECONFIG_INSTALL_DIR})
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
                                 VERSION ${ZMQ_VERSION_MAJOR}.${ZMQ_VERSION_MINOR}.${ZMQ_VERSION_PATCH}
                                 COMPATIBILITY AnyNewerVersion)
1263 1264 1265
install(EXPORT ${PROJECT_NAME}-targets
        FILE ${PROJECT_NAME}Targets.cmake
        DESTINATION ${ZEROMQ_CMAKECONFIG_INSTALL_DIR})
1266 1267 1268 1269
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
              ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
              DESTINATION ${ZEROMQ_CMAKECONFIG_INSTALL_DIR})

1270 1271
option(ENABLE_CPACK "Enables cpack rules" ON)
if (MSVC AND ENABLE_CPACK)
1272
  include (InstallRequiredSystemLibraries)
Doron Somech's avatar
Doron Somech committed
1273

1274 1275 1276 1277 1278
  if (CMAKE_CL_64)
    set (arch_name "x64")
  else ()
    set (arch_name "x86")
  endif ()
Doron Somech's avatar
Doron Somech committed
1279

1280 1281
  set (CPACK_NSIS_DISPLAY_NAME "ZeroMQ ${ZMQ_VERSION_MAJOR}.${ZMQ_VERSION_MINOR}.${ZMQ_VERSION_PATCH} (${arch_name})")
  set (CPACK_PACKAGE_FILE_NAME "ZeroMQ-${ZMQ_VERSION_MAJOR}.${ZMQ_VERSION_MINOR}.${ZMQ_VERSION_PATCH}-${arch_name}")
Doron Somech's avatar
Doron Somech committed
1282 1283 1284 1285

  # TODO: I think this part was intended to be used when running cpack
  # separately from cmake but I don't know how that works.
  #
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
  # macro (add_crt_version version)
  #   set (rel_dir "${CMAKE_CURRENT_BINARY_DIR}/build/${arch_name}/${version};ZeroMQ;ALL;/")
  #   set (debug_dir "${CMAKE_CURRENT_BINARY_DIR}/debug/${arch_name}/${version};ZeroMQ;ALL;/")
  #   if (EXISTS ${rel_dir})
  #     list (APPEND CPACK_INSTALL_CMAKE_PROJECTS ${rel_dir})
  #   endif ()

  #   if (EXISTS ${debug_dir})
  #     list (APPEND CPACK_INSTALL_CMAKE_PROJECTS ${rel_dir})
  #   endmacro ()
  # endmacro ()

  # add_crt_version (v110)
  # add_crt_version (v100)
  # add_crt_version (v90)

1302
  list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR})
1303 1304 1305 1306 1307 1308 1309 1310
  set (CPACK_GENERATOR "NSIS")
  set (CPACK_PACKAGE_NAME "ZeroMQ")
  set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "ZeroMQ lightweight messaging kernel")
  set (CPACK_PACKAGE_VENDOR "Miru")
  set (CPACK_NSIS_CONTACT "Steven McCoy <Steven.McCoy@miru.hk>")
  set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_BINARY_DIR}\\\\COPYING.txt")
#  set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_BINARY_DIR}\\\\README.txt")
#  set (CPACK_RESOURCE_FILE_WELCOME "${CMAKE_CURRENT_BINARY_DIR}\\\\WELCOME.txt")
Doron Somech's avatar
Doron Somech committed
1311
  # There is a bug in NSI that does not handle full unix paths properly. Make
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
  # sure there is at least one set of four (4) backslashes.
  set (CPACK_NSIS_MUI_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\installer.ico")
  set (CPACK_NSIS_MUI_UNIICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\installer.ico")

  set (CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\branding.bmp")
  set (CPACK_NSIS_COMPRESSOR "/SOLID lzma")
  set (CPACK_PACKAGE_VERSION ${ZMQ_VERSION})
  set (CPACK_PACKAGE_VERSION_MAJOR ${ZMQ_VERSION_MAJOR})
  set (CPACK_PACKAGE_VERSION_MINOR ${ZMQ_VERSION_MINOR})
  set (CPACK_PACKAGE_VERSION_PATCH ${ZMQ_VERSION_PATCH})
#  set (CPACK_PACKAGE_INSTALL_DIRECTORY "ZMQ Install Directory")
#  set (CPACK_TEMPORARY_DIRECTORY "ZMQ Temporary CPack Directory")

  include (CPack)

  cpack_add_component_group (Development
Doron Somech's avatar
Doron Somech committed
1328 1329
    DISPLAY_NAME "ZeroMQ software development kit"
    EXPANDED)
1330
  cpack_add_component (PerfTools
Doron Somech's avatar
Doron Somech committed
1331 1332
    DISPLAY_NAME "ZeroMQ performance tools"
    INSTALL_TYPES FullInstall DevInstall)
1333
  cpack_add_component (SourceCode
Doron Somech's avatar
Doron Somech committed
1334 1335 1336
    DISPLAY_NAME "ZeroMQ source code"
    DISABLED
    INSTALL_TYPES FullInstall)
1337
  cpack_add_component (SDK
Doron Somech's avatar
Doron Somech committed
1338 1339 1340
    DISPLAY_NAME "ZeroMQ headers and libraries"
    INSTALL_TYPES FullInstall DevInstall
    GROUP Development)
1341 1342
  if (WITH_DOC)
    cpack_add_component (RefGuide
Doron Somech's avatar
Doron Somech committed
1343 1344 1345
      DISPLAY_NAME "ZeroMQ reference guide"
      INSTALL_TYPES FullInstall DevInstall
      GROUP Development)
1346 1347
  endif ()
  cpack_add_component (Runtime
Doron Somech's avatar
Doron Somech committed
1348 1349 1350
    DISPLAY_NAME "ZeroMQ runtime files"
    REQUIRED
    INSTALL_TYPES FullInstall DevInstall MinInstall)
1351
  cpack_add_install_type (FullInstall
Doron Somech's avatar
Doron Somech committed
1352
    DISPLAY_NAME "Full install, including source code")
1353
  cpack_add_install_type (DevInstall
Doron Somech's avatar
Doron Somech committed
1354
    DISPLAY_NAME "Developer install, headers and libraries")
1355
  cpack_add_install_type (MinInstall
Doron Somech's avatar
Doron Somech committed
1356
    DISPLAY_NAME "Minimal install, runtime only")
1357
endif ()
Doron Somech's avatar
Doron Somech committed
1358 1359

# Export this for library to help build this as a sub-project
1360
set (ZEROMQ_LIBRARY libzmq CACHE STRING "ZeroMQ library")
Doron Somech's avatar
Doron Somech committed
1361 1362 1363

# Workaround for MSVS10 to avoid the Dialog Hell
# FIXME: This could be removed with future version of CMake.
1364 1365 1366 1367 1368 1369
if (MSVC_VERSION EQUAL 1600)
  set (ZMQ_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/ZeroMQ.sln")
  if (EXISTS "${ZMQ_SLN_FILENAME}")
    file (APPEND "${ZMQ_SLN_FILENAME}" "\n# This should be regenerated!\n")
  endif ()
endif ()
1370 1371

include(ClangFormat)