protobuf.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. include(common)
  2. function(target_proto_plugin Tgt Name PluginTarget)
  3. set_property(TARGET ${Tgt} APPEND PROPERTY
  4. PROTOC_OPTS --${Name}_out=${PROJECT_BINARY_DIR}/$<TARGET_PROPERTY:${Tgt},PROTO_NAMESPACE> --plugin=protoc-gen-${Name}=$<TARGET_FILE:${PluginTarget}>
  5. )
  6. set_property(TARGET ${Tgt} APPEND PROPERTY
  7. PROTOC_DEPS ${PluginTarget}
  8. )
  9. endfunction()
  10. function(target_proto_addincls Tgt)
  11. set_property(TARGET ${Tgt} APPEND PROPERTY
  12. PROTO_ADDINCL ${ARGN}
  13. )
  14. endfunction()
  15. function(target_proto_outs Tgt)
  16. set_property(TARGET ${Tgt} APPEND PROPERTY
  17. PROTO_OUTS ${ARGN}
  18. )
  19. endfunction()
  20. function(target_messages Tgt Scope UseStyleguide UseEvent2Cpp)
  21. if (vanilla_protobuf STREQUAL "yes")
  22. set(protoc_bin ${PROJECT_BINARY_DIR}/bin/protoc${CMAKE_EXECUTABLE_SUFFIX})
  23. set(protoc_dependency "")
  24. set(UseStyleguide "no") # cpp_styleguide can't compile with vanilla protobuf
  25. else()
  26. get_built_tool_path(protoc_bin protoc_dependency "contrib/tools/protoc/bin" "protoc")
  27. endif()
  28. if (UseStyleguide STREQUAL "yes")
  29. get_built_tool_path(cpp_styleguide_bin cpp_styleguide_dependency "contrib/tools/protoc/plugins/cpp_styleguide" "cpp_styleguide")
  30. set(protoc_styleguide_plugin --plugin=protoc-gen-cpp_styleguide=${cpp_styleguide_bin})
  31. else()
  32. set(protoc_styleguide_plugin "")
  33. set(cpp_styleguide_dependency "")
  34. endif()
  35. if (UseEvent2Cpp STREQUAL "yes")
  36. get_built_tool_path(event2cpp_bin event2cpp_dependency "tools/event2cpp/bin" "event2cpp")
  37. set(protoc_event2cpp_plugin --plugin=protoc-gen-event2cpp=${event2cpp_bin})
  38. set(ext_h ".ev.pb.h")
  39. set(ext_c ".ev.pb.cc")
  40. else()
  41. set(protoc_event2cpp_plugin "")
  42. set(event2cpp_dependency "")
  43. set(ext_h ".pb.h")
  44. set(ext_c ".pb.cc")
  45. endif()
  46. get_property(ProtocExtraOutsSuf TARGET ${Tgt} PROPERTY PROTOC_EXTRA_OUTS)
  47. foreach(proto ${ARGN})
  48. if(proto MATCHES ${PROJECT_BINARY_DIR})
  49. file(RELATIVE_PATH protoRel ${PROJECT_BINARY_DIR} ${proto})
  50. elseif (proto MATCHES ${PROJECT_SOURCE_DIR})
  51. file(RELATIVE_PATH protoRel ${PROJECT_SOURCE_DIR} ${proto})
  52. else()
  53. set(protoRel ${proto})
  54. endif()
  55. get_filename_component(OutputBase ${protoRel} NAME_WLE)
  56. get_filename_component(OutputDir ${PROJECT_BINARY_DIR}/${protoRel} DIRECTORY)
  57. list(TRANSFORM ProtocExtraOutsSuf PREPEND ${OutputDir}/${OutputBase} OUTPUT_VARIABLE ProtocExtraOuts)
  58. add_custom_command(
  59. OUTPUT
  60. ${OutputDir}/${OutputBase}${ext_c}
  61. ${OutputDir}/${OutputBase}${ext_h}
  62. ${ProtocExtraOuts}
  63. COMMAND ${protoc_bin}
  64. ${COMMON_PROTOC_FLAGS}
  65. "-I$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTO_ADDINCL>>,;-I>"
  66. "$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTO_OUTS>>,;>"
  67. ${protoc_styleguide_plugin}
  68. ${protoc_event2cpp_plugin}
  69. "$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTOC_OPTS>>,;>"
  70. ${protoRel}
  71. COMMAND Python3::Interpreter ${PROJECT_SOURCE_DIR}/build/scripts/re_replace.py --from-re "\"((?:struct|class)\\s+\\S+\\s+)final\\s*:\"" --to-re "\"\\1:\"" ${OutputDir}/${OutputBase}${ext_c} ${OutputDir}/${OutputBase}${ext_h}
  72. DEPENDS
  73. ${proto}
  74. $<TARGET_PROPERTY:${Tgt},PROTOC_DEPS>
  75. ${protoc_dependency}
  76. ${cpp_styleguide_dependency}
  77. ${event2cpp_dependency}
  78. ${PROJECT_SOURCE_DIR}/build/scripts/re_replace.py
  79. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  80. COMMAND_EXPAND_LISTS
  81. )
  82. target_sources(${Tgt} ${Scope}
  83. ${OutputDir}/${OutputBase}${ext_c} ${OutputDir}/${OutputBase}${ext_h}
  84. ${ProtocExtraOuts}
  85. )
  86. endforeach()
  87. endfunction()
  88. function(target_proto_messages Tgt Scope)
  89. target_messages(${Tgt} ${Scope} "yes" "no" ${ARGN})
  90. endfunction()
  91. function(target_ev_messages Tgt Scope)
  92. target_messages(${Tgt} ${Scope} "yes" "yes" ${ARGN})
  93. endfunction()