authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-17 23:18:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-19 03:41:13-07:00
logd6ba66e50d5f0dbabaf7b56e7eed4b668cd53244
treeb0b66e20951e04294edc5c63346b07261c4b02a1
parentc0b7f20893ea5ca42e0d02b59db6f459c2f80ca1

Sema: avoid false positive error for linking libc

when extern c functions are called.

2 files changed, 9 insertions(+), 8 deletions(-)

src/Sema.zig+8-7
...@@ -7567,10 +7567,11 @@ fn handleExternLibName(...@@ -7567,10 +7567,11 @@ fn handleExternLibName(
7567) CompileError![:0]u8 {7567) CompileError![:0]u8 {
7568 blk: {7568 blk: {
7569 const mod = sema.mod;7569 const mod = sema.mod;
7570 const comp = mod.comp;
7570 const target = mod.getTarget();7571 const target = mod.getTarget();
7571 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});7572 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
7572 if (target_util.is_libc_lib_name(target, lib_name)) {7573 if (target_util.is_libc_lib_name(target, lib_name)) {
7573 if (!mod.comp.bin_file.options.link_libc) {7574 if (!comp.bin_file.options.link_libc and !comp.bin_file.options.parent_compilation_link_libc) {
7574 return sema.fail(7575 return sema.fail(
7575 block,7576 block,
7576 src_loc,7577 src_loc,
...@@ -7578,11 +7579,11 @@ fn handleExternLibName(...@@ -7578,11 +7579,11 @@ fn handleExternLibName(
7578 .{},7579 .{},
7579 );7580 );
7580 }7581 }
7581 mod.comp.bin_file.options.link_libc = true;7582 comp.bin_file.options.link_libc = true;
7582 break :blk;7583 break :blk;
7583 }7584 }
7584 if (target_util.is_libcpp_lib_name(target, lib_name)) {7585 if (target_util.is_libcpp_lib_name(target, lib_name)) {
7585 if (!mod.comp.bin_file.options.link_libcpp) {7586 if (!comp.bin_file.options.link_libcpp) {
7586 return sema.fail(7587 return sema.fail(
7587 block,7588 block,
7588 src_loc,7589 src_loc,
...@@ -7590,14 +7591,14 @@ fn handleExternLibName(...@@ -7590,14 +7591,14 @@ fn handleExternLibName(
7590 .{},7591 .{},
7591 );7592 );
7592 }7593 }
7593 mod.comp.bin_file.options.link_libcpp = true;7594 comp.bin_file.options.link_libcpp = true;
7594 break :blk;7595 break :blk;
7595 }7596 }
7596 if (mem.eql(u8, lib_name, "unwind")) {7597 if (mem.eql(u8, lib_name, "unwind")) {
7597 mod.comp.bin_file.options.link_libunwind = true;7598 comp.bin_file.options.link_libunwind = true;
7598 break :blk;7599 break :blk;
7599 }7600 }
7600 if (!target.isWasm() and !mod.comp.bin_file.options.pic) {7601 if (!target.isWasm() and !comp.bin_file.options.pic) {
7601 return sema.fail(7602 return sema.fail(
7602 block,7603 block,
7603 src_loc,7604 src_loc,
...@@ -7605,7 +7606,7 @@ fn handleExternLibName(...@@ -7605,7 +7606,7 @@ fn handleExternLibName(
7605 .{ lib_name, lib_name },7606 .{ lib_name, lib_name },
7606 );7607 );
7607 }7608 }
7608 mod.comp.stage1AddLinkLib(lib_name) catch |err| {7609 comp.stage1AddLinkLib(lib_name) catch |err| {
7609 return sema.fail(block, src_loc, "unable to add link lib '{s}': {s}", .{7610 return sema.fail(block, src_loc, "unable to add link lib '{s}': {s}", .{
7610 lib_name, @errorName(err),7611 lib_name, @errorName(err),
7611 });7612 });
src/stage1.zig+1-1
...@@ -416,7 +416,7 @@ export fn stage2_add_link_lib(...@@ -416,7 +416,7 @@ export fn stage2_add_link_lib(
416 const target = comp.getTarget();416 const target = comp.getTarget();
417 const is_libc = target_util.is_libc_lib_name(target, lib_name);417 const is_libc = target_util.is_libc_lib_name(target, lib_name);
418 if (is_libc) {418 if (is_libc) {
419 if (!comp.bin_file.options.link_libc) {419 if (!comp.bin_file.options.link_libc and !comp.bin_file.options.parent_compilation_link_libc) {
420 return "dependency on libc must be explicitly specified in the build command";420 return "dependency on libc must be explicitly specified in the build command";
421 }421 }
422 return null;422 return null;