|
| 1 | +const std = @import("std"); |
| 2 | +const builtin = @import("builtin"); |
| 3 | + |
| 4 | +const WebkitGtkVersion = enum { |
| 5 | + @"4.0", |
| 6 | + @"4.1", |
| 7 | + @"6.0", |
| 8 | +}; |
| 9 | + |
| 10 | +const BuildOptions = struct { |
| 11 | + target: std.Build.ResolvedTarget, |
| 12 | + optimize: std.builtin.OptimizeMode, |
| 13 | + macos_sdk: ?[]const u8 = null, |
| 14 | + webkitgtk: WebkitGtkVersion = .@"4.1", |
| 15 | +}; |
| 16 | + |
| 17 | +pub fn build(b: *std.Build) void { |
| 18 | + const options: BuildOptions = .{ |
| 19 | + .target = b.standardTargetOptions(.{}), |
| 20 | + .optimize = b.standardOptimizeOption(.{}), |
| 21 | + .macos_sdk = b.option([]const u8, "macos-sdk", "Path to macOS SDK (optional), used on non-macOS platforms"), |
| 22 | + .webkitgtk = b.option(WebkitGtkVersion, "webkitgtk", "Version of WebKitGTK to link against (default: 4.1), Linux only") orelse .@"4.1", |
| 23 | + }; |
| 24 | + |
| 25 | + const lib = addLibrary(b, options); |
| 26 | + const mod = addModule(b, options, lib); |
| 27 | + |
| 28 | + addTestStep(b, mod); |
| 29 | + addExamplesStep(b, options, mod); |
| 30 | + addDocStep(b, mod); |
| 31 | +} |
| 32 | + |
| 33 | +fn addLibrary(b: *std.Build, options: BuildOptions) *std.Build.Step.Compile { |
| 34 | + const upstream = b.dependency("webview", .{}); |
| 35 | + const mod = b.createModule(.{ |
| 36 | + .target = options.target, |
| 37 | + .optimize = options.optimize, |
| 38 | + .link_libcpp = true, |
| 39 | + }); |
| 40 | + mod.addIncludePath(upstream.path("core/include")); |
| 41 | + mod.addCMacro("WEBVIEW_STATIC", "1"); |
| 42 | + switch (options.target.result.os.tag) { |
| 43 | + .windows => { |
| 44 | + mod.addCSourceFile(.{ .file = upstream.path("core/src/webview.cc"), .flags = &.{"-std=c++14"} }); |
| 45 | + mod.addIncludePath(b.path("deps/WebView2/")); |
| 46 | + mod.linkSystemLibrary("advapi32", .{}); |
| 47 | + mod.linkSystemLibrary("ole32", .{}); |
| 48 | + mod.linkSystemLibrary("shell32", .{}); |
| 49 | + mod.linkSystemLibrary("shlwapi", .{}); |
| 50 | + mod.linkSystemLibrary("user32", .{}); |
| 51 | + mod.linkSystemLibrary("version", .{}); |
| 52 | + }, |
| 53 | + .macos => { |
| 54 | + tryApplyMacOsSdk(b, mod, options); |
| 55 | + mod.addCSourceFile(.{ .file = upstream.path("core/src/webview.cc"), .flags = &.{"-std=c++11"} }); |
| 56 | + mod.linkFramework("WebKit", .{}); |
| 57 | + }, |
| 58 | + .linux => { |
| 59 | + mod.addCSourceFile(.{ .file = upstream.path("core/src/webview.cc"), .flags = &.{"-std=c++11"} }); |
| 60 | + switch (options.webkitgtk) { |
| 61 | + .@"4.0" => { |
| 62 | + mod.linkSystemLibrary("gtk+-3.0", .{}); |
| 63 | + mod.linkSystemLibrary("webkit2gtk-4.0", .{}); |
| 64 | + }, |
| 65 | + .@"4.1" => { |
| 66 | + mod.linkSystemLibrary("gtk+-3.0", .{}); |
| 67 | + mod.linkSystemLibrary("webkit2gtk-4.1", .{}); |
| 68 | + }, |
| 69 | + .@"6.0" => { |
| 70 | + mod.linkSystemLibrary("gtk-4", .{}); |
| 71 | + mod.linkSystemLibrary("webkitgtk-6.0", .{}); |
| 72 | + }, |
| 73 | + } |
| 74 | + }, |
| 75 | + else => unreachable, |
| 76 | + } |
| 77 | + const lib = b.addLibrary(.{ |
| 78 | + .name = "webview", |
| 79 | + .root_module = mod, |
| 80 | + .linkage = .static, |
| 81 | + }); |
| 82 | + b.installArtifact(lib); |
| 83 | + return lib; |
| 84 | +} |
| 85 | + |
| 86 | +fn addModule(b: *std.Build, options: BuildOptions, lib: *std.Build.Step.Compile) *std.Build.Module { |
| 87 | + const webview_c = createCModule(b, options); |
| 88 | + const mod = b.addModule("webview", .{ |
| 89 | + .root_source_file = b.path("src/root.zig"), |
| 90 | + .target = options.target, |
| 91 | + .optimize = options.optimize, |
| 92 | + .imports = &.{ |
| 93 | + .{ .name = "webview_c", .module = webview_c }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + mod.linkLibrary(lib); |
| 97 | + return mod; |
| 98 | +} |
| 99 | + |
| 100 | +fn addTestStep(b: *std.Build, mod: *std.Build.Module) void { |
| 101 | + const test_step = b.step("test", "Run tests"); |
| 102 | + const mod_test = b.addTest(.{ .root_module = mod }); |
| 103 | + const run_mod_test = b.addRunArtifact(mod_test); |
| 104 | + test_step.dependOn(&run_mod_test.step); |
| 105 | +} |
| 106 | + |
| 107 | +fn addExamplesStep(b: *std.Build, options: BuildOptions, mod: *std.Build.Module) void { |
| 108 | + const examples_step = b.step("examples", "Build all examples"); |
| 109 | + const examples = [_][]const u8{ |
| 110 | + "basic", |
| 111 | + "bind", |
| 112 | + }; |
| 113 | + inline for (examples) |name| { |
| 114 | + const example_mod = b.createModule(.{ |
| 115 | + .root_source_file = b.path(b.fmt("examples/{s}.zig", .{name})), |
| 116 | + .target = options.target, |
| 117 | + .optimize = options.optimize, |
| 118 | + .imports = &.{ |
| 119 | + .{ .name = "webview", .module = mod }, |
| 120 | + }, |
| 121 | + }); |
| 122 | + if (options.target.result.os.tag == .macos) { |
| 123 | + tryApplyMacOsSdk(b, example_mod, options); |
| 124 | + } |
| 125 | + const exe = b.addExecutable(.{ |
| 126 | + .name = name, |
| 127 | + .root_module = example_mod, |
| 128 | + }); |
| 129 | + const install = b.addInstallArtifact(exe, .{}); |
| 130 | + examples_step.dependOn(&install.step); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +fn addDocStep(b: *std.Build, mod: *std.Build.Module) void { |
| 135 | + const doc_step = b.step("doc", "Generate documentation"); |
| 136 | + const doc_obj = b.addObject(.{ |
| 137 | + .name = "webview", |
| 138 | + .root_module = mod, |
| 139 | + }); |
| 140 | + const install_doc = b.addInstallDirectory(.{ |
| 141 | + .source_dir = doc_obj.getEmittedDocs(), |
| 142 | + .install_dir = .prefix, |
| 143 | + .install_subdir = "doc", |
| 144 | + }); |
| 145 | + doc_step.dependOn(&install_doc.step); |
| 146 | +} |
| 147 | + |
| 148 | +fn createCModule(b: *std.Build, options: BuildOptions) *std.Build.Module { |
| 149 | + const upstream = b.dependency("webview", .{}); |
| 150 | + const c_mod = b.addTranslateC(.{ |
| 151 | + .root_source_file = upstream.path("core/include/webview.h"), |
| 152 | + .target = options.target, |
| 153 | + .optimize = options.optimize, |
| 154 | + }).createModule(); |
| 155 | + c_mod.addIncludePath(upstream.path("core/include")); |
| 156 | + c_mod.addCMacro("WEBVIEW_STATIC", "1"); |
| 157 | + return c_mod; |
| 158 | +} |
| 159 | + |
| 160 | +fn tryApplyMacOsSdk(b: *std.Build, mod: *std.Build.Module, options: BuildOptions) void { |
| 161 | + if (builtin.os.tag != .macos and options.macos_sdk != null) { |
| 162 | + const macos_sdk_path: std.Build.LazyPath = .{ .cwd_relative = options.macos_sdk.? }; |
| 163 | + mod.addSystemIncludePath(macos_sdk_path.path(b, "usr/include")); |
| 164 | + mod.addLibraryPath(macos_sdk_path.path(b, "usr/lib")); |
| 165 | + mod.addSystemFrameworkPath(macos_sdk_path.path(b, "System/Library/Frameworks")); |
| 166 | + } |
| 167 | +} |
0 commit comments