@@ -84,14 +84,44 @@ fn build_lib_llama_cpp(params: BuildParams) !*std.Build.Step.Compile {
84
84
\\char const *LLAMA_COMMIT = "{s}";
85
85
\\char const *LLAMA_COMPILER = "Zig {s}";
86
86
\\char const *LLAMA_BUILD_TARGET = "{s}";
87
- \\
88
87
, .{ 0 , commit_hash .stdout [0 .. commit_hash .stdout .len - 1 ], zig_version , zig_triple }) });
89
88
89
+ const lib_llama_cpp = b .addStaticLibrary (.{ .name = "llama.cpp" , .target = target , .optimize = optimize });
90
+
90
91
var objs = std .ArrayList (* std .Build .Step .Compile ).init (b .allocator );
91
- var objBuilder = ObjBuilder .init (.{ .b = b , .target = target , .optimize = optimize , .include_paths = &.{
92
- "llama.cpp" ,
93
- "llama.cpp/common" ,
94
- } });
92
+ var objBuilder = ObjBuilder .init (.{
93
+ .b = b ,
94
+ .target = target ,
95
+ .optimize = optimize ,
96
+ .include_paths = &.{ "llama.cpp" , "llama.cpp/common" },
97
+ });
98
+
99
+ switch (target .result .os .tag ) {
100
+ .macos = > {
101
+ try objBuilder .flags .append ("-DGGML_USE_METAL" );
102
+ try objs .append (objBuilder .build (.{ .name = "ggml_metal" , .sources = &.{"llama.cpp/ggml-metal.m" } }));
103
+
104
+ lib_llama_cpp .linkFramework ("Foundation" );
105
+ lib_llama_cpp .linkFramework ("Metal" );
106
+ lib_llama_cpp .linkFramework ("MetalKit" );
107
+
108
+ const expand_metal = b .addExecutable (.{
109
+ .name = "expand_metal" ,
110
+ .target = target ,
111
+ .root_source_file = .{ .path = "tools/expand_metal.zig" },
112
+ });
113
+ var run_expand_metal = b .addRunArtifact (expand_metal );
114
+ run_expand_metal .addArg ("--metal-file" );
115
+ run_expand_metal .addFileArg (.{ .path = "llama.cpp/ggml-metal.metal" });
116
+ run_expand_metal .addArg ("--common-file" );
117
+ run_expand_metal .addFileArg (.{ .path = "llama.cpp/ggml-common.h" });
118
+ run_expand_metal .addArg ("--output-file" );
119
+ const metal_expanded = run_expand_metal .addOutputFileArg ("ggml-metal.metal" );
120
+ const install_metal = b .addInstallFileWithDir (metal_expanded , .lib , "ggml-metal.metal" );
121
+ lib_llama_cpp .step .dependOn (& install_metal .step );
122
+ },
123
+ else = > {},
124
+ }
95
125
96
126
try objs .appendSlice (&.{
97
127
objBuilder .build (.{ .name = "ggml" , .sources = &.{"llama.cpp/ggml.c" } }),
@@ -110,8 +140,6 @@ fn build_lib_llama_cpp(params: BuildParams) !*std.Build.Step.Compile {
110
140
objBuilder .build (.{ .name = "build_info" , .sources = &.{"llama.cpp/common/build-info.cpp" } }),
111
141
});
112
142
113
- const lib_llama_cpp = b .addStaticLibrary (.{ .name = "llama.cpp" , .target = target , .optimize = optimize });
114
-
115
143
for (objs .items ) | obj | {
116
144
lib_llama_cpp .addObject (obj );
117
145
}
@@ -124,19 +152,26 @@ const ObjBuilder = struct {
124
152
target : std.Build.ResolvedTarget ,
125
153
optimize : std.builtin.OptimizeMode ,
126
154
include_paths : []const []const u8 ,
127
-
128
- fn init (params : struct { b : * std.Build , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode , include_paths : []const []const u8 }) ObjBuilder {
155
+ flags : std .ArrayList ([]const u8 ),
156
+
157
+ fn init (params : struct {
158
+ b : * std.Build ,
159
+ target : std.Build.ResolvedTarget ,
160
+ optimize : std.builtin.OptimizeMode ,
161
+ include_paths : []const []const u8 ,
162
+ }) ObjBuilder {
129
163
return ObjBuilder {
130
164
.b = params .b ,
131
165
.target = params .target ,
132
166
.optimize = params .optimize ,
133
167
.include_paths = params .include_paths ,
168
+ .flags = std .ArrayList ([]const u8 ).init (params .b .allocator ),
134
169
};
135
170
}
136
171
137
172
fn build (self : * ObjBuilder , params : struct { name : []const u8 , sources : []const []const u8 }) * std.Build.Step.Compile {
138
173
const obj = self .b .addObject (.{ .name = params .name , .target = self .target , .optimize = self .optimize });
139
- obj .addCSourceFiles (.{ .files = params .sources });
174
+ obj .addCSourceFiles (.{ .files = params .sources , . flags = self . flags . items });
140
175
for (self .include_paths ) | path | {
141
176
obj .addIncludePath (.{ .path = path });
142
177
}
0 commit comments