Fixed typo in SetGraphicsMode.
[wine/multimedia.git] / tools / winegcc.c
blob0e076799f828834695eb7c5a184891386bbadc93
1 /*
2 * MinGW wrapper: makes gcc behave like MinGW.
4 * Copyright 2000 Manuel Novoa III
5 * Copyright 2002 Dimitrie O. Paun
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <errno.h>
30 #ifdef HAVE_SYS_WAIT_H
31 #include <sys/wait.h>
32 #endif
33 #include <sys/stat.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
38 static char **tmp_files;
39 static int nb_tmp_files;
40 static int verbose = 0;
41 static int keep_generated = 0;
43 void error(const char *s, ...)
45 va_list ap;
47 va_start(ap, s);
48 fprintf(stderr, "Error: ");
49 vfprintf(stderr, s, ap);
50 fprintf(stderr, "\n");
51 va_end(ap);
52 exit(2);
55 char *strmake(const char *fmt, ...)
57 int n, size = 100;
58 char *p;
59 va_list ap;
61 if ((p = malloc (size)) == NULL)
62 error("Can not malloc %d bytes.", size);
64 while (1)
66 va_start(ap, fmt);
67 n = vsnprintf (p, size, fmt, ap);
68 va_end(ap);
69 if (n > -1 && n < size) return p;
70 size *= 2;
71 if ((p = realloc (p, size)) == NULL)
72 error("Can not realloc %d bytes.", size);
76 void spawn(char *const argv[])
78 int pid, status, wret, i;
80 if (verbose)
82 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
83 printf("\n");
86 if ((pid = fork()) == 0) execvp(argv[0], argv);
87 else if (pid > 0)
89 while (pid != (wret = waitpid(pid, &status, 0)))
90 if (wret == -1 && errno != EINTR) break;
92 if (pid == wret && WIFEXITED(status) && WEXITSTATUS(status) == 0) return;
93 error("%s failed.", argv[0]);
95 perror("Error:");
96 exit(3);
99 int strendswith(const char *str, const char *end)
101 int l = strlen(str);
102 int m = strlen(end);
104 return l >= m && strcmp(str + l - m, end) == 0;
107 void clean_temp_files()
109 int i;
111 if (keep_generated) return;
113 for (i = 0; i < nb_tmp_files; i++)
114 unlink(tmp_files[i]);
117 char *get_temp_file(const char *suffix)
119 char *tmp = strmake("wgcc.XXXXXX%s", suffix);
120 int fd = mkstemps( tmp, strlen(suffix) );
121 if (fd == -1)
123 /* could not create it in current directory, try in /tmp */
124 free(tmp);
125 tmp = strmake("/tmp/wgcc.XXXXXX%s", suffix);
126 fd = mkstemps( tmp, strlen(suffix) );
127 if (fd == -1) error( "could not create temp file" );
129 close( fd );
130 tmp_files = realloc( tmp_files, (nb_tmp_files+1) * sizeof(*tmp_files) );
131 tmp_files[nb_tmp_files++] = tmp;
133 return tmp;
136 char *get_obj_file(char **argv, int n)
138 char *tmpobj, **compargv;
139 int i, j;
141 if (strendswith(argv[n], ".o")) return argv[n];
142 if (strendswith(argv[n], ".a")) return argv[n];
143 if (strendswith(argv[n], ".res")) return argv[n];
145 tmpobj = get_temp_file(".o");
146 compargv = malloc(sizeof(char*) * (n + 10));
147 i = 0;
148 compargv[i++] = BINDIR "/winegcc";
149 compargv[i++] = "-c";
150 compargv[i++] = "-o";
151 compargv[i++] = tmpobj;
152 for (j = 1; j <= n; j++)
153 if (argv[j]) compargv[i++] = argv[j];
154 compargv[i] = 0;
156 spawn(compargv);
158 return tmpobj;
162 int main(int argc, char **argv)
164 char **gcc_argv;
165 int i, j;
166 int linking = 1, cpp = 0, use_static_linking = 0;
167 int use_stdinc = 1, use_stdlib = 1, use_msvcrt = 0, gui_app = 0;
169 atexit(clean_temp_files);
171 if (strendswith(argv[0], "++")) cpp = 1;
173 for ( i = 1 ; i < argc ; i++ )
175 if (argv[i][0] == '-') /* option */
177 switch (argv[i][1])
179 case 'c': /* compile or assemble */
180 case 'S': /* generate assembler code */
181 case 'E': /* preprocess only */
182 if (argv[i][2] == 0) linking = 0;
183 break;
184 case 'M': /* map file generation */
185 linking = 0;
186 break;
187 case 'm':
188 if (strcmp("-mno-cygwin", argv[i]) == 0)
189 use_msvcrt = 1;
190 else if (strcmp("-mwindows", argv[i]) == 0)
191 gui_app = 1;
192 break;
193 case 'n':
194 if (strcmp("-nostdinc", argv[i]) == 0)
195 use_stdinc = 0;
196 else if (strcmp("-nodefaultlibs", argv[i]) == 0)
197 use_stdlib = 0;
198 else if (strcmp("-nostdlib", argv[i]) == 0)
199 use_stdlib = 0;
200 break;
201 case 's':
202 if (strcmp("-static", argv[i]) == 0) use_static_linking = 1;
203 break;
204 case 'v': /* verbose */
205 if (argv[i][2] == 0) verbose = 1;
206 break;
207 case 'V':
208 printf("winegcc v0.3\n");
209 exit(0);
210 break;
211 case 'W':
212 if (strncmp("-Wl,", argv[i], 4) == 0)
214 if (strstr(argv[i], "-static"))
215 use_static_linking = 1;
217 break;
218 case '-':
219 if (strcmp("-static", argv[i]+1) == 0)
220 use_static_linking = 1;
221 break;
226 if (use_static_linking) error("Static linking is not supported.");
228 gcc_argv = malloc(sizeof(char*) * (argc + 20));
230 i = 0;
231 if (linking)
233 int has_output_name = 0;
235 gcc_argv[i++] = BINDIR "/winewrap";
236 if (gui_app) gcc_argv[i++] = "-mgui";
238 if (cpp) gcc_argv[i++] = "-C";
239 for ( j = 1 ; j < argc ; j++ )
241 if ( argv[j][0] == '-' )
243 switch (argv[j][1])
245 case 'L':
246 case 'o':
247 gcc_argv[i++] = argv[j];
248 argv[j] = 0;
249 if (!gcc_argv[i-1][2] && j + 1 < argc)
251 gcc_argv[i++] = argv[++j];
252 argv[j] = 0;
254 has_output_name = 1;
255 break;
256 case 'l':
257 gcc_argv[i++] = strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid";
258 argv[j] = 0;
259 break;
260 default:
261 ; /* ignore the rest */
264 else
266 gcc_argv[i++] = get_obj_file(argv, j);
267 argv[j] = 0;
270 /* Support the a.out default name, to appease configure */
271 if (!has_output_name)
273 gcc_argv[i++] = "-o";
274 gcc_argv[i++] = "a.out";
277 if (use_stdlib && use_msvcrt) gcc_argv[i++] = "-lmsvcrt";
278 if (gui_app) gcc_argv[i++] = "-lcomdlg32";
279 gcc_argv[i++] = "-ladvapi32";
280 gcc_argv[i++] = "-lshell32";
282 else
284 gcc_argv[i++] = cpp ? "g++" : "gcc";
286 gcc_argv[i++] = "-fshort-wchar";
287 gcc_argv[i++] = "-fPIC";
288 if (use_stdinc)
290 if (use_msvcrt)
292 gcc_argv[i++] = "-I" INCLUDEDIR "/msvcrt";
293 gcc_argv[i++] = "-D__MSVCRT__";
295 gcc_argv[i++] = "-I" INCLUDEDIR "/windows";
297 gcc_argv[i++] = "-DWIN32";
298 gcc_argv[i++] = "-D_WIN32";
299 gcc_argv[i++] = "-D__WIN32";
300 gcc_argv[i++] = "-D__WIN32__";
301 gcc_argv[i++] = "-D__WINNT";
302 gcc_argv[i++] = "-D__WINNT__";
304 gcc_argv[i++] = "-D__stdcall=__attribute__((__stdcall__))";
305 gcc_argv[i++] = "-D__cdecl=__attribute__((__cdecl__))";
306 gcc_argv[i++] = "-D__fastcall=__attribute__((__fastcall__))";
307 gcc_argv[i++] = "-D_stdcall=__attribute__((__stdcall__))";
308 gcc_argv[i++] = "-D_cdecl=__attribute__((__cdecl__))";
309 gcc_argv[i++] = "-D_fastcall=__attribute__((__fastcall__))";
310 gcc_argv[i++] = "-D__declspec(x)=__attribute__((x))";
312 /* Wine specific defines */
313 gcc_argv[i++] = "-D__WINE__";
314 gcc_argv[i++] = "-DWINE_UNICODE_NATIVE";
315 gcc_argv[i++] = "-D__int8=char";
316 gcc_argv[i++] = "-D__int16=short";
317 gcc_argv[i++] = "-D__int32=int";
318 gcc_argv[i++] = "-D__int64=long long";
320 for ( j = 1 ; j < argc ; j++ )
322 if (strcmp("-mno-cygwin", argv[j]) == 0)
323 ; /* ignore this option */
324 else if (strcmp("-mwindows", argv[j]) == 0)
325 ; /* ignore this option */
326 else if (strncmp("-Wl,", argv[j], 4) == 0)
327 ; /* do not pass linking options to compiler */
328 else if (strcmp("-s", argv[j]) == 0)
329 ; /* ignore this option */
330 else
331 gcc_argv[i++] = argv[j];
335 gcc_argv[i] = NULL;
337 spawn(gcc_argv);
339 return 0;