Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors during BootstrapGNUMake after upgrading to macos v15 sdk #1099

Open
jungleraptor opened this issue Oct 3, 2023 · 3 comments
Open

Comments

@jungleraptor
Copy link
Contributor

jungleraptor commented Oct 3, 2023

We've observed at least two separate issues during the BootstrapGNUMake step on users who've upgraded their machines to the latest v15 release of the mac command line tools or the new xcode sdk.

Macos version: Venture 13.6

Bazel version: 6.2.0

rules_foreign_cc: d4b19bb010ca5d433850c1d9db0510c2fd395aae758ae37f02930944596220ec

Error 1:

+ cat build.cfg
# Configuration for building GNU Make in the absence of any 'make' program.
# build.cfg.  Generated from build.cfg.in by configure.

# Copyright (C) 1993-2020 Free Software Foundation, Inc.
# This file is part of GNU Make.
#
# GNU Make is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

# See Makefile.in for comments describing these variables.

top_srcdir='.'

prefix='/private/var/tmp/_bazel_isaac.torres/a00f253b0b0fdebf676f9472a6bd7fc6/sandbox/darwin-sandbox/28/execroot/libswiftnav/bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make'
exec_prefix=`eval echo ${prefix}`

CC='/private/var/tmp/_bazel_isaac.torres/a00f253b0b0fdebf676f9472a6bd7fc6/sandbox/darwin-sandbox/28/execroot/libswiftnav/external/local_config_cc/cc_wrapper.sh'
AR='/usr/bin/libtool'
CFLAGS='-U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer -g0 -O2 -D_FORTIFY_SOURCE=1 -DNDEBUG -ffunction-sections -fdata-sections -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__=redacted -D__TIMESTAMP__=redacted -D__TIME__=redacted '
CPPFLAGS=''
DEFS='-DHAVE_CONFIG_H'
ARFLAGS='-static -s -o'
LDFLAGS='-rdynamic -undefined dynamic_lookup -headerpad_max_install_names -lc++ -lm -undefined error'
ALLOCA=''
LOADLIBES='   -Wl,-framework -Wl,CoreFoundation'
REMOTE='stub'
OBJEXT='o'
EXEEXT=''
+ ./build.sh
config.status: creating ./lib/alloca.h__
config.status: creating ./lib/fcntl.h__
config.status: creating ./lib/limits.h__
config.status: creating ./lib/fnmatch.h__
config.status: creating ./lib/glob.h__
config.status: creating ./lib/stdint.h__
config.status: creating ./lib/stdio.h__
config.status: creating ./lib/stdlib.h__
config.status: creating ./lib/string.h__
config.status: creating ./lib/sys/types.h__
config.status: creating ./lib/unistd.h__
compiling lib/concat-filename.c...
compiling lib/dirname-lgpl.c...
compiling lib/basename-lgpl.c...
compiling lib/stripslash.c...
compiling lib/exitfail.c...
compiling lib/fd-hook.c...
compiling lib/findprog-in.c...
./lib/findprog-in.c:137:25: error: call to undeclared function 'eaccess'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                    if (eaccess (progpathname, X_OK) == 0)
                        ^
./lib/findprog-in.c:137:25: note: did you mean 'access'?
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h:431:6: note: 'access' declared here
int      access(const char *, int);
         ^
./lib/findprog-in.c:211:21: error: call to undeclared function 'eaccess'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                if (eaccess (progpathname, X_OK) == 0)
                    ^
2 errors generated.
_____ END BUILD LOGS _____
rules_foreign_cc: Build wrapper script location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make_tool_foreign_cc/wrapper_build_script.sh
rules_foreign_cc: Build script location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make_tool_foreign_cc/build_script.sh
rules_foreign_cc: Build log location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make_tool_foreign_cc/BootstrapGNUMake.log

INFO: Elapsed time: 86.633s, Critical Path: 52.19s
INFO: 52 processes: 23 internal, 29 darwin-sandbox.
FAILED: Build did NOT complete successfully

Error 2:

+ cat build.cfg
# Configuration for building GNU Make in the absence of any 'make' program.
# build.cfg.  Generated from build.cfg.in by configure.

# Copyright (C) 1993-2020 Free Software Foundation, Inc.
# This file is part of GNU Make.
#
# GNU Make is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

# See Makefile.in for comments describing these variables.

top_srcdir='.'

prefix='/private/var/tmp/_bazel_isaac.torres/d66fcbe6a777458f4c1d3687ee3816a5/sandbox/darwin-sandbox/1522/execroot/foo/bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make'
exec_prefix=`eval echo ${prefix}`

CC='/private/var/tmp/_bazel_isaac.torres/d66fcbe6a777458f4c1d3687ee3816a5/sandbox/darwin-sandbox/1522/execroot/starling/external/rules_swiftnav/cc/toolchains/llvm/x86_64-darwin/wrappers/clang --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'
AR='/usr/bin/libtool'
CFLAGS='-U_FORTIFY_SOURCE --target=x86_64-apple-macosx -U_FORTIFY_SOURCE -fstack-protector -fno-omit-frame-pointer -ffp-contract=off -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -g0 -O3 -D_FORTIFY_SOURCE=1 -ffunction-sections -fdata-sections -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__=redacted -D__TIMESTAMP__=redacted -D__TIME__=redacted '
CPPFLAGS=''
DEFS='-DHAVE_CONFIG_H'
ARFLAGS='-static -s -o'
LDFLAGS='-rdynamic --target=x86_64-apple-macosx -lm -no-canonical-prefixes -headerpad_max_install_names -undefined dynamic_lookup -Wl,-no_fixup_chains -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lc++ -lc++abi -L/private/var/tmp/_bazel_isaac.torres/d66fcbe6a777458f4c1d3687ee3816a5/sandbox/darwin-sandbox/1522/execroot/foo/external/x86_64-darwin-llvm/lib -undefined error'
ALLOCA=''
LOADLIBES='  '
REMOTE='stub'
OBJEXT='o'
EXEEXT=''
+ ./build.sh
config.status: creating ./lib/alloca.h__
config.status: creating ./lib/fcntl.h__
config.status: creating ./lib/limits.h__
config.status: creating ./lib/fnmatch.h__
config.status: creating ./lib/glob.h__
config.status: creating ./lib/stdint.h__
config.status: creating ./lib/stdio.h__
config.status: creating ./lib/stdlib.h__
config.status: creating ./lib/string.h__
config.status: creating ./lib/sys/types.h__
config.status: creating ./lib/unistd.h__
compiling lib/concat-filename.c...
compiling lib/dirname-lgpl.c...
compiling lib/basename-lgpl.c...
compiling lib/stripslash.c...
compiling lib/exitfail.c...
compiling lib/fd-hook.c...
compiling lib/findprog-in.c...
./lib/findprog-in.c:137:25: warning: implicit declaration of function 'eaccess' is invalid in C99 [-Wimplicit-function-declaration]
                    if (eaccess (progpathname, X_OK) == 0)
                        ^
./lib/findprog-in.c:211:21: warning: implicit declaration of function 'eaccess' is invalid in C99 [-Wimplicit-function-declaration]
                if (eaccess (progpathname, X_OK) == 0)
                    ^
2 warnings generated.
compiling lib/getprogname.c...
compiling lib/fnmatch.c...
compiling lib/glob.c...
./lib/glob.c:823:27: warning: incompatible pointer types passing 'char **' to parameter of type 'char *'; dereference with * [-Wincompatible-pointer-types]
            = (char **) realloc (pglob->gl_pathv,
                                 ^~~~~~~~~~~~~~~
                                 *
./lib/glob.c:942:24: warning: incompatible pointer types passing 'char **' to parameter of type 'char *'; dereference with * [-Wincompatible-pointer-types]
                = (char **) realloc (pglob->gl_pathv,
                                     ^~~~~~~~~~~~~~~
                                     *
./lib/glob.c:997:39: warning: incompatible pointer types passing 'char **' to parameter of type 'char *'; dereference with * [-Wincompatible-pointer-types]
              new_pathv = (char **) realloc (pglob->gl_pathv,
                                             ^~~~~~~~~~~~~~~
                                             *
./lib/glob.c:1388:23: warning: incompatible pointer types passing 'char **' to parameter of type 'char *'; dereference with * [-Wincompatible-pointer-types]
        = (char **) realloc (pglob->gl_pathv,
                             ^~~~~~~~~~~~~~~
                             *
4 warnings generated.
compiling lib/unistd.c...
compiling lib/xmalloc.c...
compiling lib/xalloc-die.c...
compiling lib/xconcat-filename.c...
compiling lib/close.c...
compiling lib/error.c...
compiling lib/msvc-inval.c...
./lib/msvc-inval.c:124:7: warning: implicit declaration of function '_set_invalid_parameter_handler' is invalid in C99 [-Wimplicit-function-declaration]
      _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler);
      ^
1 warning generated.
compiling lib/msvc-nothrow.c...
./lib/msvc-nothrow.c:25:10: fatal error: 'windows.h' file not found
#include <windows.h>
         ^~~~~~~~~~~
1 error generated.
_____ END BUILD LOGS _____
rules_foreign_cc: Build wrapper script location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make_tool_foreign_cc/wrapper_build_script.sh
rules_foreign_cc: Build script location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make_tool_foreign_cc/build_script.sh
rules_foreign_cc: Build log location: bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make_tool_foreign_cc/BootstrapGNUMake.log

INFO: Elapsed time: 727.325s, Critical Path: 92.92s
INFO: 2122 processes: 405 internal, 1717 darwin-sandbox.
FAILED: Build did NOT complete successfully
jungleraptor added a commit to swift-nav/rules_swiftnav that referenced this issue Oct 6, 2023
The recent macos sdk v15 update has caused a number of issues with our
builds that require various tweaks to our build settings to fix.

## Switch to ninja for cmake generator

The `cmake` build process involves bootstrapping a gnu make binary from
source. The update has caused some strange build errors during this
process. More info can be found in the ticket I've created here:
bazel-contrib/rules_foreign_cc#1099

In the meantime we can just switch to using the ninja generator.

## Drop support for shared linking

We make liberal use of "weak linking" for various purposes across the
codebase - usually implemented using function pointers for (I assume)
portability.

However, this means that most of our libraries can't really be built as
shared libraries due to resulting "undefined reference" errors.

In bazel the build system will by default always try to build the shared
library unless `linkstatic` is set to `True`. So on mac this was causing
errors during the normal build. Not sure why linux was not having
problems (all our CMake builds fail when `-DBUILD_SHARED_LIBS=TRUE`).

To fix this I added `-undefined dynamic_lookup` to the link flags on mac
to tell the linker we'll supply the symbols later at the final link
step.

Now with the update, for whatever reason these flags appear to be
causing the following crash on m1 macs when the linker tries to link a
binary:
```
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
0  0x102277648  __assert_rtn + 72
1  0x10224c6c8  ld::AtomFileConsolidator::addAtomFile(ld::AtomFile const*, ld::AtomFile const*, bool) + 4860
2  0x1022581d0  ld::AtomFileConsolidator::addAtomFile(ld::AtomFile const*) + 148
3  0x102275b20  ld::pass::stubs(ld::Options const&, ld::AtomFileConsolidator&) + 1644
4  0x10225f84c  ld::AtomFileConsolidator::resolve() + 12232
5  0x1021ea1a8  main + 9104
ld: Assertion failed: (slot < _sideTableBuffer.size()), function addAtom, file AtomFileConsolidator.cpp, line 2158.
clang: error: linker command failed with exit code 1 (use -v to see invocation)
INFO: Elapsed time: 190.377s, Critical Path: 36.73s
INFO: 1484 processes: 210 internal, 1274 darwin-sandbox.
FAILED: Build did NOT complete successfully
```

Since we've never really supported shared linking except in some
exceptional cases, I've disabled shared library builds by default across
all our code and thus can remove the problematic flags (and probably get
a perf boost as well).

## Make the minimum macos target consistent

Another annoyance with the mac builds was that the bazel code was
getting compiled with a different macos deployment target than the cmake
libraries, which resulting in the build output getting spammed with
linker warnings referencing this difference.

On my system the bazel target was `13.0`, while the cmake value was
`13.6`. I'm not sure how (as of our current version of bazel 6.2) this
value is computed.

In CMake it's interpolated from the system.

To fix this I've wired up our toolchain to respect the
[macos_minimum_os](https://bazel.build/reference/command-line-reference#flag--macos_minimum_os)
flags and add `-mmacos-version-min` to our build. This is just taken
from the most recent `HEAD` of `bazel` which is not in 6.x.

The CMake side requires explicitly setting
[CMAKE_OSX_DEPLOYMENT_TARGET](https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_DEPLOYMENT_TARGET.html)
variable to the same value.

It would be better if `rules_foreign_cc` also respected the
`macos_minimum_os` flag so the value would not have to be hardcoded. We
can perhaps open a ticket to request this.

`13.0` seems like a reasonable minimum deployment target since these
builds are only intended for development machines.

## Testing

I've tested these changes on m1 and intel macs as well as the following
test PR's:
- https://github.com/swift-nav/starling/pull/8677
- https://github.com/swift-nav/orion-engine/pull/7239
@iamricard
Copy link

iamricard commented Oct 12, 2023

We're observing this too. From what I could tell, the final LDFLAGS passed to bootstrap GNUMake end up being -Wl,-S -headerpad_max_install_names -lc++ -lm -undefined error -undefined dynamic_lookup. Which seems to make it so that the -undefined dynamic_lookup overrides -undefined error (which was a fixed introduced in #894).

This patch seems to fix it:

From f84270c6bd0b513dbd77431688ab387ddd0f4916 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ricard=20Sol=C3=A9?= <[email protected]>
Date: Thu, 12 Oct 2023 18:03:11 +0200
Subject: [PATCH] gnumake: unsets -undefined dynamic_build if found

---
 foreign_cc/built_tools/make_build.bzl | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/foreign_cc/built_tools/make_build.bzl b/foreign_cc/built_tools/make_build.bzl
index 0baaa5c..f807d27 100644
--- a/foreign_cc/built_tools/make_build.bzl
+++ b/foreign_cc/built_tools/make_build.bzl
@@ -71,8 +71,10 @@ def _make_tool_impl(ctx):
         if absolute_ar == "libtool" or absolute_ar.endswith("/libtool"):
             arflags.append("-o")
 
+        _joined_non_sysroot_ldflags = _join_flags_list(ctx.workspace_name, non_sysroot_ldflags)
         if os_name(ctx) == "macos":
-            non_sysroot_ldflags += ["-undefined", "error"]
+            _joined_non_sysroot_ldflags = _joined_non_sysroot_ldflags.replace("-undefined dynamic_lookup", "")
+            _joined_non_sysroot_ldflags += " -undefined error"
 
         env.update({
             "AR": absolute_ar,
@@ -80,7 +82,7 @@ def _make_tool_impl(ctx):
             "CC": absolute_cc,
             "CFLAGS": _join_flags_list(ctx.workspace_name, non_sysroot_cflags),
             "LD": absolute_ld,
-            "LDFLAGS": _join_flags_list(ctx.workspace_name, non_sysroot_ldflags),
+            "LDFLAGS": _joined_non_sysroot_ldflags,
         })
 
         configure_env = " ".join(["%s=\"%s\"" % (key, value) for key, value in env.items()])
-- 
2.38.1

ohadravid added a commit to ohadravid/rules_foreign_cc that referenced this issue Oct 19, 2023
goniz pushed a commit to goniz/rules_foreign_cc that referenced this issue Oct 22, 2023
@vymao
Copy link

vymao commented Oct 31, 2023

Believe I am seeing this as well, MacOS 13.5

(transformers-v2) victor@Victors-MBP cpp % pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 15.0.0.0.1.1690355577
volume: /
location: /
install-time: 1691358267

@codingl2k1
Copy link

Same issue on MacOS 14

pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 15.0.0.0.1.1694021235
volume: /
location: /
install-time: 1696925492

I am using the latest version: rules_foreign_cc-0.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants