Skip to content

Add missing bsd link libraries - #859

Closed
keith wants to merge 2 commits into
bazelbuild:mainfrom
keith:ks/add-missing-openbsd-link-flags
Closed

keith wants to merge 2 commits into
bazelbuild:mainfrom
keith:ks/add-missing-openbsd-link-flags

Conversation

@keith

@keith keith commented Aug 31, 2026

Copy link
Copy Markdown
Member

This mirrors this behavior from the clang driver:

https://github.com/llvm/llvm-project/blob/3e892fad65730692057da27bebb2e34d3ad7bde7/clang/lib/Driver/ToolChains/OpenBSD.cpp#L365-L366

The same does not apply for freebsd (I didn't check netbsd which isn't
supported here right now).

Alternatively we could use clang++ for linking directly instead of
clang, which would add these automatically, but that would make it
harder to disable these in the case you are linking pure C

Fixes #857

@keith keith changed the title Add missing openbsd link flags Add missing bsd link libraries Sep 1, 2026
trybka
trybka previously approved these changes Sep 1, 2026

action_configs = [objcopy_embed_data_action] if is_bsd else []
extra_default_link_flags = []
if cpu == "openbsd":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do I even want to know why this is stuffed into cpu?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think separately we should probably pass through the constraints for OS here, since we would need that to have the correct condition for both arm64 (currently not supported) and x86_64, but I just figured I wouldn't mess with it right now, this has been here since the beginning

@zakinko

zakinko commented Sep 1, 2026

Copy link
Copy Markdown

Measured on FreeBSD 14.3, where -lm is needed as well: building Bazel 9.2.0
from source stops at 336 of 5,576 actions on undefined symbol: ceilf,
referenced from libc++'s __hash_table when it rehashes, and completes with
this change. c++ -### there passes -lc++ -lm, so the C++ driver was
supplying it and the C driver used here is not.

On OpenBSD 7.9 I get the same set you have, one library at a time: nothing
links with 20 undefined symbols, -lc++ leaves 19, -lc++abi leaves 1
(ceilf), -lm leaves 0 but libc++.so still wants pthread_join, and
-lpthread links.

netbsd and dragonfly are not in is_bsd at all, so they get the
stub_armeabi-v7a identifier rather than this toolchain. I have those
building on a fork and can send that separately if it is wanted.

@keith

keith commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

can you clarify if you're saying this works for you on freebsd/openbsd, or what libraries are missing otherwise

@keith

keith commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

dragonfly bsd would need a new entry here first probably https://github.com/bazelbuild/platforms/blob/27e914a43e306c61e8584a6c6bcd9c1123e9024c/os/BUILD#L16-L30

This mirrors this behavior from the clang driver:

https://github.com/llvm/llvm-project/blob/3e892fad65730692057da27bebb2e34d3ad7bde7/clang/lib/Driver/ToolChains/OpenBSD.cpp#L365-L366

The same does not apply for freebsd (I didn't check netbsd which isn't
supported here right now).

Alternatively we could use clang++ for linking directly instead of
clang, which would add these automatically, but that would make it
harder to disable these in the case you are linking pure C

Fixes bazelbuild#857
@zakinko

zakinko commented Sep 6, 2026 •

Copy link
Copy Markdown

Yes, both. With this change I have now built Bazel from source, with no
bootstrap Bazel at all, on all five BSDs:

  OpenBSD 7.9     6,245 actions
  FreeBSD 15.1    6,306 actions
  GhostBSD 26.1   6,308 actions
  NetBSD 11.0     6,308 actions
  DragonFly 6.4   6,687 actions

each verified by running the resulting binary on a genrule. On FreeBSD the
only thing missing on top of what you already have is -lm; without it the
build stops on undefined symbol: ceilf out of libc++'s __hash_table.
The per-library breakdown on OpenBSD is in my first comment.

Agreed on dragonfly — @platforms//os has netbsd but no dragonfly, so I
have sent the constraint as bazelbuild/platforms#143 and will keep both out
of this PR. #862 is mine and now redundant with this one; shall I repurpose
it for the netbsd/dragonfly additions once #143 lands, or just close it?

@copybara-service copybara-service Bot closed this in 2c06f12 Sep 7, 2026
zakinko added a commit to zakinko/platforms that referenced this pull request Sep 9, 2026
DragonFly BSD has no constraint_value here, so nothing can select on it
and no toolchain can name it.  Rules that already handle the other BSDs
run into this from the outside: bazelbuild/rules_cc#859 adds link flags
for FreeBSD and OpenBSD, and DragonFly cannot join them until the
constraint exists.

The host platform extension has the same gap from the inside.  The JVM
reports os.name as "DragonFlyBSD" there, which Bazel lowercases to
"dragonflybsd", so _translate_os falls through and returns None.  A host
platform on DragonFly then carries a cpu constraint and no os constraint
at all, which is worse than carrying a wrong one: the absence is silent,
and nothing can select on it either way.

Measured on DragonFly 6.4.2 with openjdk21:

	$ uname -s
	DragonFly
	$ java -XshowSettings:properties -version 2>&1 | grep os.name
	    os.name = DragonFlyBSD

Note that uname and the JVM disagree on the spelling, so startswith()
matters here rather than being a stylistic choice.
3405691582 added a commit to 3405691582/android-cuttlefish that referenced this pull request Oct 7, 2026
On OpenBSD the BSD toolchain in rules_cc drives the link with clang
rather than clang++ and only passes -lc++, so libc++abi, libpthread and
libm are missing from every C++ link. rules_cc fixed this in 0.2.24
(bazelbuild/rules_cc#859: -lc++abi and -lpthread when cpu is openbsd,
-lm on both BSDs) and also links with -z origin there
(bazelbuild/rules_cc#855), which OpenBSD's ld.so needs before it will
expand $ORIGIN in a run path. 0.2.25 is the first version with both on
the registry and the one Bazel pins at head. Bazel 8.5.1 and 9.2.0 both
generate @local_config_cc from rules_cc, so the toolchain config
changes on Linux as well.

While we are here, move the bazel_skylib and bazel_features pins up to
what the graph already resolves to, 1.9.0 (wanted by apple_support,
which the new rules_cc brings in) and 1.50.0 (wanted by rules_cc
itself); that silences the two --check_direct_dependencies warnings
without changing the resolved module graph.
3405691582 added a commit to 3405691582/android-cuttlefish that referenced this pull request Oct 8, 2026
On OpenBSD the BSD toolchain in rules_cc drives the link with clang
rather than clang++ and only passes -lc++, so libc++abi, libpthread and
libm are missing from every C++ link. rules_cc fixed this in 0.2.24
(bazelbuild/rules_cc#859: -lc++abi and -lpthread when cpu is openbsd,
-lm on both BSDs) and also links with -z origin there
(bazelbuild/rules_cc#855), which OpenBSD's ld.so needs before it will
expand $ORIGIN in a run path. 0.2.25 is the first version with both on
the registry and the one Bazel pins at head. Bazel 8.5.1 and 9.2.0 both
generate @local_config_cc from rules_cc, so the toolchain config
changes on Linux as well.

While we are here, move the bazel_skylib and bazel_features pins up to
what the graph already resolves to, 1.9.0 (wanted by apple_support,
which the new rules_cc brings in) and 1.50.0 (wanted by rules_cc
itself); that silences the two --check_direct_dependencies warnings
without changing the resolved module graph.
3405691582 added a commit to 3405691582/android-cuttlefish that referenced this pull request Oct 10, 2026
On OpenBSD the BSD toolchain in rules_cc drives the link with clang
rather than clang++ and only passes -lc++, so libc++abi, libpthread and
libm are missing from every C++ link. rules_cc fixed this in 0.2.24
(bazelbuild/rules_cc#859: -lc++abi and -lpthread when cpu is openbsd,
-lm on both BSDs) and also links with -z origin there
(bazelbuild/rules_cc#855), which OpenBSD's ld.so needs before it will
expand $ORIGIN in a run path. 0.2.25 is the first version with both on
the registry and the one Bazel pins at head. Bazel 8.5.1 and 9.2.0 both
generate @local_config_cc from rules_cc, so the toolchain config
changes on Linux as well.

While we are here, move the bazel_skylib and bazel_features pins up to
what the graph already resolves to, 1.9.0 (wanted by apple_support,
which the new rules_cc brings in) and 1.50.0 (wanted by rules_cc
itself); that silences the two --check_direct_dependencies warnings
without changing the resolved module graph.
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

Successfully merging this pull request may close these issues.

OpenBSD: linking with libc++ may require -lc++abi, -pthread and -lm

4 participants