From f035af941c0831a40be4661e45a15f530cc90e3a Mon Sep 17 00:00:00 2001 From: Matt Woodward <46688854+woodfell@users.noreply.github.com> Date: Thu, 6 Jun 2019 15:49:47 +1000 Subject: [PATCH] Import standard cmake modules, standardise dependency resolution [ESD-1246] [ESD-1247] (#126) * Start importing common cmake repo * Finish importing cmake modules * Swift to swift fork of eigen * Use https for common cmake modules * Use https for eigen * Fix CI errors * Make unit tests and examples optional * Include tests directory in interface * Update swift project options * Update project options * Bump cmake modules * Revert to old unit test run targets * Fix example build targets * Change cereal to be header only interface target * Don't clobber global compiler flags * Bump cmake modules * cmake compile features not available in higher level projects * Re-add clang-format support * Bump cmake modules * Include gzip headers as system * Remove clang-format hack * Bump cmake * Import merged submodules * Enforce no compiler extensions * Bump cmake modules * Bump cmake modules * Bump cmake modules * Bump cmake modules --- .gitmodules | 9 ++-- CMakeLists.txt | 51 ++++++++++++++-------- cmake/ClangTools.cmake | 68 ----------------------------- cmake/FindCereal.cmake | 11 +++++ cmake/FindGFlags.cmake | 50 --------------------- cmake/FindGzip-Hpp.cmake | 12 +++++ cmake/common | 1 + examples/CMakeLists.txt | 1 + tests/CMakeLists.txt | 4 +- third_party/eigen | 2 +- third_party/fast-cpp-csv-parser | 2 +- third_party/{gflags => googleflags} | 0 12 files changed, 69 insertions(+), 142 deletions(-) delete mode 100644 cmake/ClangTools.cmake create mode 100644 cmake/FindCereal.cmake delete mode 100644 cmake/FindGFlags.cmake create mode 100644 cmake/FindGzip-Hpp.cmake create mode 160000 cmake/common rename third_party/{gflags => googleflags} (100%) diff --git a/.gitmodules b/.gitmodules index 80dead1e..e26ef191 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,18 +1,21 @@ [submodule "third_party/eigen"] path = third_party/eigen - url = https://github.com/PX4/eigen.git + url = https://github.com/swift-nav/eigen.git [submodule "third_party/gflags"] - path = third_party/gflags + path = third_party/googleflags url = https://github.com/gflags/gflags.git [submodule "third_party/googletest"] path = third_party/googletest url = https://github.com/google/googletest.git [submodule "third_party/fast-cpp-csv-parser"] path = third_party/fast-cpp-csv-parser - url = https://github.com/ben-strasser/fast-cpp-csv-parser.git + url = https://github.com/swift-nav/fast-cpp-csv-parser.git [submodule "third_party/cereal"] path = third_party/cereal url = https://github.com/USCiLab/cereal.git [submodule "third_party/gzip-hpp"] path = third_party/gzip-hpp url = https://github.com/mapbox/gzip-hpp.git +[submodule "cmake/cmake"] + path = cmake/common + url = https://github.com/swift-nav/cmake.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b79607c..ed4b9d1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,31 +1,43 @@ project(albatross) cmake_minimum_required(VERSION 2.8.7) -set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") -include(ClangTools) +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake" "${CMAKE_CURRENT_LIST_DIR}/cmake/common") + +include(CCache) +include(SwiftCmakeOptions) +swift_create_project_options( + HAS_TESTS + HAS_EXAMPLES + TEST_PACKAGES "Googletest" "GFlags" + ) + +include(ClangFormat) +swift_setup_clang_format() -option(ENABLE_AUTOFORMAT "Enable automatic formatting and replacement with clang-format." ON) -option(ENABLE_AUTOLINT "Enable automatic linting and warning with clang-tidy. " OFF) option(ENABLE_STACK_ANALYSIS "Enable stack analysis. Requires gcc." OFF) -if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") +if(albatross_BUILD_EXAMPLES) + find_package(GFlags REQUIRED) endif() -add_subdirectory(third_party/googletest) +set(JUST_INSTALL_CEREAL YES CACHE BOOL "" FORCE) #Don't generate docs, it conflicts with Eigen +find_package(Cereal REQUIRED) +find_package(Eigen REQUIRED) +find_package(FastCSV REQUIRED) +find_package(Gzip-Hpp REQUIRED) add_library(albatross INTERFACE) target_include_directories(albatross INTERFACE "${PROJECT_SOURCE_DIR}/include" + "${PROJECT_SOURCE_DIR}/tests" ) -target_include_directories(albatross SYSTEM INTERFACE - "${PROJECT_SOURCE_DIR}/third_party/eigen/" - "${PROJECT_SOURCE_DIR}/third_party/cereal/include" - "${PROJECT_SOURCE_DIR}/third_party/fast-cpp-csv-parser/" - "${PROJECT_SOURCE_DIR}/third_party/gzip-hpp/include" - ) - -include(${PROJECT_SOURCE_DIR}/cmake/FindGFlags.cmake) +target_link_libraries(albatross + INTERFACE + eigen + cereal + fast-csv + gzip-hpp + ) set(albatross_COMPILE_OPTIONS -Werror @@ -67,7 +79,10 @@ set(albatross_COMPILE_OPTIONS -Wvolatile-register-var -Wwrite-strings ) -target_compile_features(albatross INTERFACE cxx_std_14) -add_subdirectory(tests) -add_subdirectory(examples) +if(albatross_BUILD_TESTS) + add_subdirectory(tests) +endif() +if(albatross_BUILD_EXAMPLES) + add_subdirectory(examples) +endif() diff --git a/cmake/ClangTools.cmake b/cmake/ClangTools.cmake deleted file mode 100644 index 0323d01f..00000000 --- a/cmake/ClangTools.cmake +++ /dev/null @@ -1,68 +0,0 @@ -# CMake script searches for clang-format and sets the following -# variables: -# -# CLANG_FORMAT_PATH : Fully-qualified path to the clang-format executable -# -# Additionally defines the following targets: -# -# clang-format-all : Run clang-format over all files. -# clang-format-diff : Run clang-format over all files differing from master. -# cppcheck-all : Run cppcheck over all files. - -# Do not use clang tooling when cross compiling. -if(CMAKE_CROSSCOMPILING) - return() -endif(CMAKE_CROSSCOMPILING) - -################################################################################ -# Search for tools. -################################################################################ - -# Check for Clang format -set(CLANG_FORMAT_PATH "NOTSET" CACHE STRING "Absolute path to the clang-format executable") -if("${CLANG_FORMAT_PATH}" STREQUAL "NOTSET") - find_program(CLANG_FORMAT NAMES - clang-format-6.0 - ) - message(STATUS "Using clang format: ${CLANG_FORMAT}") - if("${CLANG_FORMAT}" STREQUAL "CLANG_FORMAT-NOTFOUND") - message(WARNING "Could not find 'clang-format' please set CLANG_FORMAT_PATH:STRING") - else() - set(CLANG_FORMAT_PATH ${CLANG_FORMAT}) - message(STATUS "Found: ${CLANG_FORMAT_PATH}") - endif() -else() - if(NOT EXISTS ${CLANG_FORMAT_PATH}) - message(WARNING "Could not find 'clang-format': ${CLANG_FORMAT_PATH}") - else() - message(STATUS "Found: ${CLANG_FORMAT_PATH}") - endif() -endif() - -################################################################################ -# Conditionally add targets. -################################################################################ - - -if (EXISTS ${CLANG_FORMAT_PATH}) - # Format all files .cc files (and their headers) in project - add_custom_target(clang-format-all-albatross - COMMAND git ls-files -- '../*.cc' '../*.h' '../*.hpp' - | sed 's/^...//' | sed 's\#^\#${CMAKE_SOURCE_DIR}/\#' - | xargs "${CLANG_FORMAT_PATH}" -i - ) - # In-place format *.cc files that differ from master, and are not listed as - # being DELETED. - add_custom_target(clang-format-diff-albatross - COMMAND git diff --diff-filter=ACMRTUXB --name-only master -- '../*.cc' '../*.h' '../*.hpp' - | sed 's\#^\#${CMAKE_SOURCE_DIR}/\#' - | xargs "${CLANG_FORMAT_PATH}" -i - ) -endif() -if (EXISTS ${CPPCHECK_PATH}) - add_custom_target(cppcheck-all-albatross - COMMAND git ls-files -- '../*.cc' '../*.h' '../*.hpp' - | ${CPPCHECK_PATH} --enable=all --std=c++14 -I../include -isystem../third_party/eigen -q --file-list=- - ) -endif() - diff --git a/cmake/FindCereal.cmake b/cmake/FindCereal.cmake new file mode 100644 index 00000000..71497d3d --- /dev/null +++ b/cmake/FindCereal.cmake @@ -0,0 +1,11 @@ +# Cereal throws lots of compiler errors if we add_subdirectory it, but we +# only want the headers anyway +if(TARGET cereal) + return() +endif() + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cereal/include") + add_library(cereal INTERFACE) + + target_include_directories(cereal SYSTEM INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cereal/include") +endif() diff --git a/cmake/FindGFlags.cmake b/cmake/FindGFlags.cmake deleted file mode 100644 index 299b780f..00000000 --- a/cmake/FindGFlags.cmake +++ /dev/null @@ -1,50 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -# This brings in the external project support in cmake -include(ExternalProject) - -set(ALBATROSS_GFLAGS_CMAKE_CXX_FLAGS "") -if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(ALBATROSS_GFLAGS_CMAKE_CXX_FLAGS "-stdlib=libc++") -endif() - -# This adds libgflags as an external project with the specified parameters. -ExternalProject_Add(libgflags - # We use SOURCE_DIR because we use version control to track the - # version of this library instead of using the build tool - SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/gflags - # We don't want to install this globally; we just want to use it in - # place. - INSTALL_COMMAND cmake -E echo "Not installing googleflags globally." - # This determines the subdirectory of the build directory in which - # gflags gets built. - PREFIX gflags - # This simply passes down cmake arguments, which allows us to define - # gflags-specific cmake flags as arguments to the toplevel cmake - # invocation. - CMAKE_ARGS ${CMAKE_ARGS} -DCMAKE_CXX_FLAGS=${ALBATROSS_GFLAGS_CMAKE_CXX_FLAGS}) - -# This pulls out the variables `source_dir` and `binary_dir` from the -# gflags project, so we can refer to them below. -ExternalProject_Get_Property(libgflags source_dir binary_dir) - -# This tells later `target_link_libraries` commands about the gflags -# library. -add_library(gflags STATIC IMPORTED GLOBAL) - -# This tells where the static libgflags binary will end up. I have no -# idea how to control this and just found it with `locate`. -set_property(TARGET gflags - PROPERTY IMPORTED_LOCATION "${binary_dir}/lib/libgflags.a") - -# This makes the gflags library depend on the libgflags external -# project, so that when you ask to link against gflags, the external -# project will get built. -add_dependencies(gflags libgflags) - -# This tells where the libgflags headers generated during the build -# process will end up. I have no idea how to control this and just -# found it with `locate`. Note that any targets specified after this -# file fragment is included will now include gflags headers as part of -# their compile commands. -include_directories(SYSTEM "${binary_dir}/include") diff --git a/cmake/FindGzip-Hpp.cmake b/cmake/FindGzip-Hpp.cmake new file mode 100644 index 00000000..47c4bf0b --- /dev/null +++ b/cmake/FindGzip-Hpp.cmake @@ -0,0 +1,12 @@ +# Non-swift code that doesn't nicely export a library target, can't use the generic module +if(EXISTS "${PROJECT_SOURCE_DIR}/third_party/gzip-hpp/CMakeLists.txt") + add_library(gzip-hpp INTERFACE) + target_include_directories(gzip-hpp SYSTEM INTERFACE $) +else() + if(Gzip-Hpp_FIND_REQUIRED) + message(FATAL_ERROR "Could not find REQUIRED package Gzip-Hpp") + else() + message(WARNING "Could not find package Gzip-Hpp") + endif() +endif() + diff --git a/cmake/common b/cmake/common new file mode 160000 index 00000000..ed44946e --- /dev/null +++ b/cmake/common @@ -0,0 +1 @@ +Subproject commit ed44946e1acf06acea6963393a56879caea600de diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2ebee47c..c755bea6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -85,5 +85,6 @@ set(albatross_example_BINARIES ) foreach(TARGET IN LISTS albatross_example_BINARIES) target_compile_options(${TARGET} PRIVATE ${albatross_COMPILE_OPTIONS}) + set_target_properties(${TARGET} PROPERTIES CXX_STANDARD 14 CXX_EXTENSIONS OFF) endforeach() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a73a5b4c..f23274a0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(albatross_unit_tests EXCLUDE_FROM_ALL +add_executable(albatross_unit_tests test_block_utils.cc test_call_trace.cc test_core_dataset.cc @@ -50,6 +50,8 @@ target_link_libraries(albatross_unit_tests nlopt z ) +set_target_properties(albatross_unit_tests PROPERTIES CXX_STANDARD 14 CXX_EXTENSIONS OFF) + add_custom_target(run_albatross_unit_tests ALL COMMAND albatross_unit_tests COMMENT "Running unit tests" diff --git a/third_party/eigen b/third_party/eigen index 7cf1c017..64d4e3ca 160000 --- a/third_party/eigen +++ b/third_party/eigen @@ -1 +1 @@ -Subproject commit 7cf1c0179eb0f5499dfc1bffbd229783a7865fe1 +Subproject commit 64d4e3ca2dad251759a0b1ddbbdd1cd5ebc6a5a1 diff --git a/third_party/fast-cpp-csv-parser b/third_party/fast-cpp-csv-parser index 3b439a66..e8f09db8 160000 --- a/third_party/fast-cpp-csv-parser +++ b/third_party/fast-cpp-csv-parser @@ -1 +1 @@ -Subproject commit 3b439a664090681931c6ace78dcedac6d3a3907e +Subproject commit e8f09db8132df2dcf26616d2326a6a34dd98a94e diff --git a/third_party/gflags b/third_party/googleflags similarity index 100% rename from third_party/gflags rename to third_party/googleflags