-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
175 lines (163 loc) · 6.32 KB
/
build.zig
File metadata and controls
175 lines (163 loc) · 6.32 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const std = @import("std");
const builtin = @import("builtin");
/// Supported WebKitGTK versions for linking on Linux.
pub const WebkitGtkVersion = enum {
@"4.0",
@"4.1",
@"6.0",
};
/// Applies macOS SDK configuration for cross-compilation if suitable options are provided.
///
/// Automatically configures system include paths, library paths, and framework paths
/// when cross-compiling for macOS from non-macOS hosts.
pub fn tryApplyMacOsSdk(b: *std.Build, mod: *std.Build.Module, options: BuildOptions) void {
if (options.target.result.os.tag == .macos and builtin.os.tag != .macos and options.macos_sdk != null) {
const macos_sdk_path: std.Build.LazyPath = .{ .cwd_relative = options.macos_sdk.? };
mod.addSystemIncludePath(macos_sdk_path.path(b, "usr/include"));
mod.addLibraryPath(macos_sdk_path.path(b, "usr/lib"));
mod.addSystemFrameworkPath(macos_sdk_path.path(b, "System/Library/Frameworks"));
}
}
const BuildOptions = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
macos_sdk: ?[]const u8 = null,
webkitgtk: WebkitGtkVersion = .@"4.1",
};
pub fn build(b: *std.Build) void {
const options: BuildOptions = .{
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
.macos_sdk = b.option([]const u8, "macos-sdk", "Path to macOS SDK (optional), used on non-macOS platforms"),
.webkitgtk = b.option(WebkitGtkVersion, "webkitgtk", "Version of WebKitGTK to link against (default: 4.1), Linux only") orelse .@"4.1",
};
const lib = addLibrary(b, options);
const mod = addModule(b, options, lib);
addTestStep(b, mod);
addExamplesStep(b, options, mod);
addDocStep(b, mod);
}
fn addLibrary(b: *std.Build, options: BuildOptions) *std.Build.Step.Compile {
const upstream = b.dependency("upstream", .{});
const mod = b.createModule(.{
.target = options.target,
.optimize = options.optimize,
.link_libcpp = true,
});
mod.addIncludePath(upstream.path("core/include"));
mod.addIncludePath(b.path("c"));
mod.addCMacro("WEBVIEW_STATIC", "1");
mod.addCSourceFile(.{ .file = b.path("c/webview/window.c"), .flags = &.{} });
switch (options.target.result.os.tag) {
.windows => {
mod.addCSourceFile(.{ .file = b.path("c/webview.cc"), .flags = &.{"-std=c++14"} });
mod.addIncludePath(b.path("deps/WebView2/"));
mod.linkSystemLibrary("advapi32", .{});
mod.linkSystemLibrary("ole32", .{});
mod.linkSystemLibrary("shell32", .{});
mod.linkSystemLibrary("shlwapi", .{});
mod.linkSystemLibrary("user32", .{});
mod.linkSystemLibrary("version", .{});
},
.macos => {
tryApplyMacOsSdk(b, mod, options);
mod.addCSourceFile(.{ .file = b.path("c/webview.cc"), .flags = &.{"-std=c++11"} });
mod.linkFramework("WebKit", .{});
},
.linux => {
mod.addCSourceFile(.{ .file = b.path("c/webview.cc"), .flags = &.{"-std=c++11"} });
switch (options.webkitgtk) {
.@"4.0" => {
mod.linkSystemLibrary("gtk+-3.0", .{});
mod.linkSystemLibrary("webkit2gtk-4.0", .{});
},
.@"4.1" => {
mod.linkSystemLibrary("gtk+-3.0", .{});
mod.linkSystemLibrary("webkit2gtk-4.1", .{});
},
.@"6.0" => {
mod.linkSystemLibrary("gtk-4", .{});
mod.linkSystemLibrary("webkitgtk-6.0", .{});
},
}
},
else => unreachable,
}
const lib = b.addLibrary(.{
.name = "webview",
.root_module = mod,
.linkage = .static,
});
b.installArtifact(lib);
return lib;
}
fn addModule(b: *std.Build, options: BuildOptions, lib: *std.Build.Step.Compile) *std.Build.Module {
const webview_c = addCBindings(b, options);
const mod = b.addModule("webview", .{
.root_source_file = b.path("src/root.zig"),
.target = options.target,
.optimize = options.optimize,
.imports = &.{
.{ .name = "webview_c", .module = webview_c },
},
});
mod.linkLibrary(lib);
return mod;
}
fn addTestStep(b: *std.Build, mod: *std.Build.Module) void {
const test_step = b.step("test", "Run tests");
const mod_test = b.addTest(.{ .root_module = mod });
const run_mod_test = b.addRunArtifact(mod_test);
test_step.dependOn(&run_mod_test.step);
}
fn addExamplesStep(b: *std.Build, options: BuildOptions, mod: *std.Build.Module) void {
const examples_step = b.step("examples", "Build all examples");
const examples = [_][]const u8{
"basic",
"bind",
"easy",
"window",
};
inline for (examples) |name| {
const example_mod = b.createModule(.{
.root_source_file = b.path(b.fmt("examples/{s}.zig", .{name})),
.target = options.target,
.optimize = options.optimize,
.imports = &.{
.{ .name = "webview", .module = mod },
},
});
tryApplyMacOsSdk(b, example_mod, options);
const exe = b.addExecutable(.{
.name = name,
.root_module = example_mod,
});
const install = b.addInstallArtifact(exe, .{});
examples_step.dependOn(&install.step);
}
}
fn addDocStep(b: *std.Build, mod: *std.Build.Module) void {
const doc_step = b.step("doc", "Generate documentation");
const doc_obj = b.addObject(.{
.name = "webview",
.root_module = mod,
});
const install_doc = b.addInstallDirectory(.{
.source_dir = doc_obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "doc",
});
doc_step.dependOn(&install_doc.step);
}
fn addCBindings(b: *std.Build, options: BuildOptions) *std.Build.Module {
const upstream = b.dependency("upstream", .{});
const translate = b.addTranslateC(.{
.root_source_file = b.path("c/webview/all.h"),
.target = options.target,
.optimize = options.optimize,
});
translate.addIncludePath(upstream.path("core/include"));
translate.addIncludePath(b.path("c"));
translate.defineCMacro("WEBVIEW_STATIC", "1");
return translate.createModule();
}