Skip to content

Commit

Permalink
feat: Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jan 23, 2025
1 parent 8ba80e8 commit d661883
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 136 deletions.
42 changes: 41 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
on:
push:
branches: [main]
branches: [main, windows]
pull_request:
branches: [main]

jobs:
test-windows:
runs-on: windows-latest
steps:
- name: Checkout project
uses: actions/[email protected]
- name: Checkout submodules
run: git submodule update --init --recursive
- name: Setup nightly Zig
uses: mlugg/setup-zig@v1
with:
version: master
- name: Build test ffi lib
run: zig build-lib -dynamic tests/utils/foreign.zig && mv libforeign.* tests/utils/

- name: Run tests Debug
run: zig build test
- name: Cleanup
run: rm -rf zig-out zig-cache
- name: Run tests Debug with JIT always on
run: zig build -Djit_always_on test
- name: Cleanup
run: rm -rf zig-out zig-cache

- name: Run tests ReleaseSafe
run: zig build -Doptimize=ReleaseSafe test
- name: Cleanup
run: rm -rf zig-out zig-cache
- name: Run tests ReleaseSafe with JIT always on
run: zig build -Doptimize=ReleaseSafe -Djit_always_on test
- name: Cleanup
run: rm -rf zig-out zig-cache

- name: Run tests ReleaseFast
run: zig build -Doptimize=ReleaseFast test
- name: Cleanup
run: rm -rf zig-out zig-cache
- name: Run tests ReleaseFast with JIT always on
run: zig build -Doptimize=ReleaseFast -Djit_always_on test
- name: Cleanup
run: rm -rf zig-out zig-cache
test-macos:
runs-on: macos-latest
steps:
Expand Down
81 changes: 16 additions & 65 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ const BuildOptions = struct {
};
};

fn getBuzzPrefix(b: *Build) ![]const u8 {
return std.posix.getenv("BUZZ_PATH") orelse std.fs.path.dirname(b.exe_dir).?;
}

pub fn build(b: *Build) !void {
var envMap = try std.process.getEnvMap(b.allocator);
defer envMap.deinit();

// Check minimum zig version
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse("0.14.0-dev.2851+b074fb7dd") catch return;
Expand All @@ -116,8 +115,8 @@ pub fn build(b: *Build) !void {
.target = target,
.version = std.SemanticVersion{ .major = 0, .minor = 5, .patch = 0 },
// Current commit sha
.sha = std.posix.getenv("GIT_SHA") orelse
std.posix.getenv("GITHUB_SHA") orelse std.mem.trim(
.sha = envMap.get("GIT_SHA") orelse
envMap.get("GITHUB_SHA") orelse std.mem.trim(
u8,
b.run(
&.{
Expand Down Expand Up @@ -264,22 +263,6 @@ pub fn build(b: *Build) !void {

const build_option_module = build_options.step(b);

var sys_libs = std.ArrayList([]const u8).init(b.allocator);
defer sys_libs.deinit();
var includes = std.ArrayList([]const u8).init(b.allocator);
defer includes.deinit();
var llibs = std.ArrayList([]const u8).init(b.allocator);
defer llibs.deinit();

includes.appendSlice(&[_][]const u8{
"./vendors/mir",
"./vendors/mimalloc/include",
}) catch unreachable;

llibs.appendSlice(&[_][]const u8{
"./vendors/mir",
}) catch unreachable;

const lib_pcre2 = if (!is_wasm)
try buildPcre2(b, target, build_mode)
else
Expand All @@ -288,7 +271,7 @@ pub fn build(b: *Build) !void {
try buildMimalloc(b, target, build_mode)
else
null;
const lib_linenoise = if (!is_wasm)
const lib_linenoise = if (!is_wasm and target.result.os.tag != .windows)
try buildLinenoise(b, target, build_mode)
else
null;
Expand Down Expand Up @@ -337,20 +320,6 @@ pub fn build(b: *Build) !void {
}
b.step("run", "run buzz").dependOn(&run_exe.step);

for (includes.items) |include| {
exe.addIncludePath(b.path(include));
exe_check.addIncludePath(b.path(include));
}
for (llibs.items) |lib| {
exe.addLibraryPath(b.path(lib));
exe_check.addLibraryPath(b.path(lib));
}
for (sys_libs.items) |slib| {
// FIXME: if mir is linked as static library (libmir.a), here also need to link libc
// it's better to built it with Zig's build system
exe.linkSystemLibrary(slib);
exe_check.linkSystemLibrary(slib);
}
if (build_options.needLibC()) {
exe.linkLibC();
exe_check.linkLibC();
Expand All @@ -372,15 +341,6 @@ pub fn build(b: *Build) !void {

b.installArtifact(lib);

for (includes.items) |include| {
lib.addIncludePath(b.path(include));
}
for (llibs.items) |llib| {
lib.addLibraryPath(b.path(llib));
}
for (sys_libs.items) |slib| {
lib.linkSystemLibrary(slib);
}
if (build_options.needLibC()) {
lib.linkLibC();
}
Expand All @@ -392,17 +352,21 @@ pub fn build(b: *Build) !void {

if (lib_pcre2) |pcre| {
lib.linkLibrary(pcre);
exe.linkLibrary(pcre);
}

if (lib_mimalloc) |mimalloc| {
lib.linkLibrary(mimalloc);
exe.linkLibrary(mimalloc);
if (lib.root_module.resolved_target.?.result.os.tag == .windows) {
lib.linkSystemLibrary("bcrypt");
exe.linkSystemLibrary("bcrypt");
}
}

if (lib_mir) |mir| {
lib.linkLibrary(mir);
exe.linkLibrary(mir);
}

// So that JIT compiled function can reference buzz_api
Expand Down Expand Up @@ -473,15 +437,6 @@ pub fn build(b: *Build) !void {
artifact.dest_dir = .{ .custom = "lib/buzz" };

// No need to link anything when building for wasm since everything is static
for (includes.items) |include| {
std_lib.addIncludePath(b.path(include));
}
for (llibs.items) |llib| {
std_lib.addLibraryPath(b.path(llib));
}
for (sys_libs.items) |slib| {
std_lib.linkSystemLibrary(slib);
}
if (build_options.needLibC()) {
std_lib.linkLibC();
}
Expand Down Expand Up @@ -515,15 +470,6 @@ pub fn build(b: *Build) !void {
.target = target,
.optimize = build_mode,
});
for (includes.items) |include| {
tests.addIncludePath(b.path(include));
}
for (llibs.items) |llib| {
tests.addLibraryPath(b.path(llib));
}
for (sys_libs.items) |slib| {
tests.linkSystemLibrary(slib);
}
if (build_options.needLibC()) {
tests.linkLibC();
}
Expand All @@ -544,7 +490,7 @@ pub fn build(b: *Build) !void {
const test_step = b.step("test", "Run all the tests");
const run_tests = b.addRunArtifact(tests);
run_tests.cwd = b.path(".");
run_tests.setEnvironmentVariable("BUZZ_PATH", try getBuzzPrefix(b));
run_tests.setEnvironmentVariable("BUZZ_PATH", envMap.get("BUZZ_PATH") orelse std.fs.path.dirname(b.exe_dir).?);
run_tests.step.dependOn(install_step); // wait for libraries to be installed
test_step.dependOn(&run_tests.step);
}
Expand Down Expand Up @@ -757,5 +703,10 @@ pub fn buildMir(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.O
},
);

if (target.result.os.tag == .windows) {
lib.linkSystemLibrary("kernel32");
lib.linkSystemLibrary("psapi");
}

return lib;
}
22 changes: 14 additions & 8 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ pub fn defaultBuzzPrefix() []const u8 {
}

var _buzz_path_buffer: [4096]u8 = undefined;
pub fn buzzPrefix() []const u8 {
pub fn buzzPrefix(allocator: std.mem.Allocator) error{OutOfMemory}![]const u8 {
// FIXME: don't use std.posix directly
if (std.posix.getenv("BUZZ_PATH")) |buzz_path| {
if (std.process.getEnvVarOwned(allocator, "BUZZ_PATH") catch |err| env: {
switch (err) {
error.EnvironmentVariableNotFound, error.InvalidWtf8 => break :env null,
else => return error.OutOfMemory,
}
}) |buzz_path| {
return buzz_path;
}

Expand All @@ -163,8 +168,9 @@ pub fn buzzPrefix() []const u8 {

var _buzz_path_buffer2: [4096]u8 = undefined;
/// the returned string can be used only until next call to this function
pub fn buzzLibPath() ![]const u8 {
const path2 = buzzPrefix();
pub fn buzzLibPath(allocator: std.mem.Allocator) ![]const u8 {
const path2 = try buzzPrefix(allocator);
defer allocator.free(path2);
const sep = std.fs.path.sep_str;
return std.fmt.bufPrint(
&_buzz_path_buffer2,
Expand Down Expand Up @@ -7597,7 +7603,7 @@ fn searchPaths(self: *Self, file_name: []const u8) ![][]const u8 {
self.gc.allocator,
suffixed,
"$",
try buzzLibPath(),
try buzzLibPath(self.gc.allocator),
);

try paths.append(prefixed);
Expand Down Expand Up @@ -7636,7 +7642,7 @@ fn searchLibPaths(self: *Self, file_name: []const u8) !std.ArrayList([]const u8)
self.gc.allocator,
suffixed,
"$",
try buzzLibPath(),
try buzzLibPath(self.gc.allocator),
);

try paths.append(prefixed);
Expand Down Expand Up @@ -8137,7 +8143,7 @@ fn importLibSymbol(self: *Self, full_file_name: []const u8, symbol: []const u8)
"External library `{s}` not found: {s}{s}\n",
.{
file_basename,
if (builtin.link_libc)
if (builtin.link_libc and builtin.os.tag != .windows)
std.mem.sliceTo(dlerror(), 0)
else
"",
Expand Down Expand Up @@ -8348,7 +8354,7 @@ fn zdefStatement(self: *Self) Error!Ast.Node.Index {
"External library `{s}` not found: {s}{s}\n",
.{
lib_name_str,
if (builtin.link_libc)
if (builtin.link_libc and builtin.os.tag != .windows)
std.mem.sliceTo(dlerror(), 0)
else
"",
Expand Down
2 changes: 1 addition & 1 deletion src/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ export fn bz_rethrow(vm: *VM) void {
if ((vm.currentFrame() == null or vm.currentFrame().?.in_native_call) and vm.current_fiber.try_context != null) {
// FIXME: close try scope

if (builtin.os.tag == .macos or builtin.os.tag == .linux or builtin.os.windows) {
if (builtin.os.tag == .macos or builtin.os.tag == .linux) {
jmp._longjmp(&vm.current_fiber.try_context.?.env, 1);
} else {
jmp.longjmp(&vm.current_fiber.try_context.?.env, 1);
Expand Down
Loading

0 comments on commit d661883

Please sign in to comment.