diff --git a/.travis-qemu.sh b/.travis-qemu.sh deleted file mode 100755 index 3081570e..00000000 --- a/.travis-qemu.sh +++ /dev/null @@ -1,137 +0,0 @@ -#!/bin/bash -# Based on a test script from avsm/ocaml repo https://github.com/avsm/ocaml -# Adapted from https://www.tomaz.me/2013/12/02/running-travis-ci-tests-on-arm.html - -set -e - -CHROOT_DIR=/tmp/arm-chroot -MIRROR=http://archive.raspbian.org/raspbian -VERSION=wheezy -CHROOT_ARCH=armhf - -# Debian package dependencies for the host -HOST_DEPENDENCIES="debootstrap qemu-user-static binfmt-support sbuild" - -# Debian package dependencies for the chrooted environment -GUEST_DEPENDENCIES="cmake git clang-format" - -function setup_arm_chroot { - # Host dependencies - sudo apt-get install -qq -y ${HOST_DEPENDENCIES} - - # Create chrooted environment - sudo mkdir ${CHROOT_DIR} - sudo debootstrap --foreign --no-check-gpg --include=fakeroot,build-essential \ - --arch=${CHROOT_ARCH} ${VERSION} ${CHROOT_DIR} ${MIRROR} - sudo cp /usr/bin/qemu-arm-static ${CHROOT_DIR}/usr/bin/ - sudo chroot ${CHROOT_DIR} ./debootstrap/debootstrap --second-stage - sudo sbuild-createchroot --arch=${CHROOT_ARCH} --foreign --setup-only \ - ${VERSION} ${CHROOT_DIR} ${MIRROR} - - # Create file with environment variables which will be used inside chrooted - # environment - echo "export ARCH=${ARCH}" > envvars.sh - echo "export TRAVIS_BUILD_DIR=${TRAVIS_BUILD_DIR}" >> envvars.sh - chmod a+x envvars.sh - - # Install dependencies inside chroot - sudo chroot ${CHROOT_DIR} apt-get update - sudo chroot ${CHROOT_DIR} apt-get --allow-unauthenticated install \ - -qq -y ${GUEST_DEPENDENCIES} - - # Create build dir and copy travis build files to our chroot environ ment - sudo mkdir -p ${CHROOT_DIR}/${TRAVIS_BUILD_DIR} - sudo rsync -a ${TRAVIS_BUILD_DIR}/ ${CHROOT_DIR}/${TRAVIS_BUILD_DIR}/ - - # Indicate chroot environment has been set up - sudo touch ${CHROOT_DIR}/.chroot_is_done - - # Call ourselves again which will cause tests to run - sudo chroot ${CHROOT_DIR} bash -c "cd ${TRAVIS_BUILD_DIR} && ./.travis-qemu.sh" -} - - -if [ "${ARCH}" = "arm" ]; then - if [ -e "/.chroot_is_done" ]; then - # We are inside ARM chroot - echo "Running inside chrooted environment, will execute only tests" - - . ./envvars.sh - - # We need CMocka since the executables are dynamically linked - git clone git://git.cryptomilk.org/projects/cmocka.git - mkdir cmocka_build && cd cmocka_build - cmake ../cmocka - make VERBOSE=1 - make install - cd .. - - # Hack: We don't have the right CMake (takes too long to compile), but this works - ctest -VV - else - # Compilation on QEMU is too slow and times out on Travis. Crosscompile at the host - echo "Initial execution on ARM environment, will crosscompile" - arm-linux-gnueabihf-gcc-4.6 -v - - # Crosscompile CMocka - pushd ${HOME} - git clone git://git.cryptomilk.org/projects/cmocka.git - mkdir cmocka_build && cd cmocka_build - cmake ../cmocka \ - -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc-4.6 - make VERBOSE=1 - sudo make install - cd .. - rm -rf cmocka cmocka_build - popd - - # Crosscompile libcbor - cmake ${SOURCE} \ - -DCBOR_CUSTOM_ALLOC=ON \ - -DCMAKE_BUILD_TYPE=Debug \ - -DWITH_TESTS=ON \ - -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc-4.6 - make VERBOSE=1 - - # ARM test run, need to set up chrooted environment first - echo "Setting up chrooted ARM environment" - setup_arm_chroot - fi -else - # Proceed as normal - gem install coveralls-lcov - pushd ${HOME} - git clone git://git.cryptomilk.org/projects/cmocka.git - mkdir cmocka_build && cd cmocka_build - cmake ../cmocka - make -j 2 - sudo make install - cd .. - rm -rf cmocka cmocka_build - popd - - echo "Running tests" - cppcheck . --error-exitcode=1 --suppressions cppcheck_suppressions.txt --force - - clang-format -version - clang-format-8 -version - ./clang-format.sh - git diff-index --quiet HEAD - - cmake \ - -DCBOR_CUSTOM_ALLOC=ON \ - -DCMAKE_BUILD_TYPE=Debug \ - -DSANITIZE=OFF \ - -DWITH_TESTS=ON \ - -DCMAKE_PREFIX_PATH=${HOME}/usr/local \ - . - make VERBOSE=1 - - ctest -VV - - ctest -T memcheck | tee memcheck.out - if grep -q 'Memory Leak\|IPW\|Uninitialized Memory Conditional\|Uninitialized Memory Read' memcheck.out; then - exit 1 - fi -fi - diff --git a/.travis.yml b/.travis.yml index 616d8480..99a12ca9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,57 +4,54 @@ sudo: true matrix: include: - - compiler: clang - addons: - apt: - sources: - - ubuntu-toolchain-r-test - - llvm-toolchain-precise-3.6 - - george-edison55-precise-backports # cmake 3.2.3 / doxygen 1.8.3 - - sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-8 main' - key_url: https://apt.llvm.org/llvm-snapshot.gpg.key - packages: - - clang-3.6 - - libjansson-dev - - valgrind - - cppcheck - - lcov - - cmake - - cmake-data - - clang-format-8 - - compiler: gcc-6 - addons: - apt: - sources: - - ubuntu-toolchain-r-test - - george-edison55-precise-backports # cmake 3.2.3 / doxygen 1.8.3 - - sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-8 main' - key_url: https://apt.llvm.org/llvm-snapshot.gpg.key - packages: - - gcc-6 - - g++-6 - - libjansson-dev - - valgrind - - cppcheck - - lcov - - cmake - - cmake-data - - clang-format-8 -# Temporarily disabled due to slow performance and problems with Raspbian -# chroot (https://travis-ci.org/PJK/libcbor/jobs/281748138) -# - env: ARCH=arm -# addons: -# apt: -# sources: -# - george-edison55-precise-backports # cmake 3.2.3 / doxygen 1.8.3 -# packages: -# - gcc-arm-linux-gnueabihf -# - libc6-dev-armhf-cross -# - cmake -# - cmake-data + - arch: amd64 + os: linux + dist: bionic + sudo: required + compiler: clang + env: TRAVIS_ARCH="amd64" + - arch: amd64 + os: linux + dist: bionic + sudo: required + compiler: gcc + env: TRAVIS_ARCH="amd64" + - arch: arm64 + os: linux + dist: bionic + sudo: required + compiler: gcc + env: TRAVIS_ARCH="arm64" + # TODO: Add OSX support (might require some brew tweaking) + +before_install: + - pushd ${HOME} + - git clone git://git.cryptomilk.org/projects/cmocka.git + - cd cmocka && mkdir build && cd build + - cmake .. && make -j2 && sudo make install + - cd .. && popd + - if [ "$TRAVIS_OS_NAME" = "linux" ]; then sudo apt-get update -qq; sudo apt-get install -y clang-format-8 cppcheck; fi script: - - "bash -ex .travis-qemu.sh" + - cppcheck . --error-exitcode=1 --force + # Fail if re-formatting creates diffs (implying bad formatting) + - ./clang-format.sh --verbose + - git diff-index --quiet HEAD + - mkdir build && cd build + - cmake -DWITH_TESTS=ON + -DCBOR_CUSTOM_ALLOC=ON + -DCMAKE_BUILD_TYPE=Debug + -DSANITIZE=OFF + .. + - make VERBOSE=1 + - ctest -VV + - ctest -T memcheck | tee memcheck.out + - > + if grep -q 'Memory Leak\|IPW\|Uninitialized Memory Conditional\|Uninitialized Memory Read' memcheck.out; then + exit 1 + fi; + +# TODO: Set up https://codecov.io/ notifications: email: false diff --git a/CMakeLists.txt b/CMakeLists.txt index 2096cc6e..0a81a15a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 2.8) project(libcbor) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/") include(CTest) SET(CBOR_VERSION_MAJOR "0") @@ -72,8 +73,8 @@ else() if(SANITIZE) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} \ - -fsanitize-trap=undefined -fsanitize=address \ - -fsanitize-trap=bounds -fsanitize=alignment") + -fsanitize=undefined -fsanitize=address \ + -fsanitize=bounds -fsanitize=alignment") endif() endif() diff --git a/CMakeModules/FindCMocka.cmake b/CMakeModules/FindCMocka.cmake new file mode 100644 index 00000000..770b44af --- /dev/null +++ b/CMakeModules/FindCMocka.cmake @@ -0,0 +1,49 @@ +# - Try to find CMocka +# Once done this will define +# +# CMOCKA_ROOT_DIR - Set this variable to the root installation of CMocka +# +# Read-Only variables: +# CMOCKA_FOUND - system has CMocka +# CMOCKA_INCLUDE_DIR - the CMocka include directory +# CMOCKA_LIBRARIES - Link these to use CMocka +# CMOCKA_DEFINITIONS - Compiler switches required for using CMocka +# +#============================================================================= +# Copyright (c) 2011-2012 Andreas Schneider +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# + +find_path(CMOCKA_INCLUDE_DIR + NAMES + cmocka.h + PATHS + ${CMOCKA_ROOT_DIR}/include +) + +find_library(CMOCKA_LIBRARY + NAMES + cmocka cmocka_shared + PATHS + ${CMOCKA_ROOT_DIR}/include +) + +if (CMOCKA_LIBRARY) + set(CMOCKA_LIBRARIES + ${CMOCKA_LIBRARIES} + ${CMOCKA_LIBRARY} + ) +endif (CMOCKA_LIBRARY) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(CMocka DEFAULT_MSG CMOCKA_LIBRARIES CMOCKA_INCLUDE_DIR) + +# show the CMOCKA_INCLUDE_DIR and CMOCKA_LIBRARIES variables only in the advanced view +mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARIES) diff --git a/clang-format.sh b/clang-format.sh index ac0c0fbb..e26193ae 100755 --- a/clang-format.sh +++ b/clang-format.sh @@ -1,10 +1,17 @@ #!/usr/bin/env bash +# Usage: ./clang-format.sh + +DIRS="src test examples demo" +SOURCES=$(find ${DIRS} -name "*.c") +SOURCES+=" $(find ${DIRS} -name "*.h")" +SOURCES+=" $(find ${DIRS} -name "*.cpp")" + # TravisCI workaround to use new clang-format while avoiding painful aliasing # into the subshell if which clang-format-8; then - clang-format-8 --verbose -style=file -i **/*.c **/*.h **/*.cpp + clang-format-8 $@ -style=file -i ${SOURCES} else - clang-format --verbose -style=file -i **/*.c **/*.h **/*.cpp + clang-format $@ -style=file -i ${SOURCES} fi diff --git a/cppcheck_suppressions.txt b/cppcheck_suppressions.txt deleted file mode 100644 index 7a7b01f1..00000000 --- a/cppcheck_suppressions.txt +++ /dev/null @@ -1,4 +0,0 @@ -memleak:src/cbor/arrays.c:126 -mismatchSize:src/cbor/ints.c -mismatchSize:src/cbor/floats_ctrls.c -*:test/cmocka/* diff --git a/misc/hooks/pre-commit b/misc/hooks/pre-commit index 2fffd5b1..8f57c54b 100755 --- a/misc/hooks/pre-commit +++ b/misc/hooks/pre-commit @@ -15,7 +15,7 @@ cd .. # Run clang-format and add modified files MODIFIED_UNSTAGED=$(git -C . diff --name-only) -MODIFIED_STAGED=$(git -C . diff --name-only --cached) +MODIFIED_STAGED=$(git -C . diff --name-only --cached --diff-filter=d) ./clang-format.sh diff --git a/src/cbor/internal/encoders.c b/src/cbor/internal/encoders.c index e562b49b..135285f8 100644 --- a/src/cbor/internal/encoders.c +++ b/src/cbor/internal/encoders.c @@ -67,7 +67,7 @@ size_t _cbor_encode_uint64(uint64_t value, unsigned char *buffer, buffer[0] = 0x1B + offset; #ifdef IS_BIG_ENDIAN - memcpy(buffer + 1, &value, 8); + memcpy(buffer + 1, &value, 8); #else buffer[1] = value >> 56; buffer[2] = value >> 48; diff --git a/src/cbor/strings.c b/src/cbor/strings.c index 4e201ccc..ceaea855 100644 --- a/src/cbor/strings.c +++ b/src/cbor/strings.c @@ -91,9 +91,9 @@ bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) { return false; } - size_t new_chunk_capacity = data->chunk_capacity == 0 - ? 1 - : CBOR_BUFFER_GROWTH * (data->chunk_capacity); + size_t new_chunk_capacity = + data->chunk_capacity == 0 ? 1 + : CBOR_BUFFER_GROWTH * (data->chunk_capacity); cbor_item_t **new_chunks_data = _cbor_realloc_multiple( data->chunks, sizeof(cbor_item_t *), new_chunk_capacity); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f459b5fa..c5ace5d7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,8 @@ file(GLOB TESTS "*_test.c") find_package(CMocka REQUIRED) +message(STATUS "CMocka vars: ${CMOCKA_LIBRARIES} ${CMOCKA_INCLUDE_DIR}") + foreach (TEST ${TESTS}) string(REGEX REPLACE ".*/([^/]+).c" "\\1" NAME ${TEST}) message("Adding test ${NAME}")