mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-28 08:31:25 +00:00 
			
		
		
		
	[Zig] Rewrite build for Zig 0.11 (#2514)
* zig build fixes * Disable LTO on Windows.
This commit is contained in:
		
							
								
								
									
										139
									
								
								build.zig
									
									
									
									
									
								
							
							
						
						
									
										139
									
								
								build.zig
									
									
									
									
									
								
							| @@ -1,68 +1,87 @@ | |||||||
|  | // Compatible with Zig Version 0.11.0 | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const commit_hash = @embedFile(".git/refs/heads/master"); | const Compile = std.Build.Step.Compile; | ||||||
|  | const ConfigHeader = std.Build.Step.ConfigHeader; | ||||||
|  | const Mode = std.builtin.Mode; | ||||||
|  | const CrossTarget = std.zig.CrossTarget; | ||||||
|  |  | ||||||
|  | const Maker = struct { | ||||||
|  |     builder: *std.build.Builder, | ||||||
|  |     target: CrossTarget, | ||||||
|  |     optimize: Mode, | ||||||
|  |     config_header: *ConfigHeader, | ||||||
|  |  | ||||||
|  |     const cflags = .{"-std=c11"}; | ||||||
|  |     const cxxflags = .{"-std=c++11"}; | ||||||
|  |  | ||||||
|  |     fn init(builder: *std.build.Builder) Maker { | ||||||
|  |         const commit_hash = @embedFile(".git/refs/heads/master"); | ||||||
|  |         const config_header = builder.addConfigHeader( | ||||||
|  |             .{ .style = .blank, .include_path = "build-info.h" }, | ||||||
|  |             .{ | ||||||
|  |                 .BUILD_NUMBER = 0, | ||||||
|  |                 .BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline | ||||||
|  |             }, | ||||||
|  |         ); | ||||||
|  |         return Maker{ | ||||||
|  |             .builder = builder, | ||||||
|  |             .target = builder.standardTargetOptions(.{}), | ||||||
|  |             .optimize = builder.standardOptimizeOption(.{}), | ||||||
|  |             .config_header = config_header, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fn obj(m: *const Maker, name: []const u8, src: []const u8) *Compile { | ||||||
|  |         const o = m.builder.addObject(.{ .name = name, .target = m.target, .optimize = m.optimize }); | ||||||
|  |         if (std.mem.endsWith(u8, src, ".c")) { | ||||||
|  |             o.addCSourceFiles(&.{src}, &cflags); | ||||||
|  |             o.linkLibC(); | ||||||
|  |         } else { | ||||||
|  |             o.addCSourceFiles(&.{src}, &cxxflags); | ||||||
|  |             o.linkLibCpp(); | ||||||
|  |         } | ||||||
|  |         o.addIncludePath(.{ .path = "." }); | ||||||
|  |         o.addIncludePath(.{ .path = "./examples" }); | ||||||
|  |         return o; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     fn exe(m: *const Maker, name: []const u8, src: []const u8, deps: []const *Compile) *Compile { | ||||||
|  |         const e = m.builder.addExecutable(.{ .name = name, .target = m.target, .optimize = m.optimize }); | ||||||
|  |         e.addIncludePath(.{ .path = "." }); | ||||||
|  |         e.addIncludePath(.{ .path = "./examples" }); | ||||||
|  |         e.addCSourceFiles(&.{src}, &cxxflags); | ||||||
|  |         for (deps) |d| e.addObject(d); | ||||||
|  |         e.linkLibC(); | ||||||
|  |         e.linkLibCpp(); | ||||||
|  |         e.addConfigHeader(m.config_header); | ||||||
|  |         m.builder.installArtifact(e); | ||||||
|  |  | ||||||
|  |         // Currently a bug is preventing correct linking for optimized builds for Windows: | ||||||
|  |         // https://github.com/ziglang/zig/issues/15958 | ||||||
|  |         if (e.target.isWindows()) { | ||||||
|  |             e.want_lto = false; | ||||||
|  |         } | ||||||
|  |         return e; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  |  | ||||||
| // Zig Version: 0.11.0-dev.3986+e05c242cd |  | ||||||
| pub fn build(b: *std.build.Builder) void { | pub fn build(b: *std.build.Builder) void { | ||||||
|     const target = b.standardTargetOptions(.{}); |     const make = Maker.init(b); | ||||||
|     const optimize = b.standardOptimizeOption(.{}); |  | ||||||
|  |  | ||||||
|     const config_header = b.addConfigHeader( |     const ggml = make.obj("ggml", "ggml.c"); | ||||||
|         .{ .style = .blank, .include_path = "build-info.h" }, |     const ggml_alloc = make.obj("ggml-alloc", "ggml-alloc.c"); | ||||||
|         .{ |     const llama = make.obj("llama", "llama.cpp"); | ||||||
|             .BUILD_NUMBER = 0, |     const common = make.obj("common", "examples/common.cpp"); | ||||||
|             .BUILD_COMMIT = commit_hash[0 .. commit_hash.len - 1], // omit newline |     const grammar_parser = make.obj("grammar-parser", "examples/grammar-parser.cpp"); | ||||||
|         }, |  | ||||||
|     ); |  | ||||||
|  |  | ||||||
|     const lib = b.addStaticLibrary(.{ |     _ = make.exe("main", "examples/main/main.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser }); | ||||||
|         .name = "llama", |     _ = make.exe("quantize", "examples/quantize/quantize.cpp", &.{ ggml, ggml_alloc, llama }); | ||||||
|         .target = target, |     _ = make.exe("perplexity", "examples/perplexity/perplexity.cpp", &.{ ggml, ggml_alloc, llama, common }); | ||||||
|         .optimize = optimize, |     _ = make.exe("embedding", "examples/embedding/embedding.cpp", &.{ ggml, ggml_alloc, llama, common }); | ||||||
|     }); |     _ = make.exe("train-text-from-scratch", "examples/train-text-from-scratch/train-text-from-scratch.cpp", &.{ ggml, ggml_alloc, llama }); | ||||||
|     lib.linkLibC(); |  | ||||||
|     lib.linkLibCpp(); |  | ||||||
|     lib.addIncludePath("."); |  | ||||||
|     lib.addIncludePath("./examples"); |  | ||||||
|     lib.addConfigHeader(config_header); |  | ||||||
|     lib.addCSourceFiles(&.{"ggml.c"}, &.{"-std=c11"}); |  | ||||||
|     lib.addCSourceFiles(&.{"llama.cpp"}, &.{"-std=c++11"}); |  | ||||||
|     b.installArtifact(lib); |  | ||||||
|  |  | ||||||
|     const examples = .{ |     const server = make.exe("server", "examples/server/server.cpp", &.{ ggml, ggml_alloc, llama, common, grammar_parser }); | ||||||
|         "main", |     if (server.target.isWindows()) { | ||||||
|         "baby-llama", |         server.linkSystemLibrary("ws2_32"); | ||||||
|         "embedding", |  | ||||||
|         "metal", |  | ||||||
|         "perplexity", |  | ||||||
|         "quantize", |  | ||||||
|         "quantize-stats", |  | ||||||
|         "save-load-state", |  | ||||||
|         "server", |  | ||||||
|         "simple", |  | ||||||
|         "train-text-from-scratch", |  | ||||||
|     }; |  | ||||||
|  |  | ||||||
|     inline for (examples) |example_name| { |  | ||||||
|         const exe = b.addExecutable(.{ |  | ||||||
|             .name = example_name, |  | ||||||
|             .target = target, |  | ||||||
|             .optimize = optimize, |  | ||||||
|         }); |  | ||||||
|         exe.addIncludePath("."); |  | ||||||
|         exe.addIncludePath("./examples"); |  | ||||||
|         exe.addConfigHeader(config_header); |  | ||||||
|         exe.addCSourceFiles(&.{ |  | ||||||
|             std.fmt.comptimePrint("examples/{s}/{s}.cpp", .{ example_name, example_name }), |  | ||||||
|             "examples/common.cpp", |  | ||||||
|         }, &.{"-std=c++11"}); |  | ||||||
|         exe.linkLibrary(lib); |  | ||||||
|         b.installArtifact(exe); |  | ||||||
|  |  | ||||||
|         const run_cmd = b.addRunArtifact(exe); |  | ||||||
|         run_cmd.step.dependOn(b.getInstallStep()); |  | ||||||
|         if (b.args) |args| run_cmd.addArgs(args); |  | ||||||
|  |  | ||||||
|         const run_step = b.step("run-" ++ example_name, "Run the app"); |  | ||||||
|         run_step.dependOn(&run_cmd.step); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Henri Vasserman
					Henri Vasserman