2023-04-20 23:17:42 +00:00
cmake_minimum_required ( VERSION 3.0 )
2021-10-01 23:46:00 +00:00
project ( libcbor )
2023-04-20 23:17:42 +00:00
set ( CMAKE_MODULE_PATH ${ CMAKE_MODULE_PATH } "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/" )
2021-10-01 23:46:00 +00:00
include ( CTest )
SET ( CBOR_VERSION_MAJOR "0" )
2023-04-20 23:17:42 +00:00
SET ( CBOR_VERSION_MINOR "10" )
SET ( CBOR_VERSION_PATCH "2" )
2021-10-01 23:46:00 +00:00
SET ( CBOR_VERSION ${ CBOR_VERSION_MAJOR } . ${ CBOR_VERSION_MINOR } . ${ CBOR_VERSION_PATCH } )
set ( CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true )
include ( CheckIncludeFiles )
include ( TestBigEndian )
test_big_endian ( BIG_ENDIAN )
if ( BIG_ENDIAN )
add_definitions ( -DIS_BIG_ENDIAN )
endif ( )
option ( CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF )
2023-04-20 23:17:42 +00:00
if ( CBOR_CUSTOM_ALLOC )
message ( WARNING
" C B O R _ C U S T O M _ A L L O C h a s b e e n d e p r e c a t e d . C u s t o m a l l o c a t o r s a r e n o w e n a b l e d b y d e f a u l t . "
" T h e f l a g i s a n o - o p a n d w i l l b e r e m o v e d i n t h e n e x t v e r s i o n . "
" P l e a s e r e m o v e C B O R _ C U S T O M _ A L L O C f r o m y o u r b u i l d c o n f i g u a t i o n . " )
endif ( CBOR_CUSTOM_ALLOC )
2021-10-01 23:46:00 +00:00
option ( CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON )
set ( CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking" )
set ( CBOR_MAX_STACK_SIZE "2048" CACHE STRING "maximum size for decoding context stack" )
option ( WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF )
if ( WITH_TESTS )
add_definitions ( -DWITH_TESTS )
endif ( WITH_TESTS )
option ( WITH_EXAMPLES "Build examples" ON )
option ( HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF )
if ( HUGE_FUZZ )
add_definitions ( -DHUGE_FUZZ )
endif ( HUGE_FUZZ )
option ( SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF )
if ( SANE_MALLOC )
add_definitions ( -DSANE_MALLOC )
endif ( SANE_MALLOC )
option ( PRINT_FUZZ "[TEST] Print the fuzzer input" OFF )
if ( PRINT_FUZZ )
add_definitions ( -DPRINT_FUZZ )
endif ( PRINT_FUZZ )
option ( SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON )
set ( CPACK_GENERATOR "DEB" "TGZ" "RPM" )
set ( CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64" )
set ( CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda" )
set ( CPACK_DEBIAN_PACKAGE_DEPENDS "libc6" )
set ( CPACK_PACKAGE_VERSION_MAJOR ${ CBOR_VERSION_MAJOR } )
set ( CPACK_PACKAGE_VERSION_MINOR ${ CBOR_VERSION_MINOR } )
set ( CPACK_PACKAGE_VERSION_PATCH ${ CBOR_VERSION_PATCH } )
include ( CPack )
if ( MINGW )
# https://github.com/PJK/libcbor/issues/13
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99" )
elseif ( NOT MSVC )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic" )
endif ( )
if ( MSVC )
# This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
set ( CBOR_RESTRICT_SPECIFIER "" )
else ( )
set ( CBOR_RESTRICT_SPECIFIER "restrict" )
set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -g -ggdb -DDEBUG=true" )
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -DNDEBUG" )
if ( SANITIZE )
set ( CMAKE_C_FLAGS_DEBUG " ${ CMAKE_C_FLAGS_DEBUG } \
- f s a n i t i z e = u n d e f i n e d - f s a n i t i z e = a d d r e s s \
- f s a n i t i z e = b o u n d s - f s a n i t i z e = a l i g n m e n t " )
endif ( )
endif ( )
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG "-g" )
include ( CheckTypeSize )
check_type_size ( "size_t" SIZEOF_SIZE_T )
if ( SIZEOF_SIZE_T LESS 8 )
2023-04-20 23:17:42 +00:00
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." )
2021-10-01 23:46:00 +00:00
else ( )
add_definitions ( -DEIGHT_BYTE_SIZE_T )
endif ( )
enable_testing ( )
set ( CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind" )
set ( MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1" )
add_custom_target ( coverage
C O M M A N D c t e s t
C O M M A N D l c o v - - c a p t u r e - - d i r e c t o r y . - - o u t p u t - f i l e c o v e r a g e . i n f o
C O M M A N D g e n h t m l c o v e r a g e . i n f o - - h i g h l i g h t - - l e g e n d - - o u t p u t - d i r e c t o r y c o v e r a g e _ h t m l
2023-04-20 23:17:42 +00:00
C O M M A N D e c h o " C o v e r a g e r e p o r t r e a d y : $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / c o v e r a g e _ h t m l / i n d e x . h t m l " )
add_custom_target ( llvm-coverage
C O M M A N D m a k e - j 1 6
C O M M A N D r m - r f c o v e r a g e _ p r o f i l e s
C O M M A N D m k d i r c o v e r a g e _ p r o f i l e s
C O M M A N D b a s h - c [ [ f o r T E S T i n $ ( l s t e s t / * _ t e s t ) ; d o L L V M _ P R O F I L E _ F I L E = " c o v e r a g e _ p r o f i l e s / $ ( b a s e n a m e - - $ { T E S T } ) . p r o f r a w " . / $ { T E S T } ; d o n e ] ]
# VERBATIM makes escaping working, but breaks shell expansions, so we need to explicitly use bash
C O M M A N D b a s h - c [ [ l l v m - p r o f d a t a m e r g e - s p a r s e $ ( l s c o v e r a g e _ p r o f i l e s / * . p r o f r a w ) - o c o v e r a g e _ p r o f i l e s / c o m b i n e d . p r o f d a t a ] ]
C O M M A N D b a s h - c [ [ l l v m - c o v s h o w - i n s t r - p r o f i l e = c o v e r a g e _ p r o f i l e s / c o m b i n e d . p r o f d a t a t e s t / * _ t e s t - f o r m a t = h t m l > c o v e r a g e _ p r o f i l e s / r e p o r t . h t m l ] ]
C O M M A N D b a s h - c [ [ l l v m - c o v r e p o r t - i n s t r - p r o f i l e = c o v e r a g e _ p r o f i l e s / c o m b i n e d . p r o f d a t a t e s t / * _ t e s t ] ]
C O M M A N D e c h o " C o v e r a g e r e p o r t r e a d y : $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / c o v e r a g e _ p r o f i l e s / r e p o r t . h t m l "
V E R B A T I M )
2021-10-01 23:46:00 +00:00
include_directories ( src )
2023-04-20 23:17:42 +00:00
option ( c "Enable code coverage instrumentation" OFF )
2021-10-01 23:46:00 +00:00
if ( COVERAGE )
message ( "Configuring code coverage instrumentation" )
2023-04-20 23:17:42 +00:00
if ( CMAKE_C_COMPILER_ID MATCHES "GNU" )
# https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage --coverage" )
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage --coverage" )
elseif ( CMAKE_C_COMPILER_ID MATCHES "Clang" )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping" )
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fprofile-instr-generate" )
else ( )
message ( WARNING "Code coverage build not implemented for compiler ${CMAKE_C_COMPILER_ID}" )
2021-10-01 23:46:00 +00:00
endif ( )
endif ( COVERAGE )
# We want to generate configuration.h from the template and make it so that it is accessible using the same
# path during both library build and installed header use, without littering the source dir.
# Using cbor/configuration.h in the build dir works b/c headers will be installed to <prefix>/cbor
configure_file ( ${ CMAKE_CURRENT_SOURCE_DIR } /src/cbor/configuration.h.in ${ PROJECT_BINARY_DIR } /cbor/configuration.h )
install ( FILES ${ PROJECT_BINARY_DIR } /cbor/configuration.h DESTINATION include/cbor )
# Make the header visible at compile time
include_directories ( ${ PROJECT_BINARY_DIR } )
# CMake >= 3.9.0 enables LTO for GCC and Clang with INTERPROCEDURAL_OPTIMIZATION
# Policy CMP0069 enables this behavior when we set the minimum CMake version < 3.9.0
# Checking for LTO support before setting INTERPROCEDURAL_OPTIMIZATION is mandatory with CMP0069 set to NEW.
set ( use_lto FALSE )
if ( ${ CMAKE_VERSION } VERSION_GREATER "3.9.0" OR ${ CMAKE_VERSION } VERSION_EQUAL "3.9.0" )
cmake_policy ( SET CMP0069 NEW )
# Require LTO support to build libcbor with newer CMake versions
include ( CheckIPOSupported )
check_ipo_supported ( RESULT use_lto )
endif ( ${ CMAKE_VERSION } VERSION_GREATER "3.9.0" OR ${ CMAKE_VERSION } VERSION_EQUAL "3.9.0" )
if ( use_lto )
message ( STATUS "LTO is enabled" )
else ( )
message ( STATUS "LTO is not enabled" )
endif ( use_lto )
2023-04-20 23:17:42 +00:00
add_subdirectory ( src )
2021-10-01 23:46:00 +00:00
if ( use_lto )
set_property ( DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
endif ( use_lto )
if ( WITH_TESTS )
2023-04-20 23:17:42 +00:00
add_subdirectory ( test )
2021-10-01 23:46:00 +00:00
if ( use_lto )
set_property ( DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
endif ( use_lto )
endif ( WITH_TESTS )
if ( WITH_EXAMPLES )
2023-04-20 23:17:42 +00:00
add_subdirectory ( examples )
2021-10-01 23:46:00 +00:00
if ( use_lto )
set_property ( DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
endif ( use_lto )
endif ( WITH_EXAMPLES )