Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ATs with JST and enable incremental compilation #147

Merged
merged 7 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_PARCHMENT_ARTIFACT_PREFIX;
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_PARCHMENT_GROUP;
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_PARCHMENT_MAVEN_URL;
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_PARCHMENT_TOOL_ARTIFACT;
import static net.neoforged.gradle.dsl.common.util.Constants.JST_TOOL_ARTIFACT;
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_RECOMPILER_MAX_MEMORY;
import static net.neoforged.gradle.dsl.common.util.Constants.SUBSYSTEM_PROPERTY_PREFIX;

Expand Down Expand Up @@ -82,7 +82,7 @@ private void configureParchmentDefaults() {
getStringProperty("parchment.mappingsVersion")
);
parchment.getToolArtifact().convention(
getStringProperty("parchment.toolArtifact").orElse(DEFAULT_PARCHMENT_TOOL_ARTIFACT)
getStringProperty("parchment.toolArtifact").orElse(JST_TOOL_ARTIFACT)
);
parchment.getAddRepository().convention(
getBooleanProperty("parchment.addRepository").orElse(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.jvm.toolchain.JavaLanguageVersion;

import java.io.File;
import java.util.Collections;
Expand All @@ -23,35 +25,43 @@ public AccessTransformer() {

setDescription("Runs the access transformer on the decompiled sources.");

getExecutingJar().set(ToolUtilities.resolveTool(getProject(), Constants.ACCESSTRANSFORMER));
getExecutingJar().set(ToolUtilities.resolveTool(getProject(), Constants.JST_TOOL_ARTIFACT));
getRuntimeProgramArguments().convention(
getInputFile().map(inputFile -> {
final List<String> args = Lists.newArrayList();
final File outputFile = ensureFileWorkspaceReady(getOutput());

args.add("--inJar");
args.add(inputFile.getAsFile().getAbsolutePath());
args.add("--outJar");
args.add(outputFile.getAbsolutePath());
args.add("--enable-accesstransformers");
getTransformers().forEach(f -> {
args.add("--atFile");
args.add("--access-transformer");
args.add(f.getAbsolutePath());
});

args.add("--libraries-list=" + getLibraries().get().getAsFile().getAbsolutePath());

args.add(inputFile.getAsFile().getAbsolutePath());
args.add(outputFile.getAbsolutePath());

return args;
}
)
);

getJavaVersion().set(JavaLanguageVersion.of(17));

getTransformers().finalizeValueOnRead();
}

@InputFile
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getInputFile();

@InputFile
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getLibraries();

@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.NONE)
public abstract ConfigurableFileCollection getTransformers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public void run() throws IOException {

// Write the list
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8));
for (File file : libraries) {
writer.println("-e=" + file.getAbsolutePath());
Iterator<File> itr = libraries.stream().sorted(Comparator.naturalOrder()).iterator();
while (itr.hasNext()) {
writer.println("-e=" + itr.next().getAbsolutePath());
}
writer.flush();
writer.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.neoforged.gradle.dsl.common.util.CommonRuntimeUtils;
import net.neoforged.gradle.dsl.common.util.GameArtifact;
import net.neoforged.gradle.util.StringCapitalizationUtils;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;

import java.io.File;
Expand All @@ -21,7 +23,7 @@ private CommonRuntimeTaskUtils() {
throw new IllegalStateException("Can not instantiate an instance of: CommonRuntimeTaskUtils. This is a utility class");
}

public static TaskProvider<? extends AccessTransformer> createAccessTransformer(Definition<?> definition, String namePreFix, File workspaceDirectory, Consumer<TaskProvider<? extends Runtime>> dependentTaskConfigurationHandler, Iterable<File> files, Collection<String> data) {
public static TaskProvider<? extends AccessTransformer> createAccessTransformer(Definition<?> definition, String namePreFix, File workspaceDirectory, Consumer<TaskProvider<? extends Runtime>> dependentTaskConfigurationHandler, Iterable<File> files, Collection<String> data, TaskProvider<? extends WithOutput> listLibs) {
final Collection<TaskProvider<? extends WithOutput>> fileProducingTasks = new ArrayList<>();
final Map<String, Integer> indexes = new HashMap<>();
for (File file : files) {
Expand Down Expand Up @@ -62,6 +64,8 @@ public static TaskProvider<? extends AccessTransformer> createAccessTransformer(
task.getTransformers().from(fileRemapTask.flatMap(WithOutput::getOutput));
task.dependsOn(fileRemapTask);
}
task.dependsOn(listLibs);
task.getLibraries().set(listLibs.flatMap(WithOutput::getOutput));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface ExecuteSpecification extends ProjectSpecification, OutputSpecification
*
* @return The path to the console log file.
*/
@OutputFile
@Internal
@DSLProperty
RegularFileProperty getConsoleLogFile();

Expand All @@ -60,7 +60,7 @@ interface ExecuteSpecification extends ProjectSpecification, OutputSpecification
*
* @return The path to the program log file.
*/
@OutputFile
@Internal
@DSLProperty
RegularFileProperty getLogFile();

Expand Down Expand Up @@ -113,4 +113,4 @@ interface ExecuteSpecification extends ProjectSpecification, OutputSpecification
*/
@Internal
MapProperty<String, Provider<List<String>>> getMultiRuntimeArguments();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ class Constants {
public static final String BINPATCHER_VERSION_INTERPOLATION = "net.minecraftforge:binarypatcher:%s:fatjar";
public static final String BINPATCHER = String.format(BINPATCHER_VERSION_INTERPOLATION, BINPATCHER_VERSION);
public static final String ACCESSTRANSFORMER_VERSION = "10.0.+";
public static final String ACCESSTRANSFORMER_VERSION_INTERPOLATION = "net.neoforged.accesstransformers:at-cli:%s:fatjar";
public static final String ACCESSTRANSFORMER = String.format(ACCESSTRANSFORMER_VERSION_INTERPOLATION, ACCESSTRANSFORMER_VERSION);
public static final String SPECIALSOURCE = "net.md-5:SpecialSource:1.11.0:shaded";
public static final String FART_VERSION = "1.0.13";
public static final String FART_VERSION = "2.0.3";
public static final String FART_ARTIFACT_INTERPOLATION = "net.neoforged:AutoRenamingTool:%s:all";
public static final String FART = String.format(FART_ARTIFACT_INTERPOLATION, FART_VERSION);
public static final String INSTALLERTOOLS_VERSION = '2.1.2'
Expand All @@ -27,7 +25,7 @@ class Constants {
public static final String DEFAULT_PARCHMENT_GROUP = "org.parchmentmc.data"
public static final String DEFAULT_PARCHMENT_ARTIFACT_PREFIX = "parchment-"
public static final String DEFAULT_PARCHMENT_MAVEN_URL = "https://maven.parchmentmc.org/"
public static final String DEFAULT_PARCHMENT_TOOL_ARTIFACT = "net.neoforged.jst:jst-cli-bundle:1.0.31"
public static final String JST_TOOL_ARTIFACT = "net.neoforged.jst:jst-cli-bundle:1.0.35"

public static final String DEFAULT_RECOMPILER_MAX_MEMORY = "1g"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
import net.neoforged.gradle.neoform.runtime.definition.NeoFormRuntimeDefinition;
import net.neoforged.gradle.neoform.runtime.specification.NeoFormRuntimeSpecification;
import net.neoforged.gradle.neoform.runtime.tasks.InjectZipContent;
import net.neoforged.gradle.neoform.runtime.tasks.PackZip;
import net.neoforged.gradle.neoform.runtime.tasks.Patch;
import net.neoforged.gradle.neoform.runtime.tasks.RecompileSourceJar;
import net.neoforged.gradle.neoform.runtime.tasks.StripJar;
import net.neoforged.gradle.neoform.runtime.tasks.UnpackJavaSources;
import net.neoforged.gradle.neoform.runtime.tasks.UnpackZip;
import net.neoforged.gradle.neoform.util.NeoFormRuntimeConstants;
import net.neoforged.gradle.neoform.util.NeoFormRuntimeUtils;
import net.neoforged.gradle.util.CopyingFileTreeVisitor;
Expand All @@ -47,7 +50,10 @@
import org.gradle.api.file.FileTree;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Copy;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.Zip;
import org.gradle.api.tasks.compile.AbstractCompile;
import org.gradle.api.tasks.compile.ForkOptions;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -405,8 +411,9 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) {
}
});

final String taskName = neoFormRuntimeTaskProvider.getName();
if (!spec.getPostTypeAdapters().containsKey(step.getName())) {
taskOutputs.put(neoFormRuntimeTaskProvider.getName(), neoFormRuntimeTaskProvider);
taskOutputs.put(taskName, neoFormRuntimeTaskProvider);
} else {
for (TaskTreeAdapter taskTreeAdapter : spec.getPostTypeAdapters().get(step.getName())) {
final TaskProvider<? extends Runtime> taskProvider = taskTreeAdapter.adapt(definition, neoFormRuntimeTaskProvider, neoFormDirectory, definition.getGameArtifactProvidingTasks(), definition.getMappingVersionData(), dependentTaskProvider -> dependentTaskProvider.configure(task -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, task)));
Expand All @@ -416,6 +423,8 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) {
}
}

// We consider the output of the final post adapter the output of step
taskOutputs.put(taskName, neoFormRuntimeTaskProvider);
taskOutputs.put(neoFormRuntimeTaskProvider.getName(), neoFormRuntimeTaskProvider);
}
}
Expand All @@ -439,9 +448,14 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) {
);

final FileCollection recompileDependencies = spec.getAdditionalRecompileDependencies().plus(spec.getProject().files(definition.getMinecraftDependenciesConfiguration()));
final TaskProvider<? extends Runtime> recompileTask = spec.getProject()
final TaskProvider<UnpackJavaSources> unpackSources = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "unzipSources"), UnpackJavaSources.class, task -> {
task.getInputZip().set(recompileInput);
});
unpackSources.configure(neoFormRuntimeTask -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, neoFormRuntimeTask));

final TaskProvider<RecompileSourceJar> recompileTask = spec.getProject()
.getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "recompile"), RecompileSourceJar.class, task -> {
task.getInputJar().set(recompileInput);
task.setSource(unpackSources.flatMap(UnpackJavaSources::getUnpackingTarget));
task.getCompileClasspath().setFrom(recompileDependencies);
task.getStepName().set("recompile");

Expand All @@ -453,17 +467,27 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) {
forkOptions.setJvmArgs(settings.getJvmArgs().get());
task.getOptions().getCompilerArgumentProviders().add(settings.getArgs()::get);
});

recompileTask.configure(neoFormRuntimeTask -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, neoFormRuntimeTask));

taskOutputs.put(recompileTask.getName(), recompileTask);
final TaskProvider<PackZip> packTask = spec.getProject()
.getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "packRecomp"), PackZip.class, task -> {
task.getInputFiles().from(recompileTask.flatMap(AbstractCompile::getDestinationDirectory));
task.getInputFiles().from(getProject().fileTree(recompileInput)
.matching(sp -> sp.exclude("**/*.java")));
});

packTask.configure(neoFormRuntimeTask -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, neoFormRuntimeTask));

taskOutputs.put(recompileTask.getName(), packTask);

definition.getSourceJarTask().configure(task -> {
task.getInput().set(recompileInput);
task.dependsOn(remapTask);
});
definition.getRawJarTask().configure(task -> {
task.getInput().set(recompileTask.flatMap(WithOutput::getOutput));
task.dependsOn(recompileTask);
task.getInput().set(packTask.flatMap(WithOutput::getOutput));
task.dependsOn(packTask);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.neoforged.gradle.platform.runtime.runtime.tasks;
package net.neoforged.gradle.neoform.runtime.tasks;

import net.neoforged.gradle.common.runtime.tasks.DefaultRuntime;
import net.neoforged.gradle.util.ZipBuildingFileTreeVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,31 @@

import net.neoforged.gradle.common.runtime.tasks.RuntimeArgumentsImpl;
import net.neoforged.gradle.common.runtime.tasks.RuntimeMultiArgumentsImpl;
import net.neoforged.gradle.dsl.common.runtime.tasks.Runtime;
import net.neoforged.gradle.dsl.common.runtime.tasks.RuntimeArguments;
import net.neoforged.gradle.dsl.common.runtime.tasks.RuntimeMultiArguments;
import net.neoforged.gradle.util.ZipBuildingFileTreeVisitor;
import net.neoforged.gradle.dsl.common.runtime.tasks.Runtime;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.jvm.toolchain.internal.CurrentJvmToolchainSpec;
import org.gradle.work.InputChanges;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipOutputStream;
import javax.inject.Inject;

@CacheableTask
public abstract class RecompileSourceJar extends JavaCompile implements Runtime {

private Property<JavaLanguageVersion> javaVersion;
private Provider<JavaToolchainService> javaToolchainService;
private final Property<JavaLanguageVersion> javaVersion;
private final Provider<JavaToolchainService> javaToolchainService;
private final RuntimeArguments arguments;
private final RuntimeMultiArguments multiArguments;

Expand All @@ -62,7 +49,6 @@ public RecompileSourceJar() {
//And configure output default locations.
getOutputDirectory().convention(getStepsDirectory().flatMap(d -> getStepName().map(d::dir)));
getOutputFileName().convention(getArguments().getOrDefault("outputExtension", getProviderFactory().provider(() -> "jar")).map(extension -> String.format("output.%s", extension)));
getOutput().convention(getOutputDirectory().flatMap(d -> getOutputFileName().map(d::file)));

getJavaVersion().convention(getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain().getLanguageVersion());
getJavaLauncher().convention(getJavaToolChain().flatMap(toolChain -> {
Expand All @@ -74,7 +60,6 @@ public RecompileSourceJar() {
}));

setDescription("Recompiles an already existing decompiled java jar.");
setSource(getProject().zipTree(getInputJar()).matching(filter -> filter.include("**/*.java")));

setClasspath(getCompileClasspath());
getOptions().setAnnotationProcessorPath(getAnnotationProcessorPath());
Expand All @@ -95,30 +80,8 @@ public RecompileSourceJar() {
getOptions().setFork(true);
getOptions().setIncremental(true);
getOptions().getIncrementalAfterFailure().set(true);
getInputJar().finalizeValueOnRead();
}

@Override
@TaskAction
protected void compile(InputChanges inputs) {
super.compile(inputs);

if (getState().getDidWork()) {
final File outputJar = ensureFileWorkspaceReady(getOutput());
try (FileOutputStream fileOutputStream = new FileOutputStream(outputJar);
ZipOutputStream outputZipStream = new ZipOutputStream(fileOutputStream)) {
ZipBuildingFileTreeVisitor zipBuildingFileTreeVisitor = new ZipBuildingFileTreeVisitor(outputZipStream);
//Add the compiled output.
getDestinationDirectory().getAsFileTree().visit(zipBuildingFileTreeVisitor);
//Add the original resources.
getArchiveOperations().zipTree(getInputJar()).matching(filter -> filter.exclude("**/*.java")).visit(zipBuildingFileTreeVisitor);
} catch (IOException e) {
throw new RuntimeException("Failed to create recompiled output jar", e);
}
}
}


@Override
public RuntimeArguments getArguments() {
return arguments;
Expand Down Expand Up @@ -147,21 +110,17 @@ public Property<JavaLanguageVersion> getJavaVersion() {
return this.javaVersion;
}

@InputFile
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getInputJar();

@InputFiles
@Classpath
@Internal
public abstract ConfigurableFileCollection getAnnotationProcessorPath();

@InputFiles
@Classpath
@Internal
public abstract ConfigurableFileCollection getCompileClasspath();


@Inject
@Override
public abstract ObjectFactory getObjectFactory();


@Inject
@Override
public abstract ProviderFactory getProviderFactory();
}
Loading
Loading