@@ -11,6 +11,8 @@ const Maker = struct {
11
11
target : Target ,
12
12
optimize : Mode ,
13
13
enable_lto : bool ,
14
+ build_all : bool ,
15
+ install_libs : bool ,
14
16
15
17
include_dirs : ArrayList ([]const u8 ),
16
18
cflags : ArrayList ([]const u8 ),
@@ -53,6 +55,8 @@ const Maker = struct {
53
55
.target = target ,
54
56
.optimize = builder .standardOptimizeOption (.{}),
55
57
.enable_lto = false ,
58
+ .build_all = false ,
59
+ .install_libs = false ,
56
60
.include_dirs = ArrayList ([]const u8 ).init (builder .allocator ),
57
61
.cflags = ArrayList ([]const u8 ).init (builder .allocator ),
58
62
.cxxflags = ArrayList ([]const u8 ).init (builder .allocator ),
@@ -75,8 +79,8 @@ const Maker = struct {
75
79
return m ;
76
80
}
77
81
78
- fn obj (m : * const Maker , name : []const u8 , src : []const u8 ) * Compile {
79
- const o = m .builder .addObject (.{ .name = name , .target = m .target , .optimize = m .optimize });
82
+ fn lib (m : * const Maker , name : []const u8 , src : []const u8 ) * Compile {
83
+ const o = m .builder .addStaticLibrary (.{ .name = name , .target = m .target , .optimize = m .optimize });
80
84
81
85
if (std .mem .endsWith (u8 , src , ".c" ) or std .mem .endsWith (u8 , src , ".m" )) {
82
86
o .addCSourceFiles (.{ .files = &.{src }, .flags = m .cflags .items });
@@ -92,13 +96,17 @@ const Maker = struct {
92
96
}
93
97
for (m .include_dirs .items ) | i | o .addIncludePath (.{ .path = i });
94
98
o .want_lto = m .enable_lto ;
99
+ if (m .install_libs ) m .builder .installArtifact (o );
95
100
return o ;
96
101
}
97
102
98
- fn exe (m : * const Maker , name : []const u8 , src : []const u8 , deps : []const * Compile ) * Compile {
103
+ fn exe (m : * const Maker , name : []const u8 , src : []const u8 , deps : []const * Compile ) ? * Compile {
104
+ const opt = m .builder .option (bool , name , std .fmt .allocPrint (m .builder .allocator , "Build and install the {s} executable" , .{name }) catch @panic ("OOM" )) orelse false ;
105
+ if (! opt and ! m .build_all ) return null ;
106
+
99
107
const e = m .builder .addExecutable (.{ .name = name , .target = m .target , .optimize = m .optimize });
100
108
e .addCSourceFiles (.{ .files = &.{src }, .flags = m .cxxflags .items });
101
- for (deps ) | d | e .addObject (d );
109
+ for (deps ) | d | e .linkLibrary (d );
102
110
for (m .include_dirs .items ) | i | e .addIncludePath (.{ .path = i });
103
111
104
112
// https://github.com/ziglang/zig/issues/15448
@@ -117,6 +125,8 @@ const Maker = struct {
117
125
pub fn build (b : * std.Build ) ! void {
118
126
var make = try Maker .init (b );
119
127
make .enable_lto = b .option (bool , "lto" , "Enable LTO optimization, (default: false)" ) orelse false ;
128
+ make .build_all = b .option (bool , "build-all" , "Build all executables, (default: false)" ) orelse false ;
129
+ make .install_libs = b .option (bool , "install-libs" , "Install all libraries, (default: false)" ) orelse false ;
120
130
121
131
// Options
122
132
const llama_vulkan = b .option (bool , "llama-vulkan" , "Enable Vulkan backend for Llama, (default: false)" ) orelse false ;
@@ -131,33 +141,33 @@ pub fn build(b: *std.Build) !void {
131
141
try make .addFlag ("-DACCELERATE_LAPACK_ILP64" );
132
142
}
133
143
134
- // Objects
135
- var extra_objs = ArrayList (* Compile ).init (b .allocator );
144
+ // Libraries
145
+ var extra_libs = ArrayList (* Compile ).init (b .allocator );
136
146
137
147
if (llama_vulkan ) {
138
148
try make .addFlag ("-DGGML_USE_VULKAN" );
139
- const ggml_vulkan = make .obj ("ggml-vulkan" , "ggml-vulkan.cpp" );
140
- try extra_objs .append (ggml_vulkan );
149
+ const ggml_vulkan = make .lib ("ggml-vulkan" , "ggml-vulkan.cpp" );
150
+ try extra_libs .append (ggml_vulkan );
141
151
}
142
152
143
153
if (llama_metal ) {
144
154
try make .addFlag ("-DGGML_USE_METAL" );
145
- const ggml_metal = make .obj ("ggml-metal" , "ggml-metal.m" );
146
- try extra_objs .append (ggml_metal );
155
+ const ggml_metal = make .lib ("ggml-metal" , "ggml-metal.m" );
156
+ try extra_libs .append (ggml_metal );
147
157
}
148
158
149
- const ggml = make .obj ("ggml" , "ggml.c" );
150
- const ggml_alloc = make .obj ("ggml-alloc" , "ggml-alloc.c" );
151
- const ggml_backend = make .obj ("ggml-backend" , "ggml-backend.c" );
152
- const ggml_quants = make .obj ("ggml-quants" , "ggml-quants.c" );
153
- const llama = make .obj ("llama" , "llama.cpp" );
154
- const buildinfo = make .obj ("common" , "common/build-info.cpp" );
155
- const common = make .obj ("common" , "common/common.cpp" );
156
- const console = make .obj ("console" , "common/console.cpp" );
157
- const sampling = make .obj ("sampling" , "common/sampling.cpp" );
158
- const grammar_parser = make .obj ("grammar-parser" , "common/grammar-parser.cpp" );
159
- const clip = make .obj ("clip" , "examples/llava/clip.cpp" );
160
- const train = make .obj ("train" , "common/train.cpp" );
159
+ const ggml = make .lib ("ggml" , "ggml.c" );
160
+ const ggml_alloc = make .lib ("ggml-alloc" , "ggml-alloc.c" );
161
+ const ggml_backend = make .lib ("ggml-backend" , "ggml-backend.c" );
162
+ const ggml_quants = make .lib ("ggml-quants" , "ggml-quants.c" );
163
+ const llama = make .lib ("llama" , "llama.cpp" );
164
+ const buildinfo = make .lib ("common" , "common/build-info.cpp" );
165
+ const common = make .lib ("common" , "common/common.cpp" );
166
+ const console = make .lib ("console" , "common/console.cpp" );
167
+ const sampling = make .lib ("sampling" , "common/sampling.cpp" );
168
+ const grammar_parser = make .lib ("grammar-parser" , "common/grammar-parser.cpp" );
169
+ const clip = make .lib ("clip" , "examples/llava/clip.cpp" );
170
+ const train = make .lib ("train" , "common/train.cpp" );
161
171
162
172
// Executables
163
173
const main = make .exe ("main" , "examples/main/main.cpp" , &.{ ggml , ggml_alloc , ggml_backend , ggml_quants , llama , common , buildinfo , sampling , console , grammar_parser , clip });
@@ -167,27 +177,28 @@ pub fn build(b: *std.Build) !void {
167
177
const finetune = make .exe ("finetune" , "examples/finetune/finetune.cpp" , &.{ ggml , ggml_alloc , ggml_backend , ggml_quants , llama , common , buildinfo , train });
168
178
const train_text_from_scratch = 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 });
169
179
const server = make .exe ("server" , "examples/server/server.cpp" , &.{ ggml , ggml_alloc , ggml_backend , ggml_quants , llama , common , buildinfo , sampling , console , grammar_parser , clip });
170
- if (make .target .result .os .tag == .windows ) {
171
- server .linkSystemLibrary ("ws2_32" );
180
+ if (make .target .result .os .tag == .windows and server != null ) {
181
+ server .? . linkSystemLibrary ("ws2_32" );
172
182
}
173
183
174
- const exes = [_ ]* Compile { main , server , quantize , perplexity , embedding , finetune , train_text_from_scratch };
184
+ const exes = [_ ]? * Compile { main , server , quantize , perplexity , embedding , finetune , train_text_from_scratch };
175
185
176
186
for (exes ) | e | {
177
- for (extra_objs .items ) | o | e .addObject (o );
187
+ if (e == null ) continue ;
188
+ for (extra_libs .items ) | o | e .? .addObject (o );
178
189
179
190
if (llama_vulkan ) {
180
- e .linkSystemLibrary ("vulkan" );
191
+ e .? . linkSystemLibrary ("vulkan" );
181
192
}
182
193
183
194
if (llama_metal ) {
184
- e .linkFramework ("Foundation" );
185
- e .linkFramework ("Metal" );
186
- e .linkFramework ("MetalKit" );
195
+ e .? . linkFramework ("Foundation" );
196
+ e .? . linkFramework ("Metal" );
197
+ e .? . linkFramework ("MetalKit" );
187
198
}
188
199
189
200
if (llama_accelerate ) {
190
- e .linkFramework ("Accelerate" );
201
+ e .? . linkFramework ("Accelerate" );
191
202
}
192
203
}
193
204
}
0 commit comments