55load ("//rules:rv.bzl" , "rv_rule" )
66load ("@rules_cc//cc:find_cc_toolchain.bzl" , "find_cc_toolchain" )
77
8- def _get_assembler (cc_toolchain ):
9- """Find the path to riscv-unknown-elf-as."""
10-
11- # Note: the toolchain config doesn"t appear to have a good way to get
12- # access to the assembler. We should be able to access it via the
13- # the compiler, but I had trouble with //hw/ip/otbn/util/otbn_as.py invoking
14- # the compiler as assembler.
15- return [f for f in cc_toolchain .all_files .to_list () if f .basename .endswith ("as" )][0 ]
16-
178def _otbn_assemble_sources (ctx , additional_srcs = []):
189 """Helper function that, for each source file in the provided context, adds
1910 an action to the context that invokes the otbn assember (otbn_as.py),
2011 producing a corresponding object file. Returns a list of all object files
2112 that will be generated by these actions.
2213 """
2314 cc_toolchain = find_cc_toolchain (ctx )
24- assembler = _get_assembler (cc_toolchain )
2515
2616 objs = []
2717 for src in ctx .files .srcs + additional_srcs :
@@ -33,7 +23,7 @@ def _otbn_assemble_sources(ctx, additional_srcs = []):
3323 cc_toolchain .all_files .to_list () +
3424 [ctx .executable ._otbn_as ]),
3525 env = {
36- "RV32_TOOL_AS" : assembler .path ,
26+ "RV32_TOOL_AS" : ctx . executable . _riscv32_as .path ,
3727 },
3828 arguments = ["-o" , obj .path , src .path ] + ctx .attr .args ,
3929 executable = ctx .executable ._otbn_as ,
@@ -78,7 +68,6 @@ def _otbn_binary(ctx, additional_srcs = []):
7868 that other rules can depend on in their `deps`.
7969 """
8070 cc_toolchain = find_cc_toolchain (ctx )
81- assembler = _get_assembler (cc_toolchain )
8271
8372 # Run the otbn assembler on source files to produce object (.o) files.
8473 objs = _otbn_assemble_sources (ctx , additional_srcs )
@@ -100,10 +89,10 @@ def _otbn_binary(ctx, additional_srcs = []):
10089 ctx .files ._otbn_data +
10190 [ctx .executable ._wrapper ]),
10291 env = {
103- "RV32_TOOL_AS" : assembler .path ,
104- "RV32_TOOL_AR" : cc_toolchain . ar_executable ,
105- "RV32_TOOL_LD" : cc_toolchain . ld_executable ,
106- "RV32_TOOL_OBJCOPY" : cc_toolchain . objcopy_executable ,
92+ "RV32_TOOL_AS" : ctx . executable . _riscv32_as .path ,
93+ "RV32_TOOL_AR" : ctx . executable . _riscv32_ar . path ,
94+ "RV32_TOOL_LD" : ctx . executable . _riscv32_ld . path ,
95+ "RV32_TOOL_OBJCOPY" : ctx . executable . _riscv32_objcopy . path ,
10796 },
10897 arguments = [
10998 "--app-name={}" .format (ctx .attr .name ),
@@ -286,8 +275,11 @@ otbn_library = rv_rule(
286275 attrs = {
287276 "srcs" : attr .label_list (allow_files = True ),
288277 "args" : attr .string_list (),
289- "_cc_toolchain" : attr .label (
290- default = Label ("@bazel_tools//tools/cpp:current_cc_toolchain" ),
278+ "_riscv32_as" : attr .label (
279+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as" ),
280+ allow_single_file = True ,
281+ executable = True ,
282+ cfg = "exec" ,
291283 ),
292284 "_otbn_as" : attr .label (
293285 default = "//hw/ip/otbn/util:otbn_as" ,
@@ -306,7 +298,30 @@ otbn_binary = rv_rule(
306298 "srcs" : attr .label_list (allow_files = True ),
307299 "deps" : attr .label_list (providers = [DefaultInfo ]),
308300 "args" : attr .string_list (),
309- "_cc_toolchain" : attr .label (default = Label ("@bazel_tools//tools/cpp:current_cc_toolchain" )),
301+ "_riscv32_ar" : attr .label (
302+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ar" ),
303+ allow_single_file = True ,
304+ executable = True ,
305+ cfg = "exec" ,
306+ ),
307+ "_riscv32_as" : attr .label (
308+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as" ),
309+ allow_single_file = True ,
310+ executable = True ,
311+ cfg = "exec" ,
312+ ),
313+ "_riscv32_ld" : attr .label (
314+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld" ),
315+ allow_single_file = True ,
316+ executable = True ,
317+ cfg = "exec" ,
318+ ),
319+ "_riscv32_objcopy" : attr .label (
320+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-objcopy" ),
321+ allow_single_file = True ,
322+ executable = True ,
323+ cfg = "exec" ,
324+ ),
310325 "_otbn_as" : attr .label (
311326 default = "//hw/ip/otbn/util:otbn_as" ,
312327 executable = True ,
@@ -335,7 +350,30 @@ otbn_sim_test = rv_rule(
335350 "deps" : attr .label_list (providers = [DefaultInfo ]),
336351 "exp" : attr .label (allow_single_file = True ),
337352 "dexp" : attr .label (allow_single_file = True ),
338- "_cc_toolchain" : attr .label (default = Label ("@bazel_tools//tools/cpp:current_cc_toolchain" )),
353+ "_riscv32_ar" : attr .label (
354+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ar" ),
355+ allow_single_file = True ,
356+ executable = True ,
357+ cfg = "exec" ,
358+ ),
359+ "_riscv32_as" : attr .label (
360+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as" ),
361+ allow_single_file = True ,
362+ executable = True ,
363+ cfg = "exec" ,
364+ ),
365+ "_riscv32_ld" : attr .label (
366+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld" ),
367+ allow_single_file = True ,
368+ executable = True ,
369+ cfg = "exec" ,
370+ ),
371+ "_riscv32_objcopy" : attr .label (
372+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-objcopy" ),
373+ allow_single_file = True ,
374+ executable = True ,
375+ cfg = "exec" ,
376+ ),
339377 "_otbn_as" : attr .label (
340378 default = "//hw/ip/otbn/util:otbn_as" ,
341379 executable = True ,
@@ -372,7 +410,30 @@ otbn_autogen_sim_test = rv_rule(
372410 attrs = {
373411 "srcs" : attr .label_list (allow_files = True ),
374412 "deps" : attr .label_list (providers = [DefaultInfo ]),
375- "_cc_toolchain" : attr .label (default = Label ("@bazel_tools//tools/cpp:current_cc_toolchain" )),
413+ "_riscv32_ar" : attr .label (
414+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ar" ),
415+ allow_single_file = True ,
416+ executable = True ,
417+ cfg = "exec" ,
418+ ),
419+ "_riscv32_as" : attr .label (
420+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-as" ),
421+ allow_single_file = True ,
422+ executable = True ,
423+ cfg = "exec" ,
424+ ),
425+ "_riscv32_ld" : attr .label (
426+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-ld" ),
427+ allow_single_file = True ,
428+ executable = True ,
429+ cfg = "exec" ,
430+ ),
431+ "_riscv32_objcopy" : attr .label (
432+ default = Label ("@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-objcopy" ),
433+ allow_single_file = True ,
434+ executable = True ,
435+ cfg = "exec" ,
436+ ),
376437 "testgen" : attr .label (
377438 mandatory = True ,
378439 executable = True ,
0 commit comments