Commit a8ae5b6e authored by Nikolaus Demmel's avatar Nikolaus Demmel

cmake and ci improvements

- ci: add build jobs for Debug/RelWithDebInfo on Bionic, as well as
  release builds on Xenial and El Capitan. Use templates to simplify
  .gitlab-ci.yml
- cmake: raise minimum version to 3.10
- cmake: add workarounds to support macOS < 10.14
- cmake: set ccache only if not yet set and print info
- cmake: add NDEBUG define (to disable asserts) for release build and
  EIGEN_INITIALIZE_MATRICES_BY_NAN for others. Add externally supplied
  CMAKE_CXX_FLAGS at the end to allow overriding the defaults.
- tests: add enable_testing() in main CMakeLists.txt, such that we can
  call 'ctest' from build folder.
- tests: simplify gtest / gtest_main linking
- tests: enabled benchmarks only on Linux with GCC (doesn't compile
  with clang)
- tests: use the newer gtest_discover_tests instead of add_test, which
  adds a separate cmake test for every gtest, even with multiple
  defined in a single executable. Its not only more reliable and less
  verbose in the CMakeLists.txt, but also allows to run 'ctest'
  without -V and still see status for every individual test.
parent bb7300d5
Pipeline #57951904 passed with stage
in 3 minutes and 10 seconds
image: vladyslavusenko/b_image:latest
compile:
stage: build
variables:
GIT_SUBMODULE_STRATEGY: recursive
BUILD_TYPE: Release
# template for docker builds with ccache
.prepare_docker_template: &prepare_docker_definition
tags:
- docker
before_script:
- mkdir -p ccache
- export CCACHE_BASEDIR=${PWD}
- export CCACHE_DIR=${PWD}/ccache
variables:
GIT_SUBMODULE_STRATEGY: recursive
cache:
key: bionic
paths:
- ccache/
# template for secondary build & unit test configurations
.compile_and_test_template: &compile_and_test_definition
stage: build
script:
- mkdir build
- cd build
- cmake ..
- cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
- make -j4
- cd test/
- ctest -V
- ./benchmark_camera > ../../benchmark_camera.txt
- cd ../../
- ctest
# main build with benchmark and coverage
bionic-release-compile:
<<: *prepare_docker_definition
stage: build
script:
- mkdir build
- cd build
- cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
- make -j4
- ctest
- ./test/benchmark_camera > ../benchmark_camera.txt
- cd ../
- mkdir build_coverage
- cd build_coverage
- cmake .. -DCMAKE_BUILD_TYPE=Coverage
- make -j4
- cd test
- ctest -V
- cd ..
- ctest
- lcov --directory . --capture --output-file coverage.info
- lcov --remove coverage.info '*test/*' '/usr/*' '*thirdparty*' '*googletest*' --output-file coverage.info
- lcov --list coverage.info
......@@ -34,3 +51,25 @@ compile:
paths:
- benchmark_camera.txt
bionic-debug-compile:
<<: *prepare_docker_definition
<<: *compile_and_test_definition
variables:
BUILD_TYPE: Debug
bionic-relwithdebinfo-compile:
<<: *prepare_docker_definition
<<: *compile_and_test_definition
variables:
BUILD_TYPE: RelWithDebInfo
xenial-release-compile:
<<: *prepare_docker_definition
<<: *compile_and_test_definition
image: vladyslavusenko/b_image_xenial:latest
cache:
key: xenial
elcapitan-release-compile:
<<: *compile_and_test_definition
tags: [macos, "10.11"]
cmake_minimum_required(VERSION 3.8)
cmake_minimum_required(VERSION 3.10)
include("cmake_modules/PreProjectWorkarounds.cmake")
project(basalt-headers)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/" ${CMAKE_MODULE_PATH})
# Configure CCache if available
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
message(STATUS "Found ccache: ${CCACHE_PROGRAM}")
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
endif(CCACHE_PROGRAM)
if (NOT CMAKE_C_COMPILER_LAUNCHER AND NOT CMAKE_CXX_COMPILER_LAUNCHER)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
message(STATUS "Found ccache: ${CCACHE_PROGRAM}")
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
else()
message(STATUS "Dind't find ccache")
endif()
else()
message(STATUS "Compiler launcher already set. Not configuring ccache.")
message(STATUS "CMAKE_C_COMPILER_LAUNCHER: ${CMAKE_C_COMPILER_LAUNCHER}")
message(STATUS "CMAKE_CXX_COMPILER_LAUNCHER: ${CMAKE_CXX_COMPILER_LAUNCHER}")
endif()
IF( NOT CMAKE_BUILD_TYPE )
SET( CMAKE_BUILD_TYPE Release)
......@@ -25,9 +35,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -DEIGEN_INITIALIZE_MATRICES_BY_NAN") # cmake default: "-g"
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g -DEIGEN_INITIALIZE_MATRICES_BY_NAN") # cmake default: "-O2 -g -DNDEBUG"
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") # cmake default: "-O3 -DNDEBUG"
SET(CMAKE_CXX_FLAGS "-Wall -Werror -Wextra -march=${CXX_MARCH} ${CMAKE_CXX_FLAGS}")
SET(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage -fno-inline -fno-inline-small-functions -fno-default-inline")
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage")
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} --coverage")
......@@ -43,6 +54,7 @@ include_directories(thirdparty/Sophus)
include_directories(thirdparty/cereal/include)
include_directories(include)
enable_testing()
add_subdirectory(test)
......
# workarounds that need to be applied before the `project(...)` call
# macOS < 10.14 requires that we set CMAKE_OSX_DEPLOYMENT_TARGET to
# 10.14 before `project(...)` is called, otherwise clang thinks
# certail C++17 STL features such as std::visit are not available,
# even if we use a recent libc++ from homebrew that has these
# features. Moreover, unless the compiler is specified, use clang from
# homebrew, since Apple's is too old.
if(APPLE)
# Note: It is implicitly assumed that we use clang and libc++ from
# brewed llvm on macOS < 10.14, since Apple's clang on those systems
# is too old.
# Note: CMAKE_SYSTEM_VERSION doesn't work before `project(...)`
execute_process(COMMAND sw_vers -productVersion OUTPUT_VARIABLE _macos_version)
string(REGEX REPLACE "\n$" "" _macos_version "${_macos_version}")
if (_macos_version VERSION_LESS 10.14.0)
message(STATUS "Detected macOS version '${_macos_version}', which is earlier than macOS 10.14 Mojave. Applying workarounds for clang and libc++...")
# Ensure libc++ enables all features.
# See: https://stackoverflow.com/a/53868971/1813258
# See: https://stackoverflow.com/a/53887048/1813258
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14" CACHE STRING "Minimum OS X deployment version")
message(STATUS "... setting deployment target to '${CMAKE_OSX_DEPLOYMENT_TARGET}' to trick libc++ into not disabling some features (like std::visit)")
# On macOS < 10.14, we need to ensure we use brewed clang.
if (NOT CMAKE_C_COMPILER AND NOT CMAKE_CXX_COMPILER)
set(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang++")
message(STATUS "... compiler not specified; setting to brewed clang '${CMAKE_C_COMPILER}' and '${CMAKE_CXX_COMPILER}'")
else()
message(STATUS "... not setting compiler; already set to '${CMAKE_C_COMPILER}' and '${CMAKE_CXX_COMPILER}'")
endif()
else()
message(STATUS "Detected macOS version '${_macos_version}', which is newer or equal to macOS 10.14 Mojave. Not applying workarounds.")
endif()
endif()
cmake_minimum_required(VERSION 3.2)
cmake_minimum_required(VERSION 3.10)
if(NOT TARGET gtest_main)
add_subdirectory(googletest gtest EXCLUDE_FROM_ALL)
include_directories(googletest)
add_subdirectory(googletest EXCLUDE_FROM_ALL)
endif(NOT TARGET gtest_main)
set(GTEST_MAIN_LIBRARY gtest_main)
set(GTEST_LIBRARY gtest)
include_directories(include)
add_executable(test_spline src/test_spline.cpp)
target_link_libraries(test_spline ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
target_link_libraries(test_spline gtest gtest_main)
add_executable(test_spline_se3 src/test_spline_se3.cpp)
target_link_libraries(test_spline_se3 ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
target_link_libraries(test_spline_se3 gtest gtest_main)
add_executable(test_camera src/test_camera.cpp)
target_link_libraries(test_camera ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
target_link_libraries(test_camera gtest gtest_main)
add_executable(test_sophus src/test_sophus.cpp)
target_link_libraries(test_sophus ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
target_link_libraries(test_sophus gtest gtest_main)
add_executable(test_preintegration src/test_preintegration.cpp)
target_link_libraries(test_preintegration ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY} ${TBB_LIBRARIES})
target_link_libraries(test_preintegration gtest gtest_main ${TBB_LIBRARIES})
# benchmarks
IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
# benchmarks (currently doesnt work on macOS and with clang)
if(NOT APPLE AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "ENABLE tests")
set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "ENABLE exceptions")
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "ENABLE gtests")
......@@ -36,11 +32,13 @@ IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
add_executable(benchmark_camera src/benchmark_camera.cpp)
target_link_libraries(benchmark_camera benchmark)
ENDIF()
endif()
include(GoogleTest) # for gtest_discover_test
enable_testing()
add_test(test_spline test_spline COMMAND Test)
add_test(test_camera test_camera COMMAND Test)
add_test(test_sophus test_sophus COMMAND Test)
add_test(test_preintegration test_preintegration COMMAND Test)
gtest_discover_tests(test_spline)
gtest_discover_tests(test_camera)
gtest_discover_tests(test_sophus)
gtest_discover_tests(test_preintegration)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment