wrc: Fix the duplicate resource check for user-defined types.
[wine] / tools / winegcc / utils.c
1 /*
2  * Useful functions for winegcc/winewrap
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 int strendswith(const char* str, const char* end)
70 {
71     int l = strlen(str);
72     int m = strlen(end);
73    
74     return l >= m && strcmp(str + l - m, end) == 0; 
75 }
76
77 char* strmake(const char* fmt, ...)
78 {
79     int n;
80     size_t size = 100;
81     va_list ap;
82
83     while (1)
84     {
85         char *p = xmalloc (size);
86         va_start(ap, fmt);
87         n = vsnprintf (p, size, fmt, ap);
88         va_end(ap);
89         if (n == -1) size *= 2;
90         else if ((size_t)n >= size) size = n + 1;
91         else return p;
92         free(p);
93     }
94 }
95
96 strarray* strarray_alloc(void)
97 {
98     strarray* arr = xmalloc(sizeof(*arr));
99     arr->maximum = arr->size = 0;
100     arr->base = NULL;
101     return arr;
102 }
103
104 void strarray_free(strarray* arr)
105 {
106     free(arr->base);
107     free(arr);
108 }
109
110 void strarray_add(strarray* arr, const char* str)
111 {
112     if (arr->size == arr->maximum)
113     {
114         arr->maximum += 10;
115         arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
116     }
117     arr->base[arr->size++] = str;
118 }
119
120 void strarray_del(strarray* arr, unsigned int i)
121 {
122     if (i >= arr->size) error("Invalid index i=%d\n", i);
123     memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
124     arr->size--;
125 }
126
127 void strarray_addall(strarray* arr, const strarray* from)
128 {
129     unsigned int i;
130
131     for (i = 0; i < from->size; i++)
132         strarray_add(arr, from->base[i]);
133 }
134
135 strarray* strarray_dup(const strarray* arr)
136 {
137     strarray* dup = strarray_alloc();
138     unsigned int i;
139
140     for (i = 0; i < arr->size; i++)
141         strarray_add(dup, arr->base[i]);
142
143     return dup;
144 }
145
146 strarray* strarray_fromstring(const char* str, const char* delim)
147 {
148     strarray* arr = strarray_alloc();
149     char* buf = strdup(str);
150     const char* tok;
151
152     for(tok = strtok(buf, delim); tok; tok = strtok(0, delim))
153         strarray_add(arr, strdup(tok));
154
155     free(buf);
156     return arr;
157 }
158
159 char* strarray_tostring(const strarray* arr, const char* sep)
160 {
161     char *str, *newstr;
162     unsigned int i;
163
164     str = strmake("%s", arr->base[0]);
165     for (i = 1; i < arr->size; i++)
166     {
167         newstr = strmake("%s%s%s", str, sep, arr->base[i]);
168         free(str);
169         str = newstr;
170     }
171
172     return str;
173 }
174
175 char* get_basename(const char* file)
176 {
177     const char* name;
178     char *base_name, *p;
179
180     if ((name = strrchr(file, '/'))) name++;
181     else name = file;
182
183     base_name = strdup(name);
184     if ((p = strrchr(base_name, '.'))) *p = 0;
185
186     return base_name;
187 }
188
189 void create_file(const char* name, int mode, const char* fmt, ...)
190 {
191     va_list ap;
192     FILE *file;
193
194     if (verbose) printf("Creating file %s\n", name);
195     va_start(ap, fmt);
196     if ( !(file = fopen(name, "w")) )
197         error("Unable to open %s for writing\n", name);
198     vfprintf(file, fmt, ap);
199     va_end(ap);
200     fclose(file);
201     chmod(name, mode);
202 }
203
204 file_type get_file_type(const char* filename)
205 {
206     /* see tools/winebuild/res32.c: check_header for details */
207     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 };
208     char buf[sizeof(res_sig)];
209     int fd, cnt;
210
211     fd = open( filename, O_RDONLY );
212     if (fd == -1) return file_na;
213     cnt = read(fd, buf, sizeof(buf));
214     close( fd );
215     if (cnt == -1) return file_na;
216
217     if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
218     if (strendswith(filename, ".o")) return file_obj;
219     if (strendswith(filename, ".a")) return file_arh;
220     if (strendswith(filename, ".res")) return file_res;
221     if (strendswith(filename, ".so")) return file_so;
222     if (strendswith(filename, ".dylib")) return file_so;
223     if (strendswith(filename, ".def")) return file_def;
224     if (strendswith(filename, ".spec")) return file_spec;
225     if (strendswith(filename, ".rc")) return file_rc;
226
227     return file_other;
228 }
229
230 static char* try_lib_path(const char* dir, const char* pre, 
231                           const char* library, const char* ext,
232                           file_type expected_type)
233 {
234     char *fullname;
235     file_type type;
236
237     /* first try a subdir named from the library we are looking for */
238     fullname = strmake("%s/%s/%s%s%s", dir, library, pre, library, ext);
239     if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
240     type = get_file_type(fullname);
241     if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
242     if (type == expected_type) return fullname;
243     free( fullname );
244
245     fullname = strmake("%s/%s%s%s", dir, 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     return 0; 
252 }
253
254 static file_type guess_lib_type(const char* dir, const char* library, char** file)
255 {
256     /* Unix shared object */
257     if ((*file = try_lib_path(dir, "lib", library, ".so", file_so)))
258         return file_so;
259         
260     /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
261     if ((*file = try_lib_path(dir, "lib", library, ".dylib", file_so)))
262         return file_so;
263
264     /* Windows DLL */
265     if ((*file = try_lib_path(dir, "lib", library, ".def", file_def)))
266         return file_dll;
267     if ((*file = try_lib_path(dir, "", library, ".def", file_def)))
268         return file_dll;
269
270     /* Unix static archives */
271     if ((*file = try_lib_path(dir, "lib", library, ".a", file_arh)))
272         return file_arh;
273
274     return file_na;
275 }
276
277 file_type get_lib_type(strarray* path, const char* library, char** file)
278 {
279     unsigned int i;
280
281     for (i = 0; i < path->size; i++)
282     {
283         file_type type = guess_lib_type(path->base[i], library, file);
284         if (type != file_na) return type;
285     }
286     return file_na;
287 }
288
289 void spawn(const strarray* prefix, const strarray* args, int ignore_errors)
290 {
291     unsigned int i;
292     int status;
293     strarray* arr = strarray_dup(args);
294     const char** argv;
295     char* prog = 0;
296
297     strarray_add(arr, NULL);
298     argv = arr->base;
299
300     if (prefix)
301     {
302         for (i = 0; i < prefix->size; i++)
303         {
304             const char* p;
305             struct stat st;
306
307             if (!(p = strrchr(argv[0], '/'))) p = argv[0];
308             free( prog );
309             prog = strmake("%s/%s", prefix->base[i], p);
310             if (stat(prog, &st) == 0)
311             {
312                 if ((st.st_mode & S_IFREG) && (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
313                 {
314                     argv[0] = prog;
315                     break;
316                 }
317             }
318         }
319     }
320
321     if (verbose)
322     {
323         for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
324         printf("\n");
325     }
326
327     if ((status = spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
328     {
329         if (status > 0) error("%s failed\n", argv[0]);
330         else perror("winegcc");
331         exit(3);
332     }
333
334     free(prog);
335     strarray_free(arr);
336 }