-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
178 lines (151 loc) · 5.37 KB
/
build.zig
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
const std = @import ("std");
const toolbox = @import ("toolbox");
const Paths = struct
{
// prefixed attributes
__tmp: [] const u8 = undefined,
__tmp_src: [] const u8 = undefined,
__jq: [] const u8 = undefined,
__jq_src: [] const u8 = undefined,
// mandatory getters
pub fn getTmp (self: @This ()) [] const u8 { return self.__tmp; }
pub fn getTmpSrc (self: @This ()) [] const u8 { return self.__tmp_src; }
pub fn getJq (self: @This ()) [] const u8 { return self.__jq; }
pub fn getJqSrc (self: @This ()) [] const u8 { return self.__jq_src; }
// mandatory init
pub fn init (builder: *std.Build) !@This ()
{
var self = @This ()
{
.__jq = try builder.build_root.join (builder.allocator,
&.{ "jq", }),
.__tmp = try builder.build_root.join (builder.allocator,
&.{ "tmp", }),
};
self.__jq_src = try std.fs.path.join (builder.allocator,
&.{ self.getJq (), "src", });
self.__tmp_src = try std.fs.path.join (builder.allocator,
&.{ self.getTmp (), "src", });
return self;
}
};
fn update (builder: *std.Build, path: *const Paths,
dependencies: *const toolbox.Dependencies) !void
{
std.fs.deleteTreeAbsolute (path.getJq ()) catch |err|
{
switch (err)
{
error.FileNotFound => {},
else => return err,
}
};
try dependencies.clone (builder, "jq", path.getTmp ());
try toolbox.run (builder,
.{ .argv = &[_][] const u8 { "git", "submodule", "update", "--init", }, .cwd = path.getTmp (), });
try toolbox.run (builder,
.{ .argv = &[_][] const u8 { "autoreconf", "-i", }, .cwd = path.getTmp (), });
try toolbox.run (builder,
.{ .argv = &[_][] const u8 { "./configure", "--disable-docs", "--disable-valgrind", "--with-oniguruma=builtin", }, .cwd = path.getTmp (), });
try toolbox.run (builder,
.{ .argv = &[_][] const u8 { "make", "-j8", }, .cwd = path.getTmp (), });
try toolbox.make (path.getJq ());
try toolbox.make (path.getJqSrc ());
var src_dir = try std.fs.openDirAbsolute (path.getTmpSrc (),
.{ .iterate = true, });
defer src_dir.close ();
var walker = try src_dir.walk (builder.allocator);
defer walker.deinit ();
while (try walker.next ()) |*entry|
{
const dest = try std.fs.path.join (builder.allocator,
&.{ path.getJqSrc (), entry.path, });
switch (entry.kind)
{
.file => try toolbox.copy (try std.fs.path.join (builder.allocator,
&.{ path.getTmpSrc (), entry.path, }), dest),
.directory => try toolbox.make (dest),
else => return error.UnexpectedEntryKind,
}
}
try std.fs.deleteTreeAbsolute (path.getTmp ());
try std.fs.deleteTreeAbsolute (try std.fs.path.join (builder.allocator,
&.{ path.getJqSrc (), "inject_errors.c", }));
try std.fs.deleteTreeAbsolute (try std.fs.path.join (builder.allocator,
&.{ path.getJqSrc (), "main.c", }));
try toolbox.clean (builder, &.{ "jq", }, &.{ ".inc", });
}
pub fn build (builder: *std.Build) !void
{
const target = builder.standardTargetOptions (.{});
const optimize = builder.standardOptimizeOption (.{});
const path = try Paths.init (builder);
const dependencies = try toolbox.Dependencies.init (builder, "libjq.zig",
&.{ "jq", },
.{
.toolbox = .{
.name = "tiawl/toolbox",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
.oniguruma = .{
.name = "tiawl/oniguruma.zig",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
.winpthreads = .{
.name = "kassane/winpthreads-zigbuild",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.commit,
},
}, .{
.jq = .{
.name = "jqlang/jq",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.commit,
},
});
if (builder.option (bool, "update", "Update binding") orelse false)
try update (builder, &path, &dependencies);
const lib = builder.addStaticLibrary (.{
.name = "jq",
.root_source_file = builder.addWriteFiles ().add ("empty.c", ""),
.target = target,
.optimize = optimize,
});
toolbox.addInclude (lib, "jq");
if (lib.rootModuleTarget ().isMinGW ())
{
const winpthreads_dep = builder.dependency ("winpthreads", .{
.target = target,
.optimize = optimize,
});
const pthreads = winpthreads_dep.artifact ("winpthreads");
for (pthreads.root_module.include_dirs.items) |include|
{
lib.root_module.include_dirs.append (builder.allocator, include) catch {};
}
lib.linkLibrary (pthreads);
lib.linkSystemLibrary ("shlwapi");
}
const oniguruma_dep = builder.dependency ("oniguruma", .{
.target = target,
.optimize = optimize,
});
lib.linkLibrary (oniguruma_dep.artifact ("oniguruma"));
lib.installLibraryHeaders (oniguruma_dep.artifact ("oniguruma"));
lib.linkLibC ();
toolbox.addHeader (lib, path.getJqSrc (), ".", &.{ ".h", ".inc", });
var jq_src_dir =
try std.fs.openDirAbsolute (path.getJqSrc (), .{ .iterate = true, });
defer jq_src_dir.close ();
const flags = [_][] const u8 { "-DIEEE_8087=1", "-D_GNU_SOURCE=1", "-DHAVE_LIBONIG=1" };
var it = jq_src_dir.iterate ();
while (try it.next ()) |*entry|
{
if (toolbox.isCSource (entry.name) and entry.kind == .file)
try toolbox.addSource (lib, path.getJqSrc (), entry.name,
&flags);
}
builder.installArtifact (lib);
}