2003-09-12 00:16:33 +02:00
|
|
|
/*
|
2010-01-22 21:08:34 +01:00
|
|
|
* Useful functions for winegcc
|
2003-09-12 00:16:33 +02:00
|
|
|
*
|
|
|
|
* 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 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-09-12 00:16:33 +02:00
|
|
|
*/
|
2021-10-07 14:37:04 +02:00
|
|
|
|
2003-09-12 00:16:33 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
int verbose = 0;
|
|
|
|
|
2004-02-24 02:00:53 +01:00
|
|
|
void error(const char* s, ...)
|
2003-09-12 00:16:33 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, s);
|
2004-03-09 05:49:42 +01:00
|
|
|
fprintf(stderr, "winegcc: ");
|
2003-09-12 00:16:33 +02:00
|
|
|
vfprintf(stderr, s, ap);
|
|
|
|
va_end(ap);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2004-03-03 21:11:20 +01:00
|
|
|
void create_file(const char* name, int mode, const char* fmt, ...)
|
2004-02-24 02:00:53 +01: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 17:11:47 +02:00
|
|
|
error("Unable to open %s for writing\n", name);
|
2004-02-24 02:00:53 +01:00
|
|
|
vfprintf(file, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fclose(file);
|
2004-03-10 02:53:57 +01:00
|
|
|
chmod(name, mode);
|
2004-02-24 02:00:53 +01:00
|
|
|
}
|
|
|
|
|
2004-02-26 06:28:35 +01:00
|
|
|
file_type get_file_type(const char* filename)
|
2004-02-24 02:00:53 +01: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 };
|
2019-11-04 15:54:58 +01:00
|
|
|
static const char elf_sig[4] = "\177ELF";
|
2022-03-24 03:54:36 +01:00
|
|
|
static const char ar_sig[8] = "!<arch>\n";
|
2004-02-24 02:00:53 +01:00
|
|
|
char buf[sizeof(res_sig)];
|
|
|
|
int fd, cnt;
|
|
|
|
|
2004-02-26 06:28:35 +01:00
|
|
|
fd = open( filename, O_RDONLY );
|
|
|
|
if (fd == -1) return file_na;
|
2004-02-24 02:00:53 +01:00
|
|
|
cnt = read(fd, buf, sizeof(buf));
|
|
|
|
close( fd );
|
2004-02-26 06:28:35 +01:00
|
|
|
if (cnt == -1) return file_na;
|
2004-02-24 02:00:53 +01:00
|
|
|
|
|
|
|
if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
|
|
|
|
if (strendswith(filename, ".o")) return file_obj;
|
2021-07-02 11:47:58 +02:00
|
|
|
if (strendswith(filename, ".obj")) return file_obj;
|
2004-02-24 02:00:53 +01:00
|
|
|
if (strendswith(filename, ".a")) return file_arh;
|
|
|
|
if (strendswith(filename, ".res")) return file_res;
|
|
|
|
if (strendswith(filename, ".so")) return file_so;
|
2004-03-15 21:06:06 +01:00
|
|
|
if (strendswith(filename, ".dylib")) return file_so;
|
2004-03-23 01:14:54 +01:00
|
|
|
if (strendswith(filename, ".def")) return file_def;
|
|
|
|
if (strendswith(filename, ".spec")) return file_spec;
|
2004-02-24 02:00:53 +01:00
|
|
|
if (strendswith(filename, ".rc")) return file_rc;
|
2019-11-04 15:54:58 +01:00
|
|
|
if (cnt >= sizeof(elf_sig) && !memcmp(buf, elf_sig, sizeof(elf_sig))) return file_so; /* ELF lib */
|
2022-03-24 03:54:36 +01:00
|
|
|
if (cnt >= sizeof(ar_sig) && !memcmp(buf, ar_sig, sizeof(ar_sig))) return file_arh;
|
2019-11-04 15:54:58 +01:00
|
|
|
if (cnt >= sizeof(unsigned int) &&
|
|
|
|
(*(unsigned int *)buf == 0xfeedface || *(unsigned int *)buf == 0xcefaedfe ||
|
|
|
|
*(unsigned int *)buf == 0xfeedfacf || *(unsigned int *)buf == 0xcffaedfe))
|
|
|
|
return file_so; /* Mach-O lib */
|
2004-02-24 02:00:53 +01:00
|
|
|
|
|
|
|
return file_other;
|
|
|
|
}
|
|
|
|
|
2004-02-26 06:28:35 +01: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 02:00:53 +01:00
|
|
|
{
|
|
|
|
char *fullname;
|
|
|
|
file_type type;
|
|
|
|
|
2006-03-16 16:28:04 +01: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 06:28:35 +01: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 02:00:53 +01:00
|
|
|
free( fullname );
|
2004-02-26 06:28:35 +01:00
|
|
|
return 0;
|
2004-02-24 02:00:53 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
static file_type guess_lib_type(struct target target, const char* dir,
|
2021-05-28 19:02:56 +02:00
|
|
|
const char* library, const char *prefix, const char *suffix, char** file)
|
2004-02-24 02:00:53 +01:00
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.platform != PLATFORM_WINDOWS &&
|
|
|
|
target.platform != PLATFORM_MINGW &&
|
|
|
|
target.platform != PLATFORM_CYGWIN)
|
2009-05-30 15:24:07 +02:00
|
|
|
{
|
|
|
|
/* Unix shared object */
|
2021-05-28 19:02:56 +02:00
|
|
|
if ((*file = try_lib_path(dir, prefix, library, ".so", file_so)))
|
2009-05-30 15:24:07 +02:00
|
|
|
return file_so;
|
|
|
|
|
|
|
|
/* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
|
2021-05-28 19:02:56 +02:00
|
|
|
if ((*file = try_lib_path(dir, prefix, library, ".dylib", file_so)))
|
2009-05-30 15:24:07 +02:00
|
|
|
return file_so;
|
|
|
|
|
|
|
|
/* Windows DLL */
|
2021-05-28 19:02:56 +02:00
|
|
|
if ((*file = try_lib_path(dir, prefix, library, ".def", file_def)))
|
2009-05-30 15:24:07 +02:00
|
|
|
return file_dll;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static archives */
|
2021-05-28 19:02:56 +02:00
|
|
|
if ((*file = try_lib_path(dir, prefix, library, suffix, file_arh)))
|
2004-02-24 02:00:53 +01:00
|
|
|
return file_arh;
|
|
|
|
|
|
|
|
return file_na;
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
file_type get_lib_type(struct target target, struct strarray path, const char *library,
|
2021-05-28 19:02:56 +02:00
|
|
|
const char *prefix, const char *suffix, char** file)
|
2004-02-24 02:00:53 +01:00
|
|
|
{
|
2007-02-10 12:57:25 +01:00
|
|
|
unsigned int i;
|
2004-02-24 02:00:53 +01:00
|
|
|
|
2010-01-23 20:25:14 +01:00
|
|
|
if (!suffix) suffix = ".a";
|
2021-09-23 11:52:15 +02:00
|
|
|
for (i = 0; i < path.count; i++)
|
2004-02-24 02:00:53 +01:00
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
file_type type = guess_lib_type(target, path.str[i], library, prefix, suffix, file);
|
2004-02-24 02:00:53 +01:00
|
|
|
if (type != file_na) return type;
|
|
|
|
}
|
|
|
|
return file_na;
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:52:15 +02:00
|
|
|
const char *find_binary( struct strarray prefix, const char *name )
|
2019-09-10 17:57:19 +02:00
|
|
|
{
|
2020-03-03 14:01:59 +01:00
|
|
|
char *file_name, *args;
|
2021-09-23 11:52:15 +02:00
|
|
|
struct strarray dirs = empty_strarray;
|
|
|
|
static struct strarray path;
|
2019-09-10 17:57:19 +02:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (strchr( name, '/' )) return name;
|
|
|
|
|
2020-03-03 14:01:59 +01:00
|
|
|
file_name = xstrdup( name );
|
|
|
|
if ((args = strchr( file_name, ' ' ))) *args++ = 0;
|
|
|
|
|
2021-09-23 11:52:15 +02:00
|
|
|
if (!path.count) strarray_addall( &path, strarray_frompath( getenv( "PATH" )));
|
|
|
|
|
|
|
|
strarray_addall( &dirs, prefix );
|
|
|
|
strarray_addall( &dirs, path );
|
|
|
|
for (i = 0; i < dirs.count; i++)
|
2019-09-10 17:57:19 +02:00
|
|
|
{
|
2021-09-23 11:52:15 +02:00
|
|
|
struct stat st;
|
|
|
|
char *prog = strmake( "%s/%s%s", dirs.str[i], file_name, EXEEXT );
|
|
|
|
if (stat( prog, &st ) == 0 && S_ISREG( st.st_mode ) && (st.st_mode & 0111))
|
|
|
|
return args ? strmake( "%s %s", prog, args ) : prog;
|
|
|
|
free( prog );
|
2019-09-10 17:57:19 +02:00
|
|
|
}
|
2021-09-23 11:52:15 +02:00
|
|
|
return NULL;
|
2019-09-10 17:57:19 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 11:52:15 +02:00
|
|
|
int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
|
2003-09-12 00:16:33 +02:00
|
|
|
{
|
2007-02-10 12:57:25 +01:00
|
|
|
int status;
|
2003-09-12 00:16:33 +02:00
|
|
|
|
2021-09-28 18:18:49 +02:00
|
|
|
args.str[0] = find_binary( prefix, args.str[0] );
|
|
|
|
if (verbose) strarray_trace( args );
|
2003-09-12 00:16:33 +02:00
|
|
|
|
2021-09-28 18:18:49 +02:00
|
|
|
if ((status = strarray_spawn( args )) && !ignore_errors)
|
2004-02-24 02:00:53 +01:00
|
|
|
{
|
2021-09-28 18:18:49 +02:00
|
|
|
if (status > 0) error("%s failed\n", args.str[0]);
|
2004-03-09 05:49:42 +01:00
|
|
|
else perror("winegcc");
|
2004-02-24 02:00:53 +01:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
2013-08-27 11:03:42 +02:00
|
|
|
return status;
|
2003-09-12 00:16:33 +02:00
|
|
|
}
|