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"
34 static const char *app_loader_script
=
38 "# determine the application directory\n"
42 " # $0 contains a path, use it\n"
43 " appdir=`dirname \"$0\"`\n"
46 " # no directory in $0, search in PATH\n"
52 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
60 " debugmsg=\"$1 $2\"\n"
73 "# figure out the full app path\n"
74 "if [ -n \"$appdir\" ]; then\n"
75 " apppath=\"$appdir/$appname.exe.so\"\n"
76 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
77 " export WINEDLLPATH\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 <stdio.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-wrap.dll' if the application is called 'hello.exe'.\n"
123 "static char* appName = \"%s\";\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 " fprintf(stderr, \"Error: %%s\\n\", msg);\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"
179 " const char* libs[] = { %s };\n"
181 " /* Then if this application is MFC based, load the MFC module */\n"
182 " if (mfcModule) {\n"
183 " hMFC = LoadLibrary(mfcModule);\n"
184 " if (!hMFC) error(\"Could not load the MFC module %%s (%%d)\", mfcModule, GetLastError());\n"
185 " /* For MFC apps, WinMain is in the MFC library */\n"
189 " for (i = 0; i < sizeof(libs)/sizeof(libs[0]); i++) {\n"
190 " if (!LoadLibrary(libs[i])) error(\"Could not load %%s (%%d)\", libs[i], GetLastError());\n"
193 " /* Load the application's module */\n"
194 " if (!appModule) appModule = appName;\n"
195 " hApp = LoadLibrary(appModule);\n"
196 " if (!hApp) error(\"Could not load the application's module %%s (%%d)\", appModule, GetLastError());\n"
197 " if (!hMain) hMain = hApp;\n"
199 " /* Get the address of the application's entry point */\n"
200 " appMain = GetProcAddress(hMain, appInit);\n"
201 " if (!appMain) error(\"Could not get the address of %%s (%%d)\", appInit, GetLastError());\n"
203 " /* And finally invoke the application's entry point */\n"
205 " retcode = (*((WinMainFunc)appMain))(hApp, hPrevInstance, szCmdLine, iCmdShow);\n"
207 " retcode = (*((MainFunc)appMain))(argc, argv, envp);\n"
210 " /* Cleanup and done */\n"
211 " FreeLibrary(hApp);\n"
212 " FreeLibrary(hMFC);\n"
218 static const char *output_name
= "a.out";
219 static strarray
*so_files
, *arh_files
, *dll_files
, *llib_paths
, *lib_paths
, *obj_files
;
220 static int keep_generated
= 0;
222 static void rm_temp_file(const char *file
)
224 if (!keep_generated
) unlink(file
);
227 static void create_file(const char *name
, const char *fmt
, ...)
232 if (verbose
) printf("Creating file %s\n", name
);
234 if ( !(file
= fopen(name
, "w")) )
235 error ("Can not create %s.", name
);
236 vfprintf(file
, fmt
, ap
);
241 static int is_resource(const char* file
)
243 /* see tools/winebuild/res32.c: check_header for details */
244 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 };
245 char buf
[sizeof(res_sig
)];
248 if (!(fin
= fopen(file
, "r"))) error("Can not open %s", file
);
249 if (fread(buf
, sizeof(buf
), 1, fin
) != 1) error("Truncated file %s", file
);
252 return !memcmp(buf
, res_sig
, sizeof(buf
));
255 /* open the file for a given name, in a specified path, with the given extension */
256 static char *try_path( const char *path
, const char *name
, const char *ext
)
261 fullname
= strmake("%s/lib%s.%s", path
, name
, ext
);
262 if (verbose
> 1) fprintf(stderr
, "Try %s...", fullname
);
264 /* check if the file exists */
265 if ((fd
= open( fullname
, O_RDONLY
)) != -1)
268 if (verbose
> 1) fprintf(stderr
, "FOUND!\n");
272 if (verbose
> 1) fprintf(stderr
, "\n");
277 static char* find_in_path(const strarray
* path
, const char* name
, const char* ext
)
280 for (i
= 0; i
< path
->size
; i
++)
283 if ((fullname
= try_path( path
->base
[i
], name
, ext
)))
289 /* find the .def library for a given dll */
290 static char *find_dll(const char *name
)
292 return find_in_path(lib_paths
, name
, "def");
295 /* find a unix library */
296 static char *find_unix_lib(const char *name
, const char* ext
)
299 static strarray
*std_paths
;
302 std_paths
= strarray_alloc();
303 strarray_add(std_paths
, "/usr/lib");
304 strarray_add(std_paths
, "/usr/local/lib");
307 if ((fullname
= find_in_path( lib_paths
, name
, ext
)))
310 return find_in_path( std_paths
, name
, ext
);
313 static void add_lib_path(const char* path
)
315 strarray_add(lib_paths
, strdup(path
));
316 strarray_add(llib_paths
, strmake("-L%s", path
));
319 static void identify_lib_file(const char* library
)
323 if (find_unix_lib(library
, "so"))
325 /* Unix shared object */
326 strarray_add(so_files
, strmake("-l%s", library
));
328 else if (find_dll(library
))
331 strarray_add(dll_files
, strmake("-l%s", library
));
333 else if ((lib
= find_unix_lib(library
, "a")))
335 /* winebuild needs the full path for .a files */
336 strarray_add(arh_files
, lib
);
340 fprintf(stderr
, "cannot find %s.{so,def,a} in library search path\n",
345 static void identify_lib_files(strarray
*lib_files
)
348 for (i
= 0; i
< lib_files
->size
; i
++)
350 identify_lib_file( lib_files
->base
[i
]);
355 static void create_the_wrapper(char* base_file
, char* base_name
, char* app_name
, int gui_mode
)
357 char *wrp_temp_name
, *wspec_name
, *wspec_c_name
, *wspec_o_name
;
358 char *wrap_c_name
, *wrap_o_name
;
359 const char *dlls
= "";
360 strarray
*wwrap_args
, *wspec_args
, *wcomp_args
, *wlink_args
;
363 wrp_temp_name
= tempnam(0, "wwrp");
364 wspec_name
= strmake("%s.spec", wrp_temp_name
);
365 wspec_c_name
= strmake("%s.c", wspec_name
);
366 wspec_o_name
= strmake("%s.o", wspec_name
);
368 wrap_c_name
= strmake("%s.c", wrp_temp_name
);
369 wrap_o_name
= strmake("%s.o", wrp_temp_name
);
371 /* build wrapper compile argument list */
372 wwrap_args
= strarray_alloc();
373 strarray_add(wwrap_args
, "gcc");
374 strarray_add(wwrap_args
, "-fPIC");
375 strarray_add(wwrap_args
, "-I" INCLUDEDIR
"/windows");
376 strarray_add(wwrap_args
, "-o");
377 strarray_add(wwrap_args
, wrap_o_name
);
378 strarray_add(wwrap_args
, "-c");
379 strarray_add(wwrap_args
, wrap_c_name
);
380 strarray_add(wwrap_args
, NULL
);
382 for (i
= dll_files
->size
- 1; i
>= 0; i
--)
383 dlls
= strmake("\"%s\", %s", dll_files
->base
[i
] + 2, dlls
);
384 create_file(wrap_c_name
, wrapper_code
, base_name
, gui_mode
, app_name
, dlls
);
386 strarray_free(wwrap_args
);
387 rm_temp_file(wrap_c_name
);
389 /* build wrapper winebuild's argument list */
390 wspec_args
= strarray_alloc();
391 strarray_add(wspec_args
, "winebuild");
392 strarray_add(wspec_args
, "-o");
393 strarray_add(wspec_args
, wspec_c_name
);
394 strarray_add(wspec_args
, "--exe");
395 strarray_add(wspec_args
, strmake("%s.exe", base_name
));
396 strarray_add(wspec_args
, gui_mode
? "-mgui" : "-mcui");
397 strarray_add(wspec_args
, wrap_o_name
);
398 for (i
= 0; i
< llib_paths
->size
; i
++)
399 strarray_add(wspec_args
, llib_paths
->base
[i
]);
400 strarray_add(wspec_args
, "-lkernel32");
401 strarray_add(wspec_args
, NULL
);
404 strarray_free(wspec_args
);
406 /* build wrapper gcc's argument list */
407 wcomp_args
= strarray_alloc();
408 strarray_add(wcomp_args
, "gcc");
409 strarray_add(wcomp_args
, "-fPIC");
410 strarray_add(wcomp_args
, "-o");
411 strarray_add(wcomp_args
, wspec_o_name
);
412 strarray_add(wcomp_args
, "-c");
413 strarray_add(wcomp_args
, wspec_c_name
);
414 strarray_add(wcomp_args
, NULL
);
417 strarray_free(wcomp_args
);
418 rm_temp_file(wspec_c_name
);
420 /* build wrapper ld's argument list */
421 wlink_args
= strarray_alloc();
422 strarray_add(wlink_args
, "gcc");
423 strarray_add(wlink_args
, "-shared");
424 strarray_add(wlink_args
, "-Wl,-Bsymbolic,-z,defs");
425 strarray_add(wlink_args
, "-lwine");
426 strarray_add(wlink_args
, "-o");
427 strarray_add(wlink_args
, strmake("%s.exe.so", base_file
));
428 strarray_add(wlink_args
, wspec_o_name
);
429 strarray_add(wlink_args
, wrap_o_name
);
430 for (i
= 0; i
< llib_paths
->size
; i
++)
431 strarray_add(wlink_args
, llib_paths
->base
[i
]);
432 strarray_add(wlink_args
, NULL
);
435 strarray_free(wlink_args
);
436 rm_temp_file(wspec_o_name
);
437 rm_temp_file(wrap_o_name
);
440 int main(int argc
, char **argv
)
442 char *library
= 0, *path
= 0;
443 int i
, len
, cpp
= 0, no_opt
= 0, gui_mode
= 0, create_wrapper
= -1;
444 char *base_name
, *base_file
, *base_path
, *app_temp_name
, *app_name
= 0;
445 char *spec_name
, *spec_c_name
, *spec_o_name
;
446 strarray
*spec_args
, *comp_args
, *link_args
;
449 so_files
= strarray_alloc();
450 arh_files
= strarray_alloc();
451 dll_files
= strarray_alloc();
452 lib_files
= strarray_alloc();
453 lib_paths
= strarray_alloc();
454 obj_files
= strarray_alloc();
455 llib_paths
= strarray_alloc();
457 for (i
= 1; i
< argc
; i
++)
459 if (!no_opt
&& argv
[i
][0] == '-')
464 if (argv
[i
][2]) app_name
= strdup(argv
[i
]+ 2);
465 else if (i
+ 1 < argc
) app_name
= strdup(argv
[++i
]);
466 else error("The -a switch takes an argument.");
472 if (argv
[i
][2]) output_name
= strdup(argv
[i
]+ 2);
473 else if (i
+ 1 < argc
) output_name
= strdup(argv
[++i
]);
474 else error("The -o switch takes an argument.");
477 if (argv
[i
][2]) path
= argv
[i
] + 2;
478 else if (i
+ 1 < argc
) path
= argv
[++i
];
479 else error("The -L switch takes an argument\n.");
483 if (argv
[i
][2]) library
= argv
[i
] + 2;
484 else if (i
+ 1 < argc
) library
= argv
[++i
];
485 else error("The -l switch takes an argument\n.");
486 strarray_add(lib_files
, library
);
489 if (strcmp("-mgui", argv
[i
]) == 0) gui_mode
= 1;
490 else error("Unknown option %s\n", argv
[i
]);
492 case 'v': /* verbose */
493 if (argv
[i
][2] == 0) verbose
++;
496 printf("winewrap v0.40\n");
509 if (argv
[i
][2]) error("No long option supported.");
513 error("Unknown option -%c", argv
[i
][1]);
518 /* it's a filename, add it to its list */
519 strarray_add(obj_files
, strdup(argv
[i
]));
522 /* create wrapper only in C++ by default */
523 if (create_wrapper
== -1) create_wrapper
= cpp
;
525 /* include the standard library (for eg libwine.so) and DLL paths last */
526 add_lib_path(DLLDIR
);
527 add_lib_path(LIBDIR
);
529 /* link in by default the big three */
531 strarray_add(lib_files
, "gdi32");
532 strarray_add(lib_files
, "user32");
533 strarray_add(lib_files
, "kernel32");
535 /* Sort out the libraries into .def's and .a's */
536 identify_lib_files(lib_files
);
537 strarray_free(lib_files
);
539 app_temp_name
= tempnam(0, "wapp");
540 /* get base filename by removing the .exe extension, if present */
541 base_file
= strdup(output_name
);
542 len
= strlen(base_file
);
543 if (len
> 4 && strcmp(base_file
+ len
- 4, ".exe") == 0)
544 base_file
[len
- 4 ] = 0; /* remove the .exe extension */
546 /* we need to get rid of the directory part to get the base name */
547 if ( (base_name
= strrchr(base_file
, '/')) )
549 base_path
= strdup(base_file
);
550 *strrchr(base_path
, '/') = 0;
555 base_path
= strdup(".");
556 base_name
= base_file
;
559 /* create default name for the wrapper */
560 if (!app_name
) app_name
= strmake("%s-wrap.dll", base_name
);
562 spec_name
= strmake("%s.spec", app_temp_name
);
563 spec_c_name
= strmake("%s.c", spec_name
);
564 spec_o_name
= strmake("%s.o", spec_name
);
566 /* build winebuild's argument list */
567 spec_args
= strarray_alloc();
568 strarray_add(spec_args
, "winebuild");
569 strarray_add(spec_args
, "-o");
570 strarray_add(spec_args
, spec_c_name
);
573 create_file(spec_name
, gui_mode
? app_gui_spec
: app_cui_spec
);
574 strarray_add(spec_args
, "-F");
575 strarray_add(spec_args
, app_name
);
576 strarray_add(spec_args
, "--spec");
577 strarray_add(spec_args
, spec_name
);
581 strarray_add(spec_args
, "--exe");
582 strarray_add(spec_args
, strmake("%s.exe", base_name
));
583 strarray_add(spec_args
, gui_mode
? "-mgui" : "-mcui");
585 for (i
= 0; i
< llib_paths
->size
; i
++)
586 strarray_add(spec_args
, llib_paths
->base
[i
]);
587 for (i
= 0; i
< dll_files
->size
; i
++)
588 strarray_add(spec_args
, dll_files
->base
[i
]);
589 for (i
= 0; i
< obj_files
->size
; i
++)
590 strarray_add(spec_args
, obj_files
->base
[i
]);
591 for (i
= 0; i
< arh_files
->size
; i
++)
592 strarray_add(spec_args
, arh_files
->base
[i
]);
593 strarray_add(spec_args
, NULL
);
595 /* run winebuild to get the .spec.c file */
597 strarray_free(spec_args
);
600 rm_temp_file(spec_name
);
602 /* build gcc's argument list */
603 comp_args
= strarray_alloc();
604 strarray_add(comp_args
, "gcc");
605 strarray_add(comp_args
, "-fPIC");
606 strarray_add(comp_args
, "-o");
607 strarray_add(comp_args
, spec_o_name
);
608 strarray_add(comp_args
, "-c");
609 strarray_add(comp_args
, spec_c_name
);
610 strarray_add(comp_args
, NULL
);
613 strarray_free(comp_args
);
614 rm_temp_file(spec_c_name
);
616 /* build ld's argument list */
617 link_args
= strarray_alloc();
618 strarray_add(link_args
, cpp
? "g++" : "gcc");
619 strarray_add(link_args
, "-shared");
620 #ifdef HAVE_LINKER_INIT_FINI
621 strarray_add(link_args
, "-Wl,-Bsymbolic,-z,defs,-init,__wine_spec_init,-fini,__wine_spec_fini");
623 strarray_add(link_args
, "-Wl,-Bsymbolic,-z,defs");
626 for (i
= 0; i
< llib_paths
->size
; i
++)
627 strarray_add(link_args
, llib_paths
->base
[i
]);
628 strarray_add(link_args
, "-lwine");
629 strarray_add(link_args
, "-lm");
630 for (i
= 0; i
< so_files
->size
; i
++)
631 strarray_add(link_args
, so_files
->base
[i
]);
633 strarray_add(link_args
, "-o");
635 strarray_add(link_args
, strmake("%s/%s.so", base_path
, app_name
));
637 strarray_add(link_args
, strmake("%s.exe.so", base_file
));
638 strarray_add(link_args
, spec_o_name
);
640 for (i
= 0; i
< obj_files
->size
; i
++)
641 if (!is_resource(obj_files
->base
[i
]))
642 strarray_add(link_args
, obj_files
->base
[i
]);
643 for (i
= 0; i
< arh_files
->size
; i
++)
644 strarray_add(link_args
, arh_files
->base
[i
]);
645 strarray_add(link_args
, NULL
);
648 strarray_free(link_args
);
649 rm_temp_file(spec_o_name
);
652 create_the_wrapper(base_file
, base_name
, app_name
, gui_mode
);
654 /* create the loader script */
655 create_file(base_file
, app_loader_script
, base_name
);
656 chmod(base_file
, 0755);