commit eaab079bd6a5a0e76cc6361957212c782d83846a (tree)
parent 149e611688b012b6f99e0c07a0f5eba22f7cc8b0
Author: Motiejus Jakštys <motiejus@jakstys.lt>
Date: Thu, 19 Feb 2026 20:58:30 +0000
always build with valgrind
Diffstat:
3 files changed, 13 insertions(+), 148 deletions(-)
diff --git a/build.zig b/build.zig
@@ -662,7 +662,6 @@ pub fn build(b: *std.Build) !void {
const zig0_valgrind = valgrind orelse false;
const zig0_target = blk: {
- if (!zig0_valgrind) break :blk target;
var query = target.query;
const arch = query.cpu_arch orelse @import("builtin").cpu.arch;
if (arch == .x86_64) {
diff --git a/stage0/.claude/sema-plan.md b/stage0/.claude/sema-plan.md
@@ -1,147 +0,0 @@
-# Plan: Wire up Zig Compilation for reference sema in stage0 tests
-
-## Context
-
-`stagesCheck` in `stages_test.zig` runs C sema and converts the result to
-Zig Air, but has no reference Zig sema output to compare against. We want
-to run the real Zig sema pipeline on the same source and verify it succeeds.
-
-## Current State (in progress)
-
-### Files changed so far:
-
-1. **`src/test_exports.zig`** — added `Compilation` and `Package` exports
-2. **`stage0/sema.zig`** — NEW file with `ZigSemaResult` struct and `zigSema` helper
-3. **`stage0/sema_c.zig`** — unchanged (zigSema was moved out to `sema.zig`)
-4. **`stage0/stages_test.zig`** — imports `sema.zig`, passes `src_path` to `stagesCheck`
-
-### Problem to solve:
-
-`zigSema` takes a cwd-relative `src_path` (e.g. `"lib/std/crypto/codecs.zig"`)
-and points a `Compilation` at it. However, files under `lib/` overlap with the
-`zig_lib` directory, causing:
-
-```
-error: file exists in modules 'root' and 'std'
-```
-
-The current attempted fix uses `.root = .none` with an absolute path to
-dissociate the root module from zig_lib, but this hasn't been tested yet.
-
-## Approach: file-path based
-
-Point a `Compilation` at the source file directly (no temp-file copy of source).
-The Compilation reads the file, parses, AstGens, and runs sema — exactly
-like the real compiler.
-
-Still needs a temp dir for the Compilation cache.
-
-Reference: `jitCmd` in `src/main.zig:5588-5654` shows the simplest
-Compilation.create + update flow.
-
-## Changes needed
-
-### 1. `src/test_exports.zig` — export Compilation types (DONE)
-
-```zig
-pub const Compilation = @import("Compilation.zig");
-pub const Package = @import("Package.zig");
-```
-
-### 2. `stage0/sema.zig` — zigSema helper (IN PROGRESS)
-
-```zig
-const Compilation = @import("zig_internals").Compilation;
-const Package = @import("zig_internals").Package;
-
-pub const ZigSemaResult = struct {
- comp: *Compilation,
- dirs: Compilation.Directories,
- tmp_dir: std.testing.TmpDir,
- arena_state: std.heap.ArenaAllocator,
- thread_pool: std.Thread.Pool,
-
- pub fn deinit(self: *ZigSemaResult) void {
- self.comp.destroy();
- self.dirs.deinit();
- self.thread_pool.deinit();
- self.tmp_dir.cleanup();
- self.arena_state.deinit();
- }
-};
-
-pub fn zigSema(gpa: Allocator, src_path: []const u8) !ZigSemaResult { ... }
-```
-
-Inside `zigSema`:
-
-1. **Arena**: `var arena_state = std.heap.ArenaAllocator.init(gpa);`
-2. **Temp dir**: `var tmp_dir = std.testing.tmpDir(.{});` (for cache only)
-3. **Dirs**: Construct `Compilation.Directories` manually:
- - `zig_lib` = open `lib/` dir with absolute path
- - `global_cache` = `local_cache` = `.cache` subdir of tmp_dir
-4. **Target**: hardcode x86_64-linux-musl
-5. **Config**: `Compilation.Config.resolve(.{ .output_mode = .Obj, .have_zcu = true, .emit_bin = false, .is_test = false, .resolved_target = ... })`
-6. **Root path**: Must use `.root = .none` with absolute path to the source
- dir so it doesn't overlap with zig_lib (avoids "file exists in modules
- 'root' and 'std'" error)
-7. **Module**: `Package.Module.create(arena, .{ .paths = .{ .root = root_path, .root_src_path = basename }, ... })`
-8. **Thread pool**: `std.Thread.Pool` with `n_jobs = 1, track_ids = true, stack_size = 60 << 20`
-9. **Create**: `Compilation.create(gpa, arena, &diag, .{ ... .emit_bin = .no, .cache_mode = .whole })`
-10. **Run**: `try comp.update(std.Progress.Node.none)`
-11. **Errors**: `comp.getAllErrorsAlloc()` → if errors, print to stderr and return `error.ZigSemaFailed`
-12. **Return**: ZigSemaResult owning comp, dirs, tmp_dir, arena, thread_pool
-
-### 3. `stage0/stages_test.zig` — use zigSema in stagesCheck (DONE)
-
-The inline for passes `path["../".len..]` to strip the `"../"` prefix
-(corpus paths are relative to stage0/, stripping gives cwd-relative paths):
-
-```zig
-inline for (corpus_files) |path| {
- stagesCheck(gpa, @embedFile(path), path["../".len..], check) catch { ... };
-}
-
-fn stagesCheck(gpa: Allocator, source: [:0]const u8, src_path: []const u8, check: Stage) !void {
- ...
- if (check == .sema) {
- var c_result = try sema_c.cSema(gpa, @bitCast(c_zir));
- defer c_result.deinit(gpa);
- try sema_c.expectEqualAir(c_result.air(), c_result.c_air);
-
- var zig_result = try sema.zigSema(gpa, src_path);
- defer zig_result.deinit();
- }
-}
-```
-
-## Open issue: zig_lib overlap
-
-When the source file is under `lib/` (all current corpus files are), the
-Compilation's auto-created `std` module claims the same files. Fix is to
-ensure the root module path uses `.root = .none` (absolute, not associated
-with zig_lib). Current code does this but needs testing.
-
-## Key references
-
-| What | Where |
-|---|---|
-| Simplest Compilation.create | `src/main.zig:5588` (jitCmd) |
-| Config.resolve | `src/Compilation/Config.zig:152` |
-| Package.Module.create | `src/Package/Module.zig:110` |
-| Directories struct | `src/Compilation.zig:709` |
-| Path.fromUnresolved | `src/Compilation.zig:510` |
-| Cache.Directory | `lib/std/Build/Cache/Directory.zig:1` |
-| ResolvedTarget | `src/Package/Module.zig:87` |
-| resolveTargetQuery | `lib/std/zig/system.zig:183` |
-| Progress.Node.none | used in `src/Compilation.zig:261` |
-| ErrorBundle.renderToStdErr | `lib/std/zig/ErrorBundle.zig:165` |
-| thread_stack_size = 60<<20 | `src/main.zig:42` |
-
-## Verification
-
-```sh
-cd ~/code/zig && ./zig-out/bin/zig build test-zig0 -Dzig0-cc=tcc 2>&1 | tail -5
-```
-
-Should exit 0 with no output.
diff --git a/stage0/README.md b/stage0/README.md
@@ -63,3 +63,16 @@ in `astgen_test.zig` skips float128 payloads to account for this.
Previous approach used `__float128`/`strtof128` (GCC/glibc extensions) for
full precision, but these are not portable to TCC and other C11 compilers.
+
+# Rebuilding zig
+
+If you need to rebuild zig-out/bin/zig for some reason, here's how:
+
+```
+~/code/zig-bootstrap/out-0.15.2/zig-x86_64-linux-musl-x86_64_v3/zig build \
+ --zig-lib-dir lib/ -Dtarget=x86_64-linux-musl -Dcpu=x86_64_v3 \
+ -Dstatic-llvm --search-prefix \
+ $HOME/code/zig-bootstrap/out-0.15.2/x86_64-linux-musl-x86_64_v3/ \
+ -Ddebug-extensions=true -Dlog=false -Doptimize=ReleaseSafe
+
+```