Giter VIP home page Giter VIP logo

rules_m4's Introduction

Bazel build rules for GNU M4

The m4 macro processing language is commonly used as an intermediate format for text-based Unix development tools such as Bison and Flex.

This Bazel ruleset allows GNU M4 to be integrated into a Bazel build. It can be used to perform macro expansion with the //m4:m4.bzl%m4 build rule, or as a dependency in other rules via the Bazel toolchain system.

Currently, the only implementation of m4 supported by this ruleset is GNU M4.

API reference: docs/rules_m4.md

Setup

As a module dependency (bzlmod)

Add the following to your MODULE.bazel:

bazel_dep(name = "rules_m4", version = "0.2.3")

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

m4 = use_extension(
    "@rules_m4//m4/extensions:m4_repository_ext.bzl",
    "m4_repository_ext",
)
m4.repository(
    name = "m4",
    version = "1.4.17",
    extra_copts = ["-O3"],
)
use_repo(m4, "m4")
register_toolchains("@m4//: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

Add the following to your WORKSPACE.bazel:

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

http_archive(
    name = "rules_m4",
    # Obtain the package checksum from the release page:
    # https://github.com/jmillikin/rules_m4/releases/tag/v0.2.3
    sha256 = "",
    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")

To specify a version or build with additional C compiler options, use the m4_repository and m4_toolchain_repository repository rules:

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

m4_repository(
    name = "m4_v1.4.17_fast",
    version = "1.4.17",
    extra_copts = ["-O3"],
)

m4_toolchain_repository(
    name = "m4_toolchain_v1.4.17_fast",
    m4_repository = "@m4_v1.4.17_fast",
)

register_toolchains("@m4_toolchain_v1.4.17_fast//:toolchain")

Examples

Macro expansion with the //m4:m4.bzl%m4 build rule:

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

m4(
    name = "hello_world",
    srcs = ["hello_world.in.txt"],
    output = "hello_world.txt",
)

Macro expansion in a genrule:

genrule(
    name = "hello_world_gen",
    srcs = ["hello_world.in.txt"],
    outs = ["hello_world_gen.txt"],
    cmd = "$(M4) $(SRCS) > $@",
    toolchains = ["@rules_m4//m4:current_m4_toolchain"],
)

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

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

def _my_rule(ctx):
    m4 = m4_toolchain(ctx)
    ctx.actions.run(
        tools = [m4.m4_tool],
        env = m4.m4_env,
        # ...
    )

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

rules_m4'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_m4's Issues

Error in label: label() got unexpected keyword argument 'single_file' with bazel 7.1.1

I am using bazel 7.1.1. I am trying to use an external repo for GMP (GNU multiprecision) with bazel. I cloned this repo:

cd ~/dev
git clone https://github.com/robin-thomas/bazelify-gmp.git

When I try to compile it, I get an error with M4:

cd ~/dev/bazelify-gmp; bazel test //gmp_6_1_2/... --sandbox_debug --verbose_failures
Computing main repo mapping:
ERROR: Traceback (most recent call last):
File "/private/var/tmp/_bazel_jordansmith/dc88ff116f8da0e038742f8fca8fbbf9/external/io_bazel_rules_m4/m4.bzl", line 82, column 31, in
"template": attr.label(
Error in label: label() got unexpected keyword argument 'single_file'
ERROR: Error computing the main repository mapping: initialization of module 'm4.bzl' failed

Thank you for your help,
Jordan

Build failure for m4 1.4.18 and GNU libc >= v2.34

m4 older than 1.4.19 fails to build on systems with the just-released glibc 2.34. This problem will become more and more visible with distributions switching to this glibc version (it's already happened for rolling release distributions such as Fedora Rawhide and openSUSE Tumbleweed).

Could you update this project to provide m4 1.4.19?

From https://lists.gnu.org/archive/html/m4-announce/2021-05/msg00002.html:

* Noteworthy changes in release 1.4.19 (2021-05-28) [stable]

** A number of portability improvements inherited from gnulib, including
   the ability to perform stack overflow detection on more platforms
   without linking to GNU libsigsegv.

Discussion of the compilation error: https://lists.gnu.org/archive/html/bug-m4/2021-03/msg00000.html

Building rules_m4 on Windows result in errors

Bazel building using MSVC

$ bazel build @bazelify_gmp//:gmp
INFO: Invocation ID: 18ac6ddd-788d-4f38-b1f5-edd31178d40d
Loading:
Loading: 0 packages loaded
Analyzing: target @bazelify_gmp//:gmp (0 packages loaded, 0 targets configured)
INFO: Analysed target @bazelify_gmp//:gmp (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
[0 / 21] no action
ERROR: C:/users/houji/_bazel_houji/6dp3edvb/external/m4_v1.4.18/BUILD.bazel:15:1: C++ compilation of rule '@m4_v1.4.18//:m4_lib' failed (Exit 2)
c:\users\houji\_bazel_houji\6dp3edvb\execroot\__main__\external\m4_v1.4.18\lib\freadahead.h(41): error C2061: syntax error: identifier '__attribute__'
c:\users\houji\_bazel_houji\6dp3edvb\execroot\__main__\external\m4_v1.4.18\lib\freadahead.h(41): error C2059: syntax error: ';'
Target @bazelify_gmp//:gmp failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.788s, Critical Path: 0.70s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully

Bazel building using mingw-gcc

$ bazel build @bazelify_gmp//:gmp --compiler=mingw-gcc
INFO: Invocation ID: 768ff9c7-3663-4166-a1a0-24d2444af630
Loading:
Loading: 0 packages loaded
Analyzing: target @bazelify_gmp//:gmp (0 packages loaded, 0 targets configured)
INFO: Analysed target @bazelify_gmp//:gmp (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
[0 / 17] no action
ERROR: C:/users/houji/_bazel_houji/6dp3edvb/external/m4_v1.4.18/BUILD.bazel:15:1: C++ compilation of rule '@m4_v1.4.18//:m4_lib' failed (Exit 1)
In file included from external/m4_v1.4.18/lib/regex.c:68:
external/m4_v1.4.18/lib/regex_internal.h:29:10: fatal error: langinfo.h: No such file or directory
 #include <langinfo.h>
          ^~~~~~~~~~~~
compilation terminated.
Target @bazelify_gmp//:gmp failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.267s, Critical Path: 0.21s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully

It builds fine on Ubuntu and Mac.

Kindly help.

Under some conditions the m4 compilation rule is missing dependencies

00:30:13 ERROR: /root/.cache/bazel/_bazel_root/f8087e59fd95af1ae29e8fcb7ff1a3dc/external/m4_v1.4.18/BUILD.bazel:192:1: undeclared inclusion(s) in rule '@m4_v1.4.18//:gnulib':
00:30:13 this rule is missing dependency declarations for the following files included by 'external/m4_v1.4.18/lib/pipe-safer.c':
00:30:13   'external/m4_v1.4.18/lib/unistd-safer.h'

Fails to build in google rbe_ubuntu image (default for bazel runs)

docker run -it marketplace.gcr.io/google/rbe-ubuntu16-04:latest

install bazel in the image

and then after cloning, bazel test fails

bazel test //...
INFO: Invocation ID: fbb72c51-f511-4c5c-9897-96b122c7ead9
DEBUG: Rule 'io_bazel_rules_m4' modified arguments {"shallow_since": "1548637354 -0800"}
INFO: Analysed 13 targets (35 packages loaded, 1059 targets configured).
INFO: Found 12 targets and 1 test target...
ERROR: /root/.cache/bazel/_bazel_root/3ee255d691f0c03563386d57050f3aa6/external/m4_v1.4.18/bin/BUILD.bazel:1:1: Linking of rule '@m4_v1.4.18//bin:m4' failed (Exit 1) clang failed: error executing command /usr/local/bin/clang -o bazel-out/host/bin/external/m4_v1.4.18/bin/m4 '-fuse-ld=gold' -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/local/bin -Wl,--gc-sections -Wl,-S ... (remaining 1 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
bazel-out/host/bin/external/m4_v1.4.18/_objs/m4_lib/symtab.o:symtab.c:function symtab_init: error: undefined reference to '__muloti4'
bazel-out/host/bin/external/m4_v1.4.18/_objs/gnulib/clean-temp.o:clean-temp.c:function create_temp_dir: error: undefined reference to '__muloti4'
bazel-out/host/bin/external/m4_v1.4.18/_objs/gnulib/fatal-signal.o:fatal-signal.c:function at_fatal_signal: error: undefined reference to '__muloti4'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
INFO: Elapsed time: 11.813s, Critical Path: 5.11s
INFO: 184 processes: 184 processwrapper-sandbox.
FAILED: Build did NOT complete successfully
//tests:bison_test NO STATUS

FAILED: Build did NOT complete successfully

Use of new @bazel_tools//src/conditions targets breaks build on old Bazel versions

Example error:

ERROR: While resolving configuation keys for @m4_v1.4.18//gnulib:gnulib: no such target '@bazel_tools//src/conditions:openbsd': target 'openbsd' not declared in package 'src/conditions' (did you mean 'freebsd'?) defined by /github/home/.cache/bazel/_bazel_root/96335d2574d677ca0e31aa2073ba4534/external/bazel_tools/src/conditions/BUILD

Work to be done:

  • Copy Github Actions configuration from rules_flex[0] to verify building with older Bazel versions.
  • Change the select() matches to use targets not bundled into Bazel (@platforms// seems like the correct replacement).

[0] https://github.com/jmillikin/rules_flex/blob/e32e634a3f91837c66059f58aa0829f9b8caf1f4/.github/workflows/tests.yaml

//tests:m4_test fails to build on Windows due to incorrect header inclusion check

See bazelbuild/bazel#5640 for background.

(00:26:15) ERROR: D:/b/llkfrd43/external/bazel_tools/tools/cpp/runfiles/BUILD:3:1: Couldn't build file external/bazel_tools/tools/cpp/runfiles/_objs/runfiles/runfiles.obj: undeclared inclusion(s) in rule '@bazel_tools//tools/cpp/runfiles:runfiles':
--
  | this rule is missing dependency declarations for the following files included by 'external/bazel_tools/tools/cpp/runfiles/runfiles.cc':
  | 'D:/b/j4my5y73/execroot/bazel_skylib/external/bazel_tools/tools/cpp/runfiles/runfiles.h'

MSVC warning cflags in gnulib.BUILD are not being applied

It looks like select({"@bazel_tools//src/conditions:windows_msvc": doesn't work, and it's always falling through to the default case. This causes problems when updating gnulib, because without /we4013 MSVC will silently accept calls to undeclared symbols.

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.