-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builtin): add a toolchain to new core that exposes the node for …
…any platform
- Loading branch information
Showing
8 changed files
with
374 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 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,20 @@ | ||
"Simple rule to test nodejs toolchain" | ||
|
||
def _my_nodejs_impl(ctx): | ||
toolchain = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo | ||
ctx.actions.run( | ||
inputs = toolchain.tool_files + [ctx.file.entry_point], | ||
executable = toolchain.target_tool_path, | ||
arguments = [ctx.file.entry_point.path, ctx.outputs.out.path], | ||
outputs = [ctx.outputs.out], | ||
) | ||
return [] | ||
|
||
my_nodejs = rule( | ||
implementation = _my_nodejs_impl, | ||
attrs = { | ||
"entry_point": attr.label(allow_single_file = True), | ||
"out": attr.output(), | ||
}, | ||
toolchains = ["@rules_nodejs//nodejs:toolchain_type"], | ||
) |
This file contains 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 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 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,53 @@ | ||
"Provide convenience repository for the host platform like @nodejs" | ||
|
||
load("//nodejs/private:os_name.bzl", "os_name") | ||
|
||
def _nodejs_host_os_alias_impl(repository_ctx): | ||
# Base BUILD file for this repository | ||
repository_ctx.file("BUILD.bazel", """# Generated by nodejs_repo_host_os_alias.bzl | ||
package(default_visibility = ["//visibility:public"]) | ||
# aliases for exports_files | ||
alias(name = "run_npm.sh.template", actual = "@{node_repository}_{os_name}//:run_npm.sh.template") | ||
alias(name = "run_npm.bat.template", actual = "@{node_repository}_{os_name}//:run_npm.bat.template") | ||
alias(name = "bin/node_repo_args.sh", actual = "@{node_repository}_{os_name}//:bin/node_repo_args.sh") | ||
# aliases for other aliases | ||
alias(name = "node_bin", actual = "@{node_repository}_{os_name}//:node_bin") | ||
alias(name = "npm_bin", actual = "@{node_repository}_{os_name}//:npm_bin") | ||
alias(name = "npx_bin", actual = "@{node_repository}_{os_name}//:npx_bin") | ||
alias(name = "yarn_bin", actual = "@{node_repository}_{os_name}//:yarn_bin") | ||
alias(name = "node", actual = "@{node_repository}_{os_name}//:node") | ||
alias(name = "npm", actual = "@{node_repository}_{os_name}//:npm") | ||
alias(name = "yarn", actual = "@{node_repository}_{os_name}//:yarn") | ||
alias(name = "npm_node_repositories", actual = "@{node_repository}_{os_name}//:npm_node_repositories") | ||
alias(name = "yarn_node_repositories", actual = "@{node_repository}_{os_name}//:yarn_node_repositories") | ||
alias(name = "node_files", actual = "@{node_repository}_{os_name}//:node_files") | ||
alias(name = "yarn_files", actual = "@{node_repository}_{os_name}//:yarn_files") | ||
alias(name = "npm_files", actual = "@{node_repository}_{os_name}//:npm_files") | ||
exports_files(["index.bzl"]) | ||
""".format( | ||
node_repository = repository_ctx.attr.user_node_repository_name, | ||
os_name = os_name(repository_ctx), | ||
)) | ||
|
||
# index.bzl file for this repository | ||
repository_ctx.file("index.bzl", content = """# Generated by nodejs_repo_host_os_alias.bzl | ||
host_platform="{host_platform}" | ||
""".format(host_platform = os_name(repository_ctx))) | ||
|
||
nodejs_repo_host_os_alias = repository_rule( | ||
_nodejs_host_os_alias_impl, | ||
doc = """Creates a repository with a shorter name meant for the host platform, which contains | ||
- A BUILD.bazel file declaring aliases to the host platform's node binaries | ||
- index.bzl containing some constants | ||
""", | ||
attrs = { | ||
"user_node_repository_name": attr.string( | ||
default = "nodejs", | ||
doc = "User-provided name from the workspace file, eg. node16", | ||
), | ||
# FIXME: this seems unused, but not the time to make that edit right now | ||
"node_version": attr.string(), | ||
}, | ||
) |
This file contains 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,99 @@ | ||
"""Create a repository to hold the toolchains | ||
This follows guidance here: | ||
https://docs.bazel.build/versions/main/skylark/deploying.html#registering-toolchains | ||
" | ||
Note that in order to resolve toolchains in the analysis phase | ||
Bazel needs to analyze all toolchain targets that are registered. | ||
Bazel will not need to analyze all targets referenced by toolchain.toolchain attribute. | ||
If in order to register toolchains you need to perform complex computation in the repository, | ||
consider splitting the repository with toolchain targets | ||
from the repository with <LANG>_toolchain targets. | ||
Former will be always fetched, | ||
and the latter will only be fetched when user actually needs to build <LANG> code. | ||
" | ||
The "complex computation" in our case is simply downloading the node binaries from nodejs.org. | ||
This guidance tells us how to avoid that: we put the toolchain targets in the alias repository | ||
with only the toolchain attribute pointing into the platform-specific repositories. | ||
""" | ||
|
||
PLATFORMS = { | ||
"darwin_amd64": struct( | ||
compatible_with = [ | ||
"@platforms//os:macos", | ||
"@platforms//cpu:x86_64", | ||
], | ||
), | ||
"darwin_arm64": struct( | ||
compatible_with = [ | ||
"@platforms//os:macos", | ||
"@platforms//cpu:aarch64", | ||
], | ||
), | ||
"linux_amd64": struct( | ||
compatible_with = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:x86_64", | ||
], | ||
), | ||
"linux_arm64": struct( | ||
compatible_with = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:aarch64", | ||
], | ||
), | ||
"windows_amd64": struct( | ||
compatible_with = [ | ||
"@platforms//os:windows", | ||
"@platforms//cpu:x86_64", | ||
], | ||
), | ||
"linux_s390x": struct( | ||
compatible_with = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:s390x", | ||
], | ||
), | ||
"linux_ppc64le": struct( | ||
compatible_with = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:ppc", | ||
], | ||
), | ||
} | ||
|
||
def _impl(repository_ctx): | ||
build_content = """# Generated by toolchains_repo.bzl | ||
# | ||
# These can be registered in the workspace file or passed to --extra_toolchains flag. | ||
# By default all these toolchains are registered by the nodejs_register_toolchains macro | ||
# so you don't normally need to interact with these targets. | ||
""" | ||
|
||
for [platform, meta] in PLATFORMS.items(): | ||
build_content += """ | ||
toolchain( | ||
name = "{platform}_toolchain", | ||
exec_compatible_with = {compatible_with}, | ||
target_compatible_with = {compatible_with}, | ||
toolchain = "@{user_node_repository_name}_{platform}//:node_toolchain", | ||
toolchain_type = "@rules_nodejs//nodejs:toolchain_type", | ||
) | ||
""".format( | ||
platform = platform, | ||
name = repository_ctx.attr.name, | ||
user_node_repository_name = repository_ctx.attr.user_node_repository_name, | ||
compatible_with = meta.compatible_with, | ||
) | ||
|
||
# Base BUILD file for this repository | ||
repository_ctx.file("BUILD.bazel", build_content) | ||
|
||
toolchains_repo = repository_rule( | ||
_impl, | ||
doc = """Creates a repository with toolchain definitions for all known platforms | ||
which can be registered or selected.""", | ||
attrs = { | ||
"user_node_repository_name": attr.string(doc = "what the user chose for the base name, eg. node16"), | ||
}, | ||
) |
Oops, something went wrong.