Skip to content

Commit

Permalink
cmake: add support for getrandom and libcunit.
Browse files Browse the repository at this point in the history
Add also license headers and apply minor fixes.
Append version to shared library on install.
Amend README.md with instructions to use cmake.

Signed-off-by: Achim Kraus <achim.kraus@bosch.io>
  • Loading branch information
Achim Kraus committed May 14, 2022
1 parent 9458710 commit f709162
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ dtls_config.h
*.z1
*.z1sp
.project
# cmake
CMakeCache.txt
cmake_install.cmake
CMakeFiles
45 changes: 40 additions & 5 deletions AutoConf.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
###############################################################################
#
# Copyright (c) 2022 Contributors to the Eclipse Foundation
#
# See the LICENSE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-2.0
# and the Eclipse Distribution License v. 1.0
# available at http://www.eclipse.org/org/documents/edl-v10.php
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Jimmy Björklund - initial version
# Achim Kraus - add getrandom and libcunit
# additional minor fixes
#
###############################################################################

include(CheckIncludeFile)
include(CheckFunctionExists)
include(CheckLibraryExists)
include(TestBigEndian)
include(CheckCSourceCompiles)
include(CheckStructHasMember)
Expand Down Expand Up @@ -34,15 +57,27 @@ check_function_exists (strerror HAVE_STRERROR)
check_function_exists (strnlen HAVE_STRNLEN)
check_function_exists (fls HAVE_FLS)
check_function_exists (vprintf HAVE_VPRINTF)
check_function_exists (getrandom HAVE_GETRANDOM)

if( ${make_tests} )
if(BUILD_SHARED_LIBS)
check_library_exists (libcunit.so CU_initialize_registry "" HAVE_LIBCUNIT)
else()
# this link options only intended to be used for the cunit tests
set(CMAKE_REQUIRED_LINK_OPTIONS -no-pie)
check_library_exists (libcunit.a CU_initialize_registry "" HAVE_LIBCUNIT)
endif()
endif()

if( ${HAVE_STRING_H} AND ${HAVE_STRINGS_H} AND
${HAVE_FLOAT_H} AND ${HAVE_STDLIB_H} AND
${HAVE_STDDEF_H} AND ${HAVE_STDINT_H} AND
${HAVE_INTTYPES_H} AND ${HAVE_DLFCN_H} )
if( "${HAVE_STRING_H}" AND "${HAVE_STRINGS_H}" AND
"${HAVE_FLOAT_H}" AND "${HAVE_STDLIB_H}" AND
"${HAVE_STDDEF_H}" AND "${HAVE_STDINT_H}" AND
"${HAVE_INTTYPES_H}" AND "${HAVE_DLFCN_H}" )
set( STDC_HEADERS 1)
endif()

check_struct_has_member (struct sockaddr_in6.sin6_len netinet/in.h HAVE_SOCKADDR_IN6_SIN6_LEN)
check_struct_has_member ("struct sockaddr_in6" sin6_len "netinet/in.h" HAVE_SOCKADDR_IN6_SIN6_LEN)

TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
set(WORDS_BIGENDIAN 1)
Expand Down
66 changes: 54 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
###############################################################################
#
# Copyright (c) 2022 Contributors to the Eclipse Foundation
#
# See the LICENSE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-2.0
# and the Eclipse Distribution License v. 1.0
# available at http://www.eclipse.org/org/documents/edl-v10.php
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Jimmy Björklund - initial version
# Achim Kraus - minor fixes
#
###############################################################################

cmake_minimum_required(VERSION 3.5)

project(tinydtls)

include (AutoConf.cmake)

option(BUILD_SHARED_LIBS "Link using shared libs" OFF)
option(make_tests "Make test programs" OFF)
option(make_tests "Make test programs and examples" OFF)

if(NOT PLATFORM)
set(PLATFORM "posix" CACHE STRING "Choose platform." FORCE)
Expand All @@ -12,24 +35,43 @@ endif()

set(PACKAGE_NAME "tinydtls")
set(PACKAGE_VERSION "0.8.6" )

option(DTLS_ECC "disable support for TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" ON )
option(DTLS_PSK "disable support for TLS_PSK_WITH_AES_128_CCM_8" ON)
set(SOVERSION "0" )

option(DTLS_ECC "disable/enable support for TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" ON )
option(DTLS_PSK "disable/enable support for TLS_PSK_WITH_AES_128_CCM_8" ON)

configure_file(dtls_config.h.cmake.in dtls_config.h )

file(GLOB SOURCE "*.c" "ecc/ecc.c" "aes/*.c" "sha2/sha2.c" "platform/dtls_prng_${PLATFORM}.c")
add_library(tinydtls)

target_sources(tinydtls PRIVATE
dtls.c
netq.c
peer.c
session.c
crypto.c
ccm.c
hmac.c
dtls_time.c
dtls_debug.c
dtls_prng.c
aes/rijndael.c
aes/rijndael_wrap.c
sha2/sha2.c
ecc/ecc.c)

add_library(tinydtls ${SOURCE})
target_compile_options(tinydtls PRIVATE -Wall -pedantic -std=c99 -DSHA2_USE_INTTYPES_H -fPIC -pedantic -Wall -Wextra -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wunused -DDTLSv12 -DWITH_SHA256 -DDTLS_CHECK_CONTENTTYPE)
target_compile_options(tinydtls PUBLIC -DDTLSv12 -DWITH_SHA256)
target_include_directories(tinydtls PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES_H DTLS_CHECK_CONTENTTYPE)
target_compile_options(tinydtls PRIVATE -fPIC -pedantic -std=c99 -Wall -Wextra -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wunused)

set_target_properties(tinydtls PROPERTIES VERSION ${PACKAGE_VERSION} SOVERSION ${SOVERSION})

if( ${make_tests} )
add_subdirectory(tests)
endif()


if(${BUILD_SHARED_LIBS})
install(TARGETS tinydtls DESTINATION /usr/local/lib )
endif()
if(BUILD_SHARED_LIBS)
install(TARGETS tinydtls LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} )
else()
install(TARGETS tinydtls DESTINATION ${CMAKE_INSTALL_LIBDIR} )
endif()
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ to your application.
Also, if you need a specific commit of tinyDTLS you can modify
`RIOT/pkg/tinydtls/Makefile`.

## CMake

The current cmake support is experimental. Don't hesitate to report issues and/or provided fixes for it. For general and more details on using CMake, please consider [CMake - help](https://cmake.org/cmake/help/latest/index.html).

Usage:

```
mkdir tinydtls_build
cd tinydtls_build
cmake -Dmake_tests=ON <path-to-tinydtls>
cmake --build .
```

Available options:

| Option | Description | Default |
| ------ | ----------- | ------- |
| BUILD_SHARED_LIBS | build shared libraries instead of static link library | OFF |
| make_tests | build tests including the examples | OFF |
| DTLS_ECC | enable/disable ECDHE_ECDSA cipher suites | ON |
| DTLS_PSK | enable/disable PSK cipher suites | ON |

# License

Copyright (c) 2011–2022 Olaf Bergmann (TZI) and others.
Expand Down
29 changes: 28 additions & 1 deletion dtls_config.h.cmake.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
/* dtls_config.h.cmake.in. Generated from cmake */
/*******************************************************************************
*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the LICENSE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0
* which is available at http://www.eclipse.org/legal/epl-2.0
* and the Eclipse Distribution License v. 1.0
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Jimmy Björklund - initial version
* Achim Kraus - add getrandom and libcunit
*
* dtls_config.h: Generated by cmake from dtls_config.h.cmake.in
*
******************************************************************************/

/* Define if building universal (internal helper macro) */
#cmakedefine AC_APPLE_UNIVERSAL_BUILD
Expand All @@ -21,9 +42,15 @@
/* Define to 1 if you have the `fls' function. */
#cmakedefine HAVE_FLS 1

/* Define to 1 if you have the `getrandom' function. */
#cmakedefine HAVE_GETRANDOM 1

/* Define to 1 if you have the <inttypes.h> header file. */
#cmakedefine HAVE_INTTYPES_H 1

/* Define if the system has libcunit */
#cmakedefine HAVE_LIBCUNIT 1

/* Define to 1 if you have the <memory.h> header file. */
#cmakedefine HAVE_MEMORY_H 1

Expand Down
23 changes: 23 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
###############################################################################
#
# Copyright (c) 2022 Contributors to the Eclipse Foundation
#
# See the LICENSE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-2.0
# and the Eclipse Distribution License v. 1.0
# available at http://www.eclipse.org/org/documents/edl-v10.php
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Jimmy Björklund - initial version
# Achim Kraus - minor fixes
#
###############################################################################

cmake_minimum_required(VERSION 3.5)

project(tinydtls-tests)

add_executable(dtls-server dtls-server.c)
target_link_libraries(dtls-server LINK_PUBLIC tinydtls)
target_compile_options(dtls-server PUBLIC -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256)
Expand Down

0 comments on commit f709162

Please sign in to comment.