Moved all references to file descriptors out of the generic object
[wine] / tools / winegcc.c
1 /*
2  * MinGW wrapper: makes gcc behave like MinGW.
3  *
4  * Copyright 2000 Manuel Novoa III
5  * Copyright 2002 Dimitrie O. Paun
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
37
38 static char **tmp_files;
39 static int nb_tmp_files;
40 static int verbose = 0;
41 static int keep_generated = 0;
42
43 void error(const char *s, ...)
44 {
45     va_list ap;
46     
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);
53 }
54
55 char *strmake(const char *fmt, ...) 
56 {
57     int n, size = 100;
58     char *p;
59     va_list ap;
60
61     if ((p = malloc (size)) == NULL)
62         error("Can not malloc %d bytes.", size);
63     
64     while (1) 
65     {
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);
73     }
74 }
75
76 void spawn(char *const argv[])
77 {
78     int pid, status, wret, i;
79
80     if (verbose)
81     {   
82         for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
83         printf("\n");
84     }
85     
86     if ((pid = fork()) == 0) execvp(argv[0], argv);
87     else if (pid > 0)
88     {
89         while (pid != (wret = waitpid(pid, &status, 0)))
90             if (wret == -1 && errno != EINTR) break;
91         
92         if (pid == wret && WIFEXITED(status) && WEXITSTATUS(status) == 0) return;
93         error("%s failed.", argv[0]);
94     }
95     perror("Error:");
96     exit(3);
97 }
98
99 int strendswith(const char *str, const char *end)
100 {
101     int l = strlen(str);
102     int m = strlen(end);
103    
104     return l >= m && strcmp(str + l - m, end) == 0; 
105 }
106
107 void clean_temp_files()
108 {
109     int i;
110     
111     if (keep_generated) return;
112
113     for (i = 0; i < nb_tmp_files; i++)
114         unlink(tmp_files[i]);
115 }
116
117 char *get_temp_file(const char *suffix)
118 {
119     char *tmp = strmake("%s%s", tempnam(0, "wgcc"), suffix);
120
121     tmp_files = realloc( tmp_files, (nb_tmp_files+1) * sizeof(*tmp_files) );
122     tmp_files[nb_tmp_files++] = tmp;
123
124     return tmp;
125 }
126
127 char *get_obj_file(char **argv, int n)
128 {
129     char *tmpobj, **compargv;
130     int i, j;
131
132     if (strendswith(argv[n], ".o")) return argv[n];
133     if (strendswith(argv[n], ".a")) return argv[n];
134     
135     tmpobj = get_temp_file(".o");
136     compargv = malloc(sizeof(char*) * (n + 10));
137     i = 0;
138     compargv[i++] = BINDIR "/winegcc";
139     compargv[i++] = "-c";
140     compargv[i++] = "-o";
141     compargv[i++] = tmpobj;
142     for (j = 1; j <= n; j++)
143         if (argv[j]) compargv[i++] = argv[j];
144     compargv[i] = 0;
145     
146     spawn(compargv);
147
148     return tmpobj;
149 }
150
151
152 int main(int argc, char **argv)
153 {
154     char **gcc_argv;
155     int i, j;
156     int linking = 1, cpp = 0, use_static_linking = 0;
157     int use_stdinc = 1, use_stdlib = 1, use_msvcrt = 0, gui_app = 0;
158
159     atexit(clean_temp_files);
160     
161     if (strendswith(argv[0], "++")) cpp = 1;
162     
163     for ( i = 1 ; i < argc ; i++ ) 
164     {
165         if (argv[i][0] == '-')  /* option */
166         {
167             switch (argv[i][1]) 
168             {
169                 case 'c':        /* compile or assemble */
170                 case 'S':        /* generate assembler code */
171                 case 'E':        /* preprocess only */
172                     if (argv[i][2] == 0) linking = 0;
173                     break;
174                 case 'M':        /* map file generation */
175                     linking = 0;
176                     break;
177                 case 'm':
178                     if (strcmp("-mno-cygwin", argv[i]) == 0)
179                         use_msvcrt = 1;
180                     else if (strcmp("-mwindows", argv[i]) == 0)
181                         gui_app = 1;
182                     break;
183                 case 'n':
184                     if (strcmp("-nostdinc", argv[i]) == 0)
185                         use_stdinc = 0;
186                     else if (strcmp("-nodefaultlibs", argv[i]) == 0)
187                         use_stdlib = 0;
188                     else if (strcmp("-nostdlib", argv[i]) == 0)
189                         use_stdlib = 0;
190                     break;
191                 case 's':
192                     if (strcmp("-static", argv[i]) == 0) use_static_linking = 1;
193                     break;
194                 case 'v':        /* verbose */
195                     if (argv[i][2] == 0) verbose = 1;
196                     break;
197                 case 'V':
198                     printf("winegcc v0.3\n");
199                     exit(0);
200                     break;
201                 case 'W':
202                     if (strncmp("-Wl,", argv[i], 4) == 0)
203                     {
204                         if (strstr(argv[i], "-static"))
205                             use_static_linking = 1;
206                     }
207                     break;
208                 case '-':
209                     if (strcmp("-static", argv[i]+1) == 0)
210                         use_static_linking = 1;
211                     break;
212             }
213         } 
214     }
215
216     if (use_static_linking) error("Static linking is not supported.");
217
218     gcc_argv = malloc(sizeof(char*) * (argc + 20));
219
220     i = 0;
221     if (linking)
222     {
223         int has_output_name = 0;
224
225         gcc_argv[i++] = BINDIR "/winewrap";
226         if (gui_app) gcc_argv[i++] = "-mgui";
227
228         if (cpp) gcc_argv[i++] = "-C";  
229         for ( j = 1 ; j < argc ; j++ ) 
230         {
231             if ( argv[j][0] == '-' )
232             {
233                 switch (argv[j][1])
234                 {
235                 case 'L':
236                 case 'o':
237                     gcc_argv[i++] = argv[j];
238                     argv[j] = 0;
239                     if (!gcc_argv[i-1][2] && j + 1 < argc)
240                     {
241                         gcc_argv[i++] = argv[++j];
242                         argv[j] = 0;
243                     }
244                     has_output_name = 1;
245                     break;
246                 case 'l':
247                     gcc_argv[i++] = strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid"; 
248                     argv[j] = 0;
249                     break;
250                 default:
251                     ; /* ignore the rest */
252                 }
253             }
254             else
255             {
256                 gcc_argv[i++] = get_obj_file(argv, j);
257                 argv[j] = 0;
258             }
259
260             /* Support the a.out default name, to appease configure */
261             if (!has_output_name)
262             {
263                 gcc_argv[i++] = "-o";
264                 gcc_argv[i++] = "a.out";
265             }
266         }
267         if (use_stdlib && use_msvcrt) gcc_argv[i++] = "-lmsvcrt";
268         if (gui_app) gcc_argv[i++] = "-lcomdlg32";
269         if (gui_app) gcc_argv[i++] = "-lshell32";
270         gcc_argv[i++] = "-ladvapi32";
271     }
272     else
273     {
274         gcc_argv[i++] = cpp ? "g++" : "gcc";
275
276         gcc_argv[i++] = "-fshort-wchar";
277         gcc_argv[i++] = "-fPIC";
278         if (use_stdinc)
279         {
280             if (use_msvcrt) gcc_argv[i++] = "-I" INCLUDEDIR "/msvcrt";
281             gcc_argv[i++] = "-I" INCLUDEDIR "/windows";
282         }
283         gcc_argv[i++] = "-D__WINE__";
284         gcc_argv[i++] = "-D__WIN32__";
285         gcc_argv[i++] = "-DWINE_UNICODE_NATIVE";
286         gcc_argv[i++] = "-D__int8=char";
287         gcc_argv[i++] = "-D__int16=short";
288         gcc_argv[i++] = "-D__int32=int";
289         gcc_argv[i++] = "-D__int64=long long";
290
291         for ( j = 1 ; j < argc ; j++ ) 
292         {
293             if (strcmp("-mno-cygwin", argv[j]) == 0)
294                 ; /* ignore this option */
295             else if (strcmp("-mwindows", argv[j]) == 0)
296                 ; /* ignore this option */
297             else if (strcmp("-s", argv[j]) == 0)
298                 ; /* ignore this option */
299             else
300                 gcc_argv[i++] = argv[j];
301         }
302     }
303
304     gcc_argv[i] = NULL;
305
306     spawn(gcc_argv);
307
308     return 0;
309 }