mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-03-30 05:05:20 -05:00
Run the kotlin metadata remapper in its own classloader with the compiler kotlin version. (#626)
This commit is contained in:
@@ -49,11 +49,12 @@ import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
||||
import net.fabricmc.loom.configuration.RemappedConfigurationEntry;
|
||||
import net.fabricmc.loom.configuration.processors.dependency.ModDependencyInfo;
|
||||
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
||||
import net.fabricmc.loom.kotlin.remapping.KotlinMetadataTinyRemapperExtension;
|
||||
import net.fabricmc.loom.task.RemapJarTask;
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
import net.fabricmc.loom.util.TinyRemapperHelper;
|
||||
import net.fabricmc.loom.util.ZipUtils;
|
||||
import net.fabricmc.loom.util.kotlin.KotlinClasspathService;
|
||||
import net.fabricmc.loom.util.kotlin.KotlinRemapperClassloader;
|
||||
import net.fabricmc.tinyremapper.InputTag;
|
||||
import net.fabricmc.tinyremapper.NonClassCopyMode;
|
||||
import net.fabricmc.tinyremapper.OutputConsumerPath;
|
||||
@@ -139,8 +140,6 @@ public class ModProcessor {
|
||||
private void remapJars(List<ModDependencyInfo> remapList) throws IOException {
|
||||
final LoomGradleExtension extension = LoomGradleExtension.get(project);
|
||||
final MappingsProviderImpl mappingsProvider = extension.getMappingsProvider();
|
||||
final boolean useKotlinExtension = project.getPluginManager().hasPlugin("org.jetbrains.kotlin.jvm");
|
||||
|
||||
Path[] mcDeps = project.getConfigurations().getByName(Constants.Configurations.LOADER_DEPENDENCIES).getFiles()
|
||||
.stream().map(File::toPath).toArray(Path[]::new);
|
||||
|
||||
@@ -150,8 +149,12 @@ public class ModProcessor {
|
||||
.withMappings(TinyRemapperHelper.create(mappingsProvider.getMappings(), fromM, toM, false))
|
||||
.renameInvalidLocals(false);
|
||||
|
||||
if (useKotlinExtension) {
|
||||
builder.extension(KotlinMetadataTinyRemapperExtension.INSTANCE);
|
||||
final KotlinClasspathService kotlinClasspathService = KotlinClasspathService.getOrCreateIfRequired(project);
|
||||
KotlinRemapperClassloader kotlinRemapperClassloader = null;
|
||||
|
||||
if (kotlinClasspathService != null) {
|
||||
kotlinRemapperClassloader = KotlinRemapperClassloader.create(kotlinClasspathService);
|
||||
builder.extension(kotlinRemapperClassloader.getTinyRemapperExtension());
|
||||
}
|
||||
|
||||
final TinyRemapper remapper = builder.build();
|
||||
@@ -209,6 +212,10 @@ public class ModProcessor {
|
||||
}
|
||||
} finally {
|
||||
remapper.finish();
|
||||
|
||||
if (kotlinRemapperClassloader != null) {
|
||||
kotlinRemapperClassloader.close();
|
||||
}
|
||||
}
|
||||
|
||||
for (ModDependencyInfo info : remapList) {
|
||||
|
||||
@@ -36,10 +36,13 @@ import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.kotlin.remapping.KotlinMetadataTinyRemapperExtension;
|
||||
import net.fabricmc.loom.task.AbstractRemapJarTask;
|
||||
import net.fabricmc.loom.util.kotlin.KotlinClasspath;
|
||||
import net.fabricmc.loom.util.kotlin.KotlinClasspathService;
|
||||
import net.fabricmc.loom.util.kotlin.KotlinRemapperClassloader;
|
||||
import net.fabricmc.loom.util.service.SharedService;
|
||||
import net.fabricmc.loom.util.service.SharedServiceManager;
|
||||
import net.fabricmc.tinyremapper.IMappingProvider;
|
||||
@@ -54,15 +57,15 @@ public class TinyRemapperService implements SharedService {
|
||||
final LoomGradleExtension extension = LoomGradleExtension.get(project);
|
||||
final SharedServiceManager sharedServiceManager = SharedServiceManager.get(project);
|
||||
final boolean legacyMixin = extension.getMixin().getUseLegacyMixinAp().get();
|
||||
final boolean useKotlinExtension = project.getPluginManager().hasPlugin("org.jetbrains.kotlin.jvm");
|
||||
final @Nullable KotlinClasspathService kotlinClasspathService = KotlinClasspathService.getOrCreateIfRequired(project);
|
||||
|
||||
// Generates an id that is used to share the remapper across projects. This tasks in the remap jar task name to handle custom remap jar tasks separately.
|
||||
final var joiner = new StringJoiner(":");
|
||||
joiner.add(extension.getMappingsProvider().getBuildServiceName("remapJarService", from, to));
|
||||
joiner.add(remapJarTask.getName());
|
||||
|
||||
if (useKotlinExtension) {
|
||||
joiner.add("kotlin");
|
||||
if (kotlinClasspathService != null) {
|
||||
joiner.add("kotlin-" + kotlinClasspathService.version());
|
||||
}
|
||||
|
||||
if (remapJarTask.getRemapperIsolation().get()) {
|
||||
@@ -79,7 +82,7 @@ public class TinyRemapperService implements SharedService {
|
||||
mappings.add(MixinMappingsService.getService(SharedServiceManager.get(project)).getMappingProvider(from, to));
|
||||
}
|
||||
|
||||
return new TinyRemapperService(mappings, !legacyMixin, useKotlinExtension);
|
||||
return new TinyRemapperService(mappings, !legacyMixin, kotlinClasspathService);
|
||||
});
|
||||
|
||||
service.readClasspath(remapJarTask.getClasspath().getFiles().stream().map(File::toPath).toList());
|
||||
@@ -88,12 +91,14 @@ public class TinyRemapperService implements SharedService {
|
||||
}
|
||||
|
||||
private TinyRemapper tinyRemapper;
|
||||
@Nullable
|
||||
private KotlinRemapperClassloader kotlinRemapperClassloader;
|
||||
private final Map<String, InputTag> inputTagMap = new HashMap<>();
|
||||
private final HashSet<Path> classpath = new HashSet<>();
|
||||
// Set to true once remapping has started, once set no inputs can be read.
|
||||
private boolean isRemapping = false;
|
||||
|
||||
public TinyRemapperService(List<IMappingProvider> mappings, boolean useMixinExtension, boolean useKotlinExtension) {
|
||||
public TinyRemapperService(List<IMappingProvider> mappings, boolean useMixinExtension, @Nullable KotlinClasspath kotlinClasspath) {
|
||||
TinyRemapper.Builder builder = TinyRemapper.newRemapper();
|
||||
|
||||
for (IMappingProvider provider : mappings) {
|
||||
@@ -104,8 +109,9 @@ public class TinyRemapperService implements SharedService {
|
||||
builder.extension(new net.fabricmc.tinyremapper.extension.mixin.MixinExtension());
|
||||
}
|
||||
|
||||
if (useKotlinExtension) {
|
||||
builder.extension(KotlinMetadataTinyRemapperExtension.INSTANCE);
|
||||
if (kotlinClasspath != null) {
|
||||
kotlinRemapperClassloader = KotlinRemapperClassloader.create(kotlinClasspath);
|
||||
builder.extension(kotlinRemapperClassloader.getTinyRemapperExtension());
|
||||
}
|
||||
|
||||
tinyRemapper = builder.build();
|
||||
@@ -156,5 +162,9 @@ public class TinyRemapperService implements SharedService {
|
||||
tinyRemapper.finish();
|
||||
tinyRemapper = null;
|
||||
}
|
||||
|
||||
if (kotlinRemapperClassloader != null) {
|
||||
kotlinRemapperClassloader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2022 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.fabricmc.loom.util.kotlin;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.Set;
|
||||
|
||||
public interface KotlinClasspath {
|
||||
String version();
|
||||
|
||||
Set<URL> classpath();
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2022 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.fabricmc.loom.util.kotlin;
|
||||
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.fabricmc.loom.util.service.SharedService;
|
||||
import net.fabricmc.loom.util.service.SharedServiceManager;
|
||||
|
||||
public record KotlinClasspathService(Set<URL> classpath, String version) implements KotlinClasspath, SharedService {
|
||||
@Nullable
|
||||
public static KotlinClasspathService getOrCreateIfRequired(Project project) {
|
||||
if (!KotlinPluginUtils.hasKotlinPlugin(project)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getOrCreate(project, KotlinPluginUtils.getKotlinPluginVersion(project));
|
||||
}
|
||||
|
||||
public static synchronized KotlinClasspathService getOrCreate(Project project, String kotlinVersion) {
|
||||
final String id = "kotlinclasspath:" + kotlinVersion;
|
||||
final SharedServiceManager sharedServiceManager = SharedServiceManager.get(project);
|
||||
return sharedServiceManager.getOrCreateService(id, () -> create(project, kotlinVersion));
|
||||
}
|
||||
|
||||
private static KotlinClasspathService create(Project project, String kotlinVersion) {
|
||||
// Create a detached config to resolve the koltin std lib for the provided version.
|
||||
Configuration detachedConfiguration = project.getConfigurations().detachedConfiguration(
|
||||
project.getDependencies().create("org.jetbrains.kotlin:kotlin-stdlib:" + kotlinVersion)
|
||||
);
|
||||
|
||||
Set<URL> classpath = detachedConfiguration.getFiles().stream()
|
||||
.map(file -> {
|
||||
try {
|
||||
return file.toURI().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}).collect(Collectors.toSet());;
|
||||
|
||||
return new KotlinClasspathService(classpath, kotlinVersion);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2022 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.fabricmc.loom.util.kotlin;
|
||||
|
||||
import net.fabricmc.tinyremapper.TinyRemapper;
|
||||
|
||||
public interface KotlinMetadataTinyRemapperExtension extends TinyRemapper.ApplyVisitorProvider, TinyRemapper.Extension {
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2022 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.fabricmc.loom.util.kotlin;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.Dependency;
|
||||
import org.gradle.api.artifacts.DependencySet;
|
||||
|
||||
public class KotlinPluginUtils {
|
||||
private static final String KOTLIN_PLUGIN_ID = "org.jetbrains.kotlin.jvm";
|
||||
private static final String KOTLIN_PLUGIN_GROUP = "org.jetbrains.kotlin.jvm";
|
||||
private static final String KOTLIN_PLUGIN_NAME = "org.jetbrains.kotlin.jvm.gradle.plugin";
|
||||
|
||||
public static boolean hasKotlinPlugin(Project project) {
|
||||
return project.getPluginManager().hasPlugin(KOTLIN_PLUGIN_ID);
|
||||
}
|
||||
|
||||
public static String getKotlinPluginVersion(Project project) {
|
||||
DependencySet buildDependencies = project.getBuildscript().getConfigurations()
|
||||
.getByName("classpath").getDependencies();
|
||||
|
||||
for (Dependency dependency : buildDependencies) {
|
||||
if (KOTLIN_PLUGIN_GROUP.equals(dependency.getGroup()) && KOTLIN_PLUGIN_NAME.equals(dependency.getName())) {
|
||||
return dependency.getVersion();
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException("Unable to get the kotlin plugin version");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2022 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.fabricmc.loom.util.kotlin;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import kotlinx.metadata.jvm.KotlinClassHeader;
|
||||
|
||||
import net.fabricmc.loom.LoomGradlePlugin;
|
||||
import net.fabricmc.loom.kotlin.remapping.KotlinMetadataTinyRemapperExtensionImpl;
|
||||
|
||||
/**
|
||||
* Used to run the Kotlin remapper with a specific version of Koltin that may not match the kotlin version included with gradle.
|
||||
*/
|
||||
public class KotlinRemapperClassloader extends URLClassLoader {
|
||||
// Packages that should be loaded from the gradle plugin classloader.
|
||||
private static final List<String> PARENT_PACKAGES = List.of(
|
||||
"net.fabricmc.tinyremapper",
|
||||
"net.fabricmc.loom.util.kotlin",
|
||||
"org.objectweb.asm",
|
||||
"org.slf4j"
|
||||
);
|
||||
|
||||
private KotlinRemapperClassloader(URL[] urls) {
|
||||
super(urls, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
||||
if (PARENT_PACKAGES.stream().anyMatch(name::startsWith)) {
|
||||
return LoomGradlePlugin.class.getClassLoader().loadClass(name);
|
||||
}
|
||||
|
||||
return super.loadClass(name, resolve);
|
||||
}
|
||||
|
||||
public static KotlinRemapperClassloader create(KotlinClasspath classpathProvider) {
|
||||
// Include the libraries that are not on the kotlin classpath.
|
||||
final Stream<URL> loomUrls = getClassUrls(
|
||||
KotlinMetadataTinyRemapperExtensionImpl.class, // Loom
|
||||
KotlinClassHeader.class // Kotlin metadata api
|
||||
);
|
||||
|
||||
final URL[] urls = Stream.concat(
|
||||
loomUrls,
|
||||
classpathProvider.classpath().stream()
|
||||
).toArray(URL[]::new);
|
||||
|
||||
return new KotlinRemapperClassloader(urls);
|
||||
}
|
||||
|
||||
private static Stream<URL> getClassUrls(Class<?>... classes) {
|
||||
return Arrays.stream(classes).map(klass -> klass.getProtectionDomain().getCodeSource().getLocation());
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the {@link KotlinMetadataTinyRemapperExtensionImpl} class on the new classloader.
|
||||
*/
|
||||
public KotlinMetadataTinyRemapperExtension getTinyRemapperExtension() {
|
||||
try {
|
||||
Class<?> klass = this.loadClass(KotlinMetadataTinyRemapperExtensionImpl.class.getCanonicalName());
|
||||
return (KotlinMetadataTinyRemapperExtension) klass.getField("INSTANCE").get(null);
|
||||
} catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
|
||||
throw new RuntimeException("Failed to create instance", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user