Structure README file in a better way, some doc fixes.
[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 #include <sys/stat.h>
31
32 static char **tmp_files;
33 static int nb_tmp_files;
34 static int verbose = 0;
35 static int keep_generated = 0;
36
37 void error(const char *s, ...)
38 {
39     va_list ap;
40     
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);
47 }
48
49 char *strmake(const char *fmt, ...) 
50 {
51     int n, size = 100;
52     char *p;
53     va_list ap;
54
55     if ((p = malloc (size)) == NULL)
56         error("Can not malloc %d bytes.", size);
57     
58     while (1) 
59     {
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);
67     }
68 }
69
70 void spawn(char *const argv[])
71 {
72     int i, status;
73     
74     if (verbose)
75     {   
76         for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
77         printf("\n");
78     }
79     if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
80     
81     if (status > 0) error("%s failed.", argv[0]);
82     else perror("Error:");
83     exit(3);
84 }
85
86 int strendswith(const char *str, const char *end)
87 {
88     int l = strlen(str);
89     int m = strlen(end);
90    
91     return l >= m && strcmp(str + l - m, end) == 0; 
92 }
93
94 void clean_temp_files()
95 {
96     int i;
97     
98     if (keep_generated) return;
99
100     for (i = 0; i < nb_tmp_files; i++)
101         unlink(tmp_files[i]);
102 }
103
104 char *get_temp_file(const char *suffix)
105 {
106     char *tmp = strmake("wgcc.XXXXXX%s", suffix);
107     int fd = mkstemps( tmp, strlen(suffix) );
108     if (fd == -1)
109     {
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" );
115     }
116     close( fd );
117     tmp_files = realloc( tmp_files, (nb_tmp_files+1) * sizeof(*tmp_files) );
118     tmp_files[nb_tmp_files++] = tmp;
119
120     return tmp;
121 }
122
123 char *get_obj_file(char **argv, int n)
124 {
125     char *tmpobj, **compargv;
126     int i, j;
127
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];
131     
132     tmpobj = get_temp_file(".o");
133     compargv = malloc(sizeof(char*) * (n + 10));
134     i = 0;
135     compargv[i++] = BINDIR "/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;
142     
143     spawn(compargv);
144
145     return tmpobj;
146 }
147
148
149 int main(int argc, char **argv)
150 {
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;
155
156     atexit(clean_temp_files);
157     
158     if (strendswith(argv[0], "++")) cpp = 1;
159     
160     for ( i = 1 ; i < argc ; i++ ) 
161     {
162         if (argv[i][0] == '-')  /* option */
163         {
164             switch (argv[i][1]) 
165             {
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                     break;
180                 case 'n':
181                     if (strcmp("-nostdinc", argv[i]) == 0)
182                         use_stdinc = 0;
183                     else if (strcmp("-nodefaultlibs", argv[i]) == 0)
184                         use_stdlib = 0;
185                     else if (strcmp("-nostdlib", argv[i]) == 0)
186                         use_stdlib = 0;
187                     break;
188                 case 's':
189                     if (strcmp("-static", argv[i]) == 0) use_static_linking = 1;
190                     break;
191                 case 'v':        /* verbose */
192                     if (argv[i][2] == 0) verbose = 1;
193                     break;
194                 case 'V':
195                     printf("winegcc v0.3\n");
196                     exit(0);
197                     break;
198                 case 'W':
199                     if (strncmp("-Wl,", argv[i], 4) == 0)
200                     {
201                         if (strstr(argv[i], "-static"))
202                             use_static_linking = 1;
203                     }
204                     break;
205                 case '-':
206                     if (strcmp("-static", argv[i]+1) == 0)
207                         use_static_linking = 1;
208                     break;
209             }
210         } 
211     }
212
213     if (use_static_linking) error("Static linking is not supported.");
214
215     gcc_argv = malloc(sizeof(char*) * (argc + 20));
216
217     i = 0;
218     if (linking)
219     {
220         int has_output_name = 0;
221
222         gcc_argv[i++] = BINDIR "/winewrap";
223         if (gui_app) gcc_argv[i++] = "-mgui";
224
225         if (cpp) gcc_argv[i++] = "-C";  
226         for ( j = 1 ; j < argc ; j++ ) 
227         {
228             if ( argv[j][0] == '-' )
229             {
230                 switch (argv[j][1])
231                 {
232                 case 'L':
233                 case 'o':
234                     gcc_argv[i++] = argv[j];
235                     argv[j] = 0;
236                     if (!gcc_argv[i-1][2] && j + 1 < argc)
237                     {
238                         gcc_argv[i++] = argv[++j];
239                         argv[j] = 0;
240                     }
241                     has_output_name = 1;
242                     break;
243                 case 'l':
244                     gcc_argv[i++] = strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid"; 
245                     argv[j] = 0;
246                     break;
247                 default:
248                     ; /* ignore the rest */
249                 }
250             }
251             else
252             {
253                 gcc_argv[i++] = get_obj_file(argv, j);
254                 argv[j] = 0;
255             }
256
257             /* Support the a.out default name, to appease configure */
258             if (!has_output_name)
259             {
260                 gcc_argv[i++] = "-o";
261                 gcc_argv[i++] = "a.out";
262             }
263         }
264         if (use_stdlib && use_msvcrt) gcc_argv[i++] = "-lmsvcrt";
265         if (gui_app) gcc_argv[i++] = "-lcomdlg32";
266         gcc_argv[i++] = "-ladvapi32";
267         gcc_argv[i++] = "-lshell32";
268     }
269     else
270     {
271         gcc_argv[i++] = cpp ? "g++" : "gcc";
272
273         gcc_argv[i++] = "-fshort-wchar";
274         gcc_argv[i++] = "-fPIC";
275         if (use_stdinc)
276         {
277             if (use_msvcrt)
278             {
279                 gcc_argv[i++] = "-I" INCLUDEDIR "/msvcrt";
280                 gcc_argv[i++] = "-D__MSVCRT__";
281             }
282             gcc_argv[i++] = "-I" INCLUDEDIR "/windows";
283         }
284         gcc_argv[i++] = "-DWIN32";
285         gcc_argv[i++] = "-D_WIN32";
286         gcc_argv[i++] = "-D__WIN32";
287         gcc_argv[i++] = "-D__WIN32__";
288         gcc_argv[i++] = "-D__WINNT";
289         gcc_argv[i++] = "-D__WINNT__";
290
291         gcc_argv[i++] = "-D__stdcall=__attribute__((__stdcall__))";
292         gcc_argv[i++] = "-D__cdecl=__attribute__((__cdecl__))";
293         gcc_argv[i++] = "-D__fastcall=__attribute__((__fastcall__))";
294         gcc_argv[i++] = "-D_stdcall=__attribute__((__stdcall__))";
295         gcc_argv[i++] = "-D_cdecl=__attribute__((__cdecl__))";
296         gcc_argv[i++] = "-D_fastcall=__attribute__((__fastcall__))";
297         gcc_argv[i++] = "-D__declspec(x)=__attribute__((x))";
298     
299         /* Wine specific defines */
300         gcc_argv[i++] = "-D__WINE__";
301         gcc_argv[i++] = "-DWINE_UNICODE_NATIVE";
302         gcc_argv[i++] = "-D__int8=char";
303         gcc_argv[i++] = "-D__int16=short";
304         gcc_argv[i++] = "-D__int32=int";
305         gcc_argv[i++] = "-D__int64=long long";
306
307         for ( j = 1 ; j < argc ; j++ ) 
308         {
309             if (strcmp("-mno-cygwin", argv[j]) == 0)
310                 ; /* ignore this option */
311             else if (strcmp("-mwindows", argv[j]) == 0)
312                 ; /* ignore this option */
313             else if (strncmp("-Wl,", argv[j], 4) == 0)
314                 ; /* do not pass linking options to compiler */
315             else if (strcmp("-s", argv[j]) == 0)
316                 ; /* ignore this option */
317             else
318                 gcc_argv[i++] = argv[j];
319         }
320     }
321
322     gcc_argv[i] = NULL;
323
324     spawn(gcc_argv);
325
326     return 0;
327 }