@@ -28,6 +28,9 @@ pub fn build(b: *std.Build) !void {
28
28
plugin .linkLibrary (lib_godot_cpp );
29
29
30
30
b .installArtifact (plugin );
31
+
32
+ const check = b .step ("check" , "Check if plugin compiles" );
33
+ check .dependOn (& plugin .step );
31
34
}
32
35
33
36
const BuildParams = struct {
@@ -76,29 +79,38 @@ fn build_lib_llama_cpp(params: BuildParams) !*std.Build.Step.Compile {
76
79
const optimize = params .optimize ;
77
80
const zig_triple = try target .result .zigTriple (b .allocator );
78
81
79
- const commit_hash = try std .process .Child .run (.{ .allocator = b .allocator , .argv = &.{ "git" , "rev-parse" , "HEAD" }, .cwd = b .pathFromRoot ("llama.cpp" ) });
80
- const zig_version = builtin .zig_version_string ;
81
- try b .build_root .handle .writeFile (.{ .sub_path = "llama.cpp/common/build-info.cpp" , .data = b .fmt (
82
- \\int LLAMA_BUILD_NUMBER = {};
83
- \\char const *LLAMA_COMMIT = "{s}";
84
- \\char const *LLAMA_COMPILER = "Zig {s}";
85
- \\char const *LLAMA_BUILD_TARGET = "{s}";
86
- , .{ 0 , commit_hash .stdout [0 .. commit_hash .stdout .len - 1 ], zig_version , zig_triple }) });
82
+ const lib_llama_cpp = b .addStaticLibrary (.{
83
+ .name = "llama.cpp" ,
84
+ .target = target ,
85
+ .optimize = optimize ,
86
+ });
87
87
88
- const lib_llama_cpp = b .addStaticLibrary (.{ .name = "llama.cpp" , .target = target , .optimize = optimize });
88
+ const build_info_run = b .addSystemCommand (&.{
89
+ "echo" ,
90
+ "-e" ,
91
+ b .fmt (
92
+ "int LLAMA_BUILD_NUMBER = {d};\\ nchar const *LLAMA_COMMIT = \" $(git rev-parse HEAD)\" ;\\ nchar const *LLAMA_COMPILER = \" Zig {s}\" ;\\ nchar const *LLAMA_BUILD_TARGET = \" {s}\" ;\\ n" ,
93
+ .{ 0 , builtin .zig_version_string , zig_triple },
94
+ ),
95
+ });
96
+ const build_info_wf = b .addWriteFiles ();
97
+ _ = build_info_wf .addCopyFile (build_info_run .captureStdOut (), "build-info.cpp" );
89
98
90
- var objs = std .ArrayList (* std .Build .Step .Compile ).init (b .allocator );
91
99
var objBuilder = ObjBuilder .init (.{
92
100
.b = b ,
93
101
.target = target ,
94
102
.optimize = optimize ,
95
103
.include_paths = &.{ "llama.cpp" , "llama.cpp/common" },
96
104
});
105
+ try objBuilder .c_flags .append ("-std=c11" );
106
+ try objBuilder .cpp_flags .append ("-std=c++11" );
107
+
108
+ var objs = std .ArrayList (* std .Build .Step .Compile ).init (b .allocator );
97
109
98
110
switch (target .result .os .tag ) {
99
111
.macos = > {
100
- try objBuilder .flags .append ("-DGGML_USE_METAL" );
101
- try objs .append (objBuilder .build (.{ .name = "ggml_metal" , .sources = &.{ "llama.cpp/ggml-metal.m" } }));
112
+ try objBuilder .base_flags .append ("-DGGML_USE_METAL" );
113
+ try objs .append (try objBuilder .build (.{ .name = "ggml_metal" , .source = "llama.cpp/ggml-metal.m" }));
102
114
103
115
lib_llama_cpp .linkFramework ("Foundation" );
104
116
lib_llama_cpp .linkFramework ("Metal" );
@@ -119,24 +131,34 @@ fn build_lib_llama_cpp(params: BuildParams) !*std.Build.Step.Compile {
119
131
const install_metal = b .addInstallFileWithDir (metal_expanded , .lib , "ggml-metal.metal" );
120
132
lib_llama_cpp .step .dependOn (& install_metal .step );
121
133
},
134
+ .linux = > {
135
+ try objBuilder .base_flags .append ("-D_GNU_SOURCE" );
136
+ },
122
137
else = > {},
123
138
}
124
139
140
+ const build_info_compile = try objBuilder .build (.{
141
+ .name = "build_info" ,
142
+ .source = "build-info.cpp" ,
143
+ .root = build_info_wf .getDirectory (),
144
+ });
145
+ build_info_compile .step .dependOn (& build_info_wf .step );
146
+
125
147
try objs .appendSlice (&.{
126
- objBuilder .build (.{ .name = "ggml" , .sources = &.{ "llama.cpp/ggml.c" } }),
127
- objBuilder .build (.{ .name = "sgemm" , .sources = &.{ "llama.cpp/sgemm.cpp" } }),
128
- objBuilder .build (.{ .name = "ggml_alloc" , .sources = &.{ "llama.cpp/ggml-alloc.c" } }),
129
- objBuilder .build (.{ .name = "ggml_backend" , .sources = &.{ "llama.cpp/ggml-backend.c" } }),
130
- objBuilder .build (.{ .name = "ggml_quants" , .sources = &.{ "llama.cpp/ggml-quants.c" } }),
131
- objBuilder .build (.{ .name = "llama" , .sources = &.{ "llama.cpp/llama.cpp" } }),
132
- objBuilder .build (.{ .name = "unicode" , .sources = &.{ "llama.cpp/unicode.cpp" } }),
133
- objBuilder .build (.{ .name = "unicode_data" , .sources = &.{ "llama.cpp/unicode-data.cpp" } }),
134
- objBuilder .build (.{ .name = "common" , .sources = &.{ "llama.cpp/common/common.cpp" } }),
135
- objBuilder .build (.{ .name = "console" , .sources = &.{ "llama.cpp/common/console.cpp" } }),
136
- objBuilder .build (.{ .name = "sampling" , .sources = &.{ "llama.cpp/common/sampling.cpp" } }),
137
- objBuilder .build (.{ .name = "grammar_parser" , .sources = &.{ "llama.cpp/common/grammar-parser.cpp" } }),
138
- objBuilder .build (.{ .name = "json_schema_to_grammar" , .sources = &.{ "llama.cpp/common/json-schema-to-grammar.cpp" } }),
139
- objBuilder . build (.{ . name = "build_info" , . sources = &.{ "llama.cpp/common/build-info.cpp" } }) ,
148
+ try objBuilder .build (.{ .name = "ggml" , .source = "llama.cpp/ggml.c" }),
149
+ try objBuilder .build (.{ .name = "sgemm" , .source = "llama.cpp/sgemm.cpp" }),
150
+ try objBuilder .build (.{ .name = "ggml_alloc" , .source = "llama.cpp/ggml-alloc.c" }),
151
+ try objBuilder .build (.{ .name = "ggml_backend" , .source = "llama.cpp/ggml-backend.c" }),
152
+ try objBuilder .build (.{ .name = "ggml_quants" , .source = "llama.cpp/ggml-quants.c" }),
153
+ try objBuilder .build (.{ .name = "llama" , .source = "llama.cpp/llama.cpp" }),
154
+ try objBuilder .build (.{ .name = "unicode" , .source = "llama.cpp/unicode.cpp" }),
155
+ try objBuilder .build (.{ .name = "unicode_data" , .source = "llama.cpp/unicode-data.cpp" }),
156
+ try objBuilder .build (.{ .name = "common" , .source = "llama.cpp/common/common.cpp" }),
157
+ try objBuilder .build (.{ .name = "console" , .source = "llama.cpp/common/console.cpp" }),
158
+ try objBuilder .build (.{ .name = "sampling" , .source = "llama.cpp/common/sampling.cpp" }),
159
+ try objBuilder .build (.{ .name = "grammar_parser" , .source = "llama.cpp/common/grammar-parser.cpp" }),
160
+ try objBuilder .build (.{ .name = "json_schema_to_grammar" , .source = "llama.cpp/common/json-schema-to-grammar.cpp" }),
161
+ build_info_compile ,
140
162
});
141
163
142
164
for (objs .items ) | obj | {
@@ -151,7 +173,9 @@ const ObjBuilder = struct {
151
173
target : std.Build.ResolvedTarget ,
152
174
optimize : std.builtin.OptimizeMode ,
153
175
include_paths : []const []const u8 ,
154
- flags : std .ArrayList ([]const u8 ),
176
+ c_flags : std .ArrayList ([]const u8 ),
177
+ cpp_flags : std .ArrayList ([]const u8 ),
178
+ base_flags : std .ArrayList ([]const u8 ),
155
179
156
180
fn init (params : struct {
157
181
b : * std.Build ,
@@ -164,18 +188,57 @@ const ObjBuilder = struct {
164
188
.target = params .target ,
165
189
.optimize = params .optimize ,
166
190
.include_paths = params .include_paths ,
167
- .flags = std .ArrayList ([]const u8 ).init (params .b .allocator ),
191
+ .c_flags = std .ArrayList ([]const u8 ).init (params .b .allocator ),
192
+ .cpp_flags = std .ArrayList ([]const u8 ).init (params .b .allocator ),
193
+ .base_flags = std .ArrayList ([]const u8 ).init (params .b .allocator ),
168
194
};
169
195
}
170
196
171
- fn build (self : * ObjBuilder , params : struct { name : []const u8 , sources : []const []const u8 }) * std.Build.Step.Compile {
172
- const obj = self .b .addObject (.{ .name = params .name , .target = self .target , .optimize = self .optimize });
173
- obj .addCSourceFiles (.{ .files = params .sources , .flags = self .flags .items });
197
+ fn build (
198
+ self : * ObjBuilder ,
199
+ params : struct {
200
+ name : []const u8 ,
201
+ source : []const u8 ,
202
+ root : ? std.Build.LazyPath = null ,
203
+ },
204
+ ) ! * std.Build.Step.Compile {
205
+ const obj = self .b .addObject (.{
206
+ .name = params .name ,
207
+ .target = self .target ,
208
+ .optimize = self .optimize ,
209
+ .pic = true ,
210
+ });
211
+
212
+ const Extension = enum {
213
+ c ,
214
+ cpp ,
215
+ m ,
216
+ };
217
+ const extension = std .meta .stringToEnum (
218
+ Extension ,
219
+ std .mem .trimLeft (u8 , std .fs .path .extension (params .source ), "." ),
220
+ ) orelse return error .UnknownExtension ;
221
+
222
+ const flags = switch (extension ) {
223
+ .c , .m = > self .c_flags ,
224
+ .cpp = > self .cpp_flags ,
225
+ };
226
+ try self .base_flags .appendSlice (flags .items );
227
+
228
+ obj .addCSourceFiles (.{
229
+ .files = &.{params .source },
230
+ .flags = self .base_flags .items ,
231
+ .root = params .root ,
232
+ });
174
233
for (self .include_paths ) | path | {
175
234
obj .addIncludePath (.{ .src_path = .{ .owner = self .b , .sub_path = path } });
176
235
}
177
- obj .linkLibC ();
178
- obj .linkLibCpp ();
236
+
237
+ switch (extension ) {
238
+ .c , .m = > obj .linkLibC (),
239
+ .cpp = > obj .linkLibCpp (),
240
+ }
241
+
179
242
return obj ;
180
243
}
181
244
};
0 commit comments