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 e1dea3f commit 7c91651
Show file tree
Hide file tree
Showing 221 changed files with 74,693 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ samples/**/yarn.lock
zipline-gradle-plugin/src/test/projects/**/yarn.lock

generated-zipline-webpack-config.js
/zipline/.zig-cache/
/.vscode
84 changes: 59 additions & 25 deletions zipline/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ const builtin = @import("builtin");

const targets: []const std.Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .macos },
// .{ .cpu_arch = .aarch64, .os_tag = .windows },
.{ .cpu_arch = .aarch64, .os_tag = .windows },
.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
// .{ .cpu_arch = .x86_64, .os_tag = .windows },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
};

pub fn build(b: *std.Build) !void {
const mode = b.standardOptimizeOption(.{});
const mode = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSafe });

const al = std.heap.page_allocator;

Expand All @@ -27,7 +27,7 @@ pub fn build(b: *std.Build) !void {
.pic = true, // Platform-independent code (i.e., relative jumps) to be safe.
});

try commonQuickJsSetup(quickjs, version, al);
try commonQuickJsSetup(b, quickjs, version, al);

const installStep = b.addInstallArtifact(quickjs, .{
.dest_dir = .{ .override = .{ .custom = try getOutputDir(target, al) } },
Expand All @@ -36,7 +36,7 @@ pub fn build(b: *std.Build) !void {
}
}

fn commonQuickJsSetup(quickjs: *std.Build.Step.Compile, version: []const u8, al: std.mem.Allocator) !void {
fn commonQuickJsSetup(b: *std.Build, quickjs: *std.Build.Step.Compile, version: []const u8, al: std.mem.Allocator) !void {
var quoted_version_buf: [12]u8 = undefined;
const quoted_version = try std.fmt.bufPrint(&quoted_version_buf, "\"{s}\"", .{version});
quickjs.defineCMacro("CONFIG_VERSION", quoted_version);
Expand All @@ -49,7 +49,6 @@ fn commonQuickJsSetup(quickjs: *std.Build.Step.Compile, version: []const u8, al:
defer al.free(java_include);

quickjs.addIncludePath(absPath(java_include));
// quickjs.addIncludeDir(java_include);

// Walk the include/ directory for any child dirs (usually platform specific) and add them too.
const java_include_dir = try std.fs.cwd().openDir(java_include, .{ .iterate = true });
Expand All @@ -63,35 +62,36 @@ fn commonQuickJsSetup(quickjs: *std.Build.Step.Compile, version: []const u8, al:
defer al.free(include_subdir);

quickjs.addIncludePath(absPath(include_subdir));
// quickjs.addIncludeDir(include_subdir);
},
else => {},
}
}

quickjs.linkLibC();

quickjs.addCSourceFiles(.{ .files = &.{
"native/quickjs/cutils.c",
"native/quickjs/libregexp.c",
"native/quickjs/libunicode.c",
"native/quickjs/quickjs.c",
"native/common/finalization-registry.c",
"native/common/context-no-eval.c",
}, .flags = &.{
const quickjsCFiles = try listFilesWithExtension(".c", al, "native/quickjs/");
const commonCFiles = try listFilesWithExtension(".c", al, "native/common/");
quickjs.addCSourceFiles(.{ .files = quickjsCFiles, .flags = &.{
"-std=gnu99",
} });
quickjs.addCSourceFiles(.{ .files = commonCFiles, .flags = &.{
"-std=gnu99",
} });

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

if (quickjs.rootModuleTarget().os.tag == .windows) {
// Add native/winpthreads.
const winpthreadsCFiles = try listFilesWithExtension(".c", al, "native/winpthreads/src/");
quickjs.addCSourceFiles(.{ .files = winpthreadsCFiles, .flags = &.{
"-std=gnu99",
} });

quickjs.addIncludePath(b.path("native/winpthreads/include"));
quickjs.addIncludePath(b.path("native/winpthreads/src"));
}
}

fn readVersionFile(version_buf: []u8) ![]u8 {
Expand Down Expand Up @@ -128,8 +128,42 @@ fn getOutputDir(target: std.Target.Query, allocator: std.mem.Allocator) ![]const
else => return error.UnsupportedCPUArch,
};

var buffer: [20]u8 = undefined; // Adjust the size as necessary
const outputDir = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ os_dir, arch_dir });
var buffer: [64]u8 = undefined; // Adjust the size as necessary
const outputDir = try std.fmt.bufPrint(&buffer, "../src/jvmMain/resources/jni/{s}/{s}", .{ os_dir, arch_dir });

return allocator.dupe(u8, outputDir);
}

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

var files = std.ArrayList([]const u8).init(allocator);

var it = dir.iterate();

while (try it.next()) |entry| {
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);
}
allocator.free(full_path);
} else if (entry.kind == .file) {
if (std.mem.endsWith(u8, entry.name, ext)) {
try files.append(full_path);
} else {
allocator.free(full_path);
}
} else {
allocator.free(full_path);
}
}

return files.toOwnedSlice();
}
57 changes: 57 additions & 0 deletions zipline/native/winpthreads/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Copyright (c) 2011 mingw-w64 project

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.


/*
* Parts of this library are derived by:
*
* Posix Threads library for Microsoft Windows
*
* Use at own risk, there is no implied warranty to this code.
* It uses undocumented features of Microsoft Windows that can change
* at any time in the future.
*
* (C) 2010 Lockless Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Lockless Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AN
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
49 changes: 49 additions & 0 deletions zipline/native/winpthreads/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

SUBDIRS = . tests

AM_CFLAGS = -Wall -DWIN32_LEAN_AND_MEAN
ACLOCAL_AMFLAGS = -I m4

lib_LTLIBRARIES = libwinpthread.la

include_HEADERS = include/pthread.h include/sched.h include/semaphore.h include/pthread_unistd.h include/pthread_time.h include/pthread_compat.h include/pthread_signal.h
libwinpthread_la_CPPFLAGS = -I$(srcdir)/include -DIN_WINPTHREAD -DWINPTHREAD_DBG=1 -D__USE_MINGW_ANSI_STDIO=0
libwinpthread_la_LDFLAGS = -no-undefined -version-info 1:0:0 -L$(builddir)/fakelib -Wc,-no-pthread
EXTRA_libwinpthread_la_DEPENDENCIES = fakelib/libgcc.a fakelib/libgcc_eh.a fakelib/libgcc_s.a
libwinpthread_la_SOURCES = \
src/barrier.h src/cond.h src/misc.h src/mutex.h src/rwlock.h src/spinlock.h src/thread.h src/ref.h src/sem.h src/wpth_ver.h \
src/barrier.c src/cond.c src/misc.c src/mutex.c src/rwlock.c src/spinlock.c src/thread.c src/ref.c src/sem.c src/sched.c \
src/winpthread_internal.h src/clock.c src/nanosleep.c src/version.rc

# Break circular dep on bootstrap
noinst_LIBRARIES = fakelib/libgcc.a fakelib/libgcc_eh.a fakelib/libgcc_s.a
fakelib_libgcc_a_SOURCES = src/libgcc/dll_dependency.S src/libgcc/dll_math.c
fakelib_libgcc_s_a_SOURCES =
fakelib_libgcc_eh_a_SOURCES =

lib_LIBRARIES =

if COPY_STATIC
lib_LIBRARIES += libpthread.a
libpthread_a_SOURCES =
libpthread_a_DEPENDENCIES = libwinpthread.la
#FIXME: Use cp kludge until a better method presents itself
#libpthread_a_LIBADD = $(LT_OBJDIR)/libwinpthread.a
libpthread_a_AR = cp -f $(LT_OBJDIR)/libwinpthread.a
endif

if COPY_SHARED
lib_LIBRARIES += libpthread.dll.a
libpthread_dll_a_SOURCES =
libpthread_dll_a_DEPENDENCIES = libwinpthread.la
#FIXME: Use cp kludge until a better method presents itself
#libpthread_dll_a_LIBADD = $(LT_OBJDIR)/libwinpthread.dll.a
libpthread_dll_a_AR = cp -f $(LT_OBJDIR)/libwinpthread.dll.a
endif

# Tell libtool how to use the resource compiler
.rc.lo:
$(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --tag=RC --mode=compile $(RC) $(RCFLAGS) -i $< -o $@

DISTCHECK_CONFIGURE_FLAGS = --host=$(host_triplet)

Loading

0 comments on commit 7c91651

Please sign in to comment.