| """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 = { |
| "linux_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| "linux_arm64": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:arm64", |
| ], |
| ), |
| "linux_s390x": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:s390x", |
| ], |
| ), |
| "linux_ppc64le": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:ppc64le", |
| ], |
| ), |
| "darwin_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:macos", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| "darwin_arm64": struct( |
| compatible_with = [ |
| "@platforms//os:macos", |
| "@platforms//cpu:arm64", |
| ], |
| ), |
| "windows_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:windows", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| "windows_arm64": struct( |
| compatible_with = [ |
| "@platforms//os:windows", |
| "@platforms//cpu:aarch64", |
| ], |
| ), |
| } |
| |
| def _nodejs_toolchains_repo_impl(repository_ctx): |
| # TODO(7.0) Drop support for deprecated alias |
| build_content = '''# Generated by nodejs_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. |
| |
| alias( |
| name = "resolved_toolchain", |
| actual = "@rules_nodejs//nodejs:current_node_runtime", |
| deprecation = """ |
| Use one of the following instead: |
| - @rules_nodejs//nodejs:current_node_runtime |
| - @rules_nodejs//nodejs:current_host_node_runtime |
| - @rules_nodejs//nodejs:current_node_toolchain |
| See https://github.com/bazel-contrib/rules_nodejs/issues/3795. |
| """, |
| visibility = ["//visibility:public"], |
| ) |
| |
| ''' |
| |
| for [platform, meta] in PLATFORMS.items(): |
| build_content += """ |
| toolchain( |
| name = "{platform}_toolchain", |
| exec_compatible_with = {compatible_with}, |
| toolchain = "@{user_node_repository_name}_{platform}//:toolchain", |
| toolchain_type = "@rules_nodejs//nodejs:toolchain_type", |
| ) |
| toolchain( |
| name = "{platform}_runtime_toolchain", |
| target_compatible_with = {compatible_with}, |
| toolchain = "@{user_node_repository_name}_{platform}//:toolchain", |
| toolchain_type = "@rules_nodejs//nodejs:runtime_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) |
| |
| nodejs_toolchains_repo = repository_rule( |
| _nodejs_toolchains_repo_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"), |
| }, |
| ) |