Add convenience functions and a macro to libs/CMakeLists.txt

Summary:
Add json and cppitertools to libs/CMakeLists.txt.
Import external projects as libraries.
This removes the need to use `add_dependencies` in order to link with
external project.
Extract common ExternalProject_Add function.
Add macro for easier addition of external libraries.

Reviewers: mislav.bradac, mferencevic

Reviewed By: mislav.bradac

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D845
This commit is contained in:
Teon Banek 2017-10-03 13:56:41 +02:00
parent a40248ab65
commit 0c186fe76f
12 changed files with 134 additions and 114 deletions

View File

@ -105,7 +105,7 @@ else()
endif()
# default build type is debug
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
message(STATUS "CMake build type: ${CMAKE_BUILD_TYPE}")
@ -188,7 +188,6 @@ message(STATUS "Generate coverage from unit tests: ${TEST_COVERAGE}")
# includes
include_directories(${src_dir})
include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/libs) # cppitertools
include_directories(SYSTEM ${GFLAGS_INCLUDE_DIR})
include_directories(SYSTEM ${GLOG_INCLUDE_DIR})
include_directories(SYSTEM ${FMT_INCLUDE_DIR})
@ -225,8 +224,7 @@ add_custom_target(generate_opencypher_parser
include_directories(${ANTLR4_INCLUDE_DIR})
add_library(antlr_opencypher_parser_lib STATIC ${antlr_opencypher_generated_src})
add_dependencies(antlr_opencypher_parser_lib antlr4)
target_link_libraries(antlr_opencypher_parser_lib ${ANTLR4_LIBRARY})
target_link_libraries(antlr_opencypher_parser_lib antlr4)
# -----------------------------------------------------------------------------
# all memgraph src files
@ -264,8 +262,8 @@ set(memgraph_src_files
# -----------------------------------------------------------------------------
# memgraph_lib and memgraph_pic depend on these libraries
set(MEMGRAPH_ALL_LIBS stdc++fs Threads::Threads ${FMT_LIBRARY}
antlr_opencypher_parser_lib dl ${GLOG_LIBRARY} ${GFLAGS_LIBRARY})
set(MEMGRAPH_ALL_LIBS stdc++fs Threads::Threads fmt cppitertools
antlr_opencypher_parser_lib dl glog gflags)
if (READLINE_FOUND)
list(APPEND MEMGRAPH_ALL_LIBS ${READLINE_LIBRARY})
endif()
@ -273,7 +271,7 @@ endif()
# STATIC library used by memgraph executables
add_library(memgraph_lib STATIC ${memgraph_src_files})
target_link_libraries(memgraph_lib ${MEMGRAPH_ALL_LIBS})
add_dependencies(memgraph_lib generate_opencypher_parser fmt gflags glog)
add_dependencies(memgraph_lib generate_opencypher_parser)
# -----------------------------------------------------------------------------
# proof of concepts

View File

@ -40,8 +40,7 @@ foreach(test_cpp ${test_type_cpps})
target_link_libraries(${target_name} memgraph_lib)
target_link_libraries(${target_name} ${MEMGRAPH_ALL_LIBS})
# gtest
add_dependencies(${target_name} gtest)
target_link_libraries(${target_name} ${GTEST_LIBRARIES})
target_link_libraries(${target_name} gtest gtest_main)
if(${TEST_COVERAGE})
# for code coverage
target_link_libraries(${target_name} gcov)

View File

@ -7,78 +7,118 @@ if (NPROC EQUAL 0)
set(NPROC 1)
endif()
# convenience functions
function(import_header_library name include_dir)
add_library(${name} INTERFACE IMPORTED GLOBAL)
set_property(TARGET ${name} PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${include_dir})
endfunction(import_header_library)
function(import_library name type location)
add_library(${name} ${type} IMPORTED GLOBAL)
if (${ARGN})
# Optional argument is the name of the external project that we need to
# depend on.
add_dependencies(${name} ${ARGN0})
else()
add_dependencies(${name} ${name}-proj)
endif()
# Unfortunately, we cannot use include_dir before it is built.
# set_property(TARGET ${name} PROPERTY
# INTERFACE_INCLUDE_DIRECTORIES ${include_dir})
set_property(TARGET ${name} PROPERTY IMPORTED_LOCATION ${location})
endfunction(import_library)
# Calls `ExternalProject_Add(${name}-proj` with default arguments for cmake
# configuration. CMAKE_BUILD_TYPE is set to Release, CMAKE_C_COMPILER and
# CMAKE_CXX_COMPILER are forwarded as used in this project. You can pass
# NO_C_COMPILER option to avoid forwarding CMAKE_C_COMPILER. Installation is
# done in SOURCE_DIR, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}.
# You can pass additional arguments via CMAKE_ARGS. Dependencies and
# installation can be set as in regular ExternalProject_Add, via DEPENDS and
# INSTALL_COMMAND arguments.
function(add_external_project name)
set(options NO_C_COMPILER)
set(one_value_kwargs SOURCE_DIR)
set(multi_value_kwargs CMAKE_ARGS DEPENDS INSTALL_COMMAND)
cmake_parse_arguments(KW "${options}" "${one_value_kwargs}" "${multi_value_kwargs}" ${ARGN})
set(source_dir ${CMAKE_CURRENT_SOURCE_DIR}/${name})
if (KW_SOURCE_DIR)
set(source_dir ${KW_SOURCE_DIR})
endif()
if (NOT KW_NO_C_COMPILER)
set(KW_CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} ${KW_CMAKE_ARGS})
endif()
ExternalProject_Add(${name}-proj DEPENDS ${KW_DEPENDS}
PREFIX ${source_dir} SOURCE_DIR ${source_dir}
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${source_dir}
${KW_CMAKE_ARGS}
INSTALL_COMMAND ${KW_INSTALL_COMMAND})
endfunction(add_external_project)
# Calls `add_external_project`, sets NAME_LIBRARY, NAME_INCLUDE_DIR variables
# and adds the library via `import_library`.
macro(import_external_library name type library_location include_dir)
add_external_project(${name} ${ARGN})
string(TOUPPER ${name} _upper_name)
set(${_upper_name}_LIBRARY ${library_location} CACHE FILEPATH
"Path to ${name} library" FORCE)
set(${_upper_name}_INCLUDE_DIR ${include_dir} CACHE FILEPATH
"Path to ${name} include directory" FORCE)
mark_as_advanced(${_upper_name}_LIBRARY ${_upper_name}_INCLUDE_DIR)
import_library(${name} ${type} ${${_upper_name}_LIBRARY})
endmacro(import_external_library)
# setup antlr
ExternalProject_Add(antlr4
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp
import_external_library(antlr4 STATIC
${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp/lib/libantlr4-runtime.a
${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp/include/antlr4-runtime
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp
# http://stackoverflow.com/questions/37096062/get-a-basic-c-program-to-compile-using-clang-on-ubuntu-16/38385967#38385967
CMAKE_ARGS # http://stackoverflow.com/questions/37096062/get-a-basic-c-program-to-compile-using-clang-on-ubuntu-16/38385967#38385967
-DWITH_LIBCXX=OFF # because of debian bug
# Make a License.txt out of thin air, so that antlr4.6 knows how to build.
# When we upgrade antlr, this will no longer be needed.
INSTALL_COMMAND touch ${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp/License.txt
COMMAND $(MAKE) install)
set(ANTLR4_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp/lib/libantlr4-runtime.a
CACHE FILEPATH "Path to antlr4 library" FORCE)
set(ANTLR4_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/antlr4/runtime/Cpp/include/antlr4-runtime
CACHE PATH "Path to antlr4 include directory" FORCE)
mark_as_advanced(ANTLR4_LIBRARY ANTLR4_INCLUDE_DIR)
# Setup google benchmark.
ExternalProject_Add(benchmark
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/benchmark
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/benchmark
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/benchmark)
set(BENCHMARK_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/benchmark/lib/libbenchmark.a
CACHE FILEPATH "Path to benchmark library" FORCE)
set(BENCHMARK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/benchmark/include
CACHE PATH "Path to benchmark include directory" FORCE)
mark_as_advanced(BENCHMARK_LIBRARY BENCHMARK_INCLUDE_DIR)
import_external_library(benchmark STATIC
${CMAKE_CURRENT_SOURCE_DIR}/benchmark/lib/libbenchmark.a
${CMAKE_CURRENT_SOURCE_DIR}/benchmark/include)
# setup fmt format
ExternalProject_Add(fmt
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/fmt
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/fmt
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/fmt
# skip testing
-DFMT_TEST=OFF)
set(FMT_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/fmt/lib/libfmt.a
CACHE FILEPATH "Path to fmt library" FORCE)
set(FMT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/fmt/include
CACHE PATH "Path to fmt include directory" FORCE)
mark_as_advanced(FMT_LIBRARY FMT_INCLUDE_DIR)
import_external_library(fmt STATIC
${CMAKE_CURRENT_SOURCE_DIR}/fmt/lib/libfmt.a
${CMAKE_CURRENT_SOURCE_DIR}/fmt/include
# Skip testing.
CMAKE_ARGS -DFMT_TEST=OFF)
# setup rapidcheck (it cannot be external, since it doesn't have install
# target)
set(RC_ENABLE_GTEST ON CACHE BOOL "Build Google Test integration" FORCE)
set(RC_ENABLE_GMOCK ON CACHE BOOL "Build Google Mock integration" FORCE)
mark_as_advanced(RC_ENABLE_GTEST RC_ENABLE_GMOCK)
add_subdirectory(rapidcheck EXCLUDE_FROM_ALL)
# setup google test
ExternalProject_Add(gtest
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/googletest
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/googletest)
set(GTEST_LIBRARIES
${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgmock.a
${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgmock_main.a
${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgtest.a
${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgtest_main.a
CACHE FILEPATH "Path to googletest library" FORCE)
add_external_project(gtest SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
set(GTEST_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest/include
CACHE PATH "Path to googletest include directory" FORCE)
mark_as_advanced(GTEST_LIBRARIES GTEST_INCLUDE_DIR)
CACHE PATH "Path to gtest and gmock include directory" FORCE)
set(GMOCK_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgmock.a
CACHE FILEPATH "Path to gmock library" FORCE)
set(GMOCK_MAIN_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgmock_main.a
CACHE FILEPATH "Path to gmock_main library" FORCE)
set(GTEST_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgtest.a
CACHE FILEPATH "Path to gtest library" FORCE)
set(GTEST_MAIN_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgtest_main.a
CACHE FILEPATH "Path to gtest_main library" FORCE)
mark_as_advanced(GTEST_INCLUDE_DIR GMOCK_LIBRARY GMOCK_MAIN_LIBRARY GTEST_LIBRARY GTEST_MAIN_LIBRARY)
import_library(gtest STATIC ${GTEST_LIBRARY} gtest-proj)
import_library(gtest_main STATIC ${GTEST_MAIN_LIBRARY} gtest-proj)
import_library(gmock STATIC ${GMOCK_LIBRARY} gtest-proj)
import_library(gmock_main STATIC ${GMOCK_MAIN_LIBRARY} gtest-proj)
# setup google flags
set(GFLAGS_NO_FILENAMES "0")
@ -86,22 +126,16 @@ if ("${CMAKE_BUILD_TYPE}" MATCHES "^(R|r)(E|e)(L|l).+")
set(GFLAGS_NO_FILENAMES "1")
endif()
ExternalProject_Add(gflags
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/gflags
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gflags
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
# Not needed, since gflags is C++ only.
# -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DGFLAGS_NO_FILENAMES=${GFLAGS_NO_FILENAMES}
# setup google flags
import_external_library(gflags STATIC
${CMAKE_CURRENT_SOURCE_DIR}/gflags/lib/libgflags.a
${CMAKE_CURRENT_SOURCE_DIR}/gflags/include
# Not needed, since gflags is C++ only.
NO_C_COMPILER
# Don't register installation in ~/.cmake
CMAKE_ARGS -DREGISTER_INSTALL_PREFIX=OFF
-DBUILD_gflags_nothreads_LIB=OFF
-DREGISTER_INSTALL_PREFIX=OFF
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/gflags)
set(GFLAGS_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/gflags/lib/libgflags.a
CACHE FILEPATH "Path to gflags library" FORCE)
set(GFLAGS_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/gflags/include
CACHE PATH "Path to gflags include directory" FORCE)
mark_as_advanced(GFLAGS_LIBRARY GFLAGS_INCLUDE_DIR)
-DGFLAGS_NO_FILENAMES=${GFLAGS_NO_FILENAMES})
# Setup google logging after gflags (so that glog can use it).
set(GLOG_NO_FILENAMES "0")
@ -111,18 +145,17 @@ if ("${CMAKE_BUILD_TYPE}" MATCHES "^(R|r)(E|e)(L|l).+")
set(GLOG_NO_STACKTRACE "1")
endif()
ExternalProject_Add(glog DEPENDS gflags
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/glog
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/glog
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/glog
# Setup google logging after gflags (so that glog can use it).
import_external_library(glog STATIC
${CMAKE_CURRENT_SOURCE_DIR}/glog/lib/libglog.a
${CMAKE_CURRENT_SOURCE_DIR}/glog/include
DEPENDS gflags-proj
CMAKE_ARGS -Dgflags_DIR=${CMAKE_CURRENT_SOURCE_DIR}/gflags/lib/cmake/gflags
-DGLOG_NO_FILENAMES=${GLOG_NO_FILENAMES}
-DGLOG_NO_STACKTRACE=${GLOG_NO_STACKTRACE}
-Dgflags_DIR=${CMAKE_CURRENT_SOURCE_DIR}/gflags/lib/cmake/gflags)
set(GLOG_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/glog/lib/libglog.a CACHE FILEPATH
"Path to glog library" FORCE)
set(GLOG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/glog/include CACHE PATH
"Path to glog include directory" FORCE)
mark_as_advanced(GLOG_LIBRARY GLOG_INCLUDE_DIR)
-DGLOG_NO_STACKTRACE=${GLOG_NO_STACKTRACE})
# Setup cppitertools
import_header_library(cppitertools ${CMAKE_CURRENT_SOURCE_DIR})
# Setup json
import_header_library(json ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -28,9 +28,8 @@ foreach(poc_cpp ${poc_cpps})
# link libraries
target_link_libraries(${target_name} memgraph_lib)
add_dependencies(${target_name} benchmark gtest)
target_link_libraries(${target_name} ${GTEST_LIBRARIES})
target_link_libraries(${target_name} gtest gtest_main)
# google-benchmark requires threads
target_link_libraries(${target_name} ${BENCHMARK_LIBRARY} Threads::Threads)
target_link_libraries(${target_name} benchmark Threads::Threads)
endforeach()

View File

@ -42,5 +42,4 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/manual)
add_subdirectory(${PROJECT_SOURCE_DIR}/unit)
# property based test binaries
include_directories(${CMAKE_SOURCE_DIR}/libs/rapidcheck/extras/gtest/include)
add_subdirectory(${PROJECT_SOURCE_DIR}/property_based)

View File

@ -29,8 +29,7 @@ foreach(test_cpp ${test_type_cpps})
# link libraries
target_link_libraries(${target_name} memgraph_lib)
# google-benchmark
add_dependencies(${target_name} benchmark)
target_link_libraries(${target_name} ${BENCHMARK_LIBRARY} Threads::Threads)
target_link_libraries(${target_name} benchmark Threads::Threads)
# register test
set(output_path

View File

@ -28,8 +28,7 @@ foreach(test_cpp ${test_type_cpps})
# link libraries
target_link_libraries(${target_name} memgraph_lib)
# gtest
add_dependencies(${target_name} gtest)
target_link_libraries(${target_name} ${GTEST_LIBRARIES})
target_link_libraries(${target_name} gtest gmock gtest_main)
# register test
add_test(${target_name} ${exec_name})

View File

@ -33,7 +33,6 @@ foreach(test_cpp ${test_type_cpps})
# link libraries
target_link_libraries(${target_name} memgraph_lib)
# gtest
add_dependencies(${target_name} gtest)
target_link_libraries(${target_name} ${GTEST_LIBRARIES})
target_link_libraries(${target_name} gtest gtest_main)
endforeach()

View File

@ -26,9 +26,8 @@ foreach(test_cpp ${test_type_cpps})
# link libraries
target_link_libraries(${target_name} memgraph_lib)
# gtest
add_dependencies(${target_name} gtest)
target_link_libraries(${target_name} ${GTEST_LIBRARIES})
target_link_libraries(${target_name} rapidcheck)
target_link_libraries(${target_name} gtest gtest_main)
target_link_libraries(${target_name} rapidcheck rapidcheck_gtest)
# register test
set(output_path ${CMAKE_BINARY_DIR}/test_results/unit/${target_name}.xml)

View File

@ -17,7 +17,7 @@ foreach(test_cpp ${test_type_cpps})
# build exec file
add_executable(${target_name} ${test_cpp})
set_property(TARGET ${target_name} PROPERTY CXX_STANDARD ${cxx_standard})
if(${TEST_COVERAGE})
if(TEST_COVERAGE)
set_target_properties(${target_name} PROPERTIES COMPILE_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
set_target_properties(${target_name} PROPERTIES LINK_FLAGS "--coverage -fprofile-arcs -ftest-coverage")
endif()
@ -30,9 +30,8 @@ foreach(test_cpp ${test_type_cpps})
# link libraries
target_link_libraries(${target_name} memgraph_lib)
# gtest
add_dependencies(${target_name} gtest)
target_link_libraries(${target_name} ${GTEST_LIBRARIES})
if(${TEST_COVERAGE})
target_link_libraries(${target_name} gtest gmock gtest_main)
if(TEST_COVERAGE)
# for code coverage
target_link_libraries(${target_name} gcov)
endif()

View File

@ -68,7 +68,6 @@ endif()
add_subdirectory(${PROJECT_SOURCE_DIR}/../libs libs EXCLUDE_FROM_ALL)
include_directories(SYSTEM ${GFLAGS_INCLUDE_DIR})
include_directories(SYSTEM ${GLOG_INCLUDE_DIR})
include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/../libs) # cppitertools
include_directories(SYSTEM ${FMT_INCLUDE_DIR})
# Include memgraph headers

View File

@ -13,9 +13,7 @@ add_executable(csv_to_snapshot
${memgraph_src_dir}/transactions/transaction.cpp
)
target_link_libraries(csv_to_snapshot stdc++fs Threads::Threads ${FMT_LIBRARY}
${GFLAGS_LIBRARY} ${GLOG_LIBRARY})
add_dependencies(csv_to_snapshot gflags glog fmt)
target_link_libraries(csv_to_snapshot stdc++fs Threads::Threads fmt
gflags glog cppitertools)
install(TARGETS csv_to_snapshot
RUNTIME DESTINATION .)