Skip to content

Commit

Permalink
fix (build): Re-work Go & Java environment, again (fixes #546)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Mar 1, 2024
1 parent ed2b175 commit b539bf4
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 59 deletions.
1 change: 1 addition & 0 deletions .envrc
6 changes: 4 additions & 2 deletions .markdown-link-check.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{ "pattern": ".*dev/proto/core.*" },
{ "pattern": "script.svg" },
{ "pattern": "^https://vscode.dev" },
{ "pattern": "^https://graphdb.ontotext.com" }
{ "pattern": "^https://graphdb.ontotext.com" },
{ "pattern": "^https://thebrain.com" },
{ "pattern": "^https://codeberg.org" }
],
"aliveStatusCodes": [200, 0]
"aliveStatusCodes": [500, 200, 0]
}
11 changes: 3 additions & 8 deletions .tool-versions
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ protoc 25.3

# PS #1: Nota bene, we intentionally *do NOT* have e.g.
# "java temurin-21.0.2+13.0.LTS" (or "java openjdk-21") here,
# due to https://github.com/enola-dev/enola/issues/520!
# (And such a Java also wouldn't be used by Bazel,
# because of toolchains:remotejdk_21
# in tools/java_toolchain/BUILD.
# Only ./enola launcher script.)
# originally due to https://github.com/enola-dev/enola/issues/520,
# but nowadays because tools/environment/setup.bash uses Bazel's JDK.

# PS #2: We're similarly intentionally do NOT have e.g.
# "golang 1.21.7" here,
# due to https://github.com/enola-dev/enola/issues/532.
# FYI Go is required to install Bazelisk, if required
# (and perhaps for Go development later); and it's used
# by Bazel, due to go_sdk.host() in MODULE.bazel.
# tools/environment/setup.bash takes care of Go as well.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"site": false,
"tools": false,
"web": false,
"BUILT": false
"BUILT": false,
".envrc": false
}
}
33 changes: 16 additions & 17 deletions docs/dev/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,36 @@ _This may be out of date (please help to update it) - it's just so much easier t

If you do still want to try, here's how to manually install what the development environment container comes built-in with:

1. Install Java Development Kit (JDK), [same version as in `.bazelrc`](../../.bazelrc).
There are different Java (like Linux) "distributions" (all based on OpenJDK).
The easiest way to install one of them is typically to use your OS' package manager:

sudo apt-get install openjdk-21-jdk openjdk-21-doc openjdk-21-source

An alternative is to use e.g. [the SDKMAN!](https://sdkman.io)
If you work on several projects using different Java versions,
then we recommend using something like
[jEnv (with `.java-version`)](https://www.jenv.be), or
[asdf (with `.tool-versions`)](https://asdf-vm.com), or
[direnv (with `.envrc`)](https://direnv.net).
1. You do _NOT_ need to install a Java Development Kit (JDK), anymore.
The [version downloaded by Bazel via `.bazelrc`](../../.bazelrc) is now used by all scripts.

1. Install C/C++ etc. (it's required by the
[Proto rule for Bazel](https://github.com/bazelbuild/rules_proto)), e.g. do:

sudo apt-get install build-essential

1. Install [Python venv](https://docs.python.org/3/library/venv.html)
1. [Install Python venv](https://docs.python.org/3/library/venv.html)
(it's used by the presubmit and docs site generation), e.g. with:

sudo apt-get install python3-venv

1. Install [Bazelisk](https://github.com/bazelbuild/bazelisk) (NOT Bazel),
on a (recent enough...) Debian/Ubuntu [with Go](https://go.dev/doc/install)
e.g. like this (or some more manual equivalent):
1. You do _NOT_ need to install [Bazelisk](https://github.com/bazelbuild/bazelisk) (NOT Bazel),
that, and other tools, will be automagically installed by script. You do however Go for those tools,
and to build Enola's Go code.

1. [Install Go](https://go.dev/doc/install) e.g. like this on a (recent enough...) Debian/Ubuntu (or some more manual equivalent):

sudo apt update
sudo apt install golang-go

You should now be able to proceed as above (but without requiring _Docker)._
1. Now run `. .envrc` (or `source .envrc`, but NOT `./envrc`).
You could automated this with [direnv](https://direnv.net) (also [for Nix](https://github.com/direnv/direnv/wiki/Nix), and [other alternatives](https://direnv.net/#related-projects)).

You should now be able to proceed as above (but without requiring _Docker),_ try specifically:

* `./enola`

* `./test.bash`

## Further Reading

Expand Down
21 changes: 6 additions & 15 deletions enola
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,19 @@ set -euo pipefail
CWD=$(pwd)
ROOT="$(realpath "$(dirname "$0")")"

if [ -x "$(command -v gbazelisk)" ]; then
BZL=gbazelisk
elif [ -x "$(command -v bazelisk)" ]; then
BZL=bazelisk
else
# Also in test.bash
echo "bazelisk is not installed, please run e.g. 'go install github.com/bazelbuild/bazelisk@latest' "
echo "or an equivalent from https://github.com/bazelbuild/bazelisk#installation or see docs/dev/setup.md"
exit 255
fi

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

cd "$ROOT"
LOG=$(mktemp)
# NB: Similar build & launch also in tools/distro/build.sh & tools/distro/execjar-header.bash
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 "$@"
# TODO Integrate this with (use) tools/distro/build.sh instead of launching it directly like this? (But slower.)
cd "$CWD" && "$bazel_java_home"/bin/java --enable-preview -jar "$ROOT"/bazel-bin/cli/enola_deploy.jar "$@"
else
cat "$LOG" && echo >&2 && echo >&2
rm "$LOG"
cd "$CWD"
fi

# PS: Alternatively, you can also launch it simply via "bazelisk run //cli:enola -- --version" etc.
14 changes: 2 additions & 12 deletions test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,9 @@ set -euo pipefail
# This script builds the project *WITHOUT* requiring containers.
# It can be used *IN* a container though; and is so, by the ./build script.

# Similar also in the ./enola script:
GO_BIN_PATH=$(go env GOPATH)/bin
BZL=$GO_BIN_PATH/bazelisk
if ! [ -x "$(command -v "$BZL")" ]; then
if [ -x "$(command -v go)" ]; then
tools/go/install.bash
ROOT="$(realpath "$(dirname "$0")")"

else
echo "Please install Go from https://go.dev/doc/install and re-run this script!"
echo "See also https://docs.enola.dev/dev/setup/"
exit 255
fi
fi
source tools/environment/setup.bash

# https://github.com/bazelbuild/bazel/issues/4257
echo $ Bazel testing...
Expand Down
4 changes: 2 additions & 2 deletions tools/bazel-java-tool-chain/bazel-java-tool-chain.bash
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function cleanup {
# * 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 {
function bazel_java_home {
local current_java_runtime

ROOT="$(realpath "$(dirname "${BASH_SOURCE[0]}"/)")"
Expand All @@ -58,5 +58,5 @@ function java_binary {
rm "$LOG"
fi

echo "$output_base/$current_java_runtime/bin/java"
echo "$output_base/$current_java_runtime"
}
2 changes: 1 addition & 1 deletion tools/bazel-java-tool-chain/test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ cd /tmp
# shellcheck disable=SC1091
source "$ROOT"/bazel-java-tool-chain.bash

java_binary
bazel_java_home

cd "$CWD"
4 changes: 3 additions & 1 deletion tools/distro/build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ mkdir -p site/download/latest/
set -euox pipefail

# Build the end-user distributed executable fat über JAR
# NB: "bazelisk build //..." does *NOT* build "//cli:enola_deploy.jar", for some reason
# NB: "bazelisk build //..." does *NOT* build "//cli:enola_deploy.jar" (for some reason)
bazelisk build //cli:enola_deploy.jar
cp tools/distro/execjar-header.bash site/download/latest/enola
cat bazel-bin/cli/enola_deploy.jar >>site/download/latest/enola
# Let's briefly test it:
site/download/latest/enola --version

# Build the Container Image
# NB: This must work both on Docker (which turns it into docker buildx build) and Podman!
Expand Down
58 changes: 58 additions & 0 deletions tools/environment/setup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.

# This script sets environment variables which are useful for Enola developpers.
# Note that the Bazel build tool does not required these. The goal of this is
# however to give developpers the same environment in their respective shell.
# This also helps to launch IDEs, such as VSC, and make them use the same
# (versions of the) tools which run under Bazel for building & testing.

# As explained on https://docs.enola.dev/dev/setup/,
# Developers must run this script via ". .envrc" (NOT ./.envrc).
# Other scripts do so likewise (via source, same as ".").
# Ergo, no +x no this one.

# Set-Up Go
# Go is required to install Bazelisk (the way we install it), by Bazel,
# due to go_sdk.host() in MODULE.bazel, for Go development in Enola.
if ! [ -x "$(command -v go)" ]; then
echo "Please install Go from https://go.dev/doc/install and re-run this script!"
echo "See also https://docs.enola.dev/dev/setup/"
exit 255
fi

if [ -x "$(command -v gbazelisk)" ]; then
BZL=gbazelisk
elif [ -x "$(command -v bazelisk)" ]; then
BZL=bazelisk
else
source tools/go/install.bash
fi

# Set-Up Java
# This makes us use the exact same JDK as used by Bazel.
# (Which is the toolchains:remotejdk_21 in tools/java_toolchain/BUILD.)
source "$ROOT"/tools/bazel-java-tool-chain/bazel-java-tool-chain.bash
bazel_java_home=$(bazel_java_home)
export bazel_java_home

# This breaks ./enola, see https://github.com/enola-dev/enola/issues/574
#JAVA_HOME=$bazel_java_home
#export JAVA_HOME
#PATH=$bazel_java_home/bin:$PATH
#export PATH

# TODO Write a setup.fish script... can it invoke this one?

0 comments on commit b539bf4

Please sign in to comment.