]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libcbor/CMakeLists.txt
ipsec: make sure the lock allocated in key_newsav does not false-share
[FreeBSD/FreeBSD.git] / contrib / libcbor / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8)
2 project(libcbor)
3 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
4 include(CTest)
5
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})
10
11 set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
12 include(CheckIncludeFiles)
13
14 include(TestBigEndian)
15 test_big_endian(BIG_ENDIAN)
16 if(BIG_ENDIAN)
17     add_definitions(-DIS_BIG_ENDIAN)
18 endif()
19
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")
24
25 option(WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF)
26 if(WITH_TESTS)
27     add_definitions(-DWITH_TESTS)
28 endif(WITH_TESTS)
29
30 option(WITH_EXAMPLES "Build examples" ON)
31
32 option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF)
33 if(HUGE_FUZZ)
34     add_definitions(-DHUGE_FUZZ)
35 endif(HUGE_FUZZ)
36
37 option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF)
38 if(SANE_MALLOC)
39     add_definitions(-DSANE_MALLOC)
40 endif(SANE_MALLOC)
41
42 option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF)
43 if(PRINT_FUZZ)
44     add_definitions(-DPRINT_FUZZ)
45 endif(PRINT_FUZZ)
46
47 option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON)
48
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})
56
57 include(CPack)
58
59 if(MINGW)
60     # https://github.com/PJK/libcbor/issues/13
61     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
62 elseif(NOT MSVC)
63     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic")
64 endif()
65
66 if(MSVC)
67     # This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
68     set(CBOR_RESTRICT_SPECIFIER "")
69 else()
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()
80 endif()
81
82 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
83
84
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!")
89 else()
90     add_definitions(-DEIGHT_BYTE_SIZE_T)
91 endif()
92
93 enable_testing()
94
95 set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
96 set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
97
98 add_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")
103 include_directories(src)
104
105
106 option(COVERAGE "Enable code coverage instrumentation" OFF)
107 if (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")
115 endif (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
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})
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.
129 set(use_lto FALSE)
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")
136 if(use_lto)
137     message(STATUS "LTO is enabled")
138 else()
139     message(STATUS "LTO is not enabled")
140 endif(use_lto)
141
142 subdirs(src)
143 if(use_lto)
144     set_property(DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
145 endif(use_lto)
146
147 if (WITH_TESTS)
148     subdirs(test)
149     if(use_lto)
150         set_property(DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
151     endif(use_lto)
152 endif (WITH_TESTS)
153
154 if (WITH_EXAMPLES)
155     subdirs(examples)
156     if(use_lto)
157         set_property(DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
158     endif(use_lto)
159 endif (WITH_EXAMPLES)