1cmake_minimum_required(VERSION 2.8) 2project(libcbor) 3set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/") 4include(CTest) 5 6SET(CBOR_VERSION_MAJOR "0") 7SET(CBOR_VERSION_MINOR "8") 8SET(CBOR_VERSION_PATCH "0") 9SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH}) 10 11set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true) 12include(CheckIncludeFiles) 13 14include(TestBigEndian) 15test_big_endian(BIG_ENDIAN) 16if(BIG_ENDIAN) 17 add_definitions(-DIS_BIG_ENDIAN) 18endif() 19 20option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF) 21option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON) 22set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking") 23set(CBOR_MAX_STACK_SIZE "2048" CACHE STRING "maximum size for decoding context stack") 24 25option(WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF) 26if(WITH_TESTS) 27 add_definitions(-DWITH_TESTS) 28endif(WITH_TESTS) 29 30option(WITH_EXAMPLES "Build examples" ON) 31 32option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF) 33if(HUGE_FUZZ) 34 add_definitions(-DHUGE_FUZZ) 35endif(HUGE_FUZZ) 36 37option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF) 38if(SANE_MALLOC) 39 add_definitions(-DSANE_MALLOC) 40endif(SANE_MALLOC) 41 42option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF) 43if(PRINT_FUZZ) 44 add_definitions(-DPRINT_FUZZ) 45endif(PRINT_FUZZ) 46 47option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON) 48 49set(CPACK_GENERATOR "DEB" "TGZ" "RPM") 50set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64") 51set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda") 52set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6") 53set(CPACK_PACKAGE_VERSION_MAJOR ${CBOR_VERSION_MAJOR}) 54set(CPACK_PACKAGE_VERSION_MINOR ${CBOR_VERSION_MINOR}) 55set(CPACK_PACKAGE_VERSION_PATCH ${CBOR_VERSION_PATCH}) 56 57include(CPack) 58 59if(MINGW) 60 # https://github.com/PJK/libcbor/issues/13 61 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") 62elseif(NOT MSVC) 63 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic") 64endif() 65 66if(MSVC) 67 # This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx 68 set(CBOR_RESTRICT_SPECIFIER "") 69else() 70 set(CBOR_RESTRICT_SPECIFIER "restrict") 71 72 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -g -ggdb -DDEBUG=true") 73 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -DNDEBUG") 74 75 if(SANITIZE) 76 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} \ 77 -fsanitize=undefined -fsanitize=address \ 78 -fsanitize=bounds -fsanitize=alignment") 79 endif() 80endif() 81 82set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g") 83 84 85include(CheckTypeSize) 86check_type_size("size_t" SIZEOF_SIZE_T) 87if(SIZEOF_SIZE_T LESS 8) 88 message(WARNING "Your size_t is less than 8 bytes. Long items with 64b length specifiers might not work as expected. Make sure to run the tests!") 89else() 90 add_definitions(-DEIGHT_BYTE_SIZE_T) 91endif() 92 93enable_testing() 94 95set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind") 96set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1") 97 98add_custom_target(coverage 99 COMMAND ctest 100 COMMAND lcov --capture --directory . --output-file coverage.info 101 COMMAND genhtml coverage.info --highlight --legend --output-directory coverage_html 102 COMMAND echo "Coverage report ready: file://${CMAKE_CURRENT_BINARY_DIR}/coverage_html/index.html") 103include_directories(src) 104 105 106option(COVERAGE "Enable code coverage instrumentation" OFF) 107if (COVERAGE) 108 message("Configuring code coverage instrumentation") 109 if(NOT CMAKE_C_COMPILER MATCHES "gcc") 110 message(WARNING "Gcov instrumentation only works with GCC") 111 endif() 112 # https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html 113 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage --coverage") 114 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage --coverage") 115endif (COVERAGE) 116 117 118# We want to generate configuration.h from the template and make it so that it is accessible using the same 119# path during both library build and installed header use, without littering the source dir. 120# Using cbor/configuration.h in the build dir works b/c headers will be installed to <prefix>/cbor 121configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in ${PROJECT_BINARY_DIR}/cbor/configuration.h) 122install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION include/cbor) 123# Make the header visible at compile time 124include_directories(${PROJECT_BINARY_DIR}) 125 126# CMake >= 3.9.0 enables LTO for GCC and Clang with INTERPROCEDURAL_OPTIMIZATION 127# Policy CMP0069 enables this behavior when we set the minimum CMake version < 3.9.0 128# Checking for LTO support before setting INTERPROCEDURAL_OPTIMIZATION is mandatory with CMP0069 set to NEW. 129set(use_lto FALSE) 130if(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0") 131 cmake_policy(SET CMP0069 NEW) 132 # Require LTO support to build libcbor with newer CMake versions 133 include(CheckIPOSupported) 134 check_ipo_supported(RESULT use_lto) 135endif(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0") 136if(use_lto) 137 message(STATUS "LTO is enabled") 138else() 139 message(STATUS "LTO is not enabled") 140endif(use_lto) 141 142subdirs(src) 143if(use_lto) 144 set_property(DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) 145endif(use_lto) 146 147if (WITH_TESTS) 148 subdirs(test) 149 if(use_lto) 150 set_property(DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) 151 endif(use_lto) 152endif (WITH_TESTS) 153 154if (WITH_EXAMPLES) 155 subdirs(examples) 156 if(use_lto) 157 set_property(DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) 158 endif(use_lto) 159endif (WITH_EXAMPLES) 160