tools: Add more spec files to synchronize automatically.
[wine] / tools / winegcc / utils.c
1 /*
2  * Useful functions for winegcc
3  *
4  * Copyright 2000 Francois Gouget
5  * Copyright 2002 Dimitrie O. Paun
6  * Copyright 2003 Richard Cohen
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #include "utils.h"
32
33 #if !defined(min)
34 # define min(x,y) (((x) < (y)) ? (x) : (y))
35 #endif
36
37 int verbose = 0;
38
39 void error(const char* s, ...)
40 {
41     va_list ap;
42     
43     va_start(ap, s);
44     fprintf(stderr, "winegcc: ");
45     vfprintf(stderr, s, ap);
46     va_end(ap);
47     exit(2);
48 }
49
50 void* xmalloc(size_t size)
51 {
52     void* p;
53
54     if ((p = malloc (size)) == NULL)
55         error("Could not malloc %d bytes\n", size);
56
57     return p;
58 }
59
60 void *xrealloc(void* p, size_t size)
61 {
62     void* p2 = realloc (p, size);
63     if (size && !p2)
64         error("Could not realloc %d bytes\n", size);
65
66     return p2;
67 }
68
69 char *xstrdup( const char *str )
70 {
71     char *res = strdup( str );
72     if (!res) error("Virtual memory exhausted.\n");
73     return res;
74 }
75
76 int strendswith(const char* str, const char* end)
77 {
78     int l = strlen(str);
79     int m = strlen(end);
80    
81     return l >= m && strcmp(str + l - m, end) == 0; 
82 }
83
84 char* strmake(const char* fmt, ...)
85 {
86     int n;
87     size_t size = 100;
88     va_list ap;
89
90     while (1)
91     {
92         char *p = xmalloc (size);
93         va_start(ap, fmt);
94         n = vsnprintf (p, size, fmt, ap);
95         va_end(ap);
96         if (n == -1) size *= 2;
97         else if ((size_t)n >= size) size = n + 1;
98         else return p;
99         free(p);
100     }
101 }
102
103 strarray* strarray_alloc(void)
104 {
105     strarray* arr = xmalloc(sizeof(*arr));
106     arr->maximum = arr->size = 0;
107     arr->base = NULL;
108     return arr;
109 }
110
111 void strarray_free(strarray* arr)
112 {
113     free(arr->base);
114     free(arr);
115 }
116
117 void strarray_add(strarray* arr, const char* str)
118 {
119     if (arr->size == arr->maximum)
120     {
121         arr->maximum += 10;
122         arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
123     }
124     arr->base[arr->size++] = str;
125 }
126
127 void strarray_del(strarray* arr, unsigned int i)
128 {
129     if (i >= arr->size) error("Invalid index i=%d\n", i);
130     memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
131     arr->size--;
132 }
133
134 void strarray_addall(strarray* arr, const strarray* from)
135 {
136     unsigned int i;
137
138     for (i = 0; i < from->size; i++)
139         strarray_add(arr, from->base[i]);
140 }
141
142 strarray* strarray_dup(const strarray* arr)
143 {
144     strarray* dup = strarray_alloc();
145     unsigned int i;
146
147     for (i = 0; i < arr->size; i++)
148         strarray_add(dup, arr->base[i]);
149
150     return dup;
151 }
152
153 strarray* strarray_fromstring(const char* str, const char* delim)
154 {
155     strarray* arr = strarray_alloc();
156     char* buf = strdup(str);
157     const char* tok;
158
159     for(tok = strtok(buf, delim); tok; tok = strtok(0, delim))
160         strarray_add(arr, strdup(tok));
161
162     free(buf);
163     return arr;
164 }
165
166 char* strarray_tostring(const strarray* arr, const char* sep)
167 {
168     char *str, *newstr;
169     unsigned int i;
170
171     str = strmake("%s", arr->base[0]);
172     for (i = 1; i < arr->size; i++)
173     {
174         newstr = strmake("%s%s%s", str, sep, arr->base[i]);
175         free(str);
176         str = newstr;
177     }
178
179     return str;
180 }
181
182 char* get_basename(const char* file)
183 {
184     const char* name;
185     char *base_name, *p;
186
187     if ((name = strrchr(file, '/'))) name++;
188     else name = file;
189
190     base_name = strdup(name);
191     if ((p = strrchr(base_name, '.'))) *p = 0;
192
193     return base_name;
194 }
195
196 void create_file(const char* name, int mode, const char* fmt, ...)
197 {
198     va_list ap;
199     FILE *file;
200
201     if (verbose) printf("Creating file %s\n", name);
202     va_start(ap, fmt);
203     if ( !(file = fopen(name, "w")) )
204         error("Unable to open %s for writing\n", name);
205     vfprintf(file, fmt, ap);
206     va_end(ap);
207     fclose(file);
208     chmod(name, mode);
209 }
210
211 file_type get_file_type(const char* filename)
212 {
213     /* see tools/winebuild/res32.c: check_header for details */
214     static const char res_sig[] = { 0,0,0,0, 32,0,0,0, 0xff,0xff, 0,0, 0xff,0xff, 0,0, 0,0,0,0, 0,0, 0,0, 0,0,0,0, 0,0,0,0 };
215     char buf[sizeof(res_sig)];
216     int fd, cnt;
217
218     fd = open( filename, O_RDONLY );
219     if (fd == -1) return file_na;
220     cnt = read(fd, buf, sizeof(buf));
221     close( fd );
222     if (cnt == -1) return file_na;
223
224     if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
225     if (strendswith(filename, ".o")) return file_obj;
226     if (strendswith(filename, ".a")) return file_arh;
227     if (strendswith(filename, ".res")) return file_res;
228     if (strendswith(filename, ".so")) return file_so;
229     if (strendswith(filename, ".dylib")) return file_so;
230     if (strendswith(filename, ".def")) return file_def;
231     if (strendswith(filename, ".spec")) return file_spec;
232     if (strendswith(filename, ".rc")) return file_rc;
233
234     return file_other;
235 }
236
237 static char* try_lib_path(const char* dir, const char* pre, 
238                           const char* library, const char* ext,
239                           file_type expected_type)
240 {
241     char *fullname;
242     file_type type;
243
244     /* first try a subdir named from the library we are looking for */
245     fullname = strmake("%s/%s/%s%s%s", dir, library, pre, library, ext);
246     if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
247     type = get_file_type(fullname);
248     if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
249     if (type == expected_type) return fullname;
250     free( fullname );
251
252     fullname = strmake("%s/%s%s%s", dir, pre, library, ext);
253     if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
254     type = get_file_type(fullname);
255     if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
256     if (type == expected_type) return fullname;
257     free( fullname );
258     return 0; 
259 }
260
261 static file_type guess_lib_type(enum target_platform platform, const char* dir,
262                                 const char* library, const char *suffix, char** file)
263 {
264     if (platform != PLATFORM_WINDOWS && platform != PLATFORM_CYGWIN)
265     {
266         /* Unix shared object */
267         if ((*file = try_lib_path(dir, "lib", library, ".so", file_so)))
268             return file_so;
269
270         /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
271         if ((*file = try_lib_path(dir, "lib", library, ".dylib", file_so)))
272             return file_so;
273
274         /* Windows DLL */
275         if ((*file = try_lib_path(dir, "lib", library, ".def", file_def)))
276             return file_dll;
277     }
278
279     /* static archives */
280     if ((*file = try_lib_path(dir, "lib", library, suffix, file_arh)))
281         return file_arh;
282
283     return file_na;
284 }
285
286 file_type get_lib_type(enum target_platform platform, strarray* path, const char *library,
287                        const char *suffix, char** file)
288 {
289     unsigned int i;
290
291     if (!suffix) suffix = ".a";
292     for (i = 0; i < path->size; i++)
293     {
294         file_type type = guess_lib_type(platform, path->base[i], library, suffix, file);
295         if (type != file_na) return type;
296     }
297     return file_na;
298 }
299
300 void spawn(const strarray* prefix, const strarray* args, int ignore_errors)
301 {
302     unsigned int i;
303     int status;
304     strarray* arr = strarray_dup(args);
305     const char** argv;
306     char* prog = 0;
307
308     strarray_add(arr, NULL);
309     argv = arr->base;
310
311     if (prefix)
312     {
313         const char *p = strrchr(argv[0], '/');
314         if (!p) p = argv[0];
315         else p++;
316
317         for (i = 0; i < prefix->size; i++)
318         {
319             struct stat st;
320
321             free( prog );
322             prog = strmake("%s/%s%s", prefix->base[i], p, EXEEXT);
323             if (stat(prog, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111))
324             {
325                 argv[0] = prog;
326                 break;
327             }
328         }
329     }
330
331     if (verbose)
332     {
333         for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
334         printf("\n");
335     }
336
337     if ((status = spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
338     {
339         if (status > 0) error("%s failed\n", argv[0]);
340         else perror("winegcc");
341         exit(3);
342     }
343
344     free(prog);
345     strarray_free(arr);
346 }