-
Notifications
You must be signed in to change notification settings - Fork 2k
Make CODEQL_PLATFORM architecture-aware for linux-arm64 #22247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+98
−28
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0b6e96
Make CODEQL_PLATFORM architecture-aware for linux-arm64
redsun82 b6e7464
Address review: linux-arm64 docs + None-vs-falsey fallback
redsun82 ba3fce1
Add `posix` convenience to os_select
redsun82 0c20a33
Anchor linux_arm64 select key to the codeql repo
redsun82 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| codeql_platform = select({ | ||
| "@platforms//os:linux": "linux64", | ||
| "@platforms//os:macos": "osx64", | ||
| "@platforms//os:windows": "win64", | ||
| }) | ||
| load("//misc/bazel:os.bzl", "codeql_platform_select") | ||
|
|
||
| codeql_platform = codeql_platform_select( | ||
| linux64 = "linux64", | ||
| linux_arm64 = "linux-arm64", | ||
| osx64 = "osx64", | ||
| win64 = "win64", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,88 @@ | ||
| """ Os detection facilities. """ | ||
|
|
||
| def os_select( | ||
| def codeql_platform_select( | ||
| ctx = None, | ||
| *, | ||
| linux = None, | ||
| windows = None, | ||
| macos = None, | ||
| default = None): | ||
| linux64 = None, | ||
| linux_arm64 = None, | ||
| osx64 = None, | ||
| win64 = None, | ||
| otherwise = None): | ||
| """ | ||
| This can work both in a macro and a rule context to choose something based on the current OS. | ||
| If used in a rule implementation, you need to pass `ctx` and add `OS_DETECTION_ATTRS` to the | ||
| rule attributes. | ||
| Choose a value based on the target CodeQL platform, discriminating the four platforms CodeQL | ||
| knows about: `linux64` (Linux on x86_64), `linux_arm64` (Linux on arm64), `osx64` (macOS, any | ||
| architecture) and `win64` (Windows on x86_64). Any platform left unspecified uses `otherwise`. | ||
|
|
||
| There is deliberately no fallback between `linux64` and `linux_arm64`: if you want the same value | ||
| for both (i.e. you only care about the OS, not the architecture), use `os_select` instead. | ||
|
|
||
| This works both in a macro context (`ctx = None`, returning a `select`) and in a rule context | ||
| (passing `ctx`, which then needs `OS_DETECTION_ATTRS` on the rule attributes). | ||
| """ | ||
|
|
||
| def _or_otherwise(value): | ||
| return value if value != None else otherwise | ||
|
|
||
| linux_arm64_setting = Label("//misc/bazel:linux_arm64") | ||
| choices = { | ||
| "linux": linux or default, | ||
| "windows": windows or default, | ||
| "macos": macos or default, | ||
| linux_arm64_setting: _or_otherwise(linux_arm64), | ||
| "@platforms//os:linux": _or_otherwise(linux64), | ||
| "@platforms//os:macos": _or_otherwise(osx64), | ||
| "@platforms//os:windows": _or_otherwise(win64), | ||
| } | ||
| if not ctx: | ||
| return select({ | ||
| "@platforms//os:%s" % os: v | ||
| for os, v in choices.items() | ||
| setting: v | ||
| for setting, v in choices.items() | ||
| if v != None | ||
| }) | ||
|
|
||
| for os, v in choices.items(): | ||
| if ctx.target_platform_has_constraint(getattr(ctx.attr, "_%s_constraint" % os)[platform_common.ConstraintValueInfo]): | ||
| if v == None: | ||
| fail("%s not supported by %s" % (os, ctx.label)) | ||
| return v | ||
| fail("Unknown OS detected") | ||
| def has(constraint): | ||
| return ctx.target_platform_has_constraint(getattr(ctx.attr, "_%s_constraint" % constraint)[platform_common.ConstraintValueInfo]) | ||
|
|
||
| if has("linux"): | ||
| result = choices[linux_arm64_setting] if has("arm64") else choices["@platforms//os:linux"] | ||
| elif has("macos"): | ||
| result = choices["@platforms//os:macos"] | ||
| elif has("windows"): | ||
| result = choices["@platforms//os:windows"] | ||
| else: | ||
| fail("Unknown OS detected") | ||
| if result == None: | ||
| fail("platform not supported by %s" % ctx.label) | ||
| return result | ||
|
|
||
| def os_select( | ||
| ctx = None, | ||
| *, | ||
| linux = None, | ||
| windows = None, | ||
| macos = None, | ||
| posix = None, | ||
| default = None): | ||
| """ | ||
| Choose a value based on the target OS, ignoring the architecture. This is a thin, OS-only wrapper | ||
| around `codeql_platform_select` (Linux gets the same value on both x86_64 and arm64). | ||
| `posix` is a convenience for the value shared by `linux` and `macos`; it is mutually exclusive | ||
| with both. See `codeql_platform_select` for macro vs rule usage. | ||
| """ | ||
| if posix != None: | ||
| if linux != None or macos != None: | ||
| fail("`posix` is mutually exclusive with `linux` and `macos`") | ||
| linux = posix | ||
| macos = posix | ||
| return codeql_platform_select( | ||
| ctx, | ||
| linux64 = linux, | ||
| linux_arm64 = linux, | ||
| osx64 = macos, | ||
| win64 = windows, | ||
| otherwise = default, | ||
| ) | ||
|
|
||
| OS_DETECTION_ATTRS = { | ||
| "_windows_constraint": attr.label(default = "@platforms//os:windows"), | ||
| "_macos_constraint": attr.label(default = "@platforms//os:macos"), | ||
| "_linux_constraint": attr.label(default = "@platforms//os:linux"), | ||
| "_arm64_constraint": attr.label(default = "@platforms//cpu:arm64"), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.