Forward all arguments to gcc when we have no input files.
[wine/multimedia.git] / tools / winegcc.c
blobe5a143db045a15555fd022b598a6bd3aca72e7c0
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 #include <sys/stat.h>
32 static char **tmp_files;
33 static int nb_tmp_files;
34 static int verbose = 0;
35 static int keep_generated = 0;
37 void error(const char *s, ...)
39 va_list ap;
41 va_start(ap, s);
42 fprintf(stderr, "Error: ");
43 vfprintf(stderr, s, ap);
44 fprintf(stderr, "\n");
45 va_end(ap);
46 exit(2);
49 char *strmake(const char *fmt, ...)
51 int n, size = 100;
52 char *p;
53 va_list ap;
55 if ((p = malloc (size)) == NULL)
56 error("Can not malloc %d bytes.", size);
58 while (1)
60 va_start(ap, fmt);
61 n = vsnprintf (p, size, fmt, ap);
62 va_end(ap);
63 if (n > -1 && n < size) return p;
64 size *= 2;
65 if ((p = realloc (p, size)) == NULL)
66 error("Can not realloc %d bytes.", size);
70 void spawn(char *const argv[])
72 int i, status;
74 if (verbose)
76 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
77 printf("\n");
79 if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
81 if (status > 0) error("%s failed.", argv[0]);
82 else perror("Error:");
83 exit(3);
86 int strendswith(const char *str, const char *end)
88 int l = strlen(str);
89 int m = strlen(end);
91 return l >= m && strcmp(str + l - m, end) == 0;
94 void clean_temp_files()
96 int i;
98 if (keep_generated) return;
100 for (i = 0; i < nb_tmp_files; i++)
101 unlink(tmp_files[i]);
104 char *get_temp_file(const char *suffix)
106 char *tmp = strmake("wgcc.XXXXXX%s", suffix);
107 int fd = mkstemps( tmp, strlen(suffix) );
108 if (fd == -1)
110 /* could not create it in current directory, try in /tmp */
111 free(tmp);
112 tmp = strmake("/tmp/wgcc.XXXXXX%s", suffix);
113 fd = mkstemps( tmp, strlen(suffix) );
114 if (fd == -1) error( "could not create temp file" );
116 close( fd );
117 tmp_files = realloc( tmp_files, (nb_tmp_files+1) * sizeof(*tmp_files) );
118 tmp_files[nb_tmp_files++] = tmp;
120 return tmp;
123 char *get_obj_file(char **argv, int n)
125 char *tmpobj, **compargv;
126 int i, j;
128 if (strendswith(argv[n], ".o")) return argv[n];
129 if (strendswith(argv[n], ".a")) return argv[n];
130 if (strendswith(argv[n], ".res")) return argv[n];
132 tmpobj = get_temp_file(".o");
133 compargv = malloc(sizeof(char*) * (n + 10));
134 i = 0;
135 compargv[i++] = "winegcc";
136 compargv[i++] = "-c";
137 compargv[i++] = "-o";
138 compargv[i++] = tmpobj;
139 for (j = 1; j <= n; j++)
140 if (argv[j]) compargv[i++] = argv[j];
141 compargv[i] = 0;
143 spawn(compargv);
145 return tmpobj;
149 int main(int argc, char **argv)
151 char **gcc_argv;
152 int i, j;
153 int linking = 1, cpp = 0, use_static_linking = 0;
154 int use_stdinc = 1, use_stdlib = 1, use_msvcrt = 0, gui_app = 0;
156 atexit(clean_temp_files);
158 if (strendswith(argv[0], "++")) cpp = 1;
160 for ( i = 1 ; i < argc ; i++ )
162 if (argv[i][0] == '-') /* option */
164 switch (argv[i][1])
166 case 'c': /* compile or assemble */
167 case 'S': /* generate assembler code */
168 case 'E': /* preprocess only */
169 if (argv[i][2] == 0) linking = 0;
170 break;
171 case 'M': /* map file generation */
172 linking = 0;
173 break;
174 case 'm':
175 if (strcmp("-mno-cygwin", argv[i]) == 0)
176 use_msvcrt = 1;
177 else if (strcmp("-mwindows", argv[i]) == 0)
178 gui_app = 1;
179 else if (strcmp("-mconsole", argv[i]) == 0)
180 gui_app = 0;
181 break;
182 case 'n':
183 if (strcmp("-nostdinc", argv[i]) == 0)
184 use_stdinc = 0;
185 else if (strcmp("-nodefaultlibs", argv[i]) == 0)
186 use_stdlib = 0;
187 else if (strcmp("-nostdlib", argv[i]) == 0)
188 use_stdlib = 0;
189 break;
190 case 's':
191 if (strcmp("-static", argv[i]) == 0) use_static_linking = 1;
192 break;
193 case 'v': /* verbose */
194 if (argv[i][2] == 0) verbose = 1;
195 break;
196 case 'W':
197 if (strncmp("-Wl,", argv[i], 4) == 0)
199 if (strstr(argv[i], "-static"))
200 use_static_linking = 1;
202 break;
203 case '-':
204 if (strcmp("-static", argv[i]+1) == 0)
205 use_static_linking = 1;
206 break;
211 if (use_static_linking) error("Static linking is not supported.");
213 gcc_argv = malloc(sizeof(char*) * (argc + 20));
215 i = 0;
216 if (linking)
218 int has_output_name = 0;
219 int has_input_files = 0;
220 char **temp_argv;
222 /* we need this to erase some of the parameters as we go along */
223 temp_argv = malloc(sizeof(char*) * argc);
224 memcpy(temp_argv, argv, sizeof(char*) * argc);
226 gcc_argv[i++] = "winewrap";
227 if (gui_app) gcc_argv[i++] = "-mgui";
229 if (cpp) gcc_argv[i++] = "-C";
230 for ( j = 1 ; j < argc ; j++ )
232 if ( argv[j][0] == '-' )
234 switch (argv[j][1])
236 case 'L':
237 case 'o':
238 gcc_argv[i++] = argv[j];
239 temp_argv[j] = 0;
240 if (!gcc_argv[i-1][2] && j + 1 < argc)
242 gcc_argv[i++] = argv[++j];
243 temp_argv[j] = 0;
245 has_output_name = 1;
246 break;
247 case 'l':
248 gcc_argv[i++] = strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid";
249 temp_argv[j] = 0;
250 break;
251 default:
252 ; /* ignore the rest */
255 else
257 gcc_argv[i++] = get_obj_file(temp_argv, j);
258 temp_argv[j] = 0;
259 has_input_files = 1;
263 if (has_input_files)
265 /* Support the a.out default name, to appease configure */
266 if (!has_output_name)
268 gcc_argv[i++] = "-o";
269 gcc_argv[i++] = "a.out";
271 if (use_stdlib && use_msvcrt) gcc_argv[i++] = "-lmsvcrt";
272 if (gui_app) gcc_argv[i++] = "-lcomdlg32";
273 gcc_argv[i++] = "-ladvapi32";
274 gcc_argv[i++] = "-lshell32";
276 else
278 /* if we have nothing to process, just forward stuff to gcc */
279 memcpy(gcc_argv, argv, sizeof(char*) * argc);
280 gcc_argv[0] = cpp ? "g++" : "gcc";
281 i = argc;
284 else
286 gcc_argv[i++] = cpp ? "g++" : "gcc";
288 gcc_argv[i++] = "-fshort-wchar";
289 gcc_argv[i++] = "-fPIC";
290 if (use_stdinc)
292 if (use_msvcrt)
294 gcc_argv[i++] = "-I" INCLUDEDIR "/msvcrt";
295 gcc_argv[i++] = "-D__MSVCRT__";
297 gcc_argv[i++] = "-I" INCLUDEDIR "/windows";
299 gcc_argv[i++] = "-DWIN32";
300 gcc_argv[i++] = "-D_WIN32";
301 gcc_argv[i++] = "-D__WIN32";
302 gcc_argv[i++] = "-D__WIN32__";
303 gcc_argv[i++] = "-D__WINNT";
304 gcc_argv[i++] = "-D__WINNT__";
306 gcc_argv[i++] = "-D__stdcall=__attribute__((__stdcall__))";
307 gcc_argv[i++] = "-D__cdecl=__attribute__((__cdecl__))";
308 gcc_argv[i++] = "-D__fastcall=__attribute__((__fastcall__))";
309 gcc_argv[i++] = "-D_stdcall=__attribute__((__stdcall__))";
310 gcc_argv[i++] = "-D_cdecl=__attribute__((__cdecl__))";
311 gcc_argv[i++] = "-D_fastcall=__attribute__((__fastcall__))";
312 gcc_argv[i++] = "-D__declspec(x)=__declspec_##x";
313 gcc_argv[i++] = "-D__declspec_align(x)=__attribute__((aligned(x)))";
314 gcc_argv[i++] = "-D__declspec_allocate(x)=__attribute__((section(x)))";
315 gcc_argv[i++] = "-D__declspec_deprecated=__attribute__((deprecated))";
316 gcc_argv[i++] = "-D__declspec_dllimport=__attribute__((dllimport))";
317 gcc_argv[i++] = "-D__declspec_dllexport=__attribute__((dllexport))";
318 gcc_argv[i++] = "-D__declspec_naked=__attribute__((naked))";
319 gcc_argv[i++] = "-D__declspec_noinline=__attribute__((noinline))";
320 gcc_argv[i++] = "-D__declspec_noreturn=__attribute__((noreturn))";
321 gcc_argv[i++] = "-D__declspec_nothrow=__attribute__((nothrow))";
322 gcc_argv[i++] = "-D__declspec_novtable=__attribute__(())"; /* ignore it */
323 gcc_argv[i++] = "-D__declspec_selectany=__attribute__((weak))";
324 gcc_argv[i++] = "-D__declspec_thread=__thread";
326 /* Wine specific defines */
327 gcc_argv[i++] = "-D__WINE__";
328 gcc_argv[i++] = "-DWINE_UNICODE_NATIVE";
329 gcc_argv[i++] = "-D__int8=char";
330 gcc_argv[i++] = "-D__int16=short";
331 gcc_argv[i++] = "-D__int32=int";
332 gcc_argv[i++] = "-D__int64=long long";
334 for ( j = 1 ; j < argc ; j++ )
336 if (strcmp("-mno-cygwin", argv[j]) == 0)
337 ; /* ignore this option */
338 else if (strcmp("-mwindows", argv[j]) == 0)
339 ; /* ignore this option */
340 else if (strcmp("-mconsole", argv[j]) == 0)
341 ; /* ignore this option */
342 else if (strcmp("-mthreads", argv[j]) == 0)
343 ; /* ignore this option */
344 else if (strncmp("-Wl,", argv[j], 4) == 0)
345 ; /* do not pass linking options to compiler */
346 else if (strcmp("-s", argv[j]) == 0)
347 ; /* ignore this option */
348 else
349 gcc_argv[i++] = argv[j];
353 gcc_argv[i] = NULL;
355 spawn(gcc_argv);
357 return 0;