protobuf-config.cmake.in 4.1 KB

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