Skip to content

Commit 1764c65

Browse files
authored
Merge pull request #22247 from github/redsun82-arm64-platform-string
Make CODEQL_PLATFORM architecture-aware for linux-arm64
2 parents 46e738d + 0c20a33 commit 1764c65

4 files changed

Lines changed: 98 additions & 28 deletions

File tree

defs.bzl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
codeql_platform = select({
2-
"@platforms//os:linux": "linux64",
3-
"@platforms//os:macos": "osx64",
4-
"@platforms//os:windows": "win64",
5-
})
1+
load("//misc/bazel:os.bzl", "codeql_platform_select")
2+
3+
codeql_platform = codeql_platform_select(
4+
linux64 = "linux64",
5+
linux_arm64 = "linux-arm64",
6+
osx64 = "osx64",
7+
win64 = "win64",
8+
)

misc/bazel/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
load("@rules_shell//shell:sh_library.bzl", "sh_library")
22

3+
# Matches the Linux arm64 target, used to give it a distinct `CODEQL_PLATFORM` string
4+
# (`linux-arm64`). Every other configuration keeps its OS-only string.
5+
config_setting(
6+
name = "linux_arm64",
7+
constraint_values = [
8+
"@platforms//os:linux",
9+
"@platforms//cpu:arm64",
10+
],
11+
visibility = ["//visibility:public"],
12+
)
13+
314
sh_library(
415
name = "sh_runfiles",
516
srcs = ["runfiles.sh"],

misc/bazel/os.bzl

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,88 @@
11
""" Os detection facilities. """
22

3-
def os_select(
3+
def codeql_platform_select(
44
ctx = None,
55
*,
6-
linux = None,
7-
windows = None,
8-
macos = None,
9-
default = None):
6+
linux64 = None,
7+
linux_arm64 = None,
8+
osx64 = None,
9+
win64 = None,
10+
otherwise = None):
1011
"""
11-
This can work both in a macro and a rule context to choose something based on the current OS.
12-
If used in a rule implementation, you need to pass `ctx` and add `OS_DETECTION_ATTRS` to the
13-
rule attributes.
12+
Choose a value based on the target CodeQL platform, discriminating the four platforms CodeQL
13+
knows about: `linux64` (Linux on x86_64), `linux_arm64` (Linux on arm64), `osx64` (macOS, any
14+
architecture) and `win64` (Windows on x86_64). Any platform left unspecified uses `otherwise`.
15+
16+
There is deliberately no fallback between `linux64` and `linux_arm64`: if you want the same value
17+
for both (i.e. you only care about the OS, not the architecture), use `os_select` instead.
18+
19+
This works both in a macro context (`ctx = None`, returning a `select`) and in a rule context
20+
(passing `ctx`, which then needs `OS_DETECTION_ATTRS` on the rule attributes).
1421
"""
22+
23+
def _or_otherwise(value):
24+
return value if value != None else otherwise
25+
26+
linux_arm64_setting = Label("//misc/bazel:linux_arm64")
1527
choices = {
16-
"linux": linux or default,
17-
"windows": windows or default,
18-
"macos": macos or default,
28+
linux_arm64_setting: _or_otherwise(linux_arm64),
29+
"@platforms//os:linux": _or_otherwise(linux64),
30+
"@platforms//os:macos": _or_otherwise(osx64),
31+
"@platforms//os:windows": _or_otherwise(win64),
1932
}
2033
if not ctx:
2134
return select({
22-
"@platforms//os:%s" % os: v
23-
for os, v in choices.items()
35+
setting: v
36+
for setting, v in choices.items()
2437
if v != None
2538
})
2639

27-
for os, v in choices.items():
28-
if ctx.target_platform_has_constraint(getattr(ctx.attr, "_%s_constraint" % os)[platform_common.ConstraintValueInfo]):
29-
if v == None:
30-
fail("%s not supported by %s" % (os, ctx.label))
31-
return v
32-
fail("Unknown OS detected")
40+
def has(constraint):
41+
return ctx.target_platform_has_constraint(getattr(ctx.attr, "_%s_constraint" % constraint)[platform_common.ConstraintValueInfo])
42+
43+
if has("linux"):
44+
result = choices[linux_arm64_setting] if has("arm64") else choices["@platforms//os:linux"]
45+
elif has("macos"):
46+
result = choices["@platforms//os:macos"]
47+
elif has("windows"):
48+
result = choices["@platforms//os:windows"]
49+
else:
50+
fail("Unknown OS detected")
51+
if result == None:
52+
fail("platform not supported by %s" % ctx.label)
53+
return result
54+
55+
def os_select(
56+
ctx = None,
57+
*,
58+
linux = None,
59+
windows = None,
60+
macos = None,
61+
posix = None,
62+
default = None):
63+
"""
64+
Choose a value based on the target OS, ignoring the architecture. This is a thin, OS-only wrapper
65+
around `codeql_platform_select` (Linux gets the same value on both x86_64 and arm64).
66+
`posix` is a convenience for the value shared by `linux` and `macos`; it is mutually exclusive
67+
with both. See `codeql_platform_select` for macro vs rule usage.
68+
"""
69+
if posix != None:
70+
if linux != None or macos != None:
71+
fail("`posix` is mutually exclusive with `linux` and `macos`")
72+
linux = posix
73+
macos = posix
74+
return codeql_platform_select(
75+
ctx,
76+
linux64 = linux,
77+
linux_arm64 = linux,
78+
osx64 = macos,
79+
win64 = windows,
80+
otherwise = default,
81+
)
3382

3483
OS_DETECTION_ATTRS = {
3584
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
3685
"_macos_constraint": attr.label(default = "@platforms//os:macos"),
3786
"_linux_constraint": attr.label(default = "@platforms//os:linux"),
87+
"_arm64_constraint": attr.label(default = "@platforms//cpu:arm64"),
3888
}

misc/bazel/pkg.bzl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_fil
88
load("@rules_pkg//pkg:pkg.bzl", "pkg_zip")
99
load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
1010
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
11-
load("//misc/bazel:os.bzl", "OS_DETECTION_ATTRS", "os_select")
11+
load("//misc/bazel:os.bzl", "OS_DETECTION_ATTRS", "codeql_platform_select")
1212

1313
def _make_internal(name):
1414
def internal(suffix = "internal", *args):
@@ -26,7 +26,13 @@ def _expand_path(path, platform):
2626
return ("common", path)
2727

2828
def _detect_platform(ctx = None):
29-
return os_select(ctx, linux = "linux64", macos = "osx64", windows = "win64")
29+
return codeql_platform_select(
30+
ctx,
31+
linux64 = "linux64",
32+
linux_arm64 = "linux-arm64",
33+
osx64 = "osx64",
34+
win64 = "win64",
35+
)
3036

3137
def codeql_pkg_files(
3238
*,
@@ -458,12 +464,12 @@ def codeql_pack(
458464
`zips` is a map from `.zip` files to prefixes to import.
459465
The distinction between arch-specific and common contents is made based on whether the paths (including possible
460466
prefixes added by rules) contain the special `{CODEQL_PLATFORM}` placeholder, which in case it is present will also
461-
be replaced by the appropriate platform (`linux64`, `win64` or `osx64`).
467+
be replaced by the appropriate platform (`linux64`, `linux-arm64`, `win64` or `osx64`).
462468
Specific file paths can be placed in the arch-specific package by adding them to `arch_overrides`, even if their
463469
path doesn't contain the `CODEQL_PLATFORM` placeholder.
464470
465471
The codeql pack rules will expand the `{CODEQL_PLATFORM}` marker in paths, and use that to split the files into a common and an arch-specific part.
466-
This placeholder will be replaced by the appropriate platform (`linux64`, `win64` or `osx64`).
472+
This placeholder will be replaced by the appropriate platform (`linux64`, `linux-arm64`, `win64` or `osx64`).
467473
`arch_overrides` is a list of files that should be included in the arch-specific bits of the pack, even if their path doesn't
468474
contain the `{CODEQL_PLATFORM}` marker.
469475
All files in the pack will be prefixed with `name`, unless `pack_prefix` is set, then is used instead.

0 commit comments

Comments
 (0)