BUILD 15 KB

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