blob: a2535d3c5eed75409846b958a18e44c45ee5c020 [file] [log] [blame]
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rules for importing JDKs from http archive.
Rule remote_java_repository imports a JDK and creates toolchain definitions for it.
"""
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//toolchains:jdk_build_file.bzl", "JDK_BUILD_TEMPLATE")
def _toolchain_config_impl(ctx):
ctx.file("WORKSPACE", "workspace(name = \"{name}\")\n".format(name = ctx.name))
ctx.file("BUILD.bazel", ctx.attr.build_file)
_toolchain_config = repository_rule(
local = True,
implementation = _toolchain_config_impl,
attrs = {
"build_file": attr.string(),
},
)
def remote_java_repository(name, version, target_compatible_with = None, prefix = "remotejdk", **kwargs):
"""Imports a JDK from a http archive and creates runtime toolchain definitions for it.
Register the toolchains defined by this macro via `register_toolchains("@<name>//:all")`, where
`<name>` is the value of the `name` parameter.
Toolchain resolution is determined with target_compatible_with
parameter and constrained with --java_runtime_version flag either having value
of "version" or "{prefix}_{version}" parameters.
Args:
name: A unique name for this rule.
version: Version of the JDK imported.
target_compatible_with: Target platform constraints (CPU and OS) for this JDK.
prefix: Optional alternative prefix for configuration flag value used to determine this JDK.
**kwargs: Refer to http_archive documentation
"""
http_archive(
name = name,
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = version),
**kwargs
)
_toolchain_config(
name = name + "_toolchain_config_repo",
build_file = """
config_setting(
name = "prefix_version_setting",
values = {{"java_runtime_version": "{prefix}_{version}"}},
visibility = ["//visibility:private"],
)
config_setting(
name = "version_setting",
values = {{"java_runtime_version": "{version}"}},
visibility = ["//visibility:private"],
)
alias(
name = "version_or_prefix_version_setting",
actual = select({{
":version_setting": ":version_setting",
"//conditions:default": ":prefix_version_setting",
}}),
visibility = ["//visibility:private"],
)
toolchain(
name = "toolchain",
target_compatible_with = {target_compatible_with},
target_settings = [":version_or_prefix_version_setting"],
toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type",
toolchain = "{toolchain}",
)
toolchain(
name = "bootstrap_runtime_toolchain",
# These constraints are not required for correctness, but prevent fetches of remote JDK for
# different architectures. As every Java compilation toolchain depends on a bootstrap runtime in
# the same configuration, this constraint will not result in toolchain resolution failures.
exec_compatible_with = {target_compatible_with},
target_settings = [":version_or_prefix_version_setting"],
toolchain_type = "@rules_java//toolchains:bootstrap_runtime_toolchain_type",
toolchain = "{toolchain}",
)
""".format(
prefix = prefix,
version = version,
target_compatible_with = target_compatible_with,
toolchain = "@{repo}//:jdk".format(repo = name),
),
)