Skip to content

Commit

Permalink
Refactor buildjar BUILD files
Browse files Browse the repository at this point in the history
--
MOS_MIGRATED_REVID=114223734
  • Loading branch information
cushon authored and dslomov committed Feb 10, 2016
1 parent 3e34a11 commit 51d4443
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 221 deletions.
221 changes: 11 additions & 210 deletions src/java_tools/buildjar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,226 +2,27 @@ package(default_visibility = ["//src:__subpackages__"])

java_binary(
name = "JavaBuilder",
srcs = glob(
["java/com/google/devtools/build/buildjar/**/*.java"],
exclude = ["java/com/google/devtools/build/buildjar/javac/testing/**"],
),
main_class = "com.google.devtools.build.buildjar.BazelJavaBuilder",
deps = [
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper",
"//src/main/protobuf:deps_proto",
"//src/main/protobuf:java_compilation_proto",
"//src/main/protobuf:worker_protocol_proto",
"//third_party:error_prone",
"//third_party:guava",
"//third_party:jsr305",
"//third_party:protobuf",
"//tools/jdk:langtools-neverlink",
],
)

# Exports $(locations //tools/defaults:javac_bootclasspath) to the java world.
genrule(
name = "javac-bootclasspath-locations",
srcs = ["//tools/defaults:javac_bootclasspath"],
outs = ["java/com/google/devtools/build/java/bazel/JavacBootclasspathLocations.java"],
cmd = """
declare -a paths=($(SRCS)) && paths=($${paths[@]#$(GENDIR)/}) &&
IFS=: &&
cat > $@ <<EOF
package com.google.devtools.build.java.bazel;
public class JavacBootclasspathLocations {
public static final String BOOTCLASSPATH = "$${paths[*]}";
}
EOF
""",
)

# Provides java-level access to //tools/defaults:javac_bootclasspath
# as defined by blaze's --javac_bootclasspath flag
java_library(
name = "javac-bootclasspath-locations-lib",
srcs = [":javac-bootclasspath-locations"],
visibility = ["//visibility:private"],
)

java_library(
name = "JavacBootclasspath",
srcs = ["java/com/google/devtools/build/java/bazel/JavacBootclasspath.java"],
data = ["//tools/defaults:javac_bootclasspath"],
visibility = ["//visibility:public"],
deps = [":javac-bootclasspath-locations-lib"],
)

# Exports $(location //tools/defaults:java_langtools) to the java world.
genrule(
name = "java-langtools-location",
srcs = ["//tools/defaults:java_langtools"],
outs = ["java/com/google/devtools/build/java/bazel/JavaLangtoolsLocation.java"],
cmd = """
path=$(SRCS) && path=$${path#$(GENDIR)/} &&
cat > $@ <<EOF
package com.google.devtools.build.java.bazel;
public class JavaLangtoolsLocation {
public static final String FILE = "$${path}";
}
EOF
""",
)

# Provides java-level access to //tools/defaults:java_langtools
# as defined by bazel's --java_langtools flag
java_library(
name = "java-langtools-location-lib",
srcs = [":java-langtools-location"],
visibility = ["//visibility:private"],
runtime_deps = ["//src/java_tools/buildjar/java/com/google/devtools/build/buildjar"],
)

java_library(
name = "JavaLangtools",
srcs = ["java/com/google/devtools/build/java/bazel/JavaLangtools.java"],
data = ["//tools/defaults:java_langtools"],
filegroup(
name = "bootstrap_deploy.jar",
srcs = ["//src/java_tools/buildjar/java/com/google/devtools/build/buildjar:bootstrap_deploy.jar"],
visibility = ["//visibility:public"],
deps = [":java-langtools-location-lib"],
)

# Utility to export javacopts to the java world from the default javabuilder (a
# java_binary deploy jar).
java_binary(
name = "JavaBuilderConfigGenerator",
srcs = ["java/com/google/devtools/build/java/bazel/JavaBuilderConfigGenerator.java"],
main_class = "com.google.devtools.build.java.bazel.JavaBuilderConfigGenerator",
deps = [
"//src/main/java/com/google/devtools/build/lib:java-toolchain-parser",
"//third_party:guava",
],
)

# Exports the default javacopts to the java world from the default javabuilder
# (a java_binary deploy jar).
# We could put any java target as scope and in deps here.
genquery(
name = "java_toolchain_content",
expression = "kind(java_toolchain, deps(//tools/defaults:java_toolchain))",
opts = ["--output=proto"],
scope = ["//tools/defaults:java_toolchain"],
)

genrule(
name = "javabuilder-javacopts",
srcs = [":java_toolchain_content"],
outs = ["java/com/google/devtools/build/java/bazel/JavaBuilderJavacOpts.java"],
cmd = "$(location :JavaBuilderConfigGenerator) $< > $@",
tools = [":JavaBuilderConfigGenerator"],
)

# Provides java-level access to the default javacopts in the current
# JavaBuilder release as defined by bazel's --javabuilder_top flag
java_library(
name = "javabuilder-javacopts-lib",
srcs = [":javabuilder-javacopts"],
visibility = ["//visibility:private"],
)

java_library(
name = "JavaBuilderConfig",
srcs = ["java/com/google/devtools/build/java/bazel/JavaBuilderConfig.java"],
data = ["//tools/defaults:javabuilder"],
deps = [":javabuilder-javacopts-lib"],
filegroup(
name = "bootstrap_genclass_deploy.jar",
srcs = ["//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass:bootstrap_genclass_deploy.jar"],
visibility = ["//visibility:public"],
)

# Provides programmatic access to a bazel compatible javac.
# Use this instead of ToolProvider.getSystemJavaCompiler().
java_library(
name = "BazelJavaCompiler",
srcs = ["java/com/google/devtools/build/java/bazel/BazelJavaCompiler.java"],
data = ["//tools/defaults:java_langtools"],
visibility = [
"//src:__subpackages__",
"//third_party/ijar:__subpackages__",
],
deps = [
":JavaBuilderConfig",
":JavaLangtools",
":JavacBootclasspath",
],
)

# Command line version of BazelJavaCompiler, interface-compatible
# with the javac command, for use with ant, for example.
java_library(
name = "BazelJavac",
srcs = ["java/com/google/devtools/build/java/bazel/BazelJavac.java"],
deps = [":BazelJavaCompiler"],
)

# For the -Xbootclasspath option of the java_test of buildjar
genrule(
name = "langtools-test",
testonly = 1,
srcs = ["//tools/defaults:java_langtools"],
outs = ["langtools-test.jar"],
cmd = "cp $< $@",
)

java_test(
name = "BazelJavaCompilerTest",
size = "small",
srcs = ["javatests/com/google/devtools/build/java/bazel/BazelJavaCompilerTest.java"],
jvm_flags = [
# Simulates how Bazel invokes JavaBuilder
"-Xbootclasspath/p:$${TEST_SRCDIR}/src/java_tools/buildjar/langtools-test.jar",
],
tags = ["jdk8"],
test_class = "com.google.devtools.build.java.bazel.BazelJavaCompilerTest",
deps = [
":BazelJavaCompiler",
":langtools-test",
"//third_party:guava",
"//third_party:junit4",
"//third_party:truth",
],
)

#
# Bootstrapping using Skylark rules
#
load("//tools:build_rules/java_rules_skylark.bzl", "bootstrap_java_library", "bootstrap_java_binary")

bootstrap_java_library(
name = "skylark-deps",
jars = [
"//third_party:error_prone-jars",
"//third_party:guava-jars",
"//third_party:jsr305-jars",
"//third_party:protobuf-jars",
],
neverlink_jars = ["//tools/defaults:java_langtools"],
srcjars = [
"//src/main/protobuf:deps_proto_srcjar",
"//src/main/protobuf:worker_protocol_proto_srcjar",
"//src/main/protobuf:java_compilation_proto_srcjar",
],
)

bootstrap_java_binary(
name = "bootstrap",
srcs = glob(
["java/com/google/devtools/build/buildjar/**/*.java"],
exclude = ["java/com/google/devtools/build/buildjar/javac/testing/**"],
) + [
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper:JarCreator.java",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper:JarHelper.java",
],
main_class = "com.google.devtools.build.buildjar.BazelJavaBuilder",
visibility = ["//visibility:public"],
deps = [":skylark-deps"],
)

bootstrap_java_binary(
name = "bootstrap_genclass",
srcs = glob(["java/com/google/devtools/build/buildjar/genclass/*.java"]),
main_class = "com.google.devtools.build.buildjar.genclass.GenClass",
visibility = ["//visibility:public"],
deps = [":skylark-deps"],
exports = [
"//src/java_tools/buildjar/java/com/google/devtools/build/java/bazel:BazelJavaCompiler",
],
)
124 changes: 124 additions & 0 deletions src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Description:
# The Java library builders, which are used by Bazel to compile Java
# source code.

package_group(
name = "packages",
packages = [
"//src/java_tools/buildjar/...",
],
)

package(default_visibility = [":packages"])

java_library(
name = "optionsparser",
srcs = [
"InvalidCommandLineException.java",
"OptionsParser.java",
],
)

java_library(
name = "javac",
srcs = glob([
"javac/*.java",
]),
deps = [
":invalid_command_line_exception",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins",
"//third_party:guava",
"//third_party:jsr305",
"//third_party/java/jdk/langtools:javac",
],
)

# Bazel's BuildJar
java_library(
name = "buildjar",
srcs = glob(
["*.java"],
exclude = [
"InvalidCommandLineException.java",
],
),
deps = [
":invalid_command_line_exception",
":javac",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins:classloader",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins:dependency",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins:errorprone",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins:processing",
"//src/main/protobuf:worker_protocol_proto",
"//third_party:guava",
"//third_party/java/jdk/langtools:javac",
],
)

java_binary(
name = "BazelJavaBuilder",
main_class = "com.google.devtools.build.buildjar.BazelJavaBuilder",
visibility = ["//visibility:public"],
runtime_deps = [":buildjar"],
)

java_library(
name = "javac_options",
srcs = [
"javac/JavacOptions.java",
],
visibility = ["//java/com/google/devtools/build/buildjar:__pkg__"],
deps = [
"//third_party:guava",
],
)

java_library(
name = "invalid_command_line_exception",
srcs = ["InvalidCommandLineException.java"],
visibility = [
"//java/com/google/devtools/build/buildjar:__subpackages__",
"//javatests/com/google/devtools/build/buildjar:__subpackages__",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar:__subpackages__",
],
deps = [],
)

load("//tools:build_rules/java_rules_skylark.bzl", "bootstrap_java_library", "bootstrap_java_binary")

bootstrap_java_library(
name = "skylark-deps",
jars = [
"//third_party:error_prone-jars",
"//third_party:guava-jars",
"//third_party:jsr305-jars",
"//third_party:protobuf-jars",
],
neverlink_jars = ["//tools/defaults:java_langtools"],
srcjars = [
"//src/main/protobuf:deps_proto_srcjar",
"//src/main/protobuf:worker_protocol_proto_srcjar",
"//src/main/protobuf:java_compilation_proto_srcjar",
],
)

bootstrap_java_library(
name = "bootstrap_invalid_command_line_exception",
srcs = ["InvalidCommandLineException.java"],
)

bootstrap_java_binary(
name = "bootstrap",
srcs = glob(
["**/*.java"],
exclude = ["java/com/google/devtools/build/buildjar/javac/testing/**"],
),
main_class = "com.google.devtools.build.buildjar.BazelJavaBuilder",
deps = [
":skylark-deps",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper:bootstrap_jarhelper",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins:bootstrap_plugins",
],
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package_group(
name = "packages",
packages = [
"//src/java_tools/buildjar/...",
],
)

package(default_visibility = [":packages"])

java_binary(
name = "GenClass",
main_class = "com.google.devtools.build.buildjar.genclass.GenClass",
Expand All @@ -14,3 +23,19 @@ java_library(
"//third_party:guava",
],
)

#
## Bootstrapping using Skylark rules
#

load("//tools:build_rules/java_rules_skylark.bzl", "bootstrap_java_library", "bootstrap_java_binary")

bootstrap_java_binary(
name = "bootstrap_genclass",
srcs = glob(["*.java"]),
main_class = "com.google.devtools.build.buildjar.genclass.GenClass",
deps = [
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar:skylark-deps",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/jarhelper:bootstrap_jarhelper",
],
)
Loading

0 comments on commit 51d4443

Please sign in to comment.