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