1 # Building HAT
  2 
  3 ----
  4 
  5 * [Contents](hat-00.md)
  6 * House Keeping
  7     * [Project Layout](hat-01-01-project-layout.md)
  8     * [Building Babylon](hat-01-02-building-babylon.md)
  9     * [Building HAT](hat-01-03-building-hat.md)
 10 * Programming Model
 11     * [Programming Model](hat-03-programming-model.md)
 12 * Interface Mapping
 13     * [Interface Mapping Overview](hat-04-01-interface-mapping.md)
 14     * [Cascade Interface Mapping](hat-04-02-cascade-interface-mapping.md)
 15 * Implementation Detail
 16     * [Walkthrough Of Accelerator.compute()](hat-accelerator-compute.md)
 17 
 18 ---
 19 
 20 # Building HAT
 21 
 22 We can build HAT using maven and cmake.
 23 
 24 Maven controls the build but delegates to cmake for native artifacts (such as various backends).
 25 
 26 Note: You should not edit the pom.xml files, these are autogenerated (using mkpoms) and your changes will get overwritten.
 27 
 28 
 29 ## Setting environment variables JAVA_HOME and PATH
 30 
 31 To build HAT we need to ensure that `JAVA_HOME` is set
 32 to point to our babylon jdk (the one we built [here](hat-01-02-building-babylon.md))
 33 
 34 It will simplify our tasks going forward if we add `${JAVA_HOME}/bin` to our PATH (before any other JAVA installs).
 35 
 36 The `env.bash` shell script can be sourced (dot included) in your shell to set JAVA_HOME and PATH
 37 
 38 It should detect the arch type (AARCH64 or X86_46) and select the correct relative parent dir and inject that dir in your PATH.
 39 
 40 You will need jextract for your platform, and the jextract/bin dir should also be in your path.
 41 
 42 
 43 ```bash
 44 cd hat
 45 . ./env.bash
 46 export PATH=${PATH}:/path/to/my/jextract/bin
 47 echo ${JAVA_HOME}
 48 /Users/ME/github/babylon/hat/../build/macosx-aarch64-server-release/jdk
 49 echo ${PATH}
 50 /Users/ME/github/babylon/hat/../build/macosx-aarch64-server-release/jdk/bin:/usr/local/bin:......
 51 ```
 52 
 53 ## Root level maven pom.xml properties
 54 
 55 If you followed the instructions for building babylon your `pom.xml`
 56 properties should look like this, and should not need changing
 57 
 58 ```xml
 59 <project>
 60     <!-- yada -->
 61     <properties>
 62         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 63         <hat.build>${env.PWD}/build</hat.build>
 64     </properties>
 65     <!-- yada -->
 66 </project>
 67 ```
 68 ## Sanity checking your env and root pom.xml
 69 
 70 After sourcing `env.bash` or making changes to `pom.xml` we can
 71 sanity check our setup by running
 72 
 73 ```bash
 74 cd hat
 75 java @bldr/sanity
 76 ```
 77 
 78 ## Building with maven
 79 
 80 Now we should be able to use maven to build, if successful maven will place all jars and libs in a newly created `build` dir in your top level hat dir.
 81 
 82 ```bash
 83 cd hat
 84 . ./env.bash
 85 mvn clean  compile jar:jar install
 86 ls build
 87 hat-1.0.jar                         hat-example-heal-1.0.jar        libptx_backend.dylib
 88 hat-backend-ffi-cuda-1.0.jar        hat-example-mandel-1.0.jar      libspirv_backend.dylib
 89 hat-backend-ffi-mock-1.0.jar        hat-example-squares-1.0.jar     mock_info
 90 hat-backend-ffi-opencl-1.0.jar      hat-example-view-1.0.jar        opencl_info
 91 hat-backend-ffi-ptx-1.0.jar         hat-example-violajones-1.0.jar  ptx_info
 92 hat-backend-ffi-spirv-1.0.jar       libmock_backend.dylib           spirv_info
 93 hat-example-experiments-1.0.jar     libopencl_backend.dylib
 94 ```
 95 
 96 The provided `maven-build.bash` script contains the minimal maven commandline
 97 
 98 ```bash
 99 bash maven-build.bash
100 ```
101 
102 ## Running an example
103 
104 To run an example we use the maven artifacts in `build`
105 
106 ```bash
107 ${JAVA_HOME}/bin/java \
108    --enable-preview --enable-native-access=ALL-UNNAMED \
109    --class-path build/hat-1.0.jar:build/hat-example-mandel-1.0.jar:build/hat-backend-ffi-opencl-1.0.jar \
110    --add-exports=java.base/jdk.internal=ALL-UNNAMED \
111    -Djava.library.path=build\
112    mandel.Main
113 ```
114 
115 The provided `hatrun` java launch script simplifies this somewhat, we just need to pass the backend name `ffi-opencl` and the package name `mandel`
116 (all examples are assumed to be in `packagename/Main.java`
117 
118 ```bash
119 java @bldr/hatrun ffi-opencl mandel
120 ```