| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
|
| 32 |
const char* find_gcc(const char *gcc_exe) |
| 33 |
{ |
| 34 |
static char fname[256]; |
| 35 |
char spath[1024 * 10]; |
| 36 |
char *p; |
| 37 |
|
| 38 |
p = getenv("PATH"); |
| 39 |
if (strlen(p) >= sizeof(spath)) { |
| 40 |
printf("Error: find_gcc() not enough buffer 1\n"); |
| 41 |
return NULL; |
| 42 |
} |
| 43 |
|
| 44 |
strncpy(spath, p, sizeof(spath)); |
| 45 |
p = strtok(spath, ";"); |
| 46 |
while (p) { |
| 47 |
int len; |
| 48 |
|
| 49 |
|
| 50 |
if (strcmp(p, ".") == 0) { |
| 51 |
p = strtok(NULL, ";"); |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
len = snprintf(fname, sizeof(fname), "%s\\%s", p, gcc_exe); |
| 56 |
if (len < 0 || len >= sizeof(fname)) { |
| 57 |
printf("Error: find_gcc() not enough buffer 2\n"); |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
if (access(fname, F_OK | X_OK) != -1) { |
| 62 |
return fname; |
| 63 |
} |
| 64 |
|
| 65 |
p = strtok(NULL, ";"); |
| 66 |
} |
| 67 |
|
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
int check_gcc_reject_mno_cygwin(const char *gcc_path) |
| 72 |
{ |
| 73 |
FILE *fp; |
| 74 |
char tmp[1024]; |
| 75 |
const char *p; |
| 76 |
int ver; |
| 77 |
|
| 78 |
snprintf(tmp, sizeof(tmp), "%s -mno-cygwin 2>&1", gcc_path); |
| 79 |
fp = popen(tmp, "r"); |
| 80 |
if (fp == NULL) { |
| 81 |
printf("Error: failed to run gcc\n" ); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
|
| 85 |
while (fgets(tmp, sizeof(tmp), fp) != NULL) { |
| 86 |
if (strstr(tmp, "unrecognized") && strstr(tmp, "-mno-cygwin")) |
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
pclose(fp); |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
int main(int argc, const char const **argv) |
| 95 |
{ |
| 96 |
const char *app = "python cc_mingw.py"; |
| 97 |
char cmd[1024 * 8], *p; |
| 98 |
int i, len, sz; |
| 99 |
int ret = 0; |
| 100 |
const char *spath, *gcc_exe; |
| 101 |
int remove_mno_cygwin; |
| 102 |
|
| 103 |
if (strstr(argv[0], "gcc") || strstr(argv[0], "GCC")) { |
| 104 |
gcc_exe = "gcc.exe"; |
| 105 |
} else if (strstr(argv[0], "g++") || strstr(argv[0], "G++")) { |
| 106 |
gcc_exe = "g++.exe"; |
| 107 |
} else { |
| 108 |
printf("Error: app name not gcc/g++\n"); |
| 109 |
return -10; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
gcc_exe = find_gcc(gcc_exe); |
| 114 |
if (!gcc_exe) { |
| 115 |
printf("Error: real gcc/g++ not found\n"); |
| 116 |
return -20; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
remove_mno_cygwin = check_gcc_reject_mno_cygwin(gcc_exe); |
| 121 |
if (remove_mno_cygwin < 0) |
| 122 |
return -30; |
| 123 |
|
| 124 |
len = snprintf(cmd, sizeof(cmd), "%s", gcc_exe); |
| 125 |
p = cmd + len; |
| 126 |
sz = sizeof(cmd) - len; |
| 127 |
for (i = 1; i < argc && sz > 0; ++i) { |
| 128 |
|
| 129 |
if (remove_mno_cygwin && strcmp(argv[i], "-mno-cygwin") == 0) { |
| 130 |
printf("Removed option '-mno-cygwin'.\n"); |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
len = snprintf(p, sz, " %s", argv[i]); |
| 135 |
if (len < 0 || len >= sz) { |
| 136 |
ret = E2BIG; |
| 137 |
break; |
| 138 |
} |
| 139 |
p += len; |
| 140 |
sz -= len; |
| 141 |
} |
| 142 |
|
| 143 |
if (!ret) { |
| 144 |
|
| 145 |
ret = system(cmd); |
| 146 |
} |
| 147 |
|
| 148 |
if (ret) { |
| 149 |
printf("Error: %s\n", strerror(ret)); |
| 150 |
} |
| 151 |
|
| 152 |
return ret; |
| 153 |
} |