Skip to content

Commit 909fe03

Browse files
authored
Add mojo fixit support (#75)
This is experimental but can be enabled with `--@rules_mojo//:experimental_export_fixits --output_groups=+mojo_fixits` and then you can process the produced yaml files with clang-apply-replacements
1 parent a31bec6 commit 909fe03

File tree

7 files changed

+60
-2
lines changed

7 files changed

+60
-2
lines changed

BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
12
load("//mojo:build_settings.bzl", "repeatable_string_flag")
23

34
repeatable_string_flag(
@@ -21,3 +22,9 @@ toolchain_type(
2122
name = "gpu_toolchain_type",
2223
visibility = ["//visibility:public"],
2324
)
25+
26+
bool_flag(
27+
name = "experimental_export_fixits",
28+
build_setting_default = False,
29+
visibility = ["//visibility:public"],
30+
)

mojo/mojo_library.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@ def _mojo_library_implementation(ctx):
3232
if not file.dirname.startswith(root_directory):
3333
args.add_all([file], map_each = _format_include)
3434

35+
output_group_kwargs = {}
36+
package_outputs = [mojo_package]
37+
if ctx.attr._export_fixits[BuildSettingInfo].value:
38+
fixits_file = ctx.actions.declare_file(ctx.label.name + ".mojo_fixits.yaml")
39+
package_outputs.append(fixits_file)
40+
output_group_kwargs["mojo_fixits"] = depset([fixits_file])
41+
args.add("--experimental-export-fixit", fixits_file)
42+
3543
file_args.add_all(transitive_mojopkgs, map_each = _format_include)
3644
file_args.add(root_directory)
3745
ctx.actions.run(
3846
executable = mojo_toolchain.mojo,
3947
inputs = depset(ctx.files.srcs + ctx.files.additional_compiler_inputs, transitive = [transitive_mojopkgs]),
4048
tools = mojo_toolchain.all_tools,
41-
outputs = [mojo_package],
49+
outputs = package_outputs,
4250
arguments = [args, file_args],
4351
mnemonic = "MojoPackage",
4452
progress_message = "%{label} building mojo package",
@@ -67,6 +75,7 @@ def _mojo_library_implementation(ctx):
6775
import_paths = depset([mojo_package.dirname], transitive = [import_paths]),
6876
mojopkgs = depset([mojo_package], transitive = [transitive_mojopkgs]),
6977
),
78+
OutputGroupInfo(**output_group_kwargs),
7079
]
7180

7281
mojo_library = rule(
@@ -104,6 +113,9 @@ package' since it does not accept many flags.
104113
"_mojo_package_copts": attr.label(
105114
default = Label("//:mojo_package_copt"),
106115
),
116+
"_export_fixits": attr.label(
117+
default = Label("@rules_mojo//:experimental_export_fixits"),
118+
),
107119
},
108120
toolchains = ["//:toolchain_type"],
109121
)

mojo/private/mojo_binary_test.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ This is useful for shared sanitizer libraries which need to have rpaths added
5252
by bazel.
5353
""",
5454
),
55+
"_export_fixits": attr.label(
56+
default = Label("@rules_mojo//:experimental_export_fixits"),
57+
),
5558
}
5659

5760
_TOOLCHAINS = use_cpp_toolchain() + [
@@ -128,11 +131,19 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
128131
if ctx.attr.enable_assertions:
129132
args.add("-D", "ASSERT=all")
130133

134+
output_group_kwargs = {}
135+
compile_outputs = [object_file]
136+
if ctx.attr._export_fixits[BuildSettingInfo].value:
137+
fixits_file = ctx.actions.declare_file(ctx.label.name + ".mojo_fixits.yaml")
138+
compile_outputs.append(fixits_file)
139+
output_group_kwargs["mojo_fixits"] = depset([fixits_file])
140+
args.add("--experimental-export-fixit", fixits_file)
141+
131142
ctx.actions.run(
132143
executable = mojo_toolchain.mojo,
133144
tools = mojo_toolchain.all_tools,
134145
inputs = depset(ctx.files.srcs + ctx.files.additional_compiler_inputs, transitive = [transitive_mojopkgs]),
135-
outputs = [object_file],
146+
outputs = compile_outputs,
136147
arguments = [args],
137148
mnemonic = "MojoCompile",
138149
progress_message = "%{label} compiling mojo object",
@@ -267,6 +278,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
267278
]),
268279
),
269280
),
281+
OutputGroupInfo(**output_group_kwargs),
270282
]
271283
else:
272284
return [
@@ -277,6 +289,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
277289
RunEnvironmentInfo(
278290
environment = runtime_env,
279291
),
292+
OutputGroupInfo(**output_group_kwargs),
280293
]
281294

282295
mojo_binary = rule(

tests/fixits/BUILD.bazel

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("//mojo:mojo_binary.bzl", "mojo_binary")
2+
load("//mojo:mojo_library.bzl", "mojo_library")
3+
4+
# TODO: Find a fixit that doesn't fail the build, verify the output file
5+
6+
mojo_binary(
7+
name = "fixits",
8+
srcs = ["fixits.mojo"],
9+
tags = ["manual"],
10+
)
11+
12+
mojo_library(
13+
name = "package",
14+
srcs = [
15+
"package/__init__.mojo",
16+
"package/package.mojo",
17+
],
18+
tags = ["manual"],
19+
)

tests/fixits/fixits.mojo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn old_origin_of[T: AnyType](b: T):
2+
_ = __origin_of(b)
3+
4+
fn main():
5+
pass

tests/fixits/package/__init__.mojo

Whitespace-only changes.

tests/fixits/package/package.mojo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fn old_origin_of[T: AnyType](b: T):
2+
_ = __origin_of(b)

0 commit comments

Comments
 (0)