Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
184 lines (163 loc) · 7.11 KB
/
Copy pathbuild.zig
File metadata and controls
184 lines (163 loc) · 7.11 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
176
177
178
179
180
181
182
183
184
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const target_os = target.result.os.tag;
// Shared module for clipboard core logic
const clipboard_mod = b.createModule(.{
.root_source_file = b.path("src/clipboard.zig"),
.target = target,
.optimize = optimize,
});
// When cross-compiling for macOS (e.g. -Dtarget=x86_64-macos on an aarch64
// host), Zig doesn't auto-discover the SDK paths. Pass -Dmacos-sdk=/path/to/sdk
// to provide them.
const is_native = target.query.isNativeOs() and target.query.isNativeCpu();
if (!is_native and target_os == .macos) {
const macos_sdk = b.option([]const u8, "macos-sdk", "Path to macOS SDK for cross-compilation");
if (macos_sdk) |sdk| {
clipboard_mod.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/usr/lib", .{sdk}) });
clipboard_mod.addFrameworkPath(.{ .cwd_relative = b.fmt("{s}/System/Library/Frameworks", .{sdk}) });
}
}
switch (target_os) {
.macos => {
clipboard_mod.linkSystemLibrary("objc", .{});
clipboard_mod.linkFramework("AppKit", .{});
},
.linux => {
clipboard_mod.link_libc = true;
// When cross-compiling (e.g. pinning glibc version with
// -Dtarget=x86_64-linux-gnu.2.31), Zig won't search the
// default system library paths. Add them explicitly so
// that X11 and Wayland .so files are found.
if (!is_native) {
clipboard_mod.addLibraryPath(.{ .cwd_relative = "/usr/lib/x86_64-linux-gnu" });
clipboard_mod.addLibraryPath(.{ .cwd_relative = "/usr/lib/aarch64-linux-gnu" });
clipboard_mod.addIncludePath(.{ .cwd_relative = "/usr/include" });
}
clipboard_mod.linkSystemLibrary("X11", .{});
clipboard_mod.linkSystemLibrary("wayland-client", .{});
// Generate the wlr-data-control client header and private code
// from the vendored XML via wayland-scanner (which is required
// to be on PATH on Linux builds; it ships in the libwayland-bin
// package on Debian/Ubuntu).
const wl_scanner_header = b.addSystemCommand(&.{ "wayland-scanner", "client-header" });
wl_scanner_header.addFileArg(b.path("vendor/wayland-protocols/wlr-data-control-unstable-v1.xml"));
const wl_header = wl_scanner_header.addOutputFileArg("wlr-data-control-unstable-v1-client-protocol.h");
const wl_scanner_code = b.addSystemCommand(&.{ "wayland-scanner", "private-code" });
wl_scanner_code.addFileArg(b.path("vendor/wayland-protocols/wlr-data-control-unstable-v1.xml"));
const wl_code = wl_scanner_code.addOutputFileArg("wlr-data-control-unstable-v1-protocol.c");
clipboard_mod.addCSourceFile(.{ .file = wl_code, .flags = &.{} });
clipboard_mod.addIncludePath(wl_header.dirname());
},
.windows => {
clipboard_mod.link_libc = true;
clipboard_mod.linkSystemLibrary("kernel32", .{});
clipboard_mod.linkSystemLibrary("user32", .{});
},
else => {
// Other platforms are not supported by the library yet; the
// clipboard.zig @compileError still enforces that at the Zig
// level. build.zig stays silent so `zig build --help` still works.
},
}
// Shared library (C ABI for Bun FFI and other consumers)
const lib = b.addLibrary(.{
.name = "copycat",
.linkage = .dynamic,
.root_module = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "clipboard", .module = clipboard_mod },
},
}),
});
b.installArtifact(lib);
// Static library for embedding (e.g. Tauri/Rust)
const lib_static = b.addLibrary(.{
.name = "copycat",
.linkage = .static,
.root_module = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "clipboard", .module = clipboard_mod },
},
}),
});
b.installArtifact(lib_static);
// Shared module for web custom data parsing (pure Zig, no OS deps)
const web_custom_data_mod = b.createModule(.{
.root_source_file = b.path("src/web_custom_data.zig"),
.target = target,
.optimize = optimize,
});
// OSC 52 escape sequence formatting (pure Zig, no OS deps)
const osc52_mod = b.createModule(.{
.root_source_file = b.path("src/osc52.zig"),
.target = target,
.optimize = optimize,
});
// CLI executable
const exe = b.addExecutable(.{
.name = "copycat",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "clipboard", .module = clipboard_mod },
.{ .name = "web_custom_data", .module = web_custom_data_mod },
.{ .name = "osc52", .module = osc52_mod },
},
}),
});
b.installArtifact(exe);
// Run step for CLI
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the copycat CLI");
run_step.dependOn(&run_cmd.step);
// -------------------------------------------------------------------
// Unit tests for pure Zig modules (no OS dependencies).
// Run with: `zig build test`
//
// Only paths.zig is in this step. platform/* files link to platform
// libraries that are not present in the pure-Zig test binary.
// -------------------------------------------------------------------
const paths_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/paths.zig"),
.target = target,
.optimize = optimize,
}),
});
const web_custom_data_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/web_custom_data.zig"),
.target = target,
.optimize = optimize,
}),
});
const osc52_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/osc52.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_paths_tests = b.addRunArtifact(paths_tests);
const run_web_custom_data_tests = b.addRunArtifact(web_custom_data_tests);
const run_osc52_tests = b.addRunArtifact(osc52_tests);
const test_step = b.step("test", "Run pure-Zig unit tests");
test_step.dependOn(&run_paths_tests.step);
test_step.dependOn(&run_web_custom_data_tests.step);
test_step.dependOn(&run_osc52_tests.step);
}