Skip to content

Commit b567af6

Browse files
Better cmake
1 parent 9388e35 commit b567af6

5 files changed

Lines changed: 212 additions & 126 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,14 @@
1-
cmake_minimum_required( VERSION 3.12 )
1+
cmake_minimum_required( VERSION 3.12...3.16 )
22

3-
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
4-
set( USING_VCPKG "ON" )
5-
set( CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "" )
6-
message( "Using vcpkg settings" )
7-
endif()
8-
9-
project( consulcpp VERSION 0.0.1 DESCRIPTION "A C++ library that implements the Consul API" LANGUAGES CXX )
3+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
104

11-
if( NOT DEFINED USING_VCPKG AND EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
12-
message( "Using conan settings" )
13-
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
14-
conan_basic_setup()
15-
endif()
5+
include( Common )
6+
include( Tools )
167

17-
set( CMAKE_CXX_STANDARD 17 )
18-
if(APPLE)
19-
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.14)
20-
endif()
8+
project( consulcpp VERSION 0.1.1 DESCRIPTION "A C++ library that implements the Consul API" LANGUAGES CXX )
219

2210
option( BUILD_EXAMPLES "Build examples" ON )
2311

24-
find_program( CLANG_TIDY NAMES "clang-tidy" )
25-
if( CLANG_TIDY )
26-
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY}" -checks=readability-*,cppcoreguidelines-*)
27-
endif()
28-
29-
function(PROTOBUF_GENERATE_GRPC_CPP SRCS HDRS)
30-
if(NOT ARGN)
31-
message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files")
32-
return()
33-
endif()
34-
35-
if(PROTOBUF_GENERATE_CPP_APPEND_PATH) # This variable is common for all types of output.
36-
# Create an include path for each file specified
37-
foreach(FIL ${ARGN})
38-
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
39-
get_filename_component(ABS_PATH ${ABS_FIL} PATH)
40-
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
41-
if(${_contains_already} EQUAL -1)
42-
list(APPEND _protobuf_include_path -I ${ABS_PATH})
43-
endif()
44-
endforeach()
45-
else()
46-
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
47-
endif()
48-
49-
if(DEFINED PROTOBUF_IMPORT_DIRS)
50-
foreach(DIR ${Protobuf_IMPORT_DIRS})
51-
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
52-
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
53-
if(${_contains_already} EQUAL -1)
54-
list(APPEND _protobuf_include_path -I ${ABS_PATH})
55-
endif()
56-
endforeach()
57-
endif()
58-
59-
set(${SRCS})
60-
set(${HDRS})
61-
foreach(FIL ${ARGN})
62-
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
63-
get_filename_component(FIL_WE ${FIL} NAME_WE)
64-
65-
list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc")
66-
list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h")
67-
68-
add_custom_command(
69-
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc"
70-
"${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h"
71-
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
72-
ARGS --grpc_out=${CMAKE_CURRENT_BINARY_DIR}
73-
--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}
74-
-I${PROTOBUF_IMPORT_DIRS}
75-
${_protobuf_include_path} ${ABS_FIL}
76-
DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
77-
COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}"
78-
VERBATIM)
79-
endforeach()
80-
81-
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
82-
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
83-
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
84-
endfunction()
85-
8612
add_subdirectory( src )
8713
if( BUILD_EXAMPLES )
8814
add_subdirectory( examples/leader )

cmake/Common.cmake

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
cmake_minimum_required( VERSION 3.12...3.16 )
2+
3+
if( CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|Release)$")
4+
message( FATAL_ERROR "Invalid value for CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}. Use Debug or Release." )
5+
endif()
6+
7+
if( EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake" )
8+
message( "Using conan settings: ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake" )
9+
include( ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake )
10+
conan_basic_setup()
11+
elseif( DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE )
12+
message( "Using vcpkg settings: $ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" )
13+
set( CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "" )
14+
endif()
15+
16+
set( CMAKE_CXX_STANDARD 17 )
17+
if(APPLE)
18+
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.14)
19+
endif()
20+
21+
add_compile_definitions( _SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING )
22+
add_compile_definitions( QT_DEPRECATED_WARNINGS QT_DISABLE_DEPRECATED_BEFORE=0x060000 )
23+
24+
if( WIN32 )
25+
add_compile_definitions( _WIN32_WINNT=0x0A00 )
26+
add_compile_definitions( UNICODE )
27+
add_compile_definitions( _UNICODE )
28+
endif()
29+
30+
function(PROTOBUF_GENERATE_GRPC_CPP SRCS HDRS)
31+
if(NOT ARGN)
32+
message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files")
33+
return()
34+
endif()
35+
36+
if(PROTOBUF_GENERATE_CPP_APPEND_PATH) # This variable is common for all types of output.
37+
# Create an include path for each file specified
38+
foreach(FIL ${ARGN})
39+
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
40+
get_filename_component(ABS_PATH ${ABS_FIL} PATH)
41+
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
42+
if(${_contains_already} EQUAL -1)
43+
list(APPEND _protobuf_include_path -I ${ABS_PATH})
44+
endif()
45+
endforeach()
46+
else()
47+
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
48+
endif()
49+
50+
if(DEFINED PROTOBUF_IMPORT_DIRS)
51+
foreach(DIR ${Protobuf_IMPORT_DIRS})
52+
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
53+
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
54+
if(${_contains_already} EQUAL -1)
55+
list(APPEND _protobuf_include_path -I ${ABS_PATH})
56+
endif()
57+
endforeach()
58+
endif()
59+
60+
set(${SRCS})
61+
set(${HDRS})
62+
foreach(FIL ${ARGN})
63+
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
64+
get_filename_component(FIL_WE ${FIL} NAME_WE)
65+
66+
list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc")
67+
list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h")
68+
69+
add_custom_command(
70+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc" "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h"
71+
COMMAND ${Protobuf_PROTOC_EXECUTABLE}
72+
ARGS --grpc_out=${CMAKE_CURRENT_BINARY_DIR} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} -I${PROTOBUF_IMPORT_DIRS} ${_protobuf_include_path} ${ABS_FIL}
73+
DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
74+
COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}"
75+
VERBATIM
76+
)
77+
endforeach()
78+
79+
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
80+
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
81+
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
82+
endfunction()

cmake/Tools.cmake

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright Tomas Zeman 2019.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
# https://raw.githubusercontent.com/zemasoft/clangformat-cmake/master/cmake/ClangFormat.cmake
6+
7+
function(clang_format_setup)
8+
if(NOT CLANGFORMAT_EXECUTABLE)
9+
set(CLANGFORMAT_EXECUTABLE clang-format)
10+
endif()
11+
12+
if(NOT EXISTS ${CLANGFORMAT_EXECUTABLE})
13+
if( WIN32 )
14+
find_program(clangformat_executable_tmp ${CLANGFORMAT_EXECUTABLE} HINTS $ENV{VCINSTALLDIR}/Tools/Llvm/bin )
15+
else()
16+
find_program(clangformat_executable_tmp ${CLANGFORMAT_EXECUTABLE})
17+
endif()
18+
if(clangformat_executable_tmp)
19+
set(CLANGFORMAT_EXECUTABLE ${clangformat_executable_tmp})
20+
unset(clangformat_executable_tmp)
21+
endif()
22+
endif()
23+
24+
if( EXISTS ${CLANGFORMAT_EXECUTABLE} )
25+
foreach(clangformat_source ${ARGV})
26+
get_filename_component(clangformat_source ${clangformat_source} ABSOLUTE)
27+
list(APPEND clangformat_sources ${clangformat_source})
28+
endforeach()
29+
30+
add_custom_target(${PROJECT_NAME}_clangformat
31+
COMMAND
32+
${CLANGFORMAT_EXECUTABLE}
33+
-style=file
34+
-i
35+
${clangformat_sources}
36+
COMMENT
37+
"Formating with ${CLANGFORMAT_EXECUTABLE} ..."
38+
)
39+
40+
if(TARGET clangformat)
41+
add_dependencies(clangformat ${PROJECT_NAME}_clangformat)
42+
else()
43+
add_custom_target(clangformat DEPENDS ${PROJECT_NAME}_clangformat)
44+
endif()
45+
endif()
46+
endfunction()
47+
48+
function(target_clang_format_setup target)
49+
get_target_property(target_sources ${target} SOURCES)
50+
clang_format_setup(${target_sources})
51+
endfunction()
52+
53+
###
54+
55+
# Enable clang-tidy either setting the app or using the option
56+
option( ENABLE_CLANG_TIDY "Enable clang-tidy building." OFF )
57+
if( ENABLE_CLANG_TIDY OR CLANG_TIDY )
58+
if( NOT CLANG_TIDY )
59+
if( WIN32 )
60+
find_program( CLANG_TIDY NAMES "clang-tidy" HINTS $ENV{VCINSTALLDIR}/Tools/Llvm/bin )
61+
else()
62+
find_program( CLANG_TIDY NAMES "clang-tidy" )
63+
endif()
64+
endif()
65+
if( CLANG_TIDY )
66+
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY}" -checks=readability-*,cppcoreguidelines-*)
67+
endif()
68+
endif()
69+
70+
option( ENABLE_CPP_CHECK "Enable cppcheck building." OFF )
71+
if( ENABLE_CPP_CHECK OR CMAKE_CXX_CPPCHECK )
72+
if( NOT CMAKE_CXX_CPPCHECK )
73+
if( WIN32 )
74+
find_program( CMAKE_CXX_CPPCHECK NAMES "cppcheck" HINTS $ENV{ProgramFiles}/Cppcheck )
75+
else()
76+
find_program( CMAKE_CXX_CPPCHECK NAMES "cppcheck" )
77+
endif()
78+
endif()
79+
if( CMAKE_CXX_CPPCHECK )
80+
list(
81+
APPEND CMAKE_CXX_CPPCHECK
82+
"--std=c++17"
83+
"--enable=all"
84+
"--inconclusive"
85+
"--force"
86+
"--inline-suppr"
87+
"--suppressions-list=${CMAKE_SOURCE_DIR}/CppCheckSuppressions.txt"
88+
)
89+
endif()
90+
endif()

examples/leader/CMakeLists.txt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
cmake_minimum_required( VERSION 3.12 )
2-
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
3-
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
4-
endif()
1+
cmake_minimum_required( VERSION 3.12...3.16 )
52

63
project( leader )
74

85
find_package(Protobuf REQUIRED)
9-
10-
set(PROTOBUF_IMPORT_DIRS ${Protobuf_INCLUDE_DIRS})
11-
include_directories(${Protobuf_INCLUDE_DIRS})
12-
include_directories(${CMAKE_CURRENT_BINARY_DIR})
136
find_package(gRPC REQUIRED)
147
find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin)
158

16-
file( GLOB ProtoFiles health.proto )
9+
set(PROTOBUF_IMPORT_DIRS ${Protobuf_INCLUDE_DIRS})
10+
11+
set( ProtoFiles health.proto )
1712
protobuf_generate_cpp(ProtoSources ProtoHeaders ${ProtoFiles})
1813
PROTOBUF_GENERATE_GRPC_CPP(ProtoGRPCSources ProtoGRPCHeaders ${ProtoFiles})
1914

15+
set( SOURCES
16+
main.cpp
17+
)
2018
add_executable( ${PROJECT_NAME}
21-
main.cpp
19+
${SOURCES}
2220
${ProtoSources}
2321
${ProtoGRPCSources}
2422
)
@@ -31,33 +29,31 @@ set_target_properties( ${PROJECT_NAME}
3129
if( BUILD_SHARED_LIBS )
3230
add_compile_definitions( ConsulCPP_DLL )
3331
endif()
34-
add_compile_definitions( _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING )
35-
36-
set_property( TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17 )
37-
38-
include_directories( . ../../include )
3932

4033
target_link_libraries( ${PROJECT_NAME}
4134
consulcpp
4235
protobuf::libprotobuf
4336
gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc_cronet
4437
)
4538

46-
if( WIN32 )
47-
add_compile_definitions( _WIN32_WINNT=0x0A00 )
48-
endif()
49-
5039
if( UNIX )
5140
find_package( Threads REQUIRED )
5241
target_link_libraries( ${PROJECT_NAME}
5342
Threads::Threads
5443
)
5544
endif()
5645

46+
target_include_directories( ${PROJECT_NAME}
47+
PUBLIC
48+
../../include
49+
${Protobuf_INCLUDE_DIRS}
50+
${CMAKE_CURRENT_BINARY_DIR}
51+
)
52+
53+
clang_format_setup( ${SOURCES} )
54+
5755
include(GNUInstallDirs)
5856

5957
install(TARGETS ${PROJECT_NAME}
6058
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
6159
)
62-
63-
##

0 commit comments

Comments
 (0)