1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
.multivm
;
12 import cc
.squirreljme
.plugin
.SquirrelJMEPluginConfiguration
;
13 import cc
.squirreljme
.plugin
.multivm
.ident
.SourceTargetClassifier
;
14 import cc
.squirreljme
.plugin
.util
.UnassistedLaunchEntry
;
16 import java
.io
.IOException
;
17 import java
.io
.OutputStream
;
18 import java
.nio
.file
.Files
;
19 import java
.nio
.file
.Path
;
20 import java
.nio
.file
.StandardCopyOption
;
21 import java
.nio
.file
.StandardOpenOption
;
22 import java
.util
.ArrayList
;
23 import java
.util
.LinkedHashSet
;
25 import org
.gradle
.api
.Action
;
26 import org
.gradle
.api
.Task
;
29 * This performs the actual work that is needed to build the ROM.
33 public class VMRomTaskAction
34 implements Action
<Task
>
36 /** The classifier used. */
37 protected final SourceTargetClassifier classifier
;
40 * Initializes the task.
42 * @param __classifier The classifier used.
43 * @throws NullPointerException On null arguments.
46 public VMRomTaskAction(SourceTargetClassifier __classifier
)
47 throws NullPointerException
49 if (__classifier
== null)
50 throw new NullPointerException("NARG");
52 this.classifier
= __classifier
;
60 public void execute(Task __task
)
61 throws NullPointerException
63 String sourceSet
= this.classifier
.getSourceSet();
64 VMSpecifier vmType
= this.classifier
.getVmType();
69 // We need somewhere safe to store the file
70 tempFile
= Files
.createTempFile(
71 vmType
.vmName(VMNameFormat
.LOWERCASE
), sourceSet
);
73 // ROM building parameters
74 RomBuildParameters build
= new RomBuildParameters();
76 // The boot libraries which will be placed first always
77 Set
<Path
> bootPaths
= new LinkedHashSet
<>();
79 // Get all of the libraries to translate
80 Set
<Path
> normalPaths
= new LinkedHashSet
<>();
81 for (VMLibraryTask task
: VMRomDependencies
.libraries(__task
,
84 // Determine the path set
85 Set
<Path
> pathSet
= new LinkedHashSet
<>();
86 for (File f
: task
.getOutputs().getFiles().getFiles())
87 pathSet
.add(f
.toPath());
89 // Is this the boot loader library?
90 SquirrelJMEPluginConfiguration config
=
91 SquirrelJMEPluginConfiguration
.configurationOrNull(
93 boolean isBootLoader
= (config
!= null && config
.isBootLoader
);
94 boolean isMainLauncher
= (config
!= null &&
95 config
.isMainLauncher
);
97 // If this is the boot loader, add our paths
100 build
.bootLoaderMainClass
= config
.bootLoaderMainClass
;
101 build
.bootLoaderClassPath
= pathSet
.toArray(
102 new Path
[pathSet
.size()]);
105 // If this is the launcher, set the information needed to
106 // make sure it can actually be launched properly
109 UnassistedLaunchEntry entry
= config
.primaryEntry();
111 build
.launcherMainClass
= entry
.mainClass
;
112 build
.launcherArgs
= entry
.args();
113 build
.launcherClassPath
= VMHelpers
.runClassPath(
114 task
, this.classifier
);
117 // Add to the correct set of paths
119 bootPaths
.addAll(pathSet
);
121 normalPaths
.addAll(pathSet
);
124 // Make sure the boot libraries are always first
125 Set
<Path
> libPaths
= new LinkedHashSet
<>();
126 libPaths
.addAll(bootPaths
);
127 libPaths
.addAll(normalPaths
);
129 // Setup output file for writing
130 try (OutputStream out
= Files
.newOutputStream(tempFile
,
131 StandardOpenOption
.WRITE
, StandardOpenOption
.TRUNCATE_EXISTING
,
132 StandardOpenOption
.CREATE
))
134 // Run the ROM processing
135 vmType
.processRom((VMBaseTask
)__task
,
136 this.classifier
.getBangletVariant(), out
, build
,
137 new ArrayList
<>(libPaths
));
140 // Move the file over
142 __task
.getOutputs().getFiles().getSingleFile().toPath(),
143 StandardCopyOption
.REPLACE_EXISTING
);
145 catch (IOException e
)
147 throw new RuntimeException("I/O Error linking ROM for " +
148 __task
.getProject().getName(), e
);
151 // Always try to cleanup the temporary file
154 if (tempFile
!= null)
157 Files
.deleteIfExists(tempFile
);
159 catch (IOException ignored
)