blob: 6def3b151d5e4f7b7659d3b739b9da43791c1c13 [file] [log] [blame]
# Copyright 2017-2020 The Verible Authors.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Build rule for generating C or C++ sources with Bison.
"""
def _genyacc_impl(ctx):
"""Implementation for genyacc rule."""
# Argument list
args = ctx.actions.args()
args.add("--defines=%s" % ctx.outputs.header_out.path)
args.add("--output-file=%s" % ctx.outputs.source_out.path)
if ctx.attr.prefix:
args.add("--name-prefix=%s" % ctx.attr.prefix)
args.add_all([ctx.expand_location(opt) for opt in ctx.attr.extra_options])
args.add(ctx.file.src.path)
# Output files
outputs = ctx.outputs.extra_outs + [
ctx.outputs.header_out,
ctx.outputs.source_out,
]
ctx.actions.run(
executable = ctx.executable._bison,
env = {
"M4": ctx.executable._m4.path,
},
arguments = [args],
inputs = ctx.files.src,
tools = [ctx.executable._m4],
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(
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.",
),
"_bison": attr.label(
default = Label("//bazel:bison_bin"),
executable = True,
cfg = "host",
),
"_m4": attr.label(
default = Label("//bazel:m4_bin"),
executable = True,
cfg = "host",
),
},
doc = "Generate C/C++-language sources from a Yacc file using Bison.",
output_to_genfiles = True,
implementation = _genyacc_impl,
)