blob: 225062d307d1d4f550861be9da7428181286bc4e [file] [log] [blame]
Richard Cohen2e6eed62003-09-11 22:16:33 +00001/*
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Richard Cohen2e6eed62003-09-11 22:16:33 +000021 */
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
37int verbose = 0;
38
Dimitrie O. Paune11108c2004-02-24 01:00:53 +000039void error(const char* s, ...)
Richard Cohen2e6eed62003-09-11 22:16:33 +000040{
41 va_list ap;
42
43 va_start(ap, s);
Alexandre Julliard2d52cfa2004-03-09 04:49:42 +000044 fprintf(stderr, "winegcc: ");
Richard Cohen2e6eed62003-09-11 22:16:33 +000045 vfprintf(stderr, s, ap);
Richard Cohen2e6eed62003-09-11 22:16:33 +000046 va_end(ap);
47 exit(2);
48}
49
Dimitrie O. Paune11108c2004-02-24 01:00:53 +000050void* xmalloc(size_t size)
Richard Cohen2e6eed62003-09-11 22:16:33 +000051{
Dimitrie O. Paune11108c2004-02-24 01:00:53 +000052 void* p;
53
Richard Cohen2e6eed62003-09-11 22:16:33 +000054 if ((p = malloc (size)) == NULL)
Francois Gougetdc3feef2007-10-18 17:11:47 +020055 error("Could not malloc %d bytes\n", size);
Richard Cohen2e6eed62003-09-11 22:16:33 +000056
57 return p;
58}
59
60void *xrealloc(void* p, size_t size)
61{
Alexandre Julliardb30d92d2005-03-18 14:09:55 +000062 void* p2 = realloc (p, size);
63 if (size && !p2)
Francois Gougetdc3feef2007-10-18 17:11:47 +020064 error("Could not realloc %d bytes\n", size);
Richard Cohen2e6eed62003-09-11 22:16:33 +000065
66 return p2;
67}
68
Dimitrie O. Paune11108c2004-02-24 01:00:53 +000069int 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
77char* strmake(const char* fmt, ...)
Richard Cohen2e6eed62003-09-11 22:16:33 +000078{
79 int n;
80 size_t size = 100;
Richard Cohen2e6eed62003-09-11 22:16:33 +000081 va_list ap;
Richard Cohen2e6eed62003-09-11 22:16:33 +000082
83 while (1)
84 {
Alexandre Julliard3a503492005-09-12 20:31:33 +000085 char *p = xmalloc (size);
Richard Cohen2e6eed62003-09-11 22:16:33 +000086 va_start(ap, fmt);
87 n = vsnprintf (p, size, fmt, ap);
88 va_end(ap);
Alexandre Julliard3a503492005-09-12 20:31:33 +000089 if (n == -1) size *= 2;
90 else if ((size_t)n >= size) size = n + 1;
91 else return p;
92 free(p);
Richard Cohen2e6eed62003-09-11 22:16:33 +000093 }
94}
95
Dimitrie O. Paune11108c2004-02-24 01:00:53 +000096strarray* strarray_alloc(void)
Richard Cohen2e6eed62003-09-11 22:16:33 +000097{
Dimitrie O. Paune11108c2004-02-24 01:00:53 +000098 strarray* arr = xmalloc(sizeof(*arr));
Richard Cohen2e6eed62003-09-11 22:16:33 +000099 arr->maximum = arr->size = 0;
100 arr->base = NULL;
101 return arr;
102}
103
104void strarray_free(strarray* arr)
105{
106 free(arr->base);
107 free(arr);
108}
109
Daniel Marmierf95be922003-10-15 03:35:54 +0000110void strarray_add(strarray* arr, const char* str)
Richard Cohen2e6eed62003-09-11 22:16:33 +0000111{
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
Joris Huizer909425d2007-02-10 03:57:25 -0800120void strarray_del(strarray* arr, unsigned int i)
Dimitrie O. Paun5f0796d2004-03-02 06:53:16 +0000121{
Francois Gougetdc3feef2007-10-18 17:11:47 +0200122 if (i >= arr->size) error("Invalid index i=%d\n", i);
Dimitrie O. Paun5f0796d2004-03-02 06:53:16 +0000123 memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
124 arr->size--;
125}
126
Dimitrie O. Paunf41c2b22004-03-02 02:23:26 +0000127void strarray_addall(strarray* arr, const strarray* from)
128{
Joris Huizer909425d2007-02-10 03:57:25 -0800129 unsigned int i;
Dimitrie O. Paunf41c2b22004-03-02 02:23:26 +0000130
131 for (i = 0; i < from->size; i++)
132 strarray_add(arr, from->base[i]);
133}
134
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000135strarray* strarray_dup(const strarray* arr)
136{
137 strarray* dup = strarray_alloc();
Joris Huizer909425d2007-02-10 03:57:25 -0800138 unsigned int i;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000139
140 for (i = 0; i < arr->size; i++)
141 strarray_add(dup, arr->base[i]);
142
143 return dup;
144}
145
Dimitrie O. Paunf41c2b22004-03-02 02:23:26 +0000146strarray* 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
159char* strarray_tostring(const strarray* arr, const char* sep)
160{
161 char *str, *newstr;
Joris Huizer909425d2007-02-10 03:57:25 -0800162 unsigned int i;
Dimitrie O. Paunf41c2b22004-03-02 02:23:26 +0000163
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
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000175char* 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
Dimitrie O. Paun2ab690b2004-03-03 20:11:20 +0000189void create_file(const char* name, int mode, const char* fmt, ...)
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000190{
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")) )
Francois Gougetdc3feef2007-10-18 17:11:47 +0200197 error("Unable to open %s for writing\n", name);
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000198 vfprintf(file, fmt, ap);
199 va_end(ap);
200 fclose(file);
Alexandre Julliard95784302004-03-10 01:53:57 +0000201 chmod(name, mode);
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000202}
203
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000204file_type get_file_type(const char* filename)
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000205{
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)];
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000209 int fd, cnt;
210
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000211 fd = open( filename, O_RDONLY );
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000212 if (fd == -1) return file_na;
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000213 cnt = read(fd, buf, sizeof(buf));
214 close( fd );
215 if (cnt == -1) return file_na;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000216
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;
Pierre d'Herbemontec132fe2004-03-15 20:06:06 +0000222 if (strendswith(filename, ".dylib")) return file_so;
Dimitrie O. Paunb613ee72004-03-23 00:14:54 +0000223 if (strendswith(filename, ".def")) return file_def;
224 if (strendswith(filename, ".spec")) return file_spec;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000225 if (strendswith(filename, ".rc")) return file_rc;
226
227 return file_other;
228}
229
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000230static char* try_lib_path(const char* dir, const char* pre,
231 const char* library, const char* ext,
232 file_type expected_type)
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000233{
234 char *fullname;
235 file_type type;
236
Alexandre Julliard870d4902006-03-16 16:28:04 +0100237 /* 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
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000245 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;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000250 free( fullname );
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000251 return 0;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000252}
253
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000254static file_type guess_lib_type(const char* dir, const char* library, char** file)
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000255{
256 /* Unix shared object */
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000257 if ((*file = try_lib_path(dir, "lib", library, ".so", file_so)))
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000258 return file_so;
Pierre d'Herbemontec132fe2004-03-15 20:06:06 +0000259
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;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000263
264 /* Windows DLL */
Dimitrie O. Paunb613ee72004-03-23 00:14:54 +0000265 if ((*file = try_lib_path(dir, "lib", library, ".def", file_def)))
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000266 return file_dll;
Dimitrie O. Paunb613ee72004-03-23 00:14:54 +0000267 if ((*file = try_lib_path(dir, "", library, ".def", file_def)))
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000268 return file_dll;
269
270 /* Unix static archives */
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000271 if ((*file = try_lib_path(dir, "lib", library, ".a", file_arh)))
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000272 return file_arh;
273
274 return file_na;
275}
276
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000277file_type get_lib_type(strarray* path, const char* library, char** file)
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000278{
Joris Huizer909425d2007-02-10 03:57:25 -0800279 unsigned int i;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000280
281 for (i = 0; i < path->size; i++)
282 {
Dimitrie O. Paun006ec802004-02-26 05:28:35 +0000283 file_type type = guess_lib_type(path->base[i], library, file);
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000284 if (type != file_na) return type;
285 }
286 return file_na;
287}
288
Alexandre Julliard866f52b2005-08-09 20:47:18 +0000289void spawn(const strarray* prefix, const strarray* args, int ignore_errors)
Richard Cohen2e6eed62003-09-11 22:16:33 +0000290{
Joris Huizer909425d2007-02-10 03:57:25 -0800291 unsigned int i;
292 int status;
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000293 strarray* arr = strarray_dup(args);
Alexandre Julliardacead482004-03-09 03:44:52 +0000294 const char** argv;
Dimitrie O. Paunfb1ae962004-03-09 01:34:00 +0000295 char* prog = 0;
296
Alexandre Julliardacead482004-03-09 03:44:52 +0000297 strarray_add(arr, NULL);
298 argv = arr->base;
299
Dimitrie O. Paunfb1ae962004-03-09 01:34:00 +0000300 if (prefix)
301 {
Alexandre Julliard2d52cfa2004-03-09 04:49:42 +0000302 for (i = 0; i < prefix->size; i++)
303 {
304 const char* p;
305 struct stat st;
Dimitrie O. Paunfb1ae962004-03-09 01:34:00 +0000306
Alexandre Julliard2d52cfa2004-03-09 04:49:42 +0000307 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 }
Dimitrie O. Paunfb1ae962004-03-09 01:34:00 +0000319 }
Richard Cohen2e6eed62003-09-11 22:16:33 +0000320
321 if (verbose)
322 {
323 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
324 printf("\n");
325 }
Richard Cohen2e6eed62003-09-11 22:16:33 +0000326
Alexandre Julliard866f52b2005-08-09 20:47:18 +0000327 if ((status = spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000328 {
Francois Gougetdc3feef2007-10-18 17:11:47 +0200329 if (status > 0) error("%s failed\n", argv[0]);
Alexandre Julliard2d52cfa2004-03-09 04:49:42 +0000330 else perror("winegcc");
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000331 exit(3);
332 }
333
Dimitrie O. Paunfb1ae962004-03-09 01:34:00 +0000334 free(prog);
Dimitrie O. Paune11108c2004-02-24 01:00:53 +0000335 strarray_free(arr);
Richard Cohen2e6eed62003-09-11 22:16:33 +0000336}