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