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"
37 #define WINEDLLS "/usr/local/lib/wine"
40 static const char *app_loader_script
=
44 "# determine the application directory\n"
48 " # $0 contains a path, use it\n"
49 " appdir=`dirname \"$0\"`\n"
52 " # no directory in $0, search in PATH\n"
58 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
66 " debugmsg=\"$1 $2\"\n"
79 "# figure out the full app path\n"
80 "if [ -n \"$appdir\" ]; then\n"
81 " apppath=\"$appdir/$appname.exe.so\"\n"
83 " apppath=\"$appname.exe.so\"\n"
86 "# determine the WINELOADER\n"
87 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
89 "# and try to start the app\n"
90 "exec \"$WINELOADER\" $debugmsg $dll -- \"$apppath\" \"$@\"\n"
93 static const char *app_gui_spec
=
94 "@ stdcall WinMain(ptr long ptr long) WinMain\n"
97 static const char *app_cui_spec
=
98 "@ stdcall main(long ptr ptr) main\n"
101 static const char *wrapper_code
=
103 " * Copyright 2000 Francois Gouget <fgouget@codeweavers.com> for CodeWeavers\n"
104 " * Copyright 2002 Dimitrie O. Paun <dpaun@rogers.com>\n"
107 "#include <dlfcn.h>\n"
108 "#include <windows.h>\n"
112 " * Describe the wrapped application\n"
115 "/* The app name */\n"
116 "#define APPNAME \"%s\"\n"
118 " * This is either 0 for a console based application or\n"
119 " * 1 for a regular windows application.\n"
121 "#define GUIEXE %d\n"
124 " * This is the name of the library containing the application,\n"
125 " * e.g. 'hello.dll' if the application is called 'hello.exe'.\n"
127 "static char* appName = APPNAME \".dll\";\n"
130 " * This is the name of the application's Windows module. If left NULL\n"
131 " * then appName is used.\n"
133 "static char* appModule = NULL;\n"
136 " * This is the application's entry point. This is usually 'WinMain' for a\n"
137 " * gui app and 'main' for a console application.\n"
140 "static char* appInit = \"WinMain\";\n"
142 "static char* appInit = \"main\";\n"
146 " * This is either non-NULL for MFC-based applications and is the name of the\n"
147 " * MFC's module. This is the module in which we will take the 'WinMain'\n"
150 "static char* mfcModule = NULL;\n"
153 "void error(const char *format, ...)\n"
158 " va_start(ap, format);\n"
159 " vsnprintf(msg, sizeof(msg), format, ap);\n"
160 " MessageBox(NULL, msg, \"Error\", MB_OK);\n"
167 "typedef int WINAPI (*WinMainFunc)(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
168 " PSTR szCmdLine, int iCmdShow);\n"
170 "typedef int WINAPI (*MainFunc)(int argc, char** argv, char** envp);\n"
174 "int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
175 " PSTR szCmdLine, int iCmdShow)\n"
177 "int WINAPI main(int argc, char** argv, char** envp)\n"
180 " void* appLibrary;\n"
181 " HINSTANCE hApp = 0, hMFC = 0, hMain = 0;\n"
186 " /* Load the application's library */\n"
187 " libName=(char*)malloc(2+strlen(appName)+3+1);\n"
188 " /* FIXME: we should get the wrapper's path and use that as the base for\n"
191 " sprintf(libName,\"./%%s.so\",appName);\n"
192 " appLibrary=dlopen(libName,RTLD_NOW);\n"
193 " if (appLibrary==NULL) {\n"
194 " sprintf(libName,\"%%s.so\",appName);\n"
195 " appLibrary=dlopen(libName,RTLD_NOW);\n"
197 " if (!appLibrary) error(\"Could not load the %%s library: %%s\", libName, dlerror());\n"
199 " /* Then if this application is MFC based, load the MFC module */\n"
200 " /* FIXME: I'm not sure this is really necessary */\n"
201 " if (mfcModule) {\n"
202 " hMFC = LoadLibrary(mfcModule);\n"
203 " if (!hMFC) error(\"Could not load the MFC module %%s (%%d)\", mfcModule, GetLastError());\n"
204 " /* MFC is a special case: the WinMain is in the MFC library,\n"
205 " * instead of the application's library.\n"
210 " /* Load the application's module */\n"
211 " if (!appModule) appModule = appName;\n"
212 " hApp = LoadLibrary(appModule);\n"
213 " if (!hApp) error(\"Could not load the application's module %%s (%%d)\", appModule, GetLastError());\n"
214 " if (!hMain) hMain = hApp;\n"
216 " /* Get the address of the application's entry point */\n"
217 " appMain = GetProcAddress(hMain, appInit);\n"
218 " if (!appMain) error(\"Could not get the address of %%s (%%d)\", appInit, GetLastError());\n"
220 " /* And finally invoke the application's entry point */\n"
222 " retcode = (*((WinMainFunc)appMain))(hApp, hPrevInstance, szCmdLine, iCmdShow);\n"
224 " retcode = (*((MainFunc)appMain))(argc, argv, envp);\n"
227 " /* Cleanup and done */\n"
228 " FreeLibrary(hApp);\n"
229 " FreeLibrary(hMFC);\n"
230 " dlclose(appLibrary);\n"
237 static char *output_name
;
238 static char **arh_files
, **dll_files
, **lib_files
, **lib_paths
, **obj_files
;
239 static int nb_arh_files
, nb_dll_files
, nb_lib_files
, nb_lib_paths
, nb_obj_files
;
240 static int verbose
= 0;
241 static int keep_generated
= 0;
243 void error(const char *s
, ...)
248 fprintf(stderr
, "Error: ");
249 vfprintf(stderr
, s
, ap
);
250 fprintf(stderr
, "\n");
255 char *strmake(const char *fmt
, ...)
261 if ((p
= malloc (size
)) == NULL
)
262 error("Can not malloc %d bytes.", size
);
267 n
= vsnprintf (p
, size
, fmt
, ap
);
269 if (n
> -1 && n
< size
) return p
;
271 if ((p
= realloc (p
, size
)) == NULL
)
272 error("Can not realloc %d bytes.", size
);
276 void rm_temp_file(const char *file
)
278 if (!keep_generated
) unlink(file
);
281 void create_file(const char *name
, const char *fmt
, ...)
286 if (verbose
) printf("Creating file %s\n", name
);
288 if ( !(file
= fopen(name
, "w")) )
289 error ("Can not create %s.", name
);
290 vfprintf(file
, fmt
, ap
);
295 void spawn(char *const argv
[])
297 int pid
, status
, wret
, i
;
301 for(i
= 0; argv
[i
]; i
++) printf("%s ", argv
[i
]);
305 if ((pid
= fork()) == 0) execvp(argv
[0], argv
);
308 while (pid
!= (wret
= waitpid(pid
, &status
, 0)))
309 if (wret
== -1 && errno
!= EINTR
) break;
311 if (pid
== wret
&& WIFEXITED(status
) && WEXITSTATUS(status
) == 0) return;
312 error("%s failed.", argv
[0]);
318 int is_resource(const char* file
)
320 /* see tools/winebuild/res32.c: check_header for details */
321 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 };
322 char buf
[sizeof(res_sig
)];
325 if (!(fin
= fopen(file
, "r"))) error("Can not open %s", file
);
326 if (fread(buf
, sizeof(buf
), 1, fin
) != 1) error("Truncated file %s", file
);
329 return !memcmp(buf
, res_sig
, sizeof(buf
));
332 /* open the file for a given name, in a specified path, with the given extension */
333 static char *try_path( const char *path
, const char *name
, const char *ext
)
338 fullname
= strmake("%s/lib%s.%s", path
, name
, ext
);
339 if (verbose
> 1) fprintf(stderr
, "Try %s...", fullname
);
341 /* check if the file exists */
342 if ((fd
= open( fullname
, O_RDONLY
)) != -1)
345 if (verbose
> 1) fprintf(stderr
, "FOUND!\n");
349 if (verbose
> 1) fprintf(stderr
, "\n");
353 /* open the .def library for a given dll in a specified path */
354 static char *try_dll_path( const char *path
, const char *name
)
356 return try_path(path
, name
, "def");
359 /* open the .a library for a given lib in a specified path */
360 static char *try_lib_path( const char *path
, const char *name
)
362 return try_path(path
, name
, "a");
365 /* open the .def library for a given dll */
366 static char *open_dll(const char *name
)
371 for (i
= 0; i
< nb_lib_paths
; i
++)
373 if ((fullname
= try_dll_path( lib_paths
[i
], name
))) return fullname
;
375 return try_dll_path( ".", name
);
378 /* find a static library */
379 static char *find_lib(const char *name
)
381 static const char* std_paths
[] = { ".", "/usr/lib", "/usr/local/lib" };
385 for (i
= 0; i
< nb_lib_paths
; i
++)
387 if ((fullname
= try_lib_path( lib_paths
[i
], name
))) return fullname
;
390 for (i
= 0; i
< sizeof(std_paths
)/sizeof(std_paths
[0]); i
++)
392 if ((fullname
= try_lib_path( std_paths
[i
], name
))) return fullname
;
398 void add_lib_path(const char* path
)
400 lib_paths
= realloc( lib_paths
, (nb_lib_paths
+1) * sizeof(*lib_paths
) );
401 lib_paths
[nb_lib_paths
++] = strdup(path
);
402 dll_files
= realloc( dll_files
, (nb_dll_files
+1) * sizeof(*dll_files
) );
403 dll_files
[nb_dll_files
++] = strmake("-L%s", path
);
404 lib_files
= realloc( lib_files
, (nb_lib_files
+1) * sizeof(*lib_files
) );
405 lib_files
[nb_lib_files
++] = strmake("-L%s", path
);
408 void add_lib_file(const char* library
)
412 if (open_dll(library
))
414 dll_files
= realloc( dll_files
, (nb_dll_files
+1) * sizeof(*dll_files
) );
415 dll_files
[nb_dll_files
++] = strmake("-l%s", library
);
417 else if ((lib
= find_lib(library
)))
419 arh_files
= realloc( arh_files
, (nb_arh_files
+1) * sizeof(*arh_files
) );
420 arh_files
[nb_arh_files
++] = lib
;
424 lib_files
= realloc( lib_files
, (nb_lib_files
+1) * sizeof(*lib_files
) );
425 lib_files
[nb_lib_files
++] = strmake("-l%s", library
);
429 int main(int argc
, char **argv
)
431 char *library
= 0, *path
= 0;
432 int i
, j
, len
, cpp
= 0, no_opt
= 0, gui_mode
= 0, create_wrapper
= -1;
433 char *base_name
, *base_file
, *app_temp_name
, *wrp_temp_name
;
434 char *spec_name
, *spec_c_name
, *spec_o_name
;
435 char *wspec_name
, *wspec_c_name
, *wspec_o_name
;
436 char *wrap_c_name
, *wrap_o_name
;
437 char **spec_args
, **comp_args
, **link_args
;
438 char **wwrap_args
, **wspec_args
, **wcomp_args
, **wlink_args
;
440 /* include the standard DLL path first */
441 add_lib_path(WINEDLLS
);
443 for (i
= 1; i
< argc
; i
++)
445 if (!no_opt
&& argv
[i
][0] == '-')
453 if (argv
[i
][2]) output_name
= strdup(argv
[i
]+ 2);
454 else if (i
+ 1 < argc
) output_name
= strdup(argv
[++i
]);
455 else error("The -o switch takes an argument.");
458 if (argv
[i
][2]) path
= argv
[i
] + 2;
459 else if (i
+ 1 < argc
) path
= argv
[++i
];
460 else error("The -L switch takes an argument\n.");
464 if (argv
[i
][2]) library
= argv
[i
] + 2;
465 else if (i
+ 1 < argc
) library
= argv
[++i
];
466 else error("The -l switch takes an argument\n.");
467 add_lib_file(library
);
470 if (strcmp("-mwindows", argv
[i
]) == 0) gui_mode
= 1;
471 else if (strcmp("-mgui", argv
[i
]) == 0) gui_mode
= 1;
472 else error("Unknown option %s\n", argv
[i
]);
474 case 'v': /* verbose */
475 if (argv
[i
][2] == 0) verbose
++;
478 printf("winewrap v0.40\n");
491 if (argv
[i
][2]) error("No long option supported.");
495 error("Unknown option -%c", argv
[i
][1]);
500 /* it's a filename, add it to its list */
501 obj_files
= realloc( obj_files
, (nb_obj_files
+1) * sizeof(*obj_files
) );
502 obj_files
[nb_obj_files
++] = strdup(argv
[i
]);
505 /* create wrapper only in C++ by default */
506 if (create_wrapper
== -1) create_wrapper
= cpp
;
508 /* link in by default the big three */
509 add_lib_file("user32");
510 add_lib_file("gdi32");
511 add_lib_file("kernel32");
513 app_temp_name
= tempnam(0, "wapp");
514 wrp_temp_name
= tempnam(0, "wwrp");
516 /* get base filename by removing the .exe extension, if present */
517 base_file
= strdup(output_name
);
518 len
= strlen(base_file
);
519 if (len
> 4 && strcmp(base_file
+ len
- 4, ".exe") == 0)
520 base_file
[len
- 4 ] = 0; /* remove the .exe extension */
522 /* we need to get rid of the directory part to get the base name */
523 if ( (base_name
= strrchr(base_file
, '/')) ) base_name
++;
524 else base_name
= base_file
;
526 spec_name
= strmake("%s.spec", app_temp_name
);
527 spec_c_name
= strmake("%s.c", spec_name
);
528 spec_o_name
= strmake("%s.o", spec_name
);
530 wspec_name
= strmake("%s.spec", wrp_temp_name
);
531 wspec_c_name
= strmake("%s.c", wspec_name
);
532 wspec_o_name
= strmake("%s.o", wspec_name
);
534 wrap_c_name
= strmake("%s.c", wrp_temp_name
);
535 wrap_o_name
= strmake("%s.o", wrp_temp_name
);
537 /* build winebuild's argument list */
538 spec_args
= malloc( (nb_arh_files
+ nb_dll_files
+ nb_obj_files
+ 20) * sizeof (char *) );
540 spec_args
[j
++] = BINDIR
"/winebuild";
541 spec_args
[j
++] = "-o";
542 spec_args
[j
++] = spec_c_name
;
545 spec_args
[j
++] = "-F";
546 spec_args
[j
++] = strmake("%s.dll", base_name
);
547 spec_args
[j
++] = "--spec";
548 spec_args
[j
++] = spec_name
;
552 spec_args
[j
++] = "--exe";
553 spec_args
[j
++] = strmake("%s.exe", base_name
);
554 spec_args
[j
++] = gui_mode
? "-mgui" : "-mcui";
556 for (i
= 0; i
< nb_dll_files
; i
++)
557 spec_args
[j
++] = dll_files
[i
];
558 for (i
= 0; i
< nb_obj_files
; i
++)
559 spec_args
[j
++] = obj_files
[i
];
560 for (i
= 0; i
< nb_arh_files
; i
++)
561 spec_args
[j
++] = arh_files
[i
];
564 /* build gcc's argument list */
565 comp_args
= malloc ( 20 * sizeof (char *) );
567 comp_args
[j
++] = "gcc";
568 comp_args
[j
++] = "-fPIC";
569 comp_args
[j
++] = "-o";
570 comp_args
[j
++] = spec_o_name
;
571 comp_args
[j
++] = "-c";
572 comp_args
[j
++] = spec_c_name
;
575 /* build ld's argument list */
576 link_args
= malloc( (nb_arh_files
+ nb_obj_files
+ nb_lib_files
+ 20) * sizeof (char *) );
578 link_args
[j
++] = cpp
? "g++" : "gcc";
579 link_args
[j
++] = "-shared";
580 link_args
[j
++] = "-Wl,-Bsymbolic,-z,defs";
581 link_args
[j
++] = "-lwine";
582 link_args
[j
++] = "-lm";
583 for (i
= 0; i
< nb_lib_files
; i
++)
584 link_args
[j
++] = lib_files
[i
];
585 link_args
[j
++] = "-o";
586 link_args
[j
++] = strmake("%s.%s.so", base_file
, create_wrapper
? "dll" : "exe");
587 link_args
[j
++] = spec_o_name
;
588 for (i
= 0; i
< nb_obj_files
; i
++)
589 if (!is_resource(obj_files
[i
])) link_args
[j
++] = obj_files
[i
];
590 for (i
= 0; i
< nb_arh_files
; i
++)
591 link_args
[j
++] = arh_files
[i
];
594 /* build wrapper compile argument list */
595 wwrap_args
= malloc ( 20 * sizeof (char *) );
597 wwrap_args
[j
++] = "gcc";
598 wwrap_args
[j
++] = "-fPIC";
599 wwrap_args
[j
++] = "-I" INCLUDEDIR
"/windows";
600 wwrap_args
[j
++] = "-o";
601 wwrap_args
[j
++] = wrap_o_name
;
602 wwrap_args
[j
++] = "-c";
603 wwrap_args
[j
++] = wrap_c_name
;
606 /* build wrapper winebuild's argument list */
607 wspec_args
= malloc( (nb_dll_files
+ 20) * sizeof (char *) );
609 wspec_args
[j
++] = BINDIR
"/winebuild";
610 wspec_args
[j
++] = "-o";
611 wspec_args
[j
++] = wspec_c_name
;
612 wspec_args
[j
++] = "--exe";
613 wspec_args
[j
++] = strmake("%s.exe", base_name
);
614 wspec_args
[j
++] = gui_mode
? "-mgui" : "-mcui";
615 wspec_args
[j
++] = wrap_o_name
;
616 for (i
= 0; i
< nb_dll_files
; i
++)
617 wspec_args
[j
++] = dll_files
[i
];
620 /* build wrapper gcc's argument list */
621 wcomp_args
= malloc ( 20 * sizeof (char *) );
623 wcomp_args
[j
++] = "gcc";
624 wcomp_args
[j
++] = "-fPIC";
625 wcomp_args
[j
++] = "-o";
626 wcomp_args
[j
++] = wspec_o_name
;
627 wcomp_args
[j
++] = "-c";
628 wcomp_args
[j
++] = wspec_c_name
;
631 /* build wrapper ld's argument list */
632 wlink_args
= malloc( 20 * sizeof (char *) );
634 wlink_args
[j
++] = cpp
? "g++" : "gcc";
635 wlink_args
[j
++] = "-shared";
636 wlink_args
[j
++] = "-Wl,-Bsymbolic,-z,defs";
637 wlink_args
[j
++] = "-lwine";
638 wlink_args
[j
++] = "-ldl";
639 wlink_args
[j
++] = "-o";
640 wlink_args
[j
++] = strmake("%s.exe.so", base_file
);
641 wlink_args
[j
++] = wspec_o_name
;
642 wlink_args
[j
++] = wrap_o_name
;
645 /* run winebuild to get the .spec.c file */
648 create_file(spec_name
, gui_mode
? app_gui_spec
: app_cui_spec
);
650 rm_temp_file(spec_name
);
652 rm_temp_file(spec_c_name
);
654 rm_temp_file(spec_o_name
);
656 create_file(wrap_c_name
, wrapper_code
, base_name
, gui_mode
);
658 rm_temp_file(wrap_c_name
);
661 rm_temp_file(wspec_c_name
);
663 rm_temp_file(wspec_o_name
);
664 rm_temp_file(wrap_o_name
);
670 rm_temp_file(spec_c_name
);
672 rm_temp_file(spec_o_name
);
675 /* create the loader script */
676 create_file(base_file
, app_loader_script
, base_name
);
677 chmod(base_file
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);