1#
2#  Copyright 2021 Google, Inc.
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at:
7#
8#  http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16# Bison is a parser generator which reads a specification of a context-free
17# language and generates a parser which decides whether the input conforms to
18# the grammar specified.
19
20# Generate header and source files using Bison.
21#
22# Note: We generate C++ output for Bison. We also set --language to C++
23# (equivalent to %language c++) which forces Bison to generate a C++ parser.
24#
25# Parameters:
26#   sources: Grammar files used to generate the parsers.
27template("bison_source") {
28  action_name = "${target_name}_gen"
29  action_foreach(action_name) {
30    forward_variables_from(invoker, [ "sources" ])
31    assert(defined(sources), "sources must be set")
32    foreach(s, sources) {
33      assert(get_path_info(s, "extension") == "yy",
34             "Expecting Bison extension yy: ${s}")
35    }
36
37    script = "//common-mk/file_generator_wrapper.py"
38    outprefix = "${target_gen_dir}/{{source_name_part}}"
39    args = [
40      "bison",
41      "--language=c++",
42      "--defines=${outprefix}.h",
43      "-o",
44      "${outprefix}.cc",
45      "{{source}}",
46    ]
47    outputs = [
48      "${outprefix}.h",
49      "${outprefix}.cc",
50    ]
51  }
52
53  all_dependent_config_name = "_${target_name}_all_dependent_config"
54  config(all_dependent_config_name) {
55    include_dirs = [ "${target_gen_dir}" ]
56  }
57
58  source_set(target_name) {
59    sources = get_target_outputs(":${action_name}")
60    all_dependent_configs = [ ":${all_dependent_config_name}" ]
61    deps = [ ":${action_name}" ]
62
63    if (defined(invoker.configs)) {
64      configs += invoker.configs
65    }
66
67    # Silence some warnings. The autogenerated code includes parts that have
68    # fallthroughs and unreachable code. This may silence legitimate issues in
69    # user written code in the grammar files as well. User beware!
70    cflags_cc = [
71      "-Wno-implicit-fallthrough",
72      "-Wno-unreachable-code",
73    ]
74  }
75}
76