rules_foreign_cc icon indicating copy to clipboard operation
rules_foreign_cc copied to clipboard

autotools example: m4 segmentation fault

Open uhthomas opened this issue 2 years ago • 9 comments

❯ bazel build @m4//...
...
bazel-out/darwin_arm64-fastbuild/bin/external/m4/m4_foreign_cc/build_script.sh: line 102: 87922 Segmentation fault: 11  $EXT_BUILD_ROOT/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make/bin/make -C $BUILD_TMPDIR
...

uhthomas avatar Apr 19 '22 22:04 uhthomas

This looks like make is segfaulting? Are you using the native make or is make being built from source? From the platform I can see this is on Apple Silicon; I don't have access to such a machine to debug, nor is there one in bazel CI

jsharpe avatar May 29 '22 22:05 jsharpe

it could be make 4.3 is buggy https://savannah.gnu.org/bugs/?59093. rule_foreign_cc uses 4.3 by default. i encountered the same issue, but was able to get through by using a local make

xjbhenry avatar Sep 09 '22 21:09 xjbhenry

+1, I see the same moving from rules_foreign_cc 0.6.0 to 0.9.0.

ormandi avatar Oct 27 '22 13:10 ormandi

Yeah, I can confirm that with 0.9.0, I'm using make version 4.3. More specifically:

$ bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make/bin/make --version
GNU Make 4.3
Built for x86_64-apple-darwin20.3.0
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

ormandi avatar Oct 27 '22 14:10 ormandi

This seems to be the section bringing in the broken make version: https://github.com/bazelbuild/rules_foreign_cc/blob/main/toolchains/built_toolchains.bzl#L419-L432 This is present in both in tag 0.9.0 and in the main branch.

ormandi avatar Oct 27 '22 14:10 ormandi

Btw, I'm trying to build make version 4.2 after the following patch:

diff -Naur rules_foreign_cc/foreign_cc/built_tools/make_build.bzl rules_foreign_cc_make_4.2_patch/foreign_cc/built_tools/make_build.bzl
--- rules_foreign_cc/foreign_cc/built_tools/make_build.bzl      2022-10-27 09:59:04.000000000 -0500
+++ rules_foreign_cc_make_4.2_patch/foreign_cc/built_tools/make_build.bzl       2022-10-27 10:36:40.000000000 -0500
@@ -86,7 +86,6 @@
         configure_env = " ".join(["%s=\"%s\"" % (key, value) for key, value in env.items()])
         script = [
             "%s ./configure --without-guile --with-guile=no --disable-dependency-tracking --prefix=$$INSTALLDIR$$" % configure_env,
-            "cat build.cfg",
             "./build.sh",
             "./make install",
         ]
diff -Naur rules_foreign_cc/toolchains/built_toolchains.bzl rules_foreign_cc_make_4.2_patch/toolchains/built_toolchains.bzl
--- rules_foreign_cc/toolchains/built_toolchains.bzl    2022-10-27 09:59:04.000000000 -0500
+++ rules_foreign_cc_make_4.2_patch/toolchains/built_toolchains.bzl     2022-10-27 10:06:46.000000000 -0500
@@ -416,6 +416,19 @@
         native.register_toolchains(
             "@rules_foreign_cc//toolchains:built_make_toolchain",
         )
+    if version == "4.2":
+        maybe(
+            http_archive,
+            name = "gnumake_src",
+            build_file_content = _ALL_CONTENT,
+            sha256 = "e968ce3c57ad39a593a92339e23eb148af6296b9f40aa453a9a9202c99d34436",
+            strip_prefix = "make-4.2",
+            urls = [
+                "https://ftp.gnu.org/gnu/make/make-4.2.tar.gz",
+            ],
+        )
+        return
+
     if version == "4.3":
         maybe(
             http_archive,

and using the followings in my WORKSPACE file:

http_archive(
    name = "rules_foreign_cc",
    sha256 = RULES_FOREIGN_CC_SHA256,
    strip_prefix = "rules_foreign_cc-{version}".format(version = RULES_FOREIGN_CC_VERSION),
    urls = ["https://github.com/bazelbuild/rules_foreign_cc/archive/{version}.tar.gz".format(version = RULES_FOREIGN_CC_VERSION)],
    patches = [
        "//tools/repository_rules:rules_foreign_cc_make_4.2.diff",
    ],
    patch_args = ["-p1"],
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies(
    make_version = "4.2",
)

But, I'm running into various issues to actually build make during the initialization of the rule foreign_cc rule which probably has something to do with the fact that I use LLVM (and here I try to build something super GNU specific (make)).

Beware in the patch above I do not apply the patch //toolchains:make-reproducible-bootstrap.patch on make since it does not seem to fit version 4.2.

ormandi avatar Oct 27 '22 15:10 ormandi

For LLVM/clang it seems the source of make version 4.2.0 has to be patched as well... https://github.com/conan-io/conan-center-index/issues/484

ormandi avatar Oct 27 '22 16:10 ormandi

Yes, the above patch on rules_foreign_cc + the patch on the make linked above worked.

ormandi avatar Oct 27 '22 20:10 ormandi

Thanks a million @ormandi for saving me so much work on this one.

Note that on OS X, you have to hack around the fact that by default cc_toolchain_info tries to set libtool as the AR (otherwise you get a build error similar to this one while building make).

For me, the final patch ended up being (I did not have to patch Make itself):

third_party/rules_foreign_cc.patch:

diff --git a/foreign_cc/built_tools/make_build.bzl b/foreign_cc/built_tools/make_build.bzl
index 0baaa5c..2fc8a2c 100644
--- a/foreign_cc/built_tools/make_build.bzl
+++ b/foreign_cc/built_tools/make_build.bzl
@@ -75,7 +75,7 @@ def _make_tool_impl(ctx):
             non_sysroot_ldflags += ["-undefined", "error"]
 
         env.update({
-            "AR": absolute_ar,
+            "AR": "",
             "ARFLAGS": _join_flags_list(ctx.workspace_name, arflags),
             "CC": absolute_cc,
             "CFLAGS": _join_flags_list(ctx.workspace_name, non_sysroot_cflags),
@@ -86,7 +86,6 @@ def _make_tool_impl(ctx):
         configure_env = " ".join(["%s=\"%s\"" % (key, value) for key, value in env.items()])
         script = [
             "%s ./configure --without-guile --with-guile=no --disable-dependency-tracking --prefix=$$INSTALLDIR$$" % configure_env,
-            "cat build.cfg",
             "./build.sh",
             "./make install",
         ]
diff --git a/toolchains/built_toolchains.bzl b/toolchains/built_toolchains.bzl
index 5e59e79..8bfbcad 100644
--- a/toolchains/built_toolchains.bzl
+++ b/toolchains/built_toolchains.bzl
@@ -430,6 +430,20 @@ def _make_toolchain(version, register_toolchains):
             ],
         )
         return
+    if version == "4.2":
+        maybe(
+            http_archive,
+            name = "gnumake_src",
+            build_file_content = _ALL_CONTENT,
+            sha256 = "e968ce3c57ad39a593a92339e23eb148af6296b9f40aa453a9a9202c99d34436",
+            strip_prefix = "make-4.2",
+            urls = [
+                "https://ftp.gnu.org/gnu/make/make-4.2.tar.gz",
+            ],
+        )
+        return
+
+
 
     fail("Unsupported make version: " + str(version))
 

WORKSPACE:

http_archive(
    name = "rules_foreign_cc",
    sha256 = "2a4d07cd64b0719b39a7c12218a3e507672b82a97b98c6a89d38565894cf7c51",
    strip_prefix = "rules_foreign_cc-0.9.0",
    url = "https://github.com/bazelbuild/rules_foreign_cc/archive/refs/tags/0.9.0.tar.gz",
    patches = ["//third_party:rules_foreign_cc.patch"],
    patch_args = ["-p1"],
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

# Build a specific make version to work around a segmentation fault:
# https://github.com/bazelbuild/rules_foreign_cc/issues/898
# (for us, this segfault occurs when building SAMtools).
rules_foreign_cc_dependencies(make_version = "4.2")

bruno-digitbio avatar Jul 13 '23 04:07 bruno-digitbio