| # -*- Python -*- | |
| # Copyright 2017-2021 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. | |
| """Bazel rule to run flex toolchain | |
| """ | |
| # Adapter rule around the @rules_flex toolchain. | |
| def genlex(name, src, out): | |
| """Generate C/C++ language source from lex file using Flex | |
| """ | |
| native.alias( | |
| name = name + "_flex_toolchain", | |
| actual = select({ | |
| "//bazel:use_local_flex_bison_enabled": "//bazel:no_toolchain", | |
| "@platforms//os:windows": "//bazel:no_toolchain", | |
| "//conditions:default": "@rules_flex//flex:current_flex_toolchain", | |
| }), | |
| ) | |
| native.alias( | |
| name = name + "_m4_toolchain", | |
| actual = select({ | |
| "//bazel:use_local_flex_bison_enabled": "//bazel:no_toolchain", | |
| "@platforms//os:windows": "//bazel:no_toolchain", | |
| "//conditions:default": "@rules_m4//m4:current_m4_toolchain", | |
| }), | |
| ) | |
| native.genrule( | |
| name = name, | |
| srcs = [src], | |
| outs = [out], | |
| cmd = select({ | |
| "//bazel:use_local_flex_bison_enabled": "flex --outfile=$@ $<", | |
| "@platforms//os:windows": "win_flex.exe --outfile=$@ $<", | |
| "//conditions:default": "M4=$(M4) $(FLEX) --outfile=$@ $<", | |
| }), | |
| toolchains = [ | |
| ":" + name + "_flex_toolchain", | |
| ":" + name + "_m4_toolchain", | |
| ], | |
| ) |