@@ -65,8 +65,10 @@ const Maker = struct {
65
65
if (m .target .result .abi == .gnu ) {
66
66
try m .addFlag ("-D_GNU_SOURCE" );
67
67
}
68
+ if (m .target .result .os .tag == .macos ) {
69
+ try m .addFlag ("-D_DARWIN_C_SOURCE" );
70
+ }
68
71
try m .addFlag ("-D_XOPEN_SOURCE=600" );
69
- try m .addFlag ("-DGGML_USE_VULKAN" );
70
72
71
73
try m .addProjectInclude (&.{});
72
74
try m .addProjectInclude (&.{"common" });
@@ -76,7 +78,7 @@ const Maker = struct {
76
78
fn obj (m : * const Maker , name : []const u8 , src : []const u8 ) * Compile {
77
79
const o = m .builder .addObject (.{ .name = name , .target = m .target , .optimize = m .optimize });
78
80
79
- if (std .mem .endsWith (u8 , src , ".c" )) {
81
+ if (std .mem .endsWith (u8 , src , ".c" ) or std . mem . endsWith ( u8 , src , ".m" ) ) {
80
82
o .addCSourceFiles (.{ .files = &.{src }, .flags = m .cflags .items });
81
83
o .linkLibC ();
82
84
} else {
@@ -106,7 +108,6 @@ const Maker = struct {
106
108
// linkLibCpp already add (libc++ + libunwind + libc)
107
109
e .linkLibCpp ();
108
110
}
109
- e .linkSystemLibrary ("vulkan" );
110
111
m .builder .installArtifact (e );
111
112
e .want_lto = m .enable_lto ;
112
113
return e ;
@@ -117,6 +118,31 @@ pub fn build(b: *std.Build) !void {
117
118
var make = try Maker .init (b );
118
119
make .enable_lto = b .option (bool , "lto" , "Enable LTO optimization, (default: false)" ) orelse false ;
119
120
121
+ const llama_vulkan = b .option (bool , "llama-vulkan" , "Enable Vulkan backend for Llama, (default: false)" ) orelse false ;
122
+ const llama_metal = b .option (bool , "llama-metal" , "Enable Metal backend for Llama, (default: false, true for macos)" ) orelse (make .target .result .os .tag == .macos );
123
+ const llama_no_accelerate = b .option (bool , "llama-no-accelerate" , "Disable Accelerate framework for Llama, (default: false)" ) orelse false ;
124
+ const llama_accelerate = ! llama_no_accelerate and make .target .result .os .tag == .macos ;
125
+
126
+ if (llama_accelerate ) {
127
+ try make .addFlag ("-DGGML_USE_ACCELERATE" );
128
+ try make .addFlag ("-DACCELERATE_USE_LAPACK" );
129
+ try make .addFlag ("-DACCELERATE_LAPACK_ILP64" );
130
+ }
131
+
132
+ var extra_objs = ArrayList (* Compile ).init (b .allocator );
133
+
134
+ if (llama_vulkan ) {
135
+ try make .addFlag ("-DGGML_USE_VULKAN" );
136
+ const ggml_vulkan = make .obj ("ggml-vulkan" , "ggml-vulkan.cpp" );
137
+ try extra_objs .append (ggml_vulkan );
138
+ }
139
+
140
+ if (llama_metal ) {
141
+ try make .addFlag ("-DGGML_USE_METAL" );
142
+ const ggml_metal = make .obj ("ggml-metal" , "ggml-metal.m" );
143
+ try extra_objs .append (ggml_metal );
144
+ }
145
+
120
146
const ggml = make .obj ("ggml" , "ggml.c" );
121
147
const ggml_alloc = make .obj ("ggml-alloc" , "ggml-alloc.c" );
122
148
const ggml_backend = make .obj ("ggml-backend" , "ggml-backend.c" );
@@ -127,18 +153,13 @@ pub fn build(b: *std.Build) !void {
127
153
const console = make .obj ("console" , "common/console.cpp" );
128
154
const sampling = make .obj ("sampling" , "common/sampling.cpp" );
129
155
const grammar_parser = make .obj ("grammar-parser" , "common/grammar-parser.cpp" );
130
- // const train = make.obj("train", "common/train.cpp");
131
156
const clip = make .obj ("clip" , "examples/llava/clip.cpp" );
132
- const ggml_vulkan = make .obj ("ggml-vulkan " , "ggml-vulkan .cpp" );
157
+ // const train = make.obj("train ", "common/train .cpp");
133
158
134
- _ = make .exe ("main" , "examples/main/main.cpp" , &.{ ggml , ggml_alloc , ggml_backend , ggml_quants , llama , common , buildinfo , sampling , console , grammar_parser , ggml_vulkan });
135
- // _ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
136
- // _ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
137
- // _ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
138
- // _ = make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train });
139
- // _ = make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train });
159
+ var exes = ArrayList (* Compile ).init (b .allocator );
140
160
141
- const server = make .exe ("server" , "examples/server/server.cpp" , &.{
161
+ var objs = ArrayList (* Compile ).init (b .allocator );
162
+ try objs .appendSlice (&[_ ]* Compile {
142
163
ggml ,
143
164
ggml_alloc ,
144
165
ggml_backend ,
@@ -147,11 +168,40 @@ pub fn build(b: *std.Build) !void {
147
168
common ,
148
169
buildinfo ,
149
170
sampling ,
171
+ console ,
150
172
grammar_parser ,
151
173
clip ,
152
- ggml_vulkan ,
153
174
});
175
+ try objs .appendSlice (extra_objs .items );
176
+
177
+ const main = make .exe ("main" , "examples/main/main.cpp" , objs .items );
178
+ try exes .append (main );
179
+
180
+ // _ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
181
+ // _ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
182
+ // _ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo });
183
+ // _ = make.exe("finetune", "examples/finetune/finetune.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train });
184
+ // _ = make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, ggml_alloc, ggml_backend, ggml_quants, llama, common, buildinfo, train });
185
+
186
+ const server = make .exe ("server" , "examples/server/server.cpp" , objs .items );
154
187
if (make .target .result .os .tag == .windows ) {
155
188
server .linkSystemLibrary ("ws2_32" );
156
189
}
190
+ try exes .append (server );
191
+
192
+ for (exes .items ) | e | {
193
+ if (llama_vulkan ) {
194
+ e .linkSystemLibrary ("vulkan" );
195
+ }
196
+
197
+ if (llama_metal ) {
198
+ e .linkFramework ("Foundation" );
199
+ e .linkFramework ("Metal" );
200
+ e .linkFramework ("MetalKit" );
201
+ }
202
+
203
+ if (llama_accelerate ) {
204
+ e .linkFramework ("Accelerate" );
205
+ }
206
+ }
157
207
}
0 commit comments