Skip to content

Commit 788ad07

Browse files
committed
refactor build to separate dep libs
1 parent ab2d22f commit 788ad07

File tree

5 files changed

+87
-36
lines changed

5 files changed

+87
-36
lines changed

.cspell.jsonc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"version": "0.2",
33
"import": [
4-
".cspell/cesium/cspell-ext.json"
4+
".cspell/cesium/cspell-ext.json",
5+
".cspell/zig/cspell-ext.json",
56
],
67
"ignorePaths": [
78
".cspell.jsonc",
@@ -23,10 +24,11 @@
2324
"scientific-terms-us",
2425
"cesium",
2526
"project-words",
27+
"zig",
2628
],
2729
"words": [],
2830
"ignoreWords": [],
2931
"ignoreRegExpList": [
30-
"/-W.*/",
32+
"/\"-W.*\"/",
3133
]
3234
}

.cspell/zig

Submodule zig added at 5b6fc35

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "thirdparty/tree-sitter"]
88
path = thirdparty/tree-sitter
99
url = https://github.com/tree-sitter/tree-sitter.git
10+
[submodule ".cspell/cspell-zig"]
11+
path = .cspell/zig
12+
url = https://github.com/emanspeaks/cspell-zig.git

build.zig

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
const std = @import("std");
22

3-
fn setupYyJsonDependency(b: *std.Build, exe: *std.Build.Step.Compile) void {
3+
fn createYyjsonLib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
44
const yyjson_dep = b.dependency("yyjson", .{});
55

6-
exe.addCSourceFiles(.{
6+
const lib = b.addLibrary(.{
7+
.name = "yyjson",
8+
.root_module = b.createModule(.{
9+
.target = target,
10+
.optimize = optimize,
11+
}),
12+
});
13+
14+
lib.addCSourceFiles(.{
715
.root = yyjson_dep.path(""),
816
.files = &[_][]const u8{
917
"src/yyjson.c",
@@ -13,46 +21,73 @@ fn setupYyJsonDependency(b: *std.Build, exe: *std.Build.Step.Compile) void {
1321
},
1422
});
1523

16-
exe.addIncludePath(yyjson_dep.path("src"));
24+
lib.addIncludePath(yyjson_dep.path("src"));
25+
lib.linkLibC();
26+
27+
return lib;
1728
}
1829

19-
fn setupTreeSitterCppDependency(b: *std.Build, exe: *std.Build.Step.Compile) void {
30+
fn createTreeSitterCppLib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
2031
const tree_sitter_cpp_dep = b.dependency("tree_sitter_cpp", .{});
2132

22-
exe.addCSourceFiles(.{
33+
const lib = b.addLibrary(.{
34+
.name = "tree-sitter-cpp",
35+
.root_module = b.createModule(.{
36+
.target = target,
37+
.optimize = optimize,
38+
}),
39+
});
40+
41+
lib.addCSourceFiles(.{
2342
.root = tree_sitter_cpp_dep.path(""),
2443
.files = &[_][]const u8{
2544
"src/parser.c",
26-
"src/scanner.c",
45+
"src/scanner.c",
2746
},
2847
.flags = &[_][]const u8{
29-
"-std=c11", // Use C11 for static_assert support
48+
"-std=c11", // Use C11 for static_assert support
3049
},
3150
});
51+
52+
lib.linkLibC();
53+
54+
return lib;
3255
}
3356

34-
fn setupTreeSitterCoreDependency(b: *std.Build, exe: *std.Build.Step.Compile) void {
57+
fn createTreeSitterCoreLib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
58+
const lib = b.addLibrary(.{
59+
.name = "tree-sitter-core",
60+
.root_module = b.createModule(.{
61+
.target = target,
62+
.optimize = optimize,
63+
}),
64+
});
65+
3566
// Add tree-sitter core from git submodule
36-
exe.addCSourceFiles(.{
67+
lib.addCSourceFiles(.{
3768
.root = b.path("."),
3869
.files = &[_][]const u8{
39-
"thirdparty/tree-sitter/lib/src/lib.c", // Amalgamated source
70+
"thirdparty/tree-sitter/lib/src/lib.c", // Amalgamated source
4071
},
4172
.flags = &[_][]const u8{
4273
"-std=c11",
4374
},
4475
});
45-
76+
4677
// Add tree-sitter include paths
47-
exe.addIncludePath(b.path("thirdparty/tree-sitter/lib/include"));
48-
exe.addIncludePath(b.path("thirdparty/tree-sitter/lib/src"));
49-
78+
lib.addIncludePath(b.path("thirdparty/tree-sitter/lib/include"));
79+
lib.addIncludePath(b.path("thirdparty/tree-sitter/lib/src"));
80+
5081
// Add tree-sitter macros
51-
exe.root_module.addCMacro("_POSIX_C_SOURCE", "200112L");
52-
exe.root_module.addCMacro("_DEFAULT_SOURCE", "");
82+
lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112L");
83+
lib.root_module.addCMacro("_DEFAULT_SOURCE", "");
84+
85+
lib.linkLibC();
86+
87+
return lib;
5388
}
5489

55-
fn configureExecutable(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
90+
fn createCesiumExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
5691
const exe = b.addExecutable(.{
5792
.name = "cesium",
5893
.root_module = b.createModule(.{
@@ -67,11 +102,11 @@ fn configureExecutable(b: *std.Build, target: std.Build.ResolvedTarget, optimize
67102
"cesium/src/main.cpp",
68103
},
69104
.flags = &[_][]const u8{
70-
"-std=c++20", // CMAKE_CXX_STANDARD 20
71-
"-Wall", // target_compile_options
105+
"-std=c++20", // CMAKE_CXX_STANDARD 20
106+
"-Wall", // target_compile_options
72107
"-Wextra",
73-
"-Wpedantic",
74-
"-Wno-unused-parameter", // Clang-specific flag from CMake
108+
"-Wpedantic",
109+
"-Wno-unused-parameter", // Clang-specific flag from CMake
75110
},
76111
});
77112

@@ -84,7 +119,13 @@ fn configureExecutable(b: *std.Build, target: std.Build.ResolvedTarget, optimize
84119

85120
fn setupBuildSteps(b: *std.Build, exe: *std.Build.Step.Compile) void {
86121
// Install the executable
87-
b.installArtifact(exe);
122+
// b.installArtifact(exe);
123+
const install = b.addInstallArtifact(exe, .{
124+
.dest_dir = .{ .override = .{ .custom = "../build/bin" } },
125+
.pdb_dir = .{ .override = .{ .custom = "../build/bin" } },
126+
.h_dir = .{ .override = .{ .custom = "../build/include" } },
127+
});
128+
b.default_step.dependOn(&install.step);
88129

89130
// Create a run step
90131
const run_cmd = b.addRunArtifact(exe);
@@ -109,14 +150,18 @@ pub fn build(b: *std.Build) void {
109150
const target = b.standardTargetOptions(.{});
110151
const optimize = b.standardOptimizeOption(.{});
111152

112-
// Configure the main executable
113-
const exe = configureExecutable(b, target, optimize);
153+
const exe = createCesiumExe(b, target, optimize);
114154

115-
// Setup all dependencies
116-
setupYyJsonDependency(b, exe);
117-
setupTreeSitterCppDependency(b, exe);
118-
setupTreeSitterCoreDependency(b, exe);
155+
const yyjson_lib = createYyjsonLib(b, target, optimize);
156+
exe.linkLibrary(yyjson_lib);
157+
exe.addIncludePath(b.dependency("yyjson", .{}).path("src"));
158+
159+
const tree_sitter_core_lib = createTreeSitterCoreLib(b, target, optimize);
160+
exe.linkLibrary(tree_sitter_core_lib);
161+
exe.addIncludePath(b.path("thirdparty/tree-sitter/lib/include"));
162+
163+
const tree_sitter_cpp_lib = createTreeSitterCppLib(b, target, optimize);
164+
exe.linkLibrary(tree_sitter_cpp_lib);
119165

120-
// Setup build steps
121166
setupBuildSteps(b, exe);
122-
}
167+
}

build.zig.zon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// original project's identity. Thus it is recommended to leave the comment
2323
// on the following line intact, so that it shows up in code reviews that
2424
// modify the field.
25-
.fingerprint = 0x31b74be1c2c745f5, // Changing this has security and trust implications.
25+
.fingerprint = 0x31b74be1568d57fb, // Changing this has security and trust implications.
2626
// Tracks the earliest Zig version that the package considers to be a
2727
// supported use case.
2828
.minimum_zig_version = "0.15.1",
@@ -53,8 +53,8 @@
5353
"build.zig",
5454
"build.zig.zon",
5555
"cesium",
56-
// For example...
57-
//"LICENSE",
58-
//"README.md",
56+
"thirdparty",
57+
"LICENSE",
58+
"readme.md",
5959
},
6060
}

0 commit comments

Comments
 (0)