Skip to content

Make library compatible to Windows ARM64 as well #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# Note: make apparently thinks, that options specified with "/" are absolute paths and resolves them. see also https://stackoverflow.com/questions/17012419/d9024-make-unrecognized-source-file-type
ARCH ?= x64
WIN_SDK_VERSION ?= 10.0.22621.0
MSVC_VERSION ?= 14.41.34120
HEADERS := -I"src\main\headers" \
-I"${JAVA_HOME}\include" \
-I"${JAVA_HOME}\include\win32" \
-I"C:\Program Files (x86)\Windows Kits\10\Include\$(WIN_SDK_VERSION)\cppwinrt"
SOURCES := $(wildcard src/main/native/*.cpp)
OUT_DIR := target/$(ARCH)
OUT_DLL := src/main/resources/integrations-$(ARCH).dll
OUT_LIB := $(OUT_DIR)/integrations.lib

########

all: install

install:
@if not exist "$(OUT_DIR)" mkdir "$(OUT_DIR)"
cl -EHsc -std:c++17 -LD -W4 -guard:cf \
-Fe"src/main/resources/integrations.dll" \
-Fo"target/" \
-Fe"$(OUT_DLL)" \
-Fo"$(OUT_DIR)/" \
$(HEADERS) $(SOURCES) \
-link -NXCOMPAT -DYNAMICBASE \
-implib:target/integrations.lib \
-implib:$(OUT_LIB) \
crypt32.lib shell32.lib ole32.lib uuid.lib user32.lib Advapi32.lib windowsapp.lib
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This project uses the following JVM properties:

* JDK 22
* Maven
* MSVC 2022 toolset (e.g. by installing Visual Studio 2022, Workset "Desktop development with C++")
* MSVC 2022 toolset (e.g. by installing Visual Studio 2022, Workset "Desktop development with C++" and component "MSVC v143 VS 2022 C++ ARM64/ARM64EC-Buildtools")
* Make (`choco install make`)

### Build
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@
</environmentVariables>
<arguments>
<argument>/c</argument>
<argument>"${devCommandFileDir}\vcvars64.bat" &amp;&amp; make install</argument>
<argument>"${devCommandFileDir}\vcvars64.bat" &amp;&amp; make install &amp;&amp; "${devCommandFileDir}\vcvarsamd64_arm64.bat" &amp;&amp; make install ARCH=arm64</argument>
</arguments>
</configuration>
</execution>
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/cryptomator/windows/common/NativeLibLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
public class NativeLibLoader {

private static final Logger LOG = LoggerFactory.getLogger(NativeLibLoader.class);
private static final String LIB = "/integrations.dll";
private static final String X64_LIB = "/integrations-x64.dll";
private static final String ARM64_LIB = "/integrations-arm64.dll";
private static String LIBNAME;
private static volatile boolean loaded = false;

/**
Expand All @@ -21,18 +23,26 @@ public class NativeLibLoader {
*/
public static synchronized void loadLib() {
if (!loaded) {
try (var dll = NativeLibLoader.class.getResourceAsStream(LIB)) {
var arch = System.getProperty("os.arch");
if (arch.contains("amd64")) {
LOG.debug("Loading library for x86_64 architecture");
LIBNAME = X64_LIB;
} else if (arch.contains("aarch64")) {
LOG.debug("Loading library for aarch64 architecture");
LIBNAME = ARM64_LIB;
}
Comment on lines +26 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add a fallback for unrecognized architectures.

The current implementation only handles "amd64" and "aarch64" architectures but doesn't provide a fallback for unrecognized architectures. This could lead to a NullPointerException when using LIBNAME on line 34.

 var arch = System.getProperty("os.arch");
 if (arch.contains("amd64")) {
 	LOG.debug("Loading library for x86_64 architecture");
 	LIBNAME = X64_LIB;
 } else if (arch.contains("aarch64")) {
 	LOG.debug("Loading library for aarch64 architecture");
 	LIBNAME = ARM64_LIB;
+} else {
+	LOG.warn("Unrecognized architecture: {}. Defaulting to x86_64 architecture", arch);
+	LIBNAME = X64_LIB;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var arch = System.getProperty("os.arch");
if (arch.contains("amd64")) {
LOG.debug("Loading library for x86_64 architecture");
LIBNAME = X64_LIB;
} else if (arch.contains("aarch64")) {
LOG.debug("Loading library for aarch64 architecture");
LIBNAME = ARM64_LIB;
}
var arch = System.getProperty("os.arch");
if (arch.contains("amd64")) {
LOG.debug("Loading library for x86_64 architecture");
LIBNAME = X64_LIB;
} else if (arch.contains("aarch64")) {
LOG.debug("Loading library for aarch64 architecture");
LIBNAME = ARM64_LIB;
} else {
LOG.warn("Unrecognized architecture: {}. Defaulting to x86_64 architecture", arch);
LIBNAME = X64_LIB;
}

try (var dll = NativeLibLoader.class.getResourceAsStream(LIBNAME)) {
Objects.requireNonNull(dll);
Path tmpPath = Files.createTempFile("lib", ".dll");
Files.copy(dll, tmpPath, StandardCopyOption.REPLACE_EXISTING);
System.load(tmpPath.toString());
loaded = true;
} catch (NullPointerException e) {
LOG.error("Did not find resource " + LIB, e);
LOG.error("Did not find resource " + LIBNAME, e);
} catch (IOException e) {
LOG.error("Failed to copy " + LIB + " to temp dir.", e);
LOG.error("Failed to copy " + LIBNAME + " to temp dir.", e);
} catch (UnsatisfiedLinkError e) {
LOG.error("Failed to load lib from " + LIB, e);
LOG.error("Failed to load lib from " + LIBNAME, e);
}
}
}
Expand Down