| # |
| # Copyright (c) 2024 Boeing |
| # |
| # This program and the accompanying materials are made |
| # available under the terms of the Eclipse Public License 2.0 |
| # which is available at https://www.eclipse.org/legal/epl-2.0/ |
| # |
| # SPDX-License-Identifier: EPL-2.0 |
| # |
| # Contributors: |
| # Boeing - initial API and implementation |
| # |
| # higly derivative from rules_nodejs |
| PLATFORMS = { |
| "linux_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:linux", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| "windows_amd64": struct( |
| compatible_with = [ |
| "@platforms//os:windows", |
| "@platforms//cpu:x86_64", |
| ], |
| ), |
| } |
| |
| def _toolchains_repo_impl(repository_ctx): |
| starlark_content = """# Generated by toolchains_repo.bzl |
| |
| # Forward all the providers |
| def _resolved_toolchain_impl(ctx): |
| toolchain_info = ctx.toolchains["@rules_osee//bat:toolchain_type"] |
| return [ |
| toolchain_info, |
| toolchain_info.default, |
| toolchain_info.batinfo, |
| toolchain_info.template_variables |
| ] |
| |
| # Copied from java_toolchain_alias |
| # https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl |
| resolved_toolchain = rule( |
| implementation = _resolved_toolchain_impl, |
| toolchains = ["@rules_osee//bat:toolchain_type"], |
| incompatible_use_toolchain_transition = True, |
| ) |
| """ |
| repository_ctx.file("defs.bzl", starlark_content) |
| |
| 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 bat_register_toolchains macro |
| # so you don't normally need to interact with these targets. |
| |
| load(":defs.bzl", "resolved_toolchain") |
| |
| resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"]) |
| |
| """ |
| |
| for [platform, meta] in PLATFORMS.items(): |
| build_content += """ |
| toolchain( |
| name = "{platform}_toolchain", |
| exec_compatible_with = {compatible_with}, |
| toolchain = "@{user_bat_repository_name}_{platform}//:bat_toolchain", |
| toolchain_type = "@rules_osee//bat:toolchain_type", |
| ) |
| toolchain( |
| name = "{platform}_toolchain_target", |
| target_compatible_with = {compatible_with}, |
| toolchain = "@{user_bat_repository_name}_{platform}//:bat_toolchain", |
| toolchain_type = "@rules_osee//bat:toolchain_type", |
| ) |
| """.format( |
| platform = platform, |
| name = repository_ctx.attr.name, |
| user_bat_repository_name = repository_ctx.attr.user_bat_repository_name, |
| compatible_with = meta.compatible_with, |
| ) |
| |
| # Base BUILD file for this repository |
| repository_ctx.file("BUILD.bazel", build_content) |
| |
| toolchains_repo = repository_rule( |
| _toolchains_repo_impl, |
| doc = """Creates a repository with toolchain definitions for all known platforms |
| which can be registered or selected.""", |
| attrs = { |
| "user_bat_repository_name": attr.string(doc = "what the user chose for the base name, eg. 1.0.0"), |
| }, |
| ) |