If supported, add -init and -fini flags to the linker invocation.
[wine/multimedia.git] / tools / winegcc / winewrap.c
blob58712fb6042aaf3d22c2abeb1462aacc029981c1
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 #include "utils.h"
34 static const char *app_loader_script =
35 "#!/bin/sh\n"
36 "\n"
37 "appname=\"%s\"\n"
38 "# determine the application directory\n"
39 "appdir=''\n"
40 "case \"$0\" in\n"
41 " */*)\n"
42 " # $0 contains a path, use it\n"
43 " appdir=`dirname \"$0\"`\n"
44 " ;;\n"
45 " *)\n"
46 " # no directory in $0, search in PATH\n"
47 " saved_ifs=$IFS\n"
48 " IFS=:\n"
49 " for d in $PATH\n"
50 " do\n"
51 " IFS=$saved_ifs\n"
52 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
53 " done\n"
54 " ;;\n"
55 "esac\n"
56 "\n"
57 "while true; do\n"
58 " case \"$1\" in\n"
59 " --debugmsg)\n"
60 " debugmsg=\"$1 $2\"\n"
61 " shift; shift;\n"
62 " ;;\n"
63 " --dll)\n"
64 " dll=\"$1 $2\"\n"
65 " shift; shift;\n"
66 " ;;\n"
67 " *)\n"
68 " break\n"
69 " ;;\n"
70 " esac\n"
71 "done\n"
72 "\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"
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 <stdio.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-wrap.dll' if the application is called 'hello.exe'.\n"
122 " */\n"
123 "static char* appName = \"%s\";\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 " fprintf(stderr, \"Error: %%s\\n\", msg);\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 " HINSTANCE hApp = 0, hMFC = 0, hMain = 0;\n"
177 " void* appMain;\n"
178 " int retcode, i;\n"
179 " const char* libs[] = { %s };\n"
180 "\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"
186 " hMain = hMFC;\n"
187 " }\n"
188 "\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"
191 " }\n"
192 "\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"
198 "\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"
202 "\n"
203 " /* And finally invoke the application's entry point */\n"
204 "#if GUIEXE\n"
205 " retcode = (*((WinMainFunc)appMain))(hApp, hPrevInstance, szCmdLine, iCmdShow);\n"
206 "#else\n"
207 " retcode = (*((MainFunc)appMain))(argc, argv, envp);\n"
208 "#endif\n"
209 "\n"
210 " /* Cleanup and done */\n"
211 " FreeLibrary(hApp);\n"
212 " FreeLibrary(hMFC);\n"
213 "\n"
214 " return retcode;\n"
215 "}\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, ...)
229 va_list ap;
230 FILE *file;
232 if (verbose) printf("Creating file %s\n", name);
233 va_start(ap, fmt);
234 if ( !(file = fopen(name, "w")) )
235 error ("Can not create %s.", name);
236 vfprintf(file, fmt, ap);
237 va_end(ap);
238 fclose(file);
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)];
246 FILE *fin;
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);
250 fclose(fin);
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 )
258 char *fullname;
259 int fd;
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)
267 close( fd );
268 if (verbose > 1) fprintf(stderr, "FOUND!\n");
269 return fullname;
271 free( fullname );
272 if (verbose > 1) fprintf(stderr, "\n");
273 return NULL;
277 static char* find_in_path(const strarray* path, const char* name, const char* ext)
279 int i;
280 for (i = 0; i < path->size; i++)
282 char* fullname;
283 if ((fullname = try_path( path->base[i], name, ext )))
284 return fullname;
286 return NULL;
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)
298 char* fullname;
299 static strarray *std_paths;
300 if (!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 )))
308 return fullname;
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)
321 char *lib;
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))
330 /* Windows DLL */
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);
338 else
340 fprintf(stderr, "cannot find %s.{so,def,a} in library search path\n",
341 library);
345 static void identify_lib_files(strarray *lib_files)
347 int i;
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;
361 int i;
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);
385 spawn(wwrap_args);
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);
403 spawn(wspec_args);
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);
416 spawn(wcomp_args);
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);
434 spawn(wlink_args);
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;
447 strarray *lib_files;
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] == '-')
461 switch (argv[i][1])
463 case 'a':
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.");
467 break;
468 case 'k':
469 keep_generated = 1;
470 break;
471 case 'o':
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.");
475 break;
476 case 'L':
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.");
480 add_lib_path(path);
481 break;
482 case 'l':
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);
487 break;
488 case 'm':
489 if (strcmp("-mgui", argv[i]) == 0) gui_mode = 1;
490 else error("Unknown option %s\n", argv[i]);
491 break;
492 case 'v': /* verbose */
493 if (argv[i][2] == 0) verbose++;
494 break;
495 case 'V':
496 printf("winewrap v0.40\n");
497 exit(0);
498 break;
499 case 'C':
500 cpp = 1;
501 break;
502 case 'w':
503 create_wrapper = 1;
504 break;
505 case 'W':
506 create_wrapper = 0;
507 break;
508 case '-':
509 if (argv[i][2]) error("No long option supported.");
510 no_opt = 1;
511 break;
512 default:
513 error("Unknown option -%c", argv[i][1]);
515 continue;
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 */
530 if (gui_mode)
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;
551 base_name++;
553 else
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);
571 if (create_wrapper)
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);
579 else
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 */
596 spawn(spec_args);
597 strarray_free(spec_args);
599 if (create_wrapper)
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);
612 spawn(comp_args);
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");
622 #else
623 strarray_add(link_args, "-Wl,-Bsymbolic,-z,defs");
624 #endif
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");
634 if (create_wrapper)
635 strarray_add(link_args, strmake("%s/%s.so", base_path, app_name));
636 else
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);
647 spawn(link_args);
648 strarray_free(link_args);
649 rm_temp_file(spec_o_name);
651 if (create_wrapper)
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);
658 return 0;