Newer
Older
/*
* Wine wrapper: takes care of internal details for linking.
*
* Copyright 2002 Dimitrie O. Paun
*
* 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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <sys/wait.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef WINEDLLS
#define WINEDLLS "/usr/local/lib/wine"
#endif
static const char *app_loader_script =
"#!/bin/sh\n"
"\n"
"appname=\"%s\"\n"
"# determine the application directory\n"
"appdir=''\n"
"case \"$0\" in\n"
" */*)\n"
" # $0 contains a path, use it\n"
" appdir=`dirname \"$0\"`\n"
" ;;\n"
" *)\n"
" # no directory in $0, search in PATH\n"
" saved_ifs=$IFS\n"
" IFS=:\n"
" for d in $PATH\n"
" do\n"
" IFS=$saved_ifs\n"
" if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
" done\n"
" ;;\n"
"esac\n"
"\n"
"# figure out the full app path\n"
"if [ -n \"$appdir\" ]; then\n"
" apppath=\"$appdir/$appname.exe.so\"\n"
"else\n"
" apppath=\"$appname.exe.so\"\n"
"fi\n"
"\n"
"# determine the WINELOADER\n"
"if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
"\n"
"# and try to start the app\n"
"exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
;
static char *output_name;
static char **lib_files, **dll_files, **lib_paths, **obj_files;
static int nb_lib_files, nb_dll_files, nb_lib_paths, nb_obj_files;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
void error(const char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, "Error: ");
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(2);
}
char *strmake(const char *fmt, ...)
{
int n, size = 100;
char *p;
va_list ap;
if ((p = malloc (size)) == NULL) return NULL;
while (1)
{
va_start(ap, fmt);
n = vsnprintf (p, size, fmt, ap);
va_end(ap);
if (n > -1 && n < size) return p;
size *= 2;
if ((p = realloc (p, size)) == NULL) return NULL;
}
}
void spawn(char *const argv[])
{
int pid, status, wret, i;
{
for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
printf("\n");
}
if ((pid = fork()) == 0) execvp(argv[0], argv);
{
while (pid != (wret = waitpid(pid, &status, 0)))
if (wret == -1 && errno != EINTR) break;
if (pid == wret && WIFEXITED(status) && WEXITSTATUS(status) == 0) return;
error("%s failed.", argv[0]);
}
perror("Error:");
}
int is_resource(const char* file)
{
/* 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)];
FILE *fin;
if (!(fin = fopen(file, "r"))) error("Can not open %s", file);
if (fread(buf, sizeof(buf), 1, fin) != 1) error("Truncated file %s", file);
fclose(fin);
return !memcmp(buf, res_sig, sizeof(buf));
}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* open the .def library for a given dll in a specified path */
static char *try_dll_path( const char *path, const char *name )
{
char *fullname;
int fd;
fullname = strmake("%s/lib%s.def", path, name );
/* check if the file exists */
if ((fd = open( fullname, O_RDONLY )) != -1)
{
close( fd );
return fullname;
}
free( fullname );
return NULL;
}
/* open the .def library for a given dll */
static char *open_dll( const char *name )
{
char *fullname;
int i;
for (i = 0; i < nb_lib_paths; i++)
{
if ((fullname = try_dll_path( lib_paths[i], name ))) return fullname;
}
return try_dll_path( ".", name );
}
int main(int argc, char **argv)
{
int i, j, no_opt = 0, gui_mode = 0;
char *spec_name, *spec_c_name, *spec_o_name;
char **spec_args, **comp_args, **link_args;
FILE *file;
/* include the standard DLL path first */
lib_paths = realloc( lib_paths, (nb_lib_paths+1) * sizeof(*lib_paths) );
lib_paths[nb_lib_paths++] = WINEDLLS;
for (i = 1; i < argc; i++)
{
if (!no_opt && argv[i][0] == '-')
{
switch (argv[i][1])
{
case 'o':
if (argv[i][2]) output_name = strdup(argv[i]+ 2);
else if (i + 1 < argc) output_name = strdup(argv[++i]);
else error("The -o switch takes an argument.");
if ( !(p = strstr(output_name, ".exe")) || *(p+4) )
output_name = strmake("%s.exe", output_name);
break;
case 'L':
if (argv[i][2]) path = argv[i] + 2;
else if (i + 1 < argc) path = argv[++i];
else error("The -L switch takes an argument\n.");
lib_paths = realloc( lib_paths, (nb_lib_paths+1) * sizeof(*lib_paths) );
lib_paths[nb_lib_paths++] = strdup(path);
dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
dll_files[nb_dll_files++] = strmake("-L%s", path);
lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
lib_files[nb_lib_files++] = strmake("-L%s", path);
break;
case 'l':
if (argv[i][2]) library = argv[i] + 2;
else if (i + 1 < argc) library = argv[++i];
else error("The -l switch takes an argument\n.");
if (open_dll(library))
{
dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
dll_files[nb_dll_files++] = strmake("-l%s", library);
}
else
{
lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
lib_files[nb_lib_files++] = strmake("-l%s", library);
}
break;
case 'm':
if (strcmp("-mwindows", argv[i]) == 0) gui_mode = 1;
else if (strcmp("-mgui", argv[i]) == 0) gui_mode = 1;
else error("Unknown option %s\n", argv[i]);
break;
case 'v': /* verbose */
if (argv[i][2] == 0) verbose = 1;
break;
case 'W':
/* ignore such options for now for gcc compatibility */
break;
case '-':
if (argv[i][2]) error("No long option supported.");
no_opt = 1;
break;
default:
error("Unknown option -%c", argv[i][1]);
}
continue;
}
/* it's a filename, add it to its list */
obj_files = realloc( obj_files, (nb_obj_files+1) * sizeof(*obj_files) );
obj_files[nb_obj_files++] = strdup(argv[i]);
}
spec_name = tempnam(0, 0);
spec_c_name = strmake("%s.c", spec_name);
spec_o_name = strmake("%s.o", spec_name);
/* build winebuild's argument list */
spec_args = malloc( (nb_lib_files + nb_dll_files + nb_obj_files + 20) * sizeof (char *) );
j = 0;
spec_args[j++] = BINDIR "/winebuild";
spec_args[j++] = "-fPIC";
spec_args[j++] = "-o";
spec_args[j++] = strmake("%s.c", spec_name);
spec_args[j++] = "--exe";
spec_args[j++] = output_name;
spec_args[j++] = gui_mode ? "-mgui" : "-mcui";
for (i = 0; i < nb_obj_files; i++)
spec_args[j++] = obj_files[i];
spec_args[j++] = "-L" WINEDLLS;
for (i = 0; i < nb_dll_files; i++)
spec_args[j++] = dll_files[i];
spec_args[j] = 0;
/* build gcc's argument list */
comp_args = malloc (20 * sizeof (char *) );
j = 0;
comp_args[j++] = "gcc";
comp_args[j++] = "-fPIC";
comp_args[j++] = "-o";
comp_args[j++] = spec_o_name;
comp_args[j++] = "-c";
comp_args[j++] = spec_c_name;
/* build ld's argument list */
link_args = malloc( (nb_obj_files + 20) * sizeof (char *) );
j = 0;
link_args[j++] = "gcc";
link_args[j++] = "-shared";
link_args[j++] = "-Wl,-Bsymbolic,-z,defs";
for (i = 0; i < nb_lib_files; i++)
link_args[j++] = lib_files[i];
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
link_args[j++] = "-lwine";
link_args[j++] = "-lm";
link_args[j++] = "-o";
link_args[j++] = strmake("%s.so", output_name);
link_args[j++] = spec_o_name;
for (i = 0; i < nb_obj_files; i++)
if (!is_resource(obj_files[i])) link_args[j++] = obj_files[i];
link_args[j] = 0;
/* run winebuild to get the .spec.c file */
spawn(spec_args);
/* compile the .spec.c file */
spawn(comp_args);
unlink(spec_c_name);
/* run gcc to link */
spawn(link_args);
unlink(spec_o_name);
/* create the loader script */
output_name[strlen(output_name) - 4] = 0; /* remove the .exe extension */
if ( !(file = fopen(output_name, "w")) )
error ("Can not create %s.", output_name);
fprintf(file, app_loader_script, output_name);
fclose(file);
chmod(output_name, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
return 0;
}