-
Notifications
You must be signed in to change notification settings - Fork 142
/
build.zig
443 lines (374 loc) · 16 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
const std = @import("std");
const Build = std.Build;
const Step = Build.Step;
const LazyPath = Build.LazyPath;
const Compile = Step.Compile;
const Run = Step.Run;
const UpdateSourceFiles = Step.UpdateSourceFiles;
const WriteFile = Step.WriteFile;
const ResolvedTarget = Build.ResolvedTarget;
const OptimizeMode = std.builtin.OptimizeMode;
// import build tools from r_build_zig
const r_build_zig = @import("r-build-zig");
// import generated build.zig
const generated_build = @import("build-aux/generated/build.zig");
pub fn build(b: *std.Build) !void {
// const buf: [std.mem.page_size]u8 = undefined;
const target = b.standardTargetOptions(.{});
const config_path = "build-aux/config.json";
const assets = b.option(
[]const u8,
"assets",
"Path to directory with R dependencies for offline build",
);
// additional steps
const update_step = b.step("update", "Generate R package build files");
const tarball_step = b.step("dist", "Make a source archive");
const fat_tarball_step = b.step("dist-fat", "Make a source archive with all R dependencies");
// step dist-fat requires assets option
if (assets == null)
fat_tarball_step.dependOn(&b.addFail("The dist-fat step requires -Dassets to be set.").step);
// declare build install rules
try fetch_assets_and_build(b, config_path, target, .ReleaseSafe, assets);
// declare rules for conf
build_conf(b);
// declare step: update
try generate_build_script(
b,
config_path,
&.{ // dirs relative to this build.zig file, and no trailing slashes
"packages",
"rcloud.client",
"rcloud.packages",
"rcloud.support",
},
update_step,
target,
.ReleaseSafe,
);
// declare step: dist
try make_tarball(b, tarball_step);
// declare step: dist-fat
try make_fat_tarball(b, fat_tarball_step, assets);
}
/// Declare steps which fetch external assets and declare the rules
/// generated by the generate-build tool.
fn fetch_assets_and_build(
b: *Build,
config_path: []const u8,
target: ResolvedTarget,
optimize: OptimizeMode,
assets: ?[]const u8,
) !void {
if (assets) |assets_dir| {
// we are doing an offline build
const assets_path = b.path(assets_dir);
// supply assets directory to build rule declarations
try generated_build.build(b, assets_path);
// add extra rcloud targets.
// TODO: get rid of this when we move rcloud.solr into this repository
add_extra_rcloud_targets(b, assets_path);
// declare rules for offline htdocs, which is just a copy of source files
build_htdocs_offline(b);
} else {
// we are doing a standard online build
// get the fetch-assets tool
const exe = b.dependency("r-build-zig", .{
.target = target,
.optimize = optimize,
}).artifact("fetch-assets");
// run it
const step = b.addRunArtifact(exe);
_ = step.addFileArg(b.path(config_path));
const out_dir = step.addOutputDirectoryArg("assets");
// add install step to copy assets directory to prefix
const assets_install = b.addInstallDirectory(.{
.source_dir = out_dir,
.install_dir = .prefix,
.install_subdir = "assets",
});
// install assets
b.getInstallStep().dependOn(&assets_install.step);
// supply output directory to build rule declarations
try generated_build.build(b, out_dir);
// add extra rcloud targets.
// TODO: get rid of this when we move rcloud.solr into this repository
add_extra_rcloud_targets(b, out_dir);
// declare rules for htdocs, which require an online build
build_htdocs(b);
}
}
/// Declare steps which generate a new build script given a
/// configuration file and list of source package directories relative
/// to this script (in the project root).
fn generate_build_script(
b: *Build,
config_path: []const u8,
relative_source_package_paths: []const []const u8,
update_step: *Step,
target: ResolvedTarget,
optimize: OptimizeMode,
) !void {
const exe = b.dependency("r-build-zig", .{
.target = target,
.optimize = optimize,
}).artifact("generate-build");
// arguments: config_file out_dir package_dirs...
const step = b.addRunArtifact(exe);
step.has_side_effects = true;
// the tool wants a relative path, so we use addArg, and manually
// add the dependency with addFileInput
_ = step.addArg(config_path);
_ = step.addFileInput(b.path(config_path));
const out_dir = step.addOutputDirectoryArg("deps");
for (relative_source_package_paths) |path| {
_ = step.addArg(path);
}
// copy the generated build.zig file to build-aux directory
const uf = b.addUpdateSourceFiles();
uf.addCopyFileToSource(out_dir.path(b, "build.zig"), "build-aux/generated/build.zig");
update_step.dependOn(&uf.step);
}
fn make_tarball(
b: *Build,
step: *Step,
) !void {
const version = try read_version_file(b.allocator);
const dirname = b.fmt("rcloud-{s}", .{version});
const tarname = b.fmt("rcloud-{s}.tar.gz", .{version});
const wf = b.addWriteFiles();
const tar = b.addSystemCommand(&.{ "tar", "czf" });
tar.setCwd(wf.getDirectory());
const tar_out = tar.addOutputFileArg(tarname);
_ = tar.addArg(dirname);
add_all_source_files(b, wf, dirname);
const tar_install = b.addInstallFileWithDir(tar_out, .prefix, tarname);
step.dependOn(&tar_install.step);
}
fn make_fat_tarball(
b: *Build,
step: *Step,
assets: ?[]const u8,
) !void {
if (assets == null) {
return;
}
const version = try read_version_file(b.allocator);
const dirname = b.fmt("rcloud-full-{s}", .{version});
const tarname = b.fmt("rcloud-full-{s}.tar.gz", .{version});
const wf = b.addWriteFiles();
const tar = b.addSystemCommand(&.{ "tar", "czf" });
tar.setCwd(wf.getDirectory());
const tar_out = tar.addOutputFileArg(tarname);
_ = tar.addArg(dirname);
add_all_source_files_and_assets(b, wf, dirname, assets.?);
const tar_install = b.addInstallFileWithDir(tar_out, .prefix, tarname);
step.dependOn(&tar_install.step);
}
fn add_all_source_files(b: *Build, wf: *WriteFile, dirname: []const u8) void {
const options = WriteFile.Directory.Options{
.exclude_extensions = &.{},
};
_ = add_copy_directory(b, wf, "conf", dirname, options);
_ = add_copy_directory(b, wf, "doc", dirname, options);
_ = add_copy_directory(b, wf, "docker", dirname, options);
_ = add_copy_directory(b, wf, "htdocs", dirname, options);
_ = add_copy_directory(b, wf, "packages", dirname, options);
_ = add_copy_directory(b, wf, "rcloud.client", dirname, options);
_ = add_copy_directory(b, wf, "rcloud.packages", dirname, options);
_ = add_copy_directory(b, wf, "rcloud.support", dirname, options);
_ = add_copy_directory(b, wf, "scripts", dirname, options);
_ = add_copy_directory(b, wf, "services", dirname, options);
_ = add_copy_directory(b, wf, "packages", dirname, options);
_ = add_copy_directory(b, wf, "vendor", dirname, options);
_ = add_copy_file(b, wf, "build-aux/config.json", dirname);
_ = add_copy_file(b, wf, "build-aux/generated/build.zig", dirname);
_ = add_copy_file(b, wf, "build.zig", dirname);
_ = add_copy_file(b, wf, "build.zig.zon", dirname);
_ = add_copy_file(b, wf, "flake.lock", dirname);
_ = add_copy_file(b, wf, "flake.nix", dirname);
_ = add_copy_file(b, wf, "Gruntfile.js", dirname);
_ = add_copy_file(b, wf, "LICENSE", dirname);
_ = add_copy_file(b, wf, "NEWS.md", dirname);
_ = add_copy_file(b, wf, "package.json", dirname);
_ = add_copy_file(b, wf, "package-lock.json", dirname);
_ = add_copy_file(b, wf, "README.md", dirname);
_ = add_copy_file(b, wf, "VERSION", dirname);
}
fn add_all_source_files_and_assets(b: *Build, wf: *WriteFile, dirname: []const u8, assets: []const u8) void {
const options = WriteFile.Directory.Options{
.exclude_extensions = &.{},
};
add_all_source_files(b, wf, dirname);
// include assets directory
_ = add_copy_directory(b, wf, assets, dirname, options);
// include generated javascript bundles. TODO: these paths depend
// on the default install prefix, zig-out. It will fail otherwise.
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud.css", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud.css.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-discover.css", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-discover.css.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-edit.css", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-edit.css.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-view.css", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-view.css.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-viewer.css", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/css/rcloud-viewer.css.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/merger_bundle.js", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/merger_bundle.js.gz", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/merger_bundle.js.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/merger_bundle.min.js", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/merger_bundle.min.js.gz", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/merger_bundle.min.js.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/rcloud_bundle.js", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/rcloud_bundle.js.gz", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/rcloud_bundle.js.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/rcloud_bundle.min.js", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/rcloud_bundle.min.js.gz", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/js/rcloud_bundle.min.js.map", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/lib/ace_bundle.js.gz", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/lib/ace_bundle.min.js", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/lib/ace_bundle.min.js.gz", dirname);
_ = add_copy_file(b, wf, "zig-out/htdocs/lib/ace_bundle.min.js.map", dirname);
}
fn add_copy_directory(
b: *Build,
wf: *WriteFile,
name: []const u8,
root: []const u8,
options: WriteFile.Directory.Options,
) void {
_ = wf.addCopyDirectory(b.path(name), b.fmt("{s}/{s}", .{ root, name }), options);
}
fn add_copy_file(b: *Build, wf: *WriteFile, name: []const u8, root: []const u8) void {
_ = wf.addCopyFile(b.path(name), b.fmt("{s}/{s}", .{ root, name }));
}
fn read_version_file(alloc: std.mem.Allocator) ![]const u8 {
const file = try std.fs.cwd().openFile("VERSION", .{});
defer file.close();
const content = try file.readToEndAlloc(alloc, 1024);
var i: usize = 0;
while (i < content.len) : (i += 1) {
if (content[i] == '\n') return content[0..i];
}
return content;
}
fn build_htdocs(b: *Build) void {
const wf = b.addWriteFiles();
// copy htdocs source
_ = wf.addCopyDirectory(b.path("htdocs"), "htdocs", .{});
// install js requirements
const npm_ci = b.addSystemCommand(&.{ "npm", "ci" });
npm_ci.setCwd(wf.getDirectory());
npm_ci.addFileInput(wf.addCopyFile(b.path("package.json"), "package.json"));
npm_ci.addFileInput(wf.addCopyFile(b.path("package-lock.json"), "package-lock.json"));
npm_ci.expectExitCode(0);
// run grunt
const grunt = b.addSystemCommand(&.{"node_modules/grunt-cli/bin/grunt"});
grunt.setCwd(wf.getDirectory());
grunt.addFileInput(wf.addCopyFile(b.path("Gruntfile.js"), "Gruntfile.js"));
grunt.addFileInput(wf.addCopyFile(b.path("VERSION"), "VERSION"));
grunt.addFileInput(wf.getDirectory().path(b, "node_modules/grunt-cli/bin/grunt"));
grunt.expectExitCode(0);
// which depends on npm_ci
grunt.step.dependOn(&npm_ci.step);
// add an install step for post-grunt htdocs
const htdocs_install = b.addInstallDirectory(.{
.source_dir = wf.getDirectory().path(b, "htdocs"),
.install_dir = .prefix,
.install_subdir = "htdocs",
});
htdocs_install.step.dependOn(&grunt.step);
// install built htdocs files
b.getInstallStep().dependOn(&htdocs_install.step);
}
/// An offline build of htdocs, just a copy.
fn build_htdocs_offline(b: *Build) void {
const wf = b.addWriteFiles();
// copy htdocs source
_ = wf.addCopyDirectory(b.path("htdocs"), "htdocs", .{});
// add an install step for post-grunt htdocs
const htdocs_install = b.addInstallDirectory(.{
.source_dir = wf.getDirectory().path(b, "htdocs"),
.install_dir = .prefix,
.install_subdir = "htdocs",
});
// install built htdocs files
b.getInstallStep().dependOn(&htdocs_install.step);
}
fn build_conf(b: *Build) void {
// install conf directory
b.getInstallStep().dependOn(&b.addInstallDirectory(.{
.source_dir = b.path("conf"),
.install_dir = .prefix,
.install_subdir = "conf",
}).step);
// install VERSION file
b.getInstallStep().dependOn(&b.addInstallFile(
b.path("VERSION"),
"VERSION",
).step);
}
fn add_extra_rcloud_targets(b: *Build, asset_dir: LazyPath) void {
// FIXME: This is a hack until rcloud.solr is added to this repository's source directories.
const libdir = b.addWriteFiles();
const ulog = b.addSystemCommand(&.{"R"});
ulog.addArgs(&.{
"CMD",
"INSTALL",
"--no-docs",
"--no-multiarch",
"-l",
});
_ = ulog.addDirectoryArg(libdir.getDirectory());
_ = ulog.addFileArg(asset_dir.path(b, "ulog_0.1-2.tar.gz"));
ulog.step.name = "ulog";
const ulog_out = ulog.captureStdOut();
_ = ulog.captureStdErr();
const ulog_install = b.addInstallDirectory(.{
.source_dir = libdir.getDirectory().path(b, "ulog"),
.install_dir = .{ .custom = "lib" },
.install_subdir = "ulog",
});
ulog_install.step.dependOn(&ulog.step);
b.getInstallStep().dependOn(&ulog_install.step);
b.getInstallStep().dependOn(&b.addInstallFileWithDir(ulog_out, .{ .custom = "logs" }, "ulog.log").step);
//
const rcloud_solr = b.addSystemCommand(&.{"R"});
rcloud_solr.addArgs(&.{
"CMD",
"INSTALL",
"--no-docs",
"--no-multiarch",
"-l",
});
_ = rcloud_solr.addDirectoryArg(libdir.getDirectory());
_ = rcloud_solr.addFileArg(asset_dir.path(b, "rcloud.solr_0.3.8.tar.gz"));
rcloud_solr.step.name = "rcloud.solr";
const rcloud_solr_out = rcloud_solr.captureStdOut();
_ = rcloud_solr.captureStdErr();
const rcloud_solr_install = b.addInstallDirectory(.{
.source_dir = libdir.getDirectory().path(b, "rcloud.solr"),
.install_dir = .{ .custom = "lib" },
.install_subdir = "rcloud.solr",
});
rcloud_solr_install.step.dependOn(&rcloud_solr.step);
b.getInstallStep().dependOn(&rcloud_solr_install.step);
b.getInstallStep().dependOn(&b.addInstallFileWithDir(rcloud_solr_out, .{ .custom = "logs" }, "rcloud.solr.log").step);
// this is so unfortunate but needed until we can get rid of this hack.
for (b.getInstallStep().dependencies.items) |install_step| {
for (install_step.dependencies.items) |step| {
if (std.mem.eql(u8, "rcloud.support", step.name)) {
rcloud_solr.step.dependOn(step);
} else if (std.mem.eql(u8, "Rserve", step.name)) {
rcloud_solr.step.dependOn(step);
} else if (std.mem.eql(u8, "httr", step.name)) {
rcloud_solr.step.dependOn(step);
} else if (std.mem.eql(u8, "ulog", step.name)) {
rcloud_solr.step.dependOn(step);
} else if (std.mem.eql(u8, "R6", step.name)) {
rcloud_solr.step.dependOn(step);
}
}
}
}