authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-26 20:33:59-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:34-07:00
logd3d3fb8473e25273ddca2e0da8d9c76cc43c794b
treee1be1dc85fafe9beae664f59bfc3ff8e008c7991
parent6925a57d2fd4e4b1330d980cbfb84f7007f117ef

maker: progress towards updating zig CLI lowering


2 files changed, 85 insertions(+), 21 deletions(-)

lib/compiler/Maker/Step/Compile.zig+30-20
...@@ -30,8 +30,8 @@ pub fn make(...@@ -30,8 +30,8 @@ pub fn make(
30 const graph = maker.graph;30 const graph = maker.graph;
31 const step = maker.stepByIndex(step_index);31 const step = maker.stepByIndex(step_index);
32 compile.zig_args.clearRetainingCapacity();32 compile.zig_args.clearRetainingCapacity();
33 if (true) @panic("TODO implement compile.make()");
34 try lowerZigArgs(compile, step_index, maker, &compile.zig_args, false);33 try lowerZigArgs(compile, step_index, maker, &compile.zig_args, false);
34 if (true) @panic("TODO implement compile.make()");
35 const process_arena = graph.arena; // TODO don't leak into the process_arena35 const process_arena = graph.arena; // TODO don't leak into the process_arena
3636
37 const maybe_output_dir = step.evalZigProcess(37 const maybe_output_dir = step.evalZigProcess(
...@@ -92,10 +92,13 @@ fn lowerZigArgs(...@@ -92,10 +92,13 @@ fn lowerZigArgs(
92 const graph = maker.graph;92 const graph = maker.graph;
93 const arena = graph.arena; // TODO don't leak into the process arena93 const arena = graph.arena; // TODO don't leak into the process arena
94 const gpa = maker.gpa;94 const gpa = maker.gpa;
95 const conf = &maker.scanned_config.configuration;
96 const conf_step = step_index.ptr(conf);
97 const conf_comp = conf_step.extended.get(conf.extra).compile;
9598
96 try zig_args.append(gpa, graph.zig_exe);99 try zig_args.append(gpa, graph.zig_exe);
97100
98 const cmd = switch (compile.kind) {101 const cmd = switch (conf_comp.flags3.kind) {
99 .lib => "build-lib",102 .lib => "build-lib",
100 .exe => "build-exe",103 .exe => "build-exe",
101 .obj => "build-obj",104 .obj => "build-obj",
...@@ -107,25 +110,32 @@ fn lowerZigArgs(...@@ -107,25 +110,32 @@ fn lowerZigArgs(
107 if (graph.reference_trace) |some| {110 if (graph.reference_trace) |some| {
108 try zig_args.append(gpa, try allocPrint(arena, "-freference-trace={d}", .{some}));111 try zig_args.append(gpa, try allocPrint(arena, "-freference-trace={d}", .{some}));
109 }112 }
110 try addFlag(&zig_args, "allow-so-scripts", compile.allow_so_scripts orelse graph.allow_so_scripts);113 try addFlag(gpa, zig_args, "allow-so-scripts", conf_comp.flags2.allow_so_scripts.toBool() orelse graph.allow_so_scripts);
114
115 try addFlag(gpa, zig_args, "llvm", conf_comp.flags2.use_llvm.toBool());
116 try addFlag(gpa, zig_args, "lld", conf_comp.flags2.use_lld.toBool());
117 try addFlag(gpa, zig_args, "new-linker", conf_comp.flags2.use_new_linker.toBool());
111118
112 try addFlag(&zig_args, "llvm", compile.use_llvm);119 const root_module = conf_comp.root_module.get(conf);
113 try addFlag(&zig_args, "lld", compile.use_lld);
114 try addFlag(&zig_args, "new-linker", compile.use_new_linker);
115120
116 if (compile.root_module.resolved_target.?.query.ofmt) |ofmt| {121 if (root_module.resolved_target.get(conf).?.query.unwrap()) |query| {
117 try zig_args.append(gpa, try allocPrint(arena, "-ofmt={t}", .{ofmt}));122 if (query.get(conf).flags.object_format.get()) |ofmt| {
123 try zig_args.append(gpa, try allocPrint(arena, "-ofmt={t}", .{ofmt}));
124 }
118 }125 }
119126
120 switch (compile.entry) {127 switch (conf_comp.flags3.entry) {
121 .default => {},128 .default => {},
122 .disabled => try zig_args.append(gpa, "-fno-entry"),129 .disabled => try zig_args.append(gpa, "-fno-entry"),
123 .enabled => try zig_args.append(gpa, "-fentry"),130 .enabled => try zig_args.append(gpa, "-fentry"),
124 .symbol_name => |entry_name| {131 .symbol_name => {
125 try zig_args.append(gpa, try allocPrint(arena, "-fentry={s}", .{entry_name}));132 const symbol_name = conf_comp.entry.value.?.slice(conf);
133 try zig_args.append(gpa, try allocPrint(arena, "-fentry={s}", .{symbol_name}));
126 },134 },
127 }135 }
128136
137 if (true) @panic("TODO");
138
129 {139 {
130 for (compile.force_undefined_symbols.keys()) |symbol_name| {140 for (compile.force_undefined_symbols.keys()) |symbol_name| {
131 try zig_args.append(gpa, "--force_undefined");141 try zig_args.append(gpa, "--force_undefined");
...@@ -408,7 +418,7 @@ fn lowerZigArgs(...@@ -408,7 +418,7 @@ fn lowerZigArgs(
408 if (!my_responsibility) continue;418 if (!my_responsibility) continue;
409 if (cli_named_modules.modules.getIndex(mod)) |module_cli_index| {419 if (cli_named_modules.modules.getIndex(mod)) |module_cli_index| {
410 const module_cli_name = cli_named_modules.names.keys()[module_cli_index];420 const module_cli_name = cli_named_modules.names.keys()[module_cli_index];
411 try mod.appendZigProcessFlags(&zig_args, step);421 try mod.appendZigProcessFlags(zig_args, step);
412422
413 // --dep arguments423 // --dep arguments
414 try zig_args.ensureUnusedCapacity(mod.import_table.count() * 2);424 try zig_args.ensureUnusedCapacity(mod.import_table.count() * 2);
...@@ -507,7 +517,7 @@ fn lowerZigArgs(...@@ -507,7 +517,7 @@ fn lowerZigArgs(
507 if (compile.generated_llvm_ir != null) try zig_args.append(gpa, "-femit-llvm-ir");517 if (compile.generated_llvm_ir != null) try zig_args.append(gpa, "-femit-llvm-ir");
508 if (compile.generated_h != null) try zig_args.append(gpa, "-femit-h");518 if (compile.generated_h != null) try zig_args.append(gpa, "-femit-h");
509519
510 try addFlag(&zig_args, "formatted-panics", compile.formatted_panics);520 try addFlag(gpa, zig_args, "formatted-panics", compile.formatted_panics);
511521
512 switch (compile.compress_debug_sections) {522 switch (compile.compress_debug_sections) {
513 .none => {},523 .none => {},
...@@ -612,9 +622,9 @@ fn lowerZigArgs(...@@ -612,9 +622,9 @@ fn lowerZigArgs(
612 try zig_args.append(gpa, "--discard-all");622 try zig_args.append(gpa, "--discard-all");
613 }623 }
614624
615 try addFlag(&zig_args, "compiler-rt", compile.bundle_compiler_rt);625 try addFlag(gpa, zig_args, "compiler-rt", compile.bundle_compiler_rt);
616 try addFlag(&zig_args, "ubsan-rt", compile.bundle_ubsan_rt);626 try addFlag(gpa, zig_args, "ubsan-rt", compile.bundle_ubsan_rt);
617 try addFlag(&zig_args, "dll-export-fns", compile.dll_export_fns);627 try addFlag(gpa, zig_args, "dll-export-fns", compile.dll_export_fns);
618 if (compile.rdynamic) {628 if (compile.rdynamic) {
619 try zig_args.append(gpa, "-rdynamic");629 try zig_args.append(gpa, "-rdynamic");
620 }630 }
...@@ -718,7 +728,7 @@ fn lowerZigArgs(...@@ -718,7 +728,7 @@ fn lowerZigArgs(
718 try zig_args.appendSlice(gpa, &.{ "-rcincludes", @tagName(compile.rc_includes) });728 try zig_args.appendSlice(gpa, &.{ "-rcincludes", @tagName(compile.rc_includes) });
719 }729 }
720730
721 try addFlag(&zig_args, "each-lib-rpath", compile.each_lib_rpath);731 try addFlag(gpa, zig_args, "each-lib-rpath", compile.each_lib_rpath);
722732
723 if (compile.build_id orelse graph.build_id) |build_id| {733 if (compile.build_id orelse graph.build_id) |build_id| {
724 try zig_args.append(gpa, switch (build_id) {734 try zig_args.append(gpa, switch (build_id) {
...@@ -739,7 +749,7 @@ fn lowerZigArgs(...@@ -739,7 +749,7 @@ fn lowerZigArgs(
739 try zig_args.append(gpa, zig_lib_dir);749 try zig_args.append(gpa, zig_lib_dir);
740 }750 }
741751
742 try addFlag(&zig_args, "PIE", compile.pie);752 try addFlag(gpa, zig_args, "PIE", compile.pie);
743753
744 if (compile.lto) |lto| {754 if (compile.lto) |lto| {
745 try zig_args.append(gpa, switch (lto) {755 try zig_args.append(gpa, switch (lto) {
...@@ -749,7 +759,7 @@ fn lowerZigArgs(...@@ -749,7 +759,7 @@ fn lowerZigArgs(
749 });759 });
750 }760 }
751761
752 try addFlag(&zig_args, "sanitize-coverage-trace-pc-guard", compile.sanitize_coverage_trace_pc_guard);762 try addFlag(gpa, zig_args, "sanitize-coverage-trace-pc-guard", compile.sanitize_coverage_trace_pc_guard);
753763
754 if (compile.subsystem) |subsystem| {764 if (compile.subsystem) |subsystem| {
755 try zig_args.appendSlice(gpa, &.{ "--subsystem", @tagName(subsystem) });765 try zig_args.appendSlice(gpa, &.{ "--subsystem", @tagName(subsystem) });
...@@ -763,7 +773,7 @@ fn lowerZigArgs(...@@ -763,7 +773,7 @@ fn lowerZigArgs(
763 "--error-limit", try allocPrint(arena, "{d}", .{err_limit}),773 "--error-limit", try allocPrint(arena, "{d}", .{err_limit}),
764 });774 });
765775
766 try addFlag(&zig_args, "incremental", graph.incremental);776 try addFlag(gpa, zig_args, "incremental", graph.incremental);
767777
768 try zig_args.append(gpa, "--listen=-");778 try zig_args.append(gpa, "--listen=-");
769779
lib/std/zig/Configuration.zig+55-1
...@@ -1147,6 +1147,10 @@ pub const Module = struct {...@@ -1147,6 +1147,10 @@ pub const Module = struct {
11471147
1148 pub const Index = enum(u32) {1148 pub const Index = enum(u32) {
1149 _,1149 _,
1150
1151 pub fn get(this: @This(), c: *const Configuration) Module {
1152 return extraData(c, Module, @intFromEnum(this));
1153 }
1150 };1154 };
11511155
1152 pub const Flags = packed struct(u32) {1156 pub const Flags = packed struct(u32) {
...@@ -1318,6 +1322,14 @@ pub const DefaultingBool = enum(u2) {...@@ -1318,6 +1322,14 @@ pub const DefaultingBool = enum(u2) {
1318 true => .true,1322 true => .true,
1319 };1323 };
1320 }1324 }
1325
1326 pub fn toBool(db: DefaultingBool) ?bool {
1327 return switch (db) {
1328 .false => false,
1329 .true => true,
1330 .default => null,
1331 };
1332 }
1321};1333};
13221334
1323pub const SystemLib = struct {1335pub const SystemLib = struct {
...@@ -1431,11 +1443,26 @@ pub const ResolvedTarget = struct {...@@ -1431,11 +1443,26 @@ pub const ResolvedTarget = struct {
14311443
1432 pub const Index = enum(u32) {1444 pub const Index = enum(u32) {
1433 _,1445 _,
1446
1447 pub fn get(this: @This(), c: *const Configuration) ?ResolvedTarget {
1448 return extraData(c, ResolvedTarget, @intFromEnum(this));
1449 }
1434 };1450 };
14351451
1436 pub const OptionalIndex = enum(u32) {1452 pub const OptionalIndex = enum(u32) {
1437 none = maxInt(u32),1453 none = maxInt(u32),
1438 _,1454 _,
1455
1456 pub fn unwrap(this: @This()) ?Index {
1457 return switch (this) {
1458 .none => null,
1459 _ => @enumFromInt(@intFromEnum(this)),
1460 };
1461 }
1462
1463 pub fn get(this: @This(), c: *const Configuration) ?ResolvedTarget {
1464 return (unwrap(this) orelse return null).get(c);
1465 }
1439 };1466 };
1440};1467};
14411468
...@@ -1468,6 +1495,10 @@ pub const TargetQuery = struct {...@@ -1468,6 +1495,10 @@ pub const TargetQuery = struct {
1468 pub fn length(i: Index, extra: []const u32) usize {1495 pub fn length(i: Index, extra: []const u32) usize {
1469 return Storage.dataLength(extra, @intFromEnum(i), TargetQuery);1496 return Storage.dataLength(extra, @intFromEnum(i), TargetQuery);
1470 }1497 }
1498
1499 pub fn get(this: @This(), c: *const Configuration) TargetQuery {
1500 return extraData(c, TargetQuery, @intFromEnum(this));
1501 }
1471 };1502 };
14721503
1473 pub const OptionalIndex = enum(u32) {1504 pub const OptionalIndex = enum(u32) {
...@@ -1479,6 +1510,13 @@ pub const TargetQuery = struct {...@@ -1479,6 +1510,13 @@ pub const TargetQuery = struct {
1479 assert(result != .none);1510 assert(result != .none);
1480 return result;1511 return result;
1481 }1512 }
1513
1514 pub fn unwrap(this: @This()) ?Index {
1515 return switch (this) {
1516 .none => null,
1517 _ => @enumFromInt(@intFromEnum(this)),
1518 };
1519 }
1482 };1520 };
14831521
1484 pub const CpuModel = enum(u2) {1522 pub const CpuModel = enum(u2) {
...@@ -1680,6 +1718,22 @@ pub const TargetQuery = struct {...@@ -1680,6 +1718,22 @@ pub const TargetQuery = struct {
1680 // TODO comptime assert the enums match1718 // TODO comptime assert the enums match
1681 return @enumFromInt(@intFromEnum(x orelse return .default));1719 return @enumFromInt(@intFromEnum(x orelse return .default));
1682 }1720 }
1721
1722 pub fn get(this: @This()) ?std.Target.ObjectFormat {
1723 return switch (this) {
1724 .c => .c,
1725 .coff => .coff,
1726 .elf => .elf,
1727 .hex => .hex,
1728 .macho => .macho,
1729 .plan9 => .plan9,
1730 .raw => .raw,
1731 .spirv => .spirv,
1732 .wasm => .wasm,
1733
1734 .default => null,
1735 };
1736 }
1683 };1737 };
16841738
1685 pub const Flags = packed struct(u32) {1739 pub const Flags = packed struct(u32) {
...@@ -1933,7 +1987,7 @@ pub const Storage = enum {...@@ -1933,7 +1987,7 @@ pub const Storage = enum {
1933 }1987 }
1934 const end = meta_start + Field.extraLen(len);1988 const end = meta_start + Field.extraLen(len);
1935 i.* = end;1989 i.* = end;
1936 return .{ .data = end - len, .len = len };1990 return .{ .data = @ptrFromInt(end - len), .len = len };
1937 },1991 },
1938 },1992 },
1939 },1993 },