Home | History | Annotate | Line # | Download | only in dist
      1  1.1  christos cmake_minimum_required(VERSION 2.8)
      2  1.1  christos project(libcbor)
      3  1.1  christos include(CTest)
      4  1.1  christos 
      5  1.1  christos SET(CBOR_VERSION_MAJOR "0")
      6  1.1  christos SET(CBOR_VERSION_MINOR "5")
      7  1.1  christos SET(CBOR_VERSION_PATCH "0")
      8  1.1  christos SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
      9  1.1  christos 
     10  1.1  christos set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
     11  1.1  christos include(CheckIncludeFiles)
     12  1.1  christos 
     13  1.1  christos include(TestBigEndian)
     14  1.1  christos test_big_endian(BIG_ENDIAN)
     15  1.1  christos if(BIG_ENDIAN)
     16  1.1  christos     add_definitions(-DIS_BIG_ENDIAN)
     17  1.1  christos endif()
     18  1.1  christos 
     19  1.1  christos option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
     20  1.1  christos option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
     21  1.1  christos set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking")
     22  1.1  christos 
     23  1.1  christos option(WITH_TESTS "[TEST] Build unit tests (requires CMocka" OFF)
     24  1.1  christos if(WITH_TESTS)
     25  1.1  christos     add_definitions(-DWITH_TESTS)
     26  1.1  christos endif(WITH_TESTS)
     27  1.1  christos 
     28  1.1  christos option(WITH_EXAMPLES "Build examples" ON)
     29  1.1  christos 
     30  1.1  christos option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF)
     31  1.1  christos if(HUGE_FUZZ)
     32  1.1  christos     add_definitions(-DHUGE_FUZZ)
     33  1.1  christos endif(HUGE_FUZZ)
     34  1.1  christos 
     35  1.1  christos option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF)
     36  1.1  christos if(SANE_MALLOC)
     37  1.1  christos     add_definitions(-DSANE_MALLOC)
     38  1.1  christos endif(SANE_MALLOC)
     39  1.1  christos 
     40  1.1  christos option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF)
     41  1.1  christos if(PRINT_FUZZ)
     42  1.1  christos     add_definitions(-DPRINT_FUZZ)
     43  1.1  christos endif(PRINT_FUZZ)
     44  1.1  christos 
     45  1.1  christos option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON)
     46  1.1  christos 
     47  1.1  christos set(CPACK_GENERATOR "DEB" "TGZ" "RPM")
     48  1.1  christos set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
     49  1.1  christos set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda")
     50  1.1  christos set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
     51  1.1  christos set(CPACK_PACKAGE_VERSION_MAJOR ${CBOR_VERSION_MAJOR})
     52  1.1  christos set(CPACK_PACKAGE_VERSION_MINOR ${CBOR_VERSION_MINOR})
     53  1.1  christos set(CPACK_PACKAGE_VERSION_PATCH ${CBOR_VERSION_PATCH})
     54  1.1  christos 
     55  1.1  christos include(CPack)
     56  1.1  christos 
     57  1.1  christos if(MINGW)
     58  1.1  christos     # https://github.com/PJK/libcbor/issues/13
     59  1.1  christos     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
     60  1.1  christos elseif(NOT MSVC)
     61  1.1  christos     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
     62  1.1  christos endif()
     63  1.1  christos 
     64  1.1  christos if(MSVC)
     65  1.1  christos     # This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
     66  1.1  christos     set(CBOR_RESTRICT_SPECIFIER "")
     67  1.1  christos else()
     68  1.1  christos     set(CBOR_RESTRICT_SPECIFIER "restrict")
     69  1.1  christos 
     70  1.1  christos     set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -pedantic -g -ggdb -DDEBUG=true")
     71  1.1  christos     set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -flto -Wall -pedantic -DNDEBUG")
     72  1.1  christos 
     73  1.1  christos     if(SANITIZE)
     74  1.1  christos         set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} \
     75  1.1  christos             -fsanitize-trap=undefined -fsanitize=address \
     76  1.1  christos             -fsanitize-trap=bounds -fsanitize=alignment")
     77  1.1  christos     endif()
     78  1.1  christos endif()
     79  1.1  christos 
     80  1.1  christos set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
     81  1.1  christos set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")
     82  1.1  christos 
     83  1.1  christos 
     84  1.1  christos include(CheckTypeSize)
     85  1.1  christos check_type_size("size_t" SIZEOF_SIZE_T)
     86  1.1  christos if(SIZEOF_SIZE_T LESS 8)
     87  1.1  christos     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!")
     88  1.1  christos else()
     89  1.1  christos     add_definitions(-DEIGHT_BYTE_SIZE_T)
     90  1.1  christos endif()
     91  1.1  christos 
     92  1.1  christos enable_testing()
     93  1.1  christos 
     94  1.1  christos set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
     95  1.1  christos set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
     96  1.1  christos 
     97  1.1  christos add_custom_target(coverage
     98  1.1  christos                   COMMAND ctest
     99  1.1  christos                   COMMAND lcov --capture --directory . --output-file coverage.info
    100  1.1  christos                   COMMAND genhtml coverage.info --highlight --legend --output-directory coverage_html
    101  1.1  christos                   COMMAND echo "Coverage report ready: file://${CMAKE_CURRENT_BINARY_DIR}/coverage_html/index.html")
    102  1.1  christos include_directories(src)
    103  1.1  christos 
    104  1.1  christos 
    105  1.1  christos option(COVERAGE "Enable code coverage instrumentation" OFF)
    106  1.1  christos if (COVERAGE)
    107  1.1  christos     message("Configuring code coverage instrumentation")
    108  1.1  christos     if(NOT CMAKE_C_COMPILER MATCHES "gcc")
    109  1.1  christos         message(WARNING "Gcov instrumentation only works with GCC")
    110  1.1  christos     endif()
    111  1.1  christos     # https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
    112  1.1  christos     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage --coverage")
    113  1.1  christos     set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage --coverage")
    114  1.1  christos endif (COVERAGE)
    115  1.1  christos 
    116  1.1  christos 
    117  1.1  christos # We want to generate configuration.h from the template and make it so that it is accessible using the same
    118  1.1  christos # path during both library build and installed header use, without littering the source dir.
    119  1.1  christos # Using cbor/configuration.h in the build dir works b/c headers will be installed to <prefix>/cbor
    120  1.1  christos configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in ${PROJECT_BINARY_DIR}/cbor/configuration.h)
    121  1.1  christos install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION include/cbor)
    122  1.1  christos # Make the header visible at compile time
    123  1.1  christos include_directories(${PROJECT_BINARY_DIR})
    124  1.1  christos 
    125  1.1  christos subdirs(src)
    126  1.1  christos 
    127  1.1  christos if (WITH_TESTS)
    128  1.1  christos     subdirs(test)
    129  1.1  christos endif (WITH_TESTS)
    130  1.1  christos 
    131  1.1  christos if (WITH_EXAMPLES)
    132  1.1  christos      subdirs(examples)
    133  1.1  christos endif (WITH_EXAMPLES)
    134