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# Flex (fast lexical analyzer generator) is used for parsing languages. It is
17# commonly used with a parser generator (like Bison).
18
19# Generate source files using Flex.
20#
21# Note: We generate .cc files for Flex. However, you must set %option c++ if you
22# want to generate a C++ scanner class. We don't use --c++ in case you have
23# %option reentrant set, which is mutually exclusive with %option c++.
24#
25# Parameters:
26#   sources: Files used to generate the lexers.
27template("flex_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") == "ll",
34             "Expecting Flex extension ll: ${s}")
35    }
36
37    script = "//common-mk/file_generator_wrapper.py"
38    outformat = "${target_gen_dir}/{{source_name_part}}.cc"
39    args = [
40      "flex",
41      "-o",
42      outformat,
43      "{{source}}",
44    ]
45    outputs = [ outformat ]
46  }
47
48  all_dependent_config_name = "_${target_name}_all_dependent_config"
49  config(all_dependent_config_name) {
50    include_dirs = [ "${target_gen_dir}" ]
51  }
52
53  source_set(target_name) {
54    sources = get_target_outputs(":${action_name}")
55    all_dependent_configs = [ ":${all_dependent_config_name}" ]
56    deps = [ ":${action_name}" ]
57
58    if (defined(invoker.configs)) {
59      configs += invoker.configs
60    }
61
62    # Silence some warnings. The autogenerated code includes parts that have
63    # fallthroughs and unreachable code. This may silence legitimate issues in
64    # user written code in the lexer as well. User beware!
65    cflags_cc = [
66      "-Wno-implicit-fallthrough",
67      "-Wno-unreachable-code",
68    ]
69  }
70}
71