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(
3030 const graph = maker.graph;
3131 const step = maker.stepByIndex(step_index);
3232 compile.zig_args.clearRetainingCapacity();
33 if (true) @panic("TODO implement compile.make()");
3433 try lowerZigArgs(compile, step_index, maker, &compile.zig_args, false);
34 if (true) @panic("TODO implement compile.make()");
3535 const process_arena = graph.arena; // TODO don't leak into the process_arena
3636
3737 const maybe_output_dir = step.evalZigProcess(
......@@ -92,10 +92,13 @@ fn lowerZigArgs(
9292 const graph = maker.graph;
9393 const arena = graph.arena; // TODO don't leak into the process arena
9494 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
9699 try zig_args.append(gpa, graph.zig_exe);
97100
98 const cmd = switch (compile.kind) {
101 const cmd = switch (conf_comp.flags3.kind) {
99102 .lib => "build-lib",
100103 .exe => "build-exe",
101104 .obj => "build-obj",
......@@ -107,25 +110,32 @@ fn lowerZigArgs(
107110 if (graph.reference_trace) |some| {
108111 try zig_args.append(gpa, try allocPrint(arena, "-freference-trace={d}", .{some}));
109112 }
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);
113 try addFlag(&zig_args, "lld", compile.use_lld);
114 try addFlag(&zig_args, "new-linker", compile.use_new_linker);
119 const root_module = conf_comp.root_module.get(conf);
115120
116 if (compile.root_module.resolved_target.?.query.ofmt) |ofmt| {
117 try zig_args.append(gpa, try allocPrint(arena, "-ofmt={t}", .{ofmt}));
121 if (root_module.resolved_target.get(conf).?.query.unwrap()) |query| {
122 if (query.get(conf).flags.object_format.get()) |ofmt| {
123 try zig_args.append(gpa, try allocPrint(arena, "-ofmt={t}", .{ofmt}));
124 }
118125 }
119126
120 switch (compile.entry) {
127 switch (conf_comp.flags3.entry) {
121128 .default => {},
122129 .disabled => try zig_args.append(gpa, "-fno-entry"),
123130 .enabled => try zig_args.append(gpa, "-fentry"),
124 .symbol_name => |entry_name| {
125 try zig_args.append(gpa, try allocPrint(arena, "-fentry={s}", .{entry_name}));
131 .symbol_name => {
132 const symbol_name = conf_comp.entry.value.?.slice(conf);
133 try zig_args.append(gpa, try allocPrint(arena, "-fentry={s}", .{symbol_name}));
126134 },
127135 }
128136
137 if (true) @panic("TODO");
138
129139 {
130140 for (compile.force_undefined_symbols.keys()) |symbol_name| {
131141 try zig_args.append(gpa, "--force_undefined");
......@@ -408,7 +418,7 @@ fn lowerZigArgs(
408418 if (!my_responsibility) continue;
409419 if (cli_named_modules.modules.getIndex(mod)) |module_cli_index| {
410420 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
413423 // --dep arguments
414424 try zig_args.ensureUnusedCapacity(mod.import_table.count() * 2);
......@@ -507,7 +517,7 @@ fn lowerZigArgs(
507517 if (compile.generated_llvm_ir != null) try zig_args.append(gpa, "-femit-llvm-ir");
508518 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
512522 switch (compile.compress_debug_sections) {
513523 .none => {},
......@@ -612,9 +622,9 @@ fn lowerZigArgs(
612622 try zig_args.append(gpa, "--discard-all");
613623 }
614624
615 try addFlag(&zig_args, "compiler-rt", compile.bundle_compiler_rt);
616 try addFlag(&zig_args, "ubsan-rt", compile.bundle_ubsan_rt);
617 try addFlag(&zig_args, "dll-export-fns", compile.dll_export_fns);
625 try addFlag(gpa, zig_args, "compiler-rt", compile.bundle_compiler_rt);
626 try addFlag(gpa, zig_args, "ubsan-rt", compile.bundle_ubsan_rt);
627 try addFlag(gpa, zig_args, "dll-export-fns", compile.dll_export_fns);
618628 if (compile.rdynamic) {
619629 try zig_args.append(gpa, "-rdynamic");
620630 }
......@@ -718,7 +728,7 @@ fn lowerZigArgs(
718728 try zig_args.appendSlice(gpa, &.{ "-rcincludes", @tagName(compile.rc_includes) });
719729 }
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
723733 if (compile.build_id orelse graph.build_id) |build_id| {
724734 try zig_args.append(gpa, switch (build_id) {
......@@ -739,7 +749,7 @@ fn lowerZigArgs(
739749 try zig_args.append(gpa, zig_lib_dir);
740750 }
741751
742 try addFlag(&zig_args, "PIE", compile.pie);
752 try addFlag(gpa, zig_args, "PIE", compile.pie);
743753
744754 if (compile.lto) |lto| {
745755 try zig_args.append(gpa, switch (lto) {
......@@ -749,7 +759,7 @@ fn lowerZigArgs(
749759 });
750760 }
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
754764 if (compile.subsystem) |subsystem| {
755765 try zig_args.appendSlice(gpa, &.{ "--subsystem", @tagName(subsystem) });
......@@ -763,7 +773,7 @@ fn lowerZigArgs(
763773 "--error-limit", try allocPrint(arena, "{d}", .{err_limit}),
764774 });
765775
766 try addFlag(&zig_args, "incremental", graph.incremental);
776 try addFlag(gpa, zig_args, "incremental", graph.incremental);
767777
768778 try zig_args.append(gpa, "--listen=-");
769779
lib/std/zig/Configuration.zig+55-1
......@@ -1147,6 +1147,10 @@ pub const Module = struct {
11471147
11481148 pub const Index = enum(u32) {
11491149 _,
1150
1151 pub fn get(this: @This(), c: *const Configuration) Module {
1152 return extraData(c, Module, @intFromEnum(this));
1153 }
11501154 };
11511155
11521156 pub const Flags = packed struct(u32) {
......@@ -1318,6 +1322,14 @@ pub const DefaultingBool = enum(u2) {
13181322 true => .true,
13191323 };
13201324 }
1325
1326 pub fn toBool(db: DefaultingBool) ?bool {
1327 return switch (db) {
1328 .false => false,
1329 .true => true,
1330 .default => null,
1331 };
1332 }
13211333};
13221334
13231335pub const SystemLib = struct {
......@@ -1431,11 +1443,26 @@ pub const ResolvedTarget = struct {
14311443
14321444 pub const Index = enum(u32) {
14331445 _,
1446
1447 pub fn get(this: @This(), c: *const Configuration) ?ResolvedTarget {
1448 return extraData(c, ResolvedTarget, @intFromEnum(this));
1449 }
14341450 };
14351451
14361452 pub const OptionalIndex = enum(u32) {
14371453 none = maxInt(u32),
14381454 _,
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 }
14391466 };
14401467};
14411468
......@@ -1468,6 +1495,10 @@ pub const TargetQuery = struct {
14681495 pub fn length(i: Index, extra: []const u32) usize {
14691496 return Storage.dataLength(extra, @intFromEnum(i), TargetQuery);
14701497 }
1498
1499 pub fn get(this: @This(), c: *const Configuration) TargetQuery {
1500 return extraData(c, TargetQuery, @intFromEnum(this));
1501 }
14711502 };
14721503
14731504 pub const OptionalIndex = enum(u32) {
......@@ -1479,6 +1510,13 @@ pub const TargetQuery = struct {
14791510 assert(result != .none);
14801511 return result;
14811512 }
1513
1514 pub fn unwrap(this: @This()) ?Index {
1515 return switch (this) {
1516 .none => null,
1517 _ => @enumFromInt(@intFromEnum(this)),
1518 };
1519 }
14821520 };
14831521
14841522 pub const CpuModel = enum(u2) {
......@@ -1680,6 +1718,22 @@ pub const TargetQuery = struct {
16801718 // TODO comptime assert the enums match
16811719 return @enumFromInt(@intFromEnum(x orelse return .default));
16821720 }
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 }
16831737 };
16841738
16851739 pub const Flags = packed struct(u32) {
......@@ -1933,7 +1987,7 @@ pub const Storage = enum {
19331987 }
19341988 const end = meta_start + Field.extraLen(len);
19351989 i.* = end;
1936 return .{ .data = end - len, .len = len };
1990 return .{ .data = @ptrFromInt(end - len), .len = len };
19371991 },
19381992 },
19391993 },