Skip to content

Commit

Permalink
Build: Make library file names unique across target machines
Browse files Browse the repository at this point in the history
This allows us to flatten the library directory. It also
enables the possibility of having all libraries inside the
same directory
Relates to #262
  • Loading branch information
weisJ committed Aug 15, 2021
1 parent 55cc511 commit f685afc
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 49 deletions.
12 changes: 8 additions & 4 deletions buildSrc/src/main/kotlin/JniUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ val TargetMachine.targetsHost: Boolean
}
}

fun libraryFileNameFor(project: Project, osFamily: OperatingSystemFamily): String = when {
osFamily.isWindows -> "${project.name}.dll"
osFamily.isLinux -> "lib${project.name}.so"
osFamily.isMacOS -> "lib${project.name}.dylib"
fun libraryFileNameFor(project: Project, osFamily: OperatingSystemFamily): String =
libraryFileNameFor(project.name, osFamily)


fun libraryFileNameFor(name : String, osFamily: OperatingSystemFamily): String = when {
osFamily.isWindows -> "$name.dll"
osFamily.isLinux -> "lib$name.so"
osFamily.isMacOS -> "lib$name.dylib"
else -> throw GradleException("Unknown operating system family '${osFamily}'.")
}

Expand Down
16 changes: 15 additions & 1 deletion buildSrc/src/main/kotlin/UberJniJarPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import dev.nokee.platform.jni.JniJarBinary
import dev.nokee.platform.jni.JavaNativeInterfaceLibrary
import dev.nokee.runtime.nativebase.TargetMachine
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.CopySpec
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.FileTree
import org.gradle.api.provider.Provider
import org.gradle.jvm.tasks.Jar
import java.io.File

class UberJniJarPlugin : Plugin<Project> {

Expand Down Expand Up @@ -44,15 +47,26 @@ class UberJniJarPlugin : Plugin<Project> {
// nativeRuntimeFiles will be populated in this case due to using pre-build binaries.
task.from(targetVariant.map { it.nativeRuntimeFiles }) {
into(targetVariant.map { it.resourcePath })
renameLibrary(project, target)
}
} else {
task.from(targetVariant.map { it.sharedLibrary.linkTask.map { linkTask -> linkTask.linkedFile } }) {
val linkFile = targetVariant.flatMap {
it.sharedLibrary.linkTask.flatMap { linkTask -> linkTask.linkedFile }
}
task.from(linkFile) {
into(targetVariant.map { it.resourcePath })
renameLibrary(project, target)
}
}
}
}

private fun CopySpec.renameLibrary(project: Project, target: TargetMachine) {
rename {
libraryFileNameFor("${project.name}-${target.architectureString}", target.operatingSystemFamily)
}
}

private fun JniJarBinary.asZipTree(project: Project): Provider<FileTree> =
jarTask.map { project.zipTree(it.archiveFile) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ class NativeLibraryTest {
void testLibrariesArePackages() {
TestWindowsLibrary windowsLibrary = new TestWindowsLibrary();
Assertions.assertNotNull(WindowsLibrary.class.getResource(
windowsLibrary.getX64Path() + windowsLibrary.getLibraryName()),
windowsLibrary.getX64Path()),
"x64 library doesn't exist");
Assertions.assertNotNull(WindowsLibrary.class.getResource(
windowsLibrary.getX86Path() + windowsLibrary.getLibraryName()),
windowsLibrary.getX86Path()),
"x86 library doesn't exist");
checkResourcesExists(WindowsLibrary.class, windowsLibrary.getResourcePaths());

TestMacOsLibrary macOSLibrary = new TestMacOsLibrary();
Assertions.assertNotNull(MacOSLibrary.class.getResource(
macOSLibrary.getX64Path() + macOSLibrary.getLibraryName()),
macOSLibrary.getX64Path()),
"x86-64 macOS library doesn't exist");
Assertions.assertNotNull(MacOSLibrary.class.getResource(
macOSLibrary.getArm64Path() + macOSLibrary.getLibraryName()),
macOSLibrary.getArm64Path()),
"arm64 macOS library doesn't exist");
checkResourcesExists(MacOSLibrary.class, macOSLibrary.getResourcePaths());
}
Expand Down
2 changes: 1 addition & 1 deletion macos/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ library {

targetMachines.addAll(machines.macOS.x86_64, machines.macOS.architecture("arm64"))
variants.configureEach {
resourcePath.set("$nativeResourcePath/${targetMachine.variantName}")
resourcePath.set(nativeResourcePath)
sharedLibrary {
val isArm = targetMachine.architectureString == "arm64"
val minOs = if (isArm) "11" else "10.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,28 @@

public class MacOSLibrary extends AbstractLibrary {

private static final String PATH = "/com/github/weisj/darklaf/platform/darklaf-macos/";
private static final String x86_64_PATH = "macos-x86-64/";
private static final String arm64_PATH = "macos-arm64/";
private static final String DLL_NAME = "libdarklaf-macos.dylib";
private static final String PATH = "/com/github/weisj/darklaf/platform/darklaf-macos";
private static final String x86_64_PATH = PATH + "/libdarklaf-macos-x86-64.dylib";
private static final String arm64_PATH = PATH + "/libdarklaf-macos-arm64.dylib";

private static final String FRAMEWORK_TARGET_PATH = "JavaNativeFoundation.framework/";
private static final String FRAMEWORK_PATH = PATH + FRAMEWORK_TARGET_PATH + "JavaNativeFoundation";
private static final String FRAMEWORK_TARGET_PATH = "/JavaNativeFoundation.framework";
private static final String FRAMEWORK_PATH = PATH + FRAMEWORK_TARGET_PATH + "/JavaNativeFoundation";
private static final MacOSLibrary instance = new MacOSLibrary();

public static MacOSLibrary get() {
return instance;
}

protected MacOSLibrary() {
super(PATH, DLL_NAME, LogUtil.getLogger(MacOSLibrary.class));
super("darklaf-macos", LogUtil.getLogger(MacOSLibrary.class));
}

protected String getArm64Path() {
return super.getPath() + arm64_PATH;
return arm64_PATH;
}

protected String getX64Path() {
return super.getPath() + x86_64_PATH;
return x86_64_PATH;
}

@Override
Expand All @@ -62,7 +61,7 @@ final protected Class<?> getLoaderClass() {
}

@Override
protected String getPath() {
public String getLibraryPath() {
if (SystemInfo.isX86Compatible && SystemInfo.isX64) {
return getX64Path();
} else if (SystemInfo.isM1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@
import java.util.logging.Level;
import java.util.logging.Logger;


public abstract class AbstractLibrary {

private final String name;
protected final Logger logger;
private final String path;
private final String libraryName;
private boolean loaded;
private boolean attemptedLoad;

public AbstractLibrary(final String path, final String libraryName) {
this(path, libraryName, Logger.getLogger(libraryName));
public AbstractLibrary(final String name) {
this(name, Logger.getLogger(name));
}

public AbstractLibrary(final String path, final String libraryName, final Logger logger) {
this.path = path;
this.libraryName = libraryName;
public AbstractLibrary(final String name, final Logger logger) {
this.name = name;
this.logger = logger;
}

Expand All @@ -66,11 +65,11 @@ private void loadLibrary() {
NativeUtil.loadLibraryFromJarWithExtraResources(getLoaderClass(), path, resources);
}
loaded = true;
info("Loaded " + getLibraryName() + ".");
info("Loaded " + name + " at " + path + ".");
}
} catch (final Throwable e) {
// Library not found, SecurityManager prevents library loading etc.
error("Could not load library " + getLibraryName() + ".", e);
error("Could not load library " + name + ".", e);
}
}

Expand All @@ -80,17 +79,7 @@ protected List<NativeUtil.Resource> getResourcePaths() {
return Collections.emptyList();
}

public String getLibraryPath() {
return getPath() + getLibraryName();
}

protected String getPath() {
return path;
}

public String getLibraryName() {
return libraryName;
}
public abstract String getLibraryPath();

protected abstract boolean canLoad();

Expand Down
2 changes: 1 addition & 1 deletion windows/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ library {

targetMachines.addAll(machines.windows.x86, machines.windows.x86_64)
variants.configureEach {
resourcePath.set("com/github/weisj/darklaf/platform/${project.name}/${targetMachine.variantName}")
resourcePath.set("com/github/weisj/darklaf/platform/${project.name}")
sharedLibrary {
compileTasks.configureEach {
compilerArgs.addAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@

public class WindowsLibrary extends AbstractLibrary {

private static final String PATH = "/com/github/weisj/darklaf/platform/darklaf-windows/";
private static final String DLL_NAME = "darklaf-windows.dll";
private static final String x86_PATH = "windows-x86/";
private static final String x86_64_PATH = "windows-x86-64/";
private static final String PATH = "/com/github/weisj/darklaf/platform/darklaf-windows";
private static final String x86_PATH = PATH + "/darklaf-windows-x86.dll";
private static final String x86_64_PATH = PATH + "/darklaf-windows-x86-64.dll";
private static final WindowsLibrary library = new WindowsLibrary();

public static WindowsLibrary get() {
return library;
}

protected WindowsLibrary() {
super(PATH, DLL_NAME, LogUtil.getLogger(WindowsLibrary.class));
super("darklaf-windows", LogUtil.getLogger(WindowsLibrary.class));
}

@Override
Expand All @@ -47,15 +46,15 @@ final protected Class<?> getLoaderClass() {
}

protected String getX86Path() {
return super.getPath() + x86_PATH;
return x86_PATH;
}

protected String getX64Path() {
return super.getPath() + x86_64_PATH;
return x86_64_PATH;
}

@Override
protected String getPath() {
public String getLibraryPath() {
if (SystemInfo.isX86) {
return getX86Path();
} else if (SystemInfo.isX64) {
Expand Down

0 comments on commit f685afc

Please sign in to comment.