diff --git a/src/build_sysroot.c b/src/build_sysroot.c index f691c33..445920c 100644 --- a/src/build_sysroot.c +++ b/src/build_sysroot.c @@ -1031,13 +1031,19 @@ err_t build_sysroot(const compiler_t* c, int flags) { finalize_build_component(c, lockfd, &err, "librt"); } + if (!err && (flags & SYSROOT_BUILD_UNWIND) && + target_has_syslib(&c->target, SYSLIB_UNWIND) && + build_component(c, &lockfd, &err, flags, "libunwind")) + { + err = build_libunwind(c); + finalize_build_component(c, lockfd, &err, "libunwind"); + } + if (!err && (flags & SYSROOT_BUILD_CXX) && target_has_syslib(&c->target, SYSLIB_CXX) && build_component(c, &lockfd, &err, flags, "libcxx")) { - assert(target_has_syslib(&c->target, SYSLIB_UNWIND)); assert(target_has_syslib(&c->target, SYSLIB_CXXABI)); - err = build_libunwind(c); if (!err) err = build_cxx_config_site(c); // __config_site header if (!err) err = build_libcxxabi(c); if (!err) err = build_libcxx(c); @@ -1136,7 +1142,7 @@ static bool build_sysroot_for_target(compiler_t* compiler, const target_t* targe log("building sysroot for %s", tmpbuf); } - int flags = SYSROOT_BUILD_CXX; + int flags = SYSROOT_BUILD_CXX | SYSROOT_BUILD_UNWIND; if (opt_force) flags |= SYSROOT_BUILD_FORCE; if (( err = build_sysroot(compiler, flags) )) { dlog("build_sysroot: %s", err_str(err)); diff --git a/src/cc.c b/src/cc.c index 7ed61d0..e115747 100644 --- a/src/cc.c +++ b/src/cc.c @@ -76,6 +76,7 @@ int cc_main(int user_argc, char* user_argv[], bool iscxx) { bool link_libc = true; bool link_libcxx = iscxx; bool link_librt = true; + bool explicit_link_libunwind = false; // -lunwind bool startfiles = true; bool nostdinc = false; bool custom_ld = false; @@ -185,7 +186,15 @@ int cc_main(int user_argc, char* user_argv[], bool iscxx) { iscompiling = true; } else if (streq(arg, "-x")) { iscompiling = true; + } else if (string_startswith(arg, "-L") || string_startswith(arg, "-l")) { + if (streq(arg+2, "unwind")) + explicit_link_libunwind = true; + has_link_flags = true; } else if (streq(arg, "-L") || streq(arg, "-l")) { + if (i+1 < user_argc) { + if (streq(user_argv[i+1], "unwind")) + explicit_link_libunwind = true; + } has_link_flags = true; } else if (streq(arg, "-o")) { // infer "no linking" based on output filename @@ -347,6 +356,8 @@ int cc_main(int user_argc, char* user_argv[], bool iscxx) { // build sysroot if (!custom_sysroot && !print_only) { int sysroot_build_flags = 0; + if (explicit_link_libunwind || link_libcxx) + sysroot_build_flags |= SYSROOT_BUILD_UNWIND; if (link_libcxx) sysroot_build_flags |= SYSROOT_BUILD_CXX; if (( err = build_sysroot(&c, sysroot_build_flags) )) @@ -401,6 +412,9 @@ int cc_main(int user_argc, char* user_argv[], bool iscxx) { } } + if (explicit_link_libunwind) + strlist_addf(&args, "-isystem%s/libunwind/include", coroot); + // linker flags if (link) { diff --git a/src/compiler.h b/src/compiler.h index 1f53d9e..5dac67e 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -416,8 +416,9 @@ void scope_iterate(scope_t* s, u32 maxdepth, scopeit_t it, void* nullable ctx); const char* visibility_str(nodeflag_t flags); // sysroot -#define SYSROOT_BUILD_CXX (1<<0) // enable libc++, libc++abi, libunwind -#define SYSROOT_BUILD_FORCE (1<<1) // (re)build even if up to date +#define SYSROOT_BUILD_FORCE (1<<0) // (re)build even if up to date +#define SYSROOT_BUILD_CXX (1<<1) // enable libc++, libc++abi +#define SYSROOT_BUILD_UNWIND (1<<2) // enable libunwind err_t build_sysroot(const compiler_t* c, int flags); // build_sysroot.c const char* syslib_filename(const target_t* target, syslib_t);