CMakeLists.txt 7.5 KB

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