-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ABLD-395] Framework to create toolchains for OS provided tools #54154
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """toolchain to wrap the codesign binary. | ||
|
|
||
| Type: @codesign//:codesign_toolchain_type | ||
|
|
||
| Toolchains: | ||
| - @codesign//:codesign_toolchain: provides the tool | ||
| - @codesign//:codesign_missing_toolchain: provides a fallback toolchain for | ||
| exec platforms where codesign might not be available. | ||
| """ |
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| """Repository rule to autoconfigure a toolchain using the system codesign.""" | ||
|
|
||
| load("//bazel/toolchains/common:defs.bzl", "make_repo_builder") | ||
|
|
||
| # This must match the name used by register_toolchains in consuming MODULE.bazel files. | ||
| NAME = "macos_codesign" | ||
|
|
||
| build_repo_for_toolchain = make_repo_builder(name = NAME) | ||
|
|
||
| find_macos_codesign = module_extension( | ||
| implementation = lambda ctx: build_repo_for_toolchain(name = NAME), | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # //bazel/toolchains. | ||
|
|
||
| package(default_visibility = ["//visibility:private"]) | ||
|
|
||
| filegroup( | ||
| name = "common", | ||
| srcs = [ | ||
| "toolchain_BUILD.tpl", | ||
| "toolchain_defs.bzl.tpl", | ||
| ], | ||
| visibility = ["//bazel/toolchains:__subpackages__"], | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """Utilitites for creating toolchains to wrap system provided tools.""" | ||
|
|
||
| def write_toolchain_repo(rctx, tool_name, tool_path, tool_version = "<unknown>"): | ||
| if not tool_path: | ||
| tool_path = "" | ||
| rctx.template( | ||
| "BUILD", | ||
| Label("@@//bazel/toolchains/common:toolchain_BUILD.tpl"), | ||
| substitutions = { | ||
| "{GENERATOR}": "@@//bazel/toolchains/{tool_name}/{tool_name}_configure.bzl%find_system_{tool_name}".format( | ||
| tool_name = tool_name, | ||
| ), | ||
| "{TOOL_NAME}": tool_name, | ||
| "{TOOL_PATH}": str(tool_path), | ||
| "{TOOL_VERSION}": tool_version, | ||
| }, | ||
| executable = False, | ||
| ) | ||
| rctx.template( | ||
| "defs.bzl", | ||
| Label("@@//bazel/toolchains/common:toolchain_defs.bzl.tpl"), | ||
| substitutions = { | ||
| "{GENERATOR}": "@@//bazel/toolchains/{tool_name}/{tool_name}_configure.bzl%find_system_{tool_name}".format( | ||
| tool_name = tool_name, | ||
| ), | ||
| "{TOOL_NAME}": tool_name, | ||
| }, | ||
| executable = False, | ||
| ) | ||
|
|
||
| def _default_repo_builder_impl(rctx): | ||
| tool_name = rctx.original_name | ||
| tool_path = rctx.which(tool_name) | ||
| if rctx.attr.verbose: | ||
| if tool_path: | ||
| print("Found %s at '%s'" % (tool_name, tool_path)) # buildifier: disable=print | ||
| else: | ||
| print("No system %s found." % tool_name) # buildifier: disable=print | ||
|
aiuto marked this conversation as resolved.
|
||
| write_toolchain_repo( | ||
| rctx = rctx, | ||
| tool_name = tool_name, | ||
| tool_path = tool_path, | ||
| ) | ||
|
|
||
| def make_repo_builder(name, impl = _default_repo_builder_impl): | ||
| return repository_rule( | ||
| implementation = impl, | ||
| doc = """Create a repository that defines an {name} toolchain based on the system {name}.""".format(name = name), | ||
| local = True, | ||
| environ = ["PATH"], | ||
| attrs = { | ||
| "verbose": attr.bool( | ||
| doc = "If true, print status messages.", | ||
| ), | ||
| }, | ||
| ) | ||
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # This content is generated by {GENERATOR} | ||
| load(":defs.bzl", "is_{TOOL_NAME}_available", "{TOOL_NAME}_toolchain") | ||
|
|
||
| toolchain_type( | ||
| name = "{TOOL_NAME}_toolchain_type", | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| {TOOL_NAME}_toolchain( | ||
| name = "{TOOL_NAME}_auto", | ||
| path = "{TOOL_PATH}", | ||
| version = "{TOOL_VERSION}", | ||
| ) | ||
|
|
||
| toolchain( | ||
| name = "{TOOL_NAME}_toolchain", | ||
| toolchain = ":{TOOL_NAME}_auto", | ||
| toolchain_type = ":{TOOL_NAME}_toolchain_type", | ||
|
aiuto marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| # {TOOL_NAME}_missing_toolchain provides a fallback toolchain so that toolchain | ||
| # resolution can succeed even on platforms that do not have that tool. | ||
| # If this toolchain is selected, the constraint ":have_{TOOL_NAME}" will not be satistifed, | ||
| # so that can be used with with exec_compatible_with clauses. | ||
|
Comment on lines
+23
to
+24
This comment was marked as outdated.
Sorry, something went wrong. |
||
| {TOOL_NAME}_toolchain( | ||
| name = "no_{TOOL_NAME}", | ||
| ) | ||
|
|
||
| toolchain( | ||
| name = "zzz_{TOOL_NAME}_missing_toolchain", # keep name lexicographically last | ||
| toolchain = ":no_{TOOL_NAME}", | ||
| toolchain_type = ":{TOOL_NAME}_toolchain_type", | ||
| ) | ||
|
|
||
| # Expose the availability of an actual otool as a feature flag, so we can | ||
| # create a config_setting from it. | ||
| is_{TOOL_NAME}_available( | ||
| name = "is_{TOOL_NAME}_available", | ||
| ) | ||
|
|
||
| # Expose the availability of the toolchain as a config_setting, so we can | ||
| # select() on it. | ||
| config_setting( | ||
| name = "have_{TOOL_NAME}", | ||
| flag_values = { | ||
| ":is_{TOOL_NAME}_available": "1", | ||
| }, | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """toolchain to provide the {TOOL_NAME} binary.""" | ||
|
|
||
| load("@@//bazel/toolchains:toolchain_info.bzl", "ToolInfo") | ||
|
|
||
| def _{TOOL_NAME}_toolchain_impl(ctx): | ||
| if ctx.attr.label and ctx.attr.path: | ||
| fail("{TOOL_NAME}_toolchain must not specify both label and path.") | ||
| valid = bool(ctx.attr.label) or bool(ctx.attr.path) | ||
| toolchain_info = platform_common.ToolchainInfo( | ||
| {TOOL_NAME} = ToolInfo( | ||
| name = str(ctx.label), | ||
| valid = valid, | ||
| label = ctx.attr.label, | ||
| path = ctx.attr.path, | ||
| version = ctx.attr.version, | ||
| ), | ||
| ) | ||
| return [toolchain_info] | ||
|
|
||
| {TOOL_NAME}_toolchain = rule( | ||
| implementation = _{TOOL_NAME}_toolchain_impl, | ||
| attrs = { | ||
| "label": attr.label( | ||
| doc = "A valid label of a target to build or a prebuilt binary. Mutually exclusive with path.", | ||
| cfg = "exec", | ||
| executable = True, | ||
| allow_files = True, | ||
| ), | ||
| "path": attr.string( | ||
| doc = "The path to the executable. Mutually exclusive with label.", | ||
| ), | ||
| "version": attr.string( | ||
| doc = "The version string of the executable. This should be manually set.", | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
| # Expose the presence of {TOOL_NAME} in the resolved toolchain as a flag. | ||
| # The signal is to look at the default toolchain of the {TOOL_NAME}_toolchan_type | ||
| # and see that it is valid (that is, it has a path). | ||
| def _is_{TOOL_NAME}_available_impl(ctx): | ||
| toolchain = ctx.toolchains["@{TOOL_NAME}//:{TOOL_NAME}_toolchain_type"].{TOOL_NAME} | ||
| return [config_common.FeatureFlagInfo( | ||
| value = ("1" if toolchain.valid else "0"), | ||
| )] | ||
|
|
||
| is_{TOOL_NAME}_available = rule( | ||
| implementation = _is_{TOOL_NAME}_available_impl, | ||
| attrs = {}, | ||
| toolchains = ["@{TOOL_NAME}//:{TOOL_NAME}_toolchain_type"], | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| """Toolchain to wrap the pkgbuild binary. | ||
|
|
||
| Type: @macos_pkgbuild//:tool_toolchain_type | ||
|
|
||
| Toolchains: | ||
| - @macos_pkgbuild//:macos_pkgbuild_toolchain: provides the tool | ||
| - @macos_pkgbuild//:macos_pkgbuild_missing_toolchain: provides a fallback toolchain for exec platforms where pkgbuild might not be available. | ||
| """ |
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| """Repository rule to autoconfigure a toolchain using the system pkgbuild.""" | ||
|
|
||
| load("//bazel/toolchains/common:defs.bzl", "make_repo_builder") | ||
|
|
||
| # This must match the name used by register_toolchains in MODULE.bazel. | ||
| NAME = "macos_pkgbuild" | ||
|
|
||
| build_repo_for_toolchain = make_repo_builder(name = NAME) | ||
|
|
||
| find_macos_pkgbuild = module_extension( | ||
| implementation = lambda ctx: build_repo_for_toolchain(name = NAME), | ||
| ) |
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.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.