CMakeLists.txt 7.5 KB

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