1 /* vim: set ft=java:
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 import static bldr.Bldr.*;  
27  
28 
29 void main(String[] argv) {
30   var usage ="""
31     usage:
32       java @bldr/args hatrun [headless] backend package args ...
33          [headless] : Optional passes -Dheadless=true to app
34           backend   : opencl|cuda|spirv|ptx|mock
35           package   : the examples package (and dirname under hat/examples)
36 
37       class name is assumed to be package.Main  (i.e. mandel.main) 
38 
39       examples:
40          java @bldr/args ffi-opencl mandel
41          java @bldr/args java-opencl mandel
42          java @bldr/args headless ffi-opencl mandel
43          java @bldr/args ffi-opencl life
44          java @bldr/args java-opencl life
45   """;
46 
47   var hatDir =  DirEntry.current();
48   var backends = hatDir.existingDir("backends");
49   var examples = hatDir.dir("examples");
50   var buildDir = hatDir.existingBuildDir("build");
51   var jextractedOpenCLJar = buildDir.jarFile("hat-jextracted-opencl-1.0.jar");
52   var jextractedOpenGLJar = buildDir.jarFile("hat-jextracted-opengl-1.0.jar");
53   var wrapJar = buildDir.jarFile("hat-wrap-1.0.jar");
54   var clwrapJar = buildDir.jarFile("hat-clwrap-1.0.jar");
55   var glwrapJar = buildDir.jarFile("hat-glwrap-1.0.jar");
56 
57   var args = new ArrayList<>(List.of(argv));
58   java(java -> java
59      .enable_preview()
60      .add_exports("java.base", "jdk.internal", "ALL-UNNAMED")
61      .enable_native_access("ALL-UNNAMED")
62      .library_path(buildDir)
63      .class_path(buildDir.jarFile("hat-core-1.0.jar"))
64      .when((!args.isEmpty() && args.getFirst().equals("headless")), ifHeadless -> {
65         ifHeadless.headless();
66         args.removeFirst();
67      })
68      .either(!args.isEmpty(), haveBackend -> {
69         var backendName = args.removeFirst();
70         if (backends.dir(backendName.replace('-','/')) instanceof DirEntry backend && backend.exists()) {
71            haveBackend.class_path(buildDir.jarFile("hat-backend-" + backendName + "-1.0.jar"));
72            if (backendName.equals("ffi-opencl")){
73                haveBackend.class_path(wrapJar, clwrapJar, jextractedOpenCLJar);
74            }
75         } else {
76            throw new RuntimeException("No such backend " + backendName);
77         }
78         if (!args.isEmpty() && args.removeFirst() instanceof String exampleName) {
79            if (examples.dir(exampleName) instanceof DirEntry example && example.exists()) { haveBackend
80               .class_path(buildDir.jarFile("hat-example-" + exampleName + "-1.0.jar"))
81               .when(jextractedOpenCLJar.exists() && jextractedOpenGLJar.exists() && exampleName.equals("nbody"), _->{ haveBackend
82                   .class_path(jextractedOpenCLJar,jextractedOpenGLJar, wrapJar, clwrapJar, glwrapJar )
83                   .start_on_first_thread();
84               })
85               .main_class(exampleName + ".Main")
86               .args(args);
87            } else {
88               throw new RuntimeException("no such example " + exampleName);
89            }
90         }
91      }, _ -> {
92         throw new RuntimeException("No backend provided...\n" + usage);
93      })
94   );
95 }