1set(protobuf_USE_STATIC_LIBS ON) 2if(BUILD_LITE) 3 if(MSVC) 4 set(protobuf_CXXFLAGS "${CMAKE_CXX_FLAGS}") 5 set(protobuf_CFLAGS "${CMAKE_C_FLAGS}") 6 set(protobuf_LDFLAGS "${CMAKE_SHARED_LINKER_FLAGS}") 7 set(_ms_tmp_CMAKE_STATIC_LIBRARY_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX}) 8 set(CMAKE_STATIC_LIBRARY_PREFIX "lib") 9 else() 10 set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-maybe-uninitialized -Wno-unused-parameter \ 11 -fPIC -fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2") 12 if(NOT ENABLE_GLIBCXX) 13 set(protobuf_CXXFLAGS "${protobuf_CXXFLAGS} -D_GLIBCXX_USE_CXX11_ABI=0") 14 endif() 15 set(protobuf_LDFLAGS "-Wl,-z,relro,-z,now,-z,noexecstack") 16 endif() 17else() 18 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 19 set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-uninitialized -Wno-unused-parameter -fPIC \ 20 -fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2") 21 elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows") 22 set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-maybe-uninitialized -Wno-unused-parameter \ 23 -fPIC -fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2") 24 else() 25 set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-maybe-uninitialized -Wno-unused-parameter \ 26 -fPIC -fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2") 27 if(NOT ENABLE_GLIBCXX) 28 set(protobuf_CXXFLAGS "${protobuf_CXXFLAGS} -D_GLIBCXX_USE_CXX11_ABI=0") 29 endif() 30 endif() 31 set(protobuf_LDFLAGS "-Wl,-z,relro,-z,now,-z,noexecstack") 32endif() 33 34set(_ms_tmp_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) 35set(CMAKE_CXX_FLAGS ${_ms_tmp_CMAKE_CXX_FLAGS}) 36string(REPLACE " -Wall" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 37string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 38 39if(ENABLE_GITEE) 40 set(REQ_URL "https://gitee.com/mirrors/protobuf_source/repository/archive/v3.13.0.tar.gz") 41 set(MD5 "140dc2486b9358e3015aec7a8ece525c") 42else() 43 set(REQ_URL "https://github.com/protocolbuffers/protobuf/archive/v3.13.0.tar.gz") 44 set(MD5 "1a6274bc4a65b55a6fa70e264d796490") 45endif() 46 47mindspore_add_pkg(protobuf 48 VER 3.13.0 49 LIBS protobuf 50 EXE protoc 51 URL ${REQ_URL} 52 MD5 ${MD5} 53 CMAKE_PATH cmake/ 54 CMAKE_OPTION -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release) 55 56include_directories(${protobuf_INC}) 57add_library(mindspore::protobuf ALIAS protobuf::protobuf) 58set(CMAKE_CXX_FLAGS ${_ms_tmp_CMAKE_CXX_FLAGS}) 59if(MSVC) 60 set(CMAKE_STATIC_LIBRARY_PREFIX, ${_ms_tmp_CMAKE_STATIC_LIBRARY_PREFIX}) 61endif() 62 63function(common_protobuf_generate path c_var h_var) 64 if(NOT ARGN) 65 message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files") 66 return() 67 endif() 68 69 set(${c_var}) 70 set(${h_var}) 71 72 foreach(file ${ARGN}) 73 get_filename_component(abs_file ${file} ABSOLUTE) 74 get_filename_component(file_name ${file} NAME_WE) 75 get_filename_component(file_dir ${abs_file} PATH) 76 file(RELATIVE_PATH rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${file_dir}) 77 78 list(APPEND ${c_var} "${path}/${file_name}.pb.cc") 79 list(APPEND ${h_var} "${path}/${file_name}.pb.h") 80 add_custom_command( 81 OUTPUT "${path}/${file_name}.pb.cc" "${path}/${file_name}.pb.h" 82 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} 83 COMMAND ${CMAKE_COMMAND} -E make_directory "${path}" 84 COMMAND protobuf::protoc -I${file_dir} --cpp_out=${path} ${abs_file} 85 DEPENDS protobuf::protoc ${abs_file} 86 COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM) 87 endforeach() 88 89 set_source_files_properties(${${c_var}} ${${h_var}} PROPERTIES GENERATED TRUE) 90 set(${c_var} ${${c_var}} PARENT_SCOPE) 91 set(${h_var} ${${h_var}} PARENT_SCOPE) 92endfunction() 93 94function(ms_protobuf_generate c_var h_var) 95 common_protobuf_generate(${CMAKE_BINARY_DIR}/proto ${c_var} ${h_var} ${ARGN}) 96 set(${c_var} ${${c_var}} PARENT_SCOPE) 97 set(${h_var} ${${h_var}} PARENT_SCOPE) 98endfunction() 99 100function(ms_protobuf_generate_py c_var h_var py_var) 101 if(NOT ARGN) 102 message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files") 103 return() 104 endif() 105 106 set(${c_var}) 107 set(${h_var}) 108 set(${py_var}) 109 110 foreach(file ${ARGN}) 111 get_filename_component(abs_file ${file} ABSOLUTE) 112 get_filename_component(file_name ${file} NAME_WE) 113 get_filename_component(file_dir ${abs_file} PATH) 114 115 list(APPEND ${c_var} "${CMAKE_BINARY_DIR}/proto/${file_name}.pb.cc") 116 list(APPEND ${h_var} "${CMAKE_BINARY_DIR}/proto/${file_name}.pb.h") 117 list(APPEND ${py_var} "${CMAKE_BINARY_DIR}/proto/${file_name}_pb2.py") 118 if(WIN32) 119 add_custom_command( 120 OUTPUT "${CMAKE_BINARY_DIR}/proto/${file_name}.pb.cc" 121 "${CMAKE_BINARY_DIR}/proto/${file_name}.pb.h" 122 "${CMAKE_BINARY_DIR}/proto/${file_name}_pb2.py" 123 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} 124 COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/proto" 125 COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/proto ${abs_file} 126 COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/proto ${abs_file} 127 COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/proto ${abs_file} 128 COMMAND perl -pi.bak -e "s/import (.+_pb2.*)/from . import \\1/" 129 "${CMAKE_BINARY_DIR}/proto/${file_name}_pb2.py" 130 COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/proto/${file_name}_pb2.py" 131 "${PROJECT_SOURCE_DIR}/mindspore/train/" 132 DEPENDS protobuf::protoc ${abs_file} 133 COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM) 134 else() 135 add_custom_command( 136 OUTPUT "${CMAKE_BINARY_DIR}/proto/${file_name}.pb.cc" 137 "${CMAKE_BINARY_DIR}/proto/${file_name}.pb.h" 138 "${CMAKE_BINARY_DIR}/proto/${file_name}_pb2.py" 139 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} 140 COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/proto" 141 COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/proto ${abs_file} 142 COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/proto ${abs_file} 143 COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/proto ${abs_file} 144 COMMAND perl -pi -e "s/import (.+_pb2.*)/from . import \\1/" 145 "${CMAKE_BINARY_DIR}/proto/${file_name}_pb2.py" 146 COMMAND cp "${CMAKE_BINARY_DIR}/proto/${file_name}_pb2.py" "${PROJECT_SOURCE_DIR}/mindspore/train/" 147 DEPENDS protobuf::protoc ${abs_file} 148 COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM) 149 endif() 150 endforeach() 151 set_source_files_properties(${${c_var}} ${${h_var}} ${${py_var}} PROPERTIES GENERATED TRUE) 152 set(${c_var} ${${c_var}} PARENT_SCOPE) 153 set(${h_var} ${${h_var}} PARENT_SCOPE) 154 set(${py_var} ${${py_var}} PARENT_SCOPE) 155endfunction() 156