protobuf-config.cmake.in 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 PROTOC_OUT_DIR PLUGIN)
  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(NOT protobuf_generate_PROTOC_OUT_DIR)
  29. set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  30. endif()
  31. if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
  32. set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
  33. endif()
  34. if(protobuf_generate_PLUGIN)
  35. set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
  36. endif()
  37. if(NOT protobuf_generate_GENERATE_EXTENSIONS)
  38. if(protobuf_generate_LANGUAGE STREQUAL cpp)
  39. set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
  40. elseif(protobuf_generate_LANGUAGE STREQUAL python)
  41. set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
  42. else()
  43. message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
  44. return()
  45. endif()
  46. endif()
  47. if(protobuf_generate_TARGET)
  48. get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
  49. foreach(_file ${_source_list})
  50. if(_file MATCHES "proto$")
  51. list(APPEND protobuf_generate_PROTOS ${_file})
  52. endif()
  53. endforeach()
  54. endif()
  55. if(NOT protobuf_generate_PROTOS)
  56. message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
  57. return()
  58. endif()
  59. if(protobuf_generate_APPEND_PATH)
  60. # Create an include path for each file specified
  61. foreach(_file ${protobuf_generate_PROTOS})
  62. get_filename_component(_abs_file ${_file} ABSOLUTE)
  63. get_filename_component(_abs_path ${_abs_file} PATH)
  64. list(FIND _protobuf_include_path ${_abs_path} _contains_already)
  65. if(${_contains_already} EQUAL -1)
  66. list(APPEND _protobuf_include_path -I ${_abs_path})
  67. endif()
  68. endforeach()
  69. endif()
  70. foreach(DIR ${protobuf_generate_IMPORT_DIRS})
  71. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  72. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  73. if(${_contains_already} EQUAL -1)
  74. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  75. endif()
  76. endforeach()
  77. if(NOT _protobuf_include_path)
  78. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  79. endif()
  80. set(_generated_srcs_all)
  81. foreach(_proto ${protobuf_generate_PROTOS})
  82. get_filename_component(_abs_file ${_proto} ABSOLUTE)
  83. get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
  84. get_filename_component(_basename ${_proto} NAME_WLE)
  85. set(_suitable_include_found FALSE)
  86. foreach(DIR ${_protobuf_include_path})
  87. if(NOT DIR STREQUAL "-I")
  88. file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir})
  89. if(NOT "${_rel_dir}" MATCHES "^\.\.[/\\].*")
  90. set(_suitable_include_found TRUE)
  91. break()
  92. endif()
  93. endif()
  94. endforeach()
  95. if(NOT _suitable_include_found)
  96. message(SEND_ERROR "Error: protobuf_generate could not find any correct proto include directory.")
  97. return()
  98. endif()
  99. set(_generated_srcs)
  100. foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
  101. list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_rel_dir}/${_basename}${_ext}")
  102. endforeach()
  103. list(APPEND _generated_srcs_all ${_generated_srcs})
  104. add_custom_command(
  105. OUTPUT ${_generated_srcs}
  106. COMMAND protobuf::protoc
  107. ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_protobuf_include_path} ${_abs_file}
  108. DEPENDS ${_abs_file} protobuf::protoc
  109. COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
  110. VERBATIM )
  111. endforeach()
  112. set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
  113. if(protobuf_generate_OUT_VAR)
  114. set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
  115. endif()
  116. if(protobuf_generate_TARGET)
  117. target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
  118. endif()
  119. endfunction()
  120. # CMake FindProtobuf module compatible file
  121. if(protobuf_MODULE_COMPATIBLE)
  122. include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake")
  123. endif()