@@ -11,6 +11,8 @@ const Maker = struct {
1111 target : Target ,
1212 optimize : Mode ,
1313 enable_lto : bool ,
14+ build_all : bool ,
15+ install_libs : bool ,
1416
1517 include_dirs : ArrayList ([]const u8 ),
1618 cflags : ArrayList ([]const u8 ),
@@ -53,6 +55,8 @@ const Maker = struct {
5355 .target = target ,
5456 .optimize = builder .standardOptimizeOption (.{}),
5557 .enable_lto = false ,
58+ .build_all = false ,
59+ .install_libs = false ,
5660 .include_dirs = ArrayList ([]const u8 ).init (builder .allocator ),
5761 .cflags = ArrayList ([]const u8 ).init (builder .allocator ),
5862 .cxxflags = ArrayList ([]const u8 ).init (builder .allocator ),
@@ -75,8 +79,8 @@ const Maker = struct {
7579 return m ;
7680 }
7781
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 });
8084
8185 if (std .mem .endsWith (u8 , src , ".c" ) or std .mem .endsWith (u8 , src , ".m" )) {
8286 o .addCSourceFiles (.{ .files = &.{src }, .flags = m .cflags .items });
@@ -92,13 +96,17 @@ const Maker = struct {
9296 }
9397 for (m .include_dirs .items ) | i | o .addIncludePath (.{ .path = i });
9498 o .want_lto = m .enable_lto ;
99+ if (m .install_libs ) m .builder .installArtifact (o );
95100 return o ;
96101 }
97102
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+
99107 const e = m .builder .addExecutable (.{ .name = name , .target = m .target , .optimize = m .optimize });
100108 e .addCSourceFiles (.{ .files = &.{src }, .flags = m .cxxflags .items });
101- for (deps ) | d | e .addObject (d );
109+ for (deps ) | d | e .linkLibrary (d );
102110 for (m .include_dirs .items ) | i | e .addIncludePath (.{ .path = i });
103111
104112 // https://github.com/ziglang/zig/issues/15448
@@ -117,6 +125,8 @@ const Maker = struct {
117125pub fn build (b : * std.Build ) ! void {
118126 var make = try Maker .init (b );
119127 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 ;
120130
121131 // Options
122132 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 {
131141 try make .addFlag ("-DACCELERATE_LAPACK_ILP64" );
132142 }
133143
134- // Objects
135- var extra_objs = ArrayList (* Compile ).init (b .allocator );
144+ // Libraries
145+ var extra_libs = ArrayList (* Compile ).init (b .allocator );
136146
137147 if (llama_vulkan ) {
138148 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 );
141151 }
142152
143153 if (llama_metal ) {
144154 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 );
147157 }
148158
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" );
161171
162172 // Executables
163173 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 {
167177 const finetune = make .exe ("finetune" , "examples/finetune/finetune.cpp" , &.{ ggml , ggml_alloc , ggml_backend , ggml_quants , llama , common , buildinfo , train });
168178 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 });
169179 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" );
172182 }
173183
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 };
175185
176186 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 );
178189
179190 if (llama_vulkan ) {
180- e .linkSystemLibrary ("vulkan" );
191+ e .? . linkSystemLibrary ("vulkan" );
181192 }
182193
183194 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" );
187198 }
188199
189200 if (llama_accelerate ) {
190- e .linkFramework ("Accelerate" );
201+ e .? . linkFramework ("Accelerate" );
191202 }
192203 }
193204}
0 commit comments