xref: /freebsd/contrib/libcbor/CMakeLists.txt (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1cmake_minimum_required(VERSION 3.0)
2
3project(libcbor)
4set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")
5include(CTest)
6include(GNUInstallDirs) # Provides CMAKE_INSTALL_ variables
7
8SET(CBOR_VERSION_MAJOR "0")
9SET(CBOR_VERSION_MINOR "11")
10SET(CBOR_VERSION_PATCH "0")
11SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
12
13option(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY "cmake --build --target install does not depend on cmake --build" true)
14option(BUILD_SHARED_LIBS "Build as a shared library" false)
15
16include(CheckIncludeFiles)
17
18include(TestBigEndian)
19test_big_endian(BIG_ENDIAN)
20if(BIG_ENDIAN)
21    add_definitions(-DIS_BIG_ENDIAN)
22endif()
23
24option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
25if(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.")
30endif(CBOR_CUSTOM_ALLOC)
31
32option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
33set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking")
34set(CBOR_MAX_STACK_SIZE "2048" CACHE STRING "maximum size for decoding context stack")
35
36option(WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF)
37if(WITH_TESTS)
38    add_definitions(-DWITH_TESTS)
39endif(WITH_TESTS)
40
41option(WITH_EXAMPLES "Build examples" ON)
42
43option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF)
44if(HUGE_FUZZ)
45    add_definitions(-DHUGE_FUZZ)
46endif(HUGE_FUZZ)
47
48option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF)
49if(SANE_MALLOC)
50    add_definitions(-DSANE_MALLOC)
51endif(SANE_MALLOC)
52
53option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF)
54if(PRINT_FUZZ)
55    add_definitions(-DPRINT_FUZZ)
56endif(PRINT_FUZZ)
57
58option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON)
59
60set(CPACK_GENERATOR "DEB" "TGZ" "RPM")
61set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
62set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda")
63set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
64set(CPACK_PACKAGE_VERSION_MAJOR ${CBOR_VERSION_MAJOR})
65set(CPACK_PACKAGE_VERSION_MINOR ${CBOR_VERSION_MINOR})
66set(CPACK_PACKAGE_VERSION_PATCH ${CBOR_VERSION_PATCH})
67
68include(CPack)
69
70if(MINGW)
71    # https://github.com/PJK/libcbor/issues/13
72    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
73elseif(NOT MSVC)
74    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic")
75endif()
76
77if(MSVC)
78    # This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
79    set(CBOR_RESTRICT_SPECIFIER "")
80else()
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()
91endif()
92
93set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
94
95
96include(CheckTypeSize)
97check_type_size("size_t" SIZEOF_SIZE_T)
98if(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.")
100else()
101    add_definitions(-DEIGHT_BYTE_SIZE_T)
102endif()
103
104enable_testing()
105
106set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
107set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
108
109add_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
115add_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
127include_directories(src)
128
129
130option(c "Enable code coverage instrumentation" OFF)
131if (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()
143endif (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.
147configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in ${PROJECT_BINARY_DIR}/cbor/configuration.h)
148install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cbor)
149# Make the header visible at compile time
150include_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.
155set(use_lto FALSE)
156if(${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)
161endif(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
162if(use_lto)
163    message(STATUS "LTO is enabled")
164else()
165    message(STATUS "LTO is not enabled")
166endif(use_lto)
167
168add_subdirectory(src)
169if(use_lto)
170    set_property(DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
171endif(use_lto)
172
173if (WITH_TESTS)
174    add_subdirectory(test)
175    if(use_lto)
176        set_property(DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
177    endif(use_lto)
178endif (WITH_TESTS)
179
180if (WITH_EXAMPLES)
181    add_subdirectory(examples)
182    if(use_lto)
183        set_property(DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
184    endif(use_lto)
185endif (WITH_EXAMPLES)
186