authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-07 14:38:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
log35d81c99c0f737f071e1ad0083342328dbb32acb
tree037748441e1b1a64935b4b99c16b7d7df7bf60f1
parenta232f7e8ecc9f8afb210ffd483ca59556175a33c

more fixes related to previous commits Package/Module API


2 files changed, 24 insertions(+), 23 deletions(-)

src/Sema.zig+19-15
...@@ -5732,6 +5732,8 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr...@@ -5732,6 +5732,8 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
5732 const tracy = trace(@src());5732 const tracy = trace(@src());
5733 defer tracy.end();5733 defer tracy.end();
57345734
5735 const mod = sema.mod;
5736 const gpa = sema.gpa;
5735 const pl_node = sema.code.instructions.items(.data)[inst].pl_node;5737 const pl_node = sema.code.instructions.items(.data)[inst].pl_node;
5736 const src = pl_node.src();5738 const src = pl_node.src();
5737 const extra = sema.code.extraData(Zir.Inst.Block, pl_node.payload_index);5739 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...@@ -5741,7 +5743,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
5741 if (!@import("build_options").have_llvm)5743 if (!@import("build_options").have_llvm)
5742 return sema.fail(parent_block, src, "C import unavailable; Zig compiler built without LLVM extensions", .{});5744 return sema.fail(parent_block, src, "C import unavailable; Zig compiler built without LLVM extensions", .{});
57435745
5744 var c_import_buf = std.ArrayList(u8).init(sema.gpa);5746 var c_import_buf = std.ArrayList(u8).init(gpa);
5745 defer c_import_buf.deinit();5747 defer c_import_buf.deinit();
57465748
5747 var comptime_reason: Block.ComptimeReason = .{ .c_import = .{5749 var comptime_reason: Block.ComptimeReason = .{ .c_import = .{
...@@ -5763,25 +5765,24 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr...@@ -5763,25 +5765,24 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
5763 .runtime_loop = parent_block.runtime_loop,5765 .runtime_loop = parent_block.runtime_loop,
5764 .runtime_index = parent_block.runtime_index,5766 .runtime_index = parent_block.runtime_index,
5765 };5767 };
5766 defer child_block.instructions.deinit(sema.gpa);5768 defer child_block.instructions.deinit(gpa);
57675769
5768 // Ignore the result, all the relevant operations have written to c_import_buf already.5770 // Ignore the result, all the relevant operations have written to c_import_buf already.
5769 _ = try sema.analyzeBodyBreak(&child_block, body);5771 _ = try sema.analyzeBodyBreak(&child_block, body);
57705772
5771 const mod = sema.mod;
5772 var c_import_res = mod.comp.cImport(c_import_buf.items) catch |err|5773 var c_import_res = mod.comp.cImport(c_import_buf.items) catch |err|
5773 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});5774 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
5774 defer c_import_res.deinit(mod.comp.gpa);5775 defer c_import_res.deinit(gpa);
57755776
5776 if (c_import_res.errors.errorMessageCount() != 0) {5777 if (c_import_res.errors.errorMessageCount() != 0) {
5777 const msg = msg: {5778 const msg = msg: {
5778 const msg = try sema.errMsg(&child_block, src, "C import failed", .{});5779 const msg = try sema.errMsg(&child_block, src, "C import failed", .{});
5779 errdefer msg.destroy(sema.gpa);5780 errdefer msg.destroy(gpa);
57805781
5781 if (!mod.comp.bin_file.options.link_libc)5782 if (!mod.comp.bin_file.options.link_libc)
5782 try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{});5783 try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{});
57835784
5784 const gop = try mod.cimport_errors.getOrPut(sema.gpa, sema.owner_decl_index);5785 const gop = try mod.cimport_errors.getOrPut(gpa, sema.owner_decl_index);
5785 if (!gop.found_existing) {5786 if (!gop.found_existing) {
5786 gop.value_ptr.* = c_import_res.errors;5787 gop.value_ptr.* = c_import_res.errors;
5787 c_import_res.errors = std.zig.ErrorBundle.empty;5788 c_import_res.errors = std.zig.ErrorBundle.empty;
...@@ -5790,16 +5791,19 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr...@@ -5790,16 +5791,19 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
5790 };5791 };
5791 return sema.failWithOwnedErrorMsg(&child_block, msg);5792 return sema.failWithOwnedErrorMsg(&child_block, msg);
5792 }5793 }
5793 const c_import_pkg = Package.create(5794 // All modules are intended to go into an arena with a lifetime >= the ZigUnit.
5794 sema.gpa,5795 // After the other uses of `tmp_hack_arena` are eliminated, it should be
5795 null,5796 // renamed to something more appropriate such as simply `arena`.
5796 c_import_res.out_zig_path,5797 const zu_arena = mod.tmp_hack_arena.allocator();
5797 ) catch |err| switch (err) {5798 const c_import_mod = try Package.Module.create(zu_arena, .{
5798 error.OutOfMemory => return error.OutOfMemory,5799 .root = .{
5799 else => unreachable, // we pass null for root_src_dir_path5800 .root_dir = Compilation.Directory.cwd(),
5800 };5801 .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",
5802 },
5803 .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),
5804 });
58015805
5802 const result = mod.importPkg(c_import_pkg) catch |err|5806 const result = mod.importPkg(c_import_mod) catch |err|
5803 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});5807 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
58045808
5805 mod.astGenFile(result.file) catch |err|5809 mod.astGenFile(result.file) catch |err|
src/codegen/llvm.zig+5-8
...@@ -1836,14 +1836,11 @@ pub const Object = struct {...@@ -1836,14 +1836,11 @@ pub const Object = struct {
1836 }1836 }
1837 const dir_path_z = d: {1837 const dir_path_z = d: {
1838 var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;1838 var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
1839 const dir_path = file.pkg.root_src_directory.path orelse ".";1839 const sub_path = std.fs.path.dirname(file.sub_file_path) orelse "";
1840 const resolved_dir_path = if (std.fs.path.isAbsolute(dir_path))1840 const dir_path = try file.mod.root.joinStringZ(gpa, sub_path);
1841 dir_path1841 if (std.fs.path.isAbsolute(dir_path)) break :d dir_path;
1842 else1842 const abs = std.fs.realpath(dir_path, &buffer) catch break :d dir_path;
1843 std.os.realpath(dir_path, &buffer) catch dir_path; // If realpath fails, fallback to whatever dir_path was1843 break :d try std.fs.path.joinZ(gpa, &.{ abs, sub_path });
1844 break :d try std.fs.path.joinZ(gpa, &.{
1845 resolved_dir_path, std.fs.path.dirname(file.sub_file_path) orelse "",
1846 });
1847 };1844 };
1848 defer gpa.free(dir_path_z);1845 defer gpa.free(dir_path_z);
1849 const sub_file_path_z = try gpa.dupeZ(u8, std.fs.path.basename(file.sub_file_path));1846 const sub_file_path_z = try gpa.dupeZ(u8, std.fs.path.basename(file.sub_file_path));