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[] argv) { 31 var usage =""" 32 usage: 33 java @bldr/args hatrun [headless] backend package args ... 34 [headless] : Optional passes -Dheadless=true to app 35 backend : opencl|cuda|spirv|ptx|mock 36 package : the examples package (and dirname under hat/examples) 37 38 class name is assumed to be package.Main (i.e. mandel.main) 39 40 examples: 41 java @bldr/args opencl mandel 42 java @bldr/args headless opencl mandel 43 """; 44 45 var hatDir = DirEntry.current(); 46 var backends = hatDir.existingDir("backends"); 47 var examples = hatDir.dir("examples"); 48 var buildDir = hatDir.existingBuildDir("build"); 49 var jextractedOpenCLJar = buildDir.jarFile("jextracted-opencl.jar"); 50 var jextractedOpenGLJar = buildDir.jarFile("jextracted-opengl.jar"); 51 var args = new ArrayList<>(List.of(argv)); 52 java(java -> java 53 .enable_preview() 54 .add_exports("java.base", "jdk.internal", "ALL-UNNAMED") 55 .enable_native_access("ALL-UNNAMED") 56 .library_path(buildDir) 57 .class_path(buildDir.jarFile("hat-1.0.jar")) 58 .when((!args.isEmpty() && args.getFirst().equals("headless")), ifHeadless -> { 59 ifHeadless.headless(); 60 args.removeFirst(); 61 }) 62 .either(!args.isEmpty(), haveBackend -> { 63 var backendName = args.removeFirst(); 64 if (backends.dir(backendName.replace('-','/')) instanceof DirEntry backend && backend.exists()) { 65 haveBackend.class_path(buildDir.jarFile("hat-backend-" + backendName + "-1.0.jar")); 66 } else { 67 throw new RuntimeException("No such backend " + backendName); 68 } 69 if (!args.isEmpty() && args.removeFirst() instanceof String exampleName) { 70 if (examples.dir(exampleName) instanceof DirEntry example && example.exists()) { haveBackend 71 .class_path(buildDir.jarFile("hat-example-" + exampleName + "-1.0.jar")) 72 .when(jextractedOpenCLJar.exists() && jextractedOpenGLJar.exists() && exampleName.equals("nbody"), _->{ haveBackend 73 .class_path(jextractedOpenCLJar,jextractedOpenGLJar) 74 .start_on_first_thread(); 75 }) 76 .main_class(exampleName + ".Main") 77 .args(args); 78 } else { 79 throw new RuntimeException("no such example " + exampleName); 80 } 81 } 82 }, _ -> { 83 throw new RuntimeException("No backend provided...\n" + usage); 84 }) 85 ); 86 }