forked from bharathv/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindProtobuf.cmake
213 lines (186 loc) · 7.71 KB
/
FindProtobuf.cmake
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#########
# Local rewrite of the protobuf support in cmake.
#
# Supports cross-module protobuf dependencies and protobufs inside
# packages much better than the one built into cmake.
#########
#
# Locate and configure the Google Protocol Buffers library.
# Defines the following variables:
#
# PROTOBUF_FOUND - Found the Google Protocol Buffers library
# PROTOBUF_INCLUDE_DIRS - Include directories for Google Protocol Buffers
# PROTOBUF_LIBRARIES - The protobuf library
#
# The following cache variables are also defined:
# PROTOBUF_LIBRARY - The protobuf library
# PROTOBUF_STATIC_LIBRARY - The protobuf library (static link)
# PROTOBUF_PROTOC_LIBRARY - The protoc library
# PROTOBUF_INCLUDE_DIR - The include directory for protocol buffers
# PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler
#
# ====================================================================
# Example:
#
# find_package(Protobuf REQUIRED)
# include_directories(${PROTOBUF_INCLUDE_DIRS})
#
# include_directories(${CMAKE_CURRENT_BINARY_DIR})
# PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS PROTO_TGTS
# [SOURCE_ROOT <root from which source is found>]
# [BINARY_ROOT <root into which binaries are built>]
# PROTO_FILES foo.proto)
# add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
# target_link_libraries(bar ${PROTOBUF_LIBRARY})
#
# NOTE: You may need to link against pthreads, depending
# on the platform.
# ====================================================================
#
# PROTOBUF_GENERATE_CPP (public function)
# SRCS = Variable to define with autogenerated
# source files
# HDRS = Variable to define with autogenerated
# header files
# TGTS = Variable to define with autogenerated
# custom targets; if SRCS/HDRS need to be used in multiple
# libraries, those libraries should depend on these targets
# in order to "serialize" the protoc invocations
# ====================================================================
#=============================================================================
# Copyright 2011 Kirill A. Korinskiy <[email protected]>
# Copyright 2009 Kitware, Inc.
# Copyright 2009 Philip Lowman <[email protected]>
# Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distributed this file outside of CMake, substitute the full
# License text for the above reference.)
function(PROTOBUF_GENERATE_CPP SRCS HDRS TGTS)
if(NOT ARGN)
message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
return()
endif(NOT ARGN)
set(options)
set(one_value_args SOURCE_ROOT BINARY_ROOT)
set(multi_value_args EXTRA_PROTO_PATHS PROTO_FILES)
cmake_parse_arguments(ARG "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
if(ARG_UNPARSED_ARGUMENTS)
message(SEND_ERROR "Error: unrecognized arguments: ${ARG_UNPARSED_ARGUMENTS}")
endif()
set(${SRCS})
set(${HDRS})
set(${TGTS})
set(EXTRA_PROTO_PATH_ARGS)
foreach(PP ${ARG_EXTRA_PROTO_PATHS})
set(EXTRA_PROTO_PATH_ARGS ${EXTRA_PROTO_PATH_ARGS} --proto_path ${PP})
endforeach()
if("${ARG_SOURCE_ROOT}" STREQUAL "")
SET(ARG_SOURCE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
GET_FILENAME_COMPONENT(ARG_SOURCE_ROOT ${ARG_SOURCE_ROOT} ABSOLUTE)
if("${ARG_BINARY_ROOT}" STREQUAL "")
SET(ARG_BINARY_ROOT "${CMAKE_CURRENT_BINARY_DIR}")
endif()
GET_FILENAME_COMPONENT(ARG_BINARY_ROOT ${ARG_BINARY_ROOT} ABSOLUTE)
foreach(FIL ${ARG_PROTO_FILES})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(FIL_WE ${FIL} NAME_WE)
# Ensure that the protobuf file is within the source root.
# This is a requirement of protoc.
FILE(RELATIVE_PATH PROTO_REL_TO_ROOT "${ARG_SOURCE_ROOT}" "${ABS_FIL}")
GET_FILENAME_COMPONENT(REL_DIR "${PROTO_REL_TO_ROOT}" PATH)
if(NOT REL_DIR STREQUAL "")
SET(REL_DIR "${REL_DIR}/")
endif()
set(PROTO_CC_OUT "${ARG_BINARY_ROOT}/${REL_DIR}${FIL_WE}.pb.cc")
set(PROTO_H_OUT "${ARG_BINARY_ROOT}/${REL_DIR}${FIL_WE}.pb.h")
list(APPEND ${SRCS} "${PROTO_CC_OUT}")
list(APPEND ${HDRS} "${PROTO_H_OUT}")
if(NOT EXISTS ${PROTO_DST_ROOT}/${FIL_PT})
file(MAKE_DIRECTORY ${PROTO_DST_ROOT}/${FIL_PT})
endif()
add_custom_command(
OUTPUT "${PROTO_CC_OUT}" "${PROTO_H_OUT}"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS
--plugin $<TARGET_FILE:protoc-gen-insertions>
--cpp_out ${ARG_BINARY_ROOT}
--insertions_out ${ARG_BINARY_ROOT}
--proto_path ${ARG_SOURCE_ROOT}
# Used to find built-in .proto files (e.g. FileDescriptorProto)
--proto_path ${PROTOBUF_INCLUDE_DIR}
${EXTRA_PROTO_PATH_ARGS} ${ABS_FIL}
DEPENDS ${ABS_FIL} protoc-gen-insertions
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
VERBATIM )
# This custom target enforces that there's just one invocation of protoc
# when there are multiple consumers of the generated files. The target name
# must be unique; adding parts of the filename helps ensure this.
set(TGT_NAME ${REL_DIR}${FIL})
string(REPLACE "/" "-" TGT_NAME ${TGT_NAME})
add_custom_target(${TGT_NAME}
DEPENDS "${PROTO_CC_OUT}" "${PROTO_H_OUT}")
list(APPEND ${TGTS} "${TGT_NAME}")
endforeach()
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
set(${TGTS} ${${TGTS}} PARENT_SCOPE)
endfunction()
find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h
${THIRDPARTY_PREFIX}/include
NO_DEFAULT_PATH
)
# Google's provided vcproj files generate libraries with a "lib"
# prefix on Windows
if(WIN32)
set(PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
endif()
find_library(PROTOBUF_LIBRARY NAMES libprotobuf${CMAKE_SHARED_LIBRARY_SUFFIX}
DOC "The Google Protocol Buffers Library"
PATHS ${THIRDPARTY_PREFIX}/lib
NO_DEFAULT_PATH
)
find_file(PROTOBUF_STATIC_LIBRARY libprotobuf.a
DOC "Static version of the Google Protocol Buffers Library"
PATHS ${THIRDPARTY_PREFIX}/lib
NO_DEFAULT_PATH)
find_library(PROTOBUF_PROTOC_LIBRARY NAMES libprotoc${CMAKE_SHARED_LIBRARY_SUFFIX}
DOC "The Google Protocol Buffers Compiler Library"
PATHS ${THIRDPARTY_PREFIX}/lib
NO_DEFAULT_PATH
)
find_library(PROTOBUF_PROTOC_STATIC_LIBRARY NAMES libprotoc.a
DOC "Static version of the Google Protocol Buffers Compiler Library"
PATHS ${THIRDPARTY_PREFIX}/lib
NO_DEFAULT_PATH)
find_program(PROTOBUF_PROTOC_EXECUTABLE NAMES protoc
DOC "The Google Protocol Buffers Compiler"
PATHS ${THIRDPARTY_PREFIX}/bin
NO_DEFAULT_PATH
)
mark_as_advanced(PROTOBUF_INCLUDE_DIR
PROTOBUF_LIBRARY
PROTOBUF_STATIC_LIBRARY
PROTOBUF_PROTOC_LIBRARY
PROTOBUF_PROTOC_STATIC_LIBRARY
PROTOBUF_PROTOC_EXECUTABLE)
# Restore original find library prefixes
if(WIN32)
set(CMAKE_FIND_LIBRARY_PREFIXES "${PROTOBUF_ORIG_FIND_LIBRARY_PREFIXES}")
endif()
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PROTOBUF DEFAULT_MSG
PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
if(PROTOBUF_FOUND)
set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
set(PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARY})
endif()