protobuf-config.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # User options
  2. include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake")
  3. # Depend packages
  4. # Imported targets
  5. include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake")
  6. function(protobuf_generate)
  7. include(CMakeParseArguments)
  8. set(_options APPEND_PATH)
  9. set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN)
  10. if(COMMAND target_sources)
  11. list(APPEND _singleargs TARGET)
  12. endif()
  13. set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS PROTOC_OPTIONS)
  14. cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
  15. if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
  16. message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
  17. return()
  18. endif()
  19. if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
  20. message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
  21. return()
  22. endif()
  23. if(NOT protobuf_generate_LANGUAGE)
  24. set(protobuf_generate_LANGUAGE cpp)
  25. endif()
  26. string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)
  27. if(NOT protobuf_generate_PROTOC_OUT_DIR)
  28. set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  29. endif()
  30. if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
  31. set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
  32. endif()
  33. if(protobuf_generate_PLUGIN)
  34. set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
  35. endif()
  36. if(NOT protobuf_generate_GENERATE_EXTENSIONS)
  37. if(protobuf_generate_LANGUAGE STREQUAL cpp)
  38. set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
  39. elseif(protobuf_generate_LANGUAGE STREQUAL python)
  40. set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
  41. else()
  42. message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
  43. return()
  44. endif()
  45. endif()
  46. if(protobuf_generate_TARGET)
  47. get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
  48. foreach(_file ${_source_list})
  49. if(_file MATCHES "proto$")
  50. list(APPEND protobuf_generate_PROTOS ${_file})
  51. endif()
  52. endforeach()
  53. endif()
  54. if(NOT protobuf_generate_PROTOS)
  55. message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
  56. return()
  57. endif()
  58. if(protobuf_generate_APPEND_PATH)
  59. # Create an include path for each file specified
  60. foreach(_file ${protobuf_generate_PROTOS})
  61. get_filename_component(_abs_file ${_file} ABSOLUTE)
  62. get_filename_component(_abs_path ${_abs_file} PATH)
  63. list(FIND _protobuf_include_path ${_abs_path} _contains_already)
  64. if(${_contains_already} EQUAL -1)
  65. list(APPEND _protobuf_include_path -I ${_abs_path})
  66. endif()
  67. endforeach()
  68. endif()
  69. foreach(DIR ${protobuf_generate_IMPORT_DIRS})
  70. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  71. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  72. if(${_contains_already} EQUAL -1)
  73. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  74. endif()
  75. endforeach()
  76. if(NOT _protobuf_include_path)
  77. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  78. endif()
  79. set(_generated_srcs_all)
  80. foreach(_proto ${protobuf_generate_PROTOS})
  81. get_filename_component(_abs_file ${_proto} ABSOLUTE)
  82. get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
  83. get_filename_component(_file_full_name ${_proto} NAME)
  84. string(FIND "${_file_full_name}" "." _file_last_ext_pos REVERSE)
  85. string(SUBSTRING "${_file_full_name}" 0 ${_file_last_ext_pos} _basename)
  86. set(_suitable_include_found FALSE)
  87. foreach(DIR ${_protobuf_include_path})
  88. if(NOT DIR STREQUAL "-I")
  89. file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir})
  90. string(FIND "${_rel_dir}" "../" _is_in_parent_folder)
  91. if (NOT ${_is_in_parent_folder} EQUAL 0)
  92. set(_suitable_include_found TRUE)
  93. break()
  94. endif()
  95. endif()
  96. endforeach()
  97. if(NOT _suitable_include_found)
  98. message(SEND_ERROR "Error: protobuf_generate could not find any correct proto include directory.")
  99. return()
  100. endif()
  101. set(_generated_srcs)
  102. foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
  103. list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_rel_dir}/${_basename}${_ext}")
  104. endforeach()
  105. list(APPEND _generated_srcs_all ${_generated_srcs})
  106. add_custom_command(
  107. OUTPUT ${_generated_srcs}
  108. COMMAND protobuf::protoc
  109. ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_protobuf_include_path} ${_abs_file}
  110. DEPENDS ${_abs_file} protobuf::protoc
  111. COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}. Custom options: ${protobuf_generate_PROTOC_OPTIONS}"
  112. VERBATIM )
  113. endforeach()
  114. set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
  115. if(protobuf_generate_OUT_VAR)
  116. set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
  117. endif()
  118. if(protobuf_generate_TARGET)
  119. target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
  120. endif()
  121. endfunction()
  122. # CMake FindProtobuf module compatible file
  123. if(protobuf_MODULE_COMPATIBLE)
  124. include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake")
  125. endif()