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"
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 strarray
*arh_files
, *dll_files
, *lib_files
, *llib_paths
, *lib_paths
, *obj_files
;
215 static int keep_generated
= 0;
217 static void rm_temp_file(const char *file
)
219 if (!keep_generated
) unlink(file
);
222 static void create_file(const char *name
, const char *fmt
, ...)
227 if (verbose
) printf("Creating file %s\n", name
);
229 if ( !(file
= fopen(name
, "w")) )
230 error ("Can not create %s.", name
);
231 vfprintf(file
, fmt
, ap
);
236 static int is_resource(const char* file
)
238 /* see tools/winebuild/res32.c: check_header for details */
239 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 };
240 char buf
[sizeof(res_sig
)];
243 if (!(fin
= fopen(file
, "r"))) error("Can not open %s", file
);
244 if (fread(buf
, sizeof(buf
), 1, fin
) != 1) error("Truncated file %s", file
);
247 return !memcmp(buf
, res_sig
, sizeof(buf
));
250 /* open the file for a given name, in a specified path, with the given extension */
251 static char *try_path( const char *path
, const char *name
, const char *ext
)
256 fullname
= strmake("%s/lib%s.%s", path
, name
, ext
);
257 if (verbose
> 1) fprintf(stderr
, "Try %s...", fullname
);
259 /* check if the file exists */
260 if ((fd
= open( fullname
, O_RDONLY
)) != -1)
263 if (verbose
> 1) fprintf(stderr
, "FOUND!\n");
267 if (verbose
> 1) fprintf(stderr
, "\n");
271 /* open the .def library for a given dll in a specified path */
272 static char *try_dll_path( const char *path
, const char *name
)
274 return try_path(path
, name
, "def");
277 /* open the .a library for a given lib in a specified path */
278 static char *try_lib_path( const char *path
, const char *name
)
280 return try_path(path
, name
, "a");
283 /* open the .def library for a given dll */
284 static char *find_dll(const char *name
)
289 for (i
= 0; i
< lib_paths
->size
; i
++)
291 if ((fullname
= try_dll_path( lib_paths
->base
[i
], name
))) return fullname
;
293 return try_dll_path( ".", name
);
296 /* find a static library */
297 static char *find_lib(const char *name
)
299 static const char* std_paths
[] = { ".", "/usr/lib", "/usr/local/lib" };
303 for (i
= 0; i
< lib_paths
->size
; i
++)
305 if ((fullname
= try_lib_path( lib_paths
->base
[i
], name
))) return fullname
;
308 for (i
= 0; i
< sizeof(std_paths
)/sizeof(std_paths
[0]); i
++)
310 if ((fullname
= try_lib_path( std_paths
[i
], name
))) return fullname
;
316 static void add_lib_path(const char* path
)
318 strarray_add(lib_paths
, strdup(path
));
319 strarray_add(llib_paths
, strmake("-L%s", path
));
322 static void add_lib_file(const char* library
)
326 if (find_dll(library
))
328 strarray_add(dll_files
, strmake("-l%s", library
));
330 else if ((lib
= find_lib(library
)))
332 strarray_add(arh_files
, lib
);
336 strarray_add(lib_files
, strmake("-l%s", library
));
340 static void create_the_wrapper(char* base_file
, char* base_name
, char* app_name
, int gui_mode
)
342 char *wrp_temp_name
, *wspec_name
, *wspec_c_name
, *wspec_o_name
;
343 char *wrap_c_name
, *wrap_o_name
;
344 strarray
*wwrap_args
, *wspec_args
, *wcomp_args
, *wlink_args
;
346 wrp_temp_name
= tempnam(0, "wwrp");
347 wspec_name
= strmake("%s.spec", wrp_temp_name
);
348 wspec_c_name
= strmake("%s.c", wspec_name
);
349 wspec_o_name
= strmake("%s.o", wspec_name
);
351 wrap_c_name
= strmake("%s.c", wrp_temp_name
);
352 wrap_o_name
= strmake("%s.o", wrp_temp_name
);
354 /* build wrapper compile argument list */
355 wwrap_args
= strarray_alloc();
356 strarray_add(wwrap_args
, "gcc");
357 strarray_add(wwrap_args
, "-fPIC");
358 strarray_add(wwrap_args
, "-I" INCLUDEDIR
"/windows");
359 strarray_add(wwrap_args
, "-o");
360 strarray_add(wwrap_args
, wrap_o_name
);
361 strarray_add(wwrap_args
, "-c");
362 strarray_add(wwrap_args
, wrap_c_name
);
363 strarray_add(wwrap_args
, NULL
);
365 create_file(wrap_c_name
, wrapper_code
, base_name
, gui_mode
, app_name
);
367 strarray_free(wwrap_args
);
368 rm_temp_file(wrap_c_name
);
370 /* build wrapper winebuild's argument list */
371 wspec_args
= strarray_alloc();
372 strarray_add(wspec_args
, "winebuild");
373 strarray_add(wspec_args
, "-o");
374 strarray_add(wspec_args
, wspec_c_name
);
375 strarray_add(wspec_args
, "--exe");
376 strarray_add(wspec_args
, strmake("%s.exe", base_name
));
377 strarray_add(wspec_args
, gui_mode
? "-mgui" : "-mcui");
378 strarray_add(wspec_args
, wrap_o_name
);
379 strarray_add(wspec_args
, "-L" DLLDIR
);
380 strarray_add(wspec_args
, "-lkernel32");
381 strarray_add(wspec_args
, NULL
);
384 strarray_free(wspec_args
);
386 /* build wrapper gcc's argument list */
387 wcomp_args
= strarray_alloc();
388 strarray_add(wcomp_args
, "gcc");
389 strarray_add(wcomp_args
, "-fPIC");
390 strarray_add(wcomp_args
, "-o");
391 strarray_add(wcomp_args
, wspec_o_name
);
392 strarray_add(wcomp_args
, "-c");
393 strarray_add(wcomp_args
, wspec_c_name
);
394 strarray_add(wcomp_args
, NULL
);
397 strarray_free(wcomp_args
);
398 rm_temp_file(wspec_c_name
);
400 /* build wrapper ld's argument list */
401 wlink_args
= strarray_alloc();
402 strarray_add(wlink_args
, "gcc");
403 strarray_add(wlink_args
, "-shared");
404 strarray_add(wlink_args
, "-Wl,-Bsymbolic,-z,defs");
405 strarray_add(wlink_args
, "-lwine");
406 strarray_add(wlink_args
, "-o");
407 strarray_add(wlink_args
, strmake("%s.exe.so", base_file
));
408 strarray_add(wlink_args
, wspec_o_name
);
409 strarray_add(wlink_args
, wrap_o_name
);
410 strarray_add(wlink_args
, NULL
);
413 strarray_free(wlink_args
);
414 rm_temp_file(wspec_o_name
);
415 rm_temp_file(wrap_o_name
);
418 int main(int argc
, char **argv
)
420 char *library
= 0, *path
= 0;
421 int i
, len
, cpp
= 0, no_opt
= 0, gui_mode
= 0, create_wrapper
= -1;
422 char *base_name
, *base_file
, *base_path
, *app_temp_name
, *app_name
= 0;
423 char *spec_name
, *spec_c_name
, *spec_o_name
;
424 strarray
*spec_args
, *comp_args
, *link_args
;
426 arh_files
= strarray_alloc();
427 dll_files
= strarray_alloc();
428 lib_files
= strarray_alloc();
429 lib_paths
= strarray_alloc();
430 obj_files
= strarray_alloc();
431 llib_paths
= strarray_alloc();
433 /* include the standard DLL path first */
434 add_lib_path(DLLDIR
);
436 for (i
= 1; i
< argc
; i
++)
438 if (!no_opt
&& argv
[i
][0] == '-')
443 if (argv
[i
][2]) app_name
= strdup(argv
[i
]+ 2);
444 else if (i
+ 1 < argc
) app_name
= strdup(argv
[++i
]);
445 else error("The -a switch takes an argument.");
451 if (argv
[i
][2]) output_name
= strdup(argv
[i
]+ 2);
452 else if (i
+ 1 < argc
) output_name
= strdup(argv
[++i
]);
453 else error("The -o switch takes an argument.");
456 if (argv
[i
][2]) path
= argv
[i
] + 2;
457 else if (i
+ 1 < argc
) path
= argv
[++i
];
458 else error("The -L switch takes an argument\n.");
462 if (argv
[i
][2]) library
= argv
[i
] + 2;
463 else if (i
+ 1 < argc
) library
= argv
[++i
];
464 else error("The -l switch takes an argument\n.");
465 add_lib_file(library
);
468 if (strcmp("-mgui", argv
[i
]) == 0) gui_mode
= 1;
469 else error("Unknown option %s\n", argv
[i
]);
471 case 'v': /* verbose */
472 if (argv
[i
][2] == 0) verbose
++;
475 printf("winewrap v0.40\n");
488 if (argv
[i
][2]) error("No long option supported.");
492 error("Unknown option -%c", argv
[i
][1]);
497 /* it's a filename, add it to its list */
498 strarray_add(obj_files
, strdup(argv
[i
]));
501 /* create wrapper only in C++ by default */
502 if (create_wrapper
== -1) create_wrapper
= cpp
;
504 /* link in by default the big three */
505 if (gui_mode
) add_lib_file("gdi32");
506 add_lib_file("user32");
507 add_lib_file("kernel32");
509 app_temp_name
= tempnam(0, "wapp");
510 /* get base filename by removing the .exe extension, if present */
511 base_file
= strdup(output_name
);
512 len
= strlen(base_file
);
513 if (len
> 4 && strcmp(base_file
+ len
- 4, ".exe") == 0)
514 base_file
[len
- 4 ] = 0; /* remove the .exe extension */
516 /* we need to get rid of the directory part to get the base name */
517 if ( (base_name
= strrchr(base_file
, '/')) )
519 base_path
= strdup(base_file
);
520 *strrchr(base_path
, '/') = 0;
525 base_path
= strdup(".");
526 base_name
= base_file
;
529 /* create default name for the wrapper */
530 if (!app_name
) app_name
= strmake("%s-wrap.dll", base_name
);
532 spec_name
= strmake("%s.spec", app_temp_name
);
533 spec_c_name
= strmake("%s.c", spec_name
);
534 spec_o_name
= strmake("%s.o", spec_name
);
536 /* build winebuild's argument list */
537 spec_args
= strarray_alloc();
538 strarray_add(spec_args
, "winebuild");
539 strarray_add(spec_args
, "-o");
540 strarray_add(spec_args
, spec_c_name
);
543 create_file(spec_name
, gui_mode
? app_gui_spec
: app_cui_spec
);
544 strarray_add(spec_args
, "-F");
545 strarray_add(spec_args
, app_name
);
546 strarray_add(spec_args
, "--spec");
547 strarray_add(spec_args
, spec_name
);
551 strarray_add(spec_args
, "--exe");
552 strarray_add(spec_args
, strmake("%s.exe", base_name
));
553 strarray_add(spec_args
, gui_mode
? "-mgui" : "-mcui");
555 for (i
= 0; i
< llib_paths
->size
; i
++)
556 strarray_add(spec_args
, llib_paths
->base
[i
]);
557 for (i
= 0; i
< dll_files
->size
; i
++)
558 strarray_add(spec_args
, dll_files
->base
[i
]);
559 for (i
= 0; i
< obj_files
->size
; i
++)
560 strarray_add(spec_args
, obj_files
->base
[i
]);
561 for (i
= 0; i
< arh_files
->size
; i
++)
562 strarray_add(spec_args
, arh_files
->base
[i
]);
563 strarray_add(spec_args
, NULL
);
565 /* run winebuild to get the .spec.c file */
567 strarray_free(spec_args
);
570 rm_temp_file(spec_name
);
572 /* build gcc's argument list */
573 comp_args
= strarray_alloc();
574 strarray_add(comp_args
, "gcc");
575 strarray_add(comp_args
, "-fPIC");
576 strarray_add(comp_args
, "-o");
577 strarray_add(comp_args
, spec_o_name
);
578 strarray_add(comp_args
, "-c");
579 strarray_add(comp_args
, spec_c_name
);
580 strarray_add(comp_args
, NULL
);
583 strarray_free(comp_args
);
584 rm_temp_file(spec_c_name
);
586 /* build ld's argument list */
587 link_args
= strarray_alloc();
588 strarray_add(link_args
, cpp
? "g++" : "gcc");
589 strarray_add(link_args
, "-shared");
590 strarray_add(link_args
, "-Wl,-Bsymbolic,-z,defs");
591 strarray_add(link_args
, "-lwine");
592 strarray_add(link_args
, "-lm");
593 for (i
= 0; i
< llib_paths
->size
; i
++)
594 strarray_add(link_args
, llib_paths
->base
[i
]);
595 strarray_add(link_args
, "-o");
597 strarray_add(link_args
, strmake("%s/%s.so", base_path
, app_name
));
599 strarray_add(link_args
, strmake("%s.exe.so", base_file
));
600 strarray_add(link_args
, spec_o_name
);
602 for (i
= 0; i
< obj_files
->size
; i
++)
603 if (!is_resource(obj_files
->base
[i
]))
604 strarray_add(link_args
, obj_files
->base
[i
]);
605 for (i
= 0; i
< arh_files
->size
; i
++)
606 strarray_add(link_args
, arh_files
->base
[i
]);
607 strarray_add(link_args
, NULL
);
610 strarray_free(link_args
);
611 rm_temp_file(spec_o_name
);
614 create_the_wrapper(base_file
, base_name
, app_name
, gui_mode
);
616 /* create the loader script */
617 create_file(base_file
, app_loader_script
, base_name
);
618 chmod(base_file
, 0755);