From 091fa1fd1b9ae8eabb99075b3ddfd9023f95468d Mon Sep 17 00:00:00 2001 From: Vladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com> Date: Tue, 19 Nov 2019 03:16:41 +0700 Subject: [PATCH] Add testing of C++ with sanitizers (CI-Docker) (#5631) * Add C++ build testing with clang and gcc This adds Dockerfiles which test building flatc and the C++ library against clang and gcc. See discussion at #5119. It is derived from the Travis CI tooling. The GRPC tests are failing due to #5099 so those are commented out. These are run from the .travis.yml file rather than the tests/docker/languages folder because the builds may each take longer than 30 minutes and were hitting Travis timeouts. Parallel builds and build caching attempt to keep the build times low. * Add GCC 8.3 and Clang 7.0 with sanitizers into CI (based on #5130) - Add a docker based on Debian Buster. - Add C++ building scripts for the docker. - Leak-sanitizer requires SYS_PTRACE. --- .../build-and-run-docker-test-containers.sh | 13 ++++++++---- CMakeLists.txt | 5 +++++ .../Dockerfile.testing.cpp.debian_buster | 10 ++++++++++ tests/docker/build_flatc.run.sh | 15 ++++++++++++++ tests/docker/cpp_test.run.sh | 20 +++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/docker/Dockerfile.testing.cpp.debian_buster create mode 100755 tests/docker/build_flatc.run.sh create mode 100755 tests/docker/cpp_test.run.sh diff --git a/.travis/build-and-run-docker-test-containers.sh b/.travis/build-and-run-docker-test-containers.sh index e6039bf62..d377ba8dd 100755 --- a/.travis/build-and-run-docker-test-containers.sh +++ b/.travis/build-and-run-docker-test-containers.sh @@ -15,10 +15,15 @@ # limitations under the License. set -e -# build flatc on debian once to speed up the test loop below -docker build -t build_flatc_debian_stretch -f tests/docker/Dockerfile.testing.build_flatc_debian_stretch . -BUILD_CONTAINER_ID=$(docker create --read-only build_flatc_debian_stretch) -docker cp ${BUILD_CONTAINER_ID}:/code/flatc flatc_debian_stretch +docker build -t build_cpp_image -f tests/docker/Dockerfile.testing.cpp.debian_buster . +# Run tests with sanitizers (--cap-add SYS_PTRACE), both GCC and Clang. +cpp_test_args="--cap-add SYS_PTRACE build_cpp_image sh ./tests/docker/cpp_test.run.sh Debug" +docker run --rm $cpp_test_args +docker run --rm --env CC=/usr/bin/clang --env CXX=/usr/bin/clang++ $cpp_test_args +# Build flatc on debian once to speed up the test loop below. +docker run --name flatc_container build_cpp_image sh ./tests/docker/build_flatc.run.sh Debug +# All dependent dockers refer to 'flatc_debian_stretch'. +docker cp flatc_container:/flatbuffers/flatc flatc_debian_stretch for f in $(ls tests/docker/languages | sort) do diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b02d45cb..e7e998095 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library" ON) option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" ON) +option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag" + OFF) option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON) option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF) option(FLATBUFFERS_BUILD_SHAREDLIB @@ -295,6 +297,9 @@ if(FLATBUFFERS_BUILD_FLATC) # Make flatc.exe not depend on runtime dlls for easy distribution. target_compile_options(flatc PUBLIC $<$:/MT>) endif() + if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC) + target_link_libraries(flatc PRIVATE -static) + endif() endif() if(FLATBUFFERS_BUILD_FLATHASH) diff --git a/tests/docker/Dockerfile.testing.cpp.debian_buster b/tests/docker/Dockerfile.testing.cpp.debian_buster new file mode 100644 index 000000000..7b0cce2de --- /dev/null +++ b/tests/docker/Dockerfile.testing.cpp.debian_buster @@ -0,0 +1,10 @@ +FROM debian:10.1-slim as base +RUN apt -qq update >/dev/null +RUN apt -qq install -y cmake make build-essential >/dev/null +RUN apt -qq install -y autoconf git libtool >/dev/null +RUN apt -qq install -y clang >/dev/null +FROM base +# Travis machines have 2 cores. Can be redefined with 'run --env PAR_JOBS=N'. +ENV JOBS=2 +WORKDIR /flatbuffers +ADD . . diff --git a/tests/docker/build_flatc.run.sh b/tests/docker/build_flatc.run.sh new file mode 100755 index 000000000..c8885b19b --- /dev/null +++ b/tests/docker/build_flatc.run.sh @@ -0,0 +1,15 @@ +set -e + +JOBS=${JOBS:-1} +config=$1 +echo "" +echo "Build 'flatc' compiler for '$config'" + +cmake . -DCMAKE_BUILD_TYPE=$config \ + -DFLATBUFFERS_BUILD_FLATC=1 -DFLATBUFFERS_STATIC_FLATC=1 \ + -DFLATBUFFERS_BUILD_TESTS=0 -DFLATBUFFERS_INSTALL=0 +cmake --build . --target flatc --clean-first -- -j$JOBS + +echo "Check generated code" +.travis/check-generate-code.sh +echo "Done" diff --git a/tests/docker/cpp_test.run.sh b/tests/docker/cpp_test.run.sh new file mode 100755 index 000000000..fa3b0fb10 --- /dev/null +++ b/tests/docker/cpp_test.run.sh @@ -0,0 +1,20 @@ +set -e + +JOBS=${JOBS:-1} +export UBSAN_OPTIONS=halt_on_error=1 +export ASAN_OPTIONS=halt_on_error=1 +export MAKEFLAGS="-j$JOBS" + +config=$1 +echo "" +echo "Build Flatbuffers project for '$config' with jobs=$JOBS" + +cmake . -DCMAKE_BUILD_TYPE=$config \ + -DFLATBUFFERS_BUILD_TESTS=ON -DFLATBUFFERS_CODE_SANITIZE=ON +cmake --build . --target all --clean-first -- -j$JOBS +ctest --extra-verbose --output-on-failure -j$JOBS + +echo "Check generated code" +.travis/check-generate-code.sh + +echo "C++ tests done"