Skip to content

Commit 6d70f2c

Browse files
author
Gary Frost
committedOct 23, 2024
Add env.bash error tracking and update build scripts and docs
1 parent 853cb64 commit 6d70f2c

10 files changed

+110
-411
lines changed
 

‎hat/README.md

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ The HAT project is in the 'hat' subdir of the babylon project.
1414

1515
## Building HAT
1616

17-
HAT uses both maven and cmake.
18-
19-
Maven controls the build but delegates to cmake for native artifacts (such as various backends).
20-
2117
[See](docs/hat-01-03-building-hat.md)
2218

2319

‎hat/bld

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env java --enable-preview --source 24 --class-path bldr/classes
1+
//usr/bin/env java --enable-preview --source 24 --class-path bldr/bldr.jar "$0" "$@"; exit $?
22
/*
33
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -29,6 +29,7 @@ import static java.nio.file.Files.*; // so we can use isDirectory(path);
2929

3030
void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
3131
var hatDir = new Root();
32+
List.of(args).forEach(arg->println(arg));
3233

3334
withExpectedDirectory(hatDir.path(), "hat", hatProjectDir -> {
3435
var hatJavacOpts = new JavacBuilder().opts(

‎hat/bldr/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
classes
2+
bldr.jar

‎hat/docs/hat-01-03-building-hat.md

+16-34
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ We might even have `Bldr` create the maven artifacts....
3030
To build HAT we need to ensure that `JAVA_HOME` is set
3131
to point to our babylon jdk (the one we built [here](hat-01-02-building-babylon.md))
3232

33-
It will simplify our tasks going forward if we
33+
It simplifes our tasks going forward if we
3434
add `${JAVA_HOME}/bin` to our PATH (before any other JAVA installs).
3535

36-
The top level `env.bash` shell script can be sourced (dot included)
37-
into your shell to both set `JAVA_HOME` and update your `PATH`
36+
We also need to prebuild the `bldr/bldr.jar`
37+
38+
Thankfully just sourcing the top level `env.bash` script will perform these tasks
3839

3940
It should detect the arch type (AARCH64 or X86_46) and
4041
select the correct relative parent dir and inject that dir in your PATH.
@@ -46,12 +47,13 @@ echo ${JAVA_HOME}
4647
/Users/ME/github/babylon/hat/../build/macosx-aarch64-server-release/jdk
4748
echo ${PATH}
4849
/Users/ME/github/babylon/hat/../build/macosx-aarch64-server-release/jdk/bin:/usr/local/bin:......
50+
ls bldr/bldr.jar
51+
bldr/bldr.jar
4952
```
5053

5154
# Introducing Bldr
52-
`Bldr` is a minimal java build system which has all the capabilities needed so far
53-
to build existing HAT as well as it's examples and backends. It is just a set of
54-
static methods and helper classes wrapping javac/jar tooling.
55+
`Bldr` is an evolving set of static methods and types needed (so far.. ;) )
56+
to build HAT as well as the HAT examples and backends.
5557

5658
`Bldr` itself is a java class in
5759
```
@@ -63,60 +65,39 @@ bldr
6365
└── Bldr.java
6466
```
6567

66-
We do need to compile this one class using javac one time, then we can use this in `bld` scripts to actually bld.
68+
The first run of `env.bash` will compile and create build `bldr/bldr.jar`
6769

6870
Assuming we have our babylon JDK build in our path (via `. env.bash`) we should do this every time we 'pull' HAT.
6971

7072
```shell
7173
mkdir bldr/classes
7274
javac --enable-preview -source 24 -d bldr/classes bldr/src/main/java/bldr/Bldr.java
75+
jar -cf bldr/bldr.jar -C bldr/classes bldr
7376
```
74-
75-
Now the `bldr/classes` dir contains all we need to create build scripts
76-
77-
In HAT's root dir is a `#!` (Hash Bang) java launcher style script called `bld`
77+
In HAT's root dir is a `#!` (Hash Bang) java launcher style script called `bld` (and one called `sanity`)
7878
which uses tools exposed by the precompiled `Bldr` to compile, create jars, run jextract, download dependencies, tar/untar etc.
7979

8080
As git does not allow us to check in scripts with execute permission, we need to `chmod +x` this `bld` file.
8181

8282
```bash
83-
chmod +x bld
83+
chmod +x bld sanity
8484
```
8585

86-
A simple example of `bld` script which just compiles core HAT source to `build/hat-1.0.jar` is shown.
87-
88-
```java
89-
#!/usr/bin/env java --enable-preview --source 24 --class-path bldr/classes
90-
import module java.compiler;
91-
import static bldr.Bldr.*;
92-
void main(String[] args) throws IOException, InterruptedException {
93-
var hatDir = Path.of(System.getProperty("user.dir"));
94-
var target = path(hatDir, "build");// mkdir(rmdir(path(hatDir, "build")));
95-
96-
var hatJarResult = javacjar($ -> $
97-
.opts( "--source", "24",
98-
"--enable-preview",
99-
"--add-exports=java.base/jdk.internal=ALL-UNNAMED",
100-
"--add-exports=java.base/jdk.internal.vm.annotation=ALL-UNNAMED")
101-
.jar(path(target, "hat-1.0.jar"))
102-
.source_path(path(hatDir, "hat/src/main/java"))
103-
);
104-
}
105-
```
10686
Note that the first line has the `#!` magic to allow this java code to be executed as if it
10787
were a script. Whilst `bld` is indeed real java code, we do not need to compile it. Instead we just execute using
10888

10989
```bash
11090
./bld
11191
```
11292

113-
The real `bld` is more complicated, but not much more. It will will build hat-1.0.jar, along with all the backend jars hat-backend-?-1.0.jar,
93+
`bld` will build hat-1.0.jar, along with all the backend jars hat-backend-?-1.0.jar,
11494
all the example jars hat-example-?-1.0.jar and will try to build all native artifacts (.so/.dylib) it can.
95+
11596
So if cmake finds OpenCL libs/headers, you will see libopencl_backend (.so or .dylib)
11697

11798
On a CUDA machine you will see libcuda_backend(.so or .dylib)
11899

119-
`bld` will also sanity check .java/.cpp/.h files to make sure we don't have any tabs, lines that with whitespace
100+
`sanity` will sanity check all .md/.java/.cpp/.h files to make sure we don't have any tabs, lines that with whitespace
120101
or files without appropriate licence headers
121102

122103
```bash
@@ -152,4 +133,5 @@ name `opencl` and the package name `mandel`
152133

153134
```bash
154135
bash hatrun.bash opencl mandel
136+
bash hatrun.bash opencl heal
155137
```

‎hat/env.bash

+89-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#!/bin.bash
2-
cat >/dev/null<<LICENSE
1+
cat >/dev/null<<END_OF_LICENSE
32
/*
43
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
54
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -24,34 +23,96 @@ cat >/dev/null<<LICENSE
2423
* or visit www.oracle.com if you need additional information or have any
2524
* questions.
2625
*/
27-
LICENSE
26+
END_OF_LICENSE
2827

29-
OS=$(uname -s )
30-
if [[ "$OS" == Linux ]]; then
31-
export ostype=linux
32-
elif [[ "$OS" == Darwin ]]; then
33-
export ostype=macosx
34-
else
35-
echo "could not determine ostype uname -s returned ${OS}"
36-
exit 1
37-
fi
28+
# First lets check if this script was sourced into a bash compatible shell
3829

39-
ARCH=$(uname -m)
40-
if [[ "$ARCH" == x86_64 ]]; then
41-
export archtype=${ARCH}
42-
elif [[ "$ARCH" == arm64 ]]; then
43-
export archtype=aarch64
30+
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
31+
# We just bail if it was not sourced.. We want to set PATH and JAVA_HOME...
32+
echo "You must source this file ..."
33+
echo "Using either "
34+
echo " . ${0}"
35+
echo "or"
36+
echo " source ${0}"
37+
exit 1; # this is ok because we were sourced
4438
else
45-
echo "could not determine aarchtype uname -m returned ${ARCH}"
46-
exit 1
47-
fi
39+
# Don't exit below here or you will trash the users shell ;)
4840

49-
export JAVA_HOME=${PWD}/../build/${ostype}-${archtype}-server-release/jdk
50-
echo "exporting JAVA_HOME=${JAVA_HOME}"
51-
if echo ${PATH} | grep ${JAVA_HOME} >/dev/null ;then
52-
echo 'path already contains ${JAVA_HOME}/bin'
53-
else
54-
export SAFE_PATH=${PATH}
55-
echo 'adding ${JAVA_HOME}/bin prefix to PATH, SAFE_PATH contains previous value'
56-
export PATH=${JAVA_HOME}/bin:${PATH}
41+
OS=$(uname -s )
42+
if [[ "$OS" == Linux ]]; then
43+
export ostype=linux
44+
elif [[ "$OS" == Darwin ]]; then
45+
export ostype=macosx
46+
else
47+
echo "Could not determine ostype uname -s returned ${OS}"
48+
fi
49+
50+
ARCH=$(uname -m)
51+
if [[ "$ARCH" == x86_64 ]]; then
52+
export archtype=${ARCH}
53+
elif [[ "$ARCH" == arm64 ]]; then
54+
export archtype=aarch64
55+
else
56+
echo "Could not determine aarchtype uname -m returned ${ARCH}"
57+
fi
58+
59+
if [[ -z "${archtype}" || -z "${ostype}" ]]; then
60+
echo "Can't determine archtype and/or ostype"
61+
else
62+
# We expect the user to provide a value for BABYLON_JDK_HOME or we can locate one using ${PWD}/..
63+
64+
# below is a verbose version of
65+
# export BABYLON_JDK_HOME=${BABYLON_JDK_HOME:-$(realpath ${PWD}/..)}
66+
67+
if [[ -z "${BABYLON_JDK_HOME}" ]]; then
68+
echo "No user supplied BABYLON_JDK_HOME var, we will try \${PWD}/.. = $(realpath ${PWD}/..)"
69+
export BABYLON_JDK_HOME=$(realpath ${PWD}/..)
70+
else
71+
echo "Using user supplied BABYLON_JDK_HOME ${BABYLON_JDK_HOME}"
72+
fi
73+
74+
75+
if [[ -d "${BABYLON_JDK_HOME}/build" ]]; then
76+
echo "\${BABYLON_JDK_HOME}/build seems ok!"
77+
export JAVA_HOME=${BABYLON_JDK_HOME}/build/${ostype}-${archtype}-server-release/jdk
78+
echo "exporting JAVA_HOME=${JAVA_HOME}"
79+
if echo ${PATH} | grep ${JAVA_HOME} >/dev/null ;then
80+
echo "PATH already contains \${JAVA_HOME}/bin"
81+
else
82+
export SAFE_PATH=${PATH}
83+
echo "Adding \${JAVA_HOME}/bin prefix to PATH, SAFE_PATH contains previous value"
84+
export PATH=${JAVA_HOME}/bin:${PATH}
85+
fi
86+
87+
# Our java source launcher based build system needs bldr.bldr.jar so we create it here if needed.
88+
if [[ -f bldr/bldr.jar ]]; then
89+
echo "Found prebuilt bldr.jar"
90+
else
91+
mkdir -p bldr/classes
92+
echo "Bootrapping bldr.jar"
93+
javac \
94+
--enable-preview \
95+
--source 24 \
96+
-d bldr/classes \
97+
--source-path bldr/src/main/java \
98+
bldr/src/main/java/bldr/Bldr.java
99+
100+
jar -cf bldr/bldr.jar -C bldr/classes bldr
101+
fi
102+
echo "SUCCESS!"
103+
else
104+
echo "We expected either:-"
105+
echo " \${PWD} to be in a hat subdir of a compiled babylon jdk build"
106+
echo "or"
107+
echo " BABYLON_JDK_HOME to be set, to a compiled babylon jdk build"
108+
echo ""
109+
echo "If you are in a hat subdir make sure babylon jdk is built ;)"
110+
echo ""
111+
echo "If you are in another dir try "
112+
echo " BABYLON_JDK_HOME=<<YOUR_PREBULT_BABYLON>> . ${0}"
113+
echo "or"
114+
echo " BABYLON_JDK_HOME=<<YOUR_PREBULT_BABYLON>> source ${0}"
115+
fi
116+
fi
57117
fi
118+

‎hat/build.bash ‎hat/maven-build.bash

-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ cat >/dev/null<<LICENSE
2525
*/
2626
LICENSE
2727

28-
2928
mvn clean compile jar:jar install

‎hat/mkbldr

-58
This file was deleted.

‎hat/sanity

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env java --enable-preview --source 24 --class-path bldr/classes
1+
//usr/bin/env java --enable-preview --source 24 --class-path bldr/bldr.jar "$0" "$@"; exit $?
2+
23
/*
34
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
45
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.

‎hat/sanity.java

-225
This file was deleted.

‎hat/whitespacecheck.bash

-59
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.