Skip to content

Commit c471568

Browse files
committed
Allow choosing between static and shared when linking to GCC libraries
1 parent b474f9d commit c471568

1 file changed

Lines changed: 36 additions & 30 deletions

File tree

tools/gcc-wrapper/main.c

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const char GCC_MAJOR_VERSION[] = "15";
2525
static const char INCLUDE_DIR[] = PATHSEP_M "include";
2626
static const char INCLUDE_MISSING_DIR[] = PATHSEP_M "include-missing";
2727
static const char LIBRARY_DIR[] = PATHSEP_M "lib";
28+
static const char STATIC_LIBRARY_DIR[] = PATHSEP_M "lib" PATHSEP_M "static";
2829
static const char GCC_LIBRARY_DIR[] = PATHSEP_M "lib" PATHSEP_M "gcc";
2930

3031
static const char RT_LIBRARY[] = "rt";
@@ -76,6 +77,10 @@ static const char GCC_OPT_F_FAT_LTO_OBJECTS[] = "-ffat-lto-objects";
7677
static const char GCC_OPT_F_NO_FAT_LTO_OBJECTS[] = "-fno-fat-lto-objects";
7778
static const char GCC_OPT_F_LTO[] = "-flto";
7879
static const char GCC_OPT_F_USE_LD[] = "-fuse-ld=";
80+
static const char GCC_OPT_F_GNU_TM[] = "-fgnu-tm";
81+
static const char GCC_OPT_F_OPENMP[] = "-fopenmp";
82+
static const char GCC_OPT_F_OPENACC[] = "-fopenacc";
83+
7984
static const char GCC_OPT_DFORTIFY_SOURCE[] = "-D_FORTIFY_SOURCE=";
8085
static const char GCC_OPT_U_GNUC[] = "-U__GNUC__";
8186

@@ -266,7 +271,7 @@ static int get_env(const char* const key) {
266271
const char* const value = getenv(key);
267272

268273
if (value == NULL) {
269-
return 0;
274+
return -1;
270275
}
271276

272277
return (strcmp(value, "1") == 0);
@@ -536,6 +541,7 @@ int copy_shared_library(
536541

537542
int main(int argc, char* argv[], char* envp[]) {
538543

544+
int status = 0;
539545
int err = ERR_SUCCESS;
540546

541547
size_t size = 0;
@@ -552,10 +558,6 @@ int main(int argc, char* argv[], char* envp[]) {
552558
int wants_runtime_rpath = 0;
553559
int wants_nz = 0;
554560

555-
#if defined(PINO)
556-
int wants_disable_shlib = 0;
557-
#endif
558-
559561
int address_sanitizer = 0;
560562
int stack_protector = 0;
561563
int verbose = 0;
@@ -568,6 +570,7 @@ int main(int argc, char* argv[], char* envp[]) {
568570
int wants_libitm = 0;
569571
int wants_librt = 0;
570572
int wants_libssp = 0;
573+
int wants_force_static = 0;
571574

572575
#if defined(OBGGCC)
573576
int require_atomic_library = 0;
@@ -644,17 +647,23 @@ int main(int argc, char* argv[], char* envp[]) {
644647
unsigned char ch = 0;
645648

646649
#if defined(OBGGCC)
647-
wants_system_libraries = get_env(WRAPPER_FLAVOR_NAME "_SYSTEM_LIBRARIES");
648-
wants_builtin_loader = get_env(WRAPPER_FLAVOR_NAME "_BUILTIN_LOADER");
650+
wants_system_libraries = get_env(WRAPPER_FLAVOR_NAME "_SYSTEM_LIBRARIES") == 1;
651+
wants_builtin_loader = get_env(WRAPPER_FLAVOR_NAME "_BUILTIN_LOADER") == 1;
649652
#endif
650653

651654
#if defined(PINO)
652-
wants_disable_shlib = get_env(WRAPPER_FLAVOR_NAME "_DISABLE_SHLIB");
655+
wants_force_static = 1;
653656
#endif
654657

655-
wants_nz = get_env(WRAPPER_FLAVOR_NAME "_NZ");
656-
wants_runtime_rpath = get_env(WRAPPER_FLAVOR_NAME "_RUNTIME_RPATH");
657-
verbose = get_env(WRAPPER_FLAVOR_NAME "_VERBOSE");
658+
status = get_env(WRAPPER_FLAVOR_NAME "_FORCE_STATIC");
659+
660+
if (status != -1) {
661+
wants_force_static = (status == 1);
662+
}
663+
664+
wants_nz = get_env(WRAPPER_FLAVOR_NAME "_NZ") == 1;
665+
wants_runtime_rpath = get_env(WRAPPER_FLAVOR_NAME "_RUNTIME_RPATH") == 1;
666+
verbose = get_env(WRAPPER_FLAVOR_NAME "_VERBOSE") == 1;
658667

659668
kargv = malloc(
660669
sizeof(*argv) * (
@@ -666,7 +675,8 @@ int main(int argc, char* argv[], char* envp[]) {
666675
1 + /* -D__clang_patchlevel__ */
667676
1 + /* -ffat-lto-objects / -fno-fat-lto-objects */
668677
1 + /* -fuse-ld=<linker> */
669-
2 + /* -Xlinker --no-rosegment (Android) */
678+
2 + /* -Xlinker --no-rosegment (Android 9 and bellow) */
679+
1 + /* -static-libgcc */
670680
1 /* NULL */
671681
)
672682
);
@@ -692,26 +702,14 @@ int main(int argc, char* argv[], char* envp[]) {
692702
} else if (strcmp(cur, GCC_OPT_STATIC_LIBCXX) == 0) {
693703
wants_libcxx = 1;
694704
wants_static_libcxx = 1;
695-
696-
#if defined(PINO)
697-
if (!wants_disable_shlib) {
698-
continue;
699-
}
700-
#endif
701705
} else if (strcmp(cur, GCC_OPT_STATIC_LIBGCC) == 0) {
702706
wants_libgcc = 1;
703707
wants_static_libgcc = 1;
704-
705-
#if defined(PINO)
706-
if (!wants_disable_shlib) {
707-
continue;
708-
}
709-
#endif
710708
} else if (strcmp(cur, GCC_OPT_L_ATOMIC) == 0) {
711709
wants_libatomic = 1;
712-
} else if (strcmp(cur, GCC_OPT_L_GOMP) == 0) {
710+
} else if (strcmp(cur, GCC_OPT_L_GOMP) == 0 || strcmp(cur, GCC_OPT_F_OPENMP) == 0 || strcmp(cur, GCC_OPT_F_OPENACC) == 0) {
713711
wants_libgomp = 1;
714-
} else if (strcmp(cur, GCC_OPT_L_ITM) == 0) {
712+
} else if (strcmp(cur, GCC_OPT_L_ITM) == 0 || strcmp(cur, GCC_OPT_F_GNU_TM) == 0) {
715713
wants_libitm = 1;
716714
} else if (strcmp(cur, GCC_OPT_L_RT) == 0) {
717715
have_rt_library = 1;
@@ -810,7 +808,7 @@ int main(int argc, char* argv[], char* envp[]) {
810808

811809
continue;
812810
} else if (strncmp(cur, GCC_OPT_SYSROOT, strlen(GCC_OPT_SYSROOT)) == 0) {
813-
/* Don't override the system root, Clang. That's our job. */
811+
/* Don't let Clang override the system root. */
814812
cur += strlen(GCC_OPT_SYSROOT);
815813
ch = *cur;
816814

@@ -869,6 +867,14 @@ int main(int argc, char* argv[], char* envp[]) {
869867
wants_libssp = 1;
870868
}
871869

870+
/*
871+
Both libitm and libgomp rely on libgcc. If we are going to statically link with those libraries,
872+
we should statically link with libgcc as well.
873+
*/
874+
if (wants_force_static && (wants_libitm || wants_libgomp) && !wants_static_libgcc) {
875+
kargv[kargc++] = (char*) GCC_OPT_STATIC_LIBGCC;
876+
}
877+
872878
app_filename = get_app_filename();
873879

874880
if (app_filename == NULL) {
@@ -1234,15 +1240,15 @@ int main(int argc, char* argv[], char* envp[]) {
12341240
strcat(sysroot_include_missing_directory, INCLUDE_MISSING_DIR);
12351241
}
12361242

1237-
sysroot_library_directory = malloc(strlen(sysroot_directory) + strlen(LIBRARY_DIR) + 1);
1243+
sysroot_library_directory = malloc(strlen(sysroot_directory) + strlen(STATIC_LIBRARY_DIR) + 1);
12381244

12391245
if (sysroot_library_directory == NULL) {
12401246
err = ERR_MEM_ALLOC_FAILURE;
12411247
goto end;
12421248
}
12431249

12441250
strcpy(sysroot_library_directory, sysroot_directory);
1245-
strcat(sysroot_library_directory, LIBRARY_DIR);
1251+
strcat(sysroot_library_directory, ((wants_force_static) ? STATIC_LIBRARY_DIR : LIBRARY_DIR));
12461252

12471253
if (wants_runtime_rpath) {
12481254
sysroot_runtime_directory = malloc(strlen(sysroot_library_directory) + strlen(PATHSEP_S) + strlen(GCC) + 1);
@@ -1558,7 +1564,7 @@ int main(int argc, char* argv[], char* envp[]) {
15581564
15591565
FIXME: Figure out a way to detect when we are not being invoked by Gradle and avoid copying the libraries when we are not building APKs.
15601566
*/
1561-
if (!wants_disable_shlib && known_clang(cc) && output_directory != NULL) {
1567+
if (!wants_force_static && known_clang(cc) && output_directory != NULL) {
15621568
/* libatomic */
15631569
err = copy_shared_library(sysroot_library_directory, output_directory, LIBATOMIC_SHARED, LIBATOMIC_SHARED);
15641570

0 commit comments

Comments
 (0)