From f708c5fafc5087a2518e0dc7acf986b59673dddd Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 6 Oct 2023 21:29:08 -0700 Subject: CLI: finish updating module API usage Finish the work started in 4c4fb839972f66f55aa44fc0aca5f80b0608c731. Now the compiler compiles again. Wire up dependency tree fetching code in the CLI for `zig build`. Everything is hooked up except for `createDependenciesModule` is not yet implemented. --- src/codegen/llvm.zig | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 4355ac1191..3d15b5310b 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -892,21 +892,24 @@ pub const Object = struct { build_options.semver.patch, }); - // We fully resolve all paths at this point to avoid lack of source line info in stack - // traces or lack of debugging information which, if relative paths were used, would - // be very location dependent. + // We fully resolve all paths at this point to avoid lack of + // source line info in stack traces or lack of debugging + // information which, if relative paths were used, would be + // very location dependent. // TODO: the only concern I have with this is WASI as either host or target, should // we leave the paths as relative then? var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; - const compile_unit_dir = blk: { - const path = d: { - const mod = options.module orelse break :d "."; - break :d mod.root_pkg.root_src_directory.path orelse "."; - }; - if (std.fs.path.isAbsolute(path)) break :blk path; - break :blk std.os.realpath(path, &buf) catch path; // If realpath fails, fallback to whatever path was + const compile_unit_dir_z = blk: { + if (options.module) |mod| { + const d = try mod.root_mod.root.joinStringZ(builder.gpa, ""); + if (std.fs.path.isAbsolute(d)) break :blk d; + const abs = std.fs.realpath(d, &buf) catch break :blk d; + builder.gpa.free(d); + break :blk try builder.gpa.dupeZ(u8, abs); + } + const cwd = try std.process.getCwd(&buf); + break :blk try builder.gpa.dupeZ(u8, cwd); }; - const compile_unit_dir_z = try builder.gpa.dupeZ(u8, compile_unit_dir); defer builder.gpa.free(compile_unit_dir_z); builder.llvm.di_compile_unit = builder.llvm.di_builder.?.createCompileUnit( @@ -2828,8 +2831,8 @@ pub const Object = struct { fn getStackTraceType(o: *Object) Allocator.Error!Type { const mod = o.module; - const std_pkg = mod.main_pkg.table.get("std").?; - const std_file = (mod.importPkg(std_pkg) catch unreachable).file; + const std_mod = mod.main_mod.deps.get("std").?; + const std_file = (mod.importPkg(std_mod) catch unreachable).file; const builtin_str = try mod.intern_pool.getOrPutString(mod.gpa, "builtin"); const std_namespace = mod.namespacePtr(mod.declPtr(std_file.root_decl.unwrap().?).src_namespace); -- cgit v1.2.3 From 35d81c99c0f737f071e1ad0083342328dbb32acb Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 7 Oct 2023 14:38:03 -0700 Subject: more fixes related to previous commits Package/Module API --- src/Sema.zig | 34 +++++++++++++++++++--------------- src/codegen/llvm.zig | 13 +++++-------- 2 files changed, 24 insertions(+), 23 deletions(-) (limited to 'src/codegen') diff --git a/src/Sema.zig b/src/Sema.zig index 4d49546cf7..5e428c9f77 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -5732,6 +5732,8 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr const tracy = trace(@src()); defer tracy.end(); + const mod = sema.mod; + const gpa = sema.gpa; const pl_node = sema.code.instructions.items(.data)[inst].pl_node; const src = pl_node.src(); const extra = sema.code.extraData(Zir.Inst.Block, pl_node.payload_index); @@ -5741,7 +5743,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr if (!@import("build_options").have_llvm) return sema.fail(parent_block, src, "C import unavailable; Zig compiler built without LLVM extensions", .{}); - var c_import_buf = std.ArrayList(u8).init(sema.gpa); + var c_import_buf = std.ArrayList(u8).init(gpa); defer c_import_buf.deinit(); var comptime_reason: Block.ComptimeReason = .{ .c_import = .{ @@ -5763,25 +5765,24 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr .runtime_loop = parent_block.runtime_loop, .runtime_index = parent_block.runtime_index, }; - defer child_block.instructions.deinit(sema.gpa); + defer child_block.instructions.deinit(gpa); // Ignore the result, all the relevant operations have written to c_import_buf already. _ = try sema.analyzeBodyBreak(&child_block, body); - const mod = sema.mod; var c_import_res = mod.comp.cImport(c_import_buf.items) catch |err| return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)}); - defer c_import_res.deinit(mod.comp.gpa); + defer c_import_res.deinit(gpa); if (c_import_res.errors.errorMessageCount() != 0) { const msg = msg: { const msg = try sema.errMsg(&child_block, src, "C import failed", .{}); - errdefer msg.destroy(sema.gpa); + errdefer msg.destroy(gpa); if (!mod.comp.bin_file.options.link_libc) try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{}); - const gop = try mod.cimport_errors.getOrPut(sema.gpa, sema.owner_decl_index); + const gop = try mod.cimport_errors.getOrPut(gpa, sema.owner_decl_index); if (!gop.found_existing) { gop.value_ptr.* = c_import_res.errors; c_import_res.errors = std.zig.ErrorBundle.empty; @@ -5790,16 +5791,19 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr }; return sema.failWithOwnedErrorMsg(&child_block, msg); } - const c_import_pkg = Package.create( - sema.gpa, - null, - c_import_res.out_zig_path, - ) catch |err| switch (err) { - error.OutOfMemory => return error.OutOfMemory, - else => unreachable, // we pass null for root_src_dir_path - }; + // All modules are intended to go into an arena with a lifetime >= the ZigUnit. + // After the other uses of `tmp_hack_arena` are eliminated, it should be + // renamed to something more appropriate such as simply `arena`. + const zu_arena = mod.tmp_hack_arena.allocator(); + const c_import_mod = try Package.Module.create(zu_arena, .{ + .root = .{ + .root_dir = Compilation.Directory.cwd(), + .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "", + }, + .root_src_path = std.fs.path.basename(c_import_res.out_zig_path), + }); - const result = mod.importPkg(c_import_pkg) catch |err| + const result = mod.importPkg(c_import_mod) catch |err| return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)}); mod.astGenFile(result.file) catch |err| diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 3d15b5310b..66b5bfbe04 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1836,14 +1836,11 @@ pub const Object = struct { } const dir_path_z = d: { var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; - const dir_path = file.pkg.root_src_directory.path orelse "."; - const resolved_dir_path = if (std.fs.path.isAbsolute(dir_path)) - dir_path - else - std.os.realpath(dir_path, &buffer) catch dir_path; // If realpath fails, fallback to whatever dir_path was - break :d try std.fs.path.joinZ(gpa, &.{ - resolved_dir_path, std.fs.path.dirname(file.sub_file_path) orelse "", - }); + const sub_path = std.fs.path.dirname(file.sub_file_path) orelse ""; + const dir_path = try file.mod.root.joinStringZ(gpa, sub_path); + if (std.fs.path.isAbsolute(dir_path)) break :d dir_path; + const abs = std.fs.realpath(dir_path, &buffer) catch break :d dir_path; + break :d try std.fs.path.joinZ(gpa, &.{ abs, sub_path }); }; defer gpa.free(dir_path_z); const sub_file_path_z = try gpa.dupeZ(u8, std.fs.path.basename(file.sub_file_path)); -- cgit v1.2.3