Skip to content

Commit 5016909

Browse files
committed
upgrade zig 0.12
0.12.0-dev.3439+31a7f22b8
1 parent bf5d995 commit 5016909

File tree

2 files changed

+57
-60
lines changed

2 files changed

+57
-60
lines changed

build.zig

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const std = @import("std");
22
const json = std.json;
3-
const Builder = std.build.Builder;
4-
const CompileStep = std.build.CompileStep;
5-
const Step = std.build.Step;
3+
const Step = std.Build.Step;
64
const print = std.debug.print;
75
const builtin = @import("builtin");
8-
const Pkg = std.build.Pkg;
6+
const Pkg = std.Build.Pkg;
97

10-
pub fn build(b: *Builder) !void {
8+
pub fn build(b: *std.Build) !void {
119
// Options.
1210
//const build_v8 = b.option(bool, "build_v8", "Whether to build from v8 source") orelse false;
1311
const path = b.option([]const u8, "path", "Path to main file, for: build, run") orelse "";
@@ -45,7 +43,7 @@ const UseGclient = false;
4543
// V8's build process is complex and porting it to zig could take quite awhile.
4644
// It would be nice if there was a way to import .gn files into the zig build system.
4745
// For now we just use gn/ninja like rusty_v8 does: https://github.com/denoland/rusty_v8/blob/main/build.rs
48-
fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mode, use_zig_tc: bool) !*std.build.Step {
46+
fn createV8_Build(b: *std.Build, target: std.Build.ResolvedTarget, mode: std.builtin.Mode, use_zig_tc: bool) !*std.Build.Step {
4947
const step = b.step("v8", "Build v8 c binding lib.");
5048

5149
var cp: *CopyFileStep = undefined;
@@ -65,19 +63,19 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
6563

6664
var gn_args = std.ArrayList([]const u8).init(b.allocator);
6765

68-
switch (target.getOsTag()) {
66+
switch (target.result.os.tag) {
6967
.macos => try gn_args.append("target_os=\"mac\""),
7068
.windows => {
7169
try gn_args.append("target_os=\"win\"");
7270
if (!UseGclient) {
7371
// Don't use depot_tools.
74-
try b.env_map.put("DEPOT_TOOLS_WIN_TOOLCHAIN", "0");
72+
try b.graph.env_map.put("DEPOT_TOOLS_WIN_TOOLCHAIN", "0");
7573
}
7674
},
7775
.linux => try gn_args.append("target_os=\"linux\""),
7876
else => {},
7977
}
80-
switch (target.getCpuArch()) {
78+
switch (target.result.cpu.arch) {
8179
.x86_64 => try gn_args.append("target_cpu=\"x64\""),
8280
.aarch64 => try gn_args.append("target_cpu=\"arm64\""),
8381
else => {},
@@ -133,8 +131,8 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
133131
if (use_zig_tc) {
134132
// Set target and cpu for building the lib.
135133
// TODO: If mcpu is equavalent to -Dcpu then use that instead
136-
try zig_cc.append(b.fmt("zig cc --target={s} -mcpu=baseline", .{try target.zigTriple(b.allocator)}));
137-
try zig_cxx.append(b.fmt("zig c++ --target={s} -mcpu=baseline", .{try target.zigTriple(b.allocator)}));
134+
try zig_cc.append(b.fmt("zig cc --target={s} -mcpu=baseline", .{try target.result.zigTriple(b.allocator)}));
135+
try zig_cxx.append(b.fmt("zig c++ --target={s} -mcpu=baseline", .{try target.result.zigTriple(b.allocator)}));
138136

139137
try host_zig_cc.append("zig cc --target=native");
140138
try host_zig_cxx.append("zig c++ --target=native");
@@ -149,11 +147,11 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
149147
try zig_cc.append("-mcrc32");
150148
try zig_cxx.append("-mcrc32");
151149

152-
if (target.getOsTag() == .windows and target.getAbi() == .gnu) {
150+
if (target.result.os.tag == .windows and target.result.abi == .gnu) {
153151
// V8 expects __declspec(dllexport) to not expand in it's test in src/base/export-template.h but it does when compiling with mingw.
154152
try zig_cxx.append("-DEXPORT_TEMPLATE_TEST_MSVC_HACK_DEFAULT\\(...\\)=true");
155153
}
156-
if (target.getOsTag() == .windows and builtin.os.tag != .windows) {
154+
if (target.result.os.tag == .windows and builtin.os.tag != .windows) {
157155
// Cross building to windows probably is case sensitive to header files, so provide them in include.
158156
// Note: Directory is relative to ninja build folder.
159157
try zig_cxx.append("-I../../../../cross-windows");
@@ -192,12 +190,12 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
192190
// custom_toolchain is how we can set zig as the cc/cxx compiler and linker.
193191
try gn_args.append("custom_toolchain=\"//zig:main_zig_toolchain\"");
194192

195-
if (target.getOsTag() == .linux and target.getCpuArch() == .x86_64) {
193+
if (target.result.os.tag == .linux and target.result.cpu.arch == .x86_64) {
196194
// Should add target flags that matches: //build/config/compiler:compiler_cpu_abi
197195
try zig_cc.append("-m64");
198196
try zig_cxx.append("-m64");
199-
} else if (target.getOsTag() == .macos) {
200-
if (!target.isNative()) {
197+
} else if (target.result.os.tag == .macos) {
198+
if (!target.query.isNative()) {
201199
// Cross compiling.
202200
const sysroot_abs = b.pathFromRoot("./cross-macos/sysroot/macos-12/usr/include");
203201
try zig_cc.append("-isystem");
@@ -206,7 +204,7 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
206204
try zig_cxx.append(sysroot_abs);
207205
}
208206
}
209-
if (builtin.cpu.arch != target.getCpuArch() or builtin.os.tag != target.getOsTag()) {
207+
if (builtin.cpu.arch != target.result.cpu.arch or builtin.os.tag != target.result.os.tag) {
210208
try gn_args.append("v8_snapshot_toolchain=\"//zig:v8_zig_toolchain\"");
211209
}
212210

@@ -231,7 +229,7 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
231229
if (builtin.os.tag != .windows) {
232230
try gn_args.append("cxx_use_ld=\"lld\"");
233231
}
234-
if (target.getOsTag() == .linux and target.getCpuArch() == .aarch64) {
232+
if (target.result.os.tag == .linux and target.result.cpu.arch == .aarch64) {
235233
// On linux aarch64, we can not use the clang version provided in v8 sources
236234
// as it's built for x86_64 (TODO: using Rosetta2 for Linux VM on Apple Sillicon?)
237235
// Instead we can use a clang system version
@@ -245,13 +243,13 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
245243

246244
// sccache, currently does not work with zig cc
247245
if (!use_zig_tc) {
248-
if (b.env_map.get("SCCACHE")) |path| {
246+
if (b.graph.env_map.get("SCCACHE")) |path| {
249247
const cc_wrapper = try std.fmt.allocPrint(b.allocator, "cc_wrapper=\"{s}\"", .{path});
250248
try gn_args.append(cc_wrapper);
251249
} else {
252250
if (builtin.os.tag == .windows) {
253251
// findProgram look for "PATH" case sensitive.
254-
try b.env_map.put("PATH", b.env_map.get("Path") orelse "");
252+
try b.graph.env_map.put("PATH", b.graph.env_map.get("Path") orelse "");
255253
}
256254
if (b.findProgram(&.{"sccache"}, &.{})) |_| {
257255
const cc_wrapper = try std.fmt.allocPrint(b.allocator, "cc_wrapper=\"{s}\"", .{"sccache"});
@@ -264,7 +262,7 @@ fn createV8_Build(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mo
264262
if (builtin.os.tag == .windows) {
265263
// After creating PATH for windows so findProgram can find sccache, we need to delete it
266264
// or a gn tool (build/toolchain/win/setup_toolchain.py) will complain about not finding cl.exe.
267-
b.env_map.remove("PATH");
265+
b.graph.env_map.remove("PATH");
268266
}
269267
}
270268
}
@@ -313,20 +311,20 @@ fn getArchOs(alloc: std.mem.Allocator, arch: std.Target.Cpu.Arch, os: std.Target
313311
return std.fmt.allocPrint(alloc, "{s}-{s}-gnu", .{ @tagName(arch), @tagName(os) }) catch unreachable;
314312
}
315313

316-
fn getTargetId(alloc: std.mem.Allocator, target: std.zig.CrossTarget) []const u8 {
317-
return std.fmt.allocPrint(alloc, "{s}-{s}", .{ @tagName(target.getCpuArch()), @tagName(target.getOsTag()) }) catch unreachable;
314+
fn getTargetId(alloc: std.mem.Allocator, target: std.Build.ResolvedTarget) []const u8 {
315+
return std.fmt.allocPrint(alloc, "{s}-{s}", .{ @tagName(target.result.cpu.arch), @tagName(target.result.os.tag) }) catch unreachable;
318316
}
319317

320318
const CheckV8DepsStep = struct {
321319
const Self = @This();
322320

323321
step: Step,
324-
b: *Builder,
322+
b: *std.Build,
325323

326-
fn create(b: *Builder) *Self {
324+
fn create(b: *std.Build) *Self {
327325
const step = b.allocator.create(Self) catch unreachable;
328326
step.* = .{
329-
.step = std.build.Step.init(.{
327+
.step = std.Build.Step.init(.{
330328
.id = .custom,
331329
.name = "check_v8_deps",
332330
.makeFn = make,
@@ -353,7 +351,7 @@ const CheckV8DepsStep = struct {
353351
}
354352
};
355353

356-
fn createGetV8(b: *Builder) *std.build.Step {
354+
fn createGetV8(b: *std.Build) *std.Build.Step {
357355
const step = b.step("get-v8", "Gets v8 source using gclient.");
358356
if (UseGclient) {
359357
const mkpath = MakePathStep.create(b, "./gclient");
@@ -371,7 +369,7 @@ fn createGetV8(b: *Builder) *std.build.Step {
371369
return step;
372370
}
373371

374-
fn createGetTools(b: *Builder) *std.build.Step {
372+
fn createGetTools(b: *std.Build) *std.Build.Step {
375373
const step = b.step("get-tools", "Gets the build tools.");
376374

377375
var sub_step = b.addSystemCommand(&.{ "python3", "./tools/get_ninja_gn_binaries.py", "--dir", "./tools" });
@@ -386,7 +384,7 @@ fn createGetTools(b: *Builder) *std.build.Step {
386384
return step;
387385
}
388386

389-
fn getNinjaPath(b: *Builder) []const u8 {
387+
fn getNinjaPath(b: *std.Build) []const u8 {
390388
const os = switch (builtin.os.tag) {
391389
.windows => "windows",
392390
.linux => "linux",
@@ -404,7 +402,7 @@ fn getNinjaPath(b: *Builder) []const u8 {
404402
return std.fs.path.resolve(b.allocator, &.{ "./tools/ninja_gn_binaries-20221218", platform, bin }) catch unreachable;
405403
}
406404

407-
fn getGnPath(b: *Builder) []const u8 {
405+
fn getGnPath(b: *std.Build) []const u8 {
408406
const os = switch (builtin.os.tag) {
409407
.windows => "windows",
410408
.linux => "linux",
@@ -425,14 +423,14 @@ fn getGnPath(b: *Builder) []const u8 {
425423
const MakePathStep = struct {
426424
const Self = @This();
427425

428-
step: std.build.Step,
429-
b: *Builder,
426+
step: std.Build.Step,
427+
b: *std.Build,
430428
path: []const u8,
431429

432-
fn create(b: *Builder, root_path: []const u8) *Self {
430+
fn create(b: *std.Build, root_path: []const u8) *Self {
433431
const new = b.allocator.create(Self) catch unreachable;
434432
new.* = .{
435-
.step = std.build.Step.init(.{
433+
.step = std.Build.Step.init(.{
436434
.id = .custom,
437435
.name = b.fmt("make-path", .{}),
438436
.makeFn = make,
@@ -454,15 +452,15 @@ const MakePathStep = struct {
454452
const CopyFileStep = struct {
455453
const Self = @This();
456454

457-
step: std.build.Step,
458-
b: *Builder,
455+
step: std.Build.Step,
456+
b: *std.Build,
459457
src_path: []const u8,
460458
dst_path: []const u8,
461459

462-
fn create(b: *Builder, src_path: []const u8, dst_path: []const u8) *Self {
460+
fn create(b: *std.Build, src_path: []const u8, dst_path: []const u8) *Self {
463461
const new = b.allocator.create(Self) catch unreachable;
464462
new.* = .{
465-
.step = std.build.Step.init(.{
463+
.step = std.Build.Step.init(.{
466464
.id = .custom,
467465
.name = b.fmt("cp", .{}),
468466
.makeFn = make,
@@ -483,12 +481,12 @@ const CopyFileStep = struct {
483481
};
484482

485483
// TODO: Make this usable from external project.
486-
fn linkV8(b: *Builder, step: *std.build.CompileStep, use_zig_tc: bool) void {
487-
const mode = step.optimize;
488-
const target = step.target;
484+
fn linkV8(b: *std.Build, step: *std.Build.Step.Compile, use_zig_tc: bool) void {
485+
const mode = step.root_module.optimize;
486+
const target = step.root_module.resolved_target.?;
489487

490488
const mode_str: []const u8 = if (mode == .Debug) "debug" else "release";
491-
const lib: []const u8 = if (target.getOsTag() == .windows and target.getAbi() == .msvc) "c_v8.lib" else "libc_v8.a";
489+
const lib: []const u8 = if (target.result.os.tag == .windows and target.result.abi == .msvc) "c_v8.lib" else "libc_v8.a";
492490
const lib_path = std.fmt.allocPrint(b.allocator, "./v8-build/{s}/{s}/ninja/obj/zig/{s}", .{
493491
getTargetId(b.allocator, target),
494492
mode_str,
@@ -501,8 +499,8 @@ fn linkV8(b: *Builder, step: *std.build.CompileStep, use_zig_tc: bool) void {
501499
step.linkLibCpp();
502500
}
503501
step.linkSystemLibrary("unwind");
504-
} else if (target.getOsTag() == .windows) {
505-
if (target.getAbi() == .gnu) {
502+
} else if (target.result.os.tag == .windows) {
503+
if (target.result.abi == .gnu) {
506504
step.linkLibCpp();
507505
} else {
508506
step.linkSystemLibrary("Dbghelp");
@@ -518,10 +516,9 @@ fn linkV8(b: *Builder, step: *std.build.CompileStep, use_zig_tc: bool) void {
518516
}
519517
}
520518

521-
fn createTest(b: *Builder, target: std.zig.CrossTarget, mode: std.builtin.Mode, use_zig_tc: bool) *std.build.CompileStep {
519+
fn createTest(b: *std.Build, target: std.Build.ResolvedTarget, mode: std.builtin.Mode, use_zig_tc: bool) *std.Build.Step.Compile {
522520
const step = b.addTest(.{
523-
.root_source_file = .{ .path = "./test/test.zig" },
524-
.main_pkg_path = .{ .path = "." },
521+
.root_source_file = .{ .path = "./src/test.zig" },
525522
.target = target,
526523
.optimize = mode,
527524
.link_libc = true,
@@ -544,12 +541,12 @@ const DepEntry = struct {
544541
}
545542
};
546543

547-
fn getV8Rev(b: *Builder) ![]const u8 {
544+
fn getV8Rev(b: *std.Build) ![]const u8 {
548545
var file: std.fs.File = undefined;
549546
if (comptime isMinZigVersion()) {
550-
file = try std.fs.openFileAbsolute(b.pathFromRoot("V8_REVISION"), .{ .read = true, .write = false });
547+
file = try std.fs.openFileAbsolute(b.pathFromRoot("V8_REVISION"), .{ .mode = .read_only });
551548
} else {
552-
file = try std.fs.openFileAbsolute(b.pathFromRoot("V8_REVISION"), .{ .mode = std.fs.File.OpenMode.read_write });
549+
file = try std.fs.openFileAbsolute(b.pathFromRoot("V8_REVISION"), .{ .mode = .read_only });
553550
}
554551
defer file.close();
555552
return std.mem.trim(u8, try file.readToEndAlloc(b.allocator, 1e9), "\n\r ");
@@ -559,13 +556,13 @@ pub const GetV8SourceStep = struct {
559556
const Self = @This();
560557

561558
step: Step,
562-
b: *Builder,
559+
b: *std.Build,
563560

564-
pub fn create(b: *Builder) *Self {
561+
pub fn create(b: *std.Build) *Self {
565562
const self = b.allocator.create(Self) catch unreachable;
566563
self.* = .{
567564
.b = b,
568-
.step = std.build.Step.init(.{
565+
.step = std.Build.Step.init(.{
569566
.id = .run,
570567
.name = "Get V8 Sources.",
571568
.makeFn = make,
@@ -736,7 +733,7 @@ pub const GetV8SourceStep = struct {
736733
}
737734
};
738735

739-
fn createCompileStep(b: *Builder, path: []const u8, target: std.zig.CrossTarget, mode: std.builtin.Mode, use_zig_tc: bool) *CompileStep {
736+
fn createCompileStep(b: *std.Build, path: []const u8, target: std.Build.ResolvedTarget, mode: std.builtin.Mode, use_zig_tc: bool) *std.Build.Step.Compile {
740737
const basename = std.fs.path.basename(path);
741738
const i = std.mem.indexOf(u8, basename, ".zig") orelse basename.len;
742739
const name = basename[0..i];
@@ -751,7 +748,7 @@ fn createCompileStep(b: *Builder, path: []const u8, target: std.zig.CrossTarget,
751748
step.addIncludePath(.{ .path = "src" });
752749

753750
if (mode == .ReleaseSafe) {
754-
step.strip = true;
751+
step.root_module.strip = true;
755752
}
756753

757754
linkV8(b, step, use_zig_tc);
@@ -767,11 +764,11 @@ const PathStat = enum {
767764
Unknown,
768765
};
769766

770-
fn statPathFromRoot(b: *Builder, path_rel: []const u8) !PathStat {
767+
fn statPathFromRoot(b: *std.Build, path_rel: []const u8) !PathStat {
771768
const path_abs = b.pathFromRoot(path_rel);
772769
var file: std.fs.File = undefined;
773770
if (comptime isMinZigVersion()) {
774-
file = std.fs.openFileAbsolute(path_abs, .{ .read = false, .write = false }) catch |err| {
771+
file = std.fs.openFileAbsolute(path_abs, .{ .mode = .read_only }) catch |err| {
775772
if (err == error.FileNotFound) {
776773
return .NotExist;
777774
} else if (err == error.IsDir) {
@@ -781,7 +778,7 @@ fn statPathFromRoot(b: *Builder, path_rel: []const u8) !PathStat {
781778
}
782779
};
783780
} else {
784-
file = std.fs.openFileAbsolute(path_abs, .{ .mode = std.fs.File.OpenMode.read_only }) catch |err| {
781+
file = std.fs.openFileAbsolute(path_abs, .{ .mode = .read_only }) catch |err| {
785782
if (err == error.FileNotFound) {
786783
return .NotExist;
787784
} else if (err == error.IsDir) {
@@ -803,5 +800,5 @@ fn statPathFromRoot(b: *Builder, path_rel: []const u8) !PathStat {
803800
}
804801

805802
fn isMinZigVersion() bool {
806-
return builtin.zig_version.major == 0 and builtin.zig_version.minor == 9;
803+
return builtin.zig_version.major == 0 and builtin.zig_version.minor == 12;
807804
}

test/test.zig renamed to src/test.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const t = std.testing;
3-
const v8 = @import("../src/v8.zig");
3+
const v8 = @import("v8.zig");
44

55
test {
66
// Based on https://chromium.googlesource.com/v8/v8/+/branch-heads/6.8/samples/hello-world.cc

0 commit comments

Comments
 (0)