1
# Finds Google Protocol Buffers library and compilers and extends
2
# the standard cmake script with version and python generation support
3
macro(custom_protobuf_find)
4
message(STATUS "Use custom protobuf build.")
5
option(protobuf_BUILD_TESTS "" OFF)
6
option(protobuf_BUILD_EXAMPLES "" OFF)
7
option(protobuf_WITH_ZLIB "" OFF)
8
if(${CAFFE2_LINK_LOCAL_PROTOBUF})
9
# If we are going to link protobuf locally, we will need to turn off
10
# shared libs build for protobuf.
11
option(protobuf_BUILD_SHARED_LIBS "" OFF)
13
# If we are building Caffe2 as shared libs, we will also build protobuf as
15
option(protobuf_BUILD_SHARED_LIBS "" ${BUILD_SHARED_LIBS})
17
# We will make sure that protobuf and caffe2 uses the same msvc runtime.
18
option(protobuf_MSVC_STATIC_RUNTIME "" ${CAFFE2_USE_MSVC_STATIC_RUNTIME})
20
if(${CAFFE2_LINK_LOCAL_PROTOBUF})
21
set(__caffe2_CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ${CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS})
22
set(__caffe2_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
23
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
24
set(BUILD_SHARED_LIBS OFF)
25
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
26
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
28
if(${COMPILER_SUPPORTS_HIDDEN_INLINE_VISIBILITY})
29
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
33
set(__caffe2_CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE})
34
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
38
CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL
39
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL)
40
if(${flag_var} MATCHES "/Z[iI7]")
41
string(REGEX REPLACE "/Z[iI7]" "" ${flag_var} "${${flag_var}}")
46
CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELWITHDEBINFO
47
CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELWITHDEBINFO)
48
if(${flag_var} MATCHES "/Z[iI]")
49
string(REGEX REPLACE "/Z[iI]" "/Z7" ${flag_var} "${${flag_var}}")
52
endif(MSVC_Z7_OVERRIDE)
55
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/protobuf/cmake)
57
set(CMAKE_POSITION_INDEPENDENT_CODE ${__caffe2_CMAKE_POSITION_INDEPENDENT_CODE})
59
if(${CAFFE2_LINK_LOCAL_PROTOBUF})
60
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ${__caffe2_CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS})
61
set(BUILD_SHARED_LIBS ON)
62
set(CMAKE_CXX_FLAGS ${__caffe2_CMAKE_CXX_FLAGS})
65
# Protobuf "namespaced" target is only added post protobuf 3.5.1. As a
66
# result, for older versions, we will manually add alias.
67
if(NOT TARGET protobuf::libprotobuf)
68
add_library(protobuf::libprotobuf ALIAS libprotobuf)
69
add_library(protobuf::libprotobuf-lite ALIAS libprotobuf-lite)
70
# There is link error when cross compiling protoc on mobile:
71
# https://github.com/protocolbuffers/protobuf/issues/2719
72
# And protoc is very unlikely needed for mobile builds.
73
if(NOT (ANDROID OR IOS))
74
add_executable(protobuf::protoc ALIAS protoc)
79
# Main entry for protobuf. If we are building on Android, iOS or we have hard
80
# coded BUILD_CUSTOM_PROTOBUF, we will hard code the use of custom protobuf
83
if(NOT BUILD_CUSTOM_PROTOBUF)
85
"For Android and iOS cross compilation, I am automatically using "
86
"custom protobuf under third party. Note that this behavior may "
87
"change in the future, and you will need to specify "
88
"-DBUILD_CUSTOM_PROTOBUF=ON explicitly.")
90
# There is link error when cross compiling protoc on mobile:
91
# https://github.com/protocolbuffers/protobuf/issues/2719
92
# And protoc is very unlikely needed for mobile builds.
93
set(__caffe2_protobuf_BUILD_PROTOC_BINARIES ${protobuf_BUILD_PROTOC_BINARIES})
94
set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE BOOL "" FORCE)
95
custom_protobuf_find()
96
set(protobuf_BUILD_PROTOC_BINARIES ${__caffe2_protobuf_BUILD_PROTOC_BINARIES} CACHE BOOL "" FORCE)
97
elseif(BUILD_CUSTOM_PROTOBUF)
98
message(STATUS "Building using own protobuf under third_party per request.")
99
custom_protobuf_find()
101
include(cmake/public/protobuf.cmake)
104
if((NOT TARGET protobuf::libprotobuf) AND (NOT TARGET protobuf::libprotobuf-lite))
106
"Protobuf cannot be found. Caffe2 will automatically switch to use "
107
"own protobuf under third_party. Note that this behavior may change in "
108
"the future, and you will need to specify -DBUILD_CUSTOM_PROTOBUF=ON "
110
custom_protobuf_find()
112
# TODO(jiayq): enable this in the future, when Jenkins Mac support is
113
# properly set up with protobuf installs.
115
# message(FATAL_ERROR
116
# "Protobuf cannot be found. Caffe2 will have to build with libprotobuf. "
117
# "Please set the proper paths so that I can find protobuf correctly.")
120
get_target_property(__tmp protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES)
121
message(STATUS "Caffe2 protobuf include directory: " ${__tmp})
122
include_directories(BEFORE SYSTEM ${__tmp})
124
# If Protobuf_VERSION is known (true in most cases, false if we are building
125
# local protobuf), then we will add a protobuf version check in
126
# Caffe2Config.cmake.in.
127
if(DEFINED ${Protobuf_VERSION})
128
set(CAFFE2_KNOWN_PROTOBUF_VERSION TRUE)
130
set(CAFFE2_KNOWN_PROTOBUF_VERSION FALSE)
131
set(Protobuf_VERSION "Protobuf_VERSION_NOTFOUND")
135
# Figure out which protoc to use.
136
# If CAFFE2_CUSTOM_PROTOC_EXECUTABLE is set, we assume the user knows
137
# what they're doing and we blindly use the specified protoc. This
138
# is typically the case when cross-compiling where protoc must be
139
# compiled for the host architecture and libprotobuf must be
140
# compiled for the target architecture.
141
# If CAFFE2_CUSTOM_PROTOC_EXECUTABLE is NOT set, we use the protoc
142
# target that is built as part of including the protobuf project.
143
if(EXISTS "${CAFFE2_CUSTOM_PROTOC_EXECUTABLE}")
144
set(CAFFE2_PROTOC_EXECUTABLE ${CAFFE2_CUSTOM_PROTOC_EXECUTABLE})
146
set(CAFFE2_PROTOC_EXECUTABLE protobuf::protoc)
149
################################################################################################
150
# Modification of standard 'protobuf_generate_cpp()' with output dir parameter and python support
152
# caffe2_protobuf_generate_cpp_py(<srcs_var> <hdrs_var> <python_var> <proto_files>)
153
function(caffe2_protobuf_generate_cpp_py srcs_var hdrs_var python_var)
155
message(SEND_ERROR "Error: caffe_protobuf_generate_cpp_py() called without any proto files")
163
get_filename_component(abs_fil ${fil} ABSOLUTE)
164
get_filename_component(fil_we ${fil} NAME_WE)
166
list(APPEND ${srcs_var} "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.cc")
167
list(APPEND ${hdrs_var} "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.h")
168
list(APPEND ${python_var} "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}_pb2.py")
170
# Add TORCH_API prefix to protobuf classes and methods in all cases
171
set(DLLEXPORT_STR "dllexport_decl=TORCH_API:")
173
# Note: the following depends on PROTOBUF_PROTOC_EXECUTABLE. This
174
# is done to make sure protoc is built before attempting to
175
# generate sources if we're using protoc from the third_party
176
# directory and are building it as part of the Caffe2 build. If
177
# points to an existing path, it is a no-op.
179
if(${CAFFE2_LINK_LOCAL_PROTOBUF})
180
# We need to rewrite the pb.h files to route GetEmptyStringAlreadyInited
181
# through our wrapper in proto_utils so the memory location test
184
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.cc"
185
"${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.h"
186
"${CMAKE_CURRENT_BINARY_DIR}/${fil_we}_pb2.py"
187
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
188
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}"
189
COMMAND ${CAFFE2_PROTOC_EXECUTABLE} -I${PROJECT_SOURCE_DIR} --cpp_out=${DLLEXPORT_STR}${PROJECT_BINARY_DIR} ${abs_fil}
190
COMMAND ${CAFFE2_PROTOC_EXECUTABLE} -I${PROJECT_SOURCE_DIR} --python_out "${PROJECT_BINARY_DIR}" ${abs_fil}
192
# If we remove all reference to these pb.h files from external
193
# libraries and binaries this rewrite can be removed.
194
COMMAND ${CMAKE_COMMAND} -DFILENAME=${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.h -DNAMESPACES=caffe\;caffe2\;onnx\;torch -P ${PROJECT_SOURCE_DIR}/cmake/ProtoBufPatch.cmake
196
DEPENDS ${CAFFE2_PROTOC_EXECUTABLE} ${abs_fil}
197
COMMENT "Running C++/Python protocol buffer compiler on ${fil}" VERBATIM )
200
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.cc"
201
"${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.h"
202
"${CMAKE_CURRENT_BINARY_DIR}/${fil_we}_pb2.py"
203
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
204
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}"
205
COMMAND ${CAFFE2_PROTOC_EXECUTABLE} -I${PROJECT_SOURCE_DIR} --cpp_out=${DLLEXPORT_STR}${PROJECT_BINARY_DIR} ${abs_fil}
206
COMMAND ${CAFFE2_PROTOC_EXECUTABLE} -I${PROJECT_SOURCE_DIR} --python_out "${PROJECT_BINARY_DIR}" ${abs_fil}
207
COMMAND ${CMAKE_COMMAND} -DFILENAME=${CMAKE_CURRENT_BINARY_DIR}/${fil_we}.pb.h -DNAMESPACES=caffe\;caffe2\;onnx\;torch -DSYSTEM_PROTOBUF=YES -P ${PROJECT_SOURCE_DIR}/cmake/ProtoBufPatch.cmake
208
DEPENDS ${CAFFE2_PROTOC_EXECUTABLE} ${abs_fil}
209
COMMENT "Running C++/Python protocol buffer compiler on ${fil}" VERBATIM )
213
set_source_files_properties(${${srcs_var}} ${${hdrs_var}} ${${python_var}} PROPERTIES GENERATED TRUE)
214
set(${srcs_var} ${${srcs_var}} PARENT_SCOPE)
215
set(${hdrs_var} ${${hdrs_var}} PARENT_SCOPE)
216
set(${python_var} ${${python_var}} PARENT_SCOPE)