-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sqlite.zig
More file actions
254 lines (213 loc) · 9.16 KB
/
Copy pathbuild.sqlite.zig
File metadata and controls
254 lines (213 loc) · 9.16 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const std = @import("std");
const mem = std.mem;
const Target = struct {
include: [][]const u8,
files: [][]const u8,
flags: [][]const u8,
modules: [][]const u8,
};
const Options = struct {
enable_fts5: bool,
enable_carray: bool,
};
pub fn sqlite(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
opts: Options,
) !*std.Build.Step.Compile {
const sqlite_dep = b.dependency("sqlite", .{});
const sqlite_path = sqlite_dep.path("");
const libsqlite = b.addLibrary(.{
.name = "sqlite",
.linkage = .static,
.root_module = b.createModule(.{
.link_libc = true,
.target = target,
.optimize = optimize,
}),
});
const base = try getConfig(Target, b, "targets", "base");
for (base.include) |include_path| libsqlite.root_module.addIncludePath(sqlite_dep.path(include_path));
libsqlite.root_module.addCSourceFiles(.{
.root = sqlite_path,
.files = base.files,
.flags = base.flags,
});
const lemon = genLemon(b, sqlite_path);
const jimsh = genJimsh(b, sqlite_path);
const mksourceid = genMKSourceid(b, sqlite_path);
const mkkeywordhash = genMKKeywordHash(b, sqlite_path);
const pipe_concat = pipeAndConcat(b);
const parse_run = b.addRunArtifact(lemon);
const parse_dir = parse_run.addPrefixedOutputDirectoryArg("-d", "parse");
parse_run.addPrefixedFileArg("-T", sqlite_path.path(b, "tool/lempar.c"));
parse_run.addArg("-S");
parse_run.addFileArg(sqlite_path.path(b, "src/parse.y"));
const parse_vdbe_concat_run = b.addRunArtifact(pipe_concat);
parse_vdbe_concat_run.setStdIn(.{ .lazy_path = parse_dir.path(b, "parse.h") });
parse_vdbe_concat_run.addFileArg(sqlite_path.path(b, "src/vdbe.c"));
const parse_vdbe = parse_vdbe_concat_run.captureStdOut(.{});
const opcodes_h_run = b.addRunArtifact(jimsh);
opcodes_h_run.setStdIn(.{ .lazy_path = parse_vdbe });
opcodes_h_run.addFileArg(sqlite_path.path(b, "tool/mkopcodeh.tcl"));
const opcodes_h = opcodes_h_run.captureStdOut(.{});
const mksqlite3h_scope = b.addWriteFiles();
_ = mksqlite3h_scope.addCopyDirectory(mksourceid.getEmittedBinDirectory(), "", .{});
const mksqlite3h = mksqlite3h_scope.addCopyDirectory(sqlite_path, "", .{ .include_extensions = &.{
"VERSION",
"manifest",
"manifest.uuid",
"manifest.tags",
"tool/mksqlite3h.tcl",
"src/sqlite.h.in",
"ext/rtree/sqlite3rtree.h",
"ext/session/sqlite3session.h",
"ext/fts5/fts5.h",
} });
const sqlite_h_run = b.addRunArtifact(jimsh);
sqlite_h_run.setCwd(mksqlite3h);
sqlite_h_run.addFileArg(mksqlite3h.path(b, "tool/mksqlite3h.tcl"));
sqlite_h_run.addDirectoryArg(mksqlite3h);
const sqlite_h = sqlite_h_run.captureStdOut(.{});
const keywordhash_run = b.addRunArtifact(mkkeywordhash);
const keywordhash_h = keywordhash_run.captureStdOut(.{});
const pragma_h_run = b.addRunArtifact(jimsh);
pragma_h_run.addFileArg(sqlite_path.path(b, "tool/mkpragmatab.tcl"));
const pragma_h = pragma_h_run.addOutputFileArg("pragma.h");
const ctime_c_run = b.addRunArtifact(jimsh);
ctime_c_run.addFileArg(sqlite_path.path(b, "tool/mkctimec.tcl"));
const ctime_c = ctime_c_run.addOutputFileArg("ctime.c");
if (opts.enable_fts5) {
const awf_fts5 = b.addWriteFiles();
const fts5_parse_dir = awf_fts5.addCopyDirectory(sqlite_path.path(b, "ext/fts5"), "parse/fts5", .{ .exclude_extensions = &.{".test"} });
_ = awf_fts5.addCopyFile(sqlite_path.path(b, "manifest"), "manifest");
_ = awf_fts5.addCopyFile(sqlite_path.path(b, "manifest.uuid"), "manifest.uuid");
const parse_fts5_run = b.addRunArtifact(lemon);
parse_fts5_run.setCwd(fts5_parse_dir);
parse_fts5_run.addPrefixedFileArg("-T", sqlite_path.path(b, "tool/lempar.c"));
parse_fts5_run.addPrefixedDirectoryArg("-d", fts5_parse_dir);
parse_fts5_run.addArg("-S");
parse_fts5_run.addFileArg(fts5_parse_dir.path(b, "fts5parse.y"));
const fts5_c_run = b.addRunArtifact(jimsh);
fts5_c_run.setCwd(fts5_parse_dir);
fts5_c_run.addFileArg(fts5_parse_dir.path(b, "tool/mkfts5c.tcl"));
fts5_c_run.step.dependOn(&parse_fts5_run.step);
libsqlite.step.dependOn(&fts5_c_run.step);
libsqlite.root_module.addIncludePath(fts5_parse_dir);
libsqlite.root_module.addCSourceFile(.{ .file = fts5_parse_dir.path(b, "fts5.c") });
libsqlite.root_module.addCMacro("SQLITE_ENABLE_FTS5", "1");
}
const awf_h = b.addWriteFiles();
const opcodes_h_file = awf_h.addCopyFile(opcodes_h, "opcodes.h");
const sqlite_h_file = awf_h.addCopyFile(sqlite_h, "sqlite3.h");
_ = awf_h.addCopyFile(keywordhash_h, "keywordhash.h");
_ = awf_h.addCopyFile(pragma_h, "pragma.h");
const opcodes_c_run = b.addRunArtifact(jimsh);
opcodes_c_run.addFileArg(sqlite_path.path(b, "tool/mkopcodec.tcl"));
opcodes_c_run.addFileArg(opcodes_h_file);
const opcodes_c = opcodes_c_run.captureStdOut(.{});
const awf_c = b.addWriteFiles();
const opcodes_c_file = awf_c.addCopyFile(opcodes_c, "opcodes.c");
libsqlite.root_module.addIncludePath(parse_dir);
libsqlite.root_module.addIncludePath(awf_h.getDirectory());
libsqlite.root_module.addCSourceFile(.{ .file = ctime_c });
libsqlite.root_module.addCSourceFile(.{ .file = opcodes_c_file });
libsqlite.root_module.addCSourceFile(.{ .file = parse_dir.path(b, "parse.c") });
libsqlite.root_module.addCMacro("SQLITE_CORE", "1");
// TODO: Allow users to set the debug mode themselves
if (optimize == .Debug) {
libsqlite.root_module.addCMacro("SQLITE_DEBUG", "1");
}
if (opts.enable_carray) libsqlite.root_module.addCMacro("SQLITE_ENABLE_CARRAY", "1");
const sqlitebindings = b.addTranslateC(.{
.optimize = optimize,
.target = target,
.root_source_file = sqlite_h_file,
});
sqlitebindings.addIncludePath(awf_h.getDirectory());
_ = b.addModule("sqlite3.h", .{ .root_source_file = sqlite_h_file });
const lib = b.addLibrary(.{
.name = "sqlite",
.linkage = .static,
.root_module = sqlitebindings.createModule(),
});
lib.root_module.linkLibrary(libsqlite);
return lib;
}
fn pipeAndConcat(b: *std.Build) *std.Build.Step.Compile {
return b.addExecutable(.{ .name = "pipe_concat", .root_module = b.createModule(.{
.root_source_file = b.path("scripts/build.pipe_concat.zig"),
.optimize = .ReleaseSmall,
.target = b.graph.host,
}) });
}
fn genMKKeywordHash(
b: *std.Build,
sqlite_path: std.Build.LazyPath,
) *std.Build.Step.Compile {
const tool_path = sqlite_path.path(b, "tool");
const mkkeywordhash = b.addExecutable(.{ .name = "mkkeywordhash", .root_module = b.createModule(.{
.optimize = .ReleaseSmall,
.target = b.graph.host,
.link_libc = true,
}) });
mkkeywordhash.root_module.addCSourceFile(.{ .file = tool_path.path(b, "mkkeywordhash.c") });
return mkkeywordhash;
}
fn genMKSourceid(
b: *std.Build,
sqlite_path: std.Build.LazyPath,
) *std.Build.Step.Compile {
const tool_path = sqlite_path.path(b, "tool");
const mksourceid = b.addExecutable(.{ .name = "mksourceid", .root_module = b.createModule(.{
.optimize = .ReleaseSmall,
.target = b.graph.host,
.link_libc = true,
}) });
mksourceid.root_module.addCSourceFile(.{ .file = tool_path.path(b, "mksourceid.c") });
return mksourceid;
}
fn genLemon(
b: *std.Build,
sqlite_path: std.Build.LazyPath,
) *std.Build.Step.Compile {
const tool_path = sqlite_path.path(b, "tool");
const lemon = b.addExecutable(.{ .name = "lemon", .root_module = b.createModule(.{
.optimize = .ReleaseSmall,
.target = b.graph.host,
.link_libc = true,
}) });
lemon.root_module.addCSourceFile(.{ .file = tool_path.path(b, "lemon.c") });
return lemon;
}
fn genJimsh(
b: *std.Build,
sqlite_path: std.Build.LazyPath,
) *std.Build.Step.Compile {
const autosetup_path = sqlite_path.path(b, "autosetup");
const jimsh = b.addExecutable(.{ .name = "jimsh", .root_module = b.createModule(.{
.optimize = .ReleaseSmall,
.target = b.graph.host,
.link_libc = true,
}) });
jimsh.root_module.addCSourceFile(.{
.file = autosetup_path.path(b, "jimsh0.c"),
.flags = &.{"-DHAVE_REALPATH"},
});
return jimsh;
}
fn getConfig(comptime T: type, b: *std.Build, dir: []const u8, name: []const u8) !T {
const alloc = b.allocator;
const io = b.graph.io;
const config_path = try std.Io.Dir.realPathFileAbsoluteAlloc(io, b.fmt("{s}/{s}/{s}.zon", .{ b.build_root.path.?, dir, name }), alloc);
defer alloc.free(config_path);
const config_file = try std.Io.Dir.openFileAbsolute(io, config_path, .{});
defer config_file.close(io);
const file = try config_file.stat(io);
var buffer = try alloc.allocSentinel(u8, file.size, 0);
errdefer alloc.destroy(&buffer);
var reader = config_file.reader(io, buffer);
try reader.interface.readSliceAll(buffer);
return try std.zon.parse.fromSliceAlloc(T, alloc, buffer, null, .{});
}