mindspore/cmake/external_libs/protobuf.cmake

163 lines
7.5 KiB
CMake
Raw Normal View History

2020-04-02 10:14:37 +08:00
set(protobuf_USE_STATIC_LIBS ON)
2021-01-23 20:20:31 +08:00
if(BUILD_LITE)
2021-08-29 17:13:22 +08:00
if(MSVC)
set(protobuf_CXXFLAGS "${CMAKE_CXX_FLAGS}")
set(protobuf_CFLAGS "${CMAKE_C_FLAGS}")
set(protobuf_LDFLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
set(_ms_tmp_CMAKE_STATIC_LIBRARY_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
else()
set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-maybe-uninitialized -Wno-unused-parameter \
-fPIC -fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2")
2021-09-04 17:21:20 +08:00
if(NOT ENABLE_GLIBCXX)
2021-08-29 17:13:22 +08:00
set(protobuf_CXXFLAGS "${protobuf_CXXFLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
endif()
set(protobuf_LDFLAGS "-Wl,-z,relro,-z,now,-z,noexecstack")
2021-08-17 16:17:58 +08:00
endif()
2021-01-23 20:20:31 +08:00
else()
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-uninitialized -Wno-unused-parameter -fPIC \
2021-03-20 19:56:45 +08:00
-fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2")
2021-01-23 20:20:31 +08:00
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
2021-03-20 19:56:45 +08:00
set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-maybe-uninitialized -Wno-unused-parameter \
-fPIC -fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2")
else()
2021-03-20 19:56:45 +08:00
set(protobuf_CXXFLAGS "-fstack-protector-all -Wno-maybe-uninitialized -Wno-unused-parameter \
-fPIC -fvisibility=hidden -D_FORTIFY_SOURCE=2 -O2")
if(NOT ENABLE_GLIBCXX)
set(protobuf_CXXFLAGS "${protobuf_CXXFLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
endif()
endif()
2021-08-29 17:13:22 +08:00
set(protobuf_LDFLAGS "-Wl,-z,relro,-z,now,-z,noexecstack")
2021-01-23 20:20:31 +08:00
endif()
set(_ms_tmp_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
2020-04-02 10:14:37 +08:00
set(CMAKE_CXX_FLAGS ${_ms_tmp_CMAKE_CXX_FLAGS})
string(REPLACE " -Wall" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
2021-01-23 20:20:31 +08:00
if(ENABLE_GITEE)
2021-06-26 17:54:44 +08:00
set(REQ_URL "https://gitee.com/mirrors/protobuf_source/repository/archive/v3.13.0.tar.gz")
2021-11-15 21:12:45 +08:00
set(MD5 "53ab10736257b3c61749de9800b8ce97")
else()
2021-06-26 17:54:44 +08:00
set(REQ_URL "https://github.com/protocolbuffers/protobuf/archive/v3.13.0.tar.gz")
set(MD5 "1a6274bc4a65b55a6fa70e264d796490")
2021-01-23 20:20:31 +08:00
endif()
2022-03-21 10:03:38 +08:00
if(BUILD_LITE)
set(PROTOBUF_PATCH_ROOT ${TOP_DIR}/third_party/patch/protobuf)
else()
set(PROTOBUF_PATCH_ROOT ${CMAKE_SOURCE_DIR}/third_party/patch/protobuf)
endif()
2020-04-02 10:14:37 +08:00
mindspore_add_pkg(protobuf
2021-06-26 17:54:44 +08:00
VER 3.13.0
2020-04-02 10:14:37 +08:00
LIBS protobuf
EXE protoc
URL ${REQ_URL}
MD5 ${MD5}
2020-04-02 10:14:37 +08:00
CMAKE_PATH cmake/
2022-03-21 10:03:38 +08:00
CMAKE_OPTION -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release
PATCHES ${PROTOBUF_PATCH_ROOT}/CVE-2021-22570.patch)
2020-04-02 10:14:37 +08:00
include_directories(${protobuf_INC})
2022-06-22 09:31:33 +08:00
include_directories(${CMAKE_BINARY_DIR}/proto_py)
2020-04-02 10:14:37 +08:00
add_library(mindspore::protobuf ALIAS protobuf::protobuf)
set(CMAKE_CXX_FLAGS ${_ms_tmp_CMAKE_CXX_FLAGS})
2021-08-29 17:13:22 +08:00
if(MSVC)
set(CMAKE_STATIC_LIBRARY_PREFIX, ${_ms_tmp_CMAKE_STATIC_LIBRARY_PREFIX})
endif()
function(common_protobuf_generate path c_var h_var)
if(NOT ARGN)
message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files")
return()
endif()
set(${c_var})
set(${h_var})
foreach(file ${ARGN})
get_filename_component(abs_file ${file} ABSOLUTE)
get_filename_component(file_name ${file} NAME_WE)
get_filename_component(file_dir ${abs_file} PATH)
file(RELATIVE_PATH rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${file_dir})
list(APPEND ${c_var} "${path}/${file_name}.pb.cc")
list(APPEND ${h_var} "${path}/${file_name}.pb.h")
add_custom_command(
OUTPUT "${path}/${file_name}.pb.cc" "${path}/${file_name}.pb.h"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory "${path}"
COMMAND protobuf::protoc -I${file_dir} --cpp_out=${path} ${abs_file}
DEPENDS protobuf::protoc ${abs_file}
2020-04-26 20:51:18 +08:00
COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM)
endforeach()
set_source_files_properties(${${c_var}} ${${h_var}} PROPERTIES GENERATED TRUE)
set(${c_var} ${${c_var}} PARENT_SCOPE)
set(${h_var} ${${h_var}} PARENT_SCOPE)
endfunction()
function(ms_protobuf_generate c_var h_var)
2022-03-14 19:40:21 +08:00
common_protobuf_generate(${CMAKE_BINARY_DIR}/proto ${c_var} ${h_var} ${ARGN})
set(${c_var} ${${c_var}} PARENT_SCOPE)
set(${h_var} ${${h_var}} PARENT_SCOPE)
endfunction()
function(ms_protobuf_generate_py c_var h_var py_var)
if(NOT ARGN)
message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files")
return()
endif()
set(${c_var})
set(${h_var})
set(${py_var})
foreach(file ${ARGN})
get_filename_component(abs_file ${file} ABSOLUTE)
get_filename_component(file_name ${file} NAME_WE)
get_filename_component(file_dir ${abs_file} PATH)
2022-06-22 09:31:33 +08:00
list(APPEND ${c_var} "${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}.pb.cc")
list(APPEND ${h_var} "${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}.pb.h")
list(APPEND ${py_var} "${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}_pb2.py")
2021-01-23 20:20:31 +08:00
if(WIN32)
2020-04-15 17:36:48 +08:00
add_custom_command(
2022-06-22 09:31:33 +08:00
OUTPUT "${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}.pb.cc"
"${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}.pb.h"
"${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}_pb2.py"
2020-04-15 17:36:48 +08:00
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
2022-06-22 09:31:33 +08:00
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/proto_py/proto"
COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/proto_py/proto ${abs_file}
COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/proto_py/proto ${abs_file}
COMMAND perl -pi.bak -e "s/import (.+_pb2.*)/from . import \\1/"
2022-06-22 09:31:33 +08:00
"${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}_pb2.py"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}_pb2.py"
2021-12-15 10:33:16 +08:00
"${PROJECT_SOURCE_DIR}/mindspore/python/mindspore/train/"
2020-04-15 17:36:48 +08:00
DEPENDS protobuf::protoc ${abs_file}
2021-01-23 20:20:31 +08:00
COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM)
2020-04-15 17:36:48 +08:00
else()
add_custom_command(
2022-06-22 09:31:33 +08:00
OUTPUT "${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}.pb.cc"
"${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}.pb.h"
"${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}_pb2.py"
2020-04-15 17:36:48 +08:00
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
2022-06-22 09:31:33 +08:00
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/proto_py/proto"
COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/proto_py/proto ${abs_file}
COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/proto_py/proto ${abs_file}
COMMAND perl -pi -e "s/import (.+_pb2.*)/from . import \\1/"
2022-06-22 09:31:33 +08:00
"${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}_pb2.py"
COMMAND cp "${CMAKE_BINARY_DIR}/proto_py/proto/${file_name}_pb2.py"
2021-12-15 10:33:16 +08:00
"${PROJECT_SOURCE_DIR}/mindspore/python/mindspore/train/"
2020-04-15 17:36:48 +08:00
DEPENDS protobuf::protoc ${abs_file}
2020-04-26 20:51:18 +08:00
COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM)
2020-04-15 17:36:48 +08:00
endif()
endforeach()
set_source_files_properties(${${c_var}} ${${h_var}} ${${py_var}} PROPERTIES GENERATED TRUE)
set(${c_var} ${${c_var}} PARENT_SCOPE)
set(${h_var} ${${h_var}} PARENT_SCOPE)
set(${py_var} ${${py_var}} PARENT_SCOPE)
endfunction()