Skip to content

Commit

Permalink
feat(build): Coax Java Toolchain out of Bazel (fixes #546)
Browse files Browse the repository at this point in the history
As-is, this makes ./enola noticeable slower; I'll optimize it in a follow-up.

There is also some more clean-up possible based on this; that will come in
#553 on another day.
  • Loading branch information
vorburger committed Mar 1, 2024
1 parent ad932f3 commit d38f40e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tab_width = 2
[*.bash]
indent_size = 2
tab_width = 2
max_line_length = unset

[*.css]
indent_size = 2
Expand Down
5 changes: 4 additions & 1 deletion enola
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ else
exit 255
fi

source tools/bazel-java-tool-chain/bazel-java-tool-chain.bash
JAVA=$(java_binary)

cd "$ROOT"
LOG=$(mktemp)
if "$BZL" build --color=yes //cli:enola_deploy.jar >"$LOG" 2>&1 ; then
rm "$LOG"
# TODO Integrate this with (use) tools/distro/build.sh instead of launching it like this
cd "$CWD" && java --enable-preview -jar "$ROOT"/bazel-bin/cli/enola_deploy.jar "$@"
cd "$CWD" && $JAVA --enable-preview -jar "$ROOT"/bazel-bin/cli/enola_deploy.jar "$@"
else
cat "$LOG" && echo >&2 && echo >&2
rm "$LOG"
Expand Down
57 changes: 57 additions & 0 deletions tools/bazel-java-tool-chain/bazel-java-tool-chain.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2023-2024 The Enola <https://enola.dev> 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
#
# https://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.

# https://unix.stackexchange.com/a/476710/103272
OLDOPTS="$(set +o); set -${-//c}"
trap cleanup EXIT
set +euo pipefail
function cleanup {
eval "${OLDOPTS}"
trap - EXIT
}

# This obtains the path to the JDK (its "Java toolchain") which Bazel downloaded.
#
# For background, see:
# * https://github.com/enola-dev/enola/issues/546
# * https://github.com/salesforce/bazel-eclipse/blob/888bcd333ac7bd4166fdb411562b74c2b54514d5/bundles/com.salesforce.bazel.eclipse.core/src/com/salesforce/bazel/eclipse/core/model/discovery/BaseProvisioningStrategy.java#L945-L960
# * https://stackoverflow.com/questions/78057833/how-to-query-bazel-for-the-absolute-jave-home-like-path-to-the-remote-jdk-of

function java_binary {
local current_java_runtime

# Hide Bazel output, unlike it failed (same also in //enola)
LOG=$(mktemp)
if ! current_java_runtime="$(bazelisk cquery @bazel_tools//tools/jdk:current_java_runtime \
--output starlark --starlark:file tools/bazel-java-tool-chain/bazel-java-tool-chain.bzl 2>"$LOG")"; then
cat "$LOG"
rm "$LOG"
exit 7
else
rm "$LOG"
fi

LOG=$(mktemp)
if ! output_base="$(bazel info output_base 2>"$LOG")"; then
cat "$LOG"
rm "$LOG"
exit 7
else
rm "$LOG"
fi

echo "$output_base/$current_java_runtime/bin/java"
}
11 changes: 11 additions & 0 deletions tools/bazel-java-tool-chain/bazel-java-tool-chain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# https://github.com/enola-dev/enola/issues/546
def format(target):
runtime_infos = {k: v for k, v in providers(target).items() if k.endswith("JavaRuntimeInfo")}

if len(runtime_infos) == 1:
java_runtime_info = runtime_infos.values()[0]

# https://bazel.build/rules/lib/providers/JavaRuntimeInfo
return java_runtime_info.java_home

fail("Unable to obtain JavaRuntimeInfo.")
22 changes: 22 additions & 0 deletions tools/bazel-java-tool-chain/test.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2023-2024 The Enola <https://enola.dev> 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
#
# https://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.

set -euo pipefail

source tools/bazel-java-tool-chain/bazel-java-tool-chain.bash

java_binary
3 changes: 3 additions & 0 deletions web/ui/src/main/java/dev/enola/web/ui/ThingUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

public class ThingUI {

// We intentionally don't use any template engine here; see e.g.
// https://blog.machinezoo.com/template-engines-broken for why.

// See https://github.com/google/google-java-format/issues/1033 re. STR formatting :()

// TODO Use Appendable-based approach, for better memory efficiency, and less String "trashing"
Expand Down

0 comments on commit d38f40e

Please sign in to comment.