Merge pull request #5 from Deins:zig_13 · Deins/llama.cpp.zig@8798dea (original) (raw)

1

1

`const std = @import("std");

`

2

2

`const Builder = std.Build;

`

3

``

`-

const CrossTarget = std.zig.CrossTarget;

`

``

3

`+

const Target = std.Build.ResolvedTarget;

`

4

4

`const Mode = std.builtin.Mode;

`

5

``

`-

const CompileStep = std.Build.CompileStep;

`

``

5

`+

const CompileStep = std.Build.Step.Compile;

`

6

6

`const LazyPath = std.Build.LazyPath;

`

7

7

`const Module = std.Build.Module;

`

8

8

`pub const clblast = @import("clblast");

`

9

9

``

10

10

`pub const Options = struct {

`

11

``

`-

target: CrossTarget,

`

``

11

`+

target: Target,

`

12

12

`optimize: Mode,

`

13

13

`shared: bool, // static or shared lib

`

14

14

`opencl: ?clblast.OpenCL = null,

`

`@@ -27,22 +27,28 @@ pub const Context = struct {

`

27

27

`pub fn init(b: *Builder, op: Options) Context {

`

28

28

`const path_prefix = b.pathJoin(&.{ thisPath(), "/llama.cpp" });

`

29

29

`const zig_version = @import("builtin").zig_version_string;

`

30

``

`-

const exec = if (@hasDecl(std.ChildProcess, "exec")) std.ChildProcess.exec else std.ChildProcess.run; // zig 11 vs nightly compatibility

`

31

``

`-

const commit_hash = exec(

`

``

30

`+

const commit_hash = std.process.Child.run(

`

32

31

` .{ .allocator = b.allocator, .argv = &.{ "git", "rev-parse", "HEAD" } },

`

33

32

` ) catch |err| {

`

34

33

`std.log.err("Cant get git comiit hash! err: {}", .{err});

`

35

34

`unreachable;

`

36

35

` };

`

37

36

``

38

``

`-

const build_info_path = b.pathJoin(&.{ "common", "build-info.cpp" });

`

39

``

`-

const build_info = b.fmt(

`

``

37

`+

const build_info_zig = true; // use cpp or zig file for build-info

`

``

38

`+

const build_info_path = b.pathJoin(&.{ "common", "build-info." ++ if (build_info_zig) "zig" else "cpp" });

`

``

39

`+

const build_info = b.fmt(if (build_info_zig)

`

``

40

`+

\pub export var LLAMA_BUILD_NUMBER : c_int = {};

`

``

41

`+

\pub export var LLAMA_COMMIT = "{s}";

`

``

42

`+

\pub export var LLAMA_COMPILER = "Zig {s}";

`

``

43

`+

\pub export var LLAMA_BUILD_TARGET = "{s}_{s}";

`

``

44

`+

\

`

``

45

`+

else

`

40

46

`\int LLAMA_BUILD_NUMBER = {};

`

41

47

`\char const *LLAMA_COMMIT = "{s}";

`

42

48

`\char const *LLAMA_COMPILER = "Zig {s}";

`

43

49

`\char const *LLAMA_BUILD_TARGET = "{s}_{s}";

`

44

50

`\

`

45

``

`-

, .{ op.build_number, commit_hash.stdout[0 .. commit_hash.stdout.len - 1], zig_version, op.target.allocDescription(b.allocator) catch @panic("OOM"), @tagName(op.optimize) });

`

``

51

`+

, .{ op.build_number, commit_hash.stdout[0 .. commit_hash.stdout.len - 1], zig_version, op.target.result.zigTriple(b.allocator) catch unreachable, @tagName(op.optimize) });

`

46

52

``

47

53

`return .{

`

48

54

` .b = b,

`

`@@ -55,7 +61,7 @@ pub const Context = struct {

`

55

61

`/// just builds everything needed and links it to your target

`

56

62

`pub fn link(ctx: *Context, comp: *CompileStep) void {

`

57

63

`comp.linkLibrary(ctx.library());

`

58

``

`-

if (ctx.options.opencl) |ocl| ocl.link(comp);

`

``

64

`+

if (ctx.options.opencl) |ocl| ocl.link(ctx.b, comp);

`

59

65

` }

`

60

66

``

61

67

`/// build single library containing everything

`

`@@ -64,12 +70,12 @@ pub const Context = struct {

`

64

70

`const lib_opt = .{ .name = "llama.cpp", .target = ctx.options.target, .optimize = ctx.options.optimize };

`

65

71

`const lib = if (ctx.options.shared) ctx.b.addSharedLibrary(lib_opt) else ctx.b.addStaticLibrary(lib_opt);

`

66

72

`ctx.addAll(lib);

`

67

``

`-

if (ctx.options.target.getAbi() != .msvc)

`

``

73

`+

if (ctx.options.target.result.abi != .msvc)

`

68

74

`lib.defineCMacro("_GNU_SOURCE", null);

`

69

75

`if (ctx.options.shared) {

`

70

76

`lib.defineCMacro("LLAMA_SHARED", null);

`

71

77

`lib.defineCMacro("LLAMA_BUILD", null);

`

72

``

`-

if (ctx.options.target.getOsTag() == .windows) {

`

``

78

`+

if (ctx.options.target.result.os.tag == .windows) {

`

73

79

`std.log.warn("For shared linking to work, requires header llama.h modification:\n'# if defined(_WIN32) && (!defined(MINGW32) || defined(ZIG))'", .{});

`

74

80

`lib.defineCMacro("ZIG", null);

`

75

81

` }

`

`@@ -88,24 +94,26 @@ pub const Context = struct {

`

88

94

`/// zig module with translated headers

`

89

95

`pub fn moduleLlama(ctx: *Context) *Module {

`

90

96

`const tc = ctx.b.addTranslateC(.{

`

91

``

`-

.source_file = ctx.path("llama.h"),

`

``

97

`+

.root_source_file = ctx.path("llama.h"),

`

92

98

` .target = ctx.options.target,

`

93

99

` .optimize = ctx.options.optimize,

`

94

100

` });

`

95

``

`-

if (ctx.options.shared) tc.defineCMacro("LLAMA_SHARED", null);

`

96

``

`-

tc.defineCMacro("NDEBUG", null); // otherwise zig is unhappy about c ASSERT macro

`

``

101

`+

if (ctx.options.shared) tcDefineCMacro(tc, "LLAMA_SHARED", null);

`

``

102

`+

tcDefineCMacro(tc, "NDEBUG", null); // otherwise zig is unhappy about c ASSERT macro

`

97

103

`return tc.addModule("llama.h");

`

98

104

` }

`

99

105

``

100

106

`/// zig module with translated headers

`

101

107

`pub fn moduleGgml(ctx: *Context) *Module {

`

102

108

`const tc = ctx.b.addTranslateC(.{

`

103

``

`-

.source_file = ctx.path("ggml.h"),

`

``

109

`+

.root_source_file = ctx.path("ggml.h"),

`

104

110

` .target = ctx.options.target,

`

105

111

` .optimize = ctx.options.optimize,

`

106

112

` });

`

107

``

`-

if (ctx.options.shared) tc.defineCMacro("LLAMA_SHARED", null);

`

108

``

`-

tc.defineCMacro("NDEBUG", null); // otherwise zig is unhappy about c ASSERT macro

`

``

113

+

``

114

`+

tcDefineCMacro(tc, "LLAMA_SHARED", null);

`

``

115

`+

tcDefineCMacro(tc, "NDEBUG", null);

`

``

116

+

109

117

`return tc.addModule("ggml.h");

`

110

118

` }

`

111

119

``

`@@ -133,7 +141,7 @@ pub const Context = struct {

`

133

141

` .backend = .{ .opencl = ctx.options.opencl.? },

`

134

142

` });

`

135

143

`blast.link(compile);

`

136

``

`-

ctx.options.opencl.?.link(compile);

`

``

144

`+

ctx.options.opencl.?.link(ctx.b, compile);

`

137

145

` }

`

138

146

`for (sources) |src| compile.addCSourceFile(.{ .file = src, .flags = ctx.flags() });

`

139

147

`//if (ctx.cuda) compile.ctx.path("ggml-cuda.cu");

`

`@@ -171,14 +179,14 @@ pub const Context = struct {

`

171

179

`if (install) b.installArtifact(exe);

`

172

180

` { // add all c/cpp files from example dir

`

173

181

`const rpath = b.pathJoin(&.{ ctx.path_prefix, "examples", ex });

`

174

``

`-

exe.addIncludePath(.{ .path = rpath });

`

``

182

`+

exe.addIncludePath(.{ .cwd_relative = rpath });

`

175

183

`var dir = if (@hasDecl(std.fs, "openIterableDirAbsolute")) try std.fs.openIterableDirAbsolute(b.pathFromRoot(rpath), .{}) else try std.fs.openDirAbsolute(b.pathFromRoot(rpath), .{ .iterate = true }); // zig 11 vs nightly compatibility

`

176

184

`defer dir.close();

`

177

185

`var dir_it = dir.iterate();

`

178

186

`while (try dir_it.next()) |f| switch (f.kind) {

`

179

187

`.file => if (std.ascii.endsWithIgnoreCase(f.name, ".c") or std.ascii.endsWithIgnoreCase(f.name, ".cpp")) {

`

180

188

`const src = b.pathJoin(&.{ ctx.path_prefix, "examples", ex, f.name });

`

181

``

`-

exe.addCSourceFile(.{ .file = .{ .path = src }, .flags = &.{} });

`

``

189

`+

exe.addCSourceFile(.{ .file = .{ .cwd_relative = src }, .flags = &.{} });

`

182

190

` },

`

183

191

`else => {},

`

184

192

` };

`

`@@ -206,10 +214,16 @@ pub const Context = struct {

`

206

214

` }

`

207

215

``

208

216

`pub fn path(self: Context, p: []const u8) LazyPath {

`

209

``

`-

return .{ .path = self.b.pathJoin(&.{ self.path_prefix, p }) };

`

``

217

`+

return .{ .cwd_relative = self.b.pathJoin(&.{ self.path_prefix, p }) };

`

210

218

` }

`

211

219

`};

`

212

220

``

213

221

`fn thisPath() []const u8 {

`

214

222

`return std.fs.path.dirname(@src().file) orelse ".";

`

215

223

`}

`

``

224

+

``

225

`+

// TODO: idk, defineCMacro returns: TranslateC.zig:110:28: error: root struct of file 'Build' has no member named 'constructranslate_cMacro'

`

``

226

`+

// use raw macro for now

`

``

227

`+

fn tcDefineCMacro(tc: *std.Build.Step.TranslateC, comptime name: []const u8, comptime value: ?[]const u8) void {

`

``

228

`+

tc.defineCMacroRaw(name ++ "=" ++ (value orelse "1"));

`

``

229

`+

}

`