CMakeLists.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. firestore/bundle
  34. google/api/annotations
  35. google/api/http
  36. google/firestore/v1/common
  37. google/firestore/v1/document
  38. google/firestore/v1/firestore
  39. google/firestore/v1/query
  40. google/firestore/v1/write
  41. google/rpc/status
  42. google/type/latlng
  43. )
  44. # Full filenames (i.e. with the .proto) from within the proto directory,
  45. # excluding anything in google/protobuf
  46. foreach(root ${PROTO_FILE_ROOTS})
  47. list(
  48. APPEND PROTO_FILES
  49. ${OUTPUT_DIR}/protos/${root}.proto
  50. )
  51. if(EXISTS ${OUTPUT_DIR}/protos/${root}.options)
  52. list(
  53. APPEND PROTO_FILES_OPTIONS
  54. ${OUTPUT_DIR}/protos/${root}.options
  55. )
  56. endif()
  57. endforeach()
  58. # Filename "roots" (i.e. without the .proto) from within the proto directory,
  59. # from the google/protobuf package.
  60. set(
  61. WELL_KNOWN_PROTO_FILE_ROOTS
  62. google/protobuf/any
  63. google/protobuf/empty
  64. google/protobuf/struct
  65. google/protobuf/timestamp
  66. google/protobuf/wrappers
  67. )
  68. # Full filenames (i.e. with the .proto) from within the proto directory, from
  69. # the google/protobuf package.
  70. foreach(root ${WELL_KNOWN_PROTO_FILE_ROOTS})
  71. list(
  72. APPEND WELL_KNOWN_PROTO_FILES
  73. ${OUTPUT_DIR}/protos/${root}.proto
  74. )
  75. endforeach()
  76. # Populate NANOPB_GENERATED_SOURCES with the list of nanopb-generated sources.
  77. # The nanopb runtime does not include the well-known protos so we have to build
  78. # them ourselves.
  79. foreach(root ${PROTO_FILE_ROOTS} ${WELL_KNOWN_PROTO_FILE_ROOTS})
  80. list(
  81. APPEND NANOPB_GENERATED_SOURCES
  82. ${OUTPUT_DIR}/nanopb/${root}.nanopb.cc
  83. ${OUTPUT_DIR}/nanopb/${root}.nanopb.h
  84. )
  85. endforeach()
  86. # Populate PROTOBUF_CPP_GENERATED_SOURCES with the list of libprotobuf C++
  87. # sources. These are used for verifying interoperation from nanopb.
  88. #
  89. # Libprotobuf includes the well-known protos so they must be omitted here.
  90. foreach(root ${PROTO_FILE_ROOTS})
  91. list(
  92. APPEND PROTOBUF_CPP_GENERATED_SOURCES
  93. ${OUTPUT_DIR}/cpp/${root}.pb.cc
  94. ${OUTPUT_DIR}/cpp/${root}.pb.h
  95. )
  96. endforeach()
  97. firebase_ios_add_library(
  98. firestore_protos_nanopb DISABLE_STRICT_WARNINGS EXCLUDE_FROM_ALL
  99. ${NANOPB_GENERATED_SOURCES}
  100. )
  101. target_include_directories(
  102. firestore_protos_nanopb PUBLIC
  103. ${CMAKE_CURRENT_LIST_DIR}/nanopb
  104. )
  105. target_link_libraries(
  106. firestore_protos_nanopb PUBLIC
  107. firestore_nanopb
  108. protobuf-nanopb-static
  109. absl_strings
  110. )
  111. # libprotobuf based generated protos. Expected only to be used in test (as
  112. # libprotobuf[-lite] is too large; we're using nanopb instead. But we do want
  113. # to test our serialization logic against libprotobuf.)
  114. firebase_ios_add_library(
  115. firestore_protos_protobuf DISABLE_STRICT_WARNINGS EXCLUDE_FROM_ALL
  116. ${PROTOBUF_CPP_GENERATED_SOURCES}
  117. )
  118. target_include_directories(
  119. firestore_protos_protobuf PUBLIC
  120. ${CMAKE_CURRENT_LIST_DIR}/cpp
  121. )
  122. target_link_libraries(
  123. firestore_protos_protobuf PUBLIC
  124. protobuf::libprotobuf
  125. )
  126. # Generate the python representation of descriptor.proto.
  127. set(PROTOBUF_DIR ${FIREBASE_EXTERNAL_SOURCE_DIR}/protobuf)
  128. set(PROTOBUF_PROTO ${PROTOBUF_DIR}/src/google/protobuf/descriptor.proto)
  129. set(PROTOBUF_PYTHON ${PROTOBUF_DIR}/python/google/protobuf/descriptor_pb2.py)
  130. add_custom_command(
  131. COMMENT "Generating protoc python plugins"
  132. OUTPUT ${PROTOBUF_PYTHON}
  133. COMMAND
  134. protoc
  135. -I${PROTOBUF_DIR}/src
  136. --python_out=${PROTOBUF_DIR}/python
  137. ${PROTOBUF_PROTO}
  138. VERBATIM
  139. DEPENDS
  140. protoc
  141. ${PROTOBUF_PROTO}
  142. )
  143. # Generate the python representation of nanopb's protos
  144. set(NANOPB_DIR ${FIREBASE_BINARY_DIR}/external/src/nanopb)
  145. set(
  146. NANOPB_PROTO
  147. ${NANOPB_DIR}/generator/proto/nanopb.proto
  148. ${NANOPB_DIR}/generator/proto/plugin.proto
  149. )
  150. set(
  151. NANOPB_PYTHON
  152. ${NANOPB_DIR}/generator/proto/nanopb_pb2.py
  153. ${NANOPB_DIR}/generator/proto/plugin_pb2.py
  154. )
  155. set(
  156. PROTO_INCLUDES
  157. -I${CMAKE_CURRENT_SOURCE_DIR}/protos
  158. -I${NANOPB_DIR}/generator
  159. -I${PROTOBUF_DIR}/src
  160. )
  161. add_custom_command(
  162. COMMENT "Generating nanopb python plugins"
  163. OUTPUT ${NANOPB_PYTHON}
  164. COMMAND
  165. protoc
  166. -I${NANOPB_DIR}/generator
  167. -I${PROTOBUF_DIR}/src
  168. --python_out=${NANOPB_DIR}/generator
  169. ${NANOPB_PROTO}
  170. VERBATIM
  171. DEPENDS
  172. protoc
  173. ${NANOPB_PROTO}
  174. )
  175. if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
  176. add_custom_command(
  177. COMMENT "Generating nanopb sources"
  178. OUTPUT ${NANOPB_GENERATED_SOURCES}
  179. COMMAND
  180. python
  181. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  182. --nanopb
  183. --protoc=$<TARGET_FILE:protoc>
  184. --pythonpath=${PROTOBUF_DIR}/python:${NANOPB_DIR}/generator
  185. --output_dir=${OUTPUT_DIR}
  186. ${PROTO_INCLUDES}
  187. VERBATIM
  188. DEPENDS
  189. protoc
  190. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  191. ${CMAKE_CURRENT_SOURCE_DIR}/nanopb_cpp_generator.py
  192. ${CMAKE_CURRENT_SOURCE_DIR}/lib/pretty_printing.py
  193. ${NANOPB_PYTHON}
  194. ${PROTOBUF_PYTHON}
  195. ${PROTO_FILES}
  196. ${PROTO_FILES_OPTIONS}
  197. ${WELL_KNOWN_PROTO_FILES}
  198. )
  199. add_custom_target(
  200. generate_nanopb_protos
  201. DEPENDS
  202. ${NANOPB_GENERATED_SOURCES}
  203. )
  204. endif()
  205. if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
  206. add_custom_command(
  207. COMMENT "Generating C++ protobuf sources"
  208. OUTPUT ${PROTOBUF_CPP_GENERATED_SOURCES}
  209. COMMAND
  210. python
  211. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  212. --cpp
  213. --protoc=$<TARGET_FILE:protoc>
  214. --output_dir=${OUTPUT_DIR}
  215. ${PROTO_INCLUDES}
  216. VERBATIM
  217. DEPENDS
  218. protoc
  219. ${CMAKE_CURRENT_SOURCE_DIR}/build_protos.py
  220. ${PROTO_FILES}
  221. )
  222. add_custom_target(
  223. generate_cpp_protos
  224. DEPENDS
  225. ${PROTOBUF_CPP_GENERATED_SOURCES}
  226. )
  227. endif()
  228. # Custom target that runs a script to generate the proto sources. This isn't
  229. # hooked into the build, so must be run manually. (It would be easy enough to
  230. # hook into the (posix) cmake build, but for consistency with windows and xcode
  231. # builds, we require this to be run manually with the results checked into
  232. # source control.)
  233. if(FIREBASE_IOS_PROTOC_GENERATE_SOURCES)
  234. add_custom_target(
  235. generate_protos
  236. DEPENDS
  237. generate_nanopb_protos
  238. generate_cpp_protos
  239. )
  240. endif()