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 1.9 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. foreach(file ${ARGN})
  10. if("${file}" STREQUAL "TARGET")
  11. set(_add_target TRUE)
  12. continue()
  13. endif()
  14. get_filename_component(abs_file ${file} ABSOLUTE)
  15. get_filename_component(file_name ${file} NAME_WE)
  16. get_filename_component(file_dir ${abs_file} PATH)
  17. get_filename_component(parent_subdir ${file_dir} NAME)
  18. if("${parent_subdir}" STREQUAL "proto")
  19. set(proto_output_path ${CMAKE_BINARY_DIR}/proto/${comp}/proto)
  20. else()
  21. set(proto_output_path ${CMAKE_BINARY_DIR}/proto/${comp}/proto/${parent_subdir})
  22. endif()
  23. list(APPEND ${c_var} "${proto_output_path}/${file_name}.pb.cc")
  24. list(APPEND ${h_var} "${proto_output_path}/${file_name}.pb.h")
  25. add_custom_command(
  26. OUTPUT "${proto_output_path}/${file_name}.pb.cc" "${proto_output_path}/${file_name}.pb.h"
  27. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  28. COMMAND ${CMAKE_COMMAND} -E make_directory "${proto_output_path}"
  29. COMMAND ${CMAKE_COMMAND} -E echo "generate proto cpp_out ${comp} by ${abs_file}"
  30. COMMAND ${protoc_EXECUTABLE} -I${file_dir} --cpp_out=${proto_output_path} ${abs_file}
  31. DEPENDS protoc_build ${abs_file}
  32. COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM )
  33. endforeach()
  34. if(_add_target)
  35. add_custom_target(
  36. ${comp} DEPENDS ${${c_var}} ${${h_var}}
  37. )
  38. endif()
  39. set_source_files_properties(${${c_var}} ${${h_var}} PROPERTIES GENERATED TRUE)
  40. set(${c_var} ${${c_var}} PARENT_SCOPE)
  41. set(${h_var} ${${h_var}} PARENT_SCOPE)
  42. endfunction()