Skip to content

Instantly share code, notes, and snippets.

@darealshinji
Last active April 12, 2017 14:44
Show Gist options
  • Save darealshinji/588f31f828e79284f1e77b5fe79f8c3c to your computer and use it in GitHub Desktop.
Save darealshinji/588f31f828e79284f1e77b5fe79f8c3c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define LINE_SIZE 255
#include <limits.h>
#ifndef LIBC6_ARCH
# if (__WORDSIZE == 64)
# define LIBC6_ARCH "libc6,x86-64"
# else
# define LIBC6_ARCH "libc6"
# endif
#endif
int main(int argc, char *argv[])
{
int ret;
FILE *f;
char command[LINE_SIZE];
char stdcxx_sys_lib[LINE_SIZE], gcc_sys_lib[LINE_SIZE];
char stdcxx_sys_sym[LINE_SIZE], gcc_sys_sym[LINE_SIZE];
char stdcxx_bundle_sym[LINE_SIZE], gcc_bundle_sym[LINE_SIZE];
int stdcxx_sys_ver=1, stdcxx_bundle_ver=0, gcc_sys_ver=1, gcc_bundle_ver=0;
char *stdcxx_bundle_lib = "./optional/libstdc++/libstdc++.so.6";
char *gcc_bundle_lib = "./optional/libgcc/libgcc_s.so.1";
const char *format = "tr '\\0' '\\n' < '%s' | grep -e '%s' | tail -n1";
#define SCANLIB(lib,sym,regex) \
sprintf(command, format, lib, regex); \
f = popen(command, "r"); \
ret = fscanf(f, "%s", sym); (void)ret; \
pclose(f);
if (access(stdcxx_bundle_lib, F_OK) == 0) {
f = popen("ldconfig -p | grep 'libstdc++.so.6 (" LIBC6_ARCH ")' | awk 'NR==1{print $NF}'", "r");
ret = fscanf(f, "%s", stdcxx_sys_lib); (void)ret;
pclose(f);
if (access(stdcxx_sys_lib, F_OK) == 0) {
SCANLIB(stdcxx_sys_lib, stdcxx_sys_sym, "^GLIBCXX_3\\.4");
SCANLIB(stdcxx_bundle_lib, stdcxx_bundle_sym, "^GLIBCXX_3\\.4");
stdcxx_sys_ver = atoi(stdcxx_sys_sym+12);
stdcxx_bundle_ver = atoi(stdcxx_bundle_sym+12);
printf("%s ==> %s (%d)\n", stdcxx_sys_lib, stdcxx_sys_sym, stdcxx_sys_ver);
printf("%s ==> %s (%d)\n\n", stdcxx_bundle_lib, stdcxx_bundle_sym, stdcxx_bundle_ver);
}
}
if (access(gcc_bundle_lib, F_OK) == 0) {
f = popen("ldconfig -p | grep 'libgcc_s.so.1 (" LIBC6_ARCH ")' | awk 'NR==1{print $NF}'", "r");
ret = fscanf(f, "%s", gcc_sys_lib); (void)ret;
pclose(f);
if (access(gcc_sys_lib, F_OK) == 0) {
SCANLIB(gcc_sys_lib, gcc_sys_sym, "^GCC_[0-9]\\.[0-9]");
SCANLIB(gcc_bundle_lib, gcc_bundle_sym, "^GCC_[0-9]\\.[0-9]");
gcc_sys_ver = atoi(gcc_sys_sym+4) * 100 + atoi(gcc_sys_sym+6) * 10 + atoi(gcc_sys_sym+8);
gcc_bundle_ver = atoi(gcc_bundle_sym+4) * 100 + atoi(gcc_bundle_sym+6) * 10 + atoi(gcc_bundle_sym+8);
printf("%s ==> %s (%d)\n", gcc_sys_lib, gcc_sys_sym, gcc_sys_ver);
printf("%s ==> %s (%d)\n\n", gcc_bundle_lib, gcc_bundle_sym, gcc_bundle_ver);
}
}
#undef SCANLIB
if (stdcxx_bundle_ver > stdcxx_sys_ver) {
printf("using bundled libstdc++.so.6\n");
} else {
printf("using system libstdc++.so.6\n");
}
if (gcc_bundle_ver > gcc_sys_ver) {
printf("using bundled libgcc_s.so.1\n");
} else {
printf("using system libgcc_s.so.1\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment