Skip to content

Commit b082688

Browse files
Merge pull request #6 from dfinity-lab/gl/json-output
Gl/json output
2 parents 7f2dd43 + 74e8832 commit b082688

File tree

14 files changed

+209
-74
lines changed

14 files changed

+209
-74
lines changed

BUILD.bazel

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
load("//rust:defs.bzl", "capture_clippy_output", "clippy_flags", "error_format", "extra_exec_rustc_flag", "extra_exec_rustc_flags", "extra_rustc_flag", "extra_rustc_flags", "is_proc_macro_dep", "is_proc_macro_dep_enabled")
2+
load(
3+
"//rust:defs.bzl",
4+
"capture_clippy_output",
5+
"clippy_flags",
6+
"error_format",
7+
"extra_exec_rustc_flag",
8+
"extra_exec_rustc_flags",
9+
"extra_rustc_flag",
10+
"extra_rustc_flags",
11+
"is_proc_macro_dep",
12+
"is_proc_macro_dep_enabled",
13+
"output_diagnostics",
14+
"source_path_prefix",
15+
)
316

417
exports_files(["LICENSE"])
518

@@ -18,6 +31,13 @@ error_format(
1831
visibility = ["//visibility:public"],
1932
)
2033

34+
# This setting may be changed from the command line to generate rustc diagnostics.
35+
output_diagnostics(
36+
name = "output_diagnostics",
37+
build_setting_default = False,
38+
visibility = ["//visibility:public"],
39+
)
40+
2141
# This setting may be used to pass extra options to clippy from the command line.
2242
# It applies across all targets.
2343
clippy_flags(
@@ -76,6 +96,15 @@ extra_exec_rustc_flag(
7696
visibility = ["//visibility:public"],
7797
)
7898

99+
# This setting may be used to override the prefix for source paths rustc embeds into build artifacts.
100+
# Setting this setting to a fixed value (e.g., "/source/") can help achieve reproducible builds and
101+
# improve cache utilization.
102+
source_path_prefix(
103+
name = "source_path_prefix",
104+
build_setting_default = ".",
105+
visibility = ["//visibility:public"],
106+
)
107+
79108
# This setting is used by the clippy rules. See https://bazelbuild.github.io/rules_rust/rust_clippy.html
80109
label_flag(
81110
name = "clippy.toml",

examples/crate_universe/cargo_aliases/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ value-bag = "=1.0.0-alpha.7"
1717
names = "=0.13.0"
1818
pinned_names = { package = "names", git = "https://github.com/fnichol/names.git", rev = "760516503b89ddc8bc2ab42d579d4566cfb1054f" }
1919

20+
wasmtime-runtime = "0.37.0"
21+
2022
[dev-dependencies]
2123
env_logger = "0.9.0"

rust/defs.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ load(
4949
_extra_rustc_flags = "extra_rustc_flags",
5050
_is_proc_macro_dep = "is_proc_macro_dep",
5151
_is_proc_macro_dep_enabled = "is_proc_macro_dep_enabled",
52+
_output_diagnostics = "output_diagnostics",
53+
_source_path_prefix = "source_path_prefix",
5254
)
5355
load(
5456
"//rust/private:rustdoc.bzl",
@@ -103,6 +105,9 @@ rust_clippy = _rust_clippy
103105
capture_clippy_output = _capture_clippy_output
104106
# See @rules_rust//rust/private:clippy.bzl for a complete description.
105107

108+
output_diagnostics = _output_diagnostics
109+
# See @rules_rust//rust/private:rustc.bzl for a complete description.
110+
106111
error_format = _error_format
107112
# See @rules_rust//rust/private:rustc.bzl for a complete description.
108113

@@ -124,6 +129,9 @@ is_proc_macro_dep = _is_proc_macro_dep
124129
is_proc_macro_dep_enabled = _is_proc_macro_dep_enabled
125130
# See @rules_rust//rust/private:rustc.bzl for a complete description.
126131

132+
source_path_prefix = _source_path_prefix
133+
# See @rules_rust//rust/private:rustc.bzl for a complete description.
134+
127135
rust_common = _rust_common
128136
# See @rules_rust//rust/private:common.bzl for a complete description.
129137

rust/private/common.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def _create_crate_info(**kwargs):
5151
kwargs.update({"wrapped_crate_type": None})
5252
if not "metadata" in kwargs:
5353
kwargs.update({"metadata": None})
54+
if not "rust_metadata_rustc_output" in kwargs:
55+
kwargs.update({"rust_metadata_rustc_output": None})
56+
if not "rust_lib_rustc_output" in kwargs:
57+
kwargs.update({"rust_lib_rustc_output": None})
5458
if not "rustc_env_files" in kwargs:
5559
kwargs.update({"rustc_env_files": []})
5660
return CrateInfo(**kwargs)

rust/private/providers.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ CrateInfo = provider(
2323
"deps": "depset[DepVariantInfo]: This crate's (rust or cc) dependencies' providers.",
2424
"edition": "str: The edition of this crate.",
2525
"is_test": "bool: If the crate is being compiled in a test context",
26-
"metadata": "File: The rmeta file produced for this crate. It is optional.",
26+
"metadata": "File: The output from rustc from producing the output file. It is optional.",
27+
"rust_metadata_rustc_output": "File: The rmeta file produced for this crate. It is optional.",
2728
"name": "str: The name of this crate.",
2829
"output": "File: The output File that will be produced, depends on crate type.",
30+
"rust_lib_rustc_output": "File: The output from rustc from producing the output file. It is optional.",
2931
"owner": "Label: The label of the target that produced this CrateInfo",
3032
"proc_macro_deps": "depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers.",
3133
"root": "File: The source File entrypoint to this crate, eg. lib.rs",

rust/private/rust.bzl

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def _assert_correct_dep_mapping(ctx):
6565
),
6666
)
6767

68+
def _rustc_output_name(name):
69+
return name + ".rustc-output"
70+
6871
def _determine_lib_name(name, crate_type, toolchain, lib_hash = None):
6972
"""See https://github.com/bazelbuild/rules_rust/issues/405
7073
@@ -276,14 +279,24 @@ def _rust_library_common(ctx, crate_type):
276279
toolchain,
277280
output_hash,
278281
)
282+
279283
rust_lib = ctx.actions.declare_file(rust_lib_name)
284+
rust_lib_build_output = None
285+
output_diagnostics = ctx.attr._output_diagnostics
286+
if ctx.attr._process_wrapper and output_diagnostics:
287+
rust_lib_build_output = ctx.actions.declare_file(_rustc_output_name(rust_lib_name))
280288

281289
rust_metadata = None
290+
rust_metadata_build_output = None
282291
if can_build_metadata(toolchain, ctx, crate_type) and not ctx.attr.disable_pipelining:
292+
rust_metadata_name = paths.replace_extension(rust_lib_name, ".rmeta")
293+
283294
rust_metadata = ctx.actions.declare_file(
284-
paths.replace_extension(rust_lib_name, ".rmeta"),
295+
rust_metadata_name,
285296
sibling = rust_lib,
286297
)
298+
if output_diagnostics:
299+
rust_metadata_build_output = ctx.actions.declare_file(_rustc_output_name(rust_metadata_name))
287300

288301
deps = transform_deps(ctx.attr.deps)
289302
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
@@ -301,7 +314,9 @@ def _rust_library_common(ctx, crate_type):
301314
proc_macro_deps = depset(proc_macro_deps),
302315
aliases = ctx.attr.aliases,
303316
output = rust_lib,
317+
rust_lib_rustc_output = rust_lib_build_output,
304318
metadata = rust_metadata,
319+
rust_metadata_rustc_output = rust_metadata_build_output,
305320
edition = get_edition(ctx.attr, toolchain, ctx.label),
306321
rustc_env = ctx.attr.rustc_env,
307322
rustc_env_files = ctx.files.rustc_env_files,
@@ -607,7 +622,7 @@ _common_attrs = {
607622
The order that these files will be processed is unspecified, so
608623
multiple definitions of a particular variable are discouraged.
609624
610-
Note that the variables here are subject to
625+
Note that the variables here are subject to
611626
[workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status)
612627
stamping should the `stamp` attribute be enabled. Stamp variables
613628
should be wrapped in brackets in order to be resolved. E.g.
@@ -620,7 +635,7 @@ _common_attrs = {
620635
List of compiler flags passed to `rustc`.
621636
622637
These strings are subject to Make variable expansion for predefined
623-
source/output path variables like `$location`, `$execpath`, and
638+
source/output path variables like `$location`, `$execpath`, and
624639
`$rootpath`. This expansion is useful if you wish to pass a generated
625640
file of arguments to rustc: `@$(location //package:target)`.
626641
"""),
@@ -660,6 +675,9 @@ _common_attrs = {
660675
"_error_format": attr.label(
661676
default = Label("//:error_format"),
662677
),
678+
"_output_diagnostics": attr.label(
679+
default = Label("//:output_diagnostics"),
680+
),
663681
"_extra_exec_rustc_flag": attr.label(
664682
default = Label("//:extra_exec_rustc_flag"),
665683
),
@@ -672,6 +690,9 @@ _common_attrs = {
672690
"_extra_rustc_flags": attr.label(
673691
default = Label("//:extra_rustc_flags"),
674692
),
693+
"_source_path_prefix": attr.label(
694+
default = Label("//:source_path_prefix"),
695+
),
675696
"_import_macro_dep": attr.label(
676697
default = Label("//util/import"),
677698
cfg = "exec",
@@ -732,7 +753,7 @@ _rust_test_attrs = dict({
732753
mandatory = False,
733754
default = True,
734755
doc = dedent("""\
735-
Whether to use `libtest`. For targets using this flag, individual tests can be run by using the
756+
Whether to use `libtest`. For targets using this flag, individual tests can be run by using the
736757
[--test_arg](https://docs.bazel.build/versions/4.0.0/command-line-reference.html#flag--test_arg) flag.
737758
E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`.
738759
"""),

rust/private/rust_analyzer.bzl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,12 @@ def _create_single_crate(ctx, info):
188188
"include_dirs": [crate_root, _EXEC_ROOT_TEMPLATE + out_dir_path],
189189
}
190190

191+
# We deduplicate the entries from each of the additional targets to work around
192+
# https://github.com/bazelbuild/bazel/issues/16664
193+
#
191194
# TODO: The only imagined use case is an env var holding a filename in the workspace passed to a
192195
# macro like include_bytes!. Other use cases might exist that require more complex logic.
193-
expand_targets = concat([getattr(ctx.rule.attr, attr, []) for attr in ["data", "compile_data"]])
194-
196+
expand_targets = _deduplicate(_concat([getattr(ctx.rule.attr, attr, []) for attr in ["data", "compile_data"]]))
195197
crate["env"].update({k: dedup_expand_location(ctx, v, expand_targets) for k, v in info.env.items()})
196198

197199
# Omit when a crate appears to depend on itself (e.g. foo_test crates).
@@ -211,6 +213,12 @@ def _create_single_crate(ctx, info):
211213
crate["proc_macro_dylib_path"] = _EXEC_ROOT_TEMPLATE + info.proc_macro_dylib_path
212214
return crate
213215

216+
def _deduplicate(xs):
217+
return {x: True for x in xs}.keys()
218+
219+
def _concat(xss):
220+
return [x for xs in xss for x in xs]
221+
214222
def _rust_analyzer_toolchain_impl(ctx):
215223
toolchain = platform_common.ToolchainInfo(
216224
proc_macro_srv = ctx.executable.proc_macro_srv,

0 commit comments

Comments
 (0)