CMakeLists.txt 7.5 KB

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