You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

protobuf.cmake 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. mindspore_add_pkg(protobuf
  2. VER 3.8.0
  3. HEAD_ONLY ./
  4. URL https://github.com/protocolbuffers/protobuf/archive/v3.8.0.tar.gz
  5. MD5 3d9e32700639618a4d2d342c99d4507a)
  6. set(protobuf_BUILD_TESTS OFF CACHE BOOL "Disable protobuf test")
  7. set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "Gen shared library")
  8. set(_ms_tmp_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  9. string(REPLACE " -Wall" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  10. string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  11. add_subdirectory(${protobuf_DIRPATH}/cmake ${protobuf_DIRPATH}/build)
  12. set(CMAKE_CXX_FLAGS ${_ms_tmp_CMAKE_CXX_FLAGS})
  13. set(PROTOBUF_LIBRARY protobuf::libprotobuf)
  14. include_directories(${protobuf_DIRPATH}/src)
  15. add_library(mindspore::protobuf ALIAS libprotobuf)
  16. function(ms_protobuf_generate c_var h_var)
  17. if(NOT ARGN)
  18. message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files")
  19. return()
  20. endif()
  21. set(${c_var})
  22. set(${h_var})
  23. foreach(file ${ARGN})
  24. get_filename_component(abs_file ${file} ABSOLUTE)
  25. get_filename_component(file_name ${file} NAME_WE)
  26. get_filename_component(file_dir ${abs_file} PATH)
  27. file(RELATIVE_PATH rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${file_dir})
  28. list(APPEND ${c_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc")
  29. list(APPEND ${h_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h")
  30. add_custom_command(
  31. OUTPUT "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc"
  32. "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h"
  33. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  34. COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/${rel_path}"
  35. COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
  36. DEPENDS protobuf::protoc ${abs_file}
  37. COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM )
  38. endforeach()
  39. set_source_files_properties(${${c_var}} ${${h_var}} PROPERTIES GENERATED TRUE)
  40. set(${c_var} ${${c_var}} PARENT_SCOPE)
  41. set(${h_var} ${${h_var}} PARENT_SCOPE)
  42. endfunction()
  43. function(ms_protobuf_generate_py c_var h_var py_var)
  44. if(NOT ARGN)
  45. message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files")
  46. return()
  47. endif()
  48. set(${c_var})
  49. set(${h_var})
  50. set(${py_var})
  51. foreach(file ${ARGN})
  52. get_filename_component(abs_file ${file} ABSOLUTE)
  53. get_filename_component(file_name ${file} NAME_WE)
  54. get_filename_component(file_dir ${abs_file} PATH)
  55. file(RELATIVE_PATH rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${file_dir})
  56. list(APPEND ${c_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc")
  57. list(APPEND ${h_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h")
  58. list(APPEND ${py_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py")
  59. add_custom_command(
  60. OUTPUT "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc"
  61. "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h"
  62. "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py"
  63. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  64. COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/${rel_path}"
  65. COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
  66. COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
  67. COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
  68. COMMAND perl -pi -e "s/import (.+_pb2.*)/from . import \\1/" "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py"
  69. COMMAND cp "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py" "${PROJECT_SOURCE_DIR}/mindspore/train/"
  70. DEPENDS protobuf::protoc ${abs_file}
  71. COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM )
  72. endforeach()
  73. set_source_files_properties(${${c_var}} ${${h_var}} ${${py_var}} PROPERTIES GENERATED TRUE)
  74. set(${c_var} ${${c_var}} PARENT_SCOPE)
  75. set(${h_var} ${${h_var}} PARENT_SCOPE)
  76. set(${py_var} ${${py_var}} PARENT_SCOPE)
  77. endfunction()

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.

Contributors (1)