authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-16 13:09:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-16 13:09:32-07:00
log6d37ae95edc06f15e4e77f64e8e637dd5d269183
tree3ed41fafc72bc523022bf2e1e0972d9be3a187e2
parent8f8294a809f9d975735377e7bfcc2c47ccfc4cb7

build.zig: support -Duse-zig-libcxx

This supports the case when it is known that LLVM, Clang, LLD were built with Clang (or `zig c++`). This commit updates the Linux CI script to pass this since we build using a zig tarball.

2 files changed, 31 insertions(+), 25 deletions(-)

build.zig+30-24
......@@ -18,6 +18,7 @@ pub fn build(b: *Builder) !void {
1818 const mode = b.standardReleaseOptions();
1919 const target = b.standardTargetOptions(.{});
2020 const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode") orelse false;
21 const use_zig_libcxx = b.option(bool, "use-zig-libcxx", "If libc++ is needed, use zig's bundled version, don't try to integrate with the system") orelse false;
2122
2223 var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
2324 docgen_exe.single_threaded = single_threaded;
......@@ -160,8 +161,8 @@ pub fn build(b: *Builder) !void {
160161 b.addSearchPrefix(cfg.cmake_prefix_path);
161162 }
162163
163 try addCmakeCfgOptionsToExe(b, cfg, exe);
164 try addCmakeCfgOptionsToExe(b, cfg, test_stage2);
164 try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx);
165 try addCmakeCfgOptionsToExe(b, cfg, test_stage2, use_zig_libcxx);
165166 } else {
166167 // Here we are -Denable-llvm but no cmake integration.
167168 try addStaticLlvmOptionsToExe(exe);
......@@ -408,6 +409,7 @@ fn addCmakeCfgOptionsToExe(
408409 b: *Builder,
409410 cfg: CMakeConfig,
410411 exe: *std.build.LibExeObjStep,
412 use_zig_libcxx: bool,
411413) !void {
412414 exe.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{
413415 cfg.cmake_binary_dir,
......@@ -420,28 +422,32 @@ fn addCmakeCfgOptionsToExe(
420422 addCMakeLibraryList(exe, cfg.lld_libraries);
421423 addCMakeLibraryList(exe, cfg.llvm_libraries);
422424
423 const need_cpp_includes = true;
424
425 // System -lc++ must be used because in this code path we are attempting to link
426 // against system-provided LLVM, Clang, LLD.
427 if (exe.target.getOsTag() == .linux) {
428 // First we try to static link against gcc libstdc++. If that doesn't work,
429 // we fall back to -lc++ and cross our fingers.
430 addCxxKnownPath(b, cfg, exe, "libstdc++.a", "", need_cpp_includes) catch |err| switch (err) {
431 error.RequiredLibraryNotFound => {
432 exe.linkSystemLibrary("c++");
433 },
434 else => |e| return e,
435 };
436 exe.linkSystemLibrary("unwind");
437 } else if (exe.target.isFreeBSD()) {
438 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
439 exe.linkSystemLibrary("pthread");
440 } else if (exe.target.getOsTag() == .openbsd) {
441 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
442 try addCxxKnownPath(b, cfg, exe, "libc++abi.a", null, need_cpp_includes);
443 } else if (exe.target.isDarwin()) {
444 exe.linkSystemLibrary("c++");
425 if (use_zig_libcxx) {
426 exe.linkLibCpp();
427 } else {
428 const need_cpp_includes = true;
429
430 // System -lc++ must be used because in this code path we are attempting to link
431 // against system-provided LLVM, Clang, LLD.
432 if (exe.target.getOsTag() == .linux) {
433 // First we try to static link against gcc libstdc++. If that doesn't work,
434 // we fall back to -lc++ and cross our fingers.
435 addCxxKnownPath(b, cfg, exe, "libstdc++.a", "", need_cpp_includes) catch |err| switch (err) {
436 error.RequiredLibraryNotFound => {
437 exe.linkSystemLibrary("c++");
438 },
439 else => |e| return e,
440 };
441 exe.linkSystemLibrary("unwind");
442 } else if (exe.target.isFreeBSD()) {
443 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
444 exe.linkSystemLibrary("pthread");
445 } else if (exe.target.getOsTag() == .openbsd) {
446 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
447 try addCxxKnownPath(b, cfg, exe, "libc++abi.a", null, need_cpp_includes);
448 } else if (exe.target.isDarwin()) {
449 exe.linkSystemLibrary("c++");
450 }
445451 }
446452
447453 if (cfg.dia_guids_lib.len != 0) {
ci/azure/linux_script+1-1
......@@ -71,7 +71,7 @@ make $JOBS install
7171
7272release/bin/zig test ../test/behavior.zig -fno-stage1 -fLLVM -I ../test
7373
74release/bin/zig build test -Denable-qemu -Denable-wasmtime -Denable-llvm
74release/bin/zig build test -Denable-qemu -Denable-wasmtime -Denable-llvm -Duse-zig-libcxx
7575
7676# Look for HTML errors.
7777tidy -qe ../zig-cache/langref.html