blob: 009318429d1d85878793a85def251bb5ee69ab7a [file] [log] [blame]
Austin Schuh8f99c822024-05-05 22:43:40 -07001load("@com_github_google_flatbuffers//:build_defs.bzl", _flatbuffer_rust_library = "flatbuffer_rust_library")
Adam Snaider0967b812023-11-02 15:29:43 -07002load(
3 "@rules_rust//rust:defs.bzl",
4 _rust_binary = "rust_binary",
5 _rust_doc = "rust_doc",
6 _rust_doc_test = "rust_doc_test",
7 _rust_library = "rust_library",
8 _rust_test = "rust_test",
9)
Adam Snaider0967b812023-11-02 15:29:43 -070010
Adam Snaider8215ac82024-03-04 13:53:31 -050011def rust_doc_test(tags = [], **kwargs):
Adam Snaider0967b812023-11-02 15:29:43 -070012 # TODO(james): Attempting to execute this remotely results
13 # in complaints about overly large files.
14 _rust_doc_test(
15 tags = tags + ["no-remote-exec"],
Adam Snaider8215ac82024-03-04 13:53:31 -050016 # TODO(adam.snaider): Investigate why doctests only work on x86_64.
17 target_compatible_with = ["@platforms//cpu:x86_64"],
Adam Snaider0967b812023-11-02 15:29:43 -070018 **kwargs
19 )
20
Adam Snaiderb40b72f2023-11-02 19:40:55 -070021def rust_doc(target_compatible_with = ["//tools/platforms/rust:has_support"], rustdoc_flags = ["-Dwarnings"], **kwargs):
Adam Snaider0967b812023-11-02 15:29:43 -070022 _rust_doc(
23 target_compatible_with = target_compatible_with,
Adam Snaiderb40b72f2023-11-02 19:40:55 -070024 rustdoc_flags = rustdoc_flags,
Adam Snaider0967b812023-11-02 15:29:43 -070025 **kwargs
26 )
27
Adam Snaider5f000672023-11-02 16:04:30 -070028def rust_binary(target_compatible_with = ["//tools/platforms/rust:has_support"], rustc_flags = [], **kwargs):
29 _rust_binary(
30 target_compatible_with = select({
31 Label("//conditions:default"): target_compatible_with,
32 Label("//tools:has_msan"): ["@platforms//:incompatible"],
33 }),
34 # TODO: Make Rust play happy with pic vs nopic. Details at:
35 # https://github.com/bazelbuild/rules_rust/issues/118
36 rustc_flags = rustc_flags + ["-Crelocation-model=static"],
37 **kwargs
38 )
39
Adam Snaider5f000672023-11-02 16:04:30 -070040def rust_test(target_compatible_with = ["//tools/platforms/rust:has_support"], rustc_flags = [], **kwargs):
41 _rust_test(
42 target_compatible_with = select({
43 Label("//conditions:default"): target_compatible_with,
44 Label("//tools:has_msan"): ["@platforms//:incompatible"],
45 }),
46 rustc_flags = rustc_flags + ["-Crelocation-model=static"],
47 **kwargs
48 )
49
Adam Snaiderf560ae92023-11-07 17:06:21 -080050def rust_library(
51 name,
52 target_compatible_with = ["//tools/platforms/rust:has_support"],
53 gen_docs = True,
54 gen_tests = True,
55 gen_doctests = True,
56 **kwargs):
57 test_params = {}
58 doctest_params = {}
59 params = {}
60
61 for (param, value) in kwargs.items():
62 if param.startswith("test_"):
63 test_params[param[5:]] = value
64 elif param.startswith("doctest_"):
65 doctest_params[param[8:]] = value
66 else:
67 params[param] = value
68
69 _rust_library(
70 name = name,
71 target_compatible_with = select({
72 Label("//conditions:default"): target_compatible_with,
73 Label("//tools:has_msan"): ["@platforms//:incompatible"],
74 }),
75 **params
76 )
77
78 if gen_tests:
79 rust_test(
80 name = name + "_test",
81 crate = name,
82 **test_params
83 )
84
85 if gen_docs:
86 rust_doc(
87 name = name + "_doc",
88 crate = name,
89 target_compatible_with = ["//tools/platforms/rust:has_support"],
90 rustdoc_flags = ["--document-private-items", "-Dwarnings"],
91 )
92
93 if gen_doctests:
94 rust_doc_test(
95 name = name + "_doctest",
96 crate = name,
97 **doctest_params
98 )
99
Adam Snaider0967b812023-11-02 15:29:43 -0700100def flatbuffer_rust_library(target_compatible_with = ["//tools/platforms/rust:has_support"], **kwargs):
101 _flatbuffer_rust_library(
102 target_compatible_with = select({
103 Label("//conditions:default"): target_compatible_with,
104 Label("//tools:has_msan"): ["@platforms//:incompatible"],
105 }),
106 **kwargs
107 )