|
1 | 1 | """Build rule for generating C or C++ sources with Bison.""" |
2 | 2 |
|
3 | 3 | def _genyacc_impl(ctx): |
4 | | - """Implementation for genyacc rule. |
| 4 | + """Implementation for genyacc rule.""" |
| 5 | + bison_tc = ctx.toolchains["@rules_bison//bison:toolchain_type"].bison_toolchain |
5 | 6 |
|
6 | | - Expects to find bison binary on the PATH. |
7 | | - """ |
8 | | - |
9 | | - # Argument list |
10 | | - args = [] |
11 | | - args.append("--defines=%s" % ctx.outputs.header_out.path) |
12 | | - args.append("--output-file=%s" % ctx.outputs.source_out.path) |
| 7 | + args = ctx.actions.args() |
| 8 | + args.add("--defines=" + ctx.outputs.header_out.path) |
| 9 | + args.add("--output-file=" + ctx.outputs.source_out.path) |
13 | 10 | if ctx.attr.prefix: |
14 | | - args.append("--name-prefix=%s" % ctx.attr.prefix) |
15 | | - args += [ctx.expand_location(opt) for opt in ctx.attr.extra_options] |
16 | | - args.append(ctx.file.src.path) |
| 11 | + args.add("--name-prefix=" + ctx.attr.prefix) |
| 12 | + args.add_all([ctx.expand_location(opt) for opt in ctx.attr.extra_options]) |
| 13 | + args.add(ctx.file.src.path) |
17 | 14 |
|
18 | | - # Output files |
19 | 15 | outputs = ctx.outputs.extra_outs + [ |
20 | 16 | ctx.outputs.header_out, |
21 | 17 | ctx.outputs.source_out, |
22 | 18 | ] |
23 | 19 |
|
24 | | - ctx.actions.run_shell( |
25 | | - use_default_shell_env = True, |
26 | | - command = "bison " + " ".join(args), |
27 | | - inputs = ctx.files.src, |
| 20 | + ctx.actions.run( |
| 21 | + executable = bison_tc.bison_tool, |
| 22 | + arguments = [args], |
| 23 | + inputs = depset(direct = ctx.files.src), |
28 | 24 | outputs = outputs, |
| 25 | + env = bison_tc.bison_env, |
29 | 26 | mnemonic = "Yacc", |
30 | | - progress_message = "Generating %s and %s from %s" % |
31 | | - ( |
32 | | - ctx.outputs.source_out.short_path, |
33 | | - ctx.outputs.header_out.short_path, |
34 | | - ctx.file.src.short_path, |
35 | | - ), |
| 27 | + progress_message = "Generating %s and %s from %s" % ( |
| 28 | + ctx.outputs.source_out.short_path, |
| 29 | + ctx.outputs.header_out.short_path, |
| 30 | + ctx.file.src.short_path, |
| 31 | + ), |
36 | 32 | ) |
37 | 33 |
|
38 | 34 | genyacc = rule( |
39 | 35 | implementation = _genyacc_impl, |
40 | 36 | doc = "Generate C/C++-language sources from a Yacc file using Bison.", |
41 | 37 | attrs = { |
42 | | - "src": attr.label( |
43 | | - mandatory = True, |
44 | | - allow_single_file = [".y", ".yy", ".yc", ".ypp"], |
45 | | - doc = "The .y, .yy, or .yc source file for this rule", |
| 38 | + "extra_options": attr.string_list( |
| 39 | + doc = "A list of extra options to pass to Bison. These are " + |
| 40 | + "subject to $(location ...) expansion.", |
46 | 41 | ), |
| 42 | + "extra_outs": attr.output_list(doc = "A list of extra generated output files."), |
47 | 43 | "header_out": attr.output( |
48 | 44 | mandatory = True, |
49 | 45 | doc = "The generated 'defines' header file", |
50 | 46 | ), |
51 | | - "source_out": attr.output(mandatory = True, doc = "The generated source file"), |
52 | 47 | "prefix": attr.string( |
53 | 48 | doc = "External symbol prefix for Bison. This string is " + |
54 | 49 | "passed to bison as the -p option, causing the resulting C " + |
55 | 50 | "file to define external functions named 'prefix'parse, " + |
56 | 51 | "'prefix'lex, etc. instead of yyparse, yylex, etc.", |
57 | 52 | ), |
58 | | - "extra_outs": attr.output_list(doc = "A list of extra generated output files."), |
59 | | - "extra_options": attr.string_list( |
60 | | - doc = "A list of extra options to pass to Bison. These are " + |
61 | | - "subject to $(location ...) expansion.", |
| 53 | + "source_out": attr.output(mandatory = True, doc = "The generated source file"), |
| 54 | + "src": attr.label( |
| 55 | + mandatory = True, |
| 56 | + allow_single_file = [".y", ".yy", ".yc", ".ypp"], |
| 57 | + doc = "The .y, .yy, or .yc source file for this rule", |
62 | 58 | ), |
63 | 59 | }, |
| 60 | + toolchains = [ |
| 61 | + "@rules_bison//bison:toolchain_type", |
| 62 | + "@rules_m4//m4:toolchain_type", |
| 63 | + ], |
64 | 64 | ) |
0 commit comments