• 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-2021 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.0
16  LANGUAGES CXX C)
17
18set(LIBCOAP_API_VERSION 3)
19set(COAP_LIBRARY_NAME "coap-${LIBCOAP_API_VERSION}")
20
21option(
22  BUILD_SHARED_LIBS
23  "Build shared libs"
24  OFF)
25
26add_library(${COAP_LIBRARY_NAME})
27
28#
29# options to tweak the library
30#
31
32option(
33  ENABLE_DTLS
34  "Enable building with DTLS support"
35  ON)
36set(DTLS_BACKEND
37    "default"
38    CACHE
39      STRING
40      "\
41Name of the dtls backend, only relevant if `ENABLE_DTLS` is ON which is default. \
42Possible values: default, gnutls, openssl, tinydtls and mbedtls. \
43If specified then this library will be searched and if found also used. \
44If not found then the cmake configuration will stop with an error. \
45If not specified, then cmake will try to use the first one found in the following order: \
46gnutls, openssl, tinydtls, mbedtls \
47    ")
48set_property(
49  CACHE DTLS_BACKEND
50  PROPERTY STRINGS
51           default
52           openssl
53           gnutls
54           tinydtls
55           mbedtls)
56option(
57  USE_VENDORED_TINYDTLS
58  "compile with the tinydtls project in the submodule if on, otherwise try to find the compiled lib with find_package"
59  ON)
60option(
61  WITH_EPOLL
62  "compile with epoll support"
63  ON)
64option(
65  ENABLE_SMALL_STACK
66  "Define if the system has small stack size"
67  OFF)
68option(
69  ENABLE_TCP
70  "Enable building with TCP support"
71  ON)
72option(
73  ENABLE_TESTS
74  "build also tests"
75  OFF)
76option(
77  ENABLE_EXAMPLES
78  "build also examples"
79  ON)
80option(
81  ENABLE_DOCS
82  "build also doxygen documentation"
83  ON)
84
85if(NOT CMAKE_C_STANDARD)
86  set(CMAKE_C_STANDARD 11)
87endif()
88
89if(NOT CMAKE_BUILD_TYPE)
90  set(CMAKE_BUILD_TYPE Debug)
91endif()
92
93if(MSVC)
94  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
95endif()
96
97list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
98list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR})
99list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
100
101include(CheckCSourceCompiles)
102include(CheckFunctionExists)
103include(CheckIncludeFile)
104include(CheckSymbolExists)
105include(CheckTypeSize)
106include(TestBigEndian)
107
108# check for headers
109check_include_file(byteswap.h HAVE_BYTESWAP_H)
110check_include_file(inttypes.h HAVE_INTTYPES_H)
111check_include_file(limits.h HAVE_LIMITS_H)
112check_include_file(memory.h HAVE_MEMORY_H)
113check_include_file(strings.h HAVE_STRINGS_H)
114check_include_file(string.h HAVE_STRING_H)
115check_include_file(sys/sysctl.h HAVE_SYS_SYSCTL_H)
116check_include_file(net/if.h HAVE_NET_IF_H)
117check_include_file(netinet/in.h HAVE_NETINET_IN_H)
118check_include_file(sys/epoll.h HAVE_EPOLL_H)
119check_include_file(sys/timerfd.h HAVE_TIMERFD_H)
120check_include_file(arpa/inet.h HAVE_ARPA_INET_H)
121check_include_file(stdbool.h HAVE_STDBOOL_H)
122check_include_file(netdb.h HAVE_NETDB_H)
123check_include_file(pthread.h HAVE_PTHREAD_H)
124check_include_file(stdlib.h HAVE_STDINT_H)
125check_include_file(stdint.h HAVE_STDLIB_H)
126check_include_file(inttypes.h HAVE_INTTYPES_H)
127check_include_file(syslog.h HAVE_SYSLOG_H)
128check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
129check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
130check_include_file(sys/stat.h HAVE_SYS_STAT_H)
131check_include_file(sys/time.h HAVE_SYS_TIME_H)
132check_include_file(sys/types.h HAVE_SYS_TYPES_H)
133check_include_file(sys/unistd.h HAVE_SYS_UNISTD_H)
134check_include_file(time.h HAVE_TIME_H)
135check_include_file(unistd.h HAVE_UNISTD_H)
136check_include_file(float.h HAVE_FLOAT_H)
137check_include_file(stddef.h HAVE_STDDEF_H)
138check_include_file(winsock2.h HAVE_WINSOCK2_H)
139check_include_file(ws2tcpip.h HAVE_WS2TCPIP_H)
140
141# check for functions
142check_function_exists(malloc HAVE_MALLOC)
143check_function_exists(memset HAVE_MEMSET)
144check_function_exists(select HAVE_SELECT)
145check_function_exists(socket HAVE_SOCKET)
146check_function_exists(strcasecmp HAVE_STRCASECMP)
147check_function_exists(pthread_mutex_lock HAVE_PTHREAD_MUTEX_LOCK)
148check_function_exists(getaddrinfo HAVE_GETADDRINFO)
149check_function_exists(strnlen HAVE_STRNLEN)
150check_function_exists(strrchr HAVE_STRRCHR)
151check_function_exists(getrandom HAVE_GETRANDOM)
152
153# check for symbols
154if(WIN32)
155  set(HAVE_STRUCT_CMSGHDR 1)
156else()
157  check_symbol_exists(
158    cmsghdr
159    sys/socket.h
160    HAVE_STRUCT_CMSGHDR)
161endif()
162
163if(${WITH_EPOLL}
164   AND ${HAVE_EPOLL_H}
165   AND ${HAVE_TIMERFD_H})
166  set(COAP_EPOLL_SUPPORT "1")
167  message(STATUS "compiling with epoll support")
168else()
169  message(STATUS "compiling without epoll support")
170endif()
171
172if(ENABLE_SMALL_STACK)
173  set(ENABLE_SMALL_STACK "${ENABLE_SMALL_STACK}")
174  message(STATUS "compiling with small stack support")
175endif()
176
177set(WITH_GNUTLS OFF)
178set(WITH_OPENSSL OFF)
179set(WITH_TINYDTLS OFF)
180set(WITH_MBEDTLS OFF)
181
182function(compile_tinydtls)
183  set(TINYDTLS_SOURCES_DIR ${CMAKE_CURRENT_LIST_DIR}/ext/tinydtls)
184  set(TINYDTLS_SOURCES_GENERATED ${TINYDTLS_SOURCES_DIR}/dtls_config.h)
185
186  message(STATUS "compiling the tinydtls lib")
187
188  include(ExternalProject)
189
190  externalproject_add(
191    external_tinydtls
192    SOURCE_DIR "${TINYDTLS_SOURCES_DIR}"
193    BUILD_IN_SOURCE 1
194    DOWNLOAD_COMMAND ""
195    UPDATE_COMMAND ""
196    CONFIGURE_COMMAND
197      ${TINYDTLS_SOURCES_DIR}/configure
198      --disable-manpages
199      --prefix=${CMAKE_BINARY_DIR}
200    BUILD_COMMAND make install
201    INSTALL_COMMAND ""
202    LOG_DOWNLOAD 1
203    LOG_CONFIGURE 1)
204
205  externalproject_add_step(
206    external_tinydtls autoreconf
207    COMMAND autoreconf --force --install
208    ALWAYS 1
209    WORKING_DIRECTORY "${TINYDTLS_SOURCES_DIR}"
210    DEPENDERS configure
211    DEPENDEES download)
212
213  # Let cmake know that it needs to execute the external_tinydtls target to generate those files.
214  add_custom_command(
215    OUTPUT ${TINYDTLS_SOURCES_GENERATED}
216    WORKING_DIRECTORY "${TINYDTLS_SOURCES_DIR}"
217    COMMAND "make install"
218    DEPENDS external_tinydtls)
219
220  add_dependencies(${COAP_LIBRARY_NAME} external_tinydtls)
221
222  if(BUILD_SHARED_LIBS)
223    set(LIBTINYDTLS_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libtinydtls.so")
224  else()
225    set(LIBTINYDTLS_PATH "${CMAKE_CURRENT_BINARY_DIR}/lib/libtinydtls.a")
226  endif()
227
228  add_library(
229    tinydtls
230    UNKNOWN
231    IMPORTED)
232  set_target_properties(
233    tinydtls
234    PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
235               "${CMAKE_CURRENT_BINARY_DIR}/include"
236               IMPORTED_LINK_INTERFACE_LANGUAGES "C"
237               IMPORTED_LOCATION "${LIBTINYDTLS_PATH}")
238
239endfunction()
240
241if(ENABLE_DTLS)
242  message(STATUS "compiling with DTLS support")
243  message(STATUS "DTLS_BACKEND: ${DTLS_BACKEND}")
244
245  if(DTLS_BACKEND
246     STREQUAL
247     "default")
248    # try to find a crypto lib and use it, use the first one found
249
250    # libgnutls (e.g. debian libgnutls28-dev)
251    find_package(GnuTLS)
252    if(GnuTLS_FOUND)
253      set(WITH_GNUTLS ON)
254      message(STATUS "compiling with gnutls support")
255      set(HAVE_LIBGNUTLS 1)
256    else()
257      # gnutls not found
258      find_package(OpenSSL)
259      if(OpenSSL_FOUND)
260        set(WITH_OPENSSL ON)
261        message(STATUS "compiling with openssl support")
262        set(HAVE_OPENSSL 1)
263      else()
264        # openssl not found
265        # libmbedtls (e.g. debian libmbedtls-dev)
266        find_package(MbedTLS)
267        if(MbedTLS_FOUND)
268          set(WITH_MBEDTLS ON)
269          message(STATUS "compiling with mbedtls support")
270          set(HAVE_MBEDTLS 1)
271        else()
272          # mbedtls not found
273          if(USE_VENDORED_TINYDTLS)
274            compile_tinydtls()
275          else()
276            find_package(TinyDTLS)
277            if(TINYDTLS_FOUND)
278
279            else()
280              # no cryto lib found
281              message(
282                FATAL_ERROR
283                  "cannot find any cryto lib, either install one or compile without DTLS support"
284              )
285            endif()
286
287          endif()
288
289          set(WITH_TINYDTLS ON)
290          message(STATUS "compiling with tinydtls support")
291          set(HAVE_LIBTINYDTLS 1)
292
293        endif()
294
295      endif()
296
297    endif()
298
299  else()
300    # DTLS_BACKEND variable is not empty, so set all to false and set the only right to true
301    set(WITH_GNUTLS OFF)
302    set(WITH_TINYDTLS OFF)
303    set(WITH_MBEDTLS OFF)
304    set(WITH_OPENSSL OFF)
305
306    if(DTLS_BACKEND
307       STREQUAL
308       "gnutls")
309      # libgnutls (e.g. debian libgnutls28-dev)
310      find_package(GnuTLS REQUIRED)
311      set(WITH_GNUTLS ON)
312      message(STATUS "compiling with gnutls support")
313      set(HAVE_LIBGNUTLS 1)
314    endif()
315
316    if(DTLS_BACKEND
317       STREQUAL
318       "openssl")
319      # libssl (e.g. debian libssl1.0-dev)
320      find_package(OpenSSL REQUIRED)
321      set(WITH_OPENSSL ON)
322      message(STATUS "compiling with openssl support")
323      set(HAVE_OPENSSL 1)
324    endif()
325
326    if(DTLS_BACKEND
327       STREQUAL
328       "mbedtls")
329      # libmbedtls (e.g. debian libmbedtls-dev)
330      find_package(MbedTLS REQUIRED)
331      set(WITH_MBEDTLS ON)
332      message(STATUS "compiling with mbedtls support")
333      set(HAVE_MBEDTLS 1)
334    endif()
335
336    if(DTLS_BACKEND
337       STREQUAL
338       "tinydtls")
339
340      if(USE_VENDORED_TINYDTLS)
341        compile_tinydtls()
342      else(USE_VENDORED_TINYDTLS)
343        find_package(TinyDTLS REQUIRED)
344      endif(USE_VENDORED_TINYDTLS)
345
346      message(STATUS "compiling with tinydtls support")
347      set(WITH_TINYDTLS ON)
348      set(HAVE_LIBTINYDTLS 1)
349
350    endif()
351
352  endif()
353
354endif()
355
356message(STATUS "ENABLE_DTLS:.....................${ENABLE_DTLS}")
357message(STATUS "ENABLE_TCP:......................${ENABLE_TCP}")
358message(STATUS "ENABLE_DOCS:.....................${ENABLE_DOCS}")
359message(STATUS "ENABLE_EXAMPLES:.................${ENABLE_EXAMPLES}")
360message(STATUS "DTLS_BACKEND:....................${DTLS_BACKEND}")
361message(STATUS "WITH_GNUTLS:.....................${WITH_GNUTLS}")
362message(STATUS "WITH_TINYDTLS:...................${WITH_TINYDTLS}")
363message(STATUS "WITH_OPENSSL:....................${WITH_OPENSSL}")
364message(STATUS "WITH_MBEDTLS:....................${WITH_MBEDTLS}")
365message(STATUS "HAVE_LIBTINYDTLS:................${HAVE_LIBTINYDTLS}")
366message(STATUS "HAVE_LIBGNUTLS:..................${HAVE_LIBGNUTLS}")
367message(STATUS "HAVE_OPENSSL:....................${HAVE_OPENSSL}")
368message(STATUS "HAVE_MBEDTLS:....................${HAVE_MBEDTLS}")
369message(STATUS "COAP_EPOLL_SUPPORT:..............${COAP_EPOLL_SUPPORT}")
370message(STATUS "CMAKE_C_COMPILER:................${CMAKE_C_COMPILER}")
371message(STATUS "BUILD_SHARED_LIBS:...............${BUILD_SHARED_LIBS}")
372message(STATUS "CMAKE_BUILD_TYPE:................${CMAKE_BUILD_TYPE}")
373message(STATUS "CMAKE_SYSTEM_PROCESSOR:..........${CMAKE_SYSTEM_PROCESSOR}")
374
375set(PACKAGE_URL "https://libcoap.net/")
376set(PACKAGE_NAME "${PROJECT_NAME}")
377set(PACKAGE_TARNAME "${PROJECT_NAME}")
378set(PACKAGE_STRING "${PROJECT_NAME} ${PROJECT_VERSION}")
379set(PACKAGE_VERSION "${PROJECT_VERSION}")
380set(LIBCOAP_PACKAGE_VERSION "${PROJECT_VERSION}")
381set(LIBCOAP_PACKAGE_URL "${PACKAGE_URL}")
382set(LIBCOAP_PACKAGE_NAME "${PROJECT_NAME}")
383set(LIBCOAP_PACKAGE_STRING "TODO")
384set(LIBCOAP_PACKAGE_BUGREPORT "libcoap-developers@lists.sourceforge.net")
385set(PACKAGE_BUGREPORT "libcoap-developers@lists.sourceforge.net")
386set(top_srcdir "${CMAKE_CURRENT_LIST_DIR}")
387set(top_builddir "${CMAKE_CURRENT_BINARY_DIR}")
388if(ENABLE_TCP)
389  set(COAP_DISABLE_TCP 0)
390else(ENABLE_TCP)
391  set(COAP_DISABLE_TCP 1)
392endif(ENABLE_TCP)
393
394# creates config header file in build directory
395configure_file(${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap.h.in
396               ${CMAKE_CURRENT_BINARY_DIR}/include/coap${LIBCOAP_API_VERSION}/coap.h)
397
398configure_file(${CMAKE_CURRENT_LIST_DIR}/cmake_coap_config.h.in
399               ${CMAKE_CURRENT_BINARY_DIR}/coap_config.h)
400
401configure_file(${CMAKE_CURRENT_LIST_DIR}/tests/test_common.h.in
402               ${CMAKE_CURRENT_LIST_DIR}/tests/test_common.h)
403
404#
405# sources
406#
407
408target_sources(
409  ${COAP_LIBRARY_NAME}
410  PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/address.c
411          ${CMAKE_CURRENT_LIST_DIR}/src/net.c
412          ${CMAKE_CURRENT_LIST_DIR}/src/coap_debug.c
413          ${CMAKE_CURRENT_LIST_DIR}/src/encode.c
414          ${CMAKE_CURRENT_LIST_DIR}/src/uri.c
415          ${CMAKE_CURRENT_LIST_DIR}/src/subscribe.c
416          ${CMAKE_CURRENT_LIST_DIR}/src/resource.c
417          ${CMAKE_CURRENT_LIST_DIR}/src/str.c
418          ${CMAKE_CURRENT_LIST_DIR}/src/option.c
419          ${CMAKE_CURRENT_LIST_DIR}/src/async.c
420          ${CMAKE_CURRENT_LIST_DIR}/src/block.c
421          ${CMAKE_CURRENT_LIST_DIR}/src/coap_asn1.c
422          ${CMAKE_CURRENT_LIST_DIR}/src/coap_cache.c
423          ${CMAKE_CURRENT_LIST_DIR}/src/coap_debug.c
424          ${CMAKE_CURRENT_LIST_DIR}/src/coap_event.c
425          ${CMAKE_CURRENT_LIST_DIR}/src/coap_hashkey.c
426          ${CMAKE_CURRENT_LIST_DIR}/src/coap_io.c
427          ${CMAKE_CURRENT_LIST_DIR}/src/coap_notls.c
428          ${CMAKE_CURRENT_LIST_DIR}/src/coap_prng.c
429          ${CMAKE_CURRENT_LIST_DIR}/src/coap_session.c
430          ${CMAKE_CURRENT_LIST_DIR}/src/coap_tcp.c
431          ${CMAKE_CURRENT_LIST_DIR}/src/coap_time.c
432          ${CMAKE_CURRENT_LIST_DIR}/src/encode.c
433          ${CMAKE_CURRENT_LIST_DIR}/src/mem.c
434          ${CMAKE_CURRENT_LIST_DIR}/src/net.c
435          ${CMAKE_CURRENT_LIST_DIR}/src/option.c
436          ${CMAKE_CURRENT_LIST_DIR}/src/pdu.c
437          ${CMAKE_CURRENT_LIST_DIR}/src/resource.c
438          ${CMAKE_CURRENT_LIST_DIR}/src/str.c
439          ${CMAKE_CURRENT_LIST_DIR}/src/subscribe.c
440          ${CMAKE_CURRENT_LIST_DIR}/src/uri.c
441          # no need to parse those files if we do not need them
442          $<$<BOOL:${HAVE_OPENSSL}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_openssl.c>
443          $<$<BOOL:${HAVE_LIBTINYDTLS}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_tinydtls.c>
444          $<$<BOOL:${HAVE_LIBGNUTLS}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_gnutls.c>
445          $<$<BOOL:${HAVE_MBEDTLS}>:${CMAKE_CURRENT_LIST_DIR}/src/coap_mbedtls.c>
446          # headers
447          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/address.h
448          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/async.h
449          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/block.h
450          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_cache.h
451          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_debug.h
452          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_dtls.h
453          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_event.h
454          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap.h
455          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_hashkey.h
456          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap.h.in
457          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_io.h
458          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_session.h
459          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_time.h
460          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/encode.h
461          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/libcoap.h
462          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/lwippools.h
463          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/mem.h
464          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/net.h
465          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/option.h
466          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/pdu.h
467          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/coap_prng.h
468          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/resource.h
469          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/str.h
470          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/subscribe.h
471          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/uri.h
472          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/uthash.h
473          ${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}/utlist.h)
474target_include_directories(
475  ${COAP_LIBRARY_NAME}
476  PUBLIC # config headers are generated during configuration time
477         $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/>
478         $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/>
479         $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/>
480         $<INSTALL_INTERFACE:include/>
481         $<$<AND:$<BOOL:${HAVE_LIBTINYDTLS}>,$<BOOL:${USE_VENDORED_TINYDTLS}>>:${CMAKE_BINARY_DIR}/include/tinydtls>
482         $<$<BOOL:${HAVE_LIBGNUTLS}>:${GNUTLS_INCLUDE_DIR}>
483         $<$<BOOL:${HAVE_LIBGNUTLS}>:${MBEDTLS_INCLUDE_DIRS}>)
484target_link_libraries(
485  ${COAP_LIBRARY_NAME}
486  PUBLIC $<$<BOOL:${HAVE_OPENSSL}>:OpenSSL::SSL>
487         $<$<BOOL:${HAVE_OPENSSL}>:OpenSSL::Crypto>
488         $<$<BOOL:${HAVE_LIBGNUTLS}>:${GNUTLS_LIBRARIES}>
489         $<$<BOOL:${HAVE_LIBTINYDTLS}>:tinydtls>
490         $<$<BOOL:${HAVE_MBEDTLS}>:${MBEDTLS_LIBRARY}>
491         $<$<BOOL:${HAVE_MBEDTLS}>:${MBEDX509_LIBRARY}>
492         $<$<BOOL:${HAVE_MBEDTLS}>:${MBEDCRYPTO_LIBRARY}>)
493
494add_library(
495  ${PROJECT_NAME}::${COAP_LIBRARY_NAME}
496  ALIAS
497  ${COAP_LIBRARY_NAME})
498
499#
500# compiler options
501#
502
503add_compile_options(
504  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-pedantic>
505  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wall>
506  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wcast-qual>
507  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wextra>
508  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wformat-security>
509  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Winline>
510  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wmissing-declarations>
511  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wmissing-prototypes>
512  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wnested-externs>
513  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wpointer-arith>
514  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wshadow>
515  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wstrict-prototypes>
516  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wswitch-default>
517  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wswitch-enum>
518  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wunused>
519  $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wwrite-strings>)
520
521#
522# tests
523#
524
525if(ENABLE_TESTS)
526  add_executable(
527    testdriver
528    ${CMAKE_CURRENT_LIST_DIR}/tests/testdriver.c
529    ${CMAKE_CURRENT_LIST_DIR}/tests/test_common.h
530    ${CMAKE_CURRENT_LIST_DIR}/tests/test_encode.c
531    ${CMAKE_CURRENT_LIST_DIR}/tests/test_encode.h
532    ${CMAKE_CURRENT_LIST_DIR}/tests/test_error_response.c
533    ${CMAKE_CURRENT_LIST_DIR}/tests/test_error_response.h
534    ${CMAKE_CURRENT_LIST_DIR}/tests/test_options.c
535    ${CMAKE_CURRENT_LIST_DIR}/tests/test_options.h
536    ${CMAKE_CURRENT_LIST_DIR}/tests/test_pdu.c
537    ${CMAKE_CURRENT_LIST_DIR}/tests/test_pdu.h
538    ${CMAKE_CURRENT_LIST_DIR}/tests/test_sendqueue.c
539    ${CMAKE_CURRENT_LIST_DIR}/tests/test_sendqueue.h
540    ${CMAKE_CURRENT_LIST_DIR}/tests/test_session.c
541    ${CMAKE_CURRENT_LIST_DIR}/tests/test_session.h
542    ${CMAKE_CURRENT_LIST_DIR}/tests/test_tls.c
543    ${CMAKE_CURRENT_LIST_DIR}/tests/test_tls.h
544    ${CMAKE_CURRENT_LIST_DIR}/tests/test_uri.c
545    ${CMAKE_CURRENT_LIST_DIR}/tests/test_uri.h
546    ${CMAKE_CURRENT_LIST_DIR}/tests/test_wellknown.c
547    ${CMAKE_CURRENT_LIST_DIR}/tests/test_wellknown.h)
548  # tests require libcunit (e.g. debian libcunit1-dev)
549  target_link_libraries(testdriver PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME}
550                                          -lcunit)
551endif()
552
553#
554# examples
555#
556
557if(ENABLE_EXAMPLES)
558  add_executable(coap-client ${CMAKE_CURRENT_LIST_DIR}/examples/coap-client.c)
559  target_link_libraries(coap-client
560                        PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
561
562  add_executable(coap-rd ${CMAKE_CURRENT_LIST_DIR}/examples/coap-rd.c)
563  target_include_directories(coap-rd
564    PRIVATE
565         $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/coap${LIBCOAP_API_VERSION}>)
566  target_link_libraries(coap-rd PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
567
568  add_executable(coap-server ${CMAKE_CURRENT_LIST_DIR}/examples/coap-server.c)
569  target_link_libraries(coap-server
570                        PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
571
572  if(NOT WIN32)
573    add_executable(etsi_iot_01 ${CMAKE_CURRENT_LIST_DIR}/examples/etsi_iot_01.c)
574    target_link_libraries(etsi_iot_01
575                          PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
576
577    add_executable(tiny ${CMAKE_CURRENT_LIST_DIR}/examples/tiny.c)
578    target_link_libraries(tiny PUBLIC ${PROJECT_NAME}::${COAP_LIBRARY_NAME})
579  endif()
580endif()
581
582#
583# docs
584#
585
586if(ENABLE_DOCS)
587  find_package(Doxygen)
588
589  if(Doxygen_FOUND)
590    # set input and output files
591    set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in)
592    set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
593
594    # Make necessary temporary directories
595    file(MAKE_DIRECTORY  ${CMAKE_BINARY_DIR}/doc/man_tmp)
596    file(MAKE_DIRECTORY  ${CMAKE_BINARY_DIR}/doc/man_html)
597
598    # request to configure the file
599    configure_file(
600      ${DOXYGEN_IN}
601      ${DOXYGEN_OUT}
602      @ONLY)
603
604    # note the option ALL which allows to build the docs together with the
605    # application
606    add_custom_target(
607      doc_doxygen ALL
608      COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
609      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
610      COMMENT "Generating API documentation with Doxygen"
611      VERBATIM)
612
613    message(STATUS "Setup up the Doxygen documention build")
614
615  else(Doxygen_FOUND)
616    message(
617      WARNING
618        "Doxygen need to be installed to generate the doxygen documentation")
619  endif(Doxygen_FOUND)
620
621endif()
622
623#
624# install
625#
626
627include(GNUInstallDirs)
628include(CMakePackageConfigHelpers)
629
630set(LIBCOAP_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
631install(
632  TARGETS ${COAP_LIBRARY_NAME}
633  EXPORT ${PROJECT_NAME}Targets
634  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib
635  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib)
636
637install(
638  EXPORT ${PROJECT_NAME}Targets
639  DESTINATION ${LIBCOAP_CONFIG_INSTALL_DIR}
640  NAMESPACE ${PROJECT_NAME}::
641  COMPONENT dev)
642
643configure_package_config_file(
644  cmake/Config.cmake.in
645  ${PROJECT_NAME}Config.cmake
646  INSTALL_DESTINATION
647  ${LIBCOAP_CONFIG_INSTALL_DIR})
648write_basic_package_version_file(
649  ${PROJECT_NAME}ConfigVersion.cmake
650  COMPATIBILITY SameMajorVersion)
651install(
652  FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
653        ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
654  DESTINATION ${LIBCOAP_CONFIG_INSTALL_DIR}
655  COMPONENT dev)
656
657install(
658  DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/
659  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
660  COMPONENT dev
661  FILES_MATCHING
662  PATTERN "*.h"
663  PATTERN "coap.h" EXCLUDE
664  PATTERN "coap_riot.h" EXCLUDE
665  PATTERN "lwippools.h" EXCLUDE
666  PATTERN "utlist.h" EXCLUDE
667  PATTERN "*_internal.h" EXCLUDE)
668install(
669  DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
670  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
671  COMPONENT dev
672  FILES_MATCHING
673  PATTERN "*.h")
674