From 6f42876e74831b7f9cecf5a863634df920c52b98 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Feb 2022 16:55:12 -0700 Subject: [PATCH] CLI: resolve system libs into static libraries using the provided -L directories before checking if we need to integrate with system library paths. This prevents false positive of invoking system cc to find native paths when in fact all dependencies are satisfied by -L (or --search-prefix to zig build). --- build.zig | 4 +--- ci/zinc/linux_test.sh | 3 +-- src/main.zig | 42 +++++++++++++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/build.zig b/build.zig index 75c3ee61fa..6f87d5ab8f 100644 --- a/build.zig +++ b/build.zig @@ -502,9 +502,7 @@ fn addCmakeCfgOptionsToExe( } } -fn addStaticLlvmOptionsToExe( - exe: *std.build.LibExeObjStep, -) !void { +fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void { // Adds the Zig C++ sources which both stage1 and stage2 need. // // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling diff --git a/ci/zinc/linux_test.sh b/ci/zinc/linux_test.sh index 79b3174f15..ec3b3ad977 100755 --- a/ci/zinc/linux_test.sh +++ b/ci/zinc/linux_test.sh @@ -48,7 +48,7 @@ cd $WORKSPACE $ZIG fmt --check . # Build stage2 standalone so that we can test stage2 against stage2 compiler-rt. -$ZIG build -p stage2 -Denable-llvm -Dstatic-llvm -Duse-zig-libcxx +$ZIG build -p stage2 -Dstatic-llvm -Duse-zig-libcxx stage2/bin/zig test test/behavior.zig -I test -fLLVM stage2/bin/zig test test/behavior.zig -I test @@ -94,7 +94,6 @@ tidy --drop-empty-elements no -qe zig-cache/langref.html $ZIG build --prefix "RELEASE_STAGING" \ --search-prefix "$DEPS_LOCAL" \ - -Denable-llvm \ -Dstatic-llvm \ -Drelease \ -Dstrip \ diff --git a/src/main.zig b/src/main.zig index 05afc2b166..2abcc083a3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2008,28 +2008,34 @@ fn buildOutputType( // are part of libc or libc++. We remove them from the list and communicate their // existence via flags instead. { + // Similarly, if any libs in this list are statically provided, we remove + // them from this list and populate the link_objects array instead. + const sep = fs.path.sep_str; + var test_path = std.ArrayList(u8).init(gpa); + defer test_path.deinit(); + var i: usize = 0; - while (i < system_libs.count()) { + syslib: while (i < system_libs.count()) { const lib_name = system_libs.keys()[i]; if (target_util.is_libc_lib_name(target_info.target, lib_name)) { link_libc = true; - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (target_util.is_libcpp_lib_name(target_info.target, lib_name)) { link_libcpp = true; - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (mem.eql(u8, lib_name, "unwind")) { link_libunwind = true; - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (target_util.is_compiler_rt_lib_name(target_info.target, lib_name)) { std.log.warn("ignoring superfluous library '{s}': this dependency is fulfilled instead by compiler-rt which zig unconditionally provides", .{lib_name}); - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (std.fs.path.isAbsolute(lib_name)) { @@ -2038,10 +2044,30 @@ fn buildOutputType( if (target_info.target.os.tag == .wasi) { if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| { try wasi_emulated_libs.append(crt_file); - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } } + + for (lib_dirs.items) |lib_dir_path| { + test_path.clearRetainingCapacity(); + try test_path.writer().print("{s}" ++ sep ++ "{s}{s}{s}", .{ + lib_dir_path, + target_info.target.libPrefix(), + lib_name, + target_info.target.staticLibSuffix(), + }); + fs.cwd().access(test_path.items, .{}) catch |err| switch (err) { + error.FileNotFound => continue, + else => |e| fatal("unable to search for static library '{s}': {s}", .{ + test_path.items, @errorName(e), + }), + }; + try link_objects.append(.{ .path = try arena.dupe(u8, test_path.items) }); + system_libs.orderedRemoveAt(i); + continue :syslib; + } + i += 1; } } @@ -2068,7 +2094,9 @@ fn buildOutputType( want_native_include_dirs = true; } - if (sysroot == null and cross_target.isNativeOs() and (system_libs.count() != 0 or want_native_include_dirs)) { + if (sysroot == null and cross_target.isNativeOs() and + (system_libs.count() != 0 or want_native_include_dirs)) + { const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| { fatal("unable to detect native system paths: {s}", .{@errorName(err)}); };