You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.5 KiB

5 years ago
function(disable_clang_tidy target)
set_target_properties(${target} PROPERTIES
C_CLANG_TIDY ""
CXX_CLANG_TIDY ""
)
endfunction()
function(addtest test_name)
add_executable(${test_name} ${ARGN})
addtest_part(${test_name} ${ARGN})
target_link_libraries(${test_name}
GTest::main
GMock::main
)
add_test(
NAME ${test_name}
COMMAND $<TARGET_FILE:${test_name}>
)
set_target_properties(${test_name} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test_bin
ARCHIVE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/test_lib
LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/test_lib
)
disable_clang_tidy(${test_name})
endfunction()
function(addtest_part test_name)
if (POLICY CMP0076)
cmake_policy(SET CMP0076 NEW)
endif ()
target_sources(${test_name} PUBLIC
${ARGN}
)
target_link_libraries(${test_name}
GTest::gtest
)
endfunction()
# conditionally applies flag. If flag is supported by current compiler, it will be added to compile options.
function(add_flag flag)
check_cxx_compiler_flag(${flag} FLAG_${flag})
if (FLAG_${flag} EQUAL 1)
add_compile_options(${flag})
endif ()
endfunction()
function(compile_proto_to_cpp PROTO_LIBRARY_NAME PB_H PB_CC PROTO)
5 years ago
get_target_property(Protobuf_INCLUDE_DIR protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc IMPORTED_LOCATION_RELEASE)
if (NOT Protobuf_PROTOC_EXECUTABLE)
message(FATAL_ERROR "Protobuf_PROTOC_EXECUTABLE is empty")
endif ()
if (NOT Protobuf_INCLUDE_DIR)
message(FATAL_ERROR "Protobuf_INCLUDE_DIR is empty")
endif ()
get_filename_component(PROTO_ABS "${PROTO}" REALPATH)
# get relative (to CMAKE_BINARY_DIR) path of current proto file
file(RELATIVE_PATH SCHEMA_REL "${CMAKE_BINARY_DIR}/src" "${CMAKE_CURRENT_BINARY_DIR}")
set(SCHEMA_OUT_DIR ${CMAKE_BINARY_DIR}/pb/${PROTO_LIBRARY_NAME}/generated)
5 years ago
file(MAKE_DIRECTORY ${SCHEMA_OUT_DIR})
string(REGEX REPLACE "\\.proto$" ".pb.h" GEN_PB_HEADER ${PROTO})
string(REGEX REPLACE "\\.proto$" ".pb.cc" GEN_PB ${PROTO})
set(GEN_COMMAND ${Protobuf_PROTOC_EXECUTABLE})
set(GEN_ARGS ${Protobuf_INCLUDE_DIR})
set(OUT_HPP ${SCHEMA_OUT_DIR}/${SCHEMA_REL}/${GEN_PB_HEADER})
set(OUT_CPP ${SCHEMA_OUT_DIR}/${SCHEMA_REL}/${GEN_PB})
set(GENERATION_DIR ${SCHEMA_OUT_DIR}/${SCHEMA_REL})
5 years ago
add_custom_command(
OUTPUT ${OUT_HPP} ${OUT_CPP}
5 years ago
COMMAND ${GEN_COMMAND}
ARGS -I${PROJECT_SOURCE_DIR}/src -I${GEN_ARGS} --cpp_out=${SCHEMA_OUT_DIR} ${PROTO_ABS}
5 years ago
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS protobuf::protoc
VERBATIM
)
5 years ago
set(${PB_H} ${SCHEMA_OUT_DIR}/${SCHEMA_REL}/${GEN_PB_HEADER} PARENT_SCOPE)
set(${PB_CC} ${SCHEMA_OUT_DIR}/${SCHEMA_REL}/${GEN_PB} PARENT_SCOPE)
5 years ago
endfunction()
5 years ago
add_custom_target(generated
COMMENT "Building generated files..."
)
5 years ago
function(add_proto_library NAME)
set(SOURCES "")
foreach (PROTO IN ITEMS ${ARGN})
compile_proto_to_cpp(${NAME} H C ${PROTO})
5 years ago
list(APPEND SOURCES ${H} ${C})
endforeach ()
add_library(${NAME}
${SOURCES}
)
target_link_libraries(${NAME}
protobuf::libprotobuf
)
target_include_directories(${NAME} PUBLIC
# required for common targets
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/pb/${NAME}>
# required for compiling proto targets
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/pb/${NAME}/generated>
5 years ago
)
5 years ago
disable_clang_tidy(${NAME})
libp2p_install(${NAME})
5 years ago
add_dependencies(generated ${NAME})
5 years ago
endfunction()