• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 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"""Bazel Java Semantics"""
15
16load("@rules_cc//cc/common:cc_helper.bzl", "cc_helper")
17
18# copybara: default visibility
19
20def _find_java_toolchain(ctx):
21    return ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java
22
23def _find_java_runtime_toolchain(ctx):
24    return ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].java_runtime
25
26def _get_default_resource_path(path, segment_extractor):
27    # Look for src/.../resources to match Maven repository structure.
28    segments = path.split("/")
29    for idx in range(0, len(segments) - 2):
30        if segments[idx] == "src" and segments[idx + 2] == "resources":
31            return "/".join(segments[idx + 3:])
32    java_segments = segment_extractor(path)
33    return "/".join(java_segments) if java_segments != None else path
34
35def _compatible_javac_options(*_args):
36    return depset()
37
38def _check_java_info_opens_exports():
39    pass
40
41def _minimize_cc_info(cc_info):
42    return cc_info
43
44_DOCS = struct(
45    ATTRS = {
46        "resources": """
47<p>
48If resources are specified, they will be bundled in the jar along with the usual
49<code>.class</code> files produced by compilation. The location of the resources inside
50of the jar file is determined by the project structure. Bazel first looks for Maven's
51<a href="https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html">standard directory layout</a>,
52(a "src" directory followed by a "resources" directory grandchild). If that is not
53found, Bazel then looks for the topmost directory named "java" or "javatests" (so, for
54example, if a resource is at <code>&lt;workspace root&gt;/x/java/y/java/z</code>, the
55path of the resource will be <code>y/java/z</code>. This heuristic cannot be overridden,
56however, the <code>resource_strip_prefix</code> attribute can be used to specify a
57specific alternative directory for resource files.
58        """,
59        "use_testrunner": """
60Use the test runner (by default
61<code>com.google.testing.junit.runner.BazelTestRunner</code>) class as the
62main entry point for a Java program, and provide the test class
63to the test runner as a value of <code>bazel.test_suite</code>
64system property.
65        """,
66    },
67)
68
69semantics = struct(
70    JAVA_TOOLCHAIN_LABEL = "@bazel_tools//tools/jdk:current_java_toolchain",
71    JAVA_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:toolchain_type",
72    JAVA_TOOLCHAIN = config_common.toolchain_type("@bazel_tools//tools/jdk:toolchain_type", mandatory = True),
73    find_java_toolchain = _find_java_toolchain,
74    JAVA_RUNTIME_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:runtime_toolchain_type",
75    JAVA_RUNTIME_TOOLCHAIN = config_common.toolchain_type("@bazel_tools//tools/jdk:runtime_toolchain_type", mandatory = True),
76    find_java_runtime_toolchain = _find_java_runtime_toolchain,
77    JAVA_PLUGINS_FLAG_ALIAS_LABEL = "@bazel_tools//tools/jdk:java_plugins_flag_alias",
78    EXTRA_SRCS_TYPES = [],
79    ALLOWED_RULES_IN_DEPS = [
80        "cc_binary",  # NB: linkshared=1
81        "cc_library",
82        "genrule",
83        "genproto",  # TODO(bazel-team): we should filter using providers instead (starlark rule).
84        "java_import",
85        "java_library",
86        "java_proto_library",
87        "java_lite_proto_library",
88        "proto_library",
89        "sh_binary",
90        "sh_library",
91    ],
92    ALLOWED_RULES_IN_DEPS_WITH_WARNING = [],
93    LINT_PROGRESS_MESSAGE = "Running Android Lint for: %{label}",
94    JAVA_STUB_TEMPLATE_LABEL = "@bazel_tools//tools/jdk:java_stub_template.txt",
95    BUILD_INFO_TRANSLATOR_LABEL = "@bazel_tools//tools/build_defs/build_info:java_build_info",
96    JAVA_TEST_RUNNER_LABEL = "@bazel_tools//tools/jdk:TestRunner",
97    IS_BAZEL = True,
98    get_default_resource_path = _get_default_resource_path,
99    compatible_javac_options = _compatible_javac_options,
100    LAUNCHER_FLAG_LABEL = Label("@bazel_tools//tools/jdk:launcher_flag_alias"),
101    PROGUARD_ALLOWLISTER_LABEL = "@bazel_tools//tools/jdk:proguard_whitelister",
102    check_java_info_opens_exports = _check_java_info_opens_exports,
103    DOCS = struct(
104        for_attribute = lambda name: _DOCS.ATTRS.get(name, ""),
105    ),
106    minimize_cc_info = _minimize_cc_info,
107    tokenize_javacopts = cc_helper.tokenize,
108    PLATFORMS_ROOT = "@platforms//",
109)
110