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