utils.c 8.45 KB
Newer Older
1
/*
2
 * Useful functions for winegcc
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Copyright 2000 Francois Gouget
 * Copyright 2002 Dimitrie O. Paun
 * Copyright 2003 Richard Cohen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */
#include "config.h"
#include "wine/port.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>

#include "utils.h"

#if !defined(min)
# define min(x,y) (((x) < (y)) ? (x) : (y))
#endif

int verbose = 0;

39
void error(const char* s, ...)
40 41 42 43
{
    va_list ap;
    
    va_start(ap, s);
44
    fprintf(stderr, "winegcc: ");
45 46 47 48 49
    vfprintf(stderr, s, ap);
    va_end(ap);
    exit(2);
}

50
void* xmalloc(size_t size)
51
{
52 53
    void* p;

54
    if ((p = malloc (size)) == NULL)
55
	error("Could not malloc %d bytes\n", size);
56 57 58 59 60 61

    return p;
}

void *xrealloc(void* p, size_t size)
{
62 63
    void* p2 = realloc (p, size);
    if (size && !p2)
64
	error("Could not realloc %d bytes\n", size);
65 66 67 68

    return p2;
}

69 70 71 72 73 74 75
char *xstrdup( const char *str )
{
    char *res = strdup( str );
    if (!res) error("Virtual memory exhausted.\n");
    return res;
}

76 77 78 79 80 81 82 83 84
int strendswith(const char* str, const char* end)
{
    int l = strlen(str);
    int m = strlen(end);
   
    return l >= m && strcmp(str + l - m, end) == 0; 
}

char* strmake(const char* fmt, ...)
85 86 87 88 89 90 91
{
    int n;
    size_t size = 100;
    va_list ap;

    while (1)
    {
92
        char *p = xmalloc (size);
93 94 95
        va_start(ap, fmt);
	n = vsnprintf (p, size, fmt, ap);
	va_end(ap);
96 97 98 99
        if (n == -1) size *= 2;
        else if ((size_t)n >= size) size = n + 1;
        else return p;
        free(p);
100 101 102
    }
}

103
strarray* strarray_alloc(void)
104
{
105
    strarray* arr = xmalloc(sizeof(*arr));
106 107 108 109 110 111 112 113 114 115 116
    arr->maximum = arr->size = 0;
    arr->base = NULL;
    return arr;
}

void strarray_free(strarray* arr)
{
    free(arr->base);
    free(arr);
}

117
void strarray_add(strarray* arr, const char* str)
118 119 120 121 122 123 124 125 126
{
    if (arr->size == arr->maximum)
    {
	arr->maximum += 10;
	arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
    }
    arr->base[arr->size++] = str;
}

127
void strarray_del(strarray* arr, unsigned int i)
128
{
129
    if (i >= arr->size) error("Invalid index i=%d\n", i);
130 131 132 133
    memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
    arr->size--;
}

134 135
void strarray_addall(strarray* arr, const strarray* from)
{
136
    unsigned int i;
137 138 139 140 141

    for (i = 0; i < from->size; i++)
	strarray_add(arr, from->base[i]);
}

142 143 144
strarray* strarray_dup(const strarray* arr)
{
    strarray* dup = strarray_alloc();
145
    unsigned int i;
146 147 148 149 150 151 152

    for (i = 0; i < arr->size; i++)
	strarray_add(dup, arr->base[i]);

    return dup;
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
strarray* strarray_fromstring(const char* str, const char* delim)
{
    strarray* arr = strarray_alloc();
    char* buf = strdup(str);
    const char* tok;

    for(tok = strtok(buf, delim); tok; tok = strtok(0, delim))
	strarray_add(arr, strdup(tok));

    free(buf);
    return arr;
}

char* strarray_tostring(const strarray* arr, const char* sep)
{
    char *str, *newstr;
169
    unsigned int i;
170 171 172 173 174 175 176 177 178 179 180 181

    str = strmake("%s", arr->base[0]);
    for (i = 1; i < arr->size; i++)
    {
	newstr = strmake("%s%s%s", str, sep, arr->base[i]);
	free(str);
	str = newstr;
    }

    return str;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195
char* get_basename(const char* file)
{
    const char* name;
    char *base_name, *p;

    if ((name = strrchr(file, '/'))) name++;
    else name = file;

    base_name = strdup(name);
    if ((p = strrchr(base_name, '.'))) *p = 0;

    return base_name;
}

196
void create_file(const char* name, int mode, const char* fmt, ...)
197 198 199 200 201 202 203
{
    va_list ap;
    FILE *file;

    if (verbose) printf("Creating file %s\n", name);
    va_start(ap, fmt);
    if ( !(file = fopen(name, "w")) )
204
	error("Unable to open %s for writing\n", name);
205 206 207
    vfprintf(file, fmt, ap);
    va_end(ap);
    fclose(file);
208
    chmod(name, mode);
209 210
}

211
file_type get_file_type(const char* filename)
212 213 214 215 216 217
{
    /* see tools/winebuild/res32.c: check_header for details */
    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 };
    char buf[sizeof(res_sig)];
    int fd, cnt;

218 219
    fd = open( filename, O_RDONLY );
    if (fd == -1) return file_na;
220 221
    cnt = read(fd, buf, sizeof(buf));
    close( fd );
222
    if (cnt == -1) return file_na;
223 224 225 226 227 228

    if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
    if (strendswith(filename, ".o")) return file_obj;
    if (strendswith(filename, ".a")) return file_arh;
    if (strendswith(filename, ".res")) return file_res;
    if (strendswith(filename, ".so")) return file_so;
229
    if (strendswith(filename, ".dylib")) return file_so;
230 231
    if (strendswith(filename, ".def")) return file_def;
    if (strendswith(filename, ".spec")) return file_spec;
232 233 234 235 236
    if (strendswith(filename, ".rc")) return file_rc;

    return file_other;
}

237 238 239
static char* try_lib_path(const char* dir, const char* pre, 
			  const char* library, const char* ext,
			  file_type expected_type)
240 241 242 243
{
    char *fullname;
    file_type type;

244 245 246 247 248 249 250 251
    /* first try a subdir named from the library we are looking for */
    fullname = strmake("%s/%s/%s%s%s", dir, library, pre, library, ext);
    if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
    type = get_file_type(fullname);
    if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
    if (type == expected_type) return fullname;
    free( fullname );

252 253 254 255 256
    fullname = strmake("%s/%s%s%s", dir, pre, library, ext);
    if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
    type = get_file_type(fullname);
    if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
    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 267 268 269 270 271 272 273 274 275 276 277 278 279
    {
        /* Unix shared object */
        if ((*file = try_lib_path(dir, "lib", library, ".so", file_so)))
            return file_so;

        /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
        if ((*file = try_lib_path(dir, "lib", library, ".dylib", file_so)))
            return file_so;

        /* Windows DLL */
        if ((*file = try_lib_path(dir, "lib", library, ".def", file_def)))
            return file_dll;
    }

    /* static archives */
280
    if ((*file = try_lib_path(dir, "lib", library, suffix, file_arh)))
281 282 283 284 285
	return file_arh;

    return file_na;
}

286 287
file_type get_lib_type(enum target_platform platform, strarray* path, const char *library,
                       const char *suffix, char** file)
288
{
289
    unsigned int i;
290

291
    if (!suffix) suffix = ".a";
292 293
    for (i = 0; i < path->size; i++)
    {
294
        file_type type = guess_lib_type(platform, path->base[i], library, suffix, file);
295 296 297 298 299
	if (type != file_na) return type;
    }
    return file_na;
}

300
void spawn(const strarray* prefix, const strarray* args, int ignore_errors)
301
{
302 303
    unsigned int i;
    int status;
304
    strarray* arr = strarray_dup(args);
305
    const char** argv;
306 307
    char* prog = 0;

308 309 310
    strarray_add(arr, NULL);
    argv = arr->base;

311 312
    if (prefix)
    {
313 314 315 316
        const char *p = strrchr(argv[0], '/');
        if (!p) p = argv[0];
        else p++;

317 318 319 320 321
        for (i = 0; i < prefix->size; i++)
        {
            struct stat st;

            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 326
                argv[0] = prog;
                break;
327 328
            }
        }
329
    }
330 331 332 333 334 335 336

    if (verbose)
    {
	for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
	printf("\n");
    }

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 342 343
	exit(3);
    }

344
    free(prog);
345
    strarray_free(arr);
346
}