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.

dependency_protobuf.cmake 4.4 kB

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