forked from Deins/llama.cpp.zig
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_server.zig
More file actions
219 lines (191 loc) · 7.77 KB
/
Copy pathbuild_server.zig
File metadata and controls
219 lines (191 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const std = @import("std");
const llama = @import("build_llama.zig");
const CompileStep = std.Build.Step.Compile;
const LazyPath = std.Build.LazyPath;
const Target = std.Build.ResolvedTarget;
pub const ServerOptions = struct {
enable_ssl: bool = false,
};
/// Build the llama-server executable
/// This builds the C++ server from llama.cpp/tools/server
pub fn buildServer(
b: *std.Build,
llama_ctx: *llama.Context,
target: Target,
optimize: std.builtin.OptimizeMode,
options: ServerOptions,
) !*CompileStep {
// First, generate the asset header files
const index_hpp = try generateAssetHeader(b, "llama.cpp/tools/server/public/index.html.gz");
const loading_hpp = try generateAssetHeader(b, "llama.cpp/tools/server/public/loading.html");
// Create server executable (C++ based - no Zig root source)
const server_module = b.createModule(.{
.target = target,
.optimize = optimize,
});
const server_exe = b.addExecutable(.{
.name = "llama-server",
.root_module = server_module,
});
// Include paths
server_exe.addIncludePath(llama_ctx.path(&.{"include"}));
server_exe.addIncludePath(llama_ctx.path(&.{"common"}));
server_exe.addIncludePath(llama_ctx.path(&.{ "ggml", "include" }));
server_exe.addIncludePath(llama_ctx.path(&.{ "ggml", "src" }));
server_exe.addIncludePath(llama_ctx.path(&.{ "tools", "server" }));
server_exe.addIncludePath(llama_ctx.path(&.{ "tools", "mtmd" })); // For mtmd.h (multimodal)
server_exe.addIncludePath(llama_ctx.path(&.{ "tools", "mtmd", "models" })); // For model headers
server_exe.addIncludePath(llama_ctx.path(&.{"vendor"})); // Add vendor dir for nlohmann/json_fwd.hpp
server_exe.addIncludePath(llama_ctx.path(&.{ "vendor", "cpp-httplib" })); // For httplib.h
// Add llama.cpp root for includes like "common/base64.hpp"
server_exe.addIncludePath(llama_ctx.path(&.{}));
// Add generated headers directories (both assets are in same dir but we ensure both are generated)
server_exe.addIncludePath(index_hpp.dirname());
server_exe.addIncludePath(loading_hpp.dirname());
// Define _USE_MATH_DEFINES for M_PI on Windows
server_exe.root_module.addCMacro("_USE_MATH_DEFINES", "");
// Enable httplib-based remote content fetching
server_exe.root_module.addCMacro("LLAMA_USE_HTTPLIB", "");
// C++ flags for server
const cpp_flags = &[_][]const u8{
"-std=c++17",
"-fexceptions",
"-fno-sanitize=undefined",
"-DNDEBUG",
"-Wno-unknown-attributes", // Ignore gnu_printf attribute warnings
};
// Relaxed C++ flags for third-party code (stb_image, miniaudio)
const cpp_flags_relaxed = &[_][]const u8{
"-std=c++17",
"-fexceptions",
"-fno-sanitize=undefined",
"-DNDEBUG",
"-Wno-unknown-attributes",
"-Wno-cast-qual", // For stb_image.h and miniaudio.h
};
// Add all server source files
const server_source_paths = [_][]const []const u8{
&.{ "tools", "server", "server.cpp" },
&.{ "tools", "server", "server-context.cpp" },
&.{ "tools", "server", "server-common.cpp" },
&.{ "tools", "server", "server-http.cpp" },
&.{ "tools", "server", "server-models.cpp" },
&.{ "tools", "server", "server-queue.cpp" },
&.{ "tools", "server", "server-task.cpp" },
};
for (server_source_paths) |path| {
server_exe.addCSourceFile(.{
.file = llama_ctx.path(path),
.flags = cpp_flags,
});
}
// Add cpp-httplib implementation (removed - already included in llama library)
// Add mtmd (multimodal) core source files
const mtmd_core_paths = [_][]const []const u8{
&.{ "tools", "mtmd", "mtmd.cpp" },
&.{ "tools", "mtmd", "mtmd-audio.cpp" },
&.{ "tools", "mtmd", "mtmd-helper.cpp" },
};
for (mtmd_core_paths) |path| {
server_exe.addCSourceFile(.{
.file = llama_ctx.path(path),
.flags = cpp_flags_relaxed,
});
}
// Add clip.cpp removed - already included in llama library to avoid duplicate symbols
// Add all mtmd model implementations
const mtmd_model_paths = [_][]const []const u8{
&.{ "tools", "mtmd", "models", "cogvlm.cpp" },
&.{ "tools", "mtmd", "models", "conformer.cpp" },
&.{ "tools", "mtmd", "models", "glm4v.cpp" },
&.{ "tools", "mtmd", "models", "internvl.cpp" },
&.{ "tools", "mtmd", "models", "kimivl.cpp" },
&.{ "tools", "mtmd", "models", "kimik25.cpp" },
&.{ "tools", "mtmd", "models", "llama4.cpp" },
&.{ "tools", "mtmd", "models", "llava.cpp" },
&.{ "tools", "mtmd", "models", "minicpmv.cpp" },
&.{ "tools", "mtmd", "models", "mobilenetv5.cpp" },
&.{ "tools", "mtmd", "models", "pixtral.cpp" },
&.{ "tools", "mtmd", "models", "qwen2vl.cpp" },
&.{ "tools", "mtmd", "models", "qwen3vl.cpp" },
&.{ "tools", "mtmd", "models", "siglip.cpp" },
&.{ "tools", "mtmd", "models", "paddleocr.cpp" },
&.{ "tools", "mtmd", "models", "nemotron-v2-vl.cpp" },
&.{ "tools", "mtmd", "models", "whisper-enc.cpp" },
&.{ "tools", "mtmd", "models", "youtuvl.cpp" },
};
for (mtmd_model_paths) |path| {
server_exe.addCSourceFile(.{
.file = llama_ctx.path(path),
.flags = cpp_flags_relaxed,
});
}
// Link llama library (includes common library)
llama_ctx.link(server_exe);
server_exe.linkLibCpp();
// Platform-specific configuration
switch (target.result.os.tag) {
.windows => {
server_exe.linkSystemLibrary("ws2_32");
// Note: _WIN32_WINNT is already defined by Zig, don't redefine
server_exe.root_module.addCMacro("NOMINMAX", "");
},
.linux, .freebsd, .netbsd, .openbsd => {
// pthread is typically linked automatically with libstdc++
},
.macos => {
// pthread is typically linked automatically
},
else => {},
}
// SSL support (optional)
if (options.enable_ssl) {
server_exe.root_module.addCMacro("CPPHTTPLIB_OPENSSL_SUPPORT", "");
switch (target.result.os.tag) {
.windows => {
// Would need OpenSSL for Windows
},
.linux, .freebsd, .netbsd, .openbsd => {
server_exe.linkSystemLibrary("ssl");
server_exe.linkSystemLibrary("crypto");
},
.macos => {
server_exe.linkFramework("Security");
},
else => {},
}
}
return server_exe;
}
/// Generate a C++ header file from a binary asset using xxd-style embedding
fn generateAssetHeader(b: *std.Build, input_path: []const u8) !LazyPath {
// Build the asset generator tool for the host
const gen_module = b.createModule(.{
.root_source_file = b.path("tools/generate_asset.zig"),
.target = b.graph.host,
.optimize = .ReleaseFast,
});
const asset_gen = b.addExecutable(.{
.name = "generate-asset",
.root_module = gen_module,
});
// Get output filename (input.hpp)
const basename = std.fs.path.basename(input_path);
const output_name = b.fmt("{s}.hpp", .{basename});
// Run the generator
const run_gen = b.addRunArtifact(asset_gen);
run_gen.addArg("--input");
run_gen.addFileArg(b.path(input_path));
run_gen.addArg("--output");
const output = run_gen.addOutputFileArg(output_name);
return output;
}
/// Create a run step for the server
pub fn createRunStep(b: *std.Build, server_exe: *CompileStep) *std.Build.Step.Run {
const run_cmd = b.addRunArtifact(server_exe);
// Pass through user arguments
if (b.args) |args| {
run_cmd.addArgs(args);
}
return run_cmd;
}