Skip to content

Commit

Permalink
Move (and enable) remaining java rules & providers out of @_builtins
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 697997376
Change-Id: I1ef76949530cd89498826f2bae32b92d57c29f04
  • Loading branch information
hvadehra authored and rules_java Copybara committed Nov 19, 2024
1 parent ba2744a commit a156fe9
Show file tree
Hide file tree
Showing 39 changed files with 702 additions and 208 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ REMOTE_JDK_REPOS = [(("remote_jdk" if version == "8" else "remotejdk") + version
[register_toolchains("@" + name + "_toolchain_config_repo//:all") for name in REMOTE_JDK_REPOS]

# Compatibility layer
compat = use_extension("//java:extensions.bzl", "compatibility_proxy")
compat = use_extension("//java:rules_java_deps.bzl", "compatibility_proxy")
use_repo(compat, "compatibility_proxy")

# Dev dependencies
Expand Down
4 changes: 3 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ http_archive(
],
)

load("//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
load("//java:rules_java_deps.bzl", "rules_java_dependencies")

rules_java_dependencies()

load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility

proto_bazel_features(name = "proto_bazel_features")

load("//java:repositories.bzl", "rules_java_toolchains")

rules_java_toolchains()

load("@stardoc//:setup.bzl", "stardoc_repositories")
Expand Down
2 changes: 1 addition & 1 deletion java/bazel/rules/bazel_java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain")
load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load(
"//java/common/rules:android_lint.bzl",
Expand All @@ -26,6 +25,7 @@ load("//java/common/rules:rule_util.bzl", "merge_attrs")
load("//java/common/rules/impl:java_binary_deploy_jar.bzl", "create_deploy_archives")
load("//java/common/rules/impl:java_binary_impl.bzl", "basic_java_binary")
load("//java/common/rules/impl:java_helper.bzl", "helper")
load("//java/private:java_info.bzl", "JavaInfo")

def _bazel_java_binary_impl(ctx):
return bazel_base_binary_impl(ctx, is_test_rule_class = False) + helper.executable_providers(ctx)
Expand Down
6 changes: 2 additions & 4 deletions java/bazel/rules/bazel_java_binary_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ This is needed since the `executable` nature of the target must be computed from
the supplied value of the `create_executable` attribute.
"""

load("//java/common:java_common.bzl", "java_common")
load(
"//java/common/rules:java_binary_wrapper.bzl",
"register_java_binary_rules",
"register_legacy_java_binary_rules",
)
load("//java/private:native.bzl", "get_internal_java_common")
load(":bazel_java_binary.bzl", java_bin_exec = "java_binary")
load(":bazel_java_binary_nonexec.bzl", java_bin_nonexec = "java_binary")

_java_common_internal = java_common.internal_DO_NOT_USE()

# copybara: default visibility

def java_binary(**kwargs):
if _java_common_internal.incompatible_disable_non_executable_java_binary():
if get_internal_java_common().incompatible_disable_non_executable_java_binary():
register_java_binary_rules(
java_bin_exec,
**kwargs
Expand Down
2 changes: 1 addition & 1 deletion java/bazel/rules/bazel_java_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
Definition of java_import rule.
"""

load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules:java_import.bzl", "JAVA_IMPORT_ATTRS")
load("//java/common/rules/impl:bazel_java_import_impl.bzl", "bazel_java_import_rule")
load("//java/private:java_info.bzl", "JavaInfo")

def _proxy(ctx):
return bazel_java_import_rule(
Expand Down
2 changes: 1 addition & 1 deletion java/bazel/rules/bazel_java_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
Definition of java_library rule.
"""

load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
load("//java/common/rules:java_library.bzl", "JAVA_LIBRARY_ATTRS")
load("//java/common/rules/impl:bazel_java_library_impl.bzl", "bazel_java_library_rule")
load("//java/private:java_info.bzl", "JavaInfo")

def _proxy(ctx):
return bazel_java_library_rule(
Expand Down
2 changes: 1 addition & 1 deletion java/bazel/rules/bazel_java_plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
Definition of java_plugin rule.
"""

load("//java/common:java_plugin_info.bzl", "JavaPluginInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
load("//java/common/rules:java_library.bzl", "JAVA_LIBRARY_IMPLICIT_ATTRS")
load("//java/common/rules:java_plugin.bzl", "JAVA_PLUGIN_ATTRS")
load("//java/common/rules:rule_util.bzl", "merge_attrs")
load("//java/common/rules/impl:basic_java_library_impl.bzl", "basic_java_library", "construct_defaultinfo")
load("//java/private:java_info.bzl", "JavaPluginInfo")

def bazel_java_plugin_rule(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion java/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bzl_library(
name = "common",
srcs = glob(["*.bzl"]),
visibility = ["//visibility:public"],
deps = ["//java/private:native_bzl"],
deps = ["//java/private:internals"],
)

filegroup(
Expand Down
4 changes: 2 additions & 2 deletions java/common/java_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.
"""java_common module"""

load("//java/private:native.bzl", "native_java_common")
load("@compatibility_proxy//:proxy.bzl", _java_common = "java_common")

java_common = native_java_common
java_common = _java_common
4 changes: 2 additions & 2 deletions java/common/java_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.
"""JavaInfo provider"""

load("//java/private:native.bzl", "NativeJavaInfo")
load("@compatibility_proxy//:proxy.bzl", _JavaInfo = "JavaInfo")

JavaInfo = NativeJavaInfo
JavaInfo = _JavaInfo
4 changes: 2 additions & 2 deletions java/common/java_plugin_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.
"""JavaPluginInfo provider"""

load("//java/private:native.bzl", "NativeJavaPluginInfo")
load("@compatibility_proxy//:proxy.bzl", _JavaPluginInfo = "JavaPluginInfo")

JavaPluginInfo = NativeJavaPluginInfo
JavaPluginInfo = _JavaPluginInfo
6 changes: 2 additions & 4 deletions java/common/java_semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ system property.
},
)

tokenize_javacopts = cc_helper.tokenize

PLATFORMS_ROOT = "@platforms//"

semantics = struct(
JAVA_TOOLCHAIN_LABEL = "@bazel_tools//tools/jdk:current_java_toolchain",
JAVA_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:toolchain_type",
Expand Down Expand Up @@ -108,4 +104,6 @@ semantics = struct(
for_attribute = lambda name: _DOCS.ATTRS.get(name, ""),
),
minimize_cc_info = _minimize_cc_info,
tokenize_javacopts = cc_helper.tokenize,
PLATFORMS_ROOT = "@platforms//",
)
6 changes: 3 additions & 3 deletions java/common/rules/android_lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

"""Creates the android lint action for java rules"""

load("//java/common:java_semantics.bzl", "semantics", _semantics_tokenize_javacopts = "tokenize_javacopts")
load("//java/common:java_semantics.bzl", "semantics")

# copybara: default visibility

def _tokenize_opts(opts_depset):
opts = reversed(opts_depset.to_list())
return _semantics_tokenize_javacopts(opts)
return semantics.tokenize_javacopts(opts)

def _android_lint_action(ctx, source_files, source_jars, compilation_info):
"""
Expand Down Expand Up @@ -112,7 +112,7 @@ def _android_lint_action(ctx, source_files, source_jars, compilation_info):
args.add_all(linter.lint_opts)

for package_config in linter.package_config:
if package_config.matches(ctx.label):
if package_config.matches(package_config.package_specs, ctx.label):
# wrap in a list so that map_each passes the depset to _tokenize_opts
package_opts = [package_config.javac_opts]
args.add_all(package_opts, map_each = _tokenize_opts)
Expand Down
4 changes: 2 additions & 2 deletions java/common/rules/basic_java_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
Common code for reuse across java_* rules
"""

load("//java/common:java_common.bzl", "java_common")
load("//java/common:java_plugin_info.bzl", "JavaPluginInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/private:java_common.bzl", "java_common")
load("//java/private:java_info.bzl", "JavaPluginInfo")
load(":rule_util.bzl", "merge_attrs")

visibility([
Expand Down
10 changes: 3 additions & 7 deletions java/common/rules/impl/basic_java_library_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@ Common code for reuse across java_* rules
"""

load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//java/common:java_common.bzl", "java_common")
load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_plugin_info.bzl", "JavaPluginInfo")
load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
load("//java/private:boot_class_path_info.bzl", "BootClassPathInfo")
load("//java/private:java_common_internal.bzl", "target_kind")
load("//java/private:java_info.bzl", "JavaInfo", "JavaPluginInfo")
load(":compile_action.bzl", "compile_action")
load(":proguard_validation.bzl", "validate_proguard_specs")

visibility([
"//java/...",
])

_java_common_internal = java_common.internal_DO_NOT_USE()
BootClassPathInfo = java_common.BootClassPathInfo
target_kind = _java_common_internal.target_kind

def _filter_srcs(srcs, ext):
return [f for f in srcs if f.extension == ext]

Expand Down
8 changes: 3 additions & 5 deletions java/common/rules/impl/bazel_java_import_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ Definition of java_import rule.
"""

load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//java/common:java_common.bzl", "java_common")
load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules/impl:basic_java_library_impl.bzl", "construct_defaultinfo")
load("//java/common/rules/impl:import_deps_check.bzl", "import_deps_check")
load("//java/private:java_common.bzl", "java_common")
load("//java/private:java_common_internal.bzl", _run_ijar_private_for_builtins = "run_ijar")
load("//java/private:java_info.bzl", "JavaInfo")
load(":proguard_validation.bzl", "validate_proguard_specs")

# copybara: default visibility

_java_common_internal = java_common.internal_DO_NOT_USE()
_run_ijar_private_for_builtins = _java_common_internal.run_ijar_private_for_builtins

def _filter_provider(provider, *attrs):
return [dep[provider] for attr in attrs for dep in attr if provider in dep]

Expand Down
4 changes: 1 addition & 3 deletions java/common/rules/impl/compile_action.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ Java compile action
"""

load("//java/common:java_semantics.bzl", "semantics")
load("//java/private:java_common_internal.bzl", _compile_private_for_builtins = "compile")

visibility("private")

_java_common_internal = java_common.internal_DO_NOT_USE()
_compile_private_for_builtins = _java_common_internal.compile

def _filter_strict_deps(mode):
return "error" if mode in ["strict", "default"] else mode

Expand Down
15 changes: 7 additions & 8 deletions java/common/rules/impl/java_binary_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@
load("@com_google_protobuf//bazel/common:proto_info.bzl", "ProtoInfo")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//java/common:java_common.bzl", "java_common")
load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules/impl:basic_java_library_impl.bzl", "basic_java_library", "collect_deps")
load("//java/private:java_common.bzl", "java_common")
load(
"//java/private:java_common_internal.bzl",
"collect_native_deps_dirs",
"get_runtime_classpath_for_archive",
)
load("//java/private:java_info.bzl", "JavaCompilationInfo", "JavaInfo", "to_java_binary_info")
load(":java_binary_deploy_jar.bzl", "create_deploy_archive")
load(":java_helper.bzl", "helper")

# copybara: default visibility

_java_common_internal = java_common.internal_DO_NOT_USE()
JavaCompilationInfo = _java_common_internal.JavaCompilationInfo
collect_native_deps_dirs = _java_common_internal.collect_native_deps_dirs
get_runtime_classpath_for_archive = _java_common_internal.get_runtime_classpath_for_archive
to_java_binary_info = _java_common_internal.to_java_binary_info

InternalDeployJarInfo = provider(
"Provider for passing info to deploy jar rule",
fields = [
Expand Down
30 changes: 30 additions & 0 deletions java/common/rules/impl/java_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,35 @@ def _get_relative(path_a, path_b):
return path_b
return paths.normalize(paths.join(path_a, path_b))

def _tokenize_javacopts(ctx = None, opts = []):
"""Tokenizes a list or depset of options to a list.
Iff opts is a depset, we reverse the flattened list to ensure right-most
duplicates are preserved in their correct position.
If the ctx parameter is omitted, a slow, but pure Starlark, implementation
of shell tokenization is used. Otherwise, tokenization is performed using
ctx.tokenize() which has significantly better performance (up to 100x for
large options lists).
Args:
ctx: (RuleContext|None) the rule context
opts: (depset[str]|[str]) the javac options to tokenize
Returns:
[str] list of tokenized options
"""
if hasattr(opts, "to_list"):
opts = reversed(opts.to_list())
if ctx:
return [
token
for opt in opts
for token in ctx.tokenize(opt)
]
else:
# TODO: optimize and use the pure Starlark implementation in cc_helper
return semantics.tokenize_javacopts(opts)

helper = struct(
collect_all_targets_as_deps = _collect_all_targets_as_deps,
filter_launcher_for_target = _filter_launcher_for_target,
Expand All @@ -474,6 +503,7 @@ helper = struct(
create_single_jar = _create_single_jar,
shell_escape = _shell_escape,
detokenize_javacopts = _detokenize_javacopts,
tokenize_javacopts = _tokenize_javacopts,
derive_output_file = _derive_output_file,
is_stamping_enabled = _is_stamping_enabled,
get_relative = _get_relative,
Expand Down
25 changes: 12 additions & 13 deletions java/common/rules/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//java/common:java_common.bzl", "java_common")
load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_plugin_info.bzl", "JavaPluginInfo")
load("//java/common:java_semantics.bzl", "PLATFORMS_ROOT", "semantics")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/private:java_common.bzl", "java_common")
load("//java/private:java_info.bzl", "JavaInfo", "JavaPluginInfo")
load("//java/private:native.bzl", "get_internal_java_common")
load(":basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS")
load(":rule_util.bzl", "merge_attrs")

# copybara: default visibility

BootClassPathInfo = java_common.BootClassPathInfo

_java_common_internal = java_common.internal_DO_NOT_USE()
_PLATFORMS_ROOT = semantics.PLATFORMS_ROOT

BASIC_JAVA_BINARY_ATTRIBUTES = merge_attrs(
BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS,
Expand Down Expand Up @@ -321,10 +320,10 @@ binaries and not libraries, due to the danger of namespace conflicts.
),
"_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE),
"_windows_constraints": attr.label_list(
default = [paths.join(PLATFORMS_ROOT, "os:windows")],
default = [paths.join(_PLATFORMS_ROOT, "os:windows")],
),
"_build_info_translator": attr.label(default = semantics.BUILD_INFO_TRANSLATOR_LABEL),
} | ({} if _java_common_internal.incompatible_disable_non_executable_java_binary() else {"create_executable": attr.bool(default = True, doc = "Deprecated, use <code>java_single_jar</code> instead.")}),
} | ({} if get_internal_java_common().incompatible_disable_non_executable_java_binary() else {"create_executable": attr.bool(default = True, doc = "Deprecated, use <code>java_single_jar</code> instead.")}),
)

BASE_TEST_ATTRIBUTES = {
Expand Down Expand Up @@ -367,11 +366,11 @@ The Java class to be loaded by the test runner.<br/>
"env_inherit": attr.string_list(),
"_apple_constraints": attr.label_list(
default = [
paths.join(PLATFORMS_ROOT, "os:ios"),
paths.join(PLATFORMS_ROOT, "os:macos"),
paths.join(PLATFORMS_ROOT, "os:tvos"),
paths.join(PLATFORMS_ROOT, "os:visionos"),
paths.join(PLATFORMS_ROOT, "os:watchos"),
paths.join(_PLATFORMS_ROOT, "os:ios"),
paths.join(_PLATFORMS_ROOT, "os:macos"),
paths.join(_PLATFORMS_ROOT, "os:tvos"),
paths.join(_PLATFORMS_ROOT, "os:visionos"),
paths.join(_PLATFORMS_ROOT, "os:watchos"),
],
),
"_legacy_any_type_attrs": attr.string_list(default = ["stamp"]),
Expand Down
2 changes: 1 addition & 1 deletion java/common/rules/java_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
Definition of java_import rule.
"""

load("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/private:java_info.bzl", "JavaInfo")

# copybara: default visibility

Expand Down
Loading

0 comments on commit a156fe9

Please sign in to comment.