Debian Codegraph @

Xpjproject/pjsip-apps/src/swig/python/cc_mingw.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* A simple proxy application for gcc/g++ to remove '-mno-cygwin' option
20 * when the gcc/g++ doesn't support it. Note that the option seems to be
21 * deprecated and causing compile error since gcc 4.7, while the option is
22 * auto-generated by Python distutils module (up to Python 2.7) in building
23 * Python extension.
24 */
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 /* Find gcc executable from env var PATH but omitting current dir */
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 /* Skip current dir */
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 /* Resolve GCC path from PATH env var */
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 /* Check if GCC rejects '-mno-cygwin' option */
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 //printf("cmd = %s\n", cmd);
145 ret = system(cmd);
146 }
147
148 if (ret) {
149 printf("Error: %s\n", strerror(ret));
150 }
151
152 return ret;
153 }