Skip to content

Commit

Permalink
Enable Starlark java_common
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 545226950
Change-Id: Ie5e3ecac5a64157ea210acfd61be160bf1da3d90
  • Loading branch information
hvadehra authored and copybara-github committed Jul 3, 2023
1 parent 05e3b7b commit f706e7d
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ the generating actions, so that the runfiles symlink tree is staged for the depl

load(":common/java/java_binary_deploy_jar.bzl", "create_deploy_archives", "make_deploy_jars_rule")
load(":common/java/java_binary.bzl", "InternalDeployJarInfo")
load(":common/java/java_common.bzl", "get_build_info")
load(":common/java/java_common_internal_for_builtins.bzl", "get_build_info")

def _stamping_enabled(ctx, stamp):
if ctx.configuration.is_tool_configuration():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ load(":common/java/proguard_validation.bzl", "validate_proguard_specs")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_common.bzl", "java_common", "target_kind")
load(":common/java/java_common.bzl", "java_common")
load(":common/java/java_common_internal_for_builtins.bzl", "target_kind")

coverage_common = _builtins.toplevel.coverage_common

Expand Down
8 changes: 4 additions & 4 deletions src/main/starlark/builtins_bzl/common/java/compile_action.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Java compile action
"""

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

def _filter_strict_deps(mode):
return "error" if mode in ["strict", "default"] else mode
Expand Down Expand Up @@ -130,8 +130,10 @@ def compile_action(
or resources present, whereas runfiles in this case are empty.
"""

java_info = java_common.compile(
java_info = _compile_private_for_builtins(
ctx,
output = output_class_jar,
java_toolchain = semantics.find_java_toolchain(ctx),
source_files = source_files,
source_jars = source_jars,
resources = resources,
Expand All @@ -145,8 +147,6 @@ def compile_action(
exported_plugins = exported_plugins,
javac_opts = [ctx.expand_location(opt) for opt in javacopts],
neverlink = neverlink,
java_toolchain = semantics.find_java_toolchain(ctx),
output = output_class_jar,
output_source_jar = output_source_jar,
strict_deps = _filter_strict_deps(strict_deps),
enable_compile_jar_action = enable_compile_jar_action,
Expand Down
4 changes: 2 additions & 2 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/paths.bzl", "paths")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_common.bzl", "java_common")
load(
":common/java/java_common.bzl",
":common/java/java_common_internal_for_builtins.bzl",
"collect_native_deps_dirs",
"get_runtime_classpath_for_archive",
"java_common",
"to_java_binary_info",
)

Expand Down
239 changes: 203 additions & 36 deletions src/main/starlark/builtins_bzl/common/java/java_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,238 @@

""" Utilities for Java compilation support in Starlark. """

load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_common_internal_for_builtins.bzl", "compile", "merge", "run_ijar")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_semantics.bzl", "semantics")

_java_common_internal = _builtins.internal.java_common_internal_do_not_use

def target_kind(target, dereference_aliases = False):
""" Get the rule class string for a target
def _compile(
ctx,
output,
java_toolchain,
source_jars = [],
source_files = [],
output_source_jar = None,
javac_opts = [],
deps = [],
runtime_deps = [],
exports = [],
plugins = [],
exported_plugins = [],
native_libraries = [],
annotation_processor_additional_inputs = [],
annotation_processor_additional_outputs = [],
strict_deps = "ERROR",
bootclasspath = None,
sourcepath = [],
resources = [],
neverlink = False,
enable_annotation_processing = True,
add_exports = [],
add_opens = []):
return compile(
ctx,
output,
java_toolchain,
source_jars = source_jars,
source_files = source_files,
output_source_jar = output_source_jar,
javac_opts = javac_opts,
deps = deps,
runtime_deps = runtime_deps,
exports = exports,
plugins = plugins,
exported_plugins = exported_plugins,
native_libraries = native_libraries,
annotation_processor_additional_inputs = annotation_processor_additional_inputs,
annotation_processor_additional_outputs = annotation_processor_additional_outputs,
strict_deps = strict_deps,
bootclasspath = bootclasspath,
sourcepath = sourcepath,
resources = resources,
neverlink = neverlink,
enable_annotation_processing = enable_annotation_processing,
add_exports = add_exports,
add_opens = add_opens,
)

def _run_ijar(actions, jar, java_toolchain, target_label = None):
return run_ijar(
actions = actions,
jar = jar,
java_toolchain = java_toolchain,
target_label = target_label,
)

def _stamp_jar(actions, jar, java_toolchain, target_label):
"""Stamps a jar with a target label for <code>add_dep</code> support.
The return value is typically passed to `JavaInfo.compile_jar`. Prefer to use `run_ijar` when
possible.
Args:
target: (Target)
dereference_aliases: (bool) resolve the actual target rule class if an
alias
actions: (actions) ctx.actions
jar: (File) The jar to run stamp_jar on.
java_toolchain: (JavaToolchainInfo) The toolchain to used to find the stamp_jar tool.
target_label: (Label) A target label to stamp the jar with. Used for `add_dep` support.
Typically, you would pass `ctx.label` to stamp the jar with the current rule's label.
Returns:
(str) The rule class string of the target
(File) The output artifact
"""
return _java_common_internal.target_kind(
target,
dereference_aliases = dereference_aliases,
return _java_common_internal.stamp_jar(
actions = actions,
jar = jar,
java_toolchain = java_toolchain,
target_label = target_label,
)

def to_java_binary_info(java_info):
""" Get a copy of the given JavaInfo with minimal info returned by a java_binary
def _pack_sources(
actions,
java_toolchain,
output_source_jar = None,
sources = [],
source_jars = []):
"""Packs sources and source jars into a single source jar file.
The return value is typically passed to `JavaInfo.source_jar`. At least one of parameters
output_jar or output_source_jar is required.
Args:
java_info: (JavaInfo) A JavaInfo provider instance
actions: (actions) ctx.actions
java_toolchain: (JavaToolchainInfo) The toolchain used to find the ijar tool.
output_source_jar: (File) The output source jar.
sources: ([File]) A list of Java source files to be packed into the source jar.
source_jars: ([File]) A list of source jars to be packed into the source jar.
Returns:
(JavaInfo) A JavaInfo instance representing a java_binary target
(File) The output artifact
"""
return _java_common_internal.to_java_binary_info(java_info)
return _java_common_internal.pack_sources(
actions = actions,
java_toolchain = java_toolchain,
sources = sources,
source_jars = source_jars,
output_source_jar = output_source_jar,
)

def get_build_info(ctx, is_stamping_enabled):
""" Get the artifacts representing the workspace status for this build
def _default_javac_opts(java_toolchain):
"""Experimental! Get default javacopts from a java toolchain
Args:
ctx: (RuleContext) The rule context
is_stamping_enabled: (bool) If stamping is enabled
java_toolchain: (JavaToolchainInfo) the toolchain from which to get the javac options.
Returns
([File]) The build info artifacts
Returns:
([str]) A list of javac options
"""
return _java_common_internal.get_build_info(ctx, is_stamping_enabled)
return _java_common_internal.default_javac_opts(java_toolchain = java_toolchain)

def collect_native_deps_dirs(deps):
""" Collect the set of root-relative paths containing native libraries
def _merge(providers):
return merge(providers)

def _make_non_strict(java_info):
"""Returns a new JavaInfo instance whose direct-jars part is the union of both the direct and indirect jars of the given Java provider.
Args:
deps: [Target] list of targets
java_info: (JavaInfo) The java info to make non-strict.
Returns:
([String]) A set of root-relative paths as a list
(JavaInfo)
"""
return _java_common_internal.collect_native_deps_dirs(deps)
return _java_common_internal.make_non_strict(java_info)

def get_runtime_classpath_for_archive(jars, excluded_jars):
""" Filters a classpath to remove certain entries
def _get_message_bundle_info():
return None if semantics.IS_BAZEL else _java_common_internal.MessageBundleInfo

Args
jars: (depset[File]) The classpath to filter
excluded_jars: (depset[File]) The files to remove
def _add_constraints(java_info, constraints = []):
"""Returns a copy of the given JavaInfo with the given constraints added.
Args:
java_info: (JavaInfo) The JavaInfo to enhance
constraints: ([str]) Constraints to add
Returns:
(depset[File]) The filtered classpath
(JavaInfo)
"""
return _java_common_internal.get_runtime_classpath_for_archive(
jars,
excluded_jars,
return _java_common_internal.add_constraints(java_info, constraints = constraints)

def _get_constraints(java_info):
"""Returns a set of constraints added.
Args:
java_info: (JavaInfo) The JavaInfo to get constraints from.
Returns:
([str]) The constraints set on the supplied JavaInfo
"""
return _java_common_internal.get_constraints(java_info)

def _set_annotation_processing(
java_info,
enabled = False,
processor_classnames = [],
processor_classpath = None,
class_jar = None,
source_jar = None):
"""Returns a copy of the given JavaInfo with the given annotation_processing info.
Args:
java_info: (JavaInfo) The JavaInfo to enhance.
enabled: (bool) Whether the rule uses annotation processing.
processor_classnames: ([str]) Class names of annotation processors applied.
processor_classpath: (depset[File]) Class names of annotation processors applied.
class_jar: (File) Optional. Jar that is the result of annotation processing.
source_jar: (File) Optional. Source archive resulting from annotation processing.
Returns:
(JavaInfo)
"""
return _java_common_internal.set_annotation_processing(
java_info,
enabled = enabled,
processor_classnames = processor_classnames,
processor_classpath = processor_classpath,
class_jar = class_jar,
source_jar = source_jar,
)

java_common = _java_common_internal
def _java_toolchain_label(java_toolchain):
"""Returns the toolchain's label.
Args:
java_toolchain: (JavaToolchainInfo) The toolchain.
Returns:
(Label)
"""
return _java_common_internal.java_toolchain_label(java_toolchain)

def _make_java_common():
methods = {
"provider": JavaInfo,
"compile": _compile,
"run_ijar": _run_ijar,
"stamp_jar": _stamp_jar,
"pack_sources": _pack_sources,
"default_javac_opts": _default_javac_opts,
"merge": _merge,
"make_non_strict": _make_non_strict,
"JavaPluginInfo": JavaPluginInfo,
"JavaToolchainInfo": _java_common_internal.JavaToolchainInfo,
"JavaRuntimeInfo": _java_common_internal.JavaRuntimeInfo,
"BootClassPathInfo": _java_common_internal.BootClassPathInfo,
"experimental_java_proto_library_default_has_services": _java_common_internal.experimental_java_proto_library_default_has_services,
}
if _java_common_internal._google_legacy_api_enabled():
methods.update(
MessageBundleInfo = _get_message_bundle_info(), # struct field that is None in bazel
add_constraints = _add_constraints,
get_constraints = _get_constraints,
set_annotation_processing = _set_annotation_processing,
java_toolchain_label = _java_toolchain_label,
)
return struct(**methods)

java_common = _make_java_common()
Loading

0 comments on commit f706e7d

Please sign in to comment.