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
57325732 const tracy = trace(@src());
57335733 defer tracy.end();
57345734
5735 const mod = sema.mod;
5736 const gpa = sema.gpa;
57355737 const pl_node = sema.code.instructions.items(.data)[inst].pl_node;
57365738 const src = pl_node.src();
57375739 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
57415743 if (!@import("build_options").have_llvm)
57425744 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);
57455747 defer c_import_buf.deinit();
57465748
57475749 var comptime_reason: Block.ComptimeReason = .{ .c_import = .{
......@@ -5763,25 +5765,24 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57635765 .runtime_loop = parent_block.runtime_loop,
57645766 .runtime_index = parent_block.runtime_index,
57655767 };
5766 defer child_block.instructions.deinit(sema.gpa);
5768 defer child_block.instructions.deinit(gpa);
57675769
57685770 // Ignore the result, all the relevant operations have written to c_import_buf already.
57695771 _ = try sema.analyzeBodyBreak(&child_block, body);
57705772
5771 const mod = sema.mod;
57725773 var c_import_res = mod.comp.cImport(c_import_buf.items) catch |err|
57735774 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
57765777 if (c_import_res.errors.errorMessageCount() != 0) {
57775778 const msg = msg: {
57785779 const msg = try sema.errMsg(&child_block, src, "C import failed", .{});
5779 errdefer msg.destroy(sema.gpa);
5780 errdefer msg.destroy(gpa);
57805781
57815782 if (!mod.comp.bin_file.options.link_libc)
57825783 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);
57855786 if (!gop.found_existing) {
57865787 gop.value_ptr.* = c_import_res.errors;
57875788 c_import_res.errors = std.zig.ErrorBundle.empty;
......@@ -5790,16 +5791,19 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57905791 };
57915792 return sema.failWithOwnedErrorMsg(&child_block, msg);
57925793 }
5793 const c_import_pkg = Package.create(
5794 sema.gpa,
5795 null,
5796 c_import_res.out_zig_path,
5797 ) catch |err| switch (err) {
5798 error.OutOfMemory => return error.OutOfMemory,
5799 else => unreachable, // we pass null for root_src_dir_path
5800 };
5794 // All modules are intended to go into an arena with a lifetime >= the ZigUnit.
5795 // After the other uses of `tmp_hack_arena` are eliminated, it should be
5796 // renamed to something more appropriate such as simply `arena`.
5797 const zu_arena = mod.tmp_hack_arena.allocator();
5798 const c_import_mod = try Package.Module.create(zu_arena, .{
5799 .root = .{
5800 .root_dir = Compilation.Directory.cwd(),
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|
58035807 return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
58045808
58055809 mod.astGenFile(result.file) catch |err|
src/codegen/llvm.zig+5-8
......@@ -1836,14 +1836,11 @@ pub const Object = struct {
18361836 }
18371837 const dir_path_z = d: {
18381838 var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
1839 const dir_path = file.pkg.root_src_directory.path orelse ".";
1840 const resolved_dir_path = if (std.fs.path.isAbsolute(dir_path))
1841 dir_path
1842 else
1843 std.os.realpath(dir_path, &buffer) catch dir_path; // If realpath fails, fallback to whatever dir_path was
1844 break :d try std.fs.path.joinZ(gpa, &.{
1845 resolved_dir_path, std.fs.path.dirname(file.sub_file_path) orelse "",
1846 });
1839 const sub_path = std.fs.path.dirname(file.sub_file_path) orelse "";
1840 const dir_path = try file.mod.root.joinStringZ(gpa, sub_path);
1841 if (std.fs.path.isAbsolute(dir_path)) break :d dir_path;
1842 const abs = std.fs.realpath(dir_path, &buffer) catch break :d dir_path;
1843 break :d try std.fs.path.joinZ(gpa, &.{ abs, sub_path });
18471844 };
18481845 defer gpa.free(dir_path_z);
18491846 const sub_file_path_z = try gpa.dupeZ(u8, std.fs.path.basename(file.sub_file_path));