-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
125 lines (105 loc) · 4.6 KB
/
build.zig
File metadata and controls
125 lines (105 loc) · 4.6 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
const std = @import("std");
pub fn getBuildMode(b: *std.build.Builder, default: std.builtin.Mode) !std.builtin.Mode {
const description = try std.mem.join(b.allocator, "", &.{
"What mode the project should build in (default: ", @tagName(default), ")",
});
const mode = b.option(std.builtin.Mode, "mode", description) orelse default;
return mode;
}
pub fn build(b: *std.build.Builder) !void {
const target = b.standardTargetOptions(.{});
const optimize = try getBuildMode(b, .ReleaseSmall);
const include_x11_backend = b.option(bool, "x11-backend", "Compile bento with X11 support") orelse true;
const include_wayland_backend = b.option(bool, "wayland-backend", "Compile bento with Wayland support") orelse true;
const build_options = b.addOptions();
build_options.addOption(bool, "x11_backend", include_x11_backend);
build_options.addOption(bool, "wayland_backend", include_wayland_backend);
const bento = b.addExecutable(.{
.name = "bento",
.root_source_file = .{ .path = "src/main.zig" },
.optimize = optimize,
.target = target,
});
bento.addOptions("build_options", build_options);
bento.addModule("accord", b.dependency("accord", .{}).module("accord"));
bento.install();
bento.linkLibC();
if (include_x11_backend) {
bento.linkSystemLibrary("xcb");
bento.linkSystemLibrary("xcb-shape");
bento.linkSystemLibrary("xcb-cursor");
}
if (include_wayland_backend) {
bento.linkSystemLibrary("wayland-client");
bento.linkSystemLibrary("wayland-protocols");
bento.linkSystemLibrary("wayland-cursor");
bento.linkSystemLibrary("xkbcommon");
const project_protocol_dir = try std.fs.cwd().openIterableDir("protocol", .{});
const project_protocol_path = try project_protocol_dir.dir.realpathAlloc(b.allocator, ".");
bento.addIncludePath(project_protocol_path);
const system_protocol_path = std.mem.trim(
u8,
try b.exec(&.{ "pkg-config", "--variable=pkgdatadir", "wayland-protocols" }),
"\n",
);
const wayland_scanner = std.mem.trim(
u8,
try b.exec(&.{ "pkg-config", "--variable=wayland_scanner", "wayland-scanner" }),
"\n",
);
bento.addCSourceFile(try runWaylandScanner(
b,
wayland_scanner,
system_protocol_path,
"stable/xdg-shell/xdg-shell",
project_protocol_path,
), &.{});
bento.addCSourceFile(try runWaylandScanner(
b,
wayland_scanner,
system_protocol_path,
"unstable/xdg-output/xdg-output-unstable-v1",
project_protocol_path,
), &.{});
var protocol_iterator = project_protocol_dir.iterate();
while (try protocol_iterator.next()) |item| {
if (item.kind == .File and std.mem.endsWith(u8, item.name, ".xml")) {
const protocol_name = item.name[0 .. item.name.len - 4];
bento.addCSourceFile(try runWaylandScanner(
b,
wayland_scanner,
project_protocol_path,
protocol_name,
project_protocol_path,
), &.{});
}
}
}
const run_cmd = bento.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run bento");
run_step.dependOn(&run_cmd.step);
}
fn runWaylandScanner(
b: *std.build.Builder,
scanner_path: []const u8,
input_base_path: []const u8,
name: []const u8,
output_base_path: []const u8,
) ![]const u8 {
var name_iterator = std.mem.split(u8, name, "/");
var base_name = name;
while (name_iterator.next()) |part| base_name = part;
const xml_name = try std.mem.concat(b.allocator, u8, &.{ name, ".xml" });
const client_header_name = try std.mem.concat(b.allocator, u8, &.{ base_name, "-client-protocol.h" });
const glue_code_name = try std.mem.concat(b.allocator, u8, &.{ base_name, "-protocol.c" });
const xml_path = try std.mem.join(b.allocator, "/", &.{ input_base_path, xml_name });
const client_header_path = try std.mem.join(b.allocator, "/", &.{ output_base_path, client_header_name });
const glue_code_path = try std.mem.join(b.allocator, "/", &.{ output_base_path, glue_code_name });
_ = try b.exec(&.{ scanner_path, "client-header", xml_path, client_header_path });
_ = try b.exec(&.{ scanner_path, "private-code", xml_path, glue_code_path });
return glue_code_path;
}