void set_output_file(const char *file, int no_link, int no_warning) { #if defined __INTERIX && !defined _NO_CONV_PATH if(*file == '/') { char buffer[3 + PATH_MAX + 1] = { '-', 'F', no_link ? 'o' : 'e' }; if(unixpath2win(file, 0, buffer + 3, sizeof buffer - 3) < 0) { if(!no_warning) { fprintf(stderr, "warning: cannot convert '%s' to Windows path name, %s\n", file, strerror(errno)); } size_t len = strlen(file) + 1; if(3 + len > sizeof buffer) { fprintf(stderr, "error: %s: output file name too long\n", file); exit(1); } memcpy(buffer + 3, file, len); } add_to_argv(buffer); } else #endif { char buffer[3 + strlen(file) + 1]; sprintf(buffer, "-F%c%s", no_link ? 'o' : 'e', file); add_to_argv(buffer); } target.name = file; target.type = no_link ? OBJ : EXE; }
void unixPathToWin(char *path,char *buff,int maxlength) { char *ptr; unixpath2win(path, 0, buff, maxlength); // c has issues with back-slashes in strings so convert them to forward slashes while ( (ptr = strchr(buff,'\\')) ) *ptr = '/'; #ifdef TEST_PATH Winfoprintf("unixPathToWin %s -> [%s]",path,buff); char tmpPath[MAXPATH]; winpath2unix(buff, 0, tmpPath, maxlength); Winfoprintf("winpath2unix %s -> [%s]",buff,tmpPath); #endif }
void add_include_path(const char *path, int no_warning) { #if defined __INTERIX && !defined _NO_CONV_PATH if(*path == '/') { static char buffer[2 + PATH_MAX + 1] = { '-', 'I' }; if(unixpath2win(path, 0, buffer + 2, sizeof buffer - 2) == 0) { add_to_argv(buffer); return; } else if(!no_warning) { fprintf(stderr, "warning: cannot convert '%s' to Windows path name, %s\n", path, strerror(errno)); } } #endif char buffer[2 + strlen(path) + 1]; strcpy(buffer, "-I"); strcpy(buffer + 2, path); add_to_argv(buffer); }
void add_library_path(const char *path, int no_warning) { static char *llib; const char *lib = getenv("LIB"); char *old_path = lib ? strdup(lib) : (char *)""; if(!old_path) { perror(NULL); abort(); } #if defined __INTERIX && !defined _NO_CONV_PATH if(*path == '/') { static char buffer[PATH_MAX + 1]; if(unixpath2win(path, 0, buffer, sizeof buffer) == 0) { path = buffer; } else if(!no_warning) { fprintf(stderr, "warning: cannot convert '%s' to Windows path name, %s\n", path, strerror(errno)); } } #endif size_t old_path_len = strlen(old_path); size_t new_path_len = strlen(path); //llib = realloc(llib, 4 + new_path_len + 1 + old_path_len + 1); free(llib); llib = malloc(4 + new_path_len + 1 + old_path_len + 1); if(!llib) { perror(NULL); abort(); } memcpy(llib, "LIB=", 4); memcpy(llib + 4, path, new_path_len); if(old_path_len) { llib[4 + new_path_len] = ';'; memcpy(llib + 4 + new_path_len + 1, old_path, old_path_len + 1); free(old_path); } else llib[4 + new_path_len] = 0; putenv(llib); }
int main(int argc, char **argv) { #define FIND_LONG_OPTION(ARRAY) \ { \ for(i = 0; i < sizeof (ARRAY) / sizeof(struct option); i++) { \ struct option *o = (ARRAY) + i; \ if(o->arg < 2) { \ if(strcmp(arg, o->opt) == 0) { \ const char *a = o->arg ? *++v : argv[0]; \ if(o->arg && !a) { \ fprintf(stderr, "%s: option '%s' need an argument\n", argv[0], *v); \ return 1; \ } \ if(o->act) o->act(a); \ goto first_loop; \ } \ } else { \ size_t len = strlen(o->opt); \ if(strncmp(arg, o->opt, len) == 0) { \ if(arg[len] && arg[len] != '=') continue; \ if(!arg[len] || !arg[len + 1]) { \ fprintf(stderr, "%s: option '%s' need an argument\n", argv[0], *v); \ return 1; \ } \ const char *a = arg + len + 1; \ if(o->act) o->act(a); \ goto first_loop; \ } \ } \ } \ } #define UNRECOGNIZED_OPTION(O) \ do { \ fprintf(stderr, "%s: error: unrecognized option '%s'\n", \ argv[0], (O)); \ return 1; \ } while(0) int verbose = 0; int no_link = 0; int preprocess_only = -1; int no_warning = -1; int end_of_options = 0; const char *output_file = NULL; char **v = argv; init_argv(); const char *vs_path = getenv("VS_PATH"); if(!vs_path) vs_path = getenv("VSINSTALLDIR"); if(!getenv("INCLUDE")) { if(vs_path) { size_t len = strlen(vs_path); if(vs_path[len - 1] == '/' || vs_path[len - 1] == '\\') len--; char buffer[len + 12 + len + 24 + 1]; memcpy(buffer, vs_path, len); memcpy(buffer + len, "/VC/include;", 12); memcpy(buffer + len + 12, vs_path, len); strcpy(buffer + len + 12 + len, "/VC/PlatformSDK/include;"); setenv("INCLUDE", buffer, 0); } else { no_warning = find_argv(argv, "-w"); if(!no_warning) fprintf(stderr, "%s: warning: no system include path set\n", argv[0]); } } if(!getenv("LIB")) { if(vs_path) { size_t len = strlen(vs_path); if(vs_path[len - 1] == '/' || vs_path[len - 1] == '\\') len--; char buffer[len + 8 + len + 20 + 1]; memcpy(buffer, vs_path, len); memcpy(buffer + len, "/VC/lib;", 8); memcpy(buffer + len + 12, vs_path, len); strcpy(buffer + len + 12 + len, "/VC/PlatformSDK/lib;"); setenv("LIB", buffer, 0); } else { if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(!no_warning) fprintf(stderr, "%s: warning: no system library path set\n", argv[0]); } } first_loop: while(*++v) { if(!end_of_options && **v == '-') { int i; if((*v)[1] == '-') { const char *arg = *v + 2; if(!*arg) { end_of_options = 1; continue; } FIND_LONG_OPTION(double_dash_long_options); if(strcmp(arg, "verbose") == 0) { verbose = 1; } else UNRECOGNIZED_OPTION(*v); } else { const char *arg = *v + 1; FIND_LONG_OPTION(singal_dash_long_options); switch(*arg) { case 0: goto not_an_option; case 'c': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-c"); no_link = 1; break; case 'D': if(arg[1]) add_to_argv(*v); else { const char *d = *++v; if(!d) { fprintf(stderr, "%s: error: macro name missing after '-D'\n", argv[0]); return 1; } define(d); } break; case 'E': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-E"); preprocess_only = 1; break; case 'f': if(arg[1]) set_feature(arg + 1); else { const char *feature = *++v; if(!feature) { fprintf(stderr, "%s: error: option '-f' need an argument\n", argv[0]); return -1; } set_feature(feature); } break; case 'g': // -g[coff][<level>] (level: 0~3) if(arg[1]) { const char *level = arg + 1; if(strncmp(level, "coff", 4) == 0) level += 4; if(*level && *level != '-') { int i = 0; do { if(!isdigit(level[i])) { fprintf(stderr, "%s: error: unrecognized debug output level \"%s\"\n", argv[0], level); return 1; } } while(level[++i]); /* if(i > 1 || *level > '3') { fprintf(stderr, "%s: error: debug output level %s is too high\n", argv[0], level); return 1; } if(*level == '0') break; */ int l = atoi(level); if(!l) break; if(l > 3) { fprintf(stderr, "%s: error: debug output level %s is too high\n", argv[0], level); return 1; } } } add_to_argv("-Zi"); break; case 'I': if(no_warning == -1) no_warning = find_argv(argv, "-w"); //if(arg[1]) add_to_argv(*v); if(arg[1]) add_include_path(arg + 1, no_warning); else { const char *path = *++v; if(!path) { fprintf(stderr, "%s: error: option '-I' need an argument\n", argv[0]); return 1; } add_include_path(path, no_warning); } break; case 'L': if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(arg[1]) add_library_path(arg + 1, no_warning); else { const char *path = *++v; if(!path) { fprintf(stderr, "%s: error: option '-L' need an argument\n", argv[0]); return 1; } add_library_path(path, no_warning); } break; case 'l': if(arg[1]) add_library(arg + 1); else { const char *path = *++v; if(!path) { fprintf(stderr, "%s: error: option '-l' need an argument\n", argv[0]); return 1; } add_library(path); } break; case 'M': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-showIncludes"); break; case 'm': if(arg[1]) set_machine(arg + 1); else { const char *machine = *++v; if(!machine) { fprintf(stderr, "%s: argument to `-m' is missing\n", argv[0]); return 1; } set_machine(machine); } break; case 'O': if(arg[1]) { const char *o = arg + 1; if(strcmp(o, "0") == 0) add_to_argv("-Od"); else if(strcmp(o, "1") == 0) add_to_argv("-O2"); else if(strcmp(o, "3") == 0) add_to_argv("-Ox"); else if(strcmp(o, "s") == 0) add_to_argv("-O1"); else if(strcmp(o, "fast") == 0) add_to_argv("-O2"); else add_to_argv(*v); } else add_to_argv("-O2"); break; case 'o': if(arg[1]) output_file = arg + 1; else { output_file = *++v; if(!output_file) { fprintf(stderr, "%s: error: option '-o' need an argument\n", argv[0]); return 1; } } break; case 'P': if(preprocess_only == -1) preprocess_only = find_argv(argv, "-E"); if(preprocess_only) add_to_argv("-EP"); break; case 's': if(arg[1]) UNRECOGNIZED_OPTION(*v); break; case 'U': if(arg[1]) add_to_argv(*v); else { const char *u = *++v; if(!u) { fprintf(stderr, "%s: error: macro name missing after '-U'\n", argv[0]); return 1; } undefine(u); } break; case 'v': if(arg[1]) UNRECOGNIZED_OPTION(*v); verbose = 1; break; case 'W': if(!arg[1]) { if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(!no_warning) { fprintf(stderr, "%s: warning: option '-W' is deprecated; use '-Wextra' instead\n", argv[0]); } add_to_argv("-Wall"); break; } if(strncmp(arg, "Wa,", 3) == 0 || strncmp(arg, "Wp,", 3) == 0 || strncmp(arg, "Wl,", 3) == 0) { (*v)[3] = 0; // XXX fprintf(stderr, "%s: warning: option '%s' is not supported\n", argv[0], *v); break; } if(set_warning(arg + 1)) break; add_to_argv(*v); break; case 'w': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-w"); no_warning = 1; break; case 'x': if(arg[1]) set_language(arg + 1); else { const char *lang = *++v; if(!lang) { fprintf(stderr, "%s: error: missing argument to ‘-x’", argv[0]); return 4; } set_language(lang); } break; default: fprintf(stderr, "%s: error: unrecognized option '%s'\n", argv[0], *v); return 1; } } } else { not_an_option: #if defined __INTERIX && !defined _NO_CONV_PATH if(**v == '/') { char buffer[PATH_MAX + 1]; if(unixpath2win(*v, 0, buffer, sizeof buffer) == 0) { add_input_file(buffer); } else { if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(!no_warning) { fprintf(stderr, "%s: warning: cannot convert '%s' to Windows path name, %s\n", argv[0], *v, strerror(errno)); } add_input_file(*v); } } else #endif add_input_file(*v); } } setvbuf(stdout, NULL, _IOLBF, 0); if(preprocess_only == -1) preprocess_only = 0; if(no_warning == -1) no_warning = 0; if(last_language && last_language_unused && !no_warning) { fprintf(stderr, "%s: warning: '-x %s' after last input file has no effect\n", argv[0], last_language); } if(!first_input_file) { if(verbose) { if(!no_link) add_to_argv("-c"); start_cl(); return 0; } fprintf(stderr, "%s: no input files\n", argv[0]); return 1; } if(multiple_input_files && (preprocess_only || no_link)) { if(output_file) { fprintf(stderr, "%s: error: cannot specify -o with -c or -E with multiple files\n", argv[0]); return 4; } else if(no_link) { fprintf(stderr, "%s: error: '-c' with multiple files is currently not supported\n", argv[0]); return -1; } } if(!output_file && !preprocess_only) { if(no_link) { size_t len = strlen(first_input_file); int n = get_last_dot(first_input_file, len); if(n >= 0) len = n; char *p = malloc(len + 3); if(!p) { perror(argv[0]); return 1; } memcpy(p, first_input_file, len); strcpy(p + len, ".o"); output_file = p; } else output_file = DEFAULT_OUTPUT_FILENAME; } if(!verbose) add_to_argv("-nologo"); if(preprocess_only) { if(output_file) target.name = output_file; target.type = PREPROCESSED_SOURCE; } else set_output_file(output_file, no_link, no_warning); //if(no_static_link) add_to_argv("-MD"); add_to_argv(no_static_link ? "-MD" : "-MT"); add_libraries_to_argv(); if(verbose) print_argv(); return start_cl(); }