forked from JeffersonLab/ersap-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
198 lines (156 loc) · 6.38 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# cmake build file for C++ helloworld example.
# Assumes protobuf and gRPC have been installed using cmake.
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
# that automatically builds all the dependencies before building helloworld.
cmake_minimum_required(VERSION 3.5.1)
project(loadbalancer C CXX)
if (NOT DEFINED CMAKE_CXX_STANDARD)
set (CMAKE_CXX_STANDARD 14)
endif()
set(CMAKE_DEBUG_POSTFIX -dbg)
# uncomment to put in debug symbols or do on command line with:
#set(CMAKE_BUILD_TYPE Debug)
#add_compile_options(-Wall)
add_compile_options(-O3)
# Set architecture-related string for future installations of differing architectures
# set(ARCH ${CMAKE_HOST_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR})
# message(STATUS "ARCH = " ${ARCH})
find_package(Threads REQUIRED)
# This branch assumes that gRPC and all its dependencies are already installed
# on this system, so they can be located by find_package().
# Find Protobuf installation
# Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
message(STATUS "_PROTOBUF_PROTOC = ${_PROTOBUF_PROTOC}")
# Find gRPC installation
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC REQUIRED CONFIG)
if (gRPC_FOUND)
message(STATUS "gRPC package IS found")
else()
# Grpc cannot be found, let's try again by usin env var
if (NOT DEFINED ENV{GRPC_INSTALL_DIR})
if (NOT DEFINED ENV{EJFAT_ERSAP_INSTALL_DIR})
message(FATAL_ERROR "Define GRPC_INSTALL_DIR or EJFAT_ERSAP_INSTALL_DIR so gRPC package can be found")
else()
set(GRPC_INSTALL_DIR $ENV{EJFAT_ERSAP_INSTALL_DIR})
endif()
else()
set(GRPC_INSTALL_DIR $ENV{GRPC_INSTALL_DIR})
endif()
find_package(gRPC REQUIRED CONFIG PATHS ${GRPC_INSTALL_DR}/lib/cmake/grpc)
endif()
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_REFLECTION gRPC::grpc++_reflection)
set(_GRPC_GRPCPP gRPC::grpc++)
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
# Installation directory may be defined on command line with -DINSTALL_DIR=<...>
set(INSTALL_DIR_DEFINED 1)
if (NOT DEFINED INSTALL_DIR)
if (NOT DEFINED ENV{EJFAT_ERSAP_INSTALL_DIR})
message(STATUS "Use either -DINSTALL_DIR=<dir> or define EJFAT_ERSAP_INSTALL_DIR for installation")
set(INSTALL_DIR_DEFINED 0)
else()
set(INSTALL_DIR $ENV{EJFAT_ERSAP_INSTALL_DIR})
endif()
endif()
if (INSTALL_DIR_DEFINED)
message(STATUS "Installing into " ${INSTALL_DIR})
set(CMAKE_INSTALL_PREFIX ${INSTALL_DIR})
message(STATUS "CMAKE_INSTALL_PREFIX reset to " ${CMAKE_INSTALL_PREFIX})
else()
message(STATUS "No installation directory defined")
endif()
# Proto file
get_filename_component(hw_proto "loadbalancer.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)
# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/loadbalancer.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/loadbalancer.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/loadbalancer.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/loadbalancer.grpc.pb.h")
add_custom_command(
OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
COMMAND ${_PROTOBUF_PROTOC}
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
-I "${hw_proto_path}"
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${hw_proto}"
DEPENDS "${hw_proto}"
)
set(HEADER_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/ersap_grpc_packetize.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ersap_grpc_assemble.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/lb_cplane.h"
${hw_proto_hdrs} ${hw_grpc_hdrs}
)
# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
add_library(ejfat_grpc SHARED
lb_cplane.cc
${hw_grpc_srcs}
${hw_grpc_hdrs}
${hw_proto_srcs}
${hw_proto_hdrs}
)
target_link_libraries(ejfat_grpc
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF}
)
set(GRPC_EXEC_FILES
cp_tester.cc
simSender.cc
# cp_server.cc
)
# Executables
foreach(fileName ${GRPC_EXEC_FILES})
# Get file name with no directory or extension as executable name
get_filename_component(execName ${fileName} NAME_WE)
# Create executable from file
message(STATUS "Create grpc-related executable " ${execName})
add_executable(${execName} ${fileName})
# Put debug extension on if applicable
set_target_properties(${execName} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
# Location of include files
target_include_directories(${execName} PUBLIC ${CMAKE_SOURCE_DIR} ${GRPC_INCLUDE_DIRS})
# Needs these libs
target_link_libraries(${execName} PUBLIC pthread ejfat_grpc)
# Only install if installation directory has been defined
if (INSTALL_DIR_DEFINED)
# Install into bin dir
install(TARGETS ${execName} RUNTIME DESTINATION bin)
endif()
endforeach(fileName)
# Only install if installation directory has been defined.
# CMAKE_INSTALL_PREFIX will be prepended to paths
if (INSTALL_DIR_DEFINED)
install(TARGETS ejfat_grpc LIBRARY DESTINATION lib)
install(FILES ${HEADER_FILES} DESTINATION include)
endif()
# Uninstall target
if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()