Skip to content

Commit 8934e81

Browse files
committed
Integrated libc constraints from @platforms_contrib
1 parent 07634cc commit 8934e81

9 files changed

Lines changed: 55 additions & 84 deletions

File tree

MODULE.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ module(
1212
bazel_dep(name = "bazel_features", version = "1.32.0")
1313
bazel_dep(name = "bazel_skylib", version = "1.8.2")
1414
bazel_dep(name = "platforms", version = "1.0.0")
15+
bazel_dep(name = "platforms_contrib", version = "0.3.0")
1516
bazel_dep(name = "rules_cc", version = "0.2.4")
1617
bazel_dep(name = "rules_license", version = "1.0.0")
1718
bazel_dep(name = "rules_shell", version = "0.6.1")
1819
bazel_dep(name = "apple_support", version = "1.24.1", repo_name = "build_bazel_apple_support")
1920

21+
git_override(
22+
module_name = "platforms_contrib",
23+
branch = "fmeum/libc",
24+
remote = "https://github.com/bazel-contrib/platforms_contrib.git",
25+
)
26+
2027
internal_deps = use_extension("//rust/private:internal_extensions.bzl", "i")
2128
use_repo(
2229
internal_deps,

crate_universe/private/crates_repository.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ load("//rust/platform:triple.bzl", "get_host_triple")
2929
SUPPORTED_PLATFORM_TRIPLES = [
3030
"aarch64-apple-darwin",
3131
"aarch64-unknown-linux-gnu",
32+
"aarch64-unknown-linux-musl",
3233
"wasm32-unknown-unknown",
3334
"wasm32-wasip1",
3435
"x86_64-pc-windows-msvc",
3536
"x86_64-unknown-linux-gnu",
37+
"x86_64-unknown-linux-musl",
3638
"x86_64-unknown-nixos-gnu",
3739
]
3840

examples/cross_compile_musl/MODULE.bazel

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ local_path_override(
1010
)
1111

1212
bazel_dep(name = "platforms", version = "1.0.0")
13+
bazel_dep(name = "platforms_contrib", version = "0.3.0")
1314
bazel_dep(name = "rules_shell", version = "0.6.1")
1415
bazel_dep(name = "bazel_skylib", version = "1.8.2")
1516

17+
git_override(
18+
module_name = "platforms_contrib",
19+
branch = "fmeum/libc",
20+
remote = "https://github.com/bazel-contrib/platforms_contrib.git",
21+
)
22+
1623
RUST_EDITION = "2021"
1724

1825
RUST_VERSION = "1.80.0"
@@ -23,84 +30,41 @@ rust.toolchain(
2330
versions = [RUST_VERSION],
2431
)
2532

26-
# This overrides a default rust_repository_set created by rust_register_toolchain.
27-
# It must be named exactly this.
28-
# Each exec triple needs one of these calls per target triple it supports.
29-
# The first call needs all of the attrs, the subsequent calls should only set name, target_triple, and target_compatible_with.
33+
# Register musl target triples for each exec host.
34+
# target_compatible_with is not needed here because triple_to_constraint_set
35+
# now emits @platforms_contrib//libc:musl for musl triples (and glibc for gnu)
36+
# which is sufficient to distinguish the toolchains.
3037
rust.repository_set(
3138
name = "rust_linux_x86_64",
3239
edition = RUST_EDITION,
3340
exec_triple = "x86_64-unknown-linux-gnu",
34-
target_compatible_with = [
35-
"@//linker_config:unknown",
36-
"@platforms//cpu:x86_64",
37-
"@platforms//os:linux",
38-
],
39-
target_triple = "x86_64-unknown-linux-gnu",
40-
versions = [RUST_VERSION],
41-
)
42-
rust.repository_set(
43-
name = "rust_linux_x86_64",
44-
target_compatible_with = [
45-
"@//linker_config:musl",
46-
"@platforms//cpu:arm64",
47-
"@platforms//os:linux",
48-
],
4941
target_triple = "aarch64-unknown-linux-musl",
42+
versions = [RUST_VERSION],
5043
)
5144
rust.repository_set(
5245
name = "rust_linux_x86_64",
53-
target_compatible_with = [
54-
"@//linker_config:musl",
55-
"@platforms//cpu:x86_64",
56-
"@platforms//os:linux",
57-
],
5846
target_triple = "x86_64-unknown-linux-musl",
5947
)
60-
61-
# We don't need to register a repository_set for exec_triple == target_triple if we're not customising it in any way:
62-
# one will get registered by default.
63-
# But we do for the Linux case above, because we want to add the "@//linker_config:unknown" constraint in that case.
6448
rust.repository_set(
6549
name = "rust_darwin_x86_64",
6650
edition = RUST_EDITION,
6751
exec_triple = "x86_64-apple-darwin",
68-
target_compatible_with = [
69-
"@//linker_config:musl",
70-
"@platforms//cpu:x86_64",
71-
"@platforms//os:linux",
72-
],
7352
target_triple = "x86_64-unknown-linux-musl",
7453
versions = [RUST_VERSION],
7554
)
7655
rust.repository_set(
7756
name = "rust_darwin_x86_64",
78-
target_compatible_with = [
79-
"@//linker_config:musl",
80-
"@platforms//cpu:arm64",
81-
"@platforms//os:linux",
82-
],
8357
target_triple = "aarch64-unknown-linux-musl",
8458
)
8559
rust.repository_set(
8660
name = "rust_darwin_aarch64",
8761
edition = RUST_EDITION,
8862
exec_triple = "aarch64-apple-darwin",
89-
target_compatible_with = [
90-
"@//linker_config:musl",
91-
"@platforms//cpu:x86_64",
92-
"@platforms//os:linux",
93-
],
9463
target_triple = "x86_64-unknown-linux-musl",
9564
versions = [RUST_VERSION],
9665
)
9766
rust.repository_set(
9867
name = "rust_darwin_aarch64",
99-
target_compatible_with = [
100-
"@//linker_config:musl",
101-
"@platforms//cpu:arm64",
102-
"@platforms//os:linux",
103-
],
10468
target_triple = "aarch64-unknown-linux-musl",
10569
)
10670
use_repo(rust, "rust_toolchains")
@@ -125,5 +89,5 @@ bazel_dep(name = "toolchains_musl", version = "0.1.20", dev_dependency = True)
12589

12690
toolchains_musl = use_extension("@toolchains_musl//:toolchains_musl.bzl", "toolchains_musl", dev_dependency = True)
12791
toolchains_musl.config(
128-
extra_target_compatible_with = ["@//linker_config:musl"],
92+
extra_target_compatible_with = ["@platforms_contrib//libc:musl"],
12993
)

examples/cross_compile_musl/linker_config/BUILD.bazel

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
platform(
22
name = "linux_x86_64_musl",
33
constraint_values = [
4-
"@//linker_config:musl",
54
"@platforms//cpu:x86_64",
65
"@platforms//os:linux",
6+
"@platforms_contrib//libc:musl",
77
],
88
visibility = ["//visibility:public"],
99
)
1010

1111
platform(
1212
name = "linux_arm64_musl",
1313
constraint_values = [
14-
"@//linker_config:musl",
1514
"@platforms//cpu:arm64",
1615
"@platforms//os:linux",
16+
"@platforms_contrib//libc:musl",
1717
],
1818
visibility = ["//visibility:public"],
1919
)

examples/cross_compile_nix/MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ crates.from_specs(
151151
"aarch64-apple-ios",
152152
"aarch64-linux-android",
153153
"aarch64-unknown-linux-gnu",
154+
"aarch64-unknown-linux-musl",
154155
"wasm32-unknown-unknown",
155156
"wasm32-wasip1",
156157
"x86_64-apple-darwin",
157158
"x86_64-pc-windows-msvc",
158159
"x86_64-unknown-linux-gnu",
160+
"x86_64-unknown-linux-musl",
159161
"x86_64-unknown-nixos-gnu",
160162
],
161163
)

rust/platform/triple_mappings.bzl

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ SUPPORTED_T1_PLATFORM_TRIPLES = {
3232
"x86_64-pc-windows-msvc": _support(std = True, host_tools = True),
3333
"x86_64-unknown-linux-gnu": _support(std = True, host_tools = True),
3434
"x86_64-unknown-nixos-gnu": _support(std = True, host_tools = True), # Same as `x86_64-unknown-linux-gnu` but with `@platforms//os:nixos`.
35-
# N.B. These "alternative" envs are not supported, as bazel cannot distinguish between them
36-
# and others using existing @platforms// config_values
35+
# N.B. These windows-gnu "alternative" envs are not supported, as bazel cannot
36+
# distinguish between MSVC and MinGW using existing @platforms// config_values.
3737
#
3838
#"i686-pc-windows-gnu",
3939
#"x86_64-pc-windows-gnu",
@@ -48,18 +48,23 @@ SUPPORTED_T2_PLATFORM_TRIPLES = {
4848
"aarch64-linux-android": _support(std = True, host_tools = False),
4949
"aarch64-pc-windows-msvc": _support(std = True, host_tools = True),
5050
"aarch64-unknown-fuchsia": _support(std = True, host_tools = False),
51+
"aarch64-unknown-linux-musl": _support(std = True, host_tools = True),
5152
"aarch64-unknown-uefi": _support(std = True, host_tools = False),
5253
"arm-unknown-linux-gnueabi": _support(std = True, host_tools = True),
5354
"arm-unknown-linux-musleabi": _support(std = True, host_tools = True),
5455
"armv7-linux-androideabi": _support(std = True, host_tools = False),
5556
"armv7-unknown-linux-gnueabi": _support(std = True, host_tools = True),
5657
"i686-linux-android": _support(std = True, host_tools = False),
5758
"i686-unknown-freebsd": _support(std = True, host_tools = False),
59+
"i686-unknown-linux-musl": _support(std = True, host_tools = True),
5860
"powerpc-unknown-linux-gnu": _support(std = True, host_tools = True),
61+
"powerpc-unknown-linux-musl": _support(std = True, host_tools = True),
5962
"riscv32imc-unknown-none-elf": _support(std = True, host_tools = False),
6063
"riscv64gc-unknown-linux-gnu": _support(std = True, host_tools = False),
64+
"riscv64gc-unknown-linux-musl": _support(std = True, host_tools = False),
6165
"riscv64gc-unknown-none-elf": _support(std = True, host_tools = False),
6266
"s390x-unknown-linux-gnu": _support(std = True, host_tools = True),
67+
"s390x-unknown-linux-musl": _support(std = True, host_tools = True),
6368
"thumbv6m-none-eabi": _support(std = True, host_tools = False),
6469
"thumbv7em-none-eabi": _support(std = True, host_tools = False),
6570
"thumbv7em-none-eabihf": _support(std = True, host_tools = False),
@@ -74,6 +79,7 @@ SUPPORTED_T2_PLATFORM_TRIPLES = {
7479
"x86_64-linux-android": _support(std = True, host_tools = False),
7580
"x86_64-unknown-freebsd": _support(std = True, host_tools = True),
7681
"x86_64-unknown-fuchsia": _support(std = True, host_tools = False),
82+
"x86_64-unknown-linux-musl": _support(std = True, host_tools = True),
7783
"x86_64-unknown-none": _support(std = True, host_tools = False),
7884
"x86_64-unknown-uefi": _support(std = True, host_tools = False),
7985
}
@@ -268,8 +274,10 @@ _SYSTEM_TO_STDLIB_LINKFLAGS = {
268274
"fuchsia": ["-lzircon", "-lfdio"],
269275
"illumos": ["-lsocket", "-lposix4", "-lpthread", "-lresolv", "-lnsl", "-lumem"],
270276
"ios": ["-lSystem", "-lobjc", "-Wl,-framework,Security", "-Wl,-framework,Foundation", "-lresolv"],
271-
# TODO: This ignores musl. Longer term what does Bazel think about musl?
272-
"linux": ["-ldl", "-lpthread"],
277+
"linux": {
278+
None: ["-ldl", "-lpthread"],
279+
"musl": [],
280+
},
273281
"macos": ["-lSystem", "-lresolv"],
274282
"nacl": [],
275283
"netbsd": ["-lpthread", "-lrt"],
@@ -340,20 +348,18 @@ def abi_to_constraints(abi, *, arch = None, system = None):
340348

341349
all_abi_constraints = []
342350

343-
# add constraints for MUSL static compilation and linking
344-
# to separate the MUSL from the non-MUSL toolchain on x86_64
345-
# if abi == "musl" and system == "linux" and arch == "x86_64":
346-
# all_abi_constraints.append("//rust/platform/constraints:musl_on")
347-
348351
# add constraints for iOS + watchOS simulator and device triples
349352
if system in ["ios", "watchos"]:
350353
if arch == "x86_64" or abi == "sim":
351354
all_abi_constraints.append("@build_bazel_apple_support//constraints:simulator")
352355
else:
353356
all_abi_constraints.append("@build_bazel_apple_support//constraints:device")
354357

355-
# TODO(bazelbuild/platforms#38): Implement when C++ toolchain is more mature and we
356-
# figure out how they're doing this
358+
if abi:
359+
if abi.startswith("musl"):
360+
all_abi_constraints.append("@platforms_contrib//libc:musl")
361+
elif abi.startswith("gnu"):
362+
all_abi_constraints.append("@platforms_contrib//libc:glibc")
357363
return all_abi_constraints
358364

359365
def triple_to_system(target_triple):
@@ -410,8 +416,15 @@ def system_to_staticlib_ext(system):
410416
def system_to_binary_ext(system):
411417
return _SYSTEM_TO_BINARY_EXT[system]
412418

413-
def system_to_stdlib_linkflags(system):
414-
return _SYSTEM_TO_STDLIB_LINKFLAGS[system]
419+
def system_to_stdlib_linkflags(system, abi = None):
420+
flags = _SYSTEM_TO_STDLIB_LINKFLAGS[system]
421+
if type(flags) == "list":
422+
return flags
423+
if abi:
424+
for prefix, abi_flags in flags.items():
425+
if prefix and abi.startswith(prefix):
426+
return abi_flags
427+
return flags.get(None, [])
415428

416429
def triple_to_constraint_set(target_triple):
417430
"""Returns a set of constraints for a given platform triple

rust/private/repository_utils.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def BUILD_for_rust_toolchain(
417417
str: A rendered template of a `rust_toolchain` declaration
418418
"""
419419
if stdlib_linkflags == None:
420-
stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(target_triple.system)])
420+
stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(target_triple.system, target_triple.abi)])
421421

422422
rustfmt_label = None
423423
if include_rustfmt:

tools/rust_analyzer/3rdparty/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ crates_vendor(
5757
"aarch64-apple-darwin",
5858
"aarch64-pc-windows-msvc",
5959
"aarch64-unknown-linux-gnu",
60+
"aarch64-unknown-linux-musl",
6061
"aarch64-unknown-nixos-gnu",
6162
"arm-unknown-linux-gnueabi",
6263
"armv7-linux-androideabi",
@@ -71,6 +72,7 @@ crates_vendor(
7172
"x86_64-pc-windows-msvc",
7273
"x86_64-unknown-freebsd",
7374
"x86_64-unknown-linux-gnu",
75+
"x86_64-unknown-linux-musl",
7476
"x86_64-unknown-nixos-gnu",
7577
],
7678
tags = ["manual"],

0 commit comments

Comments
 (0)