protobuf.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. include(common)
  2. function(target_proto_plugin Tgt Name PluginTarget)
  3. set_property(TARGET ${Tgt} APPEND PROPERTY
  4. PROTOC_OPTS --${Name}_out=${CMAKE_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_proto_messages Tgt Scope)
  21. get_built_tool_path(protoc_bin protoc_dependency contrib/tools/protoc/bin protoc)
  22. get_built_tool_path(cpp_styleguide_bin cpp_styleguide_dependency contrib/tools/protoc/plugins/cpp_styleguide cpp_styleguide)
  23. get_property(ProtocExtraOutsSuf TARGET ${Tgt} PROPERTY PROTOC_EXTRA_OUTS)
  24. foreach(proto ${ARGN})
  25. if(proto MATCHES ${CMAKE_BINARY_DIR})
  26. file(RELATIVE_PATH protoRel ${CMAKE_BINARY_DIR} ${proto})
  27. elseif (proto MATCHES ${CMAKE_SOURCE_DIR})
  28. file(RELATIVE_PATH protoRel ${CMAKE_SOURCE_DIR} ${proto})
  29. else()
  30. set(protoRel ${proto})
  31. endif()
  32. get_filename_component(OutputBase ${protoRel} NAME_WLE)
  33. get_filename_component(OutputDir ${CMAKE_BINARY_DIR}/${protoRel} DIRECTORY)
  34. list(TRANSFORM ProtocExtraOutsSuf PREPEND ${OutputDir}/${OutputBase} OUTPUT_VARIABLE ProtocExtraOuts)
  35. add_custom_command(
  36. OUTPUT
  37. ${OutputDir}/${OutputBase}.pb.cc
  38. ${OutputDir}/${OutputBase}.pb.h
  39. ${ProtocExtraOuts}
  40. COMMAND ${protoc_bin}
  41. ${COMMON_PROTOC_FLAGS}
  42. "-I$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTO_ADDINCL>>,;-I>"
  43. "$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTO_OUTS>>,;>"
  44. --plugin=protoc-gen-cpp_styleguide=${cpp_styleguide_bin}
  45. "$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTOC_OPTS>>,;>"
  46. ${protoRel}
  47. DEPENDS
  48. ${proto}
  49. $<TARGET_PROPERTY:${Tgt},PROTOC_DEPS>
  50. ${protoc_dependency}
  51. ${cpp_styleguide_dependency}
  52. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  53. COMMAND_EXPAND_LISTS
  54. )
  55. target_sources(${Tgt} ${Scope}
  56. ${OutputDir}/${OutputBase}.pb.cc ${OutputDir}/${OutputBase}.pb.h
  57. ${ProtocExtraOuts}
  58. )
  59. endforeach()
  60. endfunction()
  61. function(target_ev_messages Tgt Scope)
  62. get_built_tool_path(protoc_bin protoc_dependency contrib/tools/protoc/bin protoc)
  63. get_built_tool_path(cpp_styleguide_bin cpp_styleguide_dependency contrib/tools/protoc/plugins/cpp_styleguide cpp_styleguide)
  64. get_built_tool_path(event2cpp_bin event2cpp_dependency tools/event2cpp/bin event2cpp)
  65. get_property(ProtocExtraOutsSuf TARGET ${Tgt} PROPERTY PROTOC_EXTRA_OUTS)
  66. foreach(proto ${ARGN})
  67. if(proto MATCHES ${CMAKE_BINARY_DIR})
  68. file(RELATIVE_PATH protoRel ${CMAKE_BINARY_DIR} ${proto})
  69. elseif (proto MATCHES ${CMAKE_SOURCE_DIR})
  70. file(RELATIVE_PATH protoRel ${CMAKE_SOURCE_DIR} ${proto})
  71. else()
  72. set(protoRel ${proto})
  73. endif()
  74. get_filename_component(OutputBase ${protoRel} NAME_WLE)
  75. get_filename_component(OutputDir ${CMAKE_BINARY_DIR}/${protoRel} DIRECTORY)
  76. list(TRANSFORM ProtocExtraOutsSuf PREPEND ${OutputDir}/${OutputBase} OUTPUT_VARIABLE ProtocExtraOuts)
  77. add_custom_command(
  78. OUTPUT
  79. ${OutputDir}/${OutputBase}.ev.pb.cc
  80. ${OutputDir}/${OutputBase}.ev.pb.h
  81. ${ProtocExtraOuts}
  82. COMMAND ${protoc_bin}
  83. ${COMMON_PROTOC_FLAGS}
  84. "-I$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTO_ADDINCL>>,;-I>"
  85. "$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTO_OUTS>>,;>"
  86. --plugin=protoc-gen-cpp_styleguide=${cpp_styleguide_bin}
  87. --plugin=protoc-gen-event2cpp=${event2cpp_bin}
  88. "$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},PROTOC_OPTS>>,;>"
  89. ${protoRel}
  90. DEPENDS
  91. ${proto}
  92. $<TARGET_PROPERTY:${Tgt},PROTOC_DEPS>
  93. ${protoc_dependency}
  94. ${cpp_styleguide_dependency}
  95. ${event2cpp_dependency}
  96. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  97. COMMAND_EXPAND_LISTS
  98. )
  99. target_sources(${Tgt} ${Scope}
  100. ${OutputDir}/${OutputBase}.ev.pb.cc ${OutputDir}/${OutputBase}.ev.pb.h
  101. ${ProtocExtraOuts}
  102. )
  103. endforeach()
  104. endfunction()