2003-09-11 22:16:33 +00:00
|
|
|
/*
|
|
|
|
* Useful functions for winegcc/winewrap
|
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-09-11 22:16:33 +00:00
|
|
|
*/
|
|
|
|
#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;
|
|
|
|
|
2004-02-24 01:00:53 +00:00
|
|
|
void error(const char* s, ...)
|
2003-09-11 22:16:33 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, s);
|
2004-03-09 04:49:42 +00:00
|
|
|
fprintf(stderr, "winegcc: ");
|
2003-09-11 22:16:33 +00:00
|
|
|
vfprintf(stderr, s, ap);
|
|
|
|
va_end(ap);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2004-02-24 01:00:53 +00:00
|
|
|
void* xmalloc(size_t size)
|
2003-09-11 22:16:33 +00:00
|
|
|
{
|
2004-02-24 01:00:53 +00:00
|
|
|
void* p;
|
|
|
|
|
2003-09-11 22:16:33 +00:00
|
|
|
if ((p = malloc (size)) == NULL)
|
2007-10-18 15:11:47 +00:00
|
|
|
error("Could not malloc %d bytes\n", size);
|
2003-09-11 22:16:33 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *xrealloc(void* p, size_t size)
|
|
|
|
{
|
2005-03-18 14:09:55 +00:00
|
|
|
void* p2 = realloc (p, size);
|
|
|
|
if (size && !p2)
|
2007-10-18 15:11:47 +00:00
|
|
|
error("Could not realloc %d bytes\n", size);
|
2003-09-11 22:16:33 +00:00
|
|
|
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
2009-02-02 11:25:37 +00:00
|
|
|
char *xstrdup( const char *str )
|
|
|
|
{
|
|
|
|
char *res = strdup( str );
|
|
|
|
if (!res) error("Virtual memory exhausted.\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-02-24 01:00:53 +00:00
|
|
|
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, ...)
|
2003-09-11 22:16:33 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
size_t size = 100;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2005-09-12 20:31:33 +00:00
|
|
|
char *p = xmalloc (size);
|
2003-09-11 22:16:33 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
n = vsnprintf (p, size, fmt, ap);
|
|
|
|
va_end(ap);
|
2005-09-12 20:31:33 +00:00
|
|
|
if (n == -1) size *= 2;
|
|
|
|
else if ((size_t)n >= size) size = n + 1;
|
|
|
|
else return p;
|
|
|
|
free(p);
|
2003-09-11 22:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-24 01:00:53 +00:00
|
|
|
strarray* strarray_alloc(void)
|
2003-09-11 22:16:33 +00:00
|
|
|
{
|
2004-02-24 01:00:53 +00:00
|
|
|
strarray* arr = xmalloc(sizeof(*arr));
|
2003-09-11 22:16:33 +00:00
|
|
|
arr->maximum = arr->size = 0;
|
|
|
|
arr->base = NULL;
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void strarray_free(strarray* arr)
|
|
|
|
{
|
|
|
|
free(arr->base);
|
|
|
|
free(arr);
|
|
|
|
}
|
|
|
|
|
2003-10-15 03:35:54 +00:00
|
|
|
void strarray_add(strarray* arr, const char* str)
|
2003-09-11 22:16:33 +00:00
|
|
|
{
|
|
|
|
if (arr->size == arr->maximum)
|
|
|
|
{
|
|
|
|
arr->maximum += 10;
|
|
|
|
arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
|
|
|
|
}
|
|
|
|
arr->base[arr->size++] = str;
|
|
|
|
}
|
|
|
|
|
2007-02-10 11:57:25 +00:00
|
|
|
void strarray_del(strarray* arr, unsigned int i)
|
2004-03-02 06:53:16 +00:00
|
|
|
{
|
2007-10-18 15:11:47 +00:00
|
|
|
if (i >= arr->size) error("Invalid index i=%d\n", i);
|
2004-03-02 06:53:16 +00:00
|
|
|
memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
|
|
|
|
arr->size--;
|
|
|
|
}
|
|
|
|
|
2004-03-02 02:23:26 +00:00
|
|
|
void strarray_addall(strarray* arr, const strarray* from)
|
|
|
|
{
|
2007-02-10 11:57:25 +00:00
|
|
|
unsigned int i;
|
2004-03-02 02:23:26 +00:00
|
|
|
|
|
|
|
for (i = 0; i < from->size; i++)
|
|
|
|
strarray_add(arr, from->base[i]);
|
|
|
|
}
|
|
|
|
|
2004-02-24 01:00:53 +00:00
|
|
|
strarray* strarray_dup(const strarray* arr)
|
|
|
|
{
|
|
|
|
strarray* dup = strarray_alloc();
|
2007-02-10 11:57:25 +00:00
|
|
|
unsigned int i;
|
2004-02-24 01:00:53 +00:00
|
|
|
|
|
|
|
for (i = 0; i < arr->size; i++)
|
|
|
|
strarray_add(dup, arr->base[i]);
|
|
|
|
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
2004-03-02 02:23:26 +00:00
|
|
|
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;
|
2007-02-10 11:57:25 +00:00
|
|
|
unsigned int i;
|
2004-03-02 02:23:26 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-02-24 01:00:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-03-03 20:11:20 +00:00
|
|
|
void create_file(const char* name, int mode, const char* fmt, ...)
|
2004-02-24 01:00:53 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
if (verbose) printf("Creating file %s\n", name);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if ( !(file = fopen(name, "w")) )
|
2007-10-18 15:11:47 +00:00
|
|
|
error("Unable to open %s for writing\n", name);
|
2004-02-24 01:00:53 +00:00
|
|
|
vfprintf(file, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fclose(file);
|
2004-03-10 01:53:57 +00:00
|
|
|
chmod(name, mode);
|
2004-02-24 01:00:53 +00:00
|
|
|
}
|
|
|
|
|
2004-02-26 05:28:35 +00:00
|
|
|
file_type get_file_type(const char* filename)
|
2004-02-24 01:00:53 +00:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
2004-02-26 05:28:35 +00:00
|
|
|
fd = open( filename, O_RDONLY );
|
|
|
|
if (fd == -1) return file_na;
|
2004-02-24 01:00:53 +00:00
|
|
|
cnt = read(fd, buf, sizeof(buf));
|
|
|
|
close( fd );
|
2004-02-26 05:28:35 +00:00
|
|
|
if (cnt == -1) return file_na;
|
2004-02-24 01:00:53 +00:00
|
|
|
|
|
|
|
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;
|
2004-03-15 20:06:06 +00:00
|
|
|
if (strendswith(filename, ".dylib")) return file_so;
|
2004-03-23 00:14:54 +00:00
|
|
|
if (strendswith(filename, ".def")) return file_def;
|
|
|
|
if (strendswith(filename, ".spec")) return file_spec;
|
2004-02-24 01:00:53 +00:00
|
|
|
if (strendswith(filename, ".rc")) return file_rc;
|
|
|
|
|
|
|
|
return file_other;
|
|
|
|
}
|
|
|
|
|
2004-02-26 05:28:35 +00:00
|
|
|
static char* try_lib_path(const char* dir, const char* pre,
|
|
|
|
const char* library, const char* ext,
|
|
|
|
file_type expected_type)
|
2004-02-24 01:00:53 +00:00
|
|
|
{
|
|
|
|
char *fullname;
|
|
|
|
file_type type;
|
|
|
|
|
2006-03-16 15:28:04 +00:00
|
|
|
/* 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 );
|
|
|
|
|
2004-02-26 05:28:35 +00:00
|
|
|
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;
|
2004-02-24 01:00:53 +00:00
|
|
|
free( fullname );
|
2004-02-26 05:28:35 +00:00
|
|
|
return 0;
|
2004-02-24 01:00:53 +00:00
|
|
|
}
|
|
|
|
|
2009-05-30 13:24:07 +00:00
|
|
|
static file_type guess_lib_type(enum target_platform platform, const char* dir,
|
|
|
|
const char* library, char** file)
|
2004-02-24 01:00:53 +00:00
|
|
|
{
|
2009-05-30 13:24:07 +00:00
|
|
|
if (platform != PLATFORM_WINDOWS)
|
|
|
|
{
|
|
|
|
/* 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 */
|
2004-02-26 05:28:35 +00:00
|
|
|
if ((*file = try_lib_path(dir, "lib", library, ".a", file_arh)))
|
2004-02-24 01:00:53 +00:00
|
|
|
return file_arh;
|
|
|
|
|
|
|
|
return file_na;
|
|
|
|
}
|
|
|
|
|
2009-05-30 13:24:07 +00:00
|
|
|
file_type get_lib_type(enum target_platform platform, strarray* path, const char* library, char** file)
|
2004-02-24 01:00:53 +00:00
|
|
|
{
|
2007-02-10 11:57:25 +00:00
|
|
|
unsigned int i;
|
2004-02-24 01:00:53 +00:00
|
|
|
|
|
|
|
for (i = 0; i < path->size; i++)
|
|
|
|
{
|
2009-05-30 13:24:07 +00:00
|
|
|
file_type type = guess_lib_type(platform, path->base[i], library, file);
|
2004-02-24 01:00:53 +00:00
|
|
|
if (type != file_na) return type;
|
|
|
|
}
|
|
|
|
return file_na;
|
|
|
|
}
|
|
|
|
|
2005-08-09 20:47:18 +00:00
|
|
|
void spawn(const strarray* prefix, const strarray* args, int ignore_errors)
|
2003-09-11 22:16:33 +00:00
|
|
|
{
|
2007-02-10 11:57:25 +00:00
|
|
|
unsigned int i;
|
|
|
|
int status;
|
2004-02-24 01:00:53 +00:00
|
|
|
strarray* arr = strarray_dup(args);
|
2004-03-09 03:44:52 +00:00
|
|
|
const char** argv;
|
2004-03-09 01:34:00 +00:00
|
|
|
char* prog = 0;
|
|
|
|
|
2004-03-09 03:44:52 +00:00
|
|
|
strarray_add(arr, NULL);
|
|
|
|
argv = arr->base;
|
|
|
|
|
2004-03-09 01:34:00 +00:00
|
|
|
if (prefix)
|
|
|
|
{
|
2004-03-09 04:49:42 +00:00
|
|
|
for (i = 0; i < prefix->size; i++)
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!(p = strrchr(argv[0], '/'))) p = argv[0];
|
|
|
|
free( prog );
|
|
|
|
prog = strmake("%s/%s", prefix->base[i], p);
|
2008-12-12 11:01:46 +00:00
|
|
|
if (stat(prog, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111))
|
2004-03-09 04:49:42 +00:00
|
|
|
{
|
2008-12-12 11:01:46 +00:00
|
|
|
argv[0] = prog;
|
|
|
|
break;
|
2004-03-09 04:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-09 01:34:00 +00:00
|
|
|
}
|
2003-09-11 22:16:33 +00:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2005-08-09 20:47:18 +00:00
|
|
|
if ((status = spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
|
2004-02-24 01:00:53 +00:00
|
|
|
{
|
2007-10-18 15:11:47 +00:00
|
|
|
if (status > 0) error("%s failed\n", argv[0]);
|
2004-03-09 04:49:42 +00:00
|
|
|
else perror("winegcc");
|
2004-02-24 01:00:53 +00:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
2004-03-09 01:34:00 +00:00
|
|
|
free(prog);
|
2004-02-24 01:00:53 +00:00
|
|
|
strarray_free(arr);
|
2003-09-11 22:16:33 +00:00
|
|
|
}
|