1cmake_minimum_required(VERSION 2.8)
2project(libcbor)
3include(CTest)
4
5SET(CBOR_VERSION_MAJOR "0")
6SET(CBOR_VERSION_MINOR "5")
7SET(CBOR_VERSION_PATCH "0")
8SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
9
10set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
11include(CheckIncludeFiles)
12
13include(TestBigEndian)
14test_big_endian(BIG_ENDIAN)
15if(BIG_ENDIAN)
16    add_definitions(-DIS_BIG_ENDIAN)
17endif()
18
19option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
20option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
21set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking")
22
23option(WITH_TESTS "[TEST] Build unit tests (requires CMocka" OFF)
24if(WITH_TESTS)
25    add_definitions(-DWITH_TESTS)
26endif(WITH_TESTS)
27
28option(WITH_EXAMPLES "Build examples" ON)
29
30option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF)
31if(HUGE_FUZZ)
32    add_definitions(-DHUGE_FUZZ)
33endif(HUGE_FUZZ)
34
35option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF)
36if(SANE_MALLOC)
37    add_definitions(-DSANE_MALLOC)
38endif(SANE_MALLOC)
39
40option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF)
41if(PRINT_FUZZ)
42    add_definitions(-DPRINT_FUZZ)
43endif(PRINT_FUZZ)
44
45option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON)
46
47set(CPACK_GENERATOR "DEB" "TGZ" "RPM")
48set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
49set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda")
50set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
51set(CPACK_PACKAGE_VERSION_MAJOR ${CBOR_VERSION_MAJOR})
52set(CPACK_PACKAGE_VERSION_MINOR ${CBOR_VERSION_MINOR})
53set(CPACK_PACKAGE_VERSION_PATCH ${CBOR_VERSION_PATCH})
54
55include(CPack)
56
57if(MINGW)
58    # https://github.com/PJK/libcbor/issues/13
59    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
60elseif(NOT MSVC)
61    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
62endif()
63
64if(MSVC)
65    # This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
66    set(CBOR_RESTRICT_SPECIFIER "")
67else()
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()
78endif()
79
80set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
81set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")
82
83
84include(CheckTypeSize)
85check_type_size("size_t" SIZEOF_SIZE_T)
86if(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!")
88else()
89    add_definitions(-DEIGHT_BYTE_SIZE_T)
90endif()
91
92enable_testing()
93
94set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
95set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
96
97add_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")
102include_directories(src)
103
104
105option(COVERAGE "Enable code coverage instrumentation" OFF)
106if (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")
114endif (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
120configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in ${PROJECT_BINARY_DIR}/cbor/configuration.h)
121install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION include/cbor)
122# Make the header visible at compile time
123include_directories(${PROJECT_BINARY_DIR})
124
125subdirs(src)
126
127if (WITH_TESTS)
128    subdirs(test)
129endif (WITH_TESTS)
130
131if (WITH_EXAMPLES)
132     subdirs(examples)
133endif (WITH_EXAMPLES)
134