authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-17 16:55:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-17 23:42:48-07:00
log6f42876e74831b7f9cecf5a863634df920c52b98
tree627bbefc0ec47b98a76055299cc700baf39d3cda
parent419074a81ba7d7111bae7d07af26aad9c7deeb1f

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).

3 files changed, 37 insertions(+), 12 deletions(-)

build.zig+1-3
...@@ -502,9 +502,7 @@ fn addCmakeCfgOptionsToExe(...@@ -502,9 +502,7 @@ fn addCmakeCfgOptionsToExe(
502 }502 }
503}503}
504504
505fn addStaticLlvmOptionsToExe(505fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void {
506 exe: *std.build.LibExeObjStep,
507) !void {
508 // Adds the Zig C++ sources which both stage1 and stage2 need.506 // Adds the Zig C++ sources which both stage1 and stage2 need.
509 //507 //
510 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling508 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling
ci/zinc/linux_test.sh+1-2
...@@ -48,7 +48,7 @@ cd $WORKSPACE...@@ -48,7 +48,7 @@ cd $WORKSPACE
48$ZIG fmt --check .48$ZIG fmt --check .
4949
50# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt.50# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt.
51$ZIG build -p stage2 -Denable-llvm -Dstatic-llvm -Duse-zig-libcxx51$ZIG build -p stage2 -Dstatic-llvm -Duse-zig-libcxx
5252
53stage2/bin/zig test test/behavior.zig -I test -fLLVM53stage2/bin/zig test test/behavior.zig -I test -fLLVM
54stage2/bin/zig test test/behavior.zig -I test54stage2/bin/zig test test/behavior.zig -I test
...@@ -94,7 +94,6 @@ tidy --drop-empty-elements no -qe zig-cache/langref.html...@@ -94,7 +94,6 @@ tidy --drop-empty-elements no -qe zig-cache/langref.html
94$ZIG build94$ZIG build
95 --prefix "RELEASE_STAGING" \95 --prefix "RELEASE_STAGING" \
96 --search-prefix "$DEPS_LOCAL" \96 --search-prefix "$DEPS_LOCAL" \
97 -Denable-llvm \
98 -Dstatic-llvm \97 -Dstatic-llvm \
99 -Drelease \98 -Drelease \
100 -Dstrip \99 -Dstrip \
src/main.zig+35-7
...@@ -2008,28 +2008,34 @@ fn buildOutputType(...@@ -2008,28 +2008,34 @@ fn buildOutputType(
2008 // are part of libc or libc++. We remove them from the list and communicate their2008 // are part of libc or libc++. We remove them from the list and communicate their
2009 // existence via flags instead.2009 // existence via flags instead.
2010 {2010 {
2011 // Similarly, if any libs in this list are statically provided, we remove
2012 // them from this list and populate the link_objects array instead.
2013 const sep = fs.path.sep_str;
2014 var test_path = std.ArrayList(u8).init(gpa);
2015 defer test_path.deinit();
2016
2011 var i: usize = 0;2017 var i: usize = 0;
2012 while (i < system_libs.count()) {2018 syslib: while (i < system_libs.count()) {
2013 const lib_name = system_libs.keys()[i];2019 const lib_name = system_libs.keys()[i];
20142020
2015 if (target_util.is_libc_lib_name(target_info.target, lib_name)) {2021 if (target_util.is_libc_lib_name(target_info.target, lib_name)) {
2016 link_libc = true;2022 link_libc = true;
2017 _ = system_libs.orderedRemove(lib_name);2023 system_libs.orderedRemoveAt(i);
2018 continue;2024 continue;
2019 }2025 }
2020 if (target_util.is_libcpp_lib_name(target_info.target, lib_name)) {2026 if (target_util.is_libcpp_lib_name(target_info.target, lib_name)) {
2021 link_libcpp = true;2027 link_libcpp = true;
2022 _ = system_libs.orderedRemove(lib_name);2028 system_libs.orderedRemoveAt(i);
2023 continue;2029 continue;
2024 }2030 }
2025 if (mem.eql(u8, lib_name, "unwind")) {2031 if (mem.eql(u8, lib_name, "unwind")) {
2026 link_libunwind = true;2032 link_libunwind = true;
2027 _ = system_libs.orderedRemove(lib_name);2033 system_libs.orderedRemoveAt(i);
2028 continue;2034 continue;
2029 }2035 }
2030 if (target_util.is_compiler_rt_lib_name(target_info.target, lib_name)) {2036 if (target_util.is_compiler_rt_lib_name(target_info.target, lib_name)) {
2031 std.log.warn("ignoring superfluous library '{s}': this dependency is fulfilled instead by compiler-rt which zig unconditionally provides", .{lib_name});2037 std.log.warn("ignoring superfluous library '{s}': this dependency is fulfilled instead by compiler-rt which zig unconditionally provides", .{lib_name});
2032 _ = system_libs.orderedRemove(lib_name);2038 system_libs.orderedRemoveAt(i);
2033 continue;2039 continue;
2034 }2040 }
2035 if (std.fs.path.isAbsolute(lib_name)) {2041 if (std.fs.path.isAbsolute(lib_name)) {
...@@ -2038,10 +2044,30 @@ fn buildOutputType(...@@ -2038,10 +2044,30 @@ fn buildOutputType(
2038 if (target_info.target.os.tag == .wasi) {2044 if (target_info.target.os.tag == .wasi) {
2039 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| {2045 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| {
2040 try wasi_emulated_libs.append(crt_file);2046 try wasi_emulated_libs.append(crt_file);
2041 _ = system_libs.orderedRemove(lib_name);2047 system_libs.orderedRemoveAt(i);
2042 continue;2048 continue;
2043 }2049 }
2044 }2050 }
2051
2052 for (lib_dirs.items) |lib_dir_path| {
2053 test_path.clearRetainingCapacity();
2054 try test_path.writer().print("{s}" ++ sep ++ "{s}{s}{s}", .{
2055 lib_dir_path,
2056 target_info.target.libPrefix(),
2057 lib_name,
2058 target_info.target.staticLibSuffix(),
2059 });
2060 fs.cwd().access(test_path.items, .{}) catch |err| switch (err) {
2061 error.FileNotFound => continue,
2062 else => |e| fatal("unable to search for static library '{s}': {s}", .{
2063 test_path.items, @errorName(e),
2064 }),
2065 };
2066 try link_objects.append(.{ .path = try arena.dupe(u8, test_path.items) });
2067 system_libs.orderedRemoveAt(i);
2068 continue :syslib;
2069 }
2070
2045 i += 1;2071 i += 1;
2046 }2072 }
2047 }2073 }
...@@ -2068,7 +2094,9 @@ fn buildOutputType(...@@ -2068,7 +2094,9 @@ fn buildOutputType(
2068 want_native_include_dirs = true;2094 want_native_include_dirs = true;
2069 }2095 }
20702096
2071 if (sysroot == null and cross_target.isNativeOs() and (system_libs.count() != 0 or want_native_include_dirs)) {2097 if (sysroot == null and cross_target.isNativeOs() and
2098 (system_libs.count() != 0 or want_native_include_dirs))
2099 {
2072 const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| {2100 const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| {
2073 fatal("unable to detect native system paths: {s}", .{@errorName(err)});2101 fatal("unable to detect native system paths: {s}", .{@errorName(err)});
2074 };2102 };