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 8a35c90 commit f4c04f5
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 50 deletions.
1 change: 1 addition & 0 deletions .envrc
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).

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

* `./enola`

* `./test.bash`

## Further Reading

Expand Down
14 changes: 4 additions & 10 deletions enola
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,14 @@ 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/environment/setup.bash

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

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
Expand All @@ -49,3 +41,5 @@ else
rm "$LOG"
cd "$CWD"
fi

# PS: Alternatively, you can also launch it simply via "bazelisk run //cli:enola -- --version" etc.
14 changes: 1 addition & 13 deletions test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@ 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

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: 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
61 changes: 61 additions & 0 deletions tools/environment/setup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/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.

# TODO set -euo pipefail

# 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 use the same JDK as used by Bazel.
# (Which is the toolchains:remotejdk_21 in tools/java_toolchain/BUILD.)
# Based on https://stackoverflow.com/q/78057833/421602:
# TODO Replace hard-coded <HASH> ...
JAVA_HOME=$HOME/.cache/bazel/_bazel_$USER/a3cf8df34f0c7a76e9926daaad9ffbf4/external/remotejdk21_linux
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH

# TODO Write a setup.fish script... can it invoke this one?
# Or recommend using https://direnv.net or https://direnv.net/#related-projects.

# TODO Consider https://github.com/direnv/direnv/wiki/Nix ?

0 comments on commit f4c04f5

Please sign in to comment.