|
| 1 | +# Bazel buuld rules for GNU Bison |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +```python |
| 6 | +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") |
| 7 | + |
| 8 | +http_archive( |
| 9 | + name = "rules_m4", |
| 10 | + urls = ["https://github.com/jmillikin/rules_m4/releases/download/v0.1/rules_m4-v0.1.tar.xz"], |
| 11 | + sha256 = "7bb12b8a5a96037ff3d36993a9bb5436c097e8d1287a573d5958b9d054c0a4f7", |
| 12 | +) |
| 13 | +load("@rules_m4//m4:m4.bzl", "m4_register_toolchains") |
| 14 | +m4_register_toolchains() |
| 15 | + |
| 16 | +http_archive( |
| 17 | + name = "rules_bison", |
| 18 | + # See https://github.com/jmillikin/rules_bison/releases for copy-pastable |
| 19 | + # URLs and checksums. |
| 20 | +) |
| 21 | +load("@rules_bison//bison:bison.bzl", "bison_register_toolchains") |
| 22 | +bison_register_toolchains() |
| 23 | +``` |
| 24 | + |
| 25 | +```python |
| 26 | +load("@rules_bison//bison:bison.bzl", "bison_cc_library") |
| 27 | +bison_cc_library( |
| 28 | + name = "hello", |
| 29 | + src = "hello.y", |
| 30 | +) |
| 31 | +cc_binary( |
| 32 | + name = "hello_bin", |
| 33 | + deps = [":hello"], |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +```python |
| 38 | +load("@rules_bison//bison:bison.bzl", "bison_java_library") |
| 39 | +bison_java_library( |
| 40 | + name = "HelloJavaParser", |
| 41 | + src = "hello_java.y", |
| 42 | +) |
| 43 | +java_binary( |
| 44 | + name = "HelloJava", |
| 45 | + srcs = ["HelloJava.java"], |
| 46 | + main_class = "HelloJava", |
| 47 | + deps = [":HelloJavaParser"], |
| 48 | +) |
| 49 | +``` |
| 50 | + |
| 51 | +## Other Rules |
| 52 | + |
| 53 | +```python |
| 54 | +load("@rules_bison//bison:bison.bzl", "bison") |
| 55 | +bison( |
| 56 | + name = "hello_bin_srcs", |
| 57 | + src = "hello.y", |
| 58 | +) |
| 59 | +cc_binary( |
| 60 | + name = "hello_bin", |
| 61 | + srcs = [":hello_bin_srcs"], |
| 62 | +) |
| 63 | +``` |
| 64 | + |
| 65 | +```python |
| 66 | +genrule( |
| 67 | + name = "hello_gen", |
| 68 | + srcs = ["hello.y"], |
| 69 | + outs = ["hello_gen.c"], |
| 70 | + cmd = "M4=$(M4) $(BISON) --output=$@ $<", |
| 71 | + toolchains = [ |
| 72 | + "@rules_bison//bison:toolchain", |
| 73 | + "@rules_m4//m4:toolchain", |
| 74 | + ], |
| 75 | +) |
| 76 | +``` |
| 77 | + |
| 78 | +## Toolchains |
| 79 | + |
| 80 | +```python |
| 81 | +load("@rules_flex//bison:bison.bzl", "bison_common") |
| 82 | +load("@rules_m4//m4:m4.bzl", "m4_common") |
| 83 | + |
| 84 | +def _my_rule(ctx): |
| 85 | + bison_toolchain = bison_common.bison_toolchain(ctx) |
| 86 | + m4_toolchain = m4_common.m4_toolchain(ctx) |
| 87 | + ctx.actions.run( |
| 88 | + executable = bison_toolchain.bison_executable, |
| 89 | + inputs = depset(transitive = [ |
| 90 | + bison_toolchain.files, |
| 91 | + m4_toolchain.files, |
| 92 | + ]), |
| 93 | + env = {"M4": m4_toolchain.m4_executable.path}, |
| 94 | + # ... |
| 95 | + ) |
| 96 | + |
| 97 | +my_rule = rule( |
| 98 | + _my_rule, |
| 99 | + toolchains = [ |
| 100 | + bison_common.TOOLCHAIN_TYPE, |
| 101 | + m4_common.TOOLCHAIN_TYPE, |
| 102 | + ], |
| 103 | +) |
| 104 | +``` |
0 commit comments