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