1 cmake_minimum_required(VERSION 2.8)
3 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
6 SET(CBOR_VERSION_MAJOR "0")
7 SET(CBOR_VERSION_MINOR "8")
8 SET(CBOR_VERSION_PATCH "0")
9 SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
11 set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
12 include(CheckIncludeFiles)
14 include(TestBigEndian)
15 test_big_endian(BIG_ENDIAN)
17 add_definitions(-DIS_BIG_ENDIAN)
20 option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
21 option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
22 set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking")
23 set(CBOR_MAX_STACK_SIZE "2048" CACHE STRING "maximum size for decoding context stack")
25 option(WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF)
27 add_definitions(-DWITH_TESTS)
30 option(WITH_EXAMPLES "Build examples" ON)
32 option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF)
34 add_definitions(-DHUGE_FUZZ)
37 option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF)
39 add_definitions(-DSANE_MALLOC)
42 option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF)
44 add_definitions(-DPRINT_FUZZ)
47 option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON)
49 set(CPACK_GENERATOR "DEB" "TGZ" "RPM")
50 set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
51 set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda")
52 set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
53 set(CPACK_PACKAGE_VERSION_MAJOR ${CBOR_VERSION_MAJOR})
54 set(CPACK_PACKAGE_VERSION_MINOR ${CBOR_VERSION_MINOR})
55 set(CPACK_PACKAGE_VERSION_PATCH ${CBOR_VERSION_PATCH})
60 # https://github.com/PJK/libcbor/issues/13
61 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
63 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic")
67 # This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
68 set(CBOR_RESTRICT_SPECIFIER "")
70 set(CBOR_RESTRICT_SPECIFIER "restrict")
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")
76 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} \
77 -fsanitize=undefined -fsanitize=address \
78 -fsanitize=bounds -fsanitize=alignment")
82 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
85 include(CheckTypeSize)
86 check_type_size("size_t" SIZEOF_SIZE_T)
87 if(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!")
90 add_definitions(-DEIGHT_BYTE_SIZE_T)
95 set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
96 set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
98 add_custom_target(coverage
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")
103 include_directories(src)
106 option(COVERAGE "Enable code coverage instrumentation" OFF)
108 message("Configuring code coverage instrumentation")
109 if(NOT CMAKE_C_COMPILER MATCHES "gcc")
110 message(WARNING "Gcov instrumentation only works with GCC")
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")
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
121 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in ${PROJECT_BINARY_DIR}/cbor/configuration.h)
122 install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION include/cbor)
123 # Make the header visible at compile time
124 include_directories(${PROJECT_BINARY_DIR})
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.
130 if(${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)
135 endif(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
137 message(STATUS "LTO is enabled")
139 message(STATUS "LTO is not enabled")
144 set_property(DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
150 set_property(DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
157 set_property(DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
159 endif (WITH_EXAMPLES)