# HG changeset patch # User Rob Landley # Date 1403521335 18000 # Node ID 586b011cb7065d64cac796e25eb1d0be5424eb7f # Parent c1addb6ebb848091fe019abdd2161241a43fc5ba Swap old wrapper for new. diff -r c1addb6ebb84 -r 586b011cb706 sources/toys/ccwrap.c --- a/sources/toys/ccwrap.c Mon Jun 23 06:01:02 2014 -0500 +++ b/sources/toys/ccwrap.c Mon Jun 23 06:02:15 2014 -0500 @@ -1,480 +1,459 @@ -/* vi: set ts=4 :*/ -/* - * Copyright (C) 2000 Manuel Novoa III - * Copyright (C) 2002-2003 Erik Andersen - * Copyright (C) 2006-2010 Rob Landley +/* Copyright 2013 Rob Landley * - * Wrapper to use make a C compiler relocatable. - * - * Licensed under GPLv2. + * C compiler wrapper. Parses command line, supplies path information for + * headers and libraries. */ -// No, we don't need to check the return value from asprintf(). - #undef _FORTIFY_SOURCE -#define _GNU_SOURCE -#include +#include +#include #include #include -#include #include -#include -#include -#include #include -#include +#include +#include + +// Default to musl +#ifndef DYNAMIC_LINKER +#define DYNAMIC_LINKER "/lib/libc.so" +#endif + +// Some plumbing from toybox + +void *xmalloc(long len) +{ + void *ret = malloc(len); + + if (!ret) { + fprintf(stderr, "bad malloc\n"); + exit(1); + } +} -static char *topdir, *devprefix; -static char nostdinc[] = "-nostdinc"; -static char nostdlib[] = "-nostdlib"; +// Die unless we can allocate enough space to sprintf() into. +char *xmprintf(char *format, ...) +{ + va_list va, va2; + int len; + char *ret; + + va_start(va, format); + va_copy(va2, va); -// For C++ -static char nostdinc_plus[] = "-nostdinc++"; + // How long is it? + len = vsnprintf(0, 0, format, va); + len++; + va_end(va); + + // Allocate and do the sprintf() + ret = xmalloc(len); + vsnprintf(ret, len, format, va2); + va_end(va2); + + return ret; +} // Confirm that a regular file exists, and (optionally) has the executable bit. int is_file(char *filename, int has_exe) { - // Confirm it has the executable bit set, if necessary. - if (!has_exe || !access(filename, X_OK)) { - struct stat st; + // Confirm it has the executable bit set, if necessary. + if (!has_exe || !access(filename, X_OK)) { + struct stat st; - // Confirm it exists and is not a directory. - if (!stat(filename, &st) && S_ISREG(st.st_mode)) return 1; - } - return 0; + // Confirm it exists and is not a directory. + if (!stat(filename, &st) && S_ISREG(st.st_mode)) return 1; + } + return 0; } -// Find an executable in a colon-separated path - +// Find a file in a colon-separated path char *find_in_path(char *path, char *filename, int has_exe) { - char *cwd = getcwd(NULL, 0); + char *cwd = getcwd(0, 0); - if (index(filename, '/') && is_file(filename, has_exe)) - return realpath(filename, NULL); + if (index(filename, '/') && is_file(filename, has_exe)) + return realpath(filename, 0); + + while (path) { + char *str, *next = path ? index(path, ':') : 0; + int len = next ? next-path : strlen(path); - while (path) { - char *str, *next = path ? index(path, ':') : NULL; - int len = next ? next-path : strlen(path); + if (!len) str = xmprintf("%s/%s", cwd, filename); + else str = xmprintf("%*s/%s", len, path, filename); - // The +3 is a corner case: if strlen(filename) is 1, make sure we - // have enough space to append ".." to make topdir. - str = malloc(strlen(filename) + (len ? len : strlen(cwd)) + 3); - if (!len) sprintf(str, "%s/%s", cwd, filename); - else { - char *str2 = str; + // If it's not a directory, return it. + if (is_file(str, has_exe)) { + char *s = realpath(str, 0); + + free(str); + free(cwd); + + return s; + } else free(str); - strncpy(str, path, len); - str2 = str+len; - *(str2++) = '/'; - strcpy(str2, filename); - } + if (next) next++; + path = next; + } + free(cwd); + + return NULL; +} + +struct dlist { + struct dlist *next, *prev; + char *str; +}; - // If it's not a directory, return it. - if (is_file(str, has_exe)) { - char *s = realpath(str, NULL); - free(str); - free(cwd); - return s; - } else free(str); +// Append to end of doubly linked list (in-order insertion) +void dlist_add(struct dlist **list, char *str) +{ + struct dlist *new = xmalloc(sizeof(struct dlist)); - if (next) next++; - path = next; - } - free(cwd); + new->str = str; + if (*list) { + new->next = *list; + new->prev = (*list)->prev; + (*list)->prev->next = new; + (*list)->prev = new; + } else *list = new->next = new->prev = new; - return NULL; + *list = new; } // Some compiler versions don't provide separate T and S versions of begin/end, // so fall back to the base version if they're not there. -char *find_TSpath(char *base, int use_shared, int use_static_linking) +char *find_TSpath(char *base, char *top, int use_shared, int use_static_linking) { - int i; - char *temp; + int i; + char *temp; - asprintf(&temp, base, devprefix, - use_shared ? "S.o" : use_static_linking ? "T.o" : ".o"); + temp = xmprintf(base, top, + use_shared ? "S.o" : use_static_linking ? "T.o" : ".o"); - if (!is_file(temp, 0)) { - free(temp); - asprintf(&temp, base, devprefix, ".o"); - } + if (!is_file(temp, 0)) { + free(temp); + temp = xmprintf(base, top, ".o"); + } - return temp; + return temp; } -int main(int argc, char **argv) -{ - int linking = 1, use_static_linking = 0, use_shared_libgcc, used_x = 0; - int use_stdinc = 1, use_start = 1, use_stdlib = 1, use_shared = 0; - int source_count = 0, verbose = 0; - int i, argcnt, lplen; - char **cc_argv, **libpath; - char *dlstr; - char *cc, *toolprefix; - char *debug_wrapper=getenv("CCWRAP_DEBUG"); - // For C++ +enum { + Clibccso, Clink, Cprofile, Cshared, Cstart, Cstatic, Cstdinc, Cstdlib, + Cverbose, Cx, Cdashdash, - char *cpp = NULL; - int prefixlen, ctor_dtor = 1, use_nostdinc_plus = 0; - - // For profiling - int profile = 0; + CPctordtor, CP, CPstdinc +}; - if(debug_wrapper) { - fprintf(stderr,"incoming: "); - for(cc_argv=argv;*cc_argv;cc_argv++) - fprintf(stderr,"%s ",*cc_argv); - fprintf(stderr,"\n\n"); - } +#define MASK_BIT(X) (1< /usr/bin), so append ".." - // instead of trucating the path. - strcat(topdir,"/.."); - } - - // What's the name of the C compiler we're wrapping? (It may have a - // cross-prefix.) - cc = getenv("CCWRAP_CC"); - if (!cc) cc = "rawcc"; + if (getenv("CCWRAP_DEBUG")) { + SET_FLAG(Cverbose); + fprintf(stderr, "incoming: "); + for (i=0; i=3 && !strcmp(toolprefix+prefixlen-3, "gcc")) prefixlen -= 3; - else if (!strcmp(toolprefix+prefixlen-2, "cc")) prefixlen -= 2; - else if (!strcmp(toolprefix+prefixlen-2, "ld")) { - prefixlen -= 2; + // Find the cannonical path to the directory containing our executable + topdir = find_in_path(getenv("PATH"), *argv, 1); + if (!topdir || !(temp = rindex(topdir, '/')) || strlen(*argv)<2) { + fprintf(stderr, "Can't find %s in $PATH (did you export it?)\n", *argv); + exit(1); + } + // We want to strip off the bin/ but the path we followed can end with + // a symlink, so append .. instead. + strcpy(++temp, ".."); - // TODO: put support for wrapping the linker here. - } else if (!strcmp(toolprefix+prefixlen-3, "cpp")) { - prefixlen -=3; - linking = 0; + // Add our binary's directory and the tools directory to $PATH so gcc's + // convulsive flailing probably blunders through here first. + // Note: do this before reading topdir from environment variable, because + // toolchain binaries go with wrapper even if headers/libraries don't. + temp = getenv("PATH"); + if (!temp) temp = ""; + temp = xmprintf("PATH=%s/bin:%s/tools/bin:%s", topdir, topdir, temp); + putenv(temp); - // Wrapping the c++ compiler? - } else if (!strcmp(toolprefix+prefixlen-2, "++")) { - int len = strlen(cc); - cpp = alloca(len+1); - strcpy(cpp, cc); - cpp[len-1]='+'; - cpp[len-2]='+'; - use_nostdinc_plus = 1; - } + // Override header/library search path with environment variable? + temp = getenv("CCWRAP_TOPDIR"); + if (!temp) { + cc = xmprintf("%sCCWRAP_TOPDIR", ccprefix); - devprefix = getenv("CCWRAP_TOPDIR"); - if (!devprefix) { - char *temp, *temp2; - asprintf(&temp, "%.*sCCWRAP_TOPDIR", prefixlen, toolprefix); - temp2 = temp; - while (*temp2) { - if (*temp2 == '-') *temp2='_'; - temp2++; - } - devprefix = getenv(temp); - } - if (!devprefix) devprefix = topdir; - - // Do we have libgcc_s.so? - - asprintf(&dlstr, "%s/lib/libgcc_s.so", devprefix); - use_shared_libgcc = is_file(dlstr, 0); - free(dlstr); + for (i=0; cc[i]; i++) if (cc[i] == '-') cc[i]='_'; + temp = getenv(cc); + free(cc); + } + if (temp) { + free(topdir); + topdir = temp; + } + + // Name of the C compiler we're wrapping. + cc = getenv("CCWRAP_CC"); + if (!cc) cc = "rawcc"; + + // figure out cross compiler prefix + i = strlen(ccprefix = basename(*argv)); + if (i<2) { + fprintf(stderr, "Bad name '%s'\n", ccprefix); + exit(1); + } + if (!strcmp("++", ccprefix+i-2)) { + cc = "raw++"; + SET_FLAG(CP); + SET_FLAG(CPstdinc); + if (i<3) exit(1); + i -= 3; + } else if (!strcmp("gcc", ccprefix+i-3)) i -= 3; // TODO: yank + else if (!strcmp("cc", ccprefix+i-2)) i-=2; + else if (!strcmp("cpp", ccprefix+i-3)) { + i -= 3; + CLEAR_FLAG(Clink); + } else return 1; // TODO: wrap ld + if (!(ccprefix = strndup(ccprefix, i))) exit(1); - // Figure out where the dynamic linker is. - dlstr = getenv("CCWRAP_DYNAMIC_LINKER"); - if (!dlstr) dlstr = "/lib/ld-uClibc.so.0"; - asprintf(&dlstr, "-Wl,--dynamic-linker,%s", dlstr); - - lplen = 0; - libpath = alloca(sizeof(char*) * (argc)); - libpath[lplen] = 0; - - // Parse the incoming compiler arguments. - - for (i=1; inext->next; + libs->prev->next = 0; - case 'f': - // profiling - if (strcmp("-fprofile-arcs",argv[i]) == 0) profile = 1; - break; - - case 'x': - used_x++; - break; - - // --longopts + // Either display the list, or find first hit. + for (dl = libs; dl; dl = dl->next) { + if (show) printf(":%s" + (dl==libs), dl->str); + else if (!access(dl->str, F_OK)) break; + } + if (dl) printf("%s", dl->str); + printf("\n"); - case '-': - if (!strncmp(argv[i],"--print-",8) - || !strncmp(argv[i],"--static",8) - || !strncmp(argv[i],"--shared",8)) - { - argv[i]++; - i--; - continue; - } else if (!strcmp("--no-ctors", argv[i])) { - ctor_dtor = 0; - argv[i] = 0; - } - break; - } - // assume it is an existing source file - } else ++source_count; - } - - argcnt = 0; + return 0; + } else if (!strcmp(c, "pg")) SET_FLAG(Cprofile); + } else if (*c == 's') { + keepc--; + if (!strcmp(c, "shared")) { + CLEAR_FLAG(Cstart); + SET_FLAG(Cshared); + } else if (!strcmp(c, "static")) { + SET_FLAG(Cstatic); + CLEAR_FLAG(Clibccso); + } else if (!strcmp(c, "shared-libgcc")) SET_FLAG(Clibccso); + else if (!strcmp(c, "static-libgcc")) CLEAR_FLAG(Clibccso); + else keepc++; + } else if (*c == 'v' && !c[1]) { + SET_FLAG(Cverbose); + printf("ccwrap: %s\n", topdir); + } else if (!strncmp(c, "Wl,", 3)) { + temp = strstr(c, ",-static"); + if (temp && (!temp[8] || temp[8]==',')) { + SET_FLAG(Cstatic); + CLEAR_FLAG(Clibccso); + } + // This argument specifies dynamic linker, so we shouldn't. + if (strstr(c, "--dynamic-linker")) dynlink = 0; + } else if (*c == 'x') SET_FLAG(Cx); + } - cc_argv[argcnt++] = cpp ? cpp : cc; + // Initialize argument list for exec call - if (cpp) cc_argv[argcnt++] = "-fno-use-cxa-atexit"; +// what's a good outc size? - if (linking && source_count) { -//#if defined HAS_ELF && ! defined HAS_MMU -// cc_argv[argcnt++] = "-Wl,-elf2flt"; -//#endif - cc_argv[argcnt++] = nostdlib; - if (use_static_linking) cc_argv[argcnt++] = "-static"; - else if (dlstr) cc_argv[argcnt++] = dlstr; - for (i=0; inext->next; + libs->prev->next = 0; + for (; libs; libs = libs->next) + outv[outc++] = xmprintf("-L%s", libs->str); + outv[outc++] = xmprintf("-Wl,-rpath-link,%s/lib", topdir); // TODO: in? - if (ctor_dtor) { - asprintf(cc_argv+(argcnt++), "%s/lib/crti.o", devprefix); - cc_argv[argcnt++]=find_TSpath("%s/cc/lib/crtbegin%s", - use_shared, use_static_linking); - } - if (use_start && !profile) - asprintf(cc_argv+(argcnt++), "%s/lib/%scrt1.o", devprefix, use_shared ? "S" : ""); - - // Add remaining unclaimed arguments. - - for (i=1; i + * + * Wrapper to use make a C compiler relocatable. + * + * Licensed under GPLv2. + */ + +// No, we don't need to check the return value from asprintf(). + +#undef _FORTIFY_SOURCE + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static char *topdir, *devprefix; +static char nostdinc[] = "-nostdinc"; +static char nostdlib[] = "-nostdlib"; + +// For C++ +static char nostdinc_plus[] = "-nostdinc++"; + +// Confirm that a regular file exists, and (optionally) has the executable bit. +int is_file(char *filename, int has_exe) +{ + // Confirm it has the executable bit set, if necessary. + if (!has_exe || !access(filename, X_OK)) { + struct stat st; + + // Confirm it exists and is not a directory. + if (!stat(filename, &st) && S_ISREG(st.st_mode)) return 1; + } + return 0; +} + +// Find an executable in a colon-separated path + +char *find_in_path(char *path, char *filename, int has_exe) +{ + char *cwd = getcwd(NULL, 0); + + if (index(filename, '/') && is_file(filename, has_exe)) + return realpath(filename, NULL); + + while (path) { + char *str, *next = path ? index(path, ':') : NULL; + int len = next ? next-path : strlen(path); + + // The +3 is a corner case: if strlen(filename) is 1, make sure we + // have enough space to append ".." to make topdir. + str = malloc(strlen(filename) + (len ? len : strlen(cwd)) + 3); + if (!len) sprintf(str, "%s/%s", cwd, filename); + else { + char *str2 = str; + + strncpy(str, path, len); + str2 = str+len; + *(str2++) = '/'; + strcpy(str2, filename); + } + + // If it's not a directory, return it. + if (is_file(str, has_exe)) { + char *s = realpath(str, NULL); + free(str); + free(cwd); + return s; + } else free(str); + + if (next) next++; + path = next; + } + free(cwd); + + return NULL; +} + +// Some compiler versions don't provide separate T and S versions of begin/end, +// so fall back to the base version if they're not there. + +char *find_TSpath(char *base, int use_shared, int use_static_linking) +{ + int i; + char *temp; + + asprintf(&temp, base, devprefix, + use_shared ? "S.o" : use_static_linking ? "T.o" : ".o"); + + if (!is_file(temp, 0)) { + free(temp); + asprintf(&temp, base, devprefix, ".o"); + } + + return temp; +} + +int main(int argc, char **argv) +{ + int linking = 1, use_static_linking = 0, use_shared_libgcc, used_x = 0; + int use_stdinc = 1, use_start = 1, use_stdlib = 1, use_shared = 0; + int source_count = 0, verbose = 0; + int i, argcnt, lplen; + char **cc_argv, **libpath; + char *dlstr; + char *cc, *toolprefix; + char *debug_wrapper=getenv("CCWRAP_DEBUG"); + + // For C++ + + char *cpp = NULL; + int prefixlen, ctor_dtor = 1, use_nostdinc_plus = 0; + + // For profiling + int profile = 0; + + if(debug_wrapper) { + fprintf(stderr,"incoming: "); + for(cc_argv=argv;*cc_argv;cc_argv++) + fprintf(stderr,"%s ",*cc_argv); + fprintf(stderr,"\n\n"); + } + + // Allocate space for new command line + cc_argv = alloca(sizeof(char*) * (argc + 128)); + + // What directory is the wrapper script in? + if(!(topdir = find_in_path(getenv("PATH"), argv[0], 1))) { + fprintf(stderr, "can't find %s in $PATH (did you export it?)\n", argv[0]); + exit(1); + } else { + char *path = getenv("PATH"), *temp; + + if (!path) path = ""; + + // Add that directory to the start of $PATH. (Better safe than sorry.) + *rindex(topdir,'/') = 0; + asprintf(&temp,"PATH=%s:%s/../tools/bin:%s",topdir,topdir,path); + putenv(temp); + + // The directory above the wrapper script should have include, cc, + // and lib directories. However, the script could have a symlink + // pointing to its directory (ala /bin -> /usr/bin), so append ".." + // instead of trucating the path. + strcat(topdir,"/.."); + } + + // What's the name of the C compiler we're wrapping? (It may have a + // cross-prefix.) + cc = getenv("CCWRAP_CC"); + if (!cc) cc = "rawcc"; + + // Check end of name, since there could be a cross-prefix on the thing + toolprefix = strrchr(argv[0], '/'); + if (!toolprefix) toolprefix = argv[0]; + else toolprefix++; + + prefixlen = strlen(toolprefix); + if (prefixlen>=3 && !strcmp(toolprefix+prefixlen-3, "gcc")) prefixlen -= 3; + else if (!strcmp(toolprefix+prefixlen-2, "cc")) prefixlen -= 2; + else if (!strcmp(toolprefix+prefixlen-2, "ld")) { + prefixlen -= 2; + + // TODO: put support for wrapping the linker here. + } else if (!strcmp(toolprefix+prefixlen-3, "cpp")) { + prefixlen -=3; + linking = 0; + + // Wrapping the c++ compiler? + } else if (!strcmp(toolprefix+prefixlen-2, "++")) { + int len = strlen(cc); + cpp = alloca(len+1); + strcpy(cpp, cc); + cpp[len-1]='+'; + cpp[len-2]='+'; + use_nostdinc_plus = 1; + } + + devprefix = getenv("CCWRAP_TOPDIR"); + if (!devprefix) { + char *temp, *temp2; + asprintf(&temp, "%.*sCCWRAP_TOPDIR", prefixlen, toolprefix); + temp2 = temp; + while (*temp2) { + if (*temp2 == '-') *temp2='_'; + temp2++; + } + devprefix = getenv(temp); + } + if (!devprefix) devprefix = topdir; + + // Do we have libgcc_s.so? + + asprintf(&dlstr, "%s/lib/libgcc_s.so", devprefix); + use_shared_libgcc = is_file(dlstr, 0); + free(dlstr); + + // Figure out where the dynamic linker is. + dlstr = getenv("CCWRAP_DYNAMIC_LINKER"); + if (!dlstr) dlstr = "/lib/ld-uClibc.so.0"; + asprintf(&dlstr, "-Wl,--dynamic-linker,%s", dlstr); + + lplen = 0; + libpath = alloca(sizeof(char*) * (argc)); + libpath[lplen] = 0; + + // Parse the incoming compiler arguments. + + for (i=1; i - * - * C compiler wrapper. Parses command line, supplies path information for - * headers and libraries. - */ - -#undef _FORTIFY_SOURCE - -#include -#include -#include -#include -#include -#include -#include -#include - -// Default to musl -#ifndef DYNAMIC_LINKER -#define DYNAMIC_LINKER "/lib/libc.so" -#endif - -// Some plumbing from toybox - -void *xmalloc(long len) -{ - void *ret = malloc(len); - - if (!ret) { - fprintf(stderr, "bad malloc\n"); - exit(1); - } -} - -// Die unless we can allocate enough space to sprintf() into. -char *xmprintf(char *format, ...) -{ - va_list va, va2; - int len; - char *ret; - - va_start(va, format); - va_copy(va2, va); - - // How long is it? - len = vsnprintf(0, 0, format, va); - len++; - va_end(va); - - // Allocate and do the sprintf() - ret = xmalloc(len); - vsnprintf(ret, len, format, va2); - va_end(va2); - - return ret; -} - -// Confirm that a regular file exists, and (optionally) has the executable bit. -int is_file(char *filename, int has_exe) -{ - // Confirm it has the executable bit set, if necessary. - if (!has_exe || !access(filename, X_OK)) { - struct stat st; - - // Confirm it exists and is not a directory. - if (!stat(filename, &st) && S_ISREG(st.st_mode)) return 1; - } - return 0; -} - -// Find a file in a colon-separated path -char *find_in_path(char *path, char *filename, int has_exe) -{ - char *cwd = getcwd(0, 0); - - if (index(filename, '/') && is_file(filename, has_exe)) - return realpath(filename, 0); - - while (path) { - char *str, *next = path ? index(path, ':') : 0; - int len = next ? next-path : strlen(path); - - if (!len) str = xmprintf("%s/%s", cwd, filename); - else str = xmprintf("%*s/%s", len, path, filename); - - // If it's not a directory, return it. - if (is_file(str, has_exe)) { - char *s = realpath(str, 0); - - free(str); - free(cwd); - - return s; - } else free(str); - - if (next) next++; - path = next; - } - free(cwd); - - return NULL; -} - -struct dlist { - struct dlist *next, *prev; - char *str; -}; - -// Append to end of doubly linked list (in-order insertion) -void dlist_add(struct dlist **list, char *str) -{ - struct dlist *new = xmalloc(sizeof(struct dlist)); - - new->str = str; - if (*list) { - new->next = *list; - new->prev = (*list)->prev; - (*list)->prev->next = new; - (*list)->prev = new; - } else *list = new->next = new->prev = new; - - *list = new; -} - -// Some compiler versions don't provide separate T and S versions of begin/end, -// so fall back to the base version if they're not there. - -char *find_TSpath(char *base, char *top, int use_shared, int use_static_linking) -{ - int i; - char *temp; - - temp = xmprintf(base, top, - use_shared ? "S.o" : use_static_linking ? "T.o" : ".o"); - - if (!is_file(temp, 0)) { - free(temp); - temp = xmprintf(base, top, ".o"); - } - - return temp; -} - - -enum { - Clibccso, Clink, Cprofile, Cshared, Cstart, Cstatic, Cstdinc, Cstdlib, - Cverbose, Cx, Cdashdash, - - CPctordtor, CP, CPstdinc -}; - -#define MASK_BIT(X) (1<next->next; - libs->prev->next = 0; - - // Either display the list, or find first hit. - for (dl = libs; dl; dl = dl->next) { - if (show) printf(":%s" + (dl==libs), dl->str); - else if (!access(dl->str, F_OK)) break; - } - if (dl) printf("%s", dl->str); - printf("\n"); - - return 0; - } else if (!strcmp(c, "pg")) SET_FLAG(Cprofile); - } else if (*c == 's') { - keepc--; - if (!strcmp(c, "shared")) { - CLEAR_FLAG(Cstart); - SET_FLAG(Cshared); - } else if (!strcmp(c, "static")) { - SET_FLAG(Cstatic); - CLEAR_FLAG(Clibccso); - } else if (!strcmp(c, "shared-libgcc")) SET_FLAG(Clibccso); - else if (!strcmp(c, "static-libgcc")) CLEAR_FLAG(Clibccso); - else keepc++; - } else if (*c == 'v' && !c[1]) { - SET_FLAG(Cverbose); - printf("ccwrap: %s\n", topdir); - } else if (!strncmp(c, "Wl,", 3)) { - temp = strstr(c, ",-static"); - if (temp && (!temp[8] || temp[8]==',')) { - SET_FLAG(Cstatic); - CLEAR_FLAG(Clibccso); - } - // This argument specifies dynamic linker, so we shouldn't. - if (strstr(c, "--dynamic-linker")) dynlink = 0; - } else if (*c == 'x') SET_FLAG(Cx); - } - - // Initialize argument list for exec call - -// what's a good outc size? - - outc = (argc+keepc+64)*sizeof(char *); - memset(outv = xmalloc(outc), 0, outc); - outc = 0; - outv[outc++] = cc; - - // Are we linking? - if (srcfiles) { - outv[outc++] = "-nostdinc"; - if (GET_FLAG(CP)) { - outv[outc++] = "-nostdinc++"; - if (GET_FLAG(CPstdinc)) { - outv[outc++] = "-isystem"; - outv[outc++] = xmprintf("%s/c++/include", topdir); - } - } - if (GET_FLAG(Cstdinc)) { - outv[outc++] = "-isystem"; - outv[outc++] = xmprintf("%s/include", topdir); - outv[outc++] = "-isystem"; - outv[outc++] = xmprintf("%s/cc/include", topdir); - } - if (GET_FLAG(Clink)) { - // Zab defaults, add dynamic linker - outv[outc++] = "-nostdlib"; - outv[outc++] = GET_FLAG(Cstatic) ? "-static" : dynlink; - if (GET_FLAG(Cshared)) outv[outc++] = "-shared"; - - // Copy libraries to output (first move fallback to end, break circle) - libs = libs->next->next; - libs->prev->next = 0; - for (; libs; libs = libs->next) - outv[outc++] = xmprintf("-L%s", libs->str); - outv[outc++] = xmprintf("-Wl,-rpath-link,%s/lib", topdir); // TODO: in? - - // TODO: -fprofile-arcs - if (GET_FLAG(Cprofile)) xmprintf("%s/lib/gcrt1.o", topdir); - if (GET_FLAG(CPctordtor)) { - outv[outc++] = xmprintf("%s/lib/crti.o", topdir); - outv[outc++] = find_TSpath("%s/cc/lib/crtbegin%s", topdir, - GET_FLAG(Cshared), GET_FLAG(Cstatic)); - } - if (!GET_FLAG(Cprofile) && GET_FLAG(Cstart)) - outv[outc++] = xmprintf("%s/lib/%scrt1.o", topdir, - GET_FLAG(Cshared) ? "S" : ""); - } - } - - // Copy unclaimed arguments - memcpy(outv+outc, keepv, keepc*sizeof(char *)); - outc += keepc; - - if (srcfiles && GET_FLAG(Clink)) { - if (GET_FLAG(Cx)) outv[outc++] = "-xnone"; - if (GET_FLAG(Cstdlib)) { - if (GET_FLAG(CP)) { - outv[outc++] = "-lstdc++"; - //outv[outc++] = "-lm"; - } - - // libgcc can call libc which can call libgcc - outv[outc++] = "-Wl,--start-group,--as-needed"; - outv[outc++] = "-lgcc"; - if (GET_FLAG(Clibccso)) outv[outc++] = "-lgcc_s"; - else outv[outc++] = "-lgcc_eh"; - outv[outc++] = "-lc"; - outv[outc++] = "-Wl,--no-as-needed,--end-group"; - } - if (GET_FLAG(CPctordtor)) { - outv[outc++] = find_TSpath("%s/cc/lib/crtend%s", topdir, - GET_FLAG(Cshared), GET_FLAG(Cstatic)); - outv[outc++] = xmprintf("%s/lib/crtn.o", topdir); - } - } - outv[outc] = 0; - - if (getenv("CCWRAP_DEBUG")) { - fprintf(stderr, "outgoing:"); - for(i=0; i