From 9e6953a596f1614b2c5d1986ff7e9954401f4dd6 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Wed, 29 Jan 2025 23:59:23 +0500 Subject: [PATCH] Fix relocation breaking runClient & runServer --- build.gradle | 7 +++++-- buildSrc/src/main/java/NativeRelocator.java | 23 ++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 0bc03316c..293994fd2 100644 --- a/build.gradle +++ b/build.gradle @@ -111,10 +111,11 @@ class NativeTransformer implements Transformer { private final HashMap rewrittenFiles = new HashMap() private var nativeRelocator + public File rootDir NativeTransformer() { try { - int exitCode = Runtime.getRuntime().exec(new String[]{"python", "--version"}).waitFor(); + int exitCode = Runtime.getRuntime().exec(new String[]{"python", "--version"}).waitFor() if (exitCode == 0) { enabled = true } @@ -150,7 +151,7 @@ class NativeTransformer implements Transformer { byte[] content = context.is.readAllBytes() if (nativeRelocator == null) { - nativeRelocator = new NativeRelocator() + nativeRelocator = new NativeRelocator(rootDir.toPath().resolve("relocate_natives")) } try { @@ -395,6 +396,8 @@ subprojects { p -> // librariesLocation isn't used because it's too long for replacing paths in native libraries // Allowing strings larger than the original string would require shifting the entire binary's contents transform(NativeTransformer) { + rootDir = project.rootDir + before { relocate "org.sqlite", "dh_sqlite", { exclude "org/sqlite/native/**" diff --git a/buildSrc/src/main/java/NativeRelocator.java b/buildSrc/src/main/java/NativeRelocator.java index eaa32152b..37c93cf78 100644 --- a/buildSrc/src/main/java/NativeRelocator.java +++ b/buildSrc/src/main/java/NativeRelocator.java @@ -6,8 +6,8 @@ import java.util.concurrent.CompletableFuture; class NativeRelocator { - private static final Path rootDirectory = Path.of(System.getProperty("user.dir"), "relocate_natives"); - private static final Path cacheRoot = rootDirectory.resolve("cache"); + private final Path rootDirectory; + private final Path cacheRoot; /** * Initializes the NativeRelocator by preparing the environment if necessary. @@ -15,15 +15,18 @@ class NativeRelocator * * @throws Exception if the preparation script fails or an unsupported OS is detected. */ - NativeRelocator() throws Exception + NativeRelocator(Path rootDirectory) throws Exception { - if (rootDirectory.resolve(".venv").toFile().exists()) + this.rootDirectory = rootDirectory; + this.cacheRoot = this.rootDirectory.resolve("cache"); + + if (this.rootDirectory.resolve(".venv").toFile().exists()) { return; } ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.directory(rootDirectory.toFile()); + processBuilder.directory(this.rootDirectory.toFile()); String os = System.getProperty("os.name").toLowerCase(); if (os.contains("win")) @@ -146,12 +149,12 @@ class NativeRelocator public byte[] fixModifiedBinary(Path outputFilePath, byte[] content) throws Exception { ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.directory(rootDirectory.toFile()); + processBuilder.directory(this.rootDirectory.toFile()); processBuilder.command( - rootDirectory.resolve(".venv/Scripts").toFile().exists() - ? rootDirectory.resolve(".venv/Scripts/python.exe").toString() - : rootDirectory.resolve(".venv/bin/python").toString(), + this.rootDirectory.resolve(".venv/Scripts").toFile().exists() + ? this.rootDirectory.resolve(".venv/Scripts/python.exe").toString() + : this.rootDirectory.resolve(".venv/bin/python").toString(), "./fix_modified_binary.py", outputFilePath.toString() ); @@ -184,7 +187,7 @@ class NativeRelocator */ public byte[] processBinary(String outputPath, byte[] content, Map replacements) throws Exception { - Path outputFilePath = cacheRoot.resolve(outputPath); + Path outputFilePath = this.cacheRoot.resolve(outputPath); //noinspection ResultOfMethodCallIgnored outputFilePath.getParent().toFile().mkdirs();