From fc26023047c78b02c29659f5d3650a2a288bd0d9 Mon Sep 17 00:00:00 2001 From: modmuss Date: Sun, 26 Jan 2025 21:06:30 +0000 Subject: [PATCH] Configure compile task mixin options lazily (#1256) * Try to configure compile task mixin options lazily * Fix CompileJava being realised too soon, and other misc lazy task fixes. --- .../mixin/AnnotationProcessorInvoker.java | 11 ++--- .../loom/build/mixin/GroovyApInvoker.java | 11 +++-- .../loom/build/mixin/JavaApInvoker.java | 10 ++--- .../loom/build/mixin/KaptApInvoker.java | 42 ++++++++++--------- .../loom/build/mixin/ScalaApInvoker.java | 10 ++--- .../loom/extension/MixinExtension.java | 5 ++- .../loom/extension/MixinExtensionImpl.java | 10 +++-- .../net/fabricmc/loom/task/RemapJarTask.java | 2 +- .../loom/task/RemapTaskConfiguration.java | 4 +- .../task/service/SourceRemapperService.java | 30 ++++++++++++- .../fabricmc/loom/util/SourceRemapper.java | 31 +------------- .../projects/mixinApSimple/build.gradle | 5 ++- 12 files changed, 90 insertions(+), 81 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java index c1f93855f..f8769b2a8 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/AnnotationProcessorInvoker.java @@ -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 @@ -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; @@ -64,13 +65,13 @@ public abstract class AnnotationProcessorInvoker { protected final Project project; private final LoomGradleExtension loomExtension; protected final MixinExtension mixinExtension; - protected final Map invokerTasks; + protected final Map> invokerTasks; private final String name; private final Collection apConfigurations; protected AnnotationProcessorInvoker(Project project, Collection apConfigurations, - Map invokerTasks, String name) { + Map> invokerTasks, String name) { this.project = project; this.loomExtension = LoomGradleExtension.get(project); this.mixinExtension = loomExtension.getMixin(); @@ -146,8 +147,8 @@ public void configureMixin() { } } - for (Map.Entry entry : invokerTasks.entrySet()) { - passMixinArguments(entry.getValue(), entry.getKey()); + for (Map.Entry> entry : invokerTasks.entrySet()) { + entry.getValue().configure(t -> passMixinArguments(t, entry.getKey())); } } diff --git a/src/main/java/net/fabricmc/loom/build/mixin/GroovyApInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/GroovyApInvoker.java index ab85b4656..742bab843 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/GroovyApInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/GroovyApInvoker.java @@ -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 @@ -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; @@ -46,11 +46,10 @@ public GroovyApInvoker(Project project) { AnnotationProcessorInvoker.GROOVY); } - private static Map getInvokerTasks(Project project) { + private static Map> 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 diff --git a/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java index 5450009d1..1327fcf2b 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/JavaApInvoker.java @@ -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 @@ -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; @@ -45,10 +45,10 @@ public JavaApInvoker(Project project) { AnnotationProcessorInvoker.JAVA); } - private static Map getInvokerTasks(Project project) { + private static Map> 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 diff --git a/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java index f5ae75544..e6ebd2599 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/KaptApInvoker.java @@ -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 @@ -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; @@ -66,35 +67,36 @@ public KaptApInvoker(Project project) { kaptExtension.setIncludeCompileClasspath(false); } - private static Map getInvokerTasks(Project project) { + private static Map> 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 entry : invokerTasks.entrySet()) { + for (Map.Entry> 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); - } + }); }); } } diff --git a/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java b/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java index 811332088..b6fa14655 100644 --- a/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java +++ b/src/main/java/net/fabricmc/loom/build/mixin/ScalaApInvoker.java @@ -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 @@ -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; @@ -47,10 +47,10 @@ public ScalaApInvoker(Project project) { AnnotationProcessorInvoker.SCALA); } - private static Map getInvokerTasks(Project project) { + private static Map> 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 diff --git a/src/main/java/net/fabricmc/loom/extension/MixinExtension.java b/src/main/java/net/fabricmc/loom/extension/MixinExtension.java index 40795d547..e7c428fef 100644 --- a/src/main/java/net/fabricmc/loom/extension/MixinExtension.java +++ b/src/main/java/net/fabricmc/loom/extension/MixinExtension.java @@ -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 @@ -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; @@ -81,7 +82,7 @@ static void setMixinInformationContainer(SourceSet sourceSet, MixinInformationCo Stream getApConfigurationsStream(Function getApConfigNameFunc); @NotNull - Stream> getInvokerTasksStream(String compileTaskLanguage); + Stream>> getInvokerTasksStream(String compileTaskLanguage, Class taskType); @NotNull @Input diff --git a/src/main/java/net/fabricmc/loom/extension/MixinExtensionImpl.java b/src/main/java/net/fabricmc/loom/extension/MixinExtensionImpl.java index 5c31fce70..06281c670 100644 --- a/src/main/java/net/fabricmc/loom/extension/MixinExtensionImpl.java +++ b/src/main/java/net/fabricmc/loom/extension/MixinExtensionImpl.java @@ -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 @@ -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; @@ -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 @@ -100,11 +102,11 @@ public Stream getApConfigurationsStream(Function> getInvokerTasksStream(String compileTaskLanguage) { + public Stream>> getInvokerTasksStream(String compileTaskLanguage, Class taskType) { return getMixinSourceSetsStream() .flatMap(sourceSet -> { try { - Task task = project.getTasks().getByName(sourceSet.getCompileTaskName(compileTaskLanguage)); + TaskProvider task = project.getTasks().named(sourceSet.getCompileTaskName(compileTaskLanguage), taskType); return Stream.of(new AbstractMap.SimpleEntry<>(sourceSet, task)); } catch (UnknownTaskException ignored) { return Stream.empty(); @@ -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 -> { }); } }); } diff --git a/src/main/java/net/fabricmc/loom/task/RemapJarTask.java b/src/main/java/net/fabricmc/loom/task/RemapJarTask.java index a50d0a996..bf09aaed7 100644 --- a/src/main/java/net/fabricmc/loom/task/RemapJarTask.java +++ b/src/main/java/net/fabricmc/loom/task/RemapJarTask.java @@ -107,7 +107,7 @@ public RemapJarTask() { getOptimizeFabricModJson().convention(false).finalizeValueOnRead(); TaskProvider 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()); diff --git a/src/main/java/net/fabricmc/loom/task/RemapTaskConfiguration.java b/src/main/java/net/fabricmc/loom/task/RemapTaskConfiguration.java index a7539afb0..0da6a9edb 100644 --- a/src/main/java/net/fabricmc/loom/task/RemapTaskConfiguration.java +++ b/src/main/java/net/fabricmc/loom/task/RemapTaskConfiguration.java @@ -79,7 +79,7 @@ public void run() { }); Action remapJarTaskAction = task -> { - final AbstractArchiveTask jarTask = getTasks().named(JavaPlugin.JAR_TASK_NAME, AbstractArchiveTask.class).get(); + final TaskProvider jarTask = getTasks().named(JavaPlugin.JAR_TASK_NAME, AbstractArchiveTask.class); // Basic task setup task.dependsOn(jarTask); @@ -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)); }; diff --git a/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java b/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java index 13b197f7d..f860b267e 100644 --- a/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java +++ b/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java @@ -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; @@ -69,7 +73,7 @@ public static Provider 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()); }); } @@ -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 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; + } } diff --git a/src/main/java/net/fabricmc/loom/util/SourceRemapper.java b/src/main/java/net/fabricmc/loom/util/SourceRemapper.java index a574cf05c..77e854e86 100644 --- a/src/main/java/net/fabricmc/loom/util/SourceRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/SourceRemapper.java @@ -31,17 +31,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import org.cadixdev.lorenz.MappingSet; 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.internal.project.ProjectInternal; -import org.gradle.api.provider.Property; -import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.internal.logging.progress.ProgressLogger; import org.gradle.internal.logging.progress.ProgressLoggerFactory; import org.slf4j.Logger; @@ -174,7 +170,8 @@ private Mercury getMercuryInstance() { MappingSet mappings = lorenzMappingService.getMappings(); Mercury mercury = createMercuryWithClassPath(project, toNamed); - mercury.setSourceCompatibilityFromRelease(getJavaCompileRelease(project)); + // Always use the latest version + mercury.setSourceCompatibilityFromRelease(Integer.MAX_VALUE); for (File file : extension.getUnmappedModCollection()) { Path path = file.toPath(); @@ -206,30 +203,6 @@ private Mercury getMercuryInstance() { return this.mercury; } - public static int getJavaCompileRelease(Project project) { - AtomicInteger release = new AtomicInteger(-1); - - project.getTasks().withType(JavaCompile.class, javaCompile -> { - Property 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; - } - public static void copyNonJavaFiles(Path from, Path to, Logger logger, Path source) throws IOException { Files.walk(from).forEach(path -> { Path targetPath = to.resolve(from.relativize(path).toString()); diff --git a/src/test/resources/projects/mixinApSimple/build.gradle b/src/test/resources/projects/mixinApSimple/build.gradle index 2e034dc65..9f565cdf4 100644 --- a/src/test/resources/projects/mixinApSimple/build.gradle +++ b/src/test/resources/projects/mixinApSimple/build.gradle @@ -73,7 +73,10 @@ loom { mixin { useLegacyMixinAp = true - defaultRefmapName = "default-refmap0000.json" + // After evaluate block only to test for https://github.com/FabricMC/fabric-loom/issues/1249 + afterEvaluate { + defaultRefmapName = "default-refmap0000.json" + } add(sourceSets["main"], "main-refmap0000.json") add(sourceSets["mixin"])