Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/libcbor/CMakeLists.txt
39475 views
1
cmake_minimum_required(VERSION 3.0)
2
3
project(libcbor)
4
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")
5
include(CTest)
6
include(GNUInstallDirs) # Provides CMAKE_INSTALL_ variables
7
8
SET(CBOR_VERSION_MAJOR "0")
9
SET(CBOR_VERSION_MINOR "11")
10
SET(CBOR_VERSION_PATCH "0")
11
SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
12
13
option(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY "cmake --build --target install does not depend on cmake --build" true)
14
option(BUILD_SHARED_LIBS "Build as a shared library" false)
15
16
include(CheckIncludeFiles)
17
18
include(TestBigEndian)
19
test_big_endian(BIG_ENDIAN)
20
if(BIG_ENDIAN)
21
add_definitions(-DIS_BIG_ENDIAN)
22
endif()
23
24
option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
25
if(CBOR_CUSTOM_ALLOC)
26
message(WARNING
27
"CBOR_CUSTOM_ALLOC has been deprecated. Custom allocators are now enabled by default."
28
"The flag is a no-op and will be removed in the next version. "
29
"Please remove CBOR_CUSTOM_ALLOC from your build configuration.")
30
endif(CBOR_CUSTOM_ALLOC)
31
32
option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
33
set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking")
34
set(CBOR_MAX_STACK_SIZE "2048" CACHE STRING "maximum size for decoding context stack")
35
36
option(WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF)
37
if(WITH_TESTS)
38
add_definitions(-DWITH_TESTS)
39
endif(WITH_TESTS)
40
41
option(WITH_EXAMPLES "Build examples" ON)
42
43
option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF)
44
if(HUGE_FUZZ)
45
add_definitions(-DHUGE_FUZZ)
46
endif(HUGE_FUZZ)
47
48
option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF)
49
if(SANE_MALLOC)
50
add_definitions(-DSANE_MALLOC)
51
endif(SANE_MALLOC)
52
53
option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF)
54
if(PRINT_FUZZ)
55
add_definitions(-DPRINT_FUZZ)
56
endif(PRINT_FUZZ)
57
58
option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON)
59
60
set(CPACK_GENERATOR "DEB" "TGZ" "RPM")
61
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
62
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda")
63
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
64
set(CPACK_PACKAGE_VERSION_MAJOR ${CBOR_VERSION_MAJOR})
65
set(CPACK_PACKAGE_VERSION_MINOR ${CBOR_VERSION_MINOR})
66
set(CPACK_PACKAGE_VERSION_PATCH ${CBOR_VERSION_PATCH})
67
68
include(CPack)
69
70
if(MINGW)
71
# https://github.com/PJK/libcbor/issues/13
72
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
73
elseif(NOT MSVC)
74
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic")
75
endif()
76
77
if(MSVC)
78
# This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
79
set(CBOR_RESTRICT_SPECIFIER "")
80
else()
81
set(CBOR_RESTRICT_SPECIFIER "restrict")
82
83
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -g -ggdb -DDEBUG=true")
84
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -DNDEBUG")
85
86
if(SANITIZE)
87
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} \
88
-fsanitize=undefined -fsanitize=address \
89
-fsanitize=bounds -fsanitize=alignment")
90
endif()
91
endif()
92
93
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
94
95
96
include(CheckTypeSize)
97
check_type_size("size_t" SIZEOF_SIZE_T)
98
if(SIZEOF_SIZE_T LESS 8)
99
message(WARNING "Your size_t is less than 8 bytes. Decoding of huge items that would exceed the memory address space will always fail. Consider implementing a custom streaming decoder if you need to deal with huge items.")
100
else()
101
add_definitions(-DEIGHT_BYTE_SIZE_T)
102
endif()
103
104
enable_testing()
105
106
set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
107
set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
108
109
add_custom_target(coverage
110
COMMAND ctest
111
COMMAND lcov --capture --directory . --output-file coverage.info
112
COMMAND genhtml coverage.info --highlight --legend --output-directory coverage_html
113
COMMAND echo "Coverage report ready: ${CMAKE_CURRENT_BINARY_DIR}/coverage_html/index.html")
114
115
add_custom_target(llvm-coverage
116
COMMAND make -j 16
117
COMMAND rm -rf coverage_profiles
118
COMMAND mkdir coverage_profiles
119
COMMAND bash -c [[ for TEST in $(ls test/*_test); do LLVM_PROFILE_FILE="coverage_profiles/$(basename -- ${TEST}).profraw" ./${TEST}; done ]]
120
# VERBATIM makes escaping working, but breaks shell expansions, so we need to explicitly use bash
121
COMMAND bash -c [[ llvm-profdata merge -sparse $(ls coverage_profiles/*.profraw) -o coverage_profiles/combined.profdata ]]
122
COMMAND bash -c [[ llvm-cov show -instr-profile=coverage_profiles/combined.profdata test/*_test -format=html > coverage_profiles/report.html ]]
123
COMMAND bash -c [[ llvm-cov report -instr-profile=coverage_profiles/combined.profdata test/*_test ]]
124
COMMAND echo "Coverage report ready: ${CMAKE_CURRENT_BINARY_DIR}/coverage_profiles/report.html"
125
VERBATIM)
126
127
include_directories(src)
128
129
130
option(c "Enable code coverage instrumentation" OFF)
131
if (COVERAGE)
132
message("Configuring code coverage instrumentation")
133
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
134
# https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
135
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage --coverage")
136
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage --coverage")
137
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
138
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
139
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fprofile-instr-generate")
140
else()
141
message(WARNING "Code coverage build not implemented for compiler ${CMAKE_C_COMPILER_ID}")
142
endif()
143
endif (COVERAGE)
144
145
# We want to generate configuration.h from the template and make it so that it is accessible using the same
146
# path during both library build and installed header use, without littering the source dir.
147
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in ${PROJECT_BINARY_DIR}/cbor/configuration.h)
148
install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cbor)
149
# Make the header visible at compile time
150
include_directories(${PROJECT_BINARY_DIR})
151
152
# CMake >= 3.9.0 enables LTO for GCC and Clang with INTERPROCEDURAL_OPTIMIZATION
153
# Policy CMP0069 enables this behavior when we set the minimum CMake version < 3.9.0
154
# Checking for LTO support before setting INTERPROCEDURAL_OPTIMIZATION is mandatory with CMP0069 set to NEW.
155
set(use_lto FALSE)
156
if(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
157
cmake_policy(SET CMP0069 NEW)
158
# Require LTO support to build libcbor with newer CMake versions
159
include(CheckIPOSupported)
160
check_ipo_supported(RESULT use_lto)
161
endif(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
162
if(use_lto)
163
message(STATUS "LTO is enabled")
164
else()
165
message(STATUS "LTO is not enabled")
166
endif(use_lto)
167
168
add_subdirectory(src)
169
if(use_lto)
170
set_property(DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
171
endif(use_lto)
172
173
if (WITH_TESTS)
174
add_subdirectory(test)
175
if(use_lto)
176
set_property(DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
177
endif(use_lto)
178
endif (WITH_TESTS)
179
180
if (WITH_EXAMPLES)
181
add_subdirectory(examples)
182
if(use_lto)
183
set_property(DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
184
endif(use_lto)
185
endif (WITH_EXAMPLES)
186
187