-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcup.bzl
69 lines (65 loc) · 2.23 KB
/
cup.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
65
66
67
68
69
# Copyright 2018-2019 Google LLC.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
""" Skylark rules for CUP. """
def _cup_impl(ctx):
""" Generates a Java parser from a CUP definition, using Java CUP. """
# Output directory is bazel-genfiles/package, regardless of Java package defined in
# the grammar
output_dir = "/".join(
[ctx.configuration.genfiles_dir.path, ctx.label.package],
)
args = []
args.extend(["-parser", ctx.attr.parser])
args.extend(["-symbols", ctx.attr.symbols])
args.extend(["-destdir", output_dir])
args.extend(["-nosummary"])
if ctx.attr.interface:
args.append("-interface")
args.extend([ctx.file.src.path])
# TODO(regisd): Add support for CUP options.
parser_file = ctx.actions.declare_file(ctx.attr.parser + ".java")
sym_file = ctx.actions.declare_file(ctx.attr.symbols + ".java")
ctx.actions.run(
mnemonic = "cup",
inputs = [ctx.file.src],
outputs = [parser_file, sym_file],
executable = ctx.executable.cup_bin,
arguments = args,
)
return [DefaultInfo(
files = depset([parser_file, sym_file]),
)]
cup = rule(
implementation = _cup_impl,
attrs = {
"src": attr.label(
allow_single_file = True,
mandatory = True,
doc = "The CUP grammar specification",
),
"cup_bin": attr.label(
default = Label("//third_party/cup:cup_bin"),
executable = True,
cfg = "host",
doc = "The java_binary of CUP",
),
"parser": attr.string(
default = "parser",
doc = "Name of the generated parser class",
),
"symbols": attr.string(
default = "sym",
doc = "Name of the generated symbols class",
),
"interface": attr.bool(
doc = "Outputs the symbol constant code as an interface rather than as a class.",
),
},
output_to_genfiles = True, # JFlex generates java files, not bin files
)