From fdfcffeee11d385404a36fc894604bdc39db3538 Mon Sep 17 00:00:00 2001 From: wqtshg Date: Fri, 6 Nov 2020 11:59:34 +0800 Subject: [PATCH] protobuf test --- cmake/external_libs/protobuf.cmake | 110 +++++++++++++++++++------------------ src/common/graph/CMakeLists.txt | 4 +- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/cmake/external_libs/protobuf.cmake b/cmake/external_libs/protobuf.cmake index 8be594c7..08f54d87 100644 --- a/cmake/external_libs/protobuf.cmake +++ b/cmake/external_libs/protobuf.cmake @@ -1,10 +1,15 @@ -if (NOT TARGET protobuf::protobuf) -set(protobuf_USE_STATIC_LIBS ON) -set(protobuf_CXXFLAGS "-Wno-maybe-uninitialized -Wno-unused-parameter -fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2") -set(protobuf_LDFLAGS "-Wl,-z,relro,-z,now,-z,noexecstack") -set(_ge_tmp_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) -string(REPLACE " -Wall" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") -string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +if (HAVE_PROTOBUF) + return() +endif() + +include(ExternalProject) +include(GNUInstallDirs) + +if ((${CMAKE_INSTALL_PREFIX} STREQUAL /usr/local) OR + (${CMAKE_INSTALL_PREFIX} STREQUAL "C:/Program Files (x86)/ascend")) + set(CMAKE_INSTALL_PREFIX ${GE_CODE_DIR}/output CACHE STRING "path for install()" FORCE) + message(STATUS "No install prefix selected, default to ${CMAKE_INSTALL_PREFIX}.") +endif() if (ENABLE_GITEE) set(REQ_URL "https://gitee.com/mirrors/protobuf_source/repository/archive/v3.8.0.tar.gz") @@ -14,50 +19,47 @@ else() set(MD5 "3d9e32700639618a4d2d342c99d4507a") endif () -graphengine_add_pkg(protobuf - VER 3.8.0 - LIBS protobuf - EXE protoc - URL ${REQ_URL} - MD5 ${MD5} - CMAKE_PATH ../cmake/ - CMAKE_OPTION -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF) -set(CMAKE_CXX_FLAGS ${_ge_tmp_CMAKE_CXX_FLAGS}) -endif() -add_library(graphengine::protobuf ALIAS protobuf::protobuf) -set(PROTOBUF_LIBRARY protobuf::protobuf) -include_directories(${protobuf_INC}) -include_directories(${protobuf_DIRPATH}/src) - -function(ge_protobuf_generate comp c_var h_var) - if(NOT ARGN) - message(SEND_ERROR "Error: ge_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) - - list(APPEND ${c_var} "${CMAKE_BINARY_DIR}/proto/${comp}/proto/${file_name}.pb.cc") - list(APPEND ${h_var} "${CMAKE_BINARY_DIR}/proto/${comp}/proto/${file_name}.pb.h") - - add_custom_command( - OUTPUT "${CMAKE_BINARY_DIR}/proto/${comp}/proto/${file_name}.pb.cc" - "${CMAKE_BINARY_DIR}/proto/${comp}/proto/${file_name}.pb.h" - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/proto/${comp}/proto" - COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/proto/${comp}/proto ${abs_file} - DEPENDS protobuf::protoc ${abs_file} - 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() +set(protobuf_CXXFLAGS "-Wno-maybe-uninitialized -Wno-unused-parameter -fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -D_GLIBCXX_USE_CXX11_ABI=0 -O2 -Dgoogle=ascend_private") +set(protobuf_LDFLAGS "-Wl,-z,relro,-z,now,-z,noexecstack") +ExternalProject_Add(protobuf_build + URL https://github.com/protocolbuffers/protobuf/archive/v3.8.0.tar.gz + CONFIGURE_COMMAND ${CMAKE_COMMAND} + -Dprotobuf_WITH_ZLIB=OFF + -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_LINKER=${CMAKE_LINKER} + -DCMAKE_AR=${CMAKE_AR} + -DCMAKE_RANLIB=${CMAKE_RANLIB} + -DLIB_PREFIX=ascend_ + -Dprotobuf_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_CXX_FLAGS=${protobuf_CXXFLAGS} -DCMAKE_CXX_LDFLAGS=${protobuf_LDFLAGS} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/protobuf /cmake + BUILD_COMMAND $(MAKE) + INSTALL_COMMAND $(MAKE) install + EXCLUDE_FROM_ALL TRUE +) +include(GNUInstallDirs) + +set(PROTOBUF_SHARED_PKG_DIR ${CMAKE_INSTALL_PREFIX}/protobuf) + +add_library(ascend_protobuf SHARED IMPORTED) + +file(MAKE_DIRECTORY ${PROTOBUF_SHARED_PKG_DIR}/include) + +set_target_properties(ascend_protobuf PROPERTIES + IMPORTED_LOCATION ${PROTOBUF_SHARED_PKG_DIR}/${CMAKE_INSTALL_LIBDIR}/libascend_protobuf.so +) + +target_include_directories(ascend_protobuf INTERFACE ${PROTOBUF_SHARED_PKG_DIR}/include) + +set(INSTALL_BASE_DIR "") +set(INSTALL_LIBRARY_DIR lib) + +install(FILES ${PROTOBUF_SHARED_PKG_DIR}/${CMAKE_INSTALL_LIBDIR}/ascend_protobuf.so.3.8.0.0 OPTIONAL + DESTINATION ${INSTALL_LIBRARY_DIR}) +install(FILES ${PROTOBUF_SHARED_PKG_DIR}/${CMAKE_INSTALL_LIBDIR}/ascend_protobuf.so OPTIONAL + DESTINATION ${INSTALL_LIBRARY_DIR}) + +add_dependencies(ascend_protobuf protobuf_build) + +#set(HAVE_PROTOBUF TRUE CACHE BOOL "protobuf build add") +set(HAVE_PROTOBUF TRUE) diff --git a/src/common/graph/CMakeLists.txt b/src/common/graph/CMakeLists.txt index bb63eb81..ef56b130 100755 --- a/src/common/graph/CMakeLists.txt +++ b/src/common/graph/CMakeLists.txt @@ -31,8 +31,8 @@ file(GLOB_RECURSE ONNX_PROTO_LIST RELATIVE ${CMAKE_CURRENT_LIST_DIR} "${onnx_INC}/onnx/onnx.proto" ) -ge_protobuf_generate(ge PROTO_SRCS PROTO_HDRS ${PROTO_LIST}) -ge_protobuf_generate(ge PROTO_ONNX_SRCS PROTO_ONNX_HDRS ${ONNX_PROTO_LIST}) +protobuf_generate(ge PROTO_SRCS PROTO_HDRS ${PROTO_LIST}) +protobuf_generate(ge PROTO_ONNX_SRCS PROTO_ONNX_HDRS ${ONNX_PROTO_LIST}) # need to remove dependencies on pb files later file(GLOB SRC_LIST RELATIVE ${CMAKE_CURRENT_LIST_DIR}