protobuf-config.cmake.in 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # User options
  2. include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake")
  3. # Depend packages
  4. @_protobuf_FIND_ZLIB@
  5. # Imported targets
  6. include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake")
  7. function(protobuf_generate)
  8. include(CMakeParseArguments)
  9. set(_singleargs LANGUAGE OUT_VAR)
  10. if(COMMAND target_sources)
  11. list(APPEND _singleargs TARGET)
  12. endif()
  13. cmake_parse_arguments(protobuf_generate "APPEND_PATH" "${_singleargs}" "PROTOS IMPORT_DIRS GENERATE_EXTENSIONS" "${ARGN}")
  14. if(protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
  15. message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
  16. return()
  17. endif()
  18. if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
  19. message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
  20. return()
  21. endif()
  22. if(NOT protobuf_generate_LANGUAGE)
  23. set(protobuf_generate_LANGUAGE cpp)
  24. endif()
  25. string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)
  26. if(NOT protobuf_GENERATE_EXTENSIONS)
  27. if(protobuf_generate_LANGUAGE STREQUAL cpp)
  28. set(protobuf_GENERATE_EXTENSIONS .pb.h .pb.cc)
  29. elseif(protobuf_generate_LANGUAGE STREQUAL python)
  30. set(protobuf_GENERATE_EXTENSIONS _pb2.py)
  31. else()
  32. message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
  33. return()
  34. endif()
  35. endif()
  36. if(protobuf_generate_APPEND_PATH)
  37. # Create an include path for each file specified
  38. foreach(_file ${ARGN})
  39. get_filename_component(_abs_file ${_file} ABSOLUTE)
  40. get_filename_component(_abs_path ${_abs_file} PATH)
  41. list(FIND _protobuf_include_path ${_abs_path} _contains_already)
  42. if(${_contains_already} EQUAL -1)
  43. list(APPEND _protobuf_include_path -I ${_abs_path})
  44. endif()
  45. endforeach()
  46. else()
  47. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  48. endif()
  49. foreach(DIR ${protobuf_generate_IMPORT_DIRS})
  50. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  51. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  52. if(${_contains_already} EQUAL -1)
  53. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  54. endif()
  55. endforeach()
  56. if(protobuf_generate_TARGET)
  57. get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
  58. foreach(_file ${_source_list})
  59. if(_file MATCHES "proto$")
  60. list(APPEND protobuf_generate_PROTOS ${_file})
  61. endif()
  62. endforeach()
  63. endif()
  64. if(NOT protobuf_generate_PROTOS)
  65. message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
  66. return()
  67. endif()
  68. set(_generated_srcs)
  69. foreach(_proto ${protobuf_generate_PROTOS})
  70. get_filename_component(_abs_file ${_proto} ABSOLUTE)
  71. get_filename_component(_basename ${_proto} NAME_WE)
  72. foreach(_ext ${_output_extensions})
  73. list(APPEND _generated_srcs "${CMAKE_CURRENT_BINARY_DIR}/${_basename}${_ext}")
  74. endforeach()
  75. add_custom_command(
  76. OUTPUT ${_generated_srcs}
  77. COMMAND protobuf::protoc
  78. ARGS --${protobuf_generate_LANGUAGE}_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${_abs_file}
  79. DEPENDS ${ABS_FIL} protobuf::protoc
  80. COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
  81. VERBATIM )
  82. endforeach()
  83. set_source_files_properties(${_generated_srcs} PROPERTIES GENERATED TRUE)
  84. if(protobuf_generate_OUT_VAR)
  85. set(${protobuf_generate_OUT_VAR} ${_generated_srcs} PARENT_SCOPE)
  86. endif()
  87. if(protobuf_generate_TARGET)
  88. target_sources(${protobuf_generate_TARGET} PUBLIC ${_generated_srcs})
  89. endif()
  90. endfunction()
  91. # CMake FindProtobuf module compatible file
  92. if(protobuf_MODULE_COMPATIBLE)
  93. include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake")
  94. endif()