CMakeLists.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # Copyright 2018 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # Generate output in-place. So long as the build is idempotent this helps
  15. # verify that the protoc-generated output isn't changing.
  16. set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  17. # Generating protobuf and nanopb sources when cross compiling doesn't work
  18. # because CMake can't build protoc for the host when building cross targets.
  19. #
  20. # On Windows, this *could* work, but it's not worth the time to figure out why
  21. # because the generated sources are already checked in.
  22. set(FIREBASE_IOS_PROTOC_GENERATE_SOURCES ON)
  23. if(WIN32 OR IOS OR ANDROID OR CMAKE_CROSSCOMPILING)
  24. set(FIREBASE_IOS_PROTOC_GENERATE_SOURCES OFF)
  25. endif()
  26. # Filename "roots" (i.e. without the .proto) from within the proto directory,
  27. # excluding anything in google/protobuf.
  28. set(
  29. PROTO_FILE_ROOTS
  30. firestore/local/maybe_document
  31. firestore/local/mutation
  32. firestore/local/target
  33. google/api/annotations
  34. google/api/http
  35. google/firestore/v1/common
  36. google/firestore/v1/document
  37. google/firestore/v1/firestore
  38. google/firestore/v1/query
  39. google/firestore/v1/write
  40. google/rpc/status
  41. google/type/latlng
  42. )
  43. # Full filenames (i.e. with the .proto) from within the proto directory,
  44. # excluding anything in google/protobuf
  45. foreach(root ${PROTO_FILE_ROOTS})
  46. list(
  47. APPEND PROTO_FILES
  48. ${OUTPUT_DIR}/protos/${root}.proto
  49. )
  50. if(EXISTS ${OUTPUT_DIR}/protos/${root}.options)
  51. list(
  52. APPEND PROTO_FILES_OPTIONS
  53. ${OUTPUT_DIR}/protos/${root}.options
  54. )
  55. endif()
  56. endforeach()
  57. # Filename "roots" (i.e. without the .proto) from within the proto directory,
  58. # from the google/protobuf package.
  59. set(
  60. WELL_KNOWN_PROTO_FILE_ROOTS
  61. google/protobuf/any
  62. google/protobuf/empty
  63. google/protobuf/struct
  64. google/protobuf/timestamp
  65. google/protobuf/wrappers
  66. )
  67. # Full filenames (i.e. with the .proto) from within the proto directory, from
  68. # the google/protobuf package.
  69. foreach(root ${WELL_KNOWN_PROTO_FILE_ROOTS})
  70. list(
  71. APPEND WELL_KNOWN_PROTO_FILES
  72. ${OUTPUT_DIR}/protos/${root}.proto
  73. )
  74. endforeach()
  75. # Populate NANOPB_GENERATED_SOURCES with the list of nanopb-generated sources.
  76. # The nanopb runtime does not include the well-known protos so we have to build
  77. # them ourselves.
  78. foreach(root ${PROTO_FILE_ROOTS} ${WELL_KNOWN_PROTO_FILE_ROOTS})
  79. list(
  80. APPEND NANOPB_GENERATED_SOURCES
  81. ${OUTPUT_DIR}/nanopb/${root}.nanopb.cc
  82. ${OUTPUT_DIR}/nanopb/${root}.nanopb.h
  83. )
  84. endforeach()
  85. # Populate PROTOBUF_CPP_GENERATED_SOURCES with the list of libprotobuf C++
  86. # sources. These are used for verifying interoperation from nanopb.
  87. #
  88. # Libprotobuf includes the well-known protos so they must be omitted here.
  89. foreach(root ${PROTO_FILE_ROOTS})
  90. list(
  91. APPEND PROTOBUF_CPP_GENERATED_SOURCES
  92. ${OUTPUT_DIR}/cpp/${root}.pb.cc
  93. ${OUTPUT_DIR}/cpp/${root}.pb.h
  94. )
  95. endforeach()
  96. firebase_ios_add_library(
  97. firestore_protos_nanopb DISABLE_STRICT_WARNINGS EXCLUDE_FROM_ALL
  98. ${NANOPB_GENERATED_SOURCES}
  99. )
  100. target_include_directories(
  101. firestore_protos_nanopb PUBLIC
  102. ${CMAKE_CURRENT_LIST_DIR}/nanopb
  103. )
  104. target_link_libraries(
  105. firestore_protos_nanopb PUBLIC
  106. firestore_nanopb
  107. protobuf-nanopb-static
  108. absl_strings
  109. )
  110. # libprotobuf based generated protos. Expected only to be used in test (as
  111. # libprotobuf[-lite] is too large; we're using nanopb instead. But we do want
  112. # to test our serialization logic against libprotobuf.)
  113. firebase_ios_add_library(
  114. firestore_protos_protobuf DISABLE_STRICT_WARNINGS EXCLUDE_FROM_ALL
  115. ${PROTOBUF_CPP_GENERATED_SOURCES}
  116. )
  117. target_include_directories(
  118. firestore_protos_protobuf PUBLIC
  119. ${CMAKE_CURRENT_LIST_DIR}/cpp
  120. )
  121. target_link_libraries(
  122. firestore_protos_protobuf PUBLIC
  123. protobuf::libprotobuf
  124. )
  125. # Generate the python representation of descriptor.proto.
  126. set(PROTOBUF_DIR ${FIREBASE_EXTERNAL_SOURCE_DIR}/protobuf)
  127. set(PROTOBUF_PROTO ${PROTOBUF_DIR}/src/google/protobuf/descriptor.proto)
  128. set(PROTOBUF_PYTHON ${PROTOBUF_DIR}/python/google/protobuf/descriptor_pb2.py)
  129. add_custom_command(
  130. COMMENT "Generating protoc python plugins"
  131. OUTPUT ${PROTOBUF_PYTHON}
  132. COMMAND
  133. protoc
  134. -I${PROTOBUF_DIR}/src
  135. --python_out=${PROTOBUF_DIR}/python
  136. ${PROTOBUF_PROTO}
  137. VERBATIM
  138. DEPENDS
  139. protoc
  140. ${PROTOBUF_PROTO}
  141. )
  142. # Generate the python representation of nanopb's protos
  143. set(NANOPB_DIR ${FIREBASE_BINARY_DIR}/external/src/nanopb)
  144. set(
  145. NANOPB_PROTO
  146. ${NANOPB_DIR}/generator/proto/nanopb.proto
  147. ${NANOPB_DIR}/generator/proto/plugin.proto
  148. )
  149. set(
  150. NANOPB_PYTHON
  151. ${NANOPB_DIR}/generator/proto/nanopb_pb2.py
  152. ${NANOPB_DIR}/generator/proto/plugin_pb2.py
  153. )
  154. set(
  155. PROTO_INCLUDES
  156. -I${CMAKE_CURRENT_SOURCE_DIR}/protos
  157. -I${NANOPB_DIR}/generator
  158. -I${PROTOBUF_DIR}/src
  159. )
  160. add_custom_command(
  161. COMMENT "Generating nanopb python plugins"
  162. OUTPUT ${NANOPB_PYTHON}
  163. COMMAND
  164. protoc
  165. -I${NANOPB_DIR}/generator
  166. -I${PROTOBUF_DIR}/src
  167. --python_out=${NANOPB_DIR}/generator
  168. ${NANOPB_PROTO}
  169. VERBATIM
  170. DEPENDS
  171. protoc
  172. ${NANOPB_PROTO}
  173. )
  174. if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
  175. add_custom_command(
  176. COMMENT "Generating nanopb sources"
  177. OUTPUT ${NANOPB_GENERATED_SOURCES}
  178. COMMAND
  179. python
  180. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  181. --nanopb
  182. --protoc=$<TARGET_FILE:protoc>
  183. --pythonpath=${PROTOBUF_DIR}/python:${NANOPB_DIR}/generator
  184. --output_dir=${OUTPUT_DIR}
  185. ${PROTO_INCLUDES}
  186. VERBATIM
  187. DEPENDS
  188. protoc
  189. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  190. ${CMAKE_CURRENT_SOURCE_DIR}/nanopb_cpp_generator.py
  191. ${CMAKE_CURRENT_SOURCE_DIR}/lib/pretty_printing.py
  192. ${NANOPB_PYTHON}
  193. ${PROTOBUF_PYTHON}
  194. ${PROTO_FILES}
  195. ${PROTO_FILES_OPTIONS}
  196. ${WELL_KNOWN_PROTO_FILES}
  197. )
  198. add_custom_target(
  199. generate_nanopb_protos
  200. DEPENDS
  201. ${NANOPB_GENERATED_SOURCES}
  202. )
  203. endif()
  204. if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
  205. add_custom_command(
  206. COMMENT "Generating C++ protobuf sources"
  207. OUTPUT ${PROTOBUF_CPP_GENERATED_SOURCES}
  208. COMMAND
  209. python
  210. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  211. --cpp
  212. --protoc=$<TARGET_FILE:protoc>
  213. --output_dir=${OUTPUT_DIR}
  214. ${PROTO_INCLUDES}
  215. VERBATIM
  216. DEPENDS
  217. protoc
  218. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  219. ${PROTO_FILES}
  220. )
  221. add_custom_target(
  222. generate_cpp_protos
  223. DEPENDS
  224. ${PROTOBUF_CPP_GENERATED_SOURCES}
  225. )
  226. endif()
  227. # Custom target that runs a script to generate the proto sources. This isn't
  228. # hooked into the build, so must be run manually. (It would be easy enough to
  229. # hook into the (posix) cmake build, but for consistency with windows and xcode
  230. # builds, we require this to be run manually with the results checked into
  231. # source control.)
  232. if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
  233. add_custom_target(
  234. generate_protos
  235. DEPENDS
  236. generate_nanopb_protos
  237. generate_cpp_protos
  238. )
  239. endif()