• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CMakeLists.txt for libcoap
2#
3# Copyright (C) 2020 Carlos Gomes Martinho <carlos.gomes_martinho@siemens.com>
4# Copyright (C) 2020-2023 Jon Shallow <supjps-libcoap@jpshallow.com>
5#
6# SPDX-License-Identifier: BSD-2-Clause
7#
8# This file is part of the CoAP library libcoap. Please see README for terms
9# of use.
10
11cmake_minimum_required(VERSION 3.10)
12
13project(
14  libcoap
15  VERSION 4.3.4
16  LANGUAGES CXX C)
17
18set(LIBCOAP_API_VERSION 3)
19set(LIBCOAP_ABI_VERSION 3.1.1)
20
21set(COAP_LIBRARY_NAME "coap-${LIBCOAP_API_VERSION}")
22
23option(
24  BUILD_SHARED_LIBS
25  "Build shared libs"
26  OFF)
27
28#
29# global compiler options
30# (need to do it before add_library())
31#
32
33add_compile_options(
34  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-pedantic>
35  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wall>
36  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wcast-qual>
37  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wextra>
38  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wformat-security>
39  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Winline>
40  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wmissing-declarations>
41  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wmissing-prototypes>
42  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wnested-externs>
43  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wpointer-arith>
44  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wshadow>
45  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wstrict-prototypes>
46  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wswitch-default>
47  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wswitch-enum>
48  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wunused>
49  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wwrite-strings>)
50
51if(MINGW)
52add_compile_options(
53  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wno-format>
54  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wno-format-security>)
55endif()
56
57if(${WARNING_TO_ERROR})
58add_compile_options(
59  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Werror>)
60endif()
61
62if(CMAKE_GENERATOR MATCHES "Visual Studio")
63  option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Export all symbols when compiling to a .dll" ON)
64  # add_compile_options(/Wall /wd4100 /wd4820 /wd4668 /wd4061)
65  if(${WARNING_TO_ERROR})
66    add_compile_options(/WX)
67  endif()
68endif()
69
70#
71# The libcoap library
72#
73
74add_library(${COAP_LIBRARY_NAME})
75set_property(TARGET ${COAP_LIBRARY_NAME} PROPERTY C_STANDARD 99)
76if(${CMAKE_VERSION} VERSION_GREATER "3.20.0")
77  cmake_policy(SET CMP0115 OLD) # Supresses libcoap configuration warning
78endif()
79
80set_target_properties(${COAP_LIBRARY_NAME} PROPERTIES SOVERSION ${LIBCOAP_API_VERSION})
81set_target_properties(${COAP_LIBRARY_NAME} PROPERTIES VERSION ${LIBCOAP_ABI_VERSION})
82
83#
84# options to tweak the library
85#
86
87option(
88  ENABLE_DTLS
89  "enable building with DTLS support"
90  ON)
91set(DTLS_BACKEND
92    "default"
93    CACHE
94      STRING
95      "\
96Name of the dtls backend, only relevant if `ENABLE_DTLS` is ON which is default. \
97Possible values: default, gnutls, openssl, tinydtls and mbedtls. \
98If specified then this library will be searched and if found also used. \
99If not found then the cmake configuration will stop with an error. \
100If not specified, then cmake will try to use the first one found in the following order: \
101gnutls, openssl, tinydtls, mbedtls \
102    ")
103set_property(
104  CACHE DTLS_BACKEND
105  PROPERTY STRINGS
106           default
107           openssl
108           gnutls
109           tinydtls
110           mbedtls)
111option(
112  USE_VENDORED_TINYDTLS
113  "compile with the tinydtls project in the submodule if on, otherwise try to find the compiled lib with find_package"
114  ON)
115option(
116  ENABLE_CLIENT_MODE
117  "compile with support for client mode code"
118  ON)
119option(
120  ENABLE_SERVER_MODE
121  "compile with support for server mode code"
122  ON)
123option(
124  ENABLE_OSCORE
125  "compile with support for OSCORE"
126  ON)
127option(
128  WITH_OBSERVE_PERSIST
129  "compile with observe persist support for server restarts"
130  ON)
131option(
132  WITH_EPOLL
133  "compile with epoll support"
134  ON)
135option(
136  ENABLE_SMALL_STACK
137  "enable if the system has small stack size"
138  OFF)
139option(
140  ENABLE_TCP
141  "enable building with TCP support"
142  ON)
143option(
144  ENABLE_IPV4
145  "enable building with IPv4 support"
146  ON)
147option(
148  ENABLE_IPV6
149  "enable building with IPv6 support"
150  ON)
151option(
152  ENABLE_AF_UNIX
153  "enable building with Unix socket support"
154  ON)
155option(
156  ENABLE_WS
157  "enable building with WebSockets support"
158  ON)
159option(
160  ENABLE_ASYNC
161  "enable building with async separate response support"
162  ON)
163option(
164  ENABLE_Q_BLOCK
165  "enable building with Q-Block (RFC9177) support"
166  ON)
167option(
168  ENABLE_TESTS
169  "build also tests"
170  OFF)
171option(
172  ENABLE_EXAMPLES
173  "build also examples"
174  ON)
175option(
176  ENABLE_DOCS
177  "build also doxygen documentation"
178  ON)
179option(
180  WARNING_TO_ERROR
181  "force all compiler warnings to be errors"
182  OFF)
183set(MAX_LOGGING_LEVEL
184    "8"
185    CACHE
186      STRING
187      "\
188Only build logging code up to and including the specified logging level (0 - 8)[default=8]]
189    ")
190set_property(
191  CACHE DTLS_BACKEND
192  PROPERTY STRINGS
193           "0"
194           "1"
195           "2"
196           "3"
197           "4"
198           "5"
199           "6"
200           "7"
201           "8")
202
203if(NOT CMAKE_C_STANDARD)
204  set(CMAKE_C_STANDARD 11)
205endif()
206
207if(NOT CMAKE_BUILD_TYPE)
208  set(CMAKE_BUILD_TYPE Debug)
209endif()
210
211if(MSVC)
212  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
213endif()
214
215if(APPLE)
216  add_definitions(-D__APPLE_USE_RFC_3542=1)
217endif()
218
219list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
220list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR})
221list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
222
223include(CheckCSourceCompiles)
224include(CheckFunctionExists)
225include(CheckIncludeFile)
226include(CheckSymbolExists)
227include(CheckTypeSize)
228include(TestBigEndian)
229
230# check for headers
231check_include_file(assert.h HAVE_ASSERT_H)
232check_include_file(string.h HAVE_STRING_H)
233check_include_file(strings.h HAVE_STRINGS_H)
234check_include_file(byteswap.h HAVE_BYTESWAP_H)
235check_include_file(inttypes.h HAVE_INTTYPES_H)
236check_include_file(errno.h HAVE_ERRNO_H)
237check_include_file(limits.h HAVE_LIMITS_H)
238check_include_file(memory.h HAVE_MEMORY_H)
239check_include_file(strings.h HAVE_STRINGS_H)
240check_include_file(string.h HAVE_STRING_H)
241check_include_file(sys/sysctl.h HAVE_SYS_SYSCTL_H)
242check_include_file(net/if.h HAVE_NET_IF_H)
243check_include_file(ifaddrs.h HAVE_IFADDRS_H)
244check_include_file(netinet/in.h HAVE_NETINET_IN_H)
245check_include_file(sys/epoll.h HAVE_EPOLL_H)
246check_include_file(sys/timerfd.h HAVE_TIMERFD_H)
247check_include_file(arpa/inet.h HAVE_ARPA_INET_H)
248check_include_file(stdbool.h HAVE_STDBOOL_H)
249check_include_file(netdb.h HAVE_NETDB_H)
250check_include_file(pthread.h HAVE_PTHREAD_H)
251check_include_file(stdlib.h HAVE_STDINT_H)
252check_include_file(stdint.h HAVE_STDLIB_H)
253check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
254check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
255check_include_file(sys/stat.h HAVE_SYS_STAT_H)
256check_include_file(sys/time.h HAVE_SYS_TIME_H)
257check_include_file(sys/types.h HAVE_SYS_TYPES_H)
258check_include_file(sys/unistd.h HAVE_SYS_UNISTD_H)
259check_include_file(time.h HAVE_TIME_H)
260check_include_file(unistd.h HAVE_UNISTD_H)
261check_include_file(float.h HAVE_FLOAT_H)
262check_include_file(stddef.h HAVE_STDDEF_H)
263check_include_file(winsock2.h HAVE_WINSOCK2_H)
264check_include_file(ws2tcpip.h HAVE_WS2TCPIP_H)
265
266# check for functions
267check_function_exists(malloc HAVE_MALLOC)
268check_function_exists(memset HAVE_MEMSET)
269check_function_exists(select HAVE_SELECT)
270check_function_exists(socket HAVE_SOCKET)
271check_function_exists(strcasecmp HAVE_STRCASECMP)
272check_function_exists(pthread_mutex_lock HAVE_PTHREAD_MUTEX_LOCK)
273check_function_exists(getaddrinfo HAVE_GETADDRINFO)
274check_function_exists(strnlen HAVE_STRNLEN)
275check_function_exists(strrchr HAVE_STRRCHR)
276check_function_exists(getrandom HAVE_GETRANDOM)
277check_function_exists(random HAVE_RANDOM)
278check_function_exists(if_nametoindex HAVE_IF_NAMETOINDEX)
279
280# check for symbols
281if(WIN32 AND NOT MINGW)
282  set(HAVE_STRUCT_CMSGHDR 1)
283  message(STATUS "setting HAVE_STRUCT_CMSGHDR")
284else()
285  check_symbol_exists(
286    CMSG_FIRSTHDR
287    sys/socket.h
288    HAVE_STRUCT_CMSGHDR)
289endif()
290
291if(${ENABLE_CLIENT_MODE})
292  set(COAP_CLIENT_SUPPORT "1")
293  message(STATUS "compiling with client support")
294else()
295  message(STATUS "compiling without client support")
296endif()
297
298if(${ENABLE_SERVER_MODE})
299  set(COAP_SERVER_SUPPORT "1")
300  message(STATUS "compiling with server support")
301else()
302  message(STATUS "compiling without server support")
303endif()
304
305if(${ENABLE_OSCORE})
306  set(COAP_OSCORE_SUPPORT "1")
307  message(STATUS "compiling with OSCORE support")
308else()
309  message(STATUS "compiling without OSCORE support")
310endif()
311
312if(${ENABLE_WS} AND $(ENABLE_TCP))
313  set(COAP_WS_SUPPORT "1")
314  message(STATUS "compiling with WebSockets support")
315else()
316  if(${ENABLE_WS})
317    set(ENABLE_WS OFF)
318    message(STATUS "WebSockets disabled as TCP not enabled")
319  endif()
320  message(STATUS "compiling without WebSockets support")
321endif()
322
323if(${ENABLE_ASYNC})
324  set(COAP_ASYNC_SUPPORT "1")
325  message(STATUS "compiling with async separate response support")
326else()
327  message(STATUS "compiling without async separate response support")
328endif()
329
330if(${ENABLE_IPV4})
331  set(COAP_IPV4_SUPPORT "1")
332  message(STATUS "compiling with IPv4 support")
333else()
334  message(STATUS "compiling without IPv4 support")
335endif()
336
337if(${ENABLE_IPV6})
338  set(COAP_IPV6_SUPPORT "1")
339  message(STATUS "compiling with IPv6 support")
340else()
341  message(STATUS "compiling without IPv6 support")
342endif()
343
344if(${ENABLE_AF_UNIX})
345  set(COAP_AF_UNIX_SUPPORT "1")
346  message(STATUS "compiling with Unix socket support")
347else()
348  message(STATUS "compiling without Unix socket support")
349endif()
350
351if(${ENABLE_Q_BLOCK})
352  set(COAP_Q_BLOCK_SUPPORT "1")
353  message(STATUS "compiling with Q-Block (RFC9177) support")
354else()
355  message(STATUS "compiling without Q-Block (RFC9177) support")
356endif()
357
358
359if(${WITH_OBSERVE_PERSIST})
360  set(COAP_WITH_OBSERVE_PERSIST "1")
361  message(STATUS "compiling with observe persistence support")
362else()
363  message(STATUS "compiling without observe persistence support")
364endif()
365
366if(${WITH_EPOLL}
367   AND HAVE_EPOLL_H
368   AND HAVE_TIMERFD_H)
369  set(COAP_EPOLL_SUPPORT "1")
370  message(STATUS "compiling with epoll support")
371else()
372  if(${WITH_EPOLL})
373    set(WITH_EPOLL OFF)
374    message(STATUS "epoll disabled as kernel support not available")
375  endif()
376  message(STATUS "compiling without epoll support")
377endif()
378
379if(ENABLE_SMALL_STACK)
380  set(ENABLE_SMALL_STACK "${ENABLE_SMALL_STACK}")
381  message(STATUS "compiling with small stack support")
382endif()
383
384if(${MAX_LOGGING_LEVEL} MATCHES "[0-7]")
385  set(COAP_MAX_LOGGING_LEVEL ${MAX_LOGGING_LEVEL})
386  message(STATUS "compiling with max logging level set to ${MAX_LOGGING_LEVEL}")
387else()
388  message(STATUS "compiling with max logging level set to none")
389endif()
390
391set(WITH_GNUTLS OFF)
392set(WITH_OPENSSL OFF)
393set(WITH_TINYDTLS OFF)
394set(WITH_MBEDTLS OFF)
395
396function(compile_tinydtls)
397  set(TINYDTLS_SOURCES_DIR ${CMAKE_CURRENT_LIST_DIR}/ext/tinydtls)
398  set(TINYDTLS_SOURCES_GENERATED ${TINYDTLS_SOURCES_DIR}/dtls_config.h)
399
400  message(STATUS "compiling the tinydtls lib")
401
402  include(ExternalProject)
403
404  externalproject_add(
405    external_tinydtls
406    SOURCE_DIR "${TINYDTLS_SOURCES_DIR}"
407    BUILD_IN_SOURCE 1
408    DOWNLOAD_COMMAND ""
409    UPDATE_COMMAND ""
410    CONFIGURE_COMMAND
411      ${TINYDTLS_SOURCES_DIR}/configure
412      --disable-manpages
413      --prefix=${CMAKE_BINARY_DIR}
414    BUILD_COMMAND make install
415    INSTALL_COMMAND ""
416    LOG_DOWNLOAD 1
417    LOG_CONFIGURE 1)
418
419  externalproject_add_step(
420    external_tinydtls autoreconf
421    COMMAND ./autogen.sh
422    ALWAYS 1
423    WORKING_DIRECTORY "${TINYDTLS_SOURCES_DIR}"
424    DEPENDERS configure
425    DEPENDEES download)
426
427  # Let cmake know that it needs to execute the external_tinydtls target to generate those files.
428  add_custom_command(
429    OUTPUT ${TINYDTLS_SOURCES_GENERATED}
430    WORKING_DIRECTORY "${TINYDTLS_SOURCES_DIR}"
431    COMMAND "make install"
432    DEPENDS external_tinydtls)
433
434  add_dependencies(${COAP_LIBRARY_NAME} external_tinydtls)
435
436  if(BUILD_SHARED_LIBS)
437    set(LIBTINYDTLS_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libtinydtls.so")
438  else()
439    set(LIBTINYDTLS_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libtinydtls.a")
440  endif()
441
442  add_library(
443    tinydtls
444    UNKNOWN
445    IMPORTED)
446  set_target_properties(
447    tinydtls
448    PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
449               "${CMAKE_CURRENT_BINARY_DIR}/include"
450               IMPORTED_LINK_INTERFACE_LANGUAGES "C"
451               IMPORTED_LOCATION "${LIBTINYDTLS_PATH}")
452
453endfunction()
454
455if(ENABLE_DTLS)
456  message(STATUS "compiling with DTLS support")
457  message(STATUS "DTLS_BACKEND: ${DTLS_BACKEND}")
458
459  if(DTLS_BACKEND
460     STREQUAL
461     "default")
462    # try to find a crypto lib and use it, use the first one found
463
464    # libgnutls (e.g. debian libgnutls28-dev)
465    find_package(GnuTLS)
466    if(GnuTLS_FOUND)
467      set(WITH_GNUTLS ON)
468      message(STATUS "compiling with gnutls support")
469      set(COAP_WITH_LIBGNUTLS 1)
470    else()
471      # gnutls not found
472      find_package(OpenSSL)
473      if(OpenSSL_FOUND)
474        set(WITH_OPENSSL ON)
475        message(STATUS "compiling with openssl support")
476        set(COAP_WITH_LIBOPENSSL 1)
477      else()
478        # openssl not found
479        # libmbedtls (e.g. debian libmbedtls-dev)
480        find_package(MbedTLS)
481        if(MbedTLS_FOUND)
482          set(WITH_MBEDTLS ON)
483          message(STATUS "compiling with mbedtls support")
484          set(COAP_WITH_LIBMBEDTLS 1)
485        else()
486          # mbedtls not found
487          if(USE_VENDORED_TINYDTLS)
488            compile_tinydtls()
489          else()
490            find_package(TinyDTLS)
491            if(TINYDTLS_FOUND)
492
493            else()
494              # no cryto lib found
495              message(
496                FATAL_ERROR
497                  "cannot find any cryto lib, either install one or compile without DTLS support"
498              )
499            endif()
500
501          endif()
502
503          set(WITH_TINYDTLS ON)
504          message(STATUS "compiling with tinydtls support")
505          set(COAP_WITH_LIBTINYDTLS 1)
506
507        endif()
508
509      endif()
510
511    endif()
512
513  else()
514    # DTLS_BACKEND variable is not empty, so set all to false and set the only right to true
515    set(WITH_GNUTLS OFF)
516    set(WITH_TINYDTLS OFF)
517    set(WITH_MBEDTLS OFF)
518    set(WITH_OPENSSL OFF)
519
520    if(DTLS_BACKEND
521       STREQUAL
522       "gnutls")
523      # libgnutls (e.g. debian libgnutls28-dev)
524      find_package(GnuTLS REQUIRED)
525      set(WITH_GNUTLS ON)
526      message(STATUS "compiling with gnutls support")
527      set(COAP_WITH_LIBGNUTLS 1)
528    endif()
529
530    if(DTLS_BACKEND
531       STREQUAL
532       "openssl")
533      # libssl (e.g. debian libssl1.0-dev)
534      find_package(OpenSSL REQUIRED)
535      set(WITH_OPENSSL ON)
536      message(STATUS "compiling with openssl support")
537      set(COAP_WITH_LIBOPENSSL 1)
538    endif()
539
540    if(DTLS_BACKEND
541       STREQUAL
542       "mbedtls")
543      # libmbedtls (e.g. debian libmbedtls-dev)
544      find_package(MbedTLS REQUIRED)
545      set(WITH_MBEDTLS ON)
546      message(STATUS "compiling with mbedtls support")
547      set(COAP_WITH_LIBMBEDTLS 1)
548    endif()
549
550    if(DTLS_BACKEND
551       STREQUAL
552       "tinydtls")
553
554      if(USE_VENDORED_TINYDTLS)
555        compile_tinydtls()
556      else(USE_VENDORED_TINYDTLS)
557        find_package(TinyDTLS REQUIRED)
558      endif(USE_VENDORED_TINYDTLS)
559
560      message(STATUS "compiling with tinydtls support")
561      set(WITH_TINYDTLS ON)
562      set(COAP_WITH_LIBTINYDTLS 1)
563
564    endif()
565
566  endif()
567
568endif()
569
570execute_process(COMMAND git describe --tags --dirty --always
571                RESULT_VARIABLE USING_GIT
572                OUTPUT_VARIABLE LIBCOAP_PACKAGE_BUILD
573                OUTPUT_STRIP_TRAILING_WHITESPACE
574                ERROR_QUIET)
575if(NOT ${USING_GIT} EQUAL 0)
576  set(LIBCOAP_PACKAGE_BUILD ${PROJECT_VERSION})
577else()
578  set(LIBCOAP_PACKAGE_BUILD "${LIBCOAP_PACKAGE_BUILD}")
579endif()
580
581set(PACKAGE_URL "https://libcoap.net/")
582set(PACKAGE_NAME "${PROJECT_NAME}")
583set(PACKAGE_TARNAME "${PROJECT_NAME}")
584set(PACKAGE_STRING "${PROJECT_NAME} ${PROJECT_VERSION}")
585set(PACKAGE_VERSION "${PROJECT_VERSION}")
586set(PACKAGE_BUGREPORT "libcoap-developers@lists.sourceforge.net")
587set(LIBCOAP_PACKAGE_VERSION "${PACKAGE_VERSION}")
588set(LIBCOAP_PACKAGE_URL "${PACKAGE_URL}")
589set(LIBCOAP_PACKAGE_NAME "${PACKAGE_NAME}")
590set(LIBCOAP_PACKAGE_STRING "${PACKAGE_STRING}")
591set(LIBCOAP_PACKAGE_BUGREPORT "${PACKAGE_BUGREPORT}")
592
593message(STATUS "")
594message(STATUS "libcoap Configuration Summary:")
595message(STATUS "")
596message(STATUS "PACKAGE VERSION..................${PACKAGE_VERSION}")
597message(STATUS "PACKAGE SOURCE...................${LIBCOAP_PACKAGE_BUILD}")
598message(STATUS "LIBRARY API VERSION..............${LIBCOAP_API_VERSION}")
599message(STATUS "LIBRARY ABI VERSION..............${LIBCOAP_ABI_VERSION}")
600message(STATUS "ENABLE_DTLS:.....................${ENABLE_DTLS}")
601message(STATUS "ENABLE_TCP:......................${ENABLE_TCP}")
602message(STATUS "ENABLE_IPV4:.....................${ENABLE_IPV4}")
603message(STATUS "ENABLE_IPV6:.....................${ENABLE_IPV6}")
604message(STATUS "ENABLE_AF_UNIX:..................${ENABLE_AF_UNIX}")
605message(STATUS "ENABLE_WEBSOCKETS:...............${ENABLE_WS}")
606message(STATUS "ENABLE_Q_BLOCK:..................${ENABLE_Q_BLOCK}")
607message(STATUS "ENABLE_CLIENT_MODE:..............${ENABLE_CLIENT_MODE}")
608message(STATUS "ENABLE_SERVER_MODE:..............${ENABLE_SERVER_MODE}")
609message(STATUS "ENABLE_OSCORE:...................${ENABLE_OSCORE}")
610message(STATUS "ENABLE_ASYNC:....................${ENABLE_ASYNC}")
611message(STATUS "ENABLE_DOCS:.....................${ENABLE_DOCS}")
612message(STATUS "ENABLE_EXAMPLES:.................${ENABLE_EXAMPLES}")
613message(STATUS "DTLS_BACKEND:....................${DTLS_BACKEND}")
614message(STATUS "WITH_GNUTLS:.....................${WITH_GNUTLS}")
615message(STATUS "WITH_TINYDTLS:...................${WITH_TINYDTLS}")
616message(STATUS "WITH_OPENSSL:....................${WITH_OPENSSL}")
617message(STATUS "WITH_MBEDTLS:....................${WITH_MBEDTLS}")
618message(STATUS "HAVE_LIBTINYDTLS:................${COAP_WITH_LIBTINYDTLS}")
619message(STATUS "HAVE_LIBGNUTLS:..................${COAP_WITH_LIBGNUTLS}")
620message(STATUS "HAVE_LIBOPENSSL:.................${COAP_WITH_LIBOPENSSL}")
621message(STATUS "HAVE_LIBMBEDTLS:.................${COAP_WITH_LIBMBEDTLS}")
622message(STATUS "WITH_EPOLL:......................${WITH_EPOLL}")
623message(STATUS "WITH_OBSERVE_PERSIST:............${WITH_OBSERVE_PERSIST}")
624message(STATUS "BUILD_SHARED_LIBS:...............${BUILD_SHARED_LIBS}")
625message(STATUS "MAX_LOGGING_LEVEL:...............${MAX_LOGGING_LEVEL}")
626message(STATUS "WARNING_TO_ERROR:................${WARNING_TO_ERROR}")
627message(STATUS "CMAKE_C_COMPILER:................${CMAKE_C_COMPILER}")
628message(STATUS "CMAKE_CXX_COMPILER_ID:...........${CMAKE_CXX_COMPILER_ID}")
629message(STATUS "CMAKE_BUILD_TYPE:................${CMAKE_BUILD_TYPE}")
630message(STATUS "CMAKE_SYSTEM_PROCESSOR:..........${CMAKE_SYSTEM_PROCESSOR}")
631message(STATUS "CMAKE_HOST_SYSTEM_NAME:..........${CMAKE_HOST_SYSTEM_NAME}")
632message(STATUS "CMAKE_GENERATOR:.................${CMAKE_GENERATOR}")
633message(STATUS "")
634
635set(top_srcdir "${CMAKE_CURRENT_LIST_DIR}")
636set(top_builddir "${CMAKE_CURRENT_BINARY_DIR}")
637if(ENABLE_TCP)
638  set(COAP_DISABLE_TCP 0)
639else(ENABLE_TCP)
640  set(COAP_DISABLE_TCP 1)
641endif(ENABLE_TCP)
642
643# creates config header file in build directory
644configure_file(${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap.h.in
645               ${CMAKE_CURRENT_BINARY_DIR}/include/coap${LIBCOAP_API_VERSION}/coap.h)
646
647configure_file(${CMAKE_CURRENT_LIST_DIR}/cmake_coap_config.h.in
648               ${CMAKE_CURRENT_BINARY_DIR}/coap_config.h)
649
650configure_file(${CMAKE_CURRENT_LIST_DIR}/tests/test_common.h.in
651               ${CMAKE_CURRENT_LIST_DIR}/tests/test_common.h)
652
653#
654# sources
655#
656
657target_sources(
658  ${COAP_LIBRARY_NAME}
659  PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/coap_address.c
660          ${CMAKE_CURRENT_LIST_DIR}/src/coap_asn1.c
661          ${CMAKE_CURRENT_LIST_DIR}/src/coap_async.c
662          ${CMAKE_CURRENT_LIST_DIR}/src/coap_block.c
663          ${CMAKE_CURRENT_LIST_DIR}/src/coap_cache.c
664          ${CMAKE_CURRENT_LIST_DIR}/src/coap_debug.c
665          ${CMAKE_CURRENT_LIST_DIR}/src/coap_dtls.c
666          ${CMAKE_CURRENT_LIST_DIR}/src/coap_encode.c
667          ${CMAKE_CURRENT_LIST_DIR}/src/coap_event.c
668          ${CMAKE_CURRENT_LIST_DIR}/src/coap_hashkey.c
669          ${CMAKE_CURRENT_LIST_DIR}/src/coap_io.c
670          ${CMAKE_CURRENT_LIST_DIR}/src/coap_layers.c
671          ${CMAKE_CURRENT_LIST_DIR}/src/coap_mem.c
672          ${CMAKE_CURRENT_LIST_DIR}/src/coap_net.c
673          ${CMAKE_CURRENT_LIST_DIR}/src/coap_netif.c
674          ${CMAKE_CURRENT_LIST_DIR}/src/coap_notls.c
675          ${CMAKE_CURRENT_LIST_DIR}/src/coap_option.c
676          ${CMAKE_CURRENT_LIST_DIR}/src/coap_oscore.c
677          ${CMAKE_CURRENT_LIST_DIR}/src/coap_pdu.c
678          ${CMAKE_CURRENT_LIST_DIR}/src/coap_prng.c
679          ${CMAKE_CURRENT_LIST_DIR}/src/coap_resource.c
680          ${CMAKE_CURRENT_LIST_DIR}/src/coap_session.c
681          ${CMAKE_CURRENT_LIST_DIR}/src/coap_str.c
682          ${CMAKE_CURRENT_LIST_DIR}/src/coap_subscribe.c
683          ${CMAKE_CURRENT_LIST_DIR}/src/coap_tcp.c
684          ${CMAKE_CURRENT_LIST_DIR}/src/coap_time.c
685          ${CMAKE_CURRENT_LIST_DIR}/src/coap_uri.c
686          ${CMAKE_CURRENT_LIST_DIR}/src/coap_ws.c
687          # no need to parse those files if we do not need them
688          $<$<BOOL:${COAP_WITH_LIBOPENSSL}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_openssl.c>
689          $<$<BOOL:${COAP_WITH_LIBTINYDTLS}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_tinydtls.c>
690          $<$<BOOL:${COAP_WITH_LIBGNUTLS}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_gnutls.c>
691          $<$<BOOL:${COAP_WITH_LIBMBEDTLS}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_mbedtls.c>
692          # needed for OSCORE is enabled
693          $<$<BOOL:${COAP_OSCORE_SUPPORT}>:${CMAKE_CURRENT_LIST_DIR}/src/oscore/oscore.c>
694          $<$<BOOL:${COAP_OSCORE_SUPPORT}>:${CMAKE_CURRENT_LIST_DIR}/src/oscore/oscore_cbor.c>
695          $<$<BOOL:${COAP_OSCORE_SUPPORT}>:${CMAKE_CURRENT_LIST_DIR}/src/oscore/oscore_context.c>
696          $<$<BOOL:${COAP_OSCORE_SUPPORT}>:${CMAKE_CURRENT_LIST_DIR}/src/oscore/oscore_cose.c>
697          $<$<BOOL:${COAP_OSCORE_SUPPORT}>:${CMAKE_CURRENT_LIST_DIR}/src/oscore/oscore_crypto.c>
698          # headers
699          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap.h
700          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/libcoap.h
701          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_address.h
702          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_async.h
703          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_block.h
704          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_cache.h
705          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_debug.h
706          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_dtls.h
707          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_encode.h
708          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_event.h
709          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_io.h
710          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_mem.h
711          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_net.h
712          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_option.h
713          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_pdu.h
714          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_prng.h
715          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_resource.h
716          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_session.h
717          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_str.h
718          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_subscribe.h
719          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_time.h
720          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_uri.h
721          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_ws.h)
722target_include_directories(
723  ${COAP_LIBRARY_NAME}
724  PUBLIC # config headers are generated during configuration time
725         $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/>
726         $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/>
727         $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/>
728         $<INSTALL_INTERFACE:include/>
729         $<$<AND:$<BOOL:${COAP_WITH_LIBTINYDTLS}>,$<BOOL:${USE_VENDORED_TINYDTLS}>>:${CMAKE_BINARY_DIR}/include/tinydtls>
730         $<$<BOOL:${COAP_WITH_LIBGNUTLS}>:${GNUTLS_INCLUDE_DIR}>
731         $<$<BOOL:${COAP_WITH_LIBMBEDTLS}>:${MBEDTLS_INCLUDE_DIRS}>)
732target_link_libraries(
733  ${COAP_LIBRARY_NAME}
734  PUBLIC $<$<BOOL:${COAP_WITH_LIBOPENSSL}>:OpenSSL::SSL>
735         $<$<BOOL:${COAP_WITH_LIBOPENSSL}>:OpenSSL::Crypto>
736         $<$<BOOL:${COAP_WITH_LIBGNUTLS}>:${GNUTLS_LIBRARIES}>
737         $<$<BOOL:${COAP_WITH_LIBTINYDTLS}>:tinydtls>
738         $<$<BOOL:${COAP_WITH_LIBMBEDTLS}>:${MBEDTLS_LIBRARY}>
739         $<$<BOOL:${COAP_WITH_LIBMBEDTLS}>:${MBEDX509_LIBRARY}>
740         $<$<BOOL:${COAP_WITH_LIBMBEDTLS}>:${MBEDCRYPTO_LIBRARY}>
741         $<$<BOOL:${MINGW}>:ws2_32>)
742
743target_compile_options(
744  ${COAP_LIBRARY_NAME}
745  PUBLIC -DLIBCOAP_PACKAGE_BUILD="${LIBCOAP_PACKAGE_BUILD}")
746
747add_library(
748  ${PROJECT_NAME}::${COAP_LIBRARY_NAME}
749  ALIAS
750  ${COAP_LIBRARY_NAME})
751
752#
753# tests
754#
755
756if(ENABLE_TESTS)
757  add_executable(
758    testdriver
759    ${CMAKE_CURRENT_LIST_DIR}/tests/testdriver.c
760    ${CMAKE_CURRENT_LIST_DIR}/tests/test_common.h
761    ${CMAKE_CURRENT_LIST_DIR}/tests/test_encode.c
762    ${CMAKE_CURRENT_LIST_DIR}/tests/test_encode.h
763    ${CMAKE_CURRENT_LIST_DIR}/tests/test_error_response.c
764    ${CMAKE_CURRENT_LIST_DIR}/tests/test_error_response.h
765    ${CMAKE_CURRENT_LIST_DIR}/tests/test_options.c
766    ${CMAKE_CURRENT_LIST_DIR}/tests/test_options.h
767    ${CMAKE_CURRENT_LIST_DIR}/tests/test_oscore.c
768    ${CMAKE_CURRENT_LIST_DIR}/tests/test_oscore.h
769    ${CMAKE_CURRENT_LIST_DIR}/tests/test_pdu.c
770    ${CMAKE_CURRENT_LIST_DIR}/tests/test_pdu.h
771    ${CMAKE_CURRENT_LIST_DIR}/tests/test_sendqueue.c
772    ${CMAKE_CURRENT_LIST_DIR}/tests/test_sendqueue.h
773    ${CMAKE_CURRENT_LIST_DIR}/tests/test_session.c
774    ${CMAKE_CURRENT_LIST_DIR}/tests/test_session.h
775    ${CMAKE_CURRENT_LIST_DIR}/tests/test_tls.c
776    ${CMAKE_CURRENT_LIST_DIR}/tests/test_tls.h
777    ${CMAKE_CURRENT_LIST_DIR}/tests/test_uri.c
778    ${CMAKE_CURRENT_LIST_DIR}/tests/test_uri.h
779    ${CMAKE_CURRENT_LIST_DIR}/tests/test_wellknown.c
780    ${CMAKE_CURRENT_LIST_DIR}/tests/test_wellknown.h)
781  # tests require libcunit (e.g. debian libcunit1-dev)
782  target_link_libraries(testdriver PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME}
783                                          -lcunit)
784endif()
785
786#
787# examples
788#
789
790if(ENABLE_EXAMPLES)
791  add_executable(coap-client ${CMAKE_CURRENT_LIST_DIR}/examples/coap-client.c)
792  target_link_libraries(coap-client
793                        PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
794
795  add_executable(coap-rd ${CMAKE_CURRENT_LIST_DIR}/examples/coap-rd.c)
796  target_include_directories(coap-rd
797    PRIVATE
798         $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}>)
799  target_link_libraries(coap-rd PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
800
801  add_executable(coap-server ${CMAKE_CURRENT_LIST_DIR}/examples/coap-server.c)
802  target_link_libraries(coap-server
803                        PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
804
805  if(NOT WIN32 AND NOT MINGW)
806    add_executable(etsi_iot_01 ${CMAKE_CURRENT_LIST_DIR}/examples/etsi_iot_01.c)
807    target_link_libraries(etsi_iot_01
808                          PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
809
810    add_executable(tiny ${CMAKE_CURRENT_LIST_DIR}/examples/tiny.c)
811    target_link_libraries(tiny PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
812
813    add_executable(oscore-interop-server
814                   ${CMAKE_CURRENT_LIST_DIR}/examples/oscore-interop-server.c)
815    target_link_libraries(oscore-interop-server
816                          PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
817  endif()
818endif()
819
820#
821# docs
822#
823
824if(ENABLE_DOCS)
825  find_package(Doxygen)
826
827  if(Doxygen_FOUND)
828    # set input and output files
829    set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in)
830    set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
831
832    # Make necessary temporary directories
833    file(MAKE_DIRECTORY  ${CMAKE_CURRENT_BINARY_DIR}/doc/man_tmp)
834    file(MAKE_DIRECTORY  ${CMAKE_CURRENT_BINARY_DIR}/doc/man_html)
835
836    # request to configure the file
837    configure_file(
838      ${DOXYGEN_IN}
839      ${DOXYGEN_OUT}
840      @ONLY)
841
842    # note the option ALL which allows to build the docs together with the
843    # application
844    add_custom_target(
845      doc_doxygen ALL
846      COMMAND ${DOXYGEN_EXECUTABLE} -u > /dev/null 2>&1
847      COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
848      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
849      COMMENT "Generating API documentation with Doxygen"
850      VERBATIM)
851
852    message(STATUS "Setup up the Doxygen documention build")
853
854  else(Doxygen_FOUND)
855    message(
856      WARNING
857        "Doxygen need to be installed to generate the doxygen documentation")
858  endif(Doxygen_FOUND)
859
860endif()
861
862#
863# install
864#
865
866include(GNUInstallDirs)
867include(CMakePackageConfigHelpers)
868
869set(LIBCOAP_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
870install(
871  TARGETS ${COAP_LIBRARY_NAME}
872  EXPORT ${PROJECT_NAME}Targets
873  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib
874  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib)
875
876install(
877  EXPORT ${PROJECT_NAME}Targets
878  DESTINATION ${LIBCOAP_CONFIG_INSTALL_DIR}
879  NAMESPACE ${PROJECT_NAME}::
880  COMPONENT dev)
881
882configure_package_config_file(
883  cmake/Config.cmake.in
884  ${PROJECT_NAME}Config.cmake
885  INSTALL_DESTINATION
886  ${LIBCOAP_CONFIG_INSTALL_DIR})
887write_basic_package_version_file(
888  ${PROJECT_NAME}ConfigVersion.cmake
889  COMPATIBILITY SameMajorVersion)
890install(
891  FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
892        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
893  DESTINATION ${LIBCOAP_CONFIG_INSTALL_DIR}
894  COMPONENT dev)
895
896install(
897  DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/
898  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
899  COMPONENT dev
900  FILES_MATCHING
901  PATTERN "*.h"
902  PATTERN "coap.h" EXCLUDE
903  PATTERN "coap_riot.h" EXCLUDE
904  PATTERN "*_internal.h" EXCLUDE
905  PATTERN "oscore*" EXCLUDE)
906install(
907  DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
908  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
909  COMPONENT dev
910  FILES_MATCHING
911  PATTERN "*.h")
912if(ENABLE_EXAMPLES)
913  install(
914    TARGETS coap-server coap-client coap-rd
915    DESTINATION ${CMAKE_INSTALL_BINDIR}
916    COMPONENT dev)
917  if(NOT WIN32 AND NOT MINGW)
918    install(
919      TARGETS etsi_iot_01 tiny oscore-interop-server
920      DESTINATION ${CMAKE_INSTALL_BINDIR}
921      COMPONENT dev)
922  endif()
923endif()
924