1# Copyright 2020 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Rules for importing and registering JDKs from http archive. 16 17Rule remote_java_repository imports and registers JDK with the toolchain resolution. 18""" 19 20load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 21load("//toolchains:jdk_build_file.bzl", "JDK_BUILD_TEMPLATE") 22 23def _toolchain_config_impl(ctx): 24 ctx.file("WORKSPACE", "workspace(name = \"{name}\")\n".format(name = ctx.name)) 25 ctx.file("BUILD.bazel", ctx.attr.build_file) 26 27_toolchain_config = repository_rule( 28 local = True, 29 implementation = _toolchain_config_impl, 30 attrs = { 31 "build_file": attr.string(), 32 }, 33) 34 35def remote_java_repository(name, version, target_compatible_with = None, prefix = "remotejdk", **kwargs): 36 """Imports and registers a JDK from a http archive. 37 38 Toolchain resolution is determined with target_compatible_with 39 parameter and constrained with --java_runtime_version flag either having value 40 of "version" or "{prefix}_{version}" parameters. 41 42 Args: 43 name: A unique name for this rule. 44 version: Version of the JDK imported. 45 target_compatible_with: Target platform constraints (CPU and OS) for this JDK. 46 prefix: Optional alternative prefix for configuration flag value used to determine this JDK. 47 **kwargs: Refer to http_archive documentation 48 """ 49 http_archive( 50 name = name, 51 build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = version), 52 **kwargs 53 ) 54 _toolchain_config( 55 name = name + "_toolchain_config_repo", 56 build_file = """ 57config_setting( 58 name = "prefix_version_setting", 59 values = {{"java_runtime_version": "{prefix}_{version}"}}, 60 visibility = ["//visibility:private"], 61) 62config_setting( 63 name = "version_setting", 64 values = {{"java_runtime_version": "{version}"}}, 65 visibility = ["//visibility:private"], 66) 67alias( 68 name = "version_or_prefix_version_setting", 69 actual = select({{ 70 ":version_setting": ":version_setting", 71 "//conditions:default": ":prefix_version_setting", 72 }}), 73 visibility = ["//visibility:private"], 74) 75toolchain( 76 name = "toolchain", 77 target_compatible_with = {target_compatible_with}, 78 target_settings = [":version_or_prefix_version_setting"], 79 toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type", 80 toolchain = "{toolchain}", 81) 82""".format( 83 prefix = prefix, 84 version = version, 85 target_compatible_with = target_compatible_with, 86 toolchain = "@{repo}//:jdk".format(repo = name), 87 ), 88 ) 89