Skip to content

Commit 586b2f4

Browse files
committed
Sync
1 parent 6a27a26 commit 586b2f4

6 files changed

+193
-432
lines changed

toolchain/internal/BUILD.bazel

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ sh_test(
4444
"llvm_distributions.golden.sel.txt",
4545
"llvm_distributions.sel.txt",
4646
],
47-
# Restrict to Linux. While this would also work for MacOS the restriction
48-
# only works for one platform. To select two platforms we would need to use
49-
# `select`. That can be made work with `tag = select...manual`.
50-
target_compatible_with = [
51-
"@platforms//os:linux",
52-
],
47+
target_compatible_with = select({
48+
"@platforms//os:osx": [],
49+
"@platforms//os:linux": [],
50+
"//conditions:default": ["@platforms//:incompatible"],
51+
}),
5352
)

toolchain/internal/common.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def host_info(rctx):
108108
_arch = arch(rctx)
109109

110110
if _os == "linux" and not rctx.attr.exec_os:
111-
(dist_name, dist_version) = _linux_dist(rctx)
111+
dist_name, dist_version = _linux_dist(rctx)
112112
else:
113113
dist_name = os
114114
dist_version = ""

toolchain/internal/llvm_distributions.bzl

Lines changed: 85 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc")
1616
load(
1717
"//toolchain/internal:common.bzl",
18-
_attr_dict = "attr_dict",
19-
_exec_os_arch_dict_value = "exec_os_arch_dict_value",
20-
_host_info = "host_info",
18+
"attr_dict",
19+
"exec_os_arch_dict_value",
20+
"host_info",
2121
)
22-
load("//toolchain/internal:release_name.bzl", _llvm_release_name_context = "llvm_release_name_context")
2322

2423
# If a new LLVM version is missing from this list, please add the shasums here
2524
# and the new version in toolchain/internal/llvm_distributions.golden.txt.
@@ -677,7 +676,7 @@ def _get_auth(ctx, urls):
677676
return {}
678677

679678
def download_llvm(rctx):
680-
"""Download the LLVM for the given context."""
679+
"""Download the LLVM distribution for the given context."""
681680
urls = []
682681
sha256 = None
683682
strip_prefix = None
@@ -705,7 +704,7 @@ def download_llvm(rctx):
705704
lib_path = clang_version.get_child("lib", lib_name)
706705
rctx.file(lib_path, libclang_rt_content, legacy_utf8 = False)
707706

708-
updated_attrs = _attr_dict(rctx.attr)
707+
updated_attrs = attr_dict(rctx.attr)
709708
if update_sha256:
710709
updated_attrs["sha256"].update([(key, res.sha256)])
711710
return updated_attrs
@@ -725,9 +724,9 @@ def _get_llvm_version(rctx):
725724
return rctx.attr.llvm_version
726725
if not rctx.attr.llvm_versions:
727726
fail("Neither 'llvm_version' nor 'llvm_versions' given.")
728-
(_, llvm_version) = _exec_os_arch_dict_value(rctx, "llvm_versions")
729-
info = _host_info(rctx)
727+
(_, llvm_version) = exec_os_arch_dict_value(rctx, "llvm_versions")
730728
if not llvm_version:
729+
info = host_info(rctx)
731730
fail(
732731
"LLVM version string missing for ({os}/{dist_name}/{dist_verison}, {arch})",
733732
os = info.os,
@@ -737,7 +736,14 @@ def _get_llvm_version(rctx):
737736
)
738737
return llvm_version
739738

740-
_UBUNTU_NAMES = ["arch", "linuxmint", "manjaro", "nixos", "pop", "ubuntu"]
739+
_UBUNTU_NAMES = [
740+
"arch",
741+
"linuxmint",
742+
"manjaro",
743+
"nixos",
744+
"pop",
745+
"ubuntu",
746+
]
741747

742748
_UBUNTU_VERSIONS = [
743749
"linux-ubuntu-20.04",
@@ -783,8 +789,8 @@ def _dist_to_os_names(dist, default_os_names = []):
783789
"linux-gnu-Fedora27",
784790
"linux-gnu",
785791
"unknown-linux-gnu",
786-
# The ubuntu list could be replaced with _UBUNTU_VERSIONS which
787-
# spawns more selections and changes a few to neer ubuntu versions.
792+
# The Ubuntu list could be replaced with _UBUNTU_VERSIONS which
793+
# Spawns more selections and changes a few to near Ubuntu versions.
788794
"linux-gnu-ubuntu-22.04",
789795
"linux-gnu-ubuntu-20.04",
790796
"linux-gnu-ubuntu-18.04",
@@ -796,6 +802,10 @@ def _dist_to_os_names(dist, default_os_names = []):
796802
return ["linux-gnueabihf", "linux-gnu"]
797803
if dist.name in ["rhel", "ol", "almalinux"]:
798804
return ["linux-rhel-", "linux-gnu-rhel-"]
805+
if dist.name == "debian":
806+
return [
807+
"linux-gnu-debian8",
808+
] + _UBUNTU_VERSIONS
799809
if dist.name in _UBUNTU_NAMES:
800810
return [
801811
"linux-gnu-ubuntu-" + dist.version,
@@ -822,8 +832,11 @@ def _find_llvm_basenames_by_stem(prefixes, *, is_prefix = False, return_first_ma
822832
basenames.append(llvm_dist)
823833
return basenames
824834

825-
def _find_llvm_basename_list(llvm_version, arch, os, dist):
835+
def _find_llvm_basename_list(llvm_version, host_info):
826836
"""Lookup (llvm_version, arch, os) in the list of basenames in `_llvm_distributions.`"""
837+
arch = host_info.arch
838+
os = host_info.os
839+
dist = host_info.dist
827840

828841
# Prefer new LLVM- distributions is available
829842
basenames = _find_llvm_basenames_by_stem([
@@ -945,71 +958,38 @@ def _find_llvm_basename_list(llvm_version, arch, os, dist):
945958
arch = arch_alias,
946959
dist_name = dist_name,
947960
))
948-
names = _find_llvm_basenames_by_stem(prefixes, is_prefix = True)
949-
if names and dist.name == "ubuntu":
950-
return [names[-1]]
951-
return names
952-
else:
953-
fail("ERROR: Unknown OS: {os}".format(os = os))
961+
return _find_llvm_basenames_by_stem(prefixes, is_prefix = True, return_first_match = True)
962+
return []
954963

955-
def _find_llvm_basename_maybe_fail(llvm_version, arch, os, dist, should_fail):
956-
basenames = _find_llvm_basename_list(llvm_version, arch, os, dist)
964+
def _find_llvm_basename_or_error(llvm_version, host_info):
965+
basenames = _find_llvm_basename_list(llvm_version, host_info)
957966
if len(basenames) > 1:
958-
if fail:
959-
fail("ERROR: Multiple configurations found [{basenames}].".format(
960-
basenames = ", ".join(basenames),
961-
))
962-
return None
967+
return None, "ERROR: Multiple configurations found [{basenames}].".format(
968+
basenames = ", ".join(basenames),
969+
)
963970
if not basenames:
964-
if should_fail:
965-
fail("ERROR: No matching config could be found for version {llvm_version} on {os}/{dist_name}/{dist_version} with arch {arch}.".format(
966-
llvm_version = llvm_version,
967-
os = os,
968-
dist_name = dist.name,
969-
dist_version = dist.version,
970-
arch = arch,
971-
))
972-
return None
971+
return None, "ERROR: No version selected"
972+
# TODO(helly25): Enable better error message:
973+
#"ERROR: No matching config could be found for version {llvm_version} on {os}/{dist_name}/{dist_version} with arch {arch}.".format(
974+
# llvm_version = llvm_version,
975+
# os = host_info.os,
976+
# dist_name = host_info.dist.name,
977+
# dist_version = host_info.dist.version,
978+
# arch = host_info.arch,
979+
#)
973980

974981
# Use the following for debugging:
975982
# print("Found LLVM: " + basenames[0]) # buildifier: disable=print
976-
return basenames[0]
977-
978-
def _major_llvm_version(llvm_version):
979-
"""Return the major version given `<major>['.' <minor> [ '.' <mini> [.*]]]."""
980-
return int(llvm_version.split(".")[0])
981-
982-
def _host_can_be_found(major_llvm_version, host_info):
983-
"""Return whether the basename can be found or needs to be predicted."""
984-
if major_llvm_version >= 19:
985-
return True
986-
if host_info.os in ["darwin", "windows"]:
987-
return True
988-
if _dist_to_os_names(host_info.dist, []):
989-
return True
990-
if host_info.arch in ["aarch64", "armv7a", "mips", "mipsel"]:
991-
return True
992-
993-
return False
994-
995-
def _llvm_release_name(rctx, llvm_version):
996-
"""Try to find the distribution in the configured list. Otherwise predict version name by input.
997-
998-
For versions 19+ or we os==darwin fail if the distribution cannot be found automatically."""
999-
major_llvm_version = _major_llvm_version(llvm_version)
1000-
host_info = _host_info(rctx)
1001-
should_fail = _host_can_be_found(major_llvm_version, host_info)
1002-
basename = _find_llvm_basename_maybe_fail(llvm_version, host_info.arch, host_info.os, host_info.dist, should_fail)
1003-
if basename:
1004-
return basename
1005-
return _llvm_release_name_context(rctx, llvm_version)
983+
return basenames[0], None
1006984

1007985
def _distribution_urls(rctx):
1008986
"""Return LLVM `urls`, `shha256` and `strip_prefix` for the given context."""
1009987
llvm_version = _get_llvm_version(rctx)
1010988

1011989
if rctx.attr.distribution == "auto":
1012-
basename = llvm_release_name_context(rctx, llvm_version)
990+
basename, error = _find_llvm_basename_or_error(rctx, llvm_version)
991+
if error:
992+
fail(error)
1013993
else:
1014994
basename = rctx.attr.distribution
1015995

@@ -1043,11 +1023,8 @@ def _distribution_urls(rctx):
10431023
def _parse_version(v):
10441024
return tuple([int(s) for s in v.split(".")])
10451025

1046-
def _version_ge(lhs, rhs):
1047-
return _parse_version(lhs) >= _parse_version(rhs)
1048-
1049-
def _version_le(lhs, rhs):
1050-
return _parse_version(lhs) <= _parse_version(rhs)
1026+
def _version_string(version):
1027+
return ".".join([str(v) for v in version])
10511028

10521029
def _write_distributions_impl(ctx):
10531030
"""Analyze the configured versions and write to a file for test consumption.
@@ -1076,15 +1053,13 @@ def _write_distributions_impl(ctx):
10761053
"linux",
10771054
"windows",
10781055
]
1079-
ANY_VER = "0" # Version does not matter, but must be valie integer
1056+
ANY_VERSION = "0" # Version does not matter, but must be a valid integer
10801057
dist_dict_list = {
10811058
"linux": [
10821059
# struct(name = "ibm-aix", version = "7.2"), unreachable
1083-
# struct(name = "linux-gnu-debian", version = "8"), unreachable
1084-
# struct(name = "linux-gnu-rhel", version = "8.4"), unreachable
10851060
# keep sorted
1086-
struct(name = "amzn", version = ANY_VER),
1087-
struct(name = "arch", version = ANY_VER),
1061+
struct(name = "amzn", version = ANY_VERSION),
1062+
struct(name = "arch", version = ANY_VERSION),
10881063
struct(name = "centos", version = "6"),
10891064
struct(name = "centos", version = "7"),
10901065
struct(name = "debian", version = "0"),
@@ -1100,8 +1075,8 @@ def _write_distributions_impl(ctx):
11001075
struct(name = "linuxmint", version = "18"),
11011076
struct(name = "linuxmint", version = "19"),
11021077
struct(name = "pc-solaris", version = "2.11"),
1103-
struct(name = "raspbian", version = ANY_VER),
1104-
struct(name = "rhel", version = ANY_VER),
1078+
struct(name = "raspbian", version = ANY_VERSION),
1079+
struct(name = "rhel", version = ANY_VERSION),
11051080
struct(name = "sun-solaris", version = "2.11"),
11061081
struct(name = "suse", version = "11.3"),
11071082
struct(name = "suse", version = "12.2"),
@@ -1123,19 +1098,20 @@ def _write_distributions_impl(ctx):
11231098
}
11241099

11251100
# Compute all unique version strings starting with `MIN_VERSION`.
1126-
MIN_VERSION = "6.0.0"
1101+
MIN_VERSION = _parse_version("6.0.0")
1102+
MAX_VERSION = _parse_version("20.1.3")
11271103
version_list = []
11281104
for name in _llvm_distributions.keys():
11291105
for prefix in ["LLVM-", "clang+llvm-"]:
11301106
if name.startswith(prefix):
1131-
version = name.split("-", 2)[1]
1132-
if _version_ge(version, MIN_VERSION):
1107+
version = _parse_version(name.split("-", 2)[1])
1108+
if version >= MIN_VERSION:
11331109
version_list.append(version)
11341110
break
1135-
for version in _llvm_distributions_base_url.keys():
1136-
if not _version_ge(version, MIN_VERSION):
1137-
continue
1138-
version_list.append(version)
1111+
for v in _llvm_distributions_base_url.keys():
1112+
version = _parse_version(v)
1113+
if version >= MIN_VERSION:
1114+
version_list.append(version)
11391115
versions = {v: None for v in version_list}
11401116

11411117
# Write versions to output to check which versions we take into account.
@@ -1156,6 +1132,7 @@ def _write_distributions_impl(ctx):
11561132
# At the end we add the not-found versions as False.
11571133
result = {}
11581134

1135+
# Collect cases that produce duplicates (or multiple) basenames.
11591136
dupes = []
11601137

11611138
# For all versions X arch X os check if we can compute the distribution.
@@ -1164,35 +1141,44 @@ def _write_distributions_impl(ctx):
11641141
for os in os_list:
11651142
dist_list = dist_dict_list.get(os, [struct(name = os, version = "")])
11661143
for dist in dist_list:
1167-
basenames = _find_llvm_basename_list(version, arch, os, dist)
1168-
if _version_le(version, "20.1.3"):
1169-
if len(basenames) == 0:
1170-
predicted = "ERROR: No version selected"
1171-
elif len(basenames) == 1:
1172-
predicted = basenames[0]
1173-
else:
1174-
predicted = "ERROR: Multiple selections"
1175-
if not predicted.startswith("ERROR:"):
1144+
host_info = struct(
1145+
arch = arch,
1146+
os = os,
1147+
dist = dist,
1148+
)
1149+
basenames = _find_llvm_basename_list(_version_string(version), host_info)
1150+
if version <= MAX_VERSION:
1151+
predicted, error = _find_llvm_basename_or_error(
1152+
_version_string(version),
1153+
host_info,
1154+
)
1155+
if not error:
11761156
if predicted.endswith(".exe"):
1177-
predicted = "ERROR: Windows .exe is not supported: " + predicted
1157+
error = "ERROR: Windows .exe is not supported: " + predicted
11781158
elif predicted not in _llvm_distributions:
1179-
predicted = "ERROR: Unavailable prediction: " + predicted
1159+
error = "ERROR: Unavailable prediction: " + predicted
1160+
elif len(basenames) == 0:
1161+
predicted = "ERROR: No version selected"
1162+
elif len(basenames) == 1:
1163+
predicted = basenames[0]
11801164
else:
1165+
predicted = "ERROR: Multiple selections"
1166+
if not error:
11811167
arch_found = [arch for arch in arch_list if arch in predicted]
11821168
if len(arch_found) == 1 and arch_found[0] != arch:
1183-
predicted = "ERROR: Bad arch selection: " + predicted
1169+
error = "ERROR: Bad arch selection: " + predicted
11841170
select.append("{version}-{arch}-{os}/{dist_name}/{dist_version} -> {basename}".format(
1185-
version = version,
1171+
version = _version_string(version),
11861172
arch = arch,
11871173
os = os,
11881174
dist_name = dist.name,
11891175
dist_version = dist.version,
1190-
basename = predicted,
1176+
basename = error or predicted,
11911177
))
11921178
if len(basenames) != 1:
11931179
if basenames:
11941180
dupes.append("dup: {version}-{arch}-{os}-{dist_name}-{dist_version} -> {count}".format(
1195-
version = version,
1181+
version = _version_string(version),
11961182
arch = arch,
11971183
os = os,
11981184
dist_name = dist.name,

0 commit comments

Comments
 (0)