Skip to content

Commit 4243769

Browse files
m3rcurieldamienmg
authored andcommitted
Add linker_script option to rust_binary (#208)
This explicitly adds a linker_script option to rust_binary as suggested in the bottom of #204. It's fundamentally identical to adding the ldscript to data and then adding --codegen=link-args=-T$(location //:ldscript) to rustc_flags, but now without the hassle of figuring that out yourself.
1 parent 5fa9b10 commit 4243769

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

rust/private/rust.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,19 @@ INFO: Elapsed time: 1.245s, Critical Path: 1.01s
354354
""",
355355
)
356356

357+
_rust_binary_attrs = {
358+
"linker_script": attr.label(
359+
doc = _tidy("""
360+
Link script to forward into linker via rustc options.
361+
"""),
362+
cfg = "host",
363+
allow_single_file = True,
364+
),
365+
}
366+
357367
rust_binary = rule(
358368
_rust_binary_impl,
359-
attrs = _rust_common_attrs,
369+
attrs = dict(_rust_common_attrs.items() + _rust_binary_attrs.items()),
360370
executable = True,
361371
fragments = ["cpp"],
362372
host_fragments = ["cpp"],

rust/private/rustc.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,16 @@ def rustc_compile_action(
182182
toolchain,
183183
)
184184

185+
linker_script = getattr(ctx.file, "linker_script") if hasattr(ctx.file, "linker_script") \
186+
else None
187+
185188
compile_inputs = depset(
186189
crate_info.srcs +
187190
getattr(ctx.files, "data", []) +
188191
dep_info.transitive_libs +
189192
[toolchain.rustc] +
190-
toolchain.crosstool_files,
193+
toolchain.crosstool_files +
194+
([] if linker_script == None else [linker_script]),
191195
transitive = [
192196
toolchain.rustc_lib.files,
193197
toolchain.rust_lib.files,
@@ -214,6 +218,8 @@ def rustc_compile_action(
214218
args.add("--target=" + toolchain.target_triple)
215219
if hasattr(ctx.attr, "crate_features"):
216220
args.add_all(getattr(ctx.attr, "crate_features"), before_each = "--cfg", format_each = 'feature="%s"')
221+
if hasattr(ctx.attr, "linker_script") and linker_script != None:
222+
args.add(linker_script.path, format = "--codegen=link-arg=-T%s")
217223
args.add_all(rust_flags)
218224
args.add_all(getattr(ctx.attr, "rustc_flags", []))
219225
add_edition_flags(args, crate_info)

0 commit comments

Comments
 (0)