Fixed a problem when multiple devices are present and a device other
[wine.git] / tools / winewrap.c
blobc61bfa838bf8862b255853c5d45ad1f40574e9db
1 /*
2 * Wine wrapper: takes care of internal details for linking.
4 * Copyright 2000 Francois Gouget
5 * Copyright 2002 Dimitrie O. Paun
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sys/stat.h>
32 #ifndef WINEDLLS
33 #define WINEDLLS "/usr/local/lib/wine"
34 #endif
36 static const char *app_loader_script =
37 "#!/bin/sh\n"
38 "\n"
39 "appname=\"%s\"\n"
40 "# determine the application directory\n"
41 "appdir=''\n"
42 "case \"$0\" in\n"
43 " */*)\n"
44 " # $0 contains a path, use it\n"
45 " appdir=`dirname \"$0\"`\n"
46 " ;;\n"
47 " *)\n"
48 " # no directory in $0, search in PATH\n"
49 " saved_ifs=$IFS\n"
50 " IFS=:\n"
51 " for d in $PATH\n"
52 " do\n"
53 " IFS=$saved_ifs\n"
54 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
55 " done\n"
56 " ;;\n"
57 "esac\n"
58 "\n"
59 "while true; do\n"
60 " case \"$1\" in\n"
61 " --debugmsg)\n"
62 " debugmsg=\"$1 $2\"\n"
63 " shift; shift;\n"
64 " ;;\n"
65 " --dll)\n"
66 " dll=\"$1 $2\"\n"
67 " shift; shift;\n"
68 " ;;\n"
69 " *)\n"
70 " break\n"
71 " ;;\n"
72 " esac\n"
73 "done\n"
74 "\n"
75 "# figure out the full app path\n"
76 "if [ -n \"$appdir\" ]; then\n"
77 " apppath=\"$appdir/$appname.exe.so\"\n"
78 "else\n"
79 " apppath=\"$appname.exe.so\"\n"
80 "fi\n"
81 "\n"
82 "# determine the WINELOADER\n"
83 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
84 "\n"
85 "# and try to start the app\n"
86 "exec \"$WINELOADER\" $debugmsg $dll -- \"$apppath\" \"$@\"\n"
89 static const char *app_gui_spec =
90 "@ stdcall WinMain(ptr long ptr long) WinMain\n"
93 static const char *app_cui_spec =
94 "@ stdcall main(long ptr ptr) main\n"
97 static const char *wrapper_code =
98 "/*\n"
99 " * Copyright 2000 Francois Gouget <fgouget@codeweavers.com> for CodeWeavers\n"
100 " * Copyright 2002 Dimitrie O. Paun <dpaun@rogers.com>\n"
101 " */\n"
102 "\n"
103 "#include <dlfcn.h>\n"
104 "#include <windows.h>\n"
105 "\n"
106 "\n"
107 "/*\n"
108 " * Describe the wrapped application\n"
109 " */\n"
110 "\n"
111 "/* The app name */\n"
112 "#define APPNAME \"%s\"\n"
113 "/**\n"
114 " * This is either 0 for a console based application or\n"
115 " * 1 for a regular windows application.\n"
116 " */\n"
117 "#define GUIEXE %d\n"
118 "\n"
119 "/**\n"
120 " * This is the name of the library containing the application,\n"
121 " * e.g. 'hello.dll' if the application is called 'hello.exe'.\n"
122 " */\n"
123 "static char* appName = APPNAME \".dll\";\n"
124 "\n"
125 "/**\n"
126 " * This is the name of the application's Windows module. If left NULL\n"
127 " * then appName is used.\n"
128 " */\n"
129 "static char* appModule = NULL;\n"
130 "\n"
131 "/**\n"
132 " * This is the application's entry point. This is usually 'WinMain' for a\n"
133 " * gui app and 'main' for a console application.\n"
134 " */\n"
135 "#if GUIEXE\n"
136 "static char* appInit = \"WinMain\";\n"
137 "#else\n"
138 "static char* appInit = \"main\";\n"
139 "#endif\n"
140 "\n"
141 "/**\n"
142 " * This is either non-NULL for MFC-based applications and is the name of the\n"
143 " * MFC's module. This is the module in which we will take the 'WinMain'\n"
144 " * function.\n"
145 " */\n"
146 "static char* mfcModule = NULL;\n"
147 "\n"
148 "\n"
149 "void error(const char *format, ...)\n"
150 "{\n"
151 " va_list ap;\n"
152 " char msg[4096];\n"
153 "\n"
154 " va_start(ap, format);\n"
155 " vsnprintf(msg, sizeof(msg), format, ap);\n"
156 " MessageBox(NULL, msg, \"Error\", MB_OK);\n"
157 " va_end(ap);\n"
158 " exit(1);\n"
159 "}\n"
160 "\n"
161 "\n"
162 "#if GUIEXE\n"
163 "typedef int WINAPI (*WinMainFunc)(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
164 " PSTR szCmdLine, int iCmdShow);\n"
165 "#else\n"
166 "typedef int WINAPI (*MainFunc)(int argc, char** argv, char** envp);\n"
167 "#endif\n"
168 "\n"
169 "#if GUIEXE\n"
170 "int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
171 " PSTR szCmdLine, int iCmdShow)\n"
172 "#else\n"
173 "int WINAPI main(int argc, char** argv, char** envp)\n"
174 "#endif\n"
175 "{\n"
176 " void* appLibrary;\n"
177 " HINSTANCE hApp = 0, hMFC = 0, hMain = 0;\n"
178 " void* appMain;\n"
179 " char* libName;\n"
180 " int retcode;\n"
181 "\n"
182 " /* Load the application's library */\n"
183 " libName=(char*)malloc(2+strlen(appName)+3+1);\n"
184 " /* FIXME: we should get the wrapper's path and use that as the base for\n"
185 " * the library\n"
186 " */\n"
187 " sprintf(libName,\"./%%s.so\",appName);\n"
188 " appLibrary=dlopen(libName,RTLD_NOW);\n"
189 " if (appLibrary==NULL) {\n"
190 " sprintf(libName,\"%%s.so\",appName);\n"
191 " appLibrary=dlopen(libName,RTLD_NOW);\n"
192 " }\n"
193 " if (!appLibrary) error(\"Could not load the %%s library: %%s\", libName, dlerror());\n"
194 "\n"
195 " /* Then if this application is MFC based, load the MFC module */\n"
196 " /* FIXME: I'm not sure this is really necessary */\n"
197 " if (mfcModule) {\n"
198 " hMFC = LoadLibrary(mfcModule);\n"
199 " if (!hMFC) error(\"Could not load the MFC module %%s (%%d)\", mfcModule, GetLastError());\n"
200 " /* MFC is a special case: the WinMain is in the MFC library,\n"
201 " * instead of the application's library.\n"
202 " */\n"
203 " hMain = hMFC;\n"
204 " }\n"
205 "\n"
206 " /* Load the application's module */\n"
207 " if (!appModule) appModule = appName;\n"
208 " hApp = LoadLibrary(appModule);\n"
209 " if (!hApp) error(\"Could not load the application's module %%s (%%d)\", appModule, GetLastError());\n"
210 " if (!hMain) hMain = hApp;\n"
211 "\n"
212 " /* Get the address of the application's entry point */\n"
213 " appMain = GetProcAddress(hMain, appInit);\n"
214 " if (!appMain) error(\"Could not get the address of %%s (%%d)\", appInit, GetLastError());\n"
215 "\n"
216 " /* And finally invoke the application's entry point */\n"
217 "#if GUIEXE\n"
218 " retcode = (*((WinMainFunc)appMain))(hApp, hPrevInstance, szCmdLine, iCmdShow);\n"
219 "#else\n"
220 " retcode = (*((MainFunc)appMain))(argc, argv, envp);\n"
221 "#endif\n"
222 "\n"
223 " /* Cleanup and done */\n"
224 " FreeLibrary(hApp);\n"
225 " FreeLibrary(hMFC);\n"
226 " dlclose(appLibrary);\n"
227 " free(libName);\n"
228 "\n"
229 " return retcode;\n"
230 "}\n"
233 static char *output_name;
234 static char **arh_files, **dll_files, **lib_files, **lib_paths, **obj_files;
235 static int nb_arh_files, nb_dll_files, nb_lib_files, nb_lib_paths, nb_obj_files;
236 static int verbose = 0;
237 static int keep_generated = 0;
239 void error(const char *s, ...)
241 va_list ap;
243 va_start(ap, s);
244 fprintf(stderr, "Error: ");
245 vfprintf(stderr, s, ap);
246 fprintf(stderr, "\n");
247 va_end(ap);
248 exit(2);
251 char *strmake(const char *fmt, ...)
253 int n, size = 100;
254 char *p;
255 va_list ap;
257 if ((p = malloc (size)) == NULL)
258 error("Can not malloc %d bytes.", size);
260 while (1)
262 va_start(ap, fmt);
263 n = vsnprintf (p, size, fmt, ap);
264 va_end(ap);
265 if (n > -1 && n < size) return p;
266 size *= 2;
267 if ((p = realloc (p, size)) == NULL)
268 error("Can not realloc %d bytes.", size);
272 void rm_temp_file(const char *file)
274 if (!keep_generated) unlink(file);
277 void create_file(const char *name, const char *fmt, ...)
279 va_list ap;
280 FILE *file;
282 if (verbose) printf("Creating file %s\n", name);
283 va_start(ap, fmt);
284 if ( !(file = fopen(name, "w")) )
285 error ("Can not create %s.", name);
286 vfprintf(file, fmt, ap);
287 va_end(ap);
288 fclose(file);
291 void spawn(char *const argv[])
293 int i, status;
295 if (verbose)
297 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
298 printf("\n");
300 if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
302 if (status > 0) error("%s failed.", argv[0]);
303 else perror("Error:");
304 exit(3);
307 int is_resource(const char* file)
309 /* see tools/winebuild/res32.c: check_header for details */
310 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 };
311 char buf[sizeof(res_sig)];
312 FILE *fin;
314 if (!(fin = fopen(file, "r"))) error("Can not open %s", file);
315 if (fread(buf, sizeof(buf), 1, fin) != 1) error("Truncated file %s", file);
316 fclose(fin);
318 return !memcmp(buf, res_sig, sizeof(buf));
321 /* open the file for a given name, in a specified path, with the given extension */
322 static char *try_path( const char *path, const char *name, const char *ext )
324 char *fullname;
325 int fd;
327 fullname = strmake("%s/lib%s.%s", path, name, ext);
328 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
330 /* check if the file exists */
331 if ((fd = open( fullname, O_RDONLY )) != -1)
333 close( fd );
334 if (verbose > 1) fprintf(stderr, "FOUND!\n");
335 return fullname;
337 free( fullname );
338 if (verbose > 1) fprintf(stderr, "\n");
339 return NULL;
342 /* open the .def library for a given dll in a specified path */
343 static char *try_dll_path( const char *path, const char *name )
345 return try_path(path, name, "def");
348 /* open the .a library for a given lib in a specified path */
349 static char *try_lib_path( const char *path, const char *name )
351 return try_path(path, name, "a");
354 /* open the .def library for a given dll */
355 static char *open_dll(const char *name)
357 char *fullname;
358 int i;
360 for (i = 0; i < nb_lib_paths; i++)
362 if ((fullname = try_dll_path( lib_paths[i], name ))) return fullname;
364 return try_dll_path( ".", name );
367 /* find a static library */
368 static char *find_lib(const char *name)
370 static const char* std_paths[] = { ".", "/usr/lib", "/usr/local/lib" };
371 char *fullname;
372 int i;
374 for (i = 0; i < nb_lib_paths; i++)
376 if ((fullname = try_lib_path( lib_paths[i], name ))) return fullname;
379 for (i = 0; i < sizeof(std_paths)/sizeof(std_paths[0]); i++)
381 if ((fullname = try_lib_path( std_paths[i], name ))) return fullname;
384 return 0;
387 void add_lib_path(const char* path)
389 lib_paths = realloc( lib_paths, (nb_lib_paths+1) * sizeof(*lib_paths) );
390 lib_paths[nb_lib_paths++] = strdup(path);
391 dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
392 dll_files[nb_dll_files++] = strmake("-L%s", path);
393 lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
394 lib_files[nb_lib_files++] = strmake("-L%s", path);
397 void add_lib_file(const char* library)
399 char *lib;
401 if (open_dll(library))
403 dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
404 dll_files[nb_dll_files++] = strmake("-l%s", library);
406 else if ((lib = find_lib(library)))
408 arh_files = realloc( arh_files, (nb_arh_files+1) * sizeof(*arh_files) );
409 arh_files[nb_arh_files++] = lib;
411 else
413 lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
414 lib_files[nb_lib_files++] = strmake("-l%s", library);
418 int main(int argc, char **argv)
420 char *library = 0, *path = 0;
421 int i, j, len, cpp = 0, no_opt = 0, gui_mode = 0, create_wrapper = -1;
422 char *base_name, *base_file, *app_temp_name, *wrp_temp_name;
423 char *spec_name, *spec_c_name, *spec_o_name;
424 char *wspec_name, *wspec_c_name, *wspec_o_name;
425 char *wrap_c_name, *wrap_o_name;
426 char **spec_args, **comp_args, **link_args;
427 char **wwrap_args, **wspec_args, **wcomp_args, **wlink_args;
429 /* include the standard DLL path first */
430 add_lib_path(WINEDLLS);
432 for (i = 1; i < argc; i++)
434 if (!no_opt && argv[i][0] == '-')
436 switch (argv[i][1])
438 case 'k':
439 keep_generated = 1;
440 break;
441 case 'o':
442 if (argv[i][2]) output_name = strdup(argv[i]+ 2);
443 else if (i + 1 < argc) output_name = strdup(argv[++i]);
444 else error("The -o switch takes an argument.");
445 break;
446 case 'L':
447 if (argv[i][2]) path = argv[i] + 2;
448 else if (i + 1 < argc) path = argv[++i];
449 else error("The -L switch takes an argument\n.");
450 add_lib_path(path);
451 break;
452 case 'l':
453 if (argv[i][2]) library = argv[i] + 2;
454 else if (i + 1 < argc) library = argv[++i];
455 else error("The -l switch takes an argument\n.");
456 add_lib_file(library);
457 break;
458 case 'm':
459 if (strcmp("-mgui", argv[i]) == 0) gui_mode = 1;
460 else error("Unknown option %s\n", argv[i]);
461 break;
462 case 'v': /* verbose */
463 if (argv[i][2] == 0) verbose++;
464 break;
465 case 'V':
466 printf("winewrap v0.40\n");
467 exit(0);
468 break;
469 case 'C':
470 cpp = 1;
471 break;
472 case 'w':
473 create_wrapper = 1;
474 break;
475 case 'W':
476 create_wrapper = 0;
477 break;
478 case '-':
479 if (argv[i][2]) error("No long option supported.");
480 no_opt = 1;
481 break;
482 default:
483 error("Unknown option -%c", argv[i][1]);
485 continue;
488 /* it's a filename, add it to its list */
489 obj_files = realloc( obj_files, (nb_obj_files+1) * sizeof(*obj_files) );
490 obj_files[nb_obj_files++] = strdup(argv[i]);
493 /* create wrapper only in C++ by default */
494 if (create_wrapper == -1) create_wrapper = cpp;
496 /* link in by default the big three */
497 if (gui_mode) add_lib_file("gdi32");
498 add_lib_file("user32");
499 add_lib_file("kernel32");
501 app_temp_name = tempnam(0, "wapp");
502 wrp_temp_name = tempnam(0, "wwrp");
504 /* get base filename by removing the .exe extension, if present */
505 base_file = strdup(output_name);
506 len = strlen(base_file);
507 if (len > 4 && strcmp(base_file + len - 4, ".exe") == 0)
508 base_file[len - 4 ] = 0; /* remove the .exe extension */
510 /* we need to get rid of the directory part to get the base name */
511 if ( (base_name = strrchr(base_file, '/')) ) base_name++;
512 else base_name = base_file;
514 spec_name = strmake("%s.spec", app_temp_name);
515 spec_c_name = strmake("%s.c", spec_name);
516 spec_o_name = strmake("%s.o", spec_name);
518 wspec_name = strmake("%s.spec", wrp_temp_name);
519 wspec_c_name = strmake("%s.c", wspec_name);
520 wspec_o_name = strmake("%s.o", wspec_name);
522 wrap_c_name = strmake("%s.c", wrp_temp_name);
523 wrap_o_name = strmake("%s.o", wrp_temp_name);
525 /* build winebuild's argument list */
526 spec_args = malloc( (nb_arh_files + nb_dll_files + nb_obj_files + 20) * sizeof (char *) );
527 j = 0;
528 spec_args[j++] = BINDIR "/winebuild";
529 spec_args[j++] = "-o";
530 spec_args[j++] = spec_c_name;
531 if (create_wrapper)
533 spec_args[j++] = "-F";
534 spec_args[j++] = strmake("%s.dll", base_name);
535 spec_args[j++] = "--spec";
536 spec_args[j++] = spec_name;
538 else
540 spec_args[j++] = "--exe";
541 spec_args[j++] = strmake("%s.exe", base_name);
542 spec_args[j++] = gui_mode ? "-mgui" : "-mcui";
544 for (i = 0; i < nb_dll_files; i++)
545 spec_args[j++] = dll_files[i];
546 for (i = 0; i < nb_obj_files; i++)
547 spec_args[j++] = obj_files[i];
548 for (i = 0; i < nb_arh_files; i++)
549 spec_args[j++] = arh_files[i];
550 spec_args[j] = 0;
552 /* build gcc's argument list */
553 comp_args = malloc ( 20 * sizeof (char *) );
554 j = 0;
555 comp_args[j++] = "gcc";
556 comp_args[j++] = "-fPIC";
557 comp_args[j++] = "-o";
558 comp_args[j++] = spec_o_name;
559 comp_args[j++] = "-c";
560 comp_args[j++] = spec_c_name;
561 comp_args[j] = 0;
563 /* build ld's argument list */
564 link_args = malloc( (nb_arh_files + nb_obj_files + nb_lib_files + 20) * sizeof (char *) );
565 j = 0;
566 link_args[j++] = cpp ? "g++" : "gcc";
567 link_args[j++] = "-shared";
568 link_args[j++] = "-Wl,-Bsymbolic,-z,defs";
569 link_args[j++] = "-lwine";
570 link_args[j++] = "-lm";
571 for (i = 0; i < nb_lib_files; i++)
572 link_args[j++] = lib_files[i];
573 link_args[j++] = "-o";
574 link_args[j++] = strmake("%s.%s.so", base_file, create_wrapper ? "dll" : "exe");
575 link_args[j++] = spec_o_name;
576 for (i = 0; i < nb_obj_files; i++)
577 if (!is_resource(obj_files[i])) link_args[j++] = obj_files[i];
578 for (i = 0; i < nb_arh_files; i++)
579 link_args[j++] = arh_files[i];
580 link_args[j] = 0;
582 /* build wrapper compile argument list */
583 wwrap_args = malloc ( 20 * sizeof (char *) );
584 j = 0;
585 wwrap_args[j++] = "gcc";
586 wwrap_args[j++] = "-fPIC";
587 wwrap_args[j++] = "-I" INCLUDEDIR "/windows";
588 wwrap_args[j++] = "-o";
589 wwrap_args[j++] = wrap_o_name;
590 wwrap_args[j++] = "-c";
591 wwrap_args[j++] = wrap_c_name;
592 wwrap_args[j] = 0;
594 /* build wrapper winebuild's argument list */
595 wspec_args = malloc( (nb_dll_files + 20) * sizeof (char *) );
596 j = 0;
597 wspec_args[j++] = BINDIR "/winebuild";
598 wspec_args[j++] = "-o";
599 wspec_args[j++] = wspec_c_name;
600 wspec_args[j++] = "--exe";
601 wspec_args[j++] = strmake("%s.exe", base_name);
602 wspec_args[j++] = gui_mode ? "-mgui" : "-mcui";
603 wspec_args[j++] = wrap_o_name;
604 for (i = 0; i < nb_dll_files; i++)
605 wspec_args[j++] = dll_files[i];
606 wspec_args[j] = 0;
608 /* build wrapper gcc's argument list */
609 wcomp_args = malloc ( 20 * sizeof (char *) );
610 j = 0;
611 wcomp_args[j++] = "gcc";
612 wcomp_args[j++] = "-fPIC";
613 wcomp_args[j++] = "-o";
614 wcomp_args[j++] = wspec_o_name;
615 wcomp_args[j++] = "-c";
616 wcomp_args[j++] = wspec_c_name;
617 wcomp_args[j] = 0;
619 /* build wrapper ld's argument list */
620 wlink_args = malloc( 20 * sizeof (char *) );
621 j = 0;
622 wlink_args[j++] = cpp ? "g++" : "gcc";
623 wlink_args[j++] = "-shared";
624 wlink_args[j++] = "-Wl,-Bsymbolic,-z,defs";
625 wlink_args[j++] = "-lwine";
626 wlink_args[j++] = "-ldl";
627 wlink_args[j++] = "-o";
628 wlink_args[j++] = strmake("%s.exe.so", base_file);
629 wlink_args[j++] = wspec_o_name;
630 wlink_args[j++] = wrap_o_name;
631 wlink_args[j] = 0;
633 /* run winebuild to get the .spec.c file */
634 if (create_wrapper)
636 create_file(spec_name, gui_mode ? app_gui_spec : app_cui_spec);
637 spawn(spec_args);
638 rm_temp_file(spec_name);
639 spawn(comp_args);
640 rm_temp_file(spec_c_name);
641 spawn(link_args);
642 rm_temp_file(spec_o_name);
644 create_file(wrap_c_name, wrapper_code, base_name, gui_mode);
645 spawn(wwrap_args);
646 rm_temp_file(wrap_c_name);
647 spawn(wspec_args);
648 spawn(wcomp_args);
649 rm_temp_file(wspec_c_name);
650 spawn(wlink_args);
651 rm_temp_file(wspec_o_name);
652 rm_temp_file(wrap_o_name);
654 else
656 spawn(spec_args);
657 spawn(comp_args);
658 rm_temp_file(spec_c_name);
659 spawn(link_args);
660 rm_temp_file(spec_o_name);
663 /* create the loader script */
664 create_file(base_file, app_loader_script, base_name);
665 chmod(base_file, 0755);
667 return 0;