Support Darwin ".dylib".
[wine] / tools / winebuild / main.c
1 /*
2  * Main function
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Martin von Loewis
6  * Copyright 1995, 1996, 1997 Alexandre Julliard
7  * Copyright 1997 Eric Youngdale
8  * Copyright 1999 Ulrich Weigand
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <ctype.h>
33 #ifdef HAVE_GETOPT_H
34 # include <getopt.h>
35 #endif
36
37 #include "build.h"
38
39 int UsePIC = 0;
40 int nb_debug_channels = 0;
41 int nb_lib_paths = 0;
42 int nb_errors = 0;
43 int display_warnings = 0;
44 int kill_at = 0;
45
46 /* we only support relay debugging on i386 */
47 #if defined(__i386__) && !defined(NO_TRACE_MSGS)
48 int debugging = 1;
49 #else
50 int debugging = 0;
51 #endif
52
53 char **debug_channels = NULL;
54 char **lib_path = NULL;
55
56 char *input_file_name = NULL;
57 const char *output_file_name = NULL;
58
59 static FILE *output_file;
60 static const char *current_src_dir;
61 static int nb_res_files;
62 static char **res_files;
63 static char *spec_file_name;
64
65 /* execution mode */
66 enum exec_mode_values
67 {
68     MODE_NONE,
69     MODE_DLL,
70     MODE_EXE,
71     MODE_DEF,
72     MODE_DEBUG,
73     MODE_RELAY16,
74     MODE_RELAY32
75 };
76
77 static enum exec_mode_values exec_mode = MODE_NONE;
78
79 /* set the dll file name from the input file name */
80 static void set_dll_file_name( const char *name, DLLSPEC *spec )
81 {
82     char *p;
83
84     if (spec->file_name) return;
85
86     if ((p = strrchr( name, '\\' ))) name = p + 1;
87     if ((p = strrchr( name, '/' ))) name = p + 1;
88     spec->file_name = xmalloc( strlen(name) + 5 );
89     strcpy( spec->file_name, name );
90     if ((p = strrchr( spec->file_name, '.' )))
91     {
92         if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
93     }
94     if (!strchr( spec->file_name, '.' )) strcat( spec->file_name, ".dll" );
95 }
96
97 /* cleanup on program exit */
98 static void cleanup(void)
99 {
100     if (output_file_name) unlink( output_file_name );
101 }
102
103
104 /*******************************************************************
105  *         command-line option handling
106  */
107 static const char usage_str[] =
108 "Usage: winebuild [OPTIONS] [FILES]\n\n"
109 "Options:\n"
110 "    -C --source-dir=DIR     Look for source files in DIR\n"
111 "    -d --delay-lib=LIB      Import the specified library in delayed mode\n"
112 "    -D SYM                  Ignored for C flags compatibility\n"
113 "    -e --entry=FUNC         Set the DLL entry point function (default: DllMain)\n"
114 "    -f FLAGS                Compiler flags (only -fPIC is supported)\n"
115 "    -F --filename=DLLFILE   Set the DLL filename (default: from input file name)\n"
116 "    -h --help               Display this help message\n"
117 "    -H --heap=SIZE          Set the heap size for a Win16 dll\n"
118 "    -i --ignore=SYM[,SYM]   Ignore specified symbols when resolving imports\n"
119 "    -I DIR                  Ignored for C flags compatibility\n"
120 "    -k --kill-at            Kill stdcall decorations in generated .def files\n"
121 "    -K FLAGS                Compiler flags (only -KPIC is supported)\n"
122 "    -l --library=LIB        Import the specified library\n"
123 "    -L --library-path=DIR   Look for imports libraries in DIR\n"
124 "    -m --mode=MODE          Set the binary mode (cui|gui|cuiw|guiw|native)\n"
125 "    -M --main-module=MODULE Set the name of the main module for a Win16 dll\n"
126 "    -N --dll-name=DLLNAME   Set the DLL name (default: from input file name)\n"
127 "    -o --output=NAME        Set the output file name (default: stdout)\n"
128 "    -r --res=RSRC.RES       Load resources from RSRC.RES\n"
129 "       --version            Print the version and exit\n"
130 "    -w --warnings           Turn on warnings\n"
131 "\nMode options:\n"
132 "       --dll=FILE           Build a .c file from a .spec or .def file\n"
133 "       --def=FILE.SPEC      Build a .def file from a spec file\n"
134 "       --exe=NAME           Build a .c file for the named executable\n"
135 "       --debug [FILES]      Build a .c file with the debug channels declarations\n"
136 "       --relay16            Build the 16-bit relay assembly routines\n"
137 "       --relay32            Build the 32-bit relay assembly routines\n\n"
138 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
139
140 enum long_options_values
141 {
142     LONG_OPT_DLL = 1,
143     LONG_OPT_DEF,
144     LONG_OPT_EXE,
145     LONG_OPT_DEBUG,
146     LONG_OPT_RELAY16,
147     LONG_OPT_RELAY32,
148     LONG_OPT_VERSION
149 };
150
151 static const char short_options[] = "C:D:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
152
153 static const struct option long_options[] =
154 {
155     { "dll",      1, 0, LONG_OPT_DLL },
156     { "def",      1, 0, LONG_OPT_DEF },
157     { "exe",      1, 0, LONG_OPT_EXE },
158     { "debug",    0, 0, LONG_OPT_DEBUG },
159     { "relay16",  0, 0, LONG_OPT_RELAY16 },
160     { "relay32",  0, 0, LONG_OPT_RELAY32 },
161     { "version",  0, 0, LONG_OPT_VERSION },
162     { "spec",     1, 0, LONG_OPT_DLL },  /* for backwards compatibility */
163     /* aliases for short options */
164     { "source-dir",    1, 0, 'C' },
165     { "delay-lib",     1, 0, 'd' },
166     { "entry",         1, 0, 'e' },
167     { "filename",      1, 0, 'F' },
168     { "help",          0, 0, 'h' },
169     { "heap",          1, 0, 'H' },
170     { "ignore",        1, 0, 'i' },
171     { "kill-at",       0, 0, 'k' },
172     { "library",       1, 0, 'l' },
173     { "library-path",  1, 0, 'L' },
174     { "mode",          1, 0, 'm' },
175     { "exe-mode",      1, 0, 'm' },  /* for backwards compatibility */
176     { "main-module",   1, 0, 'M' },
177     { "dll-name",      1, 0, 'N' },
178     { "output",        1, 0, 'o' },
179     { "res",           1, 0, 'r' },
180     { "warnings",      0, 0, 'w' },
181     { NULL,            0, 0, 0 }
182 };
183
184 static void usage( int exit_code )
185 {
186     fprintf( stderr, "%s", usage_str );
187     exit( exit_code );
188 }
189
190 static void set_exec_mode( enum exec_mode_values mode )
191 {
192     if (exec_mode != MODE_NONE) usage(1);
193     exec_mode = mode;
194 }
195
196 /* parse options from the argv array and remove all the recognized ones */
197 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
198 {
199     char *p;
200     int optc;
201
202     while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
203     {
204         switch(optc)
205         {
206         case 'C':
207             current_src_dir = optarg;
208             break;
209         case 'D':
210             /* ignored */
211             break;
212         case 'F':
213             spec->file_name = xstrdup( optarg );
214             break;
215         case 'H':
216             if (!isdigit(optarg[0]))
217                 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
218             spec->heap_size = atoi(optarg);
219             if (spec->heap_size > 65535)
220                 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
221             break;
222         case 'I':
223             /* ignored */
224             break;
225         case 'K':
226             /* ignored, because cc generates correct code. */
227             break;
228         case 'L':
229             lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
230             lib_path[nb_lib_paths++] = xstrdup( optarg );
231             break;
232         case 'M':
233             spec->owner_name = xstrdup( optarg );
234             spec->type = SPEC_WIN16;
235             break;
236         case 'N':
237             spec->dll_name = xstrdup( optarg );
238             break;
239         case 'd':
240             add_import_dll( optarg, 1 );
241             break;
242         case 'e':
243             spec->init_func = xstrdup( optarg );
244             if ((p = strchr( spec->init_func, '@' ))) *p = 0;  /* kill stdcall decoration */
245             break;
246         case 'f':
247             if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
248             /* ignore all other flags */
249             break;
250         case 'h':
251             usage(0);
252             break;
253         case 'i':
254             {
255                 char *str = xstrdup( optarg );
256                 char *token = strtok( str, "," );
257                 while (token)
258                 {
259                     add_ignore_symbol( token );
260                     token = strtok( NULL, "," );
261                 }
262                 free( str );
263             }
264             break;
265         case 'k':
266             kill_at = 1;
267             break;
268         case 'l':
269             add_import_dll( optarg, 0 );
270             break;
271         case 'm':
272             if (!strcmp( optarg, "gui" )) spec->mode = SPEC_MODE_GUIEXE;
273             else if (!strcmp( optarg, "cui" )) spec->mode = SPEC_MODE_CUIEXE;
274             else if (!strcmp( optarg, "guiw" )) spec->mode = SPEC_MODE_GUIEXE_UNICODE;
275             else if (!strcmp( optarg, "cuiw" )) spec->mode = SPEC_MODE_CUIEXE_UNICODE;
276             else if (!strcmp( optarg, "native" )) spec->mode = SPEC_MODE_NATIVE;
277             else usage(1);
278             break;
279         case 'o':
280             if (unlink( optarg ) == -1 && errno != ENOENT)
281                 fatal_error( "Unable to create output file '%s'\n", optarg );
282             if (!(output_file = fopen( optarg, "w" )))
283                 fatal_error( "Unable to create output file '%s'\n", optarg );
284             output_file_name = xstrdup(optarg);
285             atexit( cleanup );  /* make sure we remove the output file on exit */
286             break;
287         case 'r':
288             res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
289             res_files[nb_res_files++] = xstrdup( optarg );
290             break;
291         case 'w':
292             display_warnings = 1;
293             break;
294         case LONG_OPT_DLL:
295             set_exec_mode( MODE_DLL );
296             spec_file_name = xstrdup( optarg );
297             set_dll_file_name( optarg, spec );
298             break;
299         case LONG_OPT_DEF:
300             set_exec_mode( MODE_DEF );
301             spec_file_name = xstrdup( optarg );
302             set_dll_file_name( optarg, spec );
303             break;
304         case LONG_OPT_EXE:
305             set_exec_mode( MODE_EXE );
306             if ((p = strrchr( optarg, '/' ))) p++;
307             else p = optarg;
308             spec->file_name = xmalloc( strlen(p) + 5 );
309             strcpy( spec->file_name, p );
310             if (!strchr( spec->file_name, '.' )) strcat( spec->file_name, ".exe" );
311             if (spec->mode == SPEC_MODE_DLL) spec->mode = SPEC_MODE_GUIEXE;
312             break;
313         case LONG_OPT_DEBUG:
314             set_exec_mode( MODE_DEBUG );
315             break;
316         case LONG_OPT_RELAY16:
317             set_exec_mode( MODE_RELAY16 );
318             break;
319         case LONG_OPT_RELAY32:
320             set_exec_mode( MODE_RELAY32 );
321             break;
322         case LONG_OPT_VERSION:
323             printf( "winebuild version " PACKAGE_VERSION "\n" );
324             exit(0);
325         case '?':
326             usage(1);
327             break;
328         }
329     }
330     return &argv[optind];
331 }
332
333
334 /* load all specified resource files */
335 static void load_resources( char *argv[], DLLSPEC *spec )
336 {
337     int i;
338     char **ptr, **last;
339
340     switch (spec->type)
341     {
342     case SPEC_WIN16:
343         for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
344         break;
345
346     case SPEC_WIN32:
347         for (i = 0; i < nb_res_files; i++)
348         {
349             if (!load_res32_file( res_files[i], spec ))
350                 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
351         }
352
353         /* load any resource file found in the remaining arguments */
354         for (ptr = last = argv; *ptr; ptr++)
355         {
356             if (!load_res32_file( *ptr, spec ))
357                 *last++ = *ptr; /* not a resource file, keep it in the list */
358         }
359         *last = NULL;
360         break;
361     }
362 }
363
364 static int parse_input_file( DLLSPEC *spec )
365 {
366     FILE *input_file = open_input_file( NULL, spec_file_name );
367     char *extension = strrchr( spec_file_name, '.' );
368
369     if (extension && !strcmp( extension, ".def" ))
370         return parse_def_file( input_file, spec );
371     else
372         return parse_spec_file( input_file, spec );
373     close_input_file( input_file );
374 }
375
376
377 /*******************************************************************
378  *         main
379  */
380 int main(int argc, char **argv)
381 {
382     DLLSPEC *spec = alloc_dll_spec();
383
384     output_file = stdout;
385     argv = parse_options( argc, argv, spec );
386
387     switch(exec_mode)
388     {
389     case MODE_DLL:
390         load_resources( argv, spec );
391         if (!parse_input_file( spec )) break;
392         switch (spec->type)
393         {
394             case SPEC_WIN16:
395                 if (argv[0])
396                     fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
397                 BuildSpec16File( output_file, spec );
398                 break;
399             case SPEC_WIN32:
400                 read_undef_symbols( argv );
401                 BuildSpec32File( output_file, spec );
402                 break;
403             default: assert(0);
404         }
405         break;
406     case MODE_EXE:
407         if (spec->type == SPEC_WIN16) fatal_error( "Cannot build 16-bit exe files\n" );
408         load_resources( argv, spec );
409         read_undef_symbols( argv );
410         BuildSpec32File( output_file, spec );
411         break;
412     case MODE_DEF:
413         if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
414         if (spec->type == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
415         if (!parse_input_file( spec )) break;
416         BuildDef32File( output_file, spec );
417         break;
418     case MODE_DEBUG:
419         BuildDebugFile( output_file, current_src_dir, argv );
420         break;
421     case MODE_RELAY16:
422         if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
423         BuildRelays16( output_file );
424         break;
425     case MODE_RELAY32:
426         if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
427         BuildRelays32( output_file );
428         break;
429     default:
430         usage(1);
431         break;
432     }
433     if (nb_errors) exit(1);
434     if (output_file_name)
435     {
436         fclose( output_file );
437         output_file_name = NULL;
438     }
439     return 0;
440 }