Skip to content

Commit d0b6e96

Browse files
redsun82Copilot
andcommitted
Make CODEQL_PLATFORM architecture-aware for linux-arm64
CODEQL_PLATFORM is OS-only today (linux->linux64, macos->osx64, windows->win64). ELF has no fat-binary equivalent, so Linux arm64 needs its own string. Add `linux-arm64` for os:linux AND cpu:arm64 while keeping every existing string byte-identical. - Add a public `//misc/bazel:linux_arm64` config_setting (os:linux + cpu:arm64). - Turn `os_select` into `codeql_platform_select`, a full selector over the four CodeQL platforms (`linux64`, `linux_arm64`, `osx64`, `win64`, plus `otherwise`), working in both macro (select) and rule (target_platform_has_constraint) contexts. There is deliberately no fallback between the two Linux slots. - Re-express `os_select` as a thin OS-only wrapper around it (Linux maps to both `linux64` and `linux_arm64`), so its existing swift/xcode callers keep working unchanged. - Add an `_arm64_constraint` entry to OS_DETECTION_ATTRS. - Drive the platform string from `codeql_platform_select` in pkg.bzl's `_detect_platform` and defs.bzl's `codeql_platform`. macOS keeps osx64 for both arch slices (universal binary): the linux_arm64 key requires both constraints, so the OS discriminator dominates. The new branch is dormant on existing CI (no job builds linux-on-arm64), so all current configs produce byte-identical outputs. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c5c5b0bf-4afa-468c-b2dd-197d80932b4b
1 parent 478878c commit d0b6e96

4 files changed

Lines changed: 84 additions & 26 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: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,76 @@
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
"""
1522
choices = {
16-
"linux": linux or default,
17-
"windows": windows or default,
18-
"macos": macos or default,
23+
"//misc/bazel:linux_arm64": linux_arm64 or otherwise,
24+
"@platforms//os:linux": linux64 or otherwise,
25+
"@platforms//os:macos": osx64 or otherwise,
26+
"@platforms//os:windows": win64 or otherwise,
1927
}
2028
if not ctx:
2129
return select({
22-
"@platforms//os:%s" % os: v
23-
for os, v in choices.items()
30+
setting: v
31+
for setting, v in choices.items()
2432
if v != None
2533
})
2634

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")
35+
def has(constraint):
36+
return ctx.target_platform_has_constraint(getattr(ctx.attr, "_%s_constraint" % constraint)[platform_common.ConstraintValueInfo])
37+
38+
if has("linux"):
39+
result = choices["//misc/bazel:linux_arm64"] if has("arm64") else choices["@platforms//os:linux"]
40+
elif has("macos"):
41+
result = choices["@platforms//os:macos"]
42+
elif has("windows"):
43+
result = choices["@platforms//os:windows"]
44+
else:
45+
fail("Unknown OS detected")
46+
if result == None:
47+
fail("platform not supported by %s" % ctx.label)
48+
return result
49+
50+
def os_select(
51+
ctx = None,
52+
*,
53+
linux = None,
54+
windows = None,
55+
macos = None,
56+
default = None):
57+
"""
58+
Choose a value based on the target OS, ignoring the architecture. This is a thin, OS-only wrapper
59+
around `codeql_platform_select` (Linux gets the same value on both x86_64 and arm64).
60+
See `codeql_platform_select` for macro vs rule usage.
61+
"""
62+
return codeql_platform_select(
63+
ctx,
64+
linux64 = linux,
65+
linux_arm64 = linux,
66+
osx64 = macos,
67+
win64 = windows,
68+
otherwise = default,
69+
)
3370

3471
OS_DETECTION_ATTRS = {
3572
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
3673
"_macos_constraint": attr.label(default = "@platforms//os:macos"),
3774
"_linux_constraint": attr.label(default = "@platforms//os:linux"),
75+
"_arm64_constraint": attr.label(default = "@platforms//cpu:arm64"),
3876
}

misc/bazel/pkg.bzl

Lines changed: 8 additions & 2 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
*,

0 commit comments

Comments
 (0)