1 /* $Id$ */
2 /*
3 * Copyright (C) 2014 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /* A simple proxy application for gcc/g++ to remove '-mno-cygwin' option
21 * when the gcc/g++ doesn't support it. Note that the option seems to be
22 * deprecated and causing compile error since gcc 4.7, while the option is
23 * auto-generated by Python distutils module (up to Python 2.7) in building
24 * Python extension.
25 */
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 /* Find gcc executable from env var PATH but omitting current dir */
find_gcc(const char * gcc_exe)33 const char* find_gcc(const char *gcc_exe)
34 {
35 static char fname[256];
36 char spath[1024 * 10];
37 char *p;
38
39 p = getenv("PATH");
40 if (strlen(p) >= sizeof(spath)) {
41 printf("Error: find_gcc() not enough buffer 1\n");
42 return NULL;
43 }
44
45 strncpy(spath, p, sizeof(spath));
46 p = strtok(spath, ";");
47 while (p) {
48 int len;
49
50 /* Skip current dir */
51 if (strcmp(p, ".") == 0) {
52 p = strtok(NULL, ";");
53 continue;
54 }
55
56 len = snprintf(fname, sizeof(fname), "%s\\%s", p, gcc_exe);
57 if (len < 0 || len >= sizeof(fname)) {
58 printf("Error: find_gcc() not enough buffer 2\n");
59 return NULL;
60 }
61
62 if (access(fname, F_OK | X_OK) != -1) {
63 return fname;
64 }
65
66 p = strtok(NULL, ";");
67 }
68
69 return NULL;
70 }
71
check_gcc_reject_mno_cygwin(const char * gcc_path)72 int check_gcc_reject_mno_cygwin(const char *gcc_path)
73 {
74 FILE *fp;
75 char tmp[1024];
76 const char *p;
77 int ver;
78
79 snprintf(tmp, sizeof(tmp), "%s -mno-cygwin 2>&1", gcc_path);
80 fp = popen(tmp, "r");
81 if (fp == NULL) {
82 printf("Error: failed to run gcc\n" );
83 return -1;
84 }
85
86 while (fgets(tmp, sizeof(tmp), fp) != NULL) {
87 if (strstr(tmp, "unrecognized") && strstr(tmp, "-mno-cygwin"))
88 return 1;
89 }
90
91 pclose(fp);
92 return 0;
93 }
94
main(int argc,const char const ** argv)95 int main(int argc, const char const **argv)
96 {
97 const char *app = "python cc_mingw.py";
98 char cmd[1024 * 8], *p;
99 int i, len, sz;
100 int ret = 0;
101 const char *spath, *gcc_exe;
102 int remove_mno_cygwin;
103
104 if (strstr(argv[0], "gcc") || strstr(argv[0], "GCC")) {
105 gcc_exe = "gcc.exe";
106 } else if (strstr(argv[0], "g++") || strstr(argv[0], "G++")) {
107 gcc_exe = "g++.exe";
108 } else {
109 printf("Error: app name not gcc/g++\n");
110 return -10;
111 }
112
113 /* Resolve GCC path from PATH env var */
114 gcc_exe = find_gcc(gcc_exe);
115 if (!gcc_exe) {
116 printf("Error: real gcc/g++ not found\n");
117 return -20;
118 }
119
120 /* Check if GCC rejects '-mno-cygwin' option */
121 remove_mno_cygwin = check_gcc_reject_mno_cygwin(gcc_exe);
122 if (remove_mno_cygwin < 0)
123 return -30;
124
125 len = snprintf(cmd, sizeof(cmd), "%s", gcc_exe);
126 p = cmd + len;
127 sz = sizeof(cmd) - len;
128 for (i = 1; i < argc && sz > 0; ++i) {
129
130 if (remove_mno_cygwin && strcmp(argv[i], "-mno-cygwin") == 0) {
131 printf("Removed option '-mno-cygwin'.\n");
132 continue;
133 }
134
135 len = snprintf(p, sz, " %s", argv[i]);
136 if (len < 0 || len >= sz) {
137 ret = E2BIG;
138 break;
139 }
140 p += len;
141 sz -= len;
142 }
143
144 if (!ret) {
145 //printf("cmd = %s\n", cmd);
146 ret = system(cmd);
147 }
148
149 if (ret) {
150 printf("Error: %s\n", strerror(ret));
151 }
152
153 return ret;
154 }
155