ok() does not support '%S'. Store the Ansi version, convert to Unicode
[wine] / tools / winewrap.c
1 /*
2  * Wine wrapper: takes care of internal details for linking.
3  *
4  * Copyright 2000 Francois Gouget
5  * Copyright 2002 Dimitrie O. Paun
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35
36 #ifndef WINEDLLS
37 #define WINEDLLS "/usr/local/lib/wine"
38 #endif
39
40 static const char *app_loader_script =
41     "#!/bin/sh\n"
42     "\n"
43     "appname=\"%s\"\n"
44     "# determine the application directory\n"
45     "appdir=''\n"
46     "case \"$0\" in\n"
47     "  */*)\n"
48     "    # $0 contains a path, use it\n"
49     "    appdir=`dirname \"$0\"`\n"
50     "    ;;\n"
51     "  *)\n"
52     "    # no directory in $0, search in PATH\n"
53     "    saved_ifs=$IFS\n"
54     "    IFS=:\n"
55     "    for d in $PATH\n"
56     "    do\n"
57     "      IFS=$saved_ifs\n"
58     "      if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
59     "    done\n"
60     "    ;;\n"
61     "esac\n"
62     "\n"
63     "while true; do\n"
64     "  case \"$1\" in\n"
65     "    --debugmsg)\n"
66     "      debugmsg=\"$1 $2\"\n"
67     "      shift; shift;\n"
68     "      ;;\n"
69     "    --dll)\n"
70     "      dll=\"$1 $2\"\n"
71     "      shift; shift;\n"
72     "      ;;\n"
73     "    *)\n"
74     "      break\n"
75     "      ;;\n"
76     "  esac\n"
77     "done\n"
78     "\n"
79     "# figure out the full app path\n"
80     "if [ -n \"$appdir\" ]; then\n"
81     "    apppath=\"$appdir/$appname.exe.so\"\n"
82     "else\n"
83     "    apppath=\"$appname.exe.so\"\n"
84     "fi\n"
85     "\n"
86     "# determine the WINELOADER\n"
87     "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
88     "\n"
89     "# and try to start the app\n"
90     "exec \"$WINELOADER\" $debugmsg $dll -- \"$apppath\" \"$@\"\n"
91 ;
92
93 static const char *app_gui_spec =
94     "@ stdcall WinMain(ptr long ptr long) WinMain\n"
95 ;
96
97 static const char *app_cui_spec =
98     "@ stdcall main(long ptr ptr) main\n"
99 ;
100
101 static const char *wrapper_code =
102     "/*\n"
103     " * Copyright 2000 Francois Gouget <fgouget@codeweavers.com> for CodeWeavers\n"
104     " * Copyright 2002 Dimitrie O. Paun <dpaun@rogers.com>\n"
105     " */\n"
106     "\n"
107     "#include <dlfcn.h>\n"
108     "#include <windows.h>\n"
109     "\n"
110     "\n"
111     "/*\n"
112     " * Describe the wrapped application\n"
113     " */\n"
114     "\n"
115     "/* The app name */\n"
116     "#define APPNAME \"%s\"\n"
117     "/**\n"
118     " * This is either 0 for a console based application or\n"
119     " * 1 for a regular windows application.\n"
120     " */\n"
121     "#define GUIEXE %d\n"
122     "\n"
123     "/**\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"
126     " */\n"
127     "static char* appName     = APPNAME \".dll\";\n"
128     "\n"
129     "/**\n"
130     " * This is the name of the application's Windows module. If left NULL\n"
131     " * then appName is used.\n"
132     " */\n"
133     "static char* appModule   = NULL;\n"
134     "\n"
135     "/**\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"
138     " */\n"
139     "#if GUIEXE\n"
140     "static char* appInit     = \"WinMain\";\n"
141     "#else\n"
142     "static char* appInit     = \"main\";\n"
143     "#endif\n"
144     "\n"
145     "/**\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"
148     " * function.\n"
149     " */\n"
150     "static char* mfcModule   = NULL;\n"
151     "\n"
152     "\n"
153     "void error(const char *format, ...)\n"
154     "{\n"
155     "    va_list ap;\n"
156     "    char msg[4096];\n"
157     "\n"
158     "    va_start(ap, format);\n"
159     "    vsnprintf(msg, sizeof(msg), format, ap);\n"
160     "    MessageBox(NULL, msg, \"Error\", MB_OK);\n"
161     "    va_end(ap);\n"
162     "    exit(1);\n"
163     "}\n"
164     "\n"
165     "\n"
166     "#if GUIEXE\n"
167     "typedef int WINAPI (*WinMainFunc)(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
168     "                             PSTR szCmdLine, int iCmdShow);\n"
169     "#else\n"
170     "typedef int WINAPI (*MainFunc)(int argc, char** argv, char** envp);\n"
171     "#endif\n"
172     "\n"
173     "#if GUIEXE\n"
174     "int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,\n"
175     "                   PSTR szCmdLine, int iCmdShow)\n"
176     "#else\n"
177     "int WINAPI main(int argc, char** argv, char** envp)\n"
178     "#endif\n"
179     "{\n"
180     "    void* appLibrary;\n"
181     "    HINSTANCE hApp = 0, hMFC = 0, hMain = 0;\n"
182     "    void* appMain;\n"
183     "    char* libName;\n"
184     "    int retcode;\n"
185     "\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"
189     "     * the library\n"
190     "     */\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"
196     "    }\n"
197     "    if (!appLibrary) error(\"Could not load the %%s library: %%s\", libName, dlerror());\n"
198     "\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"
206     "         */\n"
207     "        hMain = hMFC;\n"
208     "    }\n"
209     "\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"
215     "\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"
219     "\n"
220     "    /* And finally invoke the application's entry point */\n"
221     "#if GUIEXE\n"
222     "    retcode = (*((WinMainFunc)appMain))(hApp, hPrevInstance, szCmdLine, iCmdShow);\n"
223     "#else\n"
224     "    retcode = (*((MainFunc)appMain))(argc, argv, envp);\n"
225     "#endif\n"
226     "\n"
227     "    /* Cleanup and done */\n"
228     "    FreeLibrary(hApp);\n"
229     "    FreeLibrary(hMFC);\n"
230     "    dlclose(appLibrary);\n"
231     "    free(libName);\n"
232     "\n"
233     "    return retcode;\n"
234     "}\n"
235 ;
236
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;
242
243 void error(const char *s, ...)
244 {
245     va_list ap;
246     
247     va_start(ap, s);
248     fprintf(stderr, "Error: ");
249     vfprintf(stderr, s, ap);
250     fprintf(stderr, "\n");
251     va_end(ap);
252     exit(2);
253 }
254
255 char *strmake(const char *fmt, ...) 
256 {
257     int n, size = 100;
258     char *p;
259     va_list ap;
260
261     if ((p = malloc (size)) == NULL)
262         error("Can not malloc %d bytes.", size);
263     
264     while (1) 
265     {
266         va_start(ap, fmt);
267         n = vsnprintf (p, size, fmt, ap);
268         va_end(ap);
269         if (n > -1 && n < size) return p;
270         size *= 2;
271         if ((p = realloc (p, size)) == NULL)
272             error("Can not realloc %d bytes.", size);
273     }
274 }
275
276 void rm_temp_file(const char *file)
277 {
278     if (!keep_generated) unlink(file);
279 }
280
281 void create_file(const char *name, const char *fmt, ...)
282 {
283     va_list ap;
284     FILE *file;
285
286     if (verbose) printf("Creating file %s\n", name);
287     va_start(ap, fmt);
288     if ( !(file = fopen(name, "w")) )
289         error ("Can not create %s.", name);
290     vfprintf(file, fmt, ap);
291     va_end(ap);
292     fclose(file);
293 }
294
295 void spawn(char *const argv[])
296 {
297     int pid, status, wret, i;
298
299     if (verbose)
300     {   
301         for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
302         printf("\n");
303     }
304     
305     if ((pid = fork()) == 0) execvp(argv[0], argv);
306     else if (pid > 0)
307     {
308         while (pid != (wret = waitpid(pid, &status, 0)))
309             if (wret == -1 && errno != EINTR) break;
310         
311         if (pid == wret && WIFEXITED(status) && WEXITSTATUS(status) == 0) return;
312         error("%s failed.", argv[0]);
313     }
314     perror("Error:");
315     exit(3);
316 }
317
318 int is_resource(const char* file)
319 {
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)];
323     FILE *fin;
324     
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);
327     fclose(fin);
328
329     return !memcmp(buf, res_sig, sizeof(buf));
330 }
331
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 )
334 {
335     char *fullname;
336     int fd;
337
338     fullname = strmake("%s/lib%s.%s", path, name, ext);
339     if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
340
341     /* check if the file exists */
342     if ((fd = open( fullname, O_RDONLY )) != -1)
343     {
344         close( fd );
345         if (verbose > 1) fprintf(stderr, "FOUND!\n");
346         return fullname;
347     }
348     free( fullname );
349     if (verbose > 1) fprintf(stderr, "\n");
350     return NULL;
351 }
352
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 )
355 {
356     return try_path(path, name, "def");
357 }
358
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 )
361 {
362     return try_path(path, name, "a");
363 }
364
365 /* open the .def library for a given dll */
366 static char *open_dll(const char *name)
367 {
368     char *fullname;
369     int i;
370
371     for (i = 0; i < nb_lib_paths; i++)
372     {
373         if ((fullname = try_dll_path( lib_paths[i], name ))) return fullname;
374     }
375     return try_dll_path( ".", name );
376 }
377
378 /* find a static library */
379 static char *find_lib(const char *name)
380 {
381     static const char* std_paths[] = { ".", "/usr/lib", "/usr/local/lib" };
382     char *fullname;
383     int i;
384     
385     for (i = 0; i < nb_lib_paths; i++)
386     {
387         if ((fullname = try_lib_path( lib_paths[i], name ))) return fullname;
388     }
389
390     for (i = 0; i < sizeof(std_paths)/sizeof(std_paths[0]); i++)
391     {
392         if ((fullname = try_lib_path( std_paths[i], name ))) return fullname;
393     }
394
395     return 0;
396 }
397
398 void add_lib_path(const char* path)
399 {
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);
406 }
407
408 void add_lib_file(const char* library)
409 {
410     char *lib;
411     
412     if (open_dll(library))
413     {
414         dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
415         dll_files[nb_dll_files++] = strmake("-l%s", library);
416     }
417     else if ((lib = find_lib(library)))
418     {
419         arh_files = realloc( arh_files, (nb_arh_files+1) * sizeof(*arh_files) );
420         arh_files[nb_arh_files++] = lib;
421     }
422     else
423     {
424         lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
425         lib_files[nb_lib_files++] = strmake("-l%s", library);
426     }
427 }
428
429 int main(int argc, char **argv)
430 {
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;
439    
440     /* include the standard DLL path first */
441     add_lib_path(WINEDLLS);
442         
443     for (i = 1; i < argc; i++)
444     {
445         if (!no_opt && argv[i][0] == '-')
446         {
447             switch (argv[i][1])
448             {
449             case 'k':
450                 keep_generated = 1;
451                 break;
452             case 'o':
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.");
456                 break;
457             case 'L':
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.");
461                 add_lib_path(path);
462                 break;
463             case 'l':
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);
468                 break;
469             case 'm':
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]);
473                 break;
474             case 'v':        /* verbose */
475                 if (argv[i][2] == 0) verbose++;
476                 break;
477             case 'V':
478                 printf("winewrap v0.40\n");
479                 exit(0);
480                 break;
481             case 'C':
482                 cpp = 1;
483                 break;
484             case 'w':
485                 create_wrapper = 1;
486                 break;
487             case 'W':
488                 create_wrapper = 0;
489                 break;
490             case '-':
491                 if (argv[i][2]) error("No long option supported.");
492                 no_opt = 1;
493                 break;
494             default:
495                 error("Unknown option -%c", argv[i][1]);
496             }
497             continue;
498         }
499         
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]);
503     }
504
505     /* create wrapper only in C++ by default */
506     if (create_wrapper == -1) create_wrapper = cpp;
507     
508     /* link in by default the big three */
509     add_lib_file("user32");
510     add_lib_file("gdi32");
511     add_lib_file("kernel32");
512
513     app_temp_name = tempnam(0, "wapp");
514     wrp_temp_name = tempnam(0, "wwrp");
515    
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 */
521
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;
525     
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);
529
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);
533
534     wrap_c_name = strmake("%s.c", wrp_temp_name);
535     wrap_o_name = strmake("%s.o", wrp_temp_name);
536
537     /* build winebuild's argument list */
538     spec_args = malloc( (nb_arh_files + nb_dll_files + nb_obj_files + 20) * sizeof (char *) );
539     j = 0;
540     spec_args[j++] = BINDIR "/winebuild";
541     spec_args[j++] = "-o";
542     spec_args[j++] = spec_c_name;
543     if (create_wrapper)
544     {
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;
549     }
550     else
551     {
552         spec_args[j++] = "--exe";
553         spec_args[j++] = strmake("%s.exe", base_name);
554         spec_args[j++] = gui_mode ? "-mgui" : "-mcui";
555     }
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];
562     spec_args[j] = 0;
563
564     /* build gcc's argument list */
565     comp_args = malloc ( 20 * sizeof (char *) );
566     j = 0;
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;
573     comp_args[j] = 0;
574     
575     /* build ld's argument list */
576     link_args = malloc( (nb_arh_files + nb_obj_files + nb_lib_files + 20) * sizeof (char *) );
577     j = 0;
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];
592     link_args[j] = 0;
593   
594     /* build wrapper compile argument list */
595     wwrap_args = malloc ( 20 * sizeof (char *) );
596     j = 0;
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;
604     wwrap_args[j] = 0;
605      
606     /* build wrapper winebuild's argument list */
607     wspec_args = malloc( (nb_dll_files + 20) * sizeof (char *) );
608     j = 0;
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];
618     wspec_args[j] = 0;
619
620     /* build wrapper gcc's argument list */
621     wcomp_args = malloc ( 20 * sizeof (char *) );
622     j = 0;
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;
629     wcomp_args[j] = 0;
630     
631     /* build wrapper ld's argument list */
632     wlink_args = malloc( 20 * sizeof (char *) );
633     j = 0;
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;
643     wlink_args[j] = 0;
644     
645     /* run winebuild to get the .spec.c file */
646     if (create_wrapper)
647     {
648         create_file(spec_name, gui_mode ? app_gui_spec : app_cui_spec);
649         spawn(spec_args);
650         rm_temp_file(spec_name);
651         spawn(comp_args);
652         rm_temp_file(spec_c_name);
653         spawn(link_args);
654         rm_temp_file(spec_o_name);
655
656         create_file(wrap_c_name, wrapper_code, base_name, gui_mode);
657         spawn(wwrap_args);
658         rm_temp_file(wrap_c_name);
659         spawn(wspec_args);
660         spawn(wcomp_args);
661         rm_temp_file(wspec_c_name);
662         spawn(wlink_args);
663         rm_temp_file(wspec_o_name);
664         rm_temp_file(wrap_o_name);
665     }
666     else
667     {
668         spawn(spec_args);
669         spawn(comp_args);
670         rm_temp_file(spec_c_name);
671         spawn(link_args);
672         rm_temp_file(spec_o_name);
673     }
674
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);
678
679     return 0;    
680