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
23 #include "wine/port.h"
33 #define WINEDLLS "/usr/local/lib/wine"
36 static const char *app_loader_script =
40 "# determine the application directory\n"
44 " # $0 contains a path, use it\n"
45 " appdir=`dirname \"$0\"`\n"
48 " # no directory in $0, search in PATH\n"
54 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
62 " debugmsg=\"$1 $2\"\n"
75 "# figure out the full app path\n"
76 "if [ -n \"$appdir\" ]; then\n"
77 " apppath=\"$appdir/$appname.exe.so\"\n"
79 " apppath=\"$appname.exe.so\"\n"
82 "# determine the WINELOADER\n"
83 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\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 =
99 " * Copyright 2000 Francois Gouget <fgouget@codeweavers.com> for CodeWeavers\n"
100 " * Copyright 2002 Dimitrie O. Paun <dpaun@rogers.com>\n"
103 "#include <dlfcn.h>\n"
104 "#include <windows.h>\n"
108 " * Describe the wrapped application\n"
111 "/* The app name */\n"
112 "#define APPNAME \"%s\"\n"
114 " * This is either 0 for a console based application or\n"
115 " * 1 for a regular windows application.\n"
117 "#define GUIEXE %d\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"
123 "static char* appName = APPNAME \".dll\";\n"
126 " * This is the name of the application's Windows module. If left NULL\n"
127 " * then appName is used.\n"
129 "static char* appModule = NULL;\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"
136 "static char* appInit = \"WinMain\";\n"
138 "static char* appInit = \"main\";\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"
146 "static char* mfcModule = NULL;\n"
149 "void error(const char *format, ...)\n"
154 " va_start(ap, format);\n"
155 " vsnprintf(msg, sizeof(msg), format, ap);\n"
156 " MessageBox(NULL, msg, \"Error\", MB_OK);\n"
163 "typedef int WINAPI (*WinMainFunc)(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
164 " PSTR szCmdLine, int iCmdShow);\n"
166 "typedef int WINAPI (*MainFunc)(int argc, char** argv, char** envp);\n"
170 "int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
171 " PSTR szCmdLine, int iCmdShow)\n"
173 "int WINAPI main(int argc, char** argv, char** envp)\n"
176 " HINSTANCE hApp = 0, hMFC = 0, hMain = 0;\n"
180 " /* Then if this application is MFC based, load the MFC module */\n"
181 " if (mfcModule) {\n"
182 " hMFC = LoadLibrary(mfcModule);\n"
183 " if (!hMFC) error(\"Could not load the MFC module %%s (%%d)\", mfcModule, GetLastError());\n"
184 " /* For MFC apps, WinMain is in the MFC library */\n"
188 " /* Load the application's module */\n"
189 " if (!appModule) appModule = appName;\n"
190 " hApp = LoadLibrary(appModule);\n"
191 " if (!hApp) error(\"Could not load the application's module %%s (%%d)\", appModule, GetLastError());\n"
192 " if (!hMain) hMain = hApp;\n"
194 " /* Get the address of the application's entry point */\n"
195 " appMain = GetProcAddress(hMain, appInit);\n"
196 " if (!appMain) error(\"Could not get the address of %%s (%%d)\", appInit, GetLastError());\n"
198 " /* And finally invoke the application's entry point */\n"
200 " retcode = (*((WinMainFunc)appMain))(hApp, hPrevInstance, szCmdLine, iCmdShow);\n"
202 " retcode = (*((MainFunc)appMain))(argc, argv, envp);\n"
205 " /* Cleanup and done */\n"
206 " FreeLibrary(hApp);\n"
207 " FreeLibrary(hMFC);\n"
213 static char *output_name;
214 static char **arh_files, **dll_files, **lib_files, **lib_paths, **obj_files;
215 static int nb_arh_files, nb_dll_files, nb_lib_files, nb_lib_paths, nb_obj_files;
216 static int verbose = 0;
217 static int keep_generated = 0;
219 void error(const char *s, ...)
224 fprintf(stderr, "Error: ");
225 vfprintf(stderr, s, ap);
226 fprintf(stderr, "\n");
231 char *strmake(const char *fmt, ...)
237 if ((p = malloc (size)) == NULL)
238 error("Can not malloc %d bytes.", size);
243 n = vsnprintf (p, size, fmt, ap);
245 if (n > -1 && n < size) return p;
247 if ((p = realloc (p, size)) == NULL)
248 error("Can not realloc %d bytes.", size);
252 void rm_temp_file(const char *file)
254 if (!keep_generated) unlink(file);
257 void create_file(const char *name, const char *fmt, ...)
262 if (verbose) printf("Creating file %s\n", name);
264 if ( !(file = fopen(name, "w")) )
265 error ("Can not create %s.", name);
266 vfprintf(file, fmt, ap);
271 void spawn(char *const argv[])
277 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
280 if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
282 if (status > 0) error("%s failed.", argv[0]);
283 else perror("Error:");
287 int is_resource(const char* file)
289 /* see tools/winebuild/res32.c: check_header for details */
290 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 };
291 char buf[sizeof(res_sig)];
294 if (!(fin = fopen(file, "r"))) error("Can not open %s", file);
295 if (fread(buf, sizeof(buf), 1, fin) != 1) error("Truncated file %s", file);
298 return !memcmp(buf, res_sig, sizeof(buf));
301 /* open the file for a given name, in a specified path, with the given extension */
302 static char *try_path( const char *path, const char *name, const char *ext )
307 fullname = strmake("%s/lib%s.%s", path, name, ext);
308 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
310 /* check if the file exists */
311 if ((fd = open( fullname, O_RDONLY )) != -1)
314 if (verbose > 1) fprintf(stderr, "FOUND!\n");
318 if (verbose > 1) fprintf(stderr, "\n");
322 /* open the .def library for a given dll in a specified path */
323 static char *try_dll_path( const char *path, const char *name )
325 return try_path(path, name, "def");
328 /* open the .a library for a given lib in a specified path */
329 static char *try_lib_path( const char *path, const char *name )
331 return try_path(path, name, "a");
334 /* open the .def library for a given dll */
335 static char *open_dll(const char *name)
340 for (i = 0; i < nb_lib_paths; i++)
342 if ((fullname = try_dll_path( lib_paths[i], name ))) return fullname;
344 return try_dll_path( ".", name );
347 /* find a static library */
348 static char *find_lib(const char *name)
350 static const char* std_paths[] = { ".", "/usr/lib", "/usr/local/lib" };
354 for (i = 0; i < nb_lib_paths; i++)
356 if ((fullname = try_lib_path( lib_paths[i], name ))) return fullname;
359 for (i = 0; i < sizeof(std_paths)/sizeof(std_paths[0]); i++)
361 if ((fullname = try_lib_path( std_paths[i], name ))) return fullname;
367 void add_lib_path(const char* path)
369 lib_paths = realloc( lib_paths, (nb_lib_paths+1) * sizeof(*lib_paths) );
370 lib_paths[nb_lib_paths++] = strdup(path);
371 dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
372 dll_files[nb_dll_files++] = strmake("-L%s", path);
373 lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
374 lib_files[nb_lib_files++] = strmake("-L%s", path);
377 void add_lib_file(const char* library)
381 if (open_dll(library))
383 dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
384 dll_files[nb_dll_files++] = strmake("-l%s", library);
386 else if ((lib = find_lib(library)))
388 arh_files = realloc( arh_files, (nb_arh_files+1) * sizeof(*arh_files) );
389 arh_files[nb_arh_files++] = lib;
393 lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
394 lib_files[nb_lib_files++] = strmake("-l%s", library);
398 int main(int argc, char **argv)
400 char *library = 0, *path = 0;
401 int i, j, len, cpp = 0, no_opt = 0, gui_mode = 0, create_wrapper = -1;
402 char *base_name, *base_file, *app_temp_name, *wrp_temp_name;
403 char *spec_name, *spec_c_name, *spec_o_name;
404 char *wspec_name, *wspec_c_name, *wspec_o_name;
405 char *wrap_c_name, *wrap_o_name;
406 char **spec_args, **comp_args, **link_args;
407 char **wwrap_args, **wspec_args, **wcomp_args, **wlink_args;
409 /* include the standard DLL path first */
410 add_lib_path(WINEDLLS);
412 for (i = 1; i < argc; i++)
414 if (!no_opt && argv[i][0] == '-')
422 if (argv[i][2]) output_name = strdup(argv[i]+ 2);
423 else if (i + 1 < argc) output_name = strdup(argv[++i]);
424 else error("The -o switch takes an argument.");
427 if (argv[i][2]) path = argv[i] + 2;
428 else if (i + 1 < argc) path = argv[++i];
429 else error("The -L switch takes an argument\n.");
433 if (argv[i][2]) library = argv[i] + 2;
434 else if (i + 1 < argc) library = argv[++i];
435 else error("The -l switch takes an argument\n.");
436 add_lib_file(library);
439 if (strcmp("-mgui", argv[i]) == 0) gui_mode = 1;
440 else error("Unknown option %s\n", argv[i]);
442 case 'v': /* verbose */
443 if (argv[i][2] == 0) verbose++;
446 printf("winewrap v0.40\n");
459 if (argv[i][2]) error("No long option supported.");
463 error("Unknown option -%c", argv[i][1]);
468 /* it's a filename, add it to its list */
469 obj_files = realloc( obj_files, (nb_obj_files+1) * sizeof(*obj_files) );
470 obj_files[nb_obj_files++] = strdup(argv[i]);
473 /* create wrapper only in C++ by default */
474 if (create_wrapper == -1) create_wrapper = cpp;
476 /* link in by default the big three */
477 if (gui_mode) add_lib_file("gdi32");
478 add_lib_file("user32");
479 add_lib_file("kernel32");
481 app_temp_name = tempnam(0, "wapp");
482 wrp_temp_name = tempnam(0, "wwrp");
484 /* get base filename by removing the .exe extension, if present */
485 base_file = strdup(output_name);
486 len = strlen(base_file);
487 if (len > 4 && strcmp(base_file + len - 4, ".exe") == 0)
488 base_file[len - 4 ] = 0; /* remove the .exe extension */
490 /* we need to get rid of the directory part to get the base name */
491 if ( (base_name = strrchr(base_file, '/')) ) base_name++;
492 else base_name = base_file;
494 spec_name = strmake("%s.spec", app_temp_name);
495 spec_c_name = strmake("%s.c", spec_name);
496 spec_o_name = strmake("%s.o", spec_name);
498 wspec_name = strmake("%s.spec", wrp_temp_name);
499 wspec_c_name = strmake("%s.c", wspec_name);
500 wspec_o_name = strmake("%s.o", wspec_name);
502 wrap_c_name = strmake("%s.c", wrp_temp_name);
503 wrap_o_name = strmake("%s.o", wrp_temp_name);
505 /* build winebuild's argument list */
506 spec_args = malloc( (nb_arh_files + nb_dll_files + nb_obj_files + 20) * sizeof (char *) );
508 spec_args[j++] = BINDIR "/winebuild";
509 spec_args[j++] = "-o";
510 spec_args[j++] = spec_c_name;
513 spec_args[j++] = "-F";
514 spec_args[j++] = strmake("%s.dll", base_name);
515 spec_args[j++] = "--spec";
516 spec_args[j++] = spec_name;
520 spec_args[j++] = "--exe";
521 spec_args[j++] = strmake("%s.exe", base_name);
522 spec_args[j++] = gui_mode ? "-mgui" : "-mcui";
524 for (i = 0; i < nb_dll_files; i++)
525 spec_args[j++] = dll_files[i];
526 for (i = 0; i < nb_obj_files; i++)
527 spec_args[j++] = obj_files[i];
528 for (i = 0; i < nb_arh_files; i++)
529 spec_args[j++] = arh_files[i];
532 /* build gcc's argument list */
533 comp_args = malloc ( 20 * sizeof (char *) );
535 comp_args[j++] = "gcc";
536 comp_args[j++] = "-fPIC";
537 comp_args[j++] = "-o";
538 comp_args[j++] = spec_o_name;
539 comp_args[j++] = "-c";
540 comp_args[j++] = spec_c_name;
543 /* build ld's argument list */
544 link_args = malloc( (nb_arh_files + nb_obj_files + nb_lib_files + 20) * sizeof (char *) );
546 link_args[j++] = cpp ? "g++" : "gcc";
547 link_args[j++] = "-shared";
548 link_args[j++] = "-Wl,-Bsymbolic,-z,defs";
549 link_args[j++] = "-lwine";
550 link_args[j++] = "-lm";
551 for (i = 0; i < nb_lib_files; i++)
552 link_args[j++] = lib_files[i];
553 link_args[j++] = "-o";
554 link_args[j++] = strmake("%s.%s.so", base_file, create_wrapper ? "dll" : "exe");
555 link_args[j++] = spec_o_name;
556 for (i = 0; i < nb_obj_files; i++)
557 if (!is_resource(obj_files[i])) link_args[j++] = obj_files[i];
558 for (i = 0; i < nb_arh_files; i++)
559 link_args[j++] = arh_files[i];
562 /* build wrapper compile argument list */
563 wwrap_args = malloc ( 20 * sizeof (char *) );
565 wwrap_args[j++] = "gcc";
566 wwrap_args[j++] = "-fPIC";
567 wwrap_args[j++] = "-I" INCLUDEDIR "/windows";
568 wwrap_args[j++] = "-o";
569 wwrap_args[j++] = wrap_o_name;
570 wwrap_args[j++] = "-c";
571 wwrap_args[j++] = wrap_c_name;
574 /* build wrapper winebuild's argument list */
575 wspec_args = malloc( (nb_dll_files + 20) * sizeof (char *) );
577 wspec_args[j++] = BINDIR "/winebuild";
578 wspec_args[j++] = "-o";
579 wspec_args[j++] = wspec_c_name;
580 wspec_args[j++] = "--exe";
581 wspec_args[j++] = strmake("%s.exe", base_name);
582 wspec_args[j++] = gui_mode ? "-mgui" : "-mcui";
583 wspec_args[j++] = wrap_o_name;
584 for (i = 0; i < nb_dll_files; i++)
585 wspec_args[j++] = dll_files[i];
588 /* build wrapper gcc's argument list */
589 wcomp_args = malloc ( 20 * sizeof (char *) );
591 wcomp_args[j++] = "gcc";
592 wcomp_args[j++] = "-fPIC";
593 wcomp_args[j++] = "-o";
594 wcomp_args[j++] = wspec_o_name;
595 wcomp_args[j++] = "-c";
596 wcomp_args[j++] = wspec_c_name;
599 /* build wrapper ld's argument list */
600 wlink_args = malloc( 20 * sizeof (char *) );
602 wlink_args[j++] = cpp ? "g++" : "gcc";
603 wlink_args[j++] = "-shared";
604 wlink_args[j++] = "-Wl,-Bsymbolic,-z,defs";
605 wlink_args[j++] = "-lwine";
606 wlink_args[j++] = "-ldl";
607 wlink_args[j++] = "-o";
608 wlink_args[j++] = strmake("%s.exe.so", base_file);
609 wlink_args[j++] = wspec_o_name;
610 wlink_args[j++] = wrap_o_name;
613 /* run winebuild to get the .spec.c file */
616 create_file(spec_name, gui_mode ? app_gui_spec : app_cui_spec);
618 rm_temp_file(spec_name);
620 rm_temp_file(spec_c_name);
622 rm_temp_file(spec_o_name);
624 create_file(wrap_c_name, wrapper_code, base_name, gui_mode);
626 rm_temp_file(wrap_c_name);
629 rm_temp_file(wspec_c_name);
631 rm_temp_file(wspec_o_name);
632 rm_temp_file(wrap_o_name);
638 rm_temp_file(spec_c_name);
640 rm_temp_file(spec_o_name);
643 /* create the loader script */
644 create_file(base_file, app_loader_script, base_name);
645 chmod(base_file, 0755);