-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.zig
executable file
·136 lines (105 loc) · 4.82 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
//Example
{
const lib = b.addSharedLibrary("jni_example", "test/demo.zig", .unversioned);
if (@hasField(std.build.LibExeObjStep, "use_stage1"))
lib.use_stage1 = true;
lib.addPackagePath("jui", "src/jui.zig");
lib.setTarget(target);
lib.setBuildMode(mode);
lib.install();
}
const java_home = b.env_map.get("JAVA_HOME") orelse @panic("JAVA_HOME not defined.");
const libjvm_path = if (builtin.os.tag == .windows) "/lib" else "/lib/server";
{
const exe = b.addExecutable("class2zig", "tools/class2zig.zig");
if (@hasField(std.build.LibExeObjStep, "use_stage1"))
exe.use_stage1 = true;
exe.addPackagePath("jui", "src/jui.zig");
exe.addPackagePath("cf", "dep/cf/cf.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("class2zig", "Run class2zig tool");
run_step.dependOn(&run_cmd.step);
}
//Example
{
const exe = b.addExecutable("guessing_game", "examples/guessing-game/main.zig");
if (@hasField(std.build.LibExeObjStep, "use_stage1"))
exe.use_stage1 = true;
exe.addPackagePath("jui", "src/jui.zig");
exe.addLibraryPath(b.pathJoin(&.{ java_home, libjvm_path }));
exe.linkSystemLibrary("jvm");
exe.linkLibC();
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("guessing_game", "Run guessing game example");
run_step.dependOn(&run_cmd.step);
}
// Tests (it requires a JDK installed)
{
const main_tests = b.addTest("src/jui.zig");
if (@hasDecl(@TypeOf(main_tests.*), "addLibraryPath")) {
main_tests.addLibraryPath(b.pathJoin(&.{ java_home, libjvm_path }));
} else {
// Deprecated on zig 0.10
main_tests.addLibPath(b.pathJoin(&.{ java_home, libjvm_path }));
}
main_tests.linkSystemLibrary("jvm");
main_tests.linkLibC();
// TODO: Depending on the JVM available to the distro:
if (builtin.os.tag == .linux) {
main_tests.target.abi = .gnu;
}
if (builtin.os.tag == .windows) {
// Sets the DLL path:
const setDllDirectory = struct {
pub extern "kernel32" fn SetDllDirectoryA(path: [*:0]const u8) callconv(.C) std.os.windows.BOOL;
}.SetDllDirectoryA;
var java_bin_path = std.fs.path.joinZ(b.allocator, &.{ java_home, "\\bin" }) catch unreachable;
defer b.allocator.free(java_bin_path);
_ = setDllDirectory(java_bin_path);
var java_bin_server_path = std.fs.path.joinZ(b.allocator, &.{ java_home, "\\bin\\server" }) catch unreachable;
defer b.allocator.free(java_bin_server_path);
_ = setDllDirectory(java_bin_server_path);
// TODO: Define how we can disable the SEGV handler just for a single call:
// The function `JNI_CreateJavaVM` tries to detect the stack size
// and causes a SEGV that is handled by the Zig side
// https://bugzilla.redhat.com/show_bug.cgi?id=1572811#c7
//
// The simplest workarround is just run the tests in "ReleaseFast" mode,
// and for some reason it is not needed on Linux.
main_tests.setBuildMode(.ReleaseFast);
} else {
main_tests.setBuildMode(mode);
}
var test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
const argv: []const []const u8 = &.{ b.pathJoin(&.{ java_home, "/bin/javac" ++ if (builtin.os.tag == .windows) ".exe" else "" }), "test/src/com/jui/TypesTest.java" };
_ = b.execFromStep(argv, test_step) catch |err| {
std.debug.panic("Failed to compile Java test files: {}", .{err});
};
}
}