Rename unzip tool.
[SquirrelJME.git] / emulators / standalone / build.gradle
blob6a327980d9efc30188544a2422af6069be8641e6
1 import cc.squirreljme.plugin.general.CollateResourceJarsTask
2 import cc.squirreljme.plugin.multivm.*
3 import cc.squirreljme.plugin.multivm.ident.SourceTargetClassifier
5 import java.util.regex.Pattern
7 plugins
9         id "application"
10         id "java"
13 apply plugin: "com.github.johnrengelman.shadow"
15 description = "Standalone SquirrelJME virtual machine on Java."
16 mainClassName = "cc.squirreljme.vm.standalone.main.Main"
18 dependencies {
19         implementation project(":emulators:springcoat-vm")
20         implementation project(":emulators:nanocoat-vm")
21         
22         // Debugger tool for quick usage
23         implementation project(":tools:squirreljme-debugger")
26 java {
27         // Use a fixed version of the JVM
28         compileJava
29         {
30                 sourceCompatibility = JavaVersion.VERSION_1_8
31                 targetCompatibility = JavaVersion.VERSION_1_8
32                 
33                 // Use the default bootstrap classpath
34                 options.bootstrapClasspath = null
35                 
36         }
37         
38         // Maximize debugging
39         compileJava.options.debug = true
40         compileJava.options.debugOptions.setDebugLevel("source,lines,vars")
41         
42         // Copy settings
43         compileTestJava.options.debug = compileJava.options.debug
44         compileTestJava.options.debugOptions = compileJava.options.debugOptions
47 // Mapper for Jar names on files to straight file names
48 static Iterable<java.nio.file.Path> mapBaseNameP(Iterable<Path> input) {
49         List<java.nio.file.Path> result = new ArrayList<>()
50         
51         input.forEach({path -> result.add(path.getFileName())})
52         
53         return result;
56 static String flatClasspath(Project project) {
57         return VMHelpers.classpathAsString(mapBaseNameP(VMHelpers
58                 .runClassPath(project, new SourceTargetClassifier(
59                         SourceSet.MAIN_SOURCE_SET_NAME,
60                         VMType.SPRINGCOAT, BangletVariant.NONE,
61                         ClutterLevel.RELEASE)) as List<Path>))
64 String MERGED_PREFIX = "SQUIRRELJME.SQC"
65 String MERGED_PREFIX_DEBUG = "SQUIRRELJME-DEBUG.SQC"
67 tasks.register("collateResourceJars",
68         CollateResourceJarsTask.class,
69         processResources, ClutterLevel.RELEASE, MERGED_PREFIX)
70 tasks.register("collateResourceJarsDebug",
71         CollateResourceJarsTask.class,
72         processResources, ClutterLevel.DEBUG, MERGED_PREFIX_DEBUG)
74 // Jar Configuration
75 jar {
76         dependsOn processResources, collateResourceJars, collateResourceJarsDebug
77         mustRunAfter collateResourceJars, collateResourceJarsDebug
78         
79         // We need to set specific manifest properties
80         manifest {
81                 attributes \
82                         "X-SquirrelJME-Standalone-Main-Class":
83                                 "javax.microedition.midlet.__MainHandler__",
84                         "X-SquirrelJME-Standalone-Parameter":
85                                 "cc.squirreljme.runtime.launcher.ui.MidletMain",
86                         "X-SquirrelJME-Standalone-Classpath": project.provider({ ->
87                                         return flatClasspath(project(":modules:launcher"))
88                                 }),
89                         "X-SquirrelJME-Standalone-Library": project.provider({ ->
90                                         return VMHelpers.classpathAsString(
91                                                 mapBaseNameP(VMHelpers.fullSuiteLibraries(
92                                                         rootProject.tasks.getByName("fullSpringCoatRelease"))
93                                                         as Iterable<Path>))
94                                 }),
95                         "X-SquirrelJME-Standalone-Internal-Jar-Root": project.provider({ ->
96                                 "/" + MERGED_PREFIX + "/"}),
97                         "X-SquirrelJME-Standalone-Internal-Debug-Jar-Root": project.provider({ ->
98                                 "/" + MERGED_PREFIX_DEBUG + "/"})
99         }
102 // Configuration for ShadowJar
103 shadowJar {
104         dependsOn collateResourceJars, collateResourceJarsDebug
105         mustRunAfter collateResourceJars, collateResourceJarsDebug
106         
107         // Always SquirrelJME
108         archiveBaseName.set("squirreljme-standalone")
109         
110         // Always merge service files, otherwise multiple VMs will just not work
111         mergeServiceFiles()
112         
113         // Set the suffix of the JAR to be the OS name and arch, since there is
114         // a dynamic library within for it
115         archiveClassifier.set(project.provider({ ->
116                         String osName = System.getProperty("os.name").toLowerCase()
117                         String osArch = System.getProperty("os.arch").toLowerCase()
118                         
119                         // Normalize OS names
120                         if (osName.contains("windows"))
121                                 osName = "windows"
122                         else if (osName.contains("mac os") || osName.contains("macos"))
123                                 osName = "macos"
124                         
125                         // Make sure there are no spaces or weird characters such as for
126                         // Windows
127                         return (osName + "-" + osArch).replaceAll(
128                                 Pattern.compile("[\\s<>:\"/\\\\|?*]"), "") 
129                 }))
130         
131         // Exclude IntelliJ Annotations, they are not needed
132         exclude "org/jetbrains/annotations/*.class"
133         exclude "org/intellij/lang/annotations/*.class"
134         
135         dependencies {
136                 // Exclude IntelliJ Annotations, they are not needed
137                 exclude(dependency("org.jetbrains:annotations-java5:.*"))
138         }
142  * Fills the execution specification.
143  * 
144  * @param __exec The specification to fill.
145  * @param __emulator The emulator to use.
146  * @param __isCluttered Is this cluttered?
147  * @param __isDebug Is this debugging?
148  * @since 2024/06/25
149  */
150 void __fillSpec(JavaExec __exec, VMType __emulator, boolean __isCluttered,
151         boolean __isDebug) {
152         // These are always the same
153         __exec.classpath(project.providers.provider {
154                 tasks.named("shadowJar").get().getOutputs().getFiles()})
155         __exec.mainClass = "cc.squirreljme.vm.standalone.main.Main"
156         
157         // Build arguments to use
158         List<String> args = new ArrayList<>()
159         
160         // Which emulator is used?
161         args += ("-Xemulator:" + __emulator.vmName(VMNameFormat.LOWERCASE))
162         
163         // Cluttered?
164         if (__isCluttered) {
165                 args += "-Xclutter:debug"
166         }
167         
168         // Debugging?
169         if (__isDebug) {
170                 args += "-Xdebug"
171         }
172         
173         // Use these args
174         __exec.args = args
177 // Shadow run tasks for the various emulator choices
178 [false, true].each { isDebug ->
179         [false, true].each { isCluttered ->
180                 VMType.values().each { vmType ->
181                         tasks.register("run" + vmType.vmName(VMNameFormat.PROPER_NOUN) +
182                                         (isDebug ? "Debug" : "") +
183                                         (isCluttered ? "Cluttered" : ""),
184                                 JavaExec) { sub ->
185                                 sub.dependsOn tasks.named("shadowJar")
186         
187                                 sub.description = "Runs the standalone VM via " +
188                                         "${vmType.properName}" +
189                                         (isDebug ? " with debugging" : "") +
190                                         (isCluttered ? " with cluttering" : "") + ".";
191                                 sub.group = "squirreljme"
192                                 
193                                 // Use simplified filler
194                                 __fillSpec(sub, vmType, isCluttered, isDebug)
195                         }
196                 }
197         }