Skip to content

Commit b006101

Browse files
committed
lib-vt: boilerplate to build a shared object
1 parent f97518c commit b006101

File tree

7 files changed

+116
-1
lines changed

7 files changed

+116
-1
lines changed

build.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn build(b: *std.Build) !void {
3131

3232
// All our steps which we'll hook up later. The steps are shown
3333
// up here just so that they are more self-documenting.
34+
const libvt_step = b.step("lib-vt", "Build libghostty-vt");
3435
const run_step = b.step("run", "Run the app");
3536
const run_valgrind_step = b.step(
3637
"run-valgrind",
@@ -86,7 +87,7 @@ pub fn build(b: *std.Build) !void {
8687
check_step.dependOn(dist.install_step);
8788
}
8889

89-
// libghostty
90+
// libghostty (internal, big)
9091
const libghostty_shared = try buildpkg.GhosttyLib.initShared(
9192
b,
9293
&deps,
@@ -96,6 +97,15 @@ pub fn build(b: *std.Build) !void {
9697
&deps,
9798
);
9899

100+
// libghostty-vt
101+
const libghostty_vt_shared = try buildpkg.GhosttyLibVt.initShared(
102+
b,
103+
&mod,
104+
&deps,
105+
);
106+
libghostty_vt_shared.install(libvt_step, "libghostty-vt.so");
107+
libghostty_vt_shared.installHeader(libvt_step);
108+
99109
// Helpgen
100110
if (config.emit_helpgen) deps.help_strings.install();
101111

include/ghostty-vt.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// libghostty-vt
2+
3+
#ifndef GHOSTTY_VT_H
4+
#define GHOSTTY_VT_H
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#ifdef __cplusplus
11+
}
12+
#endif
13+
14+
#endif /* GHOSTTY_VT_H */

src/build/GhosttyLibVt.zig

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const GhosttyLibVt = @This();
2+
3+
const std = @import("std");
4+
const RunStep = std.Build.Step.Run;
5+
const Config = @import("Config.zig");
6+
const GhosttyZig = @import("GhosttyZig.zig");
7+
const SharedDeps = @import("SharedDeps.zig");
8+
const LibtoolStep = @import("LibtoolStep.zig");
9+
const LipoStep = @import("LipoStep.zig");
10+
11+
/// The step that generates the file.
12+
step: *std.Build.Step,
13+
14+
/// The final library file
15+
output: std.Build.LazyPath,
16+
dsym: ?std.Build.LazyPath,
17+
18+
pub fn initShared(
19+
b: *std.Build,
20+
zig: *const GhosttyZig,
21+
deps: *const SharedDeps,
22+
) !GhosttyLibVt {
23+
const lib = b.addSharedLibrary(.{
24+
.name = "ghostty-vt",
25+
.root_module = zig.vt,
26+
});
27+
28+
// Get our debug symbols
29+
const dsymutil: ?std.Build.LazyPath = dsymutil: {
30+
if (!deps.config.target.result.os.tag.isDarwin()) {
31+
break :dsymutil null;
32+
}
33+
34+
const dsymutil = RunStep.create(b, "dsymutil");
35+
dsymutil.addArgs(&.{"dsymutil"});
36+
dsymutil.addFileArg(lib.getEmittedBin());
37+
dsymutil.addArgs(&.{"-o"});
38+
const output = dsymutil.addOutputFileArg("libghostty-vt.dSYM");
39+
break :dsymutil output;
40+
};
41+
42+
return .{
43+
.step = &lib.step,
44+
.output = lib.getEmittedBin(),
45+
.dsym = dsymutil,
46+
};
47+
}
48+
49+
pub fn install(
50+
self: *const GhosttyLibVt,
51+
step: *std.Build.Step,
52+
name: []const u8,
53+
) void {
54+
const b = self.step.owner;
55+
const lib_install = b.addInstallLibFile(
56+
self.output,
57+
name,
58+
);
59+
step.dependOn(&lib_install.step);
60+
}
61+
62+
pub fn installHeader(
63+
self: *const GhosttyLibVt,
64+
step: *std.Build.Step,
65+
) void {
66+
const b = self.step.owner;
67+
const header_install = b.addInstallHeaderFile(
68+
b.path("include/ghostty-vt.h"),
69+
"ghostty-vt.h",
70+
);
71+
step.dependOn(&header_install.step);
72+
}

src/build/main.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub const GhosttyDocs = @import("GhosttyDocs.zig");
1313
pub const GhosttyExe = @import("GhosttyExe.zig");
1414
pub const GhosttyFrameData = @import("GhosttyFrameData.zig");
1515
pub const GhosttyLib = @import("GhosttyLib.zig");
16+
pub const GhosttyLibVt = @import("GhosttyLibVt.zig");
1617
pub const GhosttyResources = @import("GhosttyResources.zig");
1718
pub const GhosttyI18n = @import("GhosttyI18n.zig");
1819
pub const GhosttyXcodebuild = @import("GhosttyXcodebuild.zig");

src/lib_vt.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ pub const EraseLine = terminal.EraseLine;
6565
pub const TabClear = terminal.TabClear;
6666
pub const Attribute = terminal.Attribute;
6767

68+
comptime {
69+
if (terminal.is_c_lib) {
70+
_ = terminal.c_api;
71+
}
72+
}
73+
6874
test {
6975
_ = terminal;
7076
}

src/terminal/c_api.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub export fn ghostty_hi() void {
2+
// Does nothing, but you can see this symbol exists:
3+
// nm -D --defined-only zig-out/lib/libghostty-vt.so | rg ' T '
4+
// This is temporary as we figure out the API.
5+
}

src/terminal/main.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ pub const Attribute = sgr.Attribute;
6262

6363
pub const isSafePaste = sanitize.isSafePaste;
6464

65+
/// This is set to true when we're building the C library.
66+
pub const is_c_lib = @import("root") == @import("../lib_vt.zig");
67+
68+
/// This is the C API for this package. Do NOT reference this unless
69+
/// you want a bunch of symbols exported into your final artifact.
70+
pub const c_api = @import("c_api.zig");
71+
6572
test {
6673
@import("std").testing.refAllDecls(@This());
6774

0 commit comments

Comments
 (0)