int main(int argc, char *argv[]) { char *program_name, *compiler_name, *other_wrappers, *sysroot; struct args *compiler_args, *cmd; compiler_args = args_init(argc, argv); /* check if we were called directly. */ program_name = basename(compiler_args->argv[0]); args_remove_first(compiler_args); if (str_eq(program_name, MYNAME)) { /* the first argument should be a compiler */ if (compiler_args->argc < 1 || compiler_args->argv[0][0] == '-') { fprintf(stderr, "%s", USAGE_TEXT); exit(1); } compiler_name = x_strdup(compiler_args->argv[0]); args_remove_first(compiler_args); } else { compiler_name = x_strdup(program_name); } free(program_name); other_wrappers = getenv(MYNAME "_OTHER_WRAPPERS"); if (!other_wrappers) { other_wrappers = "ccache distcc"; } cmd = find_all_executables(compiler_name, MYNAME, other_wrappers); if (!cmd) { fatal("No valid executables named %s found!", compiler_name); } free(compiler_name); sysroot = getenv("SYSROOT"); if (sysroot) { args_add(cmd, "--sysroot"); args_add(cmd, sysroot); } args_extend(cmd, compiler_args); args_free(compiler_args); execute(cmd->argv); return 1; }
/* find the real compiler. We just search the PATH to find a executable of the same name that isn't a link to ourselves */ static void find_compiler(int argc, char **argv) { char *base; char *path; orig_args = args_init(argc, argv); base = str_basename(argv[0]); /* we might be being invoked like "ccache gcc -c foo.c" */ if (strcmp(base, MYNAME) == 0) { args_remove_first(orig_args); free(base); if (strchr(argv[1],'/')) { /* a full path was given */ return; } base = str_basename(argv[1]); } /* support user override of the compiler */ if ((path=getenv("CCACHE_CC"))) { base = strdup(path); } orig_args->argv[0] = find_executable(base, MYNAME); /* can't find the compiler! */ if (!orig_args->argv[0]) { stats_update(STATS_COMPILER); perror(base); exit(1); } }