Skip to content

Commit

Permalink
Extract a small macro for the two jars created by genrule.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 699165681
  • Loading branch information
netdpb authored and Flogger Team committed Nov 22, 2024
1 parent 9911839 commit 43858aa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
51 changes: 13 additions & 38 deletions api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load("@google_bazel_common//tools/jarjar:jarjar.bzl", "jarjar_library")
load("@google_bazel_common//tools/javadoc:javadoc.bzl", "javadoc_library")
load("@rules_java//java:defs.bzl", "java_binary", "java_import", "java_library")
load("//tools:maven.bzl", "pom_file")
load(":build_defs.bzl", "genjar")

SYSTEM_BACKEND_SRCS = glob(["src/main/java/com/google/common/flogger/backend/system/**/*.java"])

Expand Down Expand Up @@ -62,51 +63,25 @@ java_import(
deps = [":stack_getter_common"],
)

genrule(
name = "gen_stack_getter_java_lang_access_impl",
srcs = STACK_GETTER_JAVA_LANG_ACCESS_IMPL_SRCS + [
":stack_getter_common",
genjar(
name = "stack_getter_java_lang_access_impl",
srcs = STACK_GETTER_JAVA_LANG_ACCESS_IMPL_SRCS,
toolchains = ["@rules_java//toolchains:jdk_8"],
deps = [
":checks",
":stack_getter_common",
],
outs = ["stack_getter_java_lang_access_impl.jar"],
cmd = """
set -eu
TMPDIR=$$(mktemp -d)
OUTPUT=$$PWD/$@
STACK_GETTER_COMMON=$(location :stack_getter_common)
CHECKS=$(location :checks)
JAVABASE=$$PWD/$(JAVABASE)
JAR=$$PWD/$(JAVABASE)/bin/jar
$$JAVABASE/bin/javac -d $$TMPDIR -source 8 -target 8 -cp $${STACK_GETTER_COMMON}:$${CHECKS} -implicit:none \
$(location src/main/java/com/google/common/flogger/util/JavaLangAccessStackGetter.java)
cd $$TMPDIR && $$JAR cf $$OUTPUT com/google/common/flogger/util/*StackGetter*.class
""",
toolchains = ["@rules_java//toolchains:jdk_8"],
)

genrule(
name = "gen_stack_getter_stack_walker_impl",
srcs = STACK_GETTER_STACK_WALKER_IMPL_SRCS + [
":stack_getter_common",
genjar(
name = "stack_getter_stack_walker_impl",
srcs = STACK_GETTER_STACK_WALKER_IMPL_SRCS,
toolchains = ["@rules_java//toolchains:remote_jdk11"],
deps = [
":checks",
":stack_getter_common",
"@google_bazel_common//third_party/java/jspecify_annotations",
],
outs = ["stack_getter_stack_walker_impl.jar"],
cmd = """
set -eu
TMPDIR=$$(mktemp -d)
OUTPUT=$$PWD/$@
STACK_GETTER_COMMON=$(location :stack_getter_common)
CHECKS=$(location :checks)
JSPECIFY_ANNOTATIONS=$(location @google_bazel_common//third_party/java/jspecify_annotations)
JAVABASE=$$PWD/$(JAVABASE)
JAR=$$PWD/$(JAVABASE)/bin/jar
$$JAVABASE/bin/javac -d $$TMPDIR -source 8 -target 8 \
-cp $${STACK_GETTER_COMMON}:$${CHECKS}:$${JSPECIFY_ANNOTATIONS} -implicit:none \
$(location src/main/java/com/google/common/flogger/util/StackWalkerStackGetter.java)
cd $$TMPDIR && $$JAR cf $$OUTPUT com/google/common/flogger/util/*StackGetter*.class
""",
toolchains = ["@rules_java//toolchains:remote_jdk11"],
)

java_library(
Expand Down
43 changes: 43 additions & 0 deletions api/build_defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (C) 2024 The Flogger Authors.
#
# 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.

"""Utilities for building Flogger."""

def genjar(name, srcs = [], deps = [], **kwargs):
"""Compiles Java source files to a jar file using a genrule.
Args:
name: The name of the target. The output jar file will be named <name>.jar.
srcs: The Java source files to compile.
deps: The Java libraries to depend on.
**kwargs: Additional arguments to pass to genrule.
"""
native.genrule(
name = name,
srcs = srcs + deps,
outs = [name + ".jar"],
cmd =
"""
set -eu
TMPDIR="$$(mktemp -d)"
"$(JAVABASE)/bin/javac" -d "$$TMPDIR" \
-source 8 -target 8 -implicit:none \
-cp "{classpath}" {srcs}
"$(JAVABASE)/bin/jar" cf "$@" -C "$$TMPDIR" .
""".format(
srcs = " ".join(["$(location %s)" % src for src in srcs]),
classpath = ":".join(["$(location %s)" % dep for dep in deps]),
),
**kwargs
)

0 comments on commit 43858aa

Please sign in to comment.