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.

function.cmake 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. function(protobuf_generate comp c_var h_var)
  2. if(NOT ARGN)
  3. message(SEND_ERROR "Error: protobuf_generate() called without any proto files")
  4. return()
  5. endif()
  6. set(${c_var})
  7. set(${h_var})
  8. set(_add_target FALSE)
  9. set(extra_option "")
  10. foreach(arg ${ARGN})
  11. if("${arg}" MATCHES "--proto_path")
  12. set(extra_option ${arg})
  13. endif()
  14. endforeach()
  15. foreach(file ${ARGN})
  16. if("${file}" MATCHES "--proto_path")
  17. continue()
  18. endif()
  19. if("${file}" STREQUAL "TARGET")
  20. set(_add_target TRUE)
  21. continue()
  22. endif()
  23. get_filename_component(abs_file ${file} ABSOLUTE)
  24. get_filename_component(file_name ${file} NAME_WE)
  25. get_filename_component(file_dir ${abs_file} PATH)
  26. get_filename_component(parent_subdir ${file_dir} NAME)
  27. if("${parent_subdir}" STREQUAL "proto")
  28. set(proto_output_path ${CMAKE_BINARY_DIR}/proto/${comp}/proto)
  29. else()
  30. set(proto_output_path ${CMAKE_BINARY_DIR}/proto/${comp}/proto/${parent_subdir})
  31. endif()
  32. list(APPEND ${c_var} "${proto_output_path}/${file_name}.pb.cc")
  33. list(APPEND ${h_var} "${proto_output_path}/${file_name}.pb.h")
  34. add_custom_command(
  35. OUTPUT "${proto_output_path}/${file_name}.pb.cc" "${proto_output_path}/${file_name}.pb.h"
  36. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  37. COMMAND ${CMAKE_COMMAND} -E make_directory "${proto_output_path}"
  38. COMMAND ${CMAKE_COMMAND} -E echo "generate proto cpp_out ${comp} by ${abs_file}"
  39. COMMAND ${PROTOC_PROGRAM} -I${file_dir} ${extra_option} --cpp_out=${proto_output_path} ${abs_file}
  40. DEPENDS ${abs_file}
  41. COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM )
  42. endforeach()
  43. if(_add_target)
  44. add_custom_target(
  45. ${comp} DEPENDS ${${c_var}} ${${h_var}}
  46. )
  47. endif()
  48. set_source_files_properties(${${c_var}} ${${h_var}} PROPERTIES GENERATED TRUE)
  49. set(${c_var} ${${c_var}} PARENT_SCOPE)
  50. set(${h_var} ${${h_var}} PARENT_SCOPE)
  51. endfunction()
  52. function(protobuf_generate_py comp py_var)
  53. if(NOT ARGN)
  54. message(SEND_ERROR "Error: protobuf_generate_py() called without any proto files")
  55. return()
  56. endif()
  57. set(${py_var})
  58. set(_add_target FALSE)
  59. foreach(file ${ARGN})
  60. if("${file}" STREQUAL "TARGET")
  61. set(_add_target TRUE)
  62. continue()
  63. endif()
  64. get_filename_component(abs_file ${file} ABSOLUTE)
  65. get_filename_component(file_name ${file} NAME_WE)
  66. get_filename_component(file_dir ${abs_file} PATH)
  67. get_filename_component(parent_subdir ${file_dir} NAME)
  68. if("${parent_subdir}" STREQUAL "proto")
  69. set(proto_output_path ${CMAKE_BINARY_DIR}/proto/${comp}/proto)
  70. else()
  71. set(proto_output_path ${CMAKE_BINARY_DIR}/proto/${comp}/proto/${parent_subdir})
  72. endif()
  73. list(APPEND ${py_var} "${proto_output_path}/${file_name}_pb2.py")
  74. add_custom_command(
  75. OUTPUT "${proto_output_path}/${file_name}_pb2.py"
  76. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  77. COMMAND ${CMAKE_COMMAND} -E make_directory "${proto_output_path}"
  78. COMMAND ${CMAKE_COMMAND} -E echo "generate proto python_out ${comp} by ${abs_file}"
  79. COMMAND ${PROTOC_PROGRAM} -I${file_dir} --python_out=${proto_output_path} ${abs_file}
  80. DEPENDS ${abs_file}
  81. COMMENT "Running PYTHON protocol buffer compiler on ${file}" VERBATIM )
  82. endforeach()
  83. if(_add_target)
  84. add_custom_target(
  85. ${comp} DEPENDS ${${py_var}}
  86. )
  87. endif()
  88. set_source_files_properties(${${py_var}} PROPERTIES GENERATED TRUE)
  89. set(${py_var} ${${py_var}} PARENT_SCOPE)
  90. endfunction()