Only reapply the texture states necessary when a different texture
[wine/dcerpc.git] / tools / winewrap.c
blob4ee448d2070522980f570eae10e158e44852e2df
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 #ifndef WINEDLLS
33 #define WINEDLLS "/usr/local/lib/wine"
34 #endif
36 static const char *app_loader_script =
37 "#!/bin/sh\n"
38 "\n"
39 "appname=\"%s\"\n"
40 "# determine the application directory\n"
41 "appdir=''\n"
42 "case \"$0\" in\n"
43 " */*)\n"
44 " # $0 contains a path, use it\n"
45 " appdir=`dirname \"$0\"`\n"
46 " ;;\n"
47 " *)\n"
48 " # no directory in $0, search in PATH\n"
49 " saved_ifs=$IFS\n"
50 " IFS=:\n"
51 " for d in $PATH\n"
52 " do\n"
53 " IFS=$saved_ifs\n"
54 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
55 " done\n"
56 " ;;\n"
57 "esac\n"
58 "\n"
59 "while true; do\n"
60 " case \"$1\" in\n"
61 " --debugmsg)\n"
62 " debugmsg=\"$1 $2\"\n"
63 " shift; shift;\n"
64 " ;;\n"
65 " --dll)\n"
66 " dll=\"$1 $2\"\n"
67 " shift; shift;\n"
68 " ;;\n"
69 " *)\n"
70 " break\n"
71 " ;;\n"
72 " esac\n"
73 "done\n"
74 "\n"
75 "# figure out the full app path\n"
76 "if [ -n \"$appdir\" ]; then\n"
77 " apppath=\"$appdir/$appname.exe.so\"\n"
78 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
79 " export WINEDLLPATH\n"
80 "else\n"
81 " apppath=\"$appname.exe.so\"\n"
82 "fi\n"
83 "\n"
84 "# determine the WINELOADER\n"
85 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
86 "\n"
87 "# and try to start the app\n"
88 "exec \"$WINELOADER\" $debugmsg $dll -- \"$apppath\" \"$@\"\n"
91 static const char *app_gui_spec =
92 "@ stdcall WinMain(ptr long ptr long) WinMain\n"
95 static const char *app_cui_spec =
96 "@ stdcall main(long ptr ptr) main\n"
99 static const char *wrapper_code =
100 "/*\n"
101 " * Copyright 2000 Francois Gouget <fgouget@codeweavers.com> for CodeWeavers\n"
102 " * Copyright 2002 Dimitrie O. Paun <dpaun@rogers.com>\n"
103 " */\n"
104 "\n"
105 "#include <dlfcn.h>\n"
106 "#include <windows.h>\n"
107 "\n"
108 "\n"
109 "/*\n"
110 " * Describe the wrapped application\n"
111 " */\n"
112 "\n"
113 "/* The app name */\n"
114 "#define APPNAME \"%s\"\n"
115 "/**\n"
116 " * This is either 0 for a console based application or\n"
117 " * 1 for a regular windows application.\n"
118 " */\n"
119 "#define GUIEXE %d\n"
120 "\n"
121 "/**\n"
122 " * This is the name of the library containing the application,\n"
123 " * e.g. 'hello-wrap.dll' if the application is called 'hello.exe'.\n"
124 " */\n"
125 "static char* appName = APPNAME \"-wrap.dll\";\n"
126 "\n"
127 "/**\n"
128 " * This is the name of the application's Windows module. If left NULL\n"
129 " * then appName is used.\n"
130 " */\n"
131 "static char* appModule = NULL;\n"
132 "\n"
133 "/**\n"
134 " * This is the application's entry point. This is usually 'WinMain' for a\n"
135 " * gui app and 'main' for a console application.\n"
136 " */\n"
137 "#if GUIEXE\n"
138 "static char* appInit = \"WinMain\";\n"
139 "#else\n"
140 "static char* appInit = \"main\";\n"
141 "#endif\n"
142 "\n"
143 "/**\n"
144 " * This is either non-NULL for MFC-based applications and is the name of the\n"
145 " * MFC's module. This is the module in which we will take the 'WinMain'\n"
146 " * function.\n"
147 " */\n"
148 "static char* mfcModule = NULL;\n"
149 "\n"
150 "\n"
151 "void error(const char *format, ...)\n"
152 "{\n"
153 " va_list ap;\n"
154 " char msg[4096];\n"
155 "\n"
156 " va_start(ap, format);\n"
157 " vsnprintf(msg, sizeof(msg), format, ap);\n"
158 " MessageBox(NULL, msg, \"Error\", MB_OK);\n"
159 " va_end(ap);\n"
160 " exit(1);\n"
161 "}\n"
162 "\n"
163 "\n"
164 "#if GUIEXE\n"
165 "typedef int WINAPI (*WinMainFunc)(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
166 " PSTR szCmdLine, int iCmdShow);\n"
167 "#else\n"
168 "typedef int WINAPI (*MainFunc)(int argc, char** argv, char** envp);\n"
169 "#endif\n"
170 "\n"
171 "#if GUIEXE\n"
172 "int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
173 " PSTR szCmdLine, int iCmdShow)\n"
174 "#else\n"
175 "int WINAPI main(int argc, char** argv, char** envp)\n"
176 "#endif\n"
177 "{\n"
178 " HINSTANCE hApp = 0, hMFC = 0, hMain = 0;\n"
179 " void* appMain;\n"
180 " int retcode;\n"
181 "\n"
182 " /* Then if this application is MFC based, load the MFC module */\n"
183 " if (mfcModule) {\n"
184 " hMFC = LoadLibrary(mfcModule);\n"
185 " if (!hMFC) error(\"Could not load the MFC module %%s (%%d)\", mfcModule, GetLastError());\n"
186 " /* For MFC apps, WinMain is in the MFC library */\n"
187 " hMain = hMFC;\n"
188 " }\n"
189 "\n"
190 " /* Load the application's module */\n"
191 " if (!appModule) appModule = appName;\n"
192 " hApp = LoadLibrary(appModule);\n"
193 " if (!hApp) error(\"Could not load the application's module %%s (%%d)\", appModule, GetLastError());\n"
194 " if (!hMain) hMain = hApp;\n"
195 "\n"
196 " /* Get the address of the application's entry point */\n"
197 " appMain = GetProcAddress(hMain, appInit);\n"
198 " if (!appMain) error(\"Could not get the address of %%s (%%d)\", appInit, GetLastError());\n"
199 "\n"
200 " /* And finally invoke the application's entry point */\n"
201 "#if GUIEXE\n"
202 " retcode = (*((WinMainFunc)appMain))(hApp, hPrevInstance, szCmdLine, iCmdShow);\n"
203 "#else\n"
204 " retcode = (*((MainFunc)appMain))(argc, argv, envp);\n"
205 "#endif\n"
206 "\n"
207 " /* Cleanup and done */\n"
208 " FreeLibrary(hApp);\n"
209 " FreeLibrary(hMFC);\n"
210 "\n"
211 " return retcode;\n"
212 "}\n"
215 static char *output_name;
216 static char **arh_files, **dll_files, **lib_files, **lib_paths, **obj_files;
217 static int nb_arh_files, nb_dll_files, nb_lib_files, nb_lib_paths, nb_obj_files;
218 static int verbose = 0;
219 static int keep_generated = 0;
221 void error(const char *s, ...)
223 va_list ap;
225 va_start(ap, s);
226 fprintf(stderr, "Error: ");
227 vfprintf(stderr, s, ap);
228 fprintf(stderr, "\n");
229 va_end(ap);
230 exit(2);
233 char *strmake(const char *fmt, ...)
235 int n, size = 100;
236 char *p;
237 va_list ap;
239 if ((p = malloc (size)) == NULL)
240 error("Can not malloc %d bytes.", size);
242 while (1)
244 va_start(ap, fmt);
245 n = vsnprintf (p, size, fmt, ap);
246 va_end(ap);
247 if (n > -1 && n < size) return p;
248 size *= 2;
249 if ((p = realloc (p, size)) == NULL)
250 error("Can not realloc %d bytes.", size);
254 void rm_temp_file(const char *file)
256 if (!keep_generated) unlink(file);
259 void create_file(const char *name, const char *fmt, ...)
261 va_list ap;
262 FILE *file;
264 if (verbose) printf("Creating file %s\n", name);
265 va_start(ap, fmt);
266 if ( !(file = fopen(name, "w")) )
267 error ("Can not create %s.", name);
268 vfprintf(file, fmt, ap);
269 va_end(ap);
270 fclose(file);
273 void spawn(char *const argv[])
275 int i, status;
277 if (verbose)
279 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
280 printf("\n");
282 if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
284 if (status > 0) error("%s failed.", argv[0]);
285 else perror("Error:");
286 exit(3);
289 int is_resource(const char* file)
291 /* see tools/winebuild/res32.c: check_header for details */
292 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 };
293 char buf[sizeof(res_sig)];
294 FILE *fin;
296 if (!(fin = fopen(file, "r"))) error("Can not open %s", file);
297 if (fread(buf, sizeof(buf), 1, fin) != 1) error("Truncated file %s", file);
298 fclose(fin);
300 return !memcmp(buf, res_sig, sizeof(buf));
303 /* open the file for a given name, in a specified path, with the given extension */
304 static char *try_path( const char *path, const char *name, const char *ext )
306 char *fullname;
307 int fd;
309 fullname = strmake("%s/lib%s.%s", path, name, ext);
310 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
312 /* check if the file exists */
313 if ((fd = open( fullname, O_RDONLY )) != -1)
315 close( fd );
316 if (verbose > 1) fprintf(stderr, "FOUND!\n");
317 return fullname;
319 free( fullname );
320 if (verbose > 1) fprintf(stderr, "\n");
321 return NULL;
324 /* open the .def library for a given dll in a specified path */
325 static char *try_dll_path( const char *path, const char *name )
327 return try_path(path, name, "def");
330 /* open the .a library for a given lib in a specified path */
331 static char *try_lib_path( const char *path, const char *name )
333 return try_path(path, name, "a");
336 /* open the .def library for a given dll */
337 static char *open_dll(const char *name)
339 char *fullname;
340 int i;
342 for (i = 0; i < nb_lib_paths; i++)
344 if ((fullname = try_dll_path( lib_paths[i], name ))) return fullname;
346 return try_dll_path( ".", name );
349 /* find a static library */
350 static char *find_lib(const char *name)
352 static const char* std_paths[] = { ".", "/usr/lib", "/usr/local/lib" };
353 char *fullname;
354 int i;
356 for (i = 0; i < nb_lib_paths; i++)
358 if ((fullname = try_lib_path( lib_paths[i], name ))) return fullname;
361 for (i = 0; i < sizeof(std_paths)/sizeof(std_paths[0]); i++)
363 if ((fullname = try_lib_path( std_paths[i], name ))) return fullname;
366 return 0;
369 void add_lib_path(const char* path)
371 lib_paths = realloc( lib_paths, (nb_lib_paths+1) * sizeof(*lib_paths) );
372 lib_paths[nb_lib_paths++] = strdup(path);
373 dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
374 dll_files[nb_dll_files++] = strmake("-L%s", path);
375 lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
376 lib_files[nb_lib_files++] = strmake("-L%s", path);
379 void add_lib_file(const char* library)
381 char *lib;
383 if (open_dll(library))
385 dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
386 dll_files[nb_dll_files++] = strmake("-l%s", library);
388 else if ((lib = find_lib(library)))
390 arh_files = realloc( arh_files, (nb_arh_files+1) * sizeof(*arh_files) );
391 arh_files[nb_arh_files++] = lib;
393 else
395 lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
396 lib_files[nb_lib_files++] = strmake("-l%s", library);
400 int main(int argc, char **argv)
402 char *library = 0, *path = 0;
403 int i, j, len, cpp = 0, no_opt = 0, gui_mode = 0, create_wrapper = -1;
404 char *base_name, *base_file, *app_temp_name, *wrp_temp_name;
405 char *spec_name, *spec_c_name, *spec_o_name;
406 char *wspec_name, *wspec_c_name, *wspec_o_name;
407 char *wrap_c_name, *wrap_o_name;
408 char **spec_args, **comp_args, **link_args;
409 char **wwrap_args, **wspec_args, **wcomp_args, **wlink_args;
411 /* include the standard DLL path first */
412 add_lib_path(WINEDLLS);
414 for (i = 1; i < argc; i++)
416 if (!no_opt && argv[i][0] == '-')
418 switch (argv[i][1])
420 case 'k':
421 keep_generated = 1;
422 break;
423 case 'o':
424 if (argv[i][2]) output_name = strdup(argv[i]+ 2);
425 else if (i + 1 < argc) output_name = strdup(argv[++i]);
426 else error("The -o switch takes an argument.");
427 break;
428 case 'L':
429 if (argv[i][2]) path = argv[i] + 2;
430 else if (i + 1 < argc) path = argv[++i];
431 else error("The -L switch takes an argument\n.");
432 add_lib_path(path);
433 break;
434 case 'l':
435 if (argv[i][2]) library = argv[i] + 2;
436 else if (i + 1 < argc) library = argv[++i];
437 else error("The -l switch takes an argument\n.");
438 add_lib_file(library);
439 break;
440 case 'm':
441 if (strcmp("-mgui", argv[i]) == 0) gui_mode = 1;
442 else error("Unknown option %s\n", argv[i]);
443 break;
444 case 'v': /* verbose */
445 if (argv[i][2] == 0) verbose++;
446 break;
447 case 'V':
448 printf("winewrap v0.40\n");
449 exit(0);
450 break;
451 case 'C':
452 cpp = 1;
453 break;
454 case 'w':
455 create_wrapper = 1;
456 break;
457 case 'W':
458 create_wrapper = 0;
459 break;
460 case '-':
461 if (argv[i][2]) error("No long option supported.");
462 no_opt = 1;
463 break;
464 default:
465 error("Unknown option -%c", argv[i][1]);
467 continue;
470 /* it's a filename, add it to its list */
471 obj_files = realloc( obj_files, (nb_obj_files+1) * sizeof(*obj_files) );
472 obj_files[nb_obj_files++] = strdup(argv[i]);
475 /* create wrapper only in C++ by default */
476 if (create_wrapper == -1) create_wrapper = cpp;
478 /* link in by default the big three */
479 if (gui_mode) add_lib_file("gdi32");
480 add_lib_file("user32");
481 add_lib_file("kernel32");
483 app_temp_name = tempnam(0, "wapp");
484 wrp_temp_name = tempnam(0, "wwrp");
486 /* get base filename by removing the .exe extension, if present */
487 base_file = strdup(output_name);
488 len = strlen(base_file);
489 if (len > 4 && strcmp(base_file + len - 4, ".exe") == 0)
490 base_file[len - 4 ] = 0; /* remove the .exe extension */
492 /* we need to get rid of the directory part to get the base name */
493 if ( (base_name = strrchr(base_file, '/')) ) base_name++;
494 else base_name = base_file;
496 spec_name = strmake("%s.spec", app_temp_name);
497 spec_c_name = strmake("%s.c", spec_name);
498 spec_o_name = strmake("%s.o", spec_name);
500 wspec_name = strmake("%s.spec", wrp_temp_name);
501 wspec_c_name = strmake("%s.c", wspec_name);
502 wspec_o_name = strmake("%s.o", wspec_name);
504 wrap_c_name = strmake("%s.c", wrp_temp_name);
505 wrap_o_name = strmake("%s.o", wrp_temp_name);
507 /* build winebuild's argument list */
508 spec_args = malloc( (nb_arh_files + nb_dll_files + nb_obj_files + 20) * sizeof (char *) );
509 j = 0;
510 spec_args[j++] = BINDIR "/winebuild";
511 spec_args[j++] = "-o";
512 spec_args[j++] = spec_c_name;
513 if (create_wrapper)
515 spec_args[j++] = "-F";
516 spec_args[j++] = strmake("%s-wrap.dll", base_name);
517 spec_args[j++] = "--spec";
518 spec_args[j++] = spec_name;
520 else
522 spec_args[j++] = "--exe";
523 spec_args[j++] = strmake("%s.exe", base_name);
524 spec_args[j++] = gui_mode ? "-mgui" : "-mcui";
526 for (i = 0; i < nb_dll_files; i++)
527 spec_args[j++] = dll_files[i];
528 for (i = 0; i < nb_obj_files; i++)
529 spec_args[j++] = obj_files[i];
530 for (i = 0; i < nb_arh_files; i++)
531 spec_args[j++] = arh_files[i];
532 spec_args[j] = 0;
534 /* build gcc's argument list */
535 comp_args = malloc ( 20 * sizeof (char *) );
536 j = 0;
537 comp_args[j++] = "gcc";
538 comp_args[j++] = "-fPIC";
539 comp_args[j++] = "-o";
540 comp_args[j++] = spec_o_name;
541 comp_args[j++] = "-c";
542 comp_args[j++] = spec_c_name;
543 comp_args[j] = 0;
545 /* build ld's argument list */
546 link_args = malloc( (nb_arh_files + nb_obj_files + nb_lib_files + 20) * sizeof (char *) );
547 j = 0;
548 link_args[j++] = cpp ? "g++" : "gcc";
549 link_args[j++] = "-shared";
550 link_args[j++] = "-Wl,-Bsymbolic,-z,defs";
551 link_args[j++] = "-lwine";
552 link_args[j++] = "-lm";
553 for (i = 0; i < nb_lib_files; i++)
554 link_args[j++] = lib_files[i];
555 link_args[j++] = "-o";
556 if (create_wrapper) link_args[j++] = strmake("%s-wrap.dll.so", base_file);
557 else link_args[j++] = strmake("%s.exe.so", base_file);
558 link_args[j++] = spec_o_name;
559 for (i = 0; i < nb_obj_files; i++)
560 if (!is_resource(obj_files[i])) link_args[j++] = obj_files[i];
561 for (i = 0; i < nb_arh_files; i++)
562 link_args[j++] = arh_files[i];
563 link_args[j] = 0;
565 /* build wrapper compile argument list */
566 wwrap_args = malloc ( 20 * sizeof (char *) );
567 j = 0;
568 wwrap_args[j++] = "gcc";
569 wwrap_args[j++] = "-fPIC";
570 wwrap_args[j++] = "-I" INCLUDEDIR "/windows";
571 wwrap_args[j++] = "-o";
572 wwrap_args[j++] = wrap_o_name;
573 wwrap_args[j++] = "-c";
574 wwrap_args[j++] = wrap_c_name;
575 wwrap_args[j] = 0;
577 /* build wrapper winebuild's argument list */
578 wspec_args = malloc( (nb_dll_files + 20) * sizeof (char *) );
579 j = 0;
580 wspec_args[j++] = BINDIR "/winebuild";
581 wspec_args[j++] = "-o";
582 wspec_args[j++] = wspec_c_name;
583 wspec_args[j++] = "--exe";
584 wspec_args[j++] = strmake("%s.exe", base_name);
585 wspec_args[j++] = gui_mode ? "-mgui" : "-mcui";
586 wspec_args[j++] = wrap_o_name;
587 for (i = 0; i < nb_dll_files; i++)
588 wspec_args[j++] = dll_files[i];
589 wspec_args[j] = 0;
591 /* build wrapper gcc's argument list */
592 wcomp_args = malloc ( 20 * sizeof (char *) );
593 j = 0;
594 wcomp_args[j++] = "gcc";
595 wcomp_args[j++] = "-fPIC";
596 wcomp_args[j++] = "-o";
597 wcomp_args[j++] = wspec_o_name;
598 wcomp_args[j++] = "-c";
599 wcomp_args[j++] = wspec_c_name;
600 wcomp_args[j] = 0;
602 /* build wrapper ld's argument list */
603 wlink_args = malloc( 20 * sizeof (char *) );
604 j = 0;
605 wlink_args[j++] = cpp ? "g++" : "gcc";
606 wlink_args[j++] = "-shared";
607 wlink_args[j++] = "-Wl,-Bsymbolic,-z,defs";
608 wlink_args[j++] = "-lwine";
609 wlink_args[j++] = "-ldl";
610 wlink_args[j++] = "-o";
611 wlink_args[j++] = strmake("%s.exe.so", base_file);
612 wlink_args[j++] = wspec_o_name;
613 wlink_args[j++] = wrap_o_name;
614 wlink_args[j] = 0;
616 /* run winebuild to get the .spec.c file */
617 if (create_wrapper)
619 create_file(spec_name, gui_mode ? app_gui_spec : app_cui_spec);
620 spawn(spec_args);
621 rm_temp_file(spec_name);
622 spawn(comp_args);
623 rm_temp_file(spec_c_name);
624 spawn(link_args);
625 rm_temp_file(spec_o_name);
627 create_file(wrap_c_name, wrapper_code, base_name, gui_mode);
628 spawn(wwrap_args);
629 rm_temp_file(wrap_c_name);
630 spawn(wspec_args);
631 spawn(wcomp_args);
632 rm_temp_file(wspec_c_name);
633 spawn(wlink_args);
634 rm_temp_file(wspec_o_name);
635 rm_temp_file(wrap_o_name);
637 else
639 spawn(spec_args);
640 spawn(comp_args);
641 rm_temp_file(spec_c_name);
642 spawn(link_args);
643 rm_temp_file(spec_o_name);
646 /* create the loader script */
647 create_file(base_file, app_loader_script, base_name);
648 chmod(base_file, 0755);
650 return 0;