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