Skip to content

Commit

Permalink
WIP: Zig with Windows Build.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shabinder committed Aug 12, 2024
1 parent 7c91651 commit 618622f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
24 changes: 13 additions & 11 deletions zipline/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ fn commonQuickJsSetup(b: *std.Build, quickjs: *std.Build.Step.Compile, version:
}

quickjs.linkLibC();
const quickjsCFiles = try listFilesWithExtension(".c", al, "native/quickjs/");
const commonCFiles = try listFilesWithExtension(".c", al, "native/common/");
const quickjsCFiles = try listFilesWithExtension(".c", al, "native/quickjs/", false);
const commonCFiles = try listFilesWithExtension(".c", al, "native/common/", false);
quickjs.addCSourceFiles(.{ .files = quickjsCFiles, .flags = &.{
"-std=gnu99",
} });
Expand All @@ -78,13 +78,13 @@ fn commonQuickJsSetup(b: *std.Build, quickjs: *std.Build.Step.Compile, version:
} });

quickjs.linkLibCpp();
quickjs.addCSourceFiles(.{ .files = try listFilesWithExtension(".cpp", al, "native/"), .flags = &.{
quickjs.addCSourceFiles(.{ .files = try listFilesWithExtension(".cpp", al, "native/", false), .flags = &.{
"-std=c++11",
} });

if (quickjs.rootModuleTarget().os.tag == .windows) {
// Add native/winpthreads.
const winpthreadsCFiles = try listFilesWithExtension(".c", al, "native/winpthreads/src/");
const winpthreadsCFiles = try listFilesWithExtension(".c", al, "native/winpthreads/src/", true);
quickjs.addCSourceFiles(.{ .files = winpthreadsCFiles, .flags = &.{
"-std=gnu99",
} });
Expand Down Expand Up @@ -134,7 +134,7 @@ fn getOutputDir(target: std.Target.Query, allocator: std.mem.Allocator) ![]const
return allocator.dupe(u8, outputDir);
}

fn listFilesWithExtension(ext: []const u8, allocator: std.mem.Allocator, dir_path: []const u8) ![]const []const u8 {
fn listFilesWithExtension(ext: []const u8, allocator: std.mem.Allocator, dir_path: []const u8, recursive: bool) ![]const []const u8 {
var dir = try std.fs.cwd().openDir(dir_path, .{});
defer dir.close();

Expand All @@ -146,12 +146,14 @@ fn listFilesWithExtension(ext: []const u8, allocator: std.mem.Allocator, dir_pat
const full_path = try std.fs.path.join(allocator, &[_][]const u8{ dir_path, entry.name });

if (entry.kind == .directory) {
// Recursively collect files in subdirectories
const subfiles = try listFilesWithExtension(ext, allocator, full_path);
defer allocator.free(subfiles);

for (subfiles) |subfile| {
try files.append(subfile);
if (recursive) {
// Recursively collect files in subdirectories (if enabled)
const subfiles = try listFilesWithExtension(ext, allocator, full_path, recursive);
defer allocator.free(subfiles);

for (subfiles) |subfile| {
try files.append(subfile);
}
}
allocator.free(full_path);
} else if (entry.kind == .file) {
Expand Down
27 changes: 11 additions & 16 deletions zipline/src/jvmMain/kotlin/app/cash/zipline/QuickJsNativeLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,33 @@ import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.util.Locale.US

@Suppress("UnsafeDynamicallyLoadedCode") // Only loading from our own JAR contents.
internal actual fun loadNativeLibrary() {
val osName = System.getProperty("os.name")
val osArch = System.getProperty("os.arch")
val osName = System.getProperty("os.name").lowercase(US)
val osArch = System.getProperty("os.arch").lowercase(US)

val targetOs = when {
osName.equals("Mac OS X", ignoreCase = true) -> "macos"
osName.startsWith("Linux", ignoreCase = true) -> "linux"
osName.startsWith("Win", ignoreCase = true) -> "windows"
else -> error("Unsupported OS: $osName")
osName.startsWith("linux") -> "linux"
osName.startsWith("mac") -> "macos"
osName.startsWith("win") -> "windows"
else -> throw IllegalStateException("Unsupported OS: $osName ($osArch)")
}

val targetArch = when (osArch) {
"x86_64", "amd64" -> "x64"
"aarch64", "arm64" -> "arm64"
else -> error("Unsupported arch: $osArch")
else -> throw IllegalStateException("Unsupported Arch: $osName ($osArch)")
}

val extension = when {
osName.startsWith("Win", true) -> "dll"
osName.startsWith("Mac") -> "dylib"
osName.startsWith("mac") -> "dylib"
osName.startsWith("win") -> "dll"
else -> "so"
}

val nativeLibraryJarPath = if (osName.contains("linux")) {
"/jni/$targetOs/$targetArch/$osArch/libquickjs.$extension"
} else if (osName.contains("mac")) {
"/jni/$targetOs/$targetArch/libquickjs.$extension"
} else {
throw IllegalStateException("Unsupported OS: $osName ($osArch)")
}
val nativeLibraryJarPath = "/jni/$targetOs/$targetArch/libquickjs.$extension"
val nativeLibraryUrl = QuickJs::class.java.getResource(nativeLibraryJarPath)
?: throw IllegalStateException("Unable to read $nativeLibraryJarPath from JAR")
val nativeLibraryFile: Path
Expand Down

0 comments on commit 618622f

Please sign in to comment.