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