BUILD 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. load("@rules_proto//proto:defs.bzl", "proto_library")
  2. load(
  3. "//bazel:build_defs.bzl",
  4. "generated_file_staleness_test",
  5. "licenses", # copybara:strip_for_google3
  6. "lua_binary",
  7. "lua_cclibrary",
  8. "lua_library",
  9. "lua_test",
  10. "make_shell_script",
  11. "upb_amalgamation",
  12. )
  13. load(
  14. "//bazel:upb_proto_library.bzl",
  15. "upb_proto_library",
  16. "upb_proto_reflection_library",
  17. )
  18. licenses(["notice"]) # BSD (Google-authored w/ possible external contributions)
  19. exports_files([
  20. "LICENSE",
  21. "build_defs",
  22. ])
  23. CPPOPTS = [
  24. # copybara:strip_for_google3_begin
  25. "-Werror",
  26. "-Wno-long-long",
  27. # copybara:strip_end
  28. ]
  29. COPTS = CPPOPTS + [
  30. # copybara:strip_for_google3_begin
  31. "-pedantic",
  32. "-Wstrict-prototypes",
  33. # copybara:strip_end
  34. ]
  35. config_setting(
  36. name = "darwin",
  37. values = {"cpu": "darwin"},
  38. visibility = ["//visibility:public"],
  39. )
  40. config_setting(
  41. name = "windows",
  42. constraint_values = ["@bazel_tools//platforms:windows"],
  43. )
  44. config_setting(
  45. name = "fuzz",
  46. values = {"define": "fuzz=true"},
  47. )
  48. # Public C/C++ libraries #######################################################
  49. cc_library(
  50. name = "port",
  51. srcs = [
  52. "upb/port.c",
  53. ],
  54. textual_hdrs = [
  55. "upb/port_def.inc",
  56. "upb/port_undef.inc",
  57. ],
  58. )
  59. cc_library(
  60. name = "upb",
  61. srcs = [
  62. "upb/decode.c",
  63. "upb/encode.c",
  64. "upb/generated_util.h",
  65. "upb/msg.c",
  66. "upb/msg.h",
  67. "upb/table.c",
  68. "upb/table.int.h",
  69. "upb/upb.c",
  70. ],
  71. hdrs = [
  72. "upb/decode.h",
  73. "upb/encode.h",
  74. "upb/upb.h",
  75. ],
  76. copts = select({
  77. ":windows": [],
  78. "//conditions:default": COPTS,
  79. }),
  80. visibility = ["//visibility:public"],
  81. deps = [":port"],
  82. )
  83. # Common support routines used by generated code. This library has no
  84. # implementation, but depends on :upb and exposes a few more hdrs.
  85. #
  86. # This is public only because we have no way of visibility-limiting it to
  87. # upb_proto_library() only. This interface is not stable and by using it you
  88. # give up any backward compatibility guarantees.
  89. cc_library(
  90. name = "generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
  91. hdrs = [
  92. "upb/generated_util.h",
  93. "upb/msg.h",
  94. "upb/port_def.inc",
  95. "upb/port_undef.inc",
  96. ],
  97. copts = select({
  98. ":windows": [],
  99. "//conditions:default": COPTS,
  100. }),
  101. visibility = ["//visibility:public"],
  102. deps = [
  103. ":upb",
  104. ],
  105. )
  106. upb_proto_library(
  107. name = "descriptor_upbproto",
  108. visibility = ["//visibility:public"],
  109. deps = ["@com_google_protobuf//:descriptor_proto"],
  110. )
  111. cc_library(
  112. name = "reflection",
  113. srcs = [
  114. "upb/def.c",
  115. "upb/msgfactory.c",
  116. ],
  117. hdrs = [
  118. "upb/def.h",
  119. "upb/msgfactory.h",
  120. ],
  121. copts = select({
  122. ":windows": [],
  123. "//conditions:default": COPTS,
  124. }),
  125. visibility = ["//visibility:public"],
  126. deps = [
  127. ":descriptor_upbproto",
  128. ":port",
  129. ":table",
  130. ":upb",
  131. ],
  132. )
  133. # Internal C/C++ libraries #####################################################
  134. cc_library(
  135. name = "table",
  136. hdrs = ["upb/table.int.h"],
  137. deps = [
  138. ":port",
  139. ":upb",
  140. ],
  141. )
  142. # Legacy C/C++ Libraries (not recommended for new code) ########################
  143. cc_library(
  144. name = "legacy_msg_reflection",
  145. srcs = [
  146. "upb/legacy_msg_reflection.c",
  147. "upb/msg.h",
  148. ],
  149. hdrs = ["upb/legacy_msg_reflection.h"],
  150. copts = select({
  151. ":windows": [],
  152. "//conditions:default": COPTS,
  153. }),
  154. deps = [
  155. ":port",
  156. ":table",
  157. ":upb",
  158. ],
  159. )
  160. cc_library(
  161. name = "handlers",
  162. srcs = [
  163. "upb/handlers.c",
  164. "upb/handlers-inl.h",
  165. "upb/sink.c",
  166. ],
  167. hdrs = [
  168. "upb/handlers.h",
  169. "upb/sink.h",
  170. ],
  171. copts = select({
  172. ":windows": [],
  173. "//conditions:default": COPTS,
  174. }),
  175. deps = [
  176. ":port",
  177. ":reflection",
  178. ":table",
  179. ":upb",
  180. ],
  181. )
  182. cc_library(
  183. name = "upb_pb",
  184. srcs = [
  185. "upb/pb/compile_decoder.c",
  186. "upb/pb/decoder.c",
  187. "upb/pb/decoder.int.h",
  188. "upb/pb/encoder.c",
  189. "upb/pb/textprinter.c",
  190. "upb/pb/varint.c",
  191. "upb/pb/varint.int.h",
  192. ],
  193. hdrs = [
  194. "upb/pb/decoder.h",
  195. "upb/pb/encoder.h",
  196. "upb/pb/textprinter.h",
  197. ],
  198. copts = select({
  199. ":windows": [],
  200. "//conditions:default": COPTS,
  201. }),
  202. deps = [
  203. ":descriptor_upbproto",
  204. ":handlers",
  205. ":port",
  206. ":reflection",
  207. ":table",
  208. ":upb",
  209. ],
  210. )
  211. # copybara:strip_for_google3_begin
  212. cc_library(
  213. name = "upb_json",
  214. srcs = [
  215. "upb/json/parser.c",
  216. "upb/json/printer.c",
  217. ],
  218. hdrs = [
  219. "upb/json/parser.h",
  220. "upb/json/printer.h",
  221. ],
  222. copts = select({
  223. ":windows": [],
  224. "//conditions:default": COPTS,
  225. }),
  226. deps = [
  227. ":upb",
  228. ":upb_pb",
  229. ],
  230. )
  231. # copybara:strip_end
  232. cc_library(
  233. name = "upb_cc_bindings",
  234. hdrs = [
  235. "upb/bindings/stdc++/string.h",
  236. ],
  237. deps = [
  238. ":descriptor_upbproto",
  239. ":handlers",
  240. ":port",
  241. ":upb",
  242. ],
  243. )
  244. # upb compiler #################################################################
  245. cc_library(
  246. name = "upbc_generator",
  247. srcs = [
  248. "upbc/generator.cc",
  249. "upbc/message_layout.cc",
  250. "upbc/message_layout.h",
  251. ],
  252. hdrs = ["upbc/generator.h"],
  253. copts = select({
  254. ":windows": [],
  255. "//conditions:default": CPPOPTS,
  256. }),
  257. deps = [
  258. "@com_google_absl//absl/base:core_headers",
  259. "@com_google_absl//absl/container:flat_hash_map",
  260. "@com_google_absl//absl/strings",
  261. "@com_google_protobuf//:protobuf",
  262. "@com_google_protobuf//:protoc_lib",
  263. ],
  264. )
  265. cc_binary(
  266. name = "protoc-gen-upb",
  267. srcs = ["upbc/main.cc"],
  268. copts = select({
  269. ":windows": [],
  270. "//conditions:default": CPPOPTS,
  271. }),
  272. visibility = ["//visibility:public"],
  273. deps = [
  274. ":upbc_generator",
  275. "@com_google_protobuf//:protoc_lib",
  276. ],
  277. )
  278. # We strip the tests and remaining rules from google3 until the upb_proto_library()
  279. # and upb_proto_reflection_library() rules are fixed.
  280. # C/C++ tests ##################################################################
  281. upb_proto_reflection_library(
  282. name = "descriptor_upbreflection",
  283. deps = ["@com_google_protobuf//:descriptor_proto"],
  284. )
  285. cc_binary(
  286. name = "benchmark",
  287. testonly = 1,
  288. srcs = ["tests/benchmark.cc"],
  289. deps = [
  290. ":descriptor_upbproto",
  291. ":descriptor_upbreflection",
  292. "@com_github_google_benchmark//:benchmark_main",
  293. ],
  294. )
  295. cc_library(
  296. name = "upb_test",
  297. testonly = 1,
  298. srcs = [
  299. "tests/testmain.cc",
  300. ],
  301. hdrs = [
  302. "tests/test_util.h",
  303. "tests/upb_test.h",
  304. ],
  305. copts = select({
  306. ":windows": [],
  307. "//conditions:default": CPPOPTS,
  308. }),
  309. deps = [
  310. ":handlers",
  311. ":port",
  312. ":upb",
  313. ],
  314. )
  315. cc_test(
  316. name = "test_varint",
  317. srcs = [
  318. "tests/pb/test_varint.c",
  319. "upb/pb/varint.int.h",
  320. ],
  321. copts = select({
  322. ":windows": [],
  323. "//conditions:default": COPTS,
  324. }),
  325. deps = [
  326. ":port",
  327. ":upb",
  328. ":upb_pb",
  329. ":upb_test",
  330. ],
  331. )
  332. proto_library(
  333. name = "test_decoder_proto",
  334. srcs = [
  335. "tests/pb/test_decoder.proto",
  336. ],
  337. )
  338. upb_proto_reflection_library(
  339. name = "test_decoder_upbproto",
  340. deps = [":test_decoder_proto"],
  341. )
  342. cc_test(
  343. name = "test_decoder",
  344. srcs = [
  345. "tests/pb/test_decoder.cc",
  346. "upb/pb/varint.int.h",
  347. ],
  348. copts = select({
  349. ":windows": [],
  350. "//conditions:default": CPPOPTS,
  351. }),
  352. deps = [
  353. ":handlers",
  354. ":port",
  355. ":test_decoder_upbproto",
  356. ":upb",
  357. ":upb_pb",
  358. ":upb_test",
  359. ],
  360. )
  361. proto_library(
  362. name = "test_cpp_proto",
  363. srcs = [
  364. "tests/test_cpp.proto",
  365. ],
  366. )
  367. upb_proto_reflection_library(
  368. name = "test_cpp_upbproto",
  369. deps = ["test_cpp_proto"],
  370. )
  371. cc_test(
  372. name = "test_cpp",
  373. srcs = ["tests/test_cpp.cc"],
  374. copts = select({
  375. ":windows": [],
  376. "//conditions:default": CPPOPTS,
  377. }),
  378. deps = [
  379. ":handlers",
  380. ":port",
  381. ":reflection",
  382. ":test_cpp_upbproto",
  383. ":upb",
  384. ":upb_pb",
  385. ":upb_test",
  386. ],
  387. )
  388. cc_test(
  389. name = "test_table",
  390. srcs = ["tests/test_table.cc"],
  391. copts = select({
  392. ":windows": [],
  393. "//conditions:default": CPPOPTS,
  394. }),
  395. deps = [
  396. ":port",
  397. ":table",
  398. ":upb",
  399. ":upb_test",
  400. ],
  401. )
  402. # OSS-Fuzz test
  403. cc_binary(
  404. name = "file_descriptor_parsenew_fuzzer",
  405. testonly = 1,
  406. srcs = ["tests/file_descriptor_parsenew_fuzzer.cc"],
  407. copts = select({
  408. ":windows": [],
  409. "//conditions:default": CPPOPTS,
  410. }) + select({
  411. "//conditions:default": [],
  412. ":fuzz": ["-fsanitize=fuzzer,address"],
  413. }),
  414. defines = select({
  415. "//conditions:default": [],
  416. ":fuzz": ["HAVE_FUZZER"],
  417. }),
  418. deps = [
  419. ":descriptor_upbproto",
  420. ":upb",
  421. ],
  422. )
  423. # copybara:strip_for_google3_begin
  424. cc_test(
  425. name = "test_encoder",
  426. srcs = ["tests/pb/test_encoder.cc"],
  427. copts = select({
  428. ":windows": [],
  429. "//conditions:default": CPPOPTS,
  430. }),
  431. deps = [
  432. ":descriptor_upbproto",
  433. ":descriptor_upbreflection",
  434. ":upb",
  435. ":upb_cc_bindings",
  436. ":upb_pb",
  437. ":upb_test",
  438. ],
  439. )
  440. proto_library(
  441. name = "test_json_enum_from_separate",
  442. srcs = ["tests/json/enum_from_separate_file.proto"],
  443. deps = [":test_json_proto"],
  444. )
  445. proto_library(
  446. name = "test_json_proto",
  447. srcs = ["tests/json/test.proto"],
  448. )
  449. upb_proto_reflection_library(
  450. name = "test_json_upbprotoreflection",
  451. deps = ["test_json_proto"],
  452. )
  453. upb_proto_library(
  454. name = "test_json_enum_from_separate_upbproto",
  455. deps = [":test_json_enum_from_separate"],
  456. )
  457. upb_proto_library(
  458. name = "test_json_upbproto",
  459. deps = [":test_json_proto"],
  460. )
  461. cc_test(
  462. name = "test_json",
  463. srcs = [
  464. "tests/json/test_json.cc",
  465. ],
  466. copts = select({
  467. ":windows": [],
  468. "//conditions:default": CPPOPTS,
  469. }),
  470. deps = [
  471. ":test_json_upbproto",
  472. ":test_json_upbprotoreflection",
  473. ":upb_json",
  474. ":upb_test",
  475. ],
  476. )
  477. # copybara:strip_end
  478. upb_proto_library(
  479. name = "conformance_proto_upb",
  480. testonly = 1,
  481. deps = ["@com_google_protobuf//:conformance_proto"],
  482. )
  483. upb_proto_library(
  484. name = "test_messages_proto3_proto_upb",
  485. testonly = 1,
  486. deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
  487. )
  488. cc_binary(
  489. name = "conformance_upb",
  490. testonly = 1,
  491. srcs = [
  492. "tests/conformance_upb.c",
  493. ],
  494. copts = select({
  495. ":windows": [],
  496. "//conditions:default": COPTS,
  497. }) + ["-Ibazel-out/k8-fastbuild/bin"],
  498. deps = [
  499. ":conformance_proto_upb",
  500. ":test_messages_proto3_proto_upb",
  501. ":upb",
  502. ],
  503. )
  504. make_shell_script(
  505. name = "gen_test_conformance_upb",
  506. out = "test_conformance_upb.sh",
  507. contents = "external/com_google_protobuf/conformance_test_runner ./conformance_upb",
  508. )
  509. sh_test(
  510. name = "test_conformance_upb",
  511. srcs = ["test_conformance_upb.sh"],
  512. data = [
  513. "tests/conformance_upb_failures.txt",
  514. ":conformance_upb",
  515. "@com_google_protobuf//:conformance_test_runner",
  516. ],
  517. )
  518. # copybara:strip_for_google3_begin
  519. # Amalgamation #################################################################
  520. py_binary(
  521. name = "amalgamate",
  522. srcs = ["tools/amalgamate.py"],
  523. )
  524. upb_amalgamation(
  525. name = "gen_amalgamation",
  526. outs = [
  527. "upb.c",
  528. "upb.h",
  529. ],
  530. amalgamator = ":amalgamate",
  531. libs = [
  532. ":upb",
  533. ":descriptor_upbproto",
  534. ":reflection",
  535. ":handlers",
  536. ":port",
  537. ":upb_pb",
  538. ":upb_json",
  539. ],
  540. )
  541. cc_library(
  542. name = "amalgamation",
  543. srcs = ["upb.c"],
  544. hdrs = ["upb.h"],
  545. copts = select({
  546. ":windows": [],
  547. "//conditions:default": COPTS,
  548. }),
  549. )
  550. # Lua libraries. ###############################################################
  551. lua_cclibrary(
  552. name = "lua/upb_c",
  553. srcs = [
  554. "upb/bindings/lua/def.c",
  555. "upb/bindings/lua/msg.c",
  556. "upb/bindings/lua/upb.c",
  557. ],
  558. hdrs = [
  559. "upb/bindings/lua/upb.h",
  560. ],
  561. deps = [
  562. "legacy_msg_reflection",
  563. "upb",
  564. "upb_pb",
  565. ],
  566. )
  567. lua_library(
  568. name = "lua/upb",
  569. srcs = ["upb/bindings/lua/upb.lua"],
  570. luadeps = ["lua/upb_c"],
  571. strip_prefix = "upb/bindings/lua",
  572. )
  573. lua_cclibrary(
  574. name = "lua/upb/pb_c",
  575. srcs = ["upb/bindings/lua/upb/pb.c"],
  576. luadeps = ["lua/upb_c"],
  577. deps = ["upb_pb"],
  578. )
  579. lua_library(
  580. name = "lua/upb/pb",
  581. srcs = ["upb/bindings/lua/upb/pb.lua"],
  582. luadeps = [
  583. "lua/upb",
  584. "lua/upb/pb_c",
  585. ],
  586. strip_prefix = "upb/bindings/lua",
  587. )
  588. # Lua tests. ###################################################################
  589. lua_test(
  590. name = "lua/test_upb",
  591. luadeps = ["lua/upb"],
  592. luamain = "tests/bindings/lua/test_upb.lua",
  593. )
  594. lua_test(
  595. name = "lua/test_upb_pb",
  596. luadeps = ["lua/upb/pb"],
  597. luamain = "tests/bindings/lua/test_upb.pb.lua",
  598. )
  599. # Test the CMake build #########################################################
  600. filegroup(
  601. name = "cmake_files",
  602. srcs = glob([
  603. "CMakeLists.txt",
  604. "generated_for_cmake/**/*",
  605. "google/**/*",
  606. "upbc/**/*",
  607. "upb/**/*",
  608. "tests/**/*",
  609. ]),
  610. )
  611. make_shell_script(
  612. name = "gen_run_cmake_build",
  613. out = "run_cmake_build.sh",
  614. contents = "find . && mkdir build && cd build && cmake .. && make -j8 && make test",
  615. )
  616. sh_test(
  617. name = "cmake_build",
  618. srcs = ["run_cmake_build.sh"],
  619. data = [":cmake_files"],
  620. )
  621. # Generated files ##############################################################
  622. exports_files(["tools/staleness_test.py"])
  623. py_library(
  624. name = "staleness_test_lib",
  625. testonly = 1,
  626. srcs = ["tools/staleness_test_lib.py"],
  627. )
  628. py_binary(
  629. name = "make_cmakelists",
  630. srcs = ["tools/make_cmakelists.py"],
  631. )
  632. genrule(
  633. name = "gen_cmakelists",
  634. srcs = [
  635. "BUILD",
  636. "WORKSPACE",
  637. ":cmake_files",
  638. ],
  639. outs = ["generated-in/CMakeLists.txt"],
  640. cmd = "$(location :make_cmakelists) $@",
  641. tools = [":make_cmakelists"],
  642. )
  643. genrule(
  644. name = "generate_json_ragel",
  645. srcs = ["upb/json/parser.rl"],
  646. outs = ["upb/json/parser.c"],
  647. cmd = "$(location @ragel//:ragelc) -C -o upb/json/parser.c $< && mv upb/json/parser.c $@",
  648. tools = ["@ragel//:ragelc"],
  649. )
  650. genrule(
  651. name = "copy_json_ragel",
  652. srcs = ["upb/json/parser.c"],
  653. outs = ["generated-in/generated_for_cmake/upb/json/parser.c"],
  654. cmd = "cp $< $@",
  655. )
  656. genrule(
  657. name = "copy_protos",
  658. srcs = [":descriptor_upbproto"],
  659. outs = [
  660. "generated-in/generated_for_cmake/google/protobuf/descriptor.upb.c",
  661. "generated-in/generated_for_cmake/google/protobuf/descriptor.upb.h",
  662. ],
  663. cmd = "cp $(SRCS) $(@D)/generated-in/generated_for_cmake/google/protobuf",
  664. )
  665. generated_file_staleness_test(
  666. name = "test_generated_files",
  667. outs = [
  668. "CMakeLists.txt",
  669. "generated_for_cmake/google/protobuf/descriptor.upb.c",
  670. "generated_for_cmake/google/protobuf/descriptor.upb.h",
  671. "generated_for_cmake/upb/json/parser.c",
  672. ],
  673. generated_pattern = "generated-in/%s",
  674. )
  675. # copybara:strip_end