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.*;           // all the helpers are here 
27 import static java.nio.file.Files.*; // so we can use isDirectory(path);
28  
29 
30 void main(String[] args) {
31  var usage =
32     """
33     usage:
34       java @bldr/args hatrun [headless] backend package args ...
35          [headless] : Optional passes -Dheadless=true to app
36           backend   : opencl|cuda|spirv|ptx|mock
37           package   : the examples package (and dirname under hat/examples)
38 
39       class name is assumed to be package.Main  (i.e. mandel.main) 
40 
41       examples:
42          java @bldr/args opencl mandel
43          java @bldr/args headless opencl mandel
44     """;
45 
46   var hatDir =  Dir.current();
47   var backends = assertExists(hatDir.dir("backends"));
48   var examples = assertExists(hatDir.dir("examples"));
49   var buildDir = assertExists(BuildDir.of(hatDir.path("build")));
50 
51   var javaBuilder = javaBuilder().vmopts(
52                   "--enable-preview",
53                   "--enable-native-access=ALL-UNNAMED",
54                   "--add-exports=java.base/jdk.internal=ALL-UNNAMED"
55           )
56           .library_path(buildDir)
57           .class_path(buildDir.jarFile("hat-1.0.jar"));
58 
59   int argn = 0;
60   if (args.length > 0 && args[argn].equals("headless")) {
61       javaBuilder.vmopts("-Dheadless=true");
62       argn++;
63   }
64   if ((argn + 2) > args.length) {
65       print("args[" + args.length + "] = [ ");
66       List.of(args).forEach(a -> print(" " + a));
67       println(" ]");
68       println(usage);
69   } else {
70       var backendName = args[argn++];
71       var exampleName = args[argn++];
72 
73       if (backendName.equals("java")) {
74           javaBuilder.class_path(ClassDir.of(backends.path("shared/src/main/resources")));
75       } else if (backends.dir(backendName) instanceof Dir backend && backend.exists()) {
76           javaBuilder.class_path(buildDir.jarFile("hat-backend-" + backendName + "-1.0.jar"));
77       } else {
78           println("No backend " + backendName);
79       }
80 
81       if (examples.dir(exampleName) instanceof Dir example && example.exists()) {
82           java(javaBuilder
83                   .verbose()
84                   .class_path(buildDir.jarFile("hat-example-" + exampleName + "-1.0.jar"))
85                   .main_class(exampleName + ".Main")
86                   .args(Arrays.copyOfRange(args, argn, args.length))
87           );
88       } else {
89           println("no example " + exampleName);
90       }
91   }
92 }