123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <string.h>
- #include <unistd.h>
- #include <libgen.h>
- #include "gcc-uClibc.h"
- static char *our_usr_lib_path = "-L"UCLIBC_DEVEL_PREFIX"/lib";
- static char static_linking[] = "-static";
- static char nostdinc[] = "-nostdinc";
- static char nostartfiles[] = "-nostartfiles";
- static char nodefaultlibs[] = "-nodefaultlibs";
- static char nostdlib[] = "-nostdlib";
- #ifdef __UCLIBC_CTOR_DTOR__
- static char nostdinc_plus[] = "-nostdinc++";
- #endif
- extern void *xmalloc(size_t size)
- {
- void *ptr = malloc(size);
- if (!ptr) {
- fprintf(stderr, "memory exhausted");
- exit(EXIT_FAILURE);
- }
- return ptr;
- }
- void xstrcat(char **string, ...)
- {
- const char *c;
- va_list p;
-
- va_start(p, string);
- *string = xmalloc(BUFSIZ);
- **string = '\0';
- while(1) {
- if (!(c = va_arg(p, const char *)))
- break;
- strcat(*string, c);
- }
- va_end(p);
- }
- int main(int argc, char **argv)
- {
- int use_build_dir = 0, linking = 1, use_static_linking = 0;
- int use_stdinc = 1, use_start = 1, use_stdlib = 1, use_pic = 0;
- int source_count = 0, use_rpath = 0, verbose = 0;
- int i, j, k, l, m, n;
- char ** gcc_argv;
- char ** gcc_argument;
- char ** libraries;
- char ** libpath;
- char *dlstr;
- char *incstr;
- char *devprefix;
- char *builddir;
- char *libstr;
- char *build_dlstr;
- char *ep;
- char *rpath_link[2];
- char *rpath[2];
- char *uClibc_inc[2];
- char *our_lib_path[2];
- char *crt0_path[2];
- const char *application_name;
- #ifdef __UCLIBC_CTOR_DTOR__
- char *crti_path[2];
- char *crtn_path[2];
- int len;
- int ctor_dtor = 1, cplusplus = 0, use_nostdinc_plus = 0;
- char *GPLUSPLUS_BIN = NULL;
- #endif
- application_name = basename(argv[0]);
- if (application_name[0] == '-')
- application_name++;
- #ifdef __UCLIBC_CTOR_DTOR__
-
- len = strlen(application_name);
- if ((strcmp(application_name+len-3, "g++")==0) ||
- (strcmp(application_name+len-3, "c++")==0)) {
- char *gcc_bin = GCC_BIN;
- len = strlen(gcc_bin);
- if (strcmp(gcc_bin+len-3, "gcc")==0) {
- GPLUSPLUS_BIN = strdup(gcc_bin);
- GPLUSPLUS_BIN[len-1]='+';
- GPLUSPLUS_BIN[len-2]='+';
- }
- cplusplus = 1;
- use_nostdinc_plus = 1;
- }
- #endif
- devprefix = getenv("UCLIBC_DEVEL_PREFIX");
- if (!devprefix) {
- devprefix = UCLIBC_DEVEL_PREFIX;
- }
- builddir = getenv("UCLIBC_BUILD_DIR");
- if (!builddir) {
- builddir = UCLIBC_BUILD_DIR;
- }
- incstr = getenv("UCLIBC_GCC_INC");
- libstr = getenv("UCLIBC_GCC_LIB");
- xstrcat(&(rpath_link[0]), "-Wl,-rpath-link,", devprefix, "/lib", NULL);
- xstrcat(&(rpath_link[1]), "-Wl,-rpath-link,", builddir, "/lib", NULL);
- xstrcat(&(rpath[0]), "-Wl,-rpath,", devprefix, "/lib", NULL);
- xstrcat(&(rpath[1]), "-Wl,-rpath,", builddir, "/lib", NULL);
- xstrcat(&(uClibc_inc[0]), devprefix, "/include/", NULL);
- xstrcat(&(uClibc_inc[1]), builddir, "/include/", NULL);
- #ifdef __UCLIBC_CTOR_DTOR__
- xstrcat(&(crt0_path[0]), devprefix, "/lib/crt1.o", NULL);
- xstrcat(&(crt0_path[1]), builddir, "/lib/crt1.o", NULL);
- xstrcat(&(crti_path[0]), devprefix, "/lib/crti.o", NULL);
- xstrcat(&(crti_path[1]), builddir, "/lib/crti.o", NULL);
- xstrcat(&(crtn_path[0]), devprefix, "/lib/crtn.o", NULL);
- xstrcat(&(crtn_path[1]), builddir, "/lib/crtn.o", NULL);
- #else
- xstrcat(&(crt0_path[0]), devprefix, "/lib/crt0.o", NULL);
- xstrcat(&(crt0_path[1]), builddir, "/lib/crt0.o", NULL);
- #endif
- xstrcat(&(our_lib_path[0]), "-L", devprefix, "/lib", NULL);
- xstrcat(&(our_lib_path[1]), "-L", builddir, "/lib", NULL);
- build_dlstr = "-Wl,--dynamic-linker," BUILD_DYNAMIC_LINKER;
- dlstr = getenv("UCLIBC_GCC_DLOPT");
- if (!dlstr) {
- dlstr = "-Wl,--dynamic-linker," DYNAMIC_LINKER;
- }
- ep = getenv("UCLIBC_GCC");
- if (!ep) {
- ep = "";
- }
- if (strstr(ep,"build") != 0) {
- use_build_dir = 1;
- }
- if (strstr(ep,"rpath") != 0) {
- use_rpath = 1;
- }
- m = 0;
- libraries = __builtin_alloca(sizeof(char*) * (argc));
- libraries[m] = '\0';
- n = 0;
- libpath = __builtin_alloca(sizeof(char*) * (argc));
- libpath[n] = '\0';
- for ( i = 1 ; i < argc ; i++ ) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'c':
- case 'S':
- case 'E':
- case 'M':
- linking = 0;
- break;
- case 'L':
- libpath[n++] = argv[i];
- libpath[n] = '\0';
- if (argv[i][2] == 0) {
- argv[i] = '\0';
- libpath[n++] = argv[++i];
- libpath[n] = '\0';
- }
- argv[i] = '\0';
- break;
- case 'l':
- libraries[m++] = argv[i];
- libraries[m] = '\0';
- argv[i] = '\0';
- break;
- case 'v':
- if (argv[i][2] == 0) verbose = 1;
- printf("Invoked as %s\n", argv[0]);
- break;
- case 'n':
- if (strcmp(nostdinc,argv[i]) == 0) {
- use_stdinc = 0;
- } else if (strcmp(nostartfiles,argv[i]) == 0) {
- use_start = 0;
- } else if (strcmp(nodefaultlibs,argv[i]) == 0) {
- use_stdlib = 0;
- argv[i] = '\0';
- } else if (strcmp(nostdlib,argv[i]) == 0) {
- use_start = 0;
- use_stdlib = 0;
- }
- #ifdef __UCLIBC_CTOR_DTOR__
- else if (strcmp(nostdinc_plus,argv[i]) == 0) {
- if (cplusplus==1) {
- use_nostdinc_plus = 0;
- }
- }
- #endif
- break;
- case 's':
- if (strstr(argv[i],static_linking) != NULL) {
- use_static_linking = 1;
- }
- if (strcmp("-shared",argv[i]) == 0) {
- use_start = 0;
- use_pic = 1;
- }
- break;
- case 'W':
- if (strncmp("-Wl,",argv[i],4) == 0) {
- if (strstr(argv[i],static_linking) != 0) {
- use_static_linking = 1;
- }
- if (strstr(argv[i],"--dynamic-linker") != 0) {
- dlstr = 0;
- }
- }
- break;
- case 'f':
-
- if (strcmp("-fPIC",argv[i]) == 0) {
- use_pic = 1;
- } else if (strcmp("-fpic",argv[i]) == 0) {
- use_pic = 1;
- }
- break;
- case '-':
- if (strstr(argv[i]+1,static_linking) != NULL) {
- use_static_linking = 1;
- argv[i]='\0';
- } else if (strcmp("--uclibc-use-build-dir",argv[i]) == 0) {
- use_build_dir = 1;
- argv[i]='\0';
- } else if (strcmp("--uclibc-use-rpath",argv[i]) == 0) {
- use_rpath = 1;
- argv[i]='\0';
- }
- #ifdef __UCLIBC_CTOR_DTOR__
- else if (strcmp("--uclibc-no-ctors",argv[i]) == 0) {
- ctor_dtor = 0;
- argv[i]='\0';
- }
- #endif
- break;
- }
- } else {
- ++source_count;
- }
- }
- gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 64));
- gcc_argument = __builtin_alloca(sizeof(char*) * (argc + 20));
- i = 0; k = 0;
- #ifdef __UCLIBC_CTOR_DTOR__
- if (cplusplus && GPLUSPLUS_BIN)
- gcc_argv[i++] = GPLUSPLUS_BIN;
- else
- #endif
- gcc_argv[i++] = GCC_BIN;
-
- for ( j = 1 ; j < argc ; j++ ) {
- if (argv[j]=='\0') {
- continue;
- } else {
- gcc_argument[k++] = argv[j];
- gcc_argument[k] = '\0';
- }
- }
- if (linking && source_count) {
- #if defined __HAVE_ELF__ && ! defined __UCLIBC_HAS_MMU__
- gcc_argv[i++] = "-Wl,-elf2flt";
- #endif
- gcc_argv[i++] = nostdlib;
- if (use_static_linking) {
- gcc_argv[i++] = static_linking;
- }
- if (!use_static_linking) {
- if (dlstr && use_build_dir) {
- gcc_argv[i++] = build_dlstr;
- } else if (dlstr) {
- gcc_argv[i++] = dlstr;
- }
- if (use_rpath) {
- gcc_argv[i++] = rpath[use_build_dir];
- }
- }
- for ( l = 0 ; l < n ; l++ ) {
- if (libpath[l]) gcc_argv[i++] = libpath[l];
- }
- gcc_argv[i++] = rpath_link[use_build_dir];
- if( libstr )
- gcc_argv[i++] = libstr;
- gcc_argv[i++] = our_lib_path[use_build_dir];
- if (!use_build_dir) {
- gcc_argv[i++] = our_usr_lib_path;
- }
- }
- if (use_stdinc && source_count) {
- gcc_argv[i++] = nostdinc;
- #ifdef __UCLIBC_CTOR_DTOR__
- if (cplusplus) {
- char *cppinc;
- if (use_nostdinc_plus) {
- gcc_argv[i++] = nostdinc_plus;
- }
- xstrcat(&cppinc, uClibc_inc[use_build_dir], "g++/", NULL);
- gcc_argv[i++] = "-isystem";
- gcc_argv[i++] = cppinc;
- xstrcat(&cppinc, uClibc_inc[use_build_dir], "g++-v3/", NULL);
- gcc_argv[i++] = "-isystem";
- gcc_argv[i++] = cppinc;
- }
- #endif
- gcc_argv[i++] = "-isystem";
- gcc_argv[i++] = uClibc_inc[use_build_dir];
- gcc_argv[i++] = "-iwithprefix";
- gcc_argv[i++] = "include";
- if( incstr )
- gcc_argv[i++] = incstr;
- }
- if (linking && source_count) {
- if (use_start) {
- #ifdef __UCLIBC_CTOR_DTOR__
- if (ctor_dtor) {
- gcc_argv[i++] = crti_path[use_build_dir];
- if (use_pic) {
- gcc_argv[i++] = LIBGCC_DIR "crtbeginS.o" ;
- } else {
- gcc_argv[i++] = LIBGCC_DIR "crtbegin.o" ;
- }
- }
- #endif
- gcc_argv[i++] = crt0_path[use_build_dir];
- }
- for ( l = 0 ; l < k ; l++ ) {
- if (gcc_argument[l]) gcc_argv[i++] = gcc_argument[l];
- }
- if (use_stdlib) {
-
- gcc_argv[i++] = "-lgcc";
- }
- for ( l = 0 ; l < m ; l++ ) {
- if (libraries[l]) gcc_argv[i++] = libraries[l];
- }
- if (use_stdlib) {
- #ifdef __UCLIBC_CTOR_DTOR__
- if (cplusplus) {
- gcc_argv[ i++ ] = "-lstdc++";
- gcc_argv[ i++ ] = "-lm";
- }
- #endif
- gcc_argv[i++] = "-lc";
- gcc_argv[i++] = "-lgcc";
-
- }
- #ifdef __UCLIBC_CTOR_DTOR__
- if (use_start) {
- if (ctor_dtor) {
- if (use_pic) {
- gcc_argv[i++] = LIBGCC_DIR "crtendS.o" ;
- } else {
- gcc_argv[i++] = LIBGCC_DIR "crtend.o" ;
- }
- gcc_argv[i++] = crtn_path[use_build_dir];
- }
- }
- #endif
- } else {
- for ( l = 0 ; l < k ; l++ ) {
- if (gcc_argument[l]) gcc_argv[i++] = gcc_argument[l];
- }
- }
- gcc_argv[i++] = NULL;
- if (verbose) {
- for ( j = 0 ; gcc_argv[j] ; j++ ) {
- printf("arg[%2i] = %s\n", j, gcc_argv[j]);
- }
- fflush(stdout);
- }
-
- #ifdef __UCLIBC_CTOR_DTOR__
- if (cplusplus && GPLUSPLUS_BIN)
- return execvp(GPLUSPLUS_BIN, gcc_argv);
- else
- #endif
- return execvp(GCC_BIN, gcc_argv);
- }
|