Giter VIP home page Giter VIP logo

rules_bison's Introduction

Bazel build rules for GNU Bison

This Bazel ruleset allows GNU Bison to be integrated into a Bazel build. It can be used to generate parsers in C, C++, or Java.

API reference: docs/rules_bison.md

Setup

As a module dependency (bzlmod)

Add the following to your MODULE.bazel:

bazel_dep(name = "rules_bison", version = "0.2.2")

To specify a version or build with additional C compiler options, use the bison_repository_ext module extension:

bison = use_extension(
    "@rules_bison//bison/extensions:bison_repository_ext.bzl",
    "bison_repository_ext",
)
bison.repository(
    name = "bison",
    version = "3.3.2",
    extra_copts = ["-O3"],
)
use_repo(bison, "bison")
register_toolchains("@bison//:toolchain")

Note that repository names registered with a given bzlmod module extension must be unique within the scope of that extension. See the Bazel module extensions documentation for more details.

As a workspace dependency

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_m4",
    sha256 = "10ce41f150ccfbfddc9d2394ee680eb984dc8a3dfea613afd013cfb22ea7445c",
    urls = ["https://github.com/jmillikin/rules_m4/releases/download/v0.2.3/rules_m4-v0.2.3.tar.xz"],
)

load("@rules_m4//m4:m4.bzl", "m4_register_toolchains")

m4_register_toolchains(version = "1.4.18")

http_archive(
    name = "rules_bison",
    # Obtain the package checksum from the release page:
    # https://github.com/jmillikin/rules_bison/releases/tag/v0.2.2
    sha256 = "",
    urls = ["https://github.com/jmillikin/rules_bison/releases/download/v0.2.2/rules_bison-v0.2.2.tar.xz"],
)

load("@rules_bison//bison:bison.bzl", "bison_register_toolchains")

bison_register_toolchains(version = "3.3.2")

Examples

Integrating Bison into a C/C++ dependency graph:

load("@rules_bison//bison:bison.bzl", "bison_cc_library")

bison_cc_library(
    name = "hello_parser",
    src = "hello.y",
)

cc_binary(
    name = "hello",
    deps = [":hello_parser"],
)

Integrating Bison into a Java dependency graph:

load("@rules_bison//bison:bison.bzl", "bison_java_library")

bison_java_library(
    name = "HelloParser",
    src = "hello.y",
)

java_binary(
    name = "Hello",
    srcs = ["Hello.java"],
    main_class = "Hello",
    deps = [":HelloParser"],
)

Generating .c / .h / .cc source files (not as a CcInfo):

load("@rules_bison//bison:bison.bzl", "bison")

bison(
    name = "hello_parser_srcs",
    src = "hello.y",
)

cc_binary(
    name = "hello",
    srcs = [":hello_parser_srcs"],
)

Running Bison in a genrule:

genrule(
    name = "hello_gen",
    srcs = ["hello.y"],
    outs = ["hello_gen.c"],
    cmd = "M4=$(M4) $(BISON) --output=$@ $<",
    toolchains = [
        "@rules_bison//bison:current_bison_toolchain",
        "@rules_m4//m4:current_m4_toolchain",
    ],
)

Writing a custom rule that depends on Bison as a toolchain:

load("@rules_bison//bison:bison.bzl", "BISON_TOOLCHAIN_TYPE", "bison_toolchain")
load("@rules_m4//m4:m4.bzl", "M4_TOOLCHAIN_TYPE")

def _my_rule(ctx):
    bison = bison_toolchain(ctx)
    ctx.actions.run(
        executable = bison.bison_tool,
        env = bison.bison_env,
        # ...
    )

my_rule = rule(
    implementation = _my_rule,
    toolchains = [
        BISON_TOOLCHAIN_TYPE,
        M4_TOOLCHAIN_TYPE,
    ],
)

rules_bison's People

Contributors

chandlerc avatar jmillikin avatar micttyl avatar philwo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rules_bison's Issues

Execution fails on Windows

Bison fails to execute on Windows:

bazel build //tests:hello_c

fails with:

ERROR: D:/rules_bison/tests/BUILD:24:1: Bison //tests:hello_c failed (Exit 1)
bison.exe: bazel-out/host/bin/external/bison_v3.3.2/bin/bison.exe.runfiles/bison_v3.3.2/data/m4sugar/m4sugar.m4: cannot open: No such file or directory
Target //tests:hello_c failed to build

I believe the issue is that on Windows the runfiles are not symlinked but instead bazel put a MANIFEST file in bazel-out/host/bin/external/bison_v3.3.2/bin/bison.exe.runfiles with the correct mapping.

Replacing M4 and and BISON_PKGDATADIR with the mapped values make the execution pass. I am not sure how to do that with the bazel scripts though.

Fails on bazel 0.21.0 on OSX

b3f74fd65c8a/external/parser/BUILD.bazel:10:1: in bison_cc_library rule @parser//:bison_parser:
Traceback (most recent call last):
	File "/private/var/tmp/_bazel_dmitry/9878c75bb785aeb934f7b3f74fd65c8a/external/parser/BUILD.bazel", line 10
		bison_cc_library(name = 'bison_parser')
	File "/private/var/tmp/_bazel_dmitry/9878c75bb785aeb934f7b3f74fd65c8a/external/io_bazel_rules_bison/bison/bison.bzl", line 457, in _bison_cc_library
		_build_cc_info(ctx, result.source, result.header)
	File "/private/var/tmp/_bazel_dmitry/9878c75bb785aeb934f7b3f74fd65c8a/external/io_bazel_rules_bison/bison/bison.bzl", line 404, in _build_cc_info
		cc_toolchain.needs_pic_for_dynamic_libraries(feature_configuration = ld_featu...)
type 'CcToolchainInfo' has no method needs_pic_for_dynamic_libraries(feature_configuration feature_configuration)

bison compilation fails on CentOS 7.9.2009

I tried to use rules_bison/rules_flex on centos-7.9. But this is continuously failing for following errors. same rules compile well for ubuntu-20.04. I tried setting up BAZEL_COPTS="-std=c99", but still same output. I am using bazel-4.0.0

/home/dgautam/.cache/bazel/_bazel_dgautam/install/1a4a2fac02d50c77031d44c0d91b8920/process-wrapper '--timeout=0' '--kill_delay=15' /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG -ffunction-sections -fdata-sections -MD -MF bazel-out/host/bin/external/bison_v3.3.2/_objs/bison_lib/AnnotationList.d '-frandom-seed=bazel-out/host/bin/external/bison_v3.3.2/_objs/bison_lib/AnnotationList.o' -iquote external/bison_v3.3.2 -iquote bazel-out/host/bin/external/bison_v3.3.2 -Ibazel-out/host/bin/external/bison_v3.3.2/_virtual_includes/bison_lib -isystem external/bison_v3.3.2 -isystem bazel-out/host/bin/external/bison_v3.3.2 -isystem external/bison_v3.3.2/bison-lib -isystem bazel-out/host/bin/external/bison_v3.3.2/bison-lib -isystem external/bison_v3.3.2/gnulib/lib -isystem bazel-out/host/bin/external/bison_v3.3.2/gnulib/lib -isystem external/bison_v3.3.2/gnulib/config-linux -isystem bazel-out/host/bin/external/bison_v3.3.2/gnulib/config-linux -g0 -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c external/bison_v3.3.2/src/AnnotationList.c -o bazel-out/host/bin/external/bison_v3.3.2/_objs/bison_lib/AnnotationList.o)
external/bison_v3.3.2/src/AnnotationList.c: In function 'AnnotationList__compute_conflicted_tokens':
external/bison_v3.3.2/src/AnnotationList.c:173:3: error: 'for' loop initial declarations are only allowed in C99 mode
   for (int i = 0; i < reds->num; ++i)
   ^
external/bison_v3.3.2/src/AnnotationList.c:173:3: note: use option -std=c99 or -std=gnu99 to compile your code
external/bison_v3.3.2/src/AnnotationList.c: In function 'AnnotationList__computePredecessorAnnotations':
external/bison_v3.3.2/src/AnnotationList.c:227:3: error: 'for' loop initial declarations are only allowed in C99 mode
   for (state **predecessor = predecessors[s->number]; *predecessor; ++predecessor)
   ^
external/bison_v3.3.2/src/AnnotationList.c:236:9: error: 'for' loop initial declarations are only allowed in C99 mode
         for (ContributionIndex ci = 0; ci < self->inadequacyNode->contributionCount; ++ci)
         ^
external/bison_v3.3.2/src/AnnotationList.c:334:25: error: 'for' loop initial declarations are only allowed in C99 mode
                         for (size_t j = 0; j < (*predecessor)->nitems; ++j)
                         ^
external/bison_v3.3.2/src/AnnotationList.c:374:13: error: 'for' loop initial declarations are only allowed in C99 mode
             for (size_t i = 0; i < (*predecessor)->nitems; ++i)
             ^
external/bison_v3.3.2/src/AnnotationList.c: In function 'AnnotationList__compute_from_inadequacies':
external/bison_v3.3.2/src/AnnotationList.c:436:9: error: 'for' loop initial declarations are only allowed in C99 mode
         for (int rule_i = 0; rule_i < s->reductions->num; ++rule_i)
         ^
external/bison_v3.3.2/src/AnnotationList.c:452:9: error: 'for' loop initial declarations are only allowed in C99 mode
         for (int rule_i = 0; rule_i < s->reductions->num; ++rule_i)
         ^
external/bison_v3.3.2/src/AnnotationList.c: In function 'AnnotationList__debug':
external/bison_v3.3.2/src/AnnotationList.c:574:7: error: 'for' loop initial declarations are only allowed in C99 mode
       for (int j = 0; j < spaces; ++j)
       ^
external/bison_v3.3.2/src/AnnotationList.c:581:9: error: 'for' loop initial declarations are only allowed in C99 mode
         for (ContributionIndex ci = 0; ci < a->inadequacyNode->contributionCount; ++ci)
         ^
external/bison_v3.3.2/src/AnnotationList.c:586:13: error: 'for' loop initial declarations are only allowed in C99 mode
             for (int j = 0; j < spaces+2; ++j)
             ^
external/bison_v3.3.2/src/AnnotationList.c: In function 'AnnotationList__computeLookaheadFilter':
external/bison_v3.3.2/src/AnnotationList.c:622:5: error: 'for' loop initial declarations are only allowed in C99 mode
     for (ContributionIndex ci = 0; ci < self->inadequacyNode->contributionCount; ++ci)
     ^
external/bison_v3.3.2/src/AnnotationList.c: In function 'AnnotationList__computeDominantContribution':
external/bison_v3.3.2/src/AnnotationList.c:777:3: error: 'for' loop initial declarations are only allowed in C99 mode
   for (ContributionIndex ci = 0; ci < self->inadequacyNode->contributionCount; ++ci)
   ^
INFO: Elapsed time: 1.718s, Critical Path: 0.25s
INFO: 13 processes: 10 internal, 3 processwrapper-sandbox.
FAILED: Build did NOT complete successfully

Update for Bison 3.8+

I'd like to update to a more recent version of Bison (3.8 or later.) I've made a small attempt to update them myself here, but this fails with errors related to gnulib.

~/Workspace/rules_bison$ bazel test //tests:genrule_test           
WARNING: Download from https://mirror.bazel.build/github.com/coreutils/gnulib/archive/7818455627c5e54813ac89924b8b67d0bc869146.zip failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
INFO: Analyzed target //tests:genrule_test (0 packages loaded, 0 targets configured).
INFO: Found 1 target and 0 test targets...
ERROR: /private/var/tmp/_bazel_kkiningh/9d4359696dfa5ef018618c8abc478887/external/bison_v3.8.2/gnulib/BUILD.bazel:171:11: Compiling gnulib/lib/asnprintf.c [for host] failed: (Aborted): wrapped_clang failed: error executing command external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -g0 -O2 -DNDEBUG '-DNS_BLOCK_ASSERTIONS=1' ... (remaining 35 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
In file included from external/bison_v3.8.2/gnulib/lib/asnprintf.c:17:
external/bison_v3.8.2/gnulib/config-darwin/config.h:2:10: fatal error: 'gnulib/lib/config.in.h' file not found
#include "gnulib/lib/config.in.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Error in child process '/usr/bin/xcrun'. 1
Target //tests:genrule_test failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.392s, Critical Path: 0.31s
INFO: 11 processes: 11 internal.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully

Any chance the rules could be updated? Or guidance on how to fix the gnulib rules?

Support building on Windows

Bison uses a vendored copy of gnulib as a portability layer, which makes it difficult to build on Windows. It'll need its own copies of gnulib.bzl and vendored config.h headers that were used for M4 (jmillikin/rules_m4#1).

Handle location.hh, position.hh, and stack.hh

out_src = ctx.actions.declare_file("{}.{}".format(ctx.attr.name, out_src_ext))

I have a bison ypp that uses both %defines and %locations, thus these files should be generated per https://www.gnu.org/software/bison/manual/html_node/C_002b_002b-Bison-Interface.html. Have you thought about how to handle these files? Otherwise, the compilation process won't work because the output source file contains # include "location.hh"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.