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 6.4 kB

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