Permalink
Browse files

Update J2CL javac compilation to set the bootclasspath.

CL also adds extra emul classes to be included in the classpath since javac expects them due to byte code references which were fulfilled by JDK earlier. However these dependencies have been split off and no longer transpiled to JavaScript.

See the related BUILD for the details of the new setup.

PiperOrigin-RevId: 232759511
  • Loading branch information...
gkdn authored and Copybara-Service committed Feb 6, 2019
1 parent df04e2c commit 09ed7baeb78a2ba895181ffa1df2c326f81b6cb5
@@ -1,6 +1,8 @@
# Description:
# Build rules used by J2CL and BUILD targets used by the rules.

load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain")

licenses(["notice"]) # Apache 2.0

##########################################################
@@ -48,6 +50,11 @@ alias(
actual = "//jre/java:jre",
)

default_java_toolchain(
name = "j2cl_java_toolchain",
bootclasspath = ["//jre/java:libjre_bootclasspath.jar"],
)

# ***************************************************
# JUnit library related
# ***************************************************
@@ -71,6 +71,10 @@ def _java_compile(ctx, java_srcs):
plugins = [p[JavaInfo] for p in ctx.attr.plugins]
exported_plugins = [p[JavaInfo] for p in ctx.attr.exported_plugins]

default_j2cl_javac_opts = [
# Avoid log site injection which introduces calls to unsupported APIs
"-XDinjectLogSites=false",
]
return java_common.compile(
ctx,
source_files = ctx.files._srcs_hack,
@@ -82,7 +86,8 @@ def _java_compile(ctx, java_srcs):
output = ctx.outputs.jar,
java_toolchain = ctx.attr._java_toolchain,
host_javabase = ctx.attr._host_javabase,
javac_opts = java_common.default_javac_opts(ctx, java_toolchain_attr = "_java_toolchain"),
javac_opts = java_common.default_javac_opts(ctx, java_toolchain_attr = "_java_toolchain") +
default_j2cl_javac_opts,
)

def _strip_gwt_incompatible(ctx, java_srcs):
@@ -117,7 +122,7 @@ _J2CL_LIB_ATTRS = {
"javacopts": attr.string_list(),
"licenses": attr.license(),
"_java_toolchain": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
default = Label("//build_defs/internal_do_not_use:j2cl_java_toolchain"),
),
"_host_javabase": attr.label(
default = Label("@bazel_tools//tools/jdk:current_host_java_runtime"),
@@ -5,7 +5,10 @@ def j2cl_transpile(ctx, java_provider, js_srcs, output, library_info_output):

# Using source_jars of the java_library since that includes APT generated src.
srcs = java_provider.source_jars + js_srcs
classpath = java_provider.compilation_info.compilation_classpath
classpath = depset(
java_provider.compilation_info.boot_classpath,
transitive = [java_provider.compilation_info.compilation_classpath],
)

args = ctx.actions.args()
args.use_param_file("@%s", use_always = True)
@@ -1,7 +1,6 @@
# Description:
# j2cl/jre: JRE subset for transpilation.
#
# Can depend on the native bootstrap JS but not on the VM opcode JS.

package(default_visibility = [
"//:__pkg__",
@@ -14,8 +13,30 @@ licenses(["notice"]) # Apache 2.0

load(":j2cl_mirror_from_gwt.bzl", "j2cl_mirror_from_gwt")

# This target includes part of the JRE that is emulated by J2CL. We don't use it
# as the 'bootclasspath' since doing so would have multiple implications:
#
# 1. Non-JRE deps would need to be included in the bootclasspath. As result javaemul.internal can
# no longer be visibility controlled and user code will compile# without depending
# jsinterop.annotations (which will be diverge from java_library).
#
# 2. JRE still needs to be included in JS deps and achieving that without including JRE
# twice in the classpath would introduce a lot of new complexity.
#
# So instead; we have chosen a bootclasspath that is not composed of emulated JRE classes. Instead
# it is composed of only classes that are not emulated but their byte-code is still needed to make
# javac happy. This setup provides following benefits:
#
# 1. Still takes real JDK out of bootclasspath and solves all the issues that comes with it.
# 2. We no longer transpile the classes that are only required for byte-code.
# 3. Since JRE is included as a regular dep we get folllowing for free:
# - Generated JS is included in the deps
# - JRE emulation bytecode is included only once
# - JRE emulation gets JavaBuilder i.jar optimizations.

j2cl_mirror_from_gwt(
name = "jre",
excluded_srcs = ["javasynth/**"],
extra_js_srcs = [
"//jre/java/javaemul/internal/vmbootstrap/primitives",
],
@@ -30,3 +51,11 @@ j2cl_mirror_from_gwt(
"//third_party:gwt-jsinterop-annotations-j2cl",
],
)

# Target includes classes that are referenced due to synthesized byte code and only included to
# satisfy the compiler checks. They are not available as part of the JRE emulation.
# See above for more details on this setup.
java_library(
name = "jre_bootclasspath",
srcs = glob(["javasynth/**"]),
)
@@ -39,9 +39,10 @@ def j2cl_mirror_from_gwt(
mirrored_files,
extra_srcs = [],
extra_js_srcs = [],
excluded_srcs = [],
deps = [],
**kwargs):
super_srcs = native.glob(["**/*.java"]) + extra_srcs
super_srcs = native.glob(["**/*.java"], exclude = excluded_srcs) + extra_srcs
native_srcs = native.glob(["**/*.native.js"])
js_srcs = native.glob(["**/*.js"], exclude = native_srcs) + extra_js_srcs

@@ -0,0 +1,21 @@
/*
* Copyright 2019 Google Inc.
*
* 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.
*/
package java.lang.invoke;

/** Exists solely to make javac happy. */
public class LambdaMetafactory {
public void metafactory(Object... a) {}

public void altMetafactory(Object... a) {}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2019 Google Inc.
*
* 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.
*/
package java.lang.invoke;

/** Exists solely to make javac happy. */
class MethodHandle {}
@@ -13,9 +13,7 @@
*/
package java.lang.invoke;

/**
* See <a
* href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/invoke/MethodHandles.html">the
* official Java API doc</a> for details.
*/
public class MethodHandles {}
/** Exists solely to make javac happy. */
class MethodHandles {
static class Lookup {}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2019 Google Inc.
*
* 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.
*/
package java.lang.invoke;

/** Exists solely to make javac happy. */
class MethodType {}
@@ -0,0 +1,17 @@
/*
* Copyright 2019 Google Inc.
*
* 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.
*/
package java.lang;

/** Exists solely to make javac happy. */
class NoSuchFieldError {}
@@ -15,11 +15,7 @@

import java.io.Serializable;

/**
* See
* <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/invoke/SerializedLambda.html">the
* official Java API doc</a> for details.
*/
/** Exists solely to make javac happy. */
public final class SerializedLambda implements Serializable {

public Object getCapturedArg(int i) {
@@ -12,6 +12,7 @@ java_library(
name = "jre",
srcs = ["//jre/java:jre_java_files"],
deps = [
"//jre/java:jre_bootclasspath",
"//third_party:gwt-javaemul-internal-annotations",
"//third_party:gwt-jsinterop-annotations",
],

0 comments on commit 09ed7ba

Please sign in to comment.