From dcfaf85fe13a120e13d5c72896a12edd9c5d0fd5 Mon Sep 17 00:00:00 2001 From: Samuel Debionne Date: Wed, 23 Aug 2023 16:13:36 +0200 Subject: [PATCH 1/2] Add CMake support --- CMakeLists.txt | 95 +++++++++++++++++++++++++++++++++++ cmake/PackageConfig.cmake | 35 +++++++++++++ cmake/project-config.cmake.in | 16 ++++++ src/cbor.h | 2 +- tests/CMakeLists.txt | 3 ++ 5 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 CMakeLists.txt create mode 100644 cmake/PackageConfig.cmake create mode 100644 cmake/project-config.cmake.in create mode 100644 tests/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..08d5d451 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,95 @@ +# /**************************************************************************** +# ** +# ** Copyright (C) 2015 Intel Corporation +# ** +# ** Permission is hereby granted, free of charge, to any person obtaining a copy +# ** of this software and associated documentation files (the "Software"), to deal +# ** in the Software without restriction, including without limitation the rights +# ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# ** copies of the Software, and to permit persons to whom the Software is +# ** furnished to do so, subject to the following conditions: +# ** +# ** The above copyright notice and this permission notice shall be included in +# ** all copies or substantial portions of the Software. +# ** +# ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# ** THE SOFTWARE. +# ** +# ****************************************************************************/ + +cmake_minimum_required(VERSION 3.10) + +project(tinycbor LANGUAGES C VERSION 0.6.0) + +# Set path to additional cmake scripts +set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) + +set(TARGETS_EXPORT_NAME "tinycbor-targets") + +# Include additional modules that are used unconditionally +include(GNUInstallDirs) +include(GenerateExportHeader) + +add_library(tinycbor SHARED + src/cborencoder.c + src/cborencoder_close_container_checked.c + src/cborerrorstrings.c + src/cborparser.c + src/cborparser_dup_string.c + src/cborpretty.c + src/cborpretty_stdio.c + src/cbortojson.c + src/cborvalidation.c + src/cbor.h + src/tinycbor-version.h +) + +# Generate export macros +generate_export_header(tinycbor BASE_NAME "cbor" EXPORT_MACRO_NAME "CBOR_API" EXPORT_FILE_NAME "tinycbor-export.h") + +# Check for open_memstream and store the result in HAVE_OPEN_MEMSTREAM +include (CheckSymbolExists) +check_symbol_exists(open_memstream stdio.h HAVE_OPEN_MEMSTREAM) +check_symbol_exists(funopen stdio.h HAVE_OPEN_FUNOPEN) +check_symbol_exists(fopencookie stdio.h HAVE_OPEN_FOPENCOOKIE) + +if(NOT HAVE_OPEN_MEMSTREAM) + if (HAVE_OPEN_FUNOPEN AND HAVE_OPEN_FOPENCOOKIE) + message(STATUS "using open_memstream implementation") + target_sources(tinycbor PRIVATE src/open_memstream.c) + else() + target_compile_definitions(tinycbor PRIVATE WITHOUT_OPEN_MEMSTREAM) + message(WARNING "funopen and fopencookie unavailable, open_memstream can not be implemented and conversion to JSON will not work properly!") + endif() +endif() + +target_include_directories(tinycbor + PUBLIC "$" + PUBLIC "$" + PUBLIC "$") + +# Set version and output name +set_target_properties(tinycbor PROPERTIES + VERSION "${PROJECT_VERSION}" + SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}") + +install(FILES src/cbor.h src/tinycbor-version.h ${CMAKE_BINARY_DIR}/tinycbor-export.h TYPE INCLUDE) +install( + TARGETS tinycbor + EXPORT "${TARGETS_EXPORT_NAME}" + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # import library + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # .so files are libraries + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # .dll files are binaries + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # this does not actually install anything (but used by downstream projects) +) + +set (PROJECT_LIBRARIES tinycbor) +include(PackageConfig) + +enable_testing() +add_subdirectory(tests) diff --git a/cmake/PackageConfig.cmake b/cmake/PackageConfig.cmake new file mode 100644 index 00000000..ec9cff87 --- /dev/null +++ b/cmake/PackageConfig.cmake @@ -0,0 +1,35 @@ +# This cmake code creates the configuration that is found and used by +# find_package() of another cmake project + +# get lower and upper case project name for the configuration files + +# configure and install the configuration files +include(CMakePackageConfigHelpers) + +configure_package_config_file( + "${CMAKE_SOURCE_DIR}/cmake/project-config.cmake.in" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + #PATH_VARS CMAKE_INSTALL_DIR +) + +write_basic_package_version_file( + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMinorVersion +) + +install(FILES + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake" + COMPONENT devel + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} +) + +if (PROJECT_LIBRARIES OR PROJECT_STATIC_LIBRARIES) + install( + EXPORT "${TARGETS_EXPORT_NAME}" + FILE ${TARGETS_EXPORT_NAME}.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + ) +endif () diff --git a/cmake/project-config.cmake.in b/cmake/project-config.cmake.in new file mode 100644 index 00000000..6729c5cb --- /dev/null +++ b/cmake/project-config.cmake.in @@ -0,0 +1,16 @@ +# Config file for @PROJECT_NAME_LOWER@ +# +# It defines the following variables: +# +# @PROJECT_NAME_UPPER@_INCLUDE_DIRS - include directory +# @PROJECT_NAME_UPPER@_LIBRARIES - all dynamic libraries +# @PROJECT_NAME_UPPER@_STATIC_LIBRARIES - all static libraries + +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) + +# Add optional dependencies here + +include("${CMAKE_CURRENT_LIST_DIR}/@TARGETS_EXPORT_NAME@.cmake") +check_required_components("@PROJECT_NAME@") diff --git a/src/cbor.h b/src/cbor.h index 31b1036e..a150164f 100644 --- a/src/cbor.h +++ b/src/cbor.h @@ -39,6 +39,7 @@ #endif #include "tinycbor-version.h" +#include "tinycbor-export.h" #define TINYCBOR_VERSION ((TINYCBOR_VERSION_MAJOR << 16) | (TINYCBOR_VERSION_MINOR << 8) | TINYCBOR_VERSION_PATCH) @@ -725,4 +726,3 @@ CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value #endif #endif /* CBOR_H */ - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..92a2be06 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(tst_c90 c90/tst_c90.c) +target_link_libraries(tst_c90 tinycbor) +add_test(NAME c90 COMMAND tst_c90) From 2f1b80bc0c31295d8d47982191f195f45e4be5c5 Mon Sep 17 00:00:00 2001 From: Samuel Debionne Date: Wed, 23 Aug 2023 16:14:19 +0200 Subject: [PATCH 2/2] Fix static_assert compiler support --- src/compilersupport_p.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compilersupport_p.h b/src/compilersupport_p.h index 3e133a32..095eeb9e 100644 --- a/src/compilersupport_p.h +++ b/src/compilersupport_p.h @@ -44,7 +44,7 @@ # include #endif -#if __STDC_VERSION__ >= 201112L || (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(__cpp_static_assert) && __cpp_static_assert >= 200410) +#if __STDC_VERSION__ >= 202311L || (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(__cpp_static_assert) && __cpp_static_assert >= 200410) # define cbor_static_assert(x) static_assert(x, #x) #elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) && (__STDC_VERSION__ > 199901L) # define cbor_static_assert(x) _Static_assert(x, #x)