|
1 | 1 | """ Os detection facilities. """ |
2 | 2 |
|
3 | | -def os_select( |
| 3 | +def codeql_platform_select( |
4 | 4 | ctx = None, |
5 | 5 | *, |
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): |
10 | 11 | """ |
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). |
14 | 21 | """ |
15 | 22 | 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, |
19 | 27 | } |
20 | 28 | if not ctx: |
21 | 29 | 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() |
24 | 32 | if v != None |
25 | 33 | }) |
26 | 34 |
|
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 | + ) |
33 | 70 |
|
34 | 71 | OS_DETECTION_ATTRS = { |
35 | 72 | "_windows_constraint": attr.label(default = "@platforms//os:windows"), |
36 | 73 | "_macos_constraint": attr.label(default = "@platforms//os:macos"), |
37 | 74 | "_linux_constraint": attr.label(default = "@platforms//os:linux"), |
| 75 | + "_arm64_constraint": attr.label(default = "@platforms//cpu:arm64"), |
38 | 76 | } |
0 commit comments