Skip to content

Commit

Permalink
cc: detect -lunwind for C (already automatic for C++) and build libun…
Browse files Browse the repository at this point in the history
…wind when needed
  • Loading branch information
rsms committed Dec 21, 2023
1 parent fcd9955 commit ac41992
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/build_sysroot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
14 changes: 14 additions & 0 deletions src/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) ))
Expand Down Expand Up @@ -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) {

Expand Down
5 changes: 3 additions & 2 deletions src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit ac41992

Please sign in to comment.