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