Skip to content

Commit

Permalink
Configure compile task mixin options lazily (#1256)
Browse files Browse the repository at this point in the history
* Try to configure compile task mixin options lazily

* Fix CompileJava being realised too soon, and other misc lazy task fixes.
  • Loading branch information
modmuss50 authored Jan 26, 2025
1 parent cd6e7a3 commit fc26023
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2020-2022 FabricMC
* Copyright (c) 2020-2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -40,6 +40,7 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;

import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.ide.idea.IdeaUtils;
Expand All @@ -64,13 +65,13 @@ public abstract class AnnotationProcessorInvoker<T extends Task> {
protected final Project project;
private final LoomGradleExtension loomExtension;
protected final MixinExtension mixinExtension;
protected final Map<SourceSet, T> invokerTasks;
protected final Map<SourceSet, TaskProvider<T>> invokerTasks;
private final String name;
private final Collection<Configuration> apConfigurations;

protected AnnotationProcessorInvoker(Project project,
Collection<Configuration> apConfigurations,
Map<SourceSet, T> invokerTasks, String name) {
Map<SourceSet, TaskProvider<T>> invokerTasks, String name) {
this.project = project;
this.loomExtension = LoomGradleExtension.get(project);
this.mixinExtension = loomExtension.getMixin();
Expand Down Expand Up @@ -146,8 +147,8 @@ public void configureMixin() {
}
}

for (Map.Entry<SourceSet, T> entry : invokerTasks.entrySet()) {
passMixinArguments(entry.getValue(), entry.getKey());
for (Map.Entry<SourceSet, TaskProvider<T>> entry : invokerTasks.entrySet()) {
entry.getValue().configure(t -> passMixinArguments(t, entry.getKey()));
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/net/fabricmc/loom/build/mixin/GroovyApInvoker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2022 FabricMC
* Copyright (c) 2022-2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,12 +26,12 @@

import java.io.File;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.GroovyCompile;

import net.fabricmc.loom.LoomGradleExtension;
Expand All @@ -46,11 +46,10 @@ public GroovyApInvoker(Project project) {
AnnotationProcessorInvoker.GROOVY);
}

private static Map<SourceSet, GroovyCompile> getInvokerTasks(Project project) {
private static Map<SourceSet, TaskProvider<GroovyCompile>> getInvokerTasks(Project project) {
MixinExtension mixin = LoomGradleExtension.get(project).getMixin();
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.GROOVY).collect(
Collectors.toMap(Map.Entry::getKey,
entry -> Objects.requireNonNull((GroovyCompile) entry.getValue())));
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.GROOVY, GroovyCompile.class).collect(
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2016-2022 FabricMC
* Copyright (c) 2016-2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,11 +26,11 @@

import java.io.File;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.JavaCompile;

import net.fabricmc.loom.LoomGradleExtension;
Expand All @@ -45,10 +45,10 @@ public JavaApInvoker(Project project) {
AnnotationProcessorInvoker.JAVA);
}

private static Map<SourceSet, JavaCompile> getInvokerTasks(Project project) {
private static Map<SourceSet, TaskProvider<JavaCompile>> getInvokerTasks(Project project) {
MixinExtension mixin = LoomGradleExtension.get(project).getMixin();
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.JAVA)
.collect(Collectors.toMap(Map.Entry::getKey, entry -> Objects.requireNonNull((JavaCompile) entry.getValue())));
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.JAVA, JavaCompile.class)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Override
Expand Down
42 changes: 22 additions & 20 deletions src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2020-2022 FabricMC
* Copyright (c) 2020-2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,6 +36,7 @@
import kotlin.Unit;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.JavaCompile;
import org.jetbrains.kotlin.gradle.plugin.KaptExtension;

Expand Down Expand Up @@ -66,35 +67,36 @@ public KaptApInvoker(Project project) {
kaptExtension.setIncludeCompileClasspath(false);
}

private static Map<SourceSet, JavaCompile> getInvokerTasks(Project project) {
private static Map<SourceSet, TaskProvider<JavaCompile>> getInvokerTasks(Project project) {
MixinExtension mixin = LoomGradleExtension.get(project).getMixin();
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.JAVA)
.collect(Collectors.toMap(Map.Entry::getKey, entry -> Objects.requireNonNull((JavaCompile) entry.getValue())));
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.JAVA, JavaCompile.class)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Override
public void configureMixin() {
super.configureMixin();

for (Map.Entry<SourceSet, JavaCompile> entry : invokerTasks.entrySet()) {
for (Map.Entry<SourceSet, TaskProvider<JavaCompile>> entry : invokerTasks.entrySet()) {
// Kapt only allows specifying javac args to all annotation processors at once. So we need to specify some dummy
// target location for the refmap and then move it to the correct place for each sourceset
JavaCompile task = entry.getValue();
SourceSet sourceSet = entry.getKey();
task.doLast(t -> {
try {
String refmapName = Objects.requireNonNull(MixinExtension.getMixinInformationContainer(sourceSet)).refmapNameProvider().get();
Path src = Paths.get(getRefmapDestination(task, refmapName));
Path dest = Paths.get(task.getDestinationDirectory().get().getAsFile().toString(), refmapName);

// Possible that no mixin annotations exist
if (Files.exists(src)) {
project.getLogger().info("Copying refmap from " + src + " to " + dest);
Files.move(src, dest);
entry.getValue().configure(task -> {
SourceSet sourceSet = entry.getKey();
task.doLast(t -> {
try {
String refmapName = Objects.requireNonNull(MixinExtension.getMixinInformationContainer(sourceSet)).refmapNameProvider().get();
Path src = Paths.get(getRefmapDestination(task, refmapName));
Path dest = Paths.get(task.getDestinationDirectory().get().getAsFile().toString(), refmapName);

// Possible that no mixin annotations exist
if (Files.exists(src)) {
project.getLogger().info("Copying refmap from " + src + " to " + dest);
Files.move(src, dest);
}
} catch (IOException e) {
project.getLogger().warn("Could not move refmap generated by kapt for task " + task, e);
}
} catch (IOException e) {
project.getLogger().warn("Could not move refmap generated by kapt for task " + task, e);
}
});
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2016-2020 FabricMC
* Copyright (c) 2016-2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,12 +26,12 @@

import java.io.File;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.scala.ScalaCompile;

import net.fabricmc.loom.LoomGradleExtension;
Expand All @@ -47,10 +47,10 @@ public ScalaApInvoker(Project project) {
AnnotationProcessorInvoker.SCALA);
}

private static Map<SourceSet, ScalaCompile> getInvokerTasks(Project project) {
private static Map<SourceSet, TaskProvider<ScalaCompile>> getInvokerTasks(Project project) {
MixinExtension mixin = LoomGradleExtension.get(project).getMixin();
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.SCALA)
.collect(Collectors.toMap(Map.Entry::getKey, entry -> Objects.requireNonNull((ScalaCompile) entry.getValue())));
return mixin.getInvokerTasksStream(AnnotationProcessorInvoker.SCALA, ScalaCompile.class)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/fabricmc/loom/extension/MixinExtension.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2016-2012 FabricMC
* Copyright (c) 2016-2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,6 +36,7 @@
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.util.PatternSet;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -81,7 +82,7 @@ static void setMixinInformationContainer(SourceSet sourceSet, MixinInformationCo
Stream<Configuration> getApConfigurationsStream(Function<SourceSet, String> getApConfigNameFunc);

@NotNull
Stream<Map.Entry<SourceSet, Task>> getInvokerTasksStream(String compileTaskLanguage);
<T extends Task> Stream<Map.Entry<SourceSet, TaskProvider<T>>> getInvokerTasksStream(String compileTaskLanguage, Class<T> taskType);

@NotNull
@Input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021-2022 FabricMC
* Copyright (c) 2021-2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -44,6 +44,7 @@
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.util.PatternSet;
import org.jetbrains.annotations.NotNull;

Expand All @@ -57,6 +58,7 @@ public MixinExtensionImpl(Project project) {
this.isDefault = true;
this.defaultRefmapName = project.getObjects().property(String.class)
.convention(project.provider(this::getDefaultMixinRefmapName));
this.defaultRefmapName.finalizeValueOnRead();
}

@Override
Expand Down Expand Up @@ -100,11 +102,11 @@ public Stream<Configuration> getApConfigurationsStream(Function<SourceSet, Strin

@Override
@NotNull
public Stream<Map.Entry<SourceSet, Task>> getInvokerTasksStream(String compileTaskLanguage) {
public <T extends Task> Stream<Map.Entry<SourceSet, TaskProvider<T>>> getInvokerTasksStream(String compileTaskLanguage, Class<T> taskType) {
return getMixinSourceSetsStream()
.flatMap(sourceSet -> {
try {
Task task = project.getTasks().getByName(sourceSet.getCompileTaskName(compileTaskLanguage));
TaskProvider<T> task = project.getTasks().named(sourceSet.getCompileTaskName(compileTaskLanguage), taskType);
return Stream.of(new AbstractMap.SimpleEntry<>(sourceSet, task));
} catch (UnknownTaskException ignored) {
return Stream.empty();
Expand Down Expand Up @@ -133,7 +135,7 @@ private void initDefault() {
if (sourceSet.getName().equals("main")) {
add(sourceSet);
} else {
add(sourceSet, sourceSet.getName() + "-" + getDefaultRefmapName().get());
add(sourceSet, getDefaultRefmapName().map(defaultRefmapName -> "%s-%s".formatted(sourceSet.getName(), defaultRefmapName)), x -> { });
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/fabricmc/loom/task/RemapJarTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public RemapJarTask() {
getOptimizeFabricModJson().convention(false).finalizeValueOnRead();

TaskProvider<NestableJarGenerationTask> processIncludeJars = getProject().getTasks().named(Constants.Task.PROCESS_INCLUDE_JARS, NestableJarGenerationTask.class);
getNestedJars().from(getProject().fileTree(processIncludeJars.get().getOutputDirectory()));
getNestedJars().from(processIncludeJars.map(task -> getProject().fileTree(task.getOutputDirectory())));
getNestedJars().builtBy(processIncludeJars);

getUseMixinAP().set(LoomGradleExtension.get(getProject()).getMixin().getUseLegacyMixinAp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void run() {
});

Action<RemapJarTask> remapJarTaskAction = task -> {
final AbstractArchiveTask jarTask = getTasks().named(JavaPlugin.JAR_TASK_NAME, AbstractArchiveTask.class).get();
final TaskProvider<AbstractArchiveTask> jarTask = getTasks().named(JavaPlugin.JAR_TASK_NAME, AbstractArchiveTask.class);

// Basic task setup
task.dependsOn(jarTask);
Expand All @@ -89,7 +89,7 @@ public void run() {
getArtifacts().add(JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME, task);

// Setup the input file and the nested deps
task.getInputFile().convention(jarTask.getArchiveFile());
task.getInputFile().convention(jarTask.flatMap(AbstractArchiveTask::getArchiveFile));
task.dependsOn(getTasks().named(JavaPlugin.JAR_TASK_NAME));
task.getIncludesClientOnlyClasses().set(getProject().provider(extension::areEnvironmentSourceSetsSplit));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicInteger;

import org.cadixdev.mercury.Mercury;
import org.cadixdev.mercury.remapper.MercuryRemapper;
import org.gradle.api.JavaVersion;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.compile.JavaCompile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -69,7 +73,7 @@ public static Provider<Options> createOptions(RemapSourcesJarTask task) {
task.getSourceNamespace(),
task.getTargetNamespace()
));
o.getJavaCompileRelease().set(SourceRemapper.getJavaCompileRelease(task.getProject()));
o.getJavaCompileRelease().set(getJavaCompileRelease(task.getProject()));
o.getClasspath().from(task.getClasspath());
});
}
Expand Down Expand Up @@ -135,4 +139,28 @@ private Mercury createMercury() throws IOException {

return mercury;
}

public static int getJavaCompileRelease(Project project) {
AtomicInteger release = new AtomicInteger(-1);

project.getTasks().withType(JavaCompile.class, javaCompile -> {
Property<Integer> releaseProperty = javaCompile.getOptions().getRelease();

if (!releaseProperty.isPresent()) {
return;
}

int compileRelease = releaseProperty.get();
release.set(Math.max(release.get(), compileRelease));
});

final int i = release.get();

if (i < 0) {
// Unable to find the release used to compile with, default to the current version
return Integer.parseInt(JavaVersion.current().getMajorVersion());
}

return i;
}
}
Loading

0 comments on commit fc26023

Please sign in to comment.