Fix some -Wsign-compare warnings.
[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 <signal.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <ctype.h>
35 #ifdef HAVE_GETOPT_H
36 # include <getopt.h>
37 #endif
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "build.h"
42
43 int UsePIC = 0;
44 int nb_debug_channels = 0;
45 int nb_lib_paths = 0;
46 int nb_errors = 0;
47 int display_warnings = 0;
48 int kill_at = 0;
49 int debugging = 0;
50
51 #ifdef __i386__
52 enum target_cpu target_cpu = CPU_x86;
53 #elif defined(__sparc__)
54 enum target_cpu target_cpu = CPU_SPARC;
55 #elif defined(__ALPHA__)
56 enum target_cpu target_cpu = CPU_ALPHA;
57 #elif defined(__powerpc__)
58 enum target_cpu target_cpu = CPU_POWERPC;
59 #else
60 #error Unsupported CPU
61 #endif
62
63 #ifdef __APPLE__
64 enum target_platform target_platform = PLATFORM_APPLE;
65 #elif defined(__svr4__)
66 enum target_platform target_platform = PLATFORM_SVR4;
67 #elif defined(_WINDOWS)
68 enum target_platform target_platform = PLATFORM_WINDOWS;
69 #else
70 enum target_platform target_platform = PLATFORM_UNSPECIFIED;
71 #endif
72
73 char **debug_channels = NULL;
74 char **lib_path = NULL;
75
76 char *input_file_name = NULL;
77 char *spec_file_name = NULL;
78 const char *output_file_name = NULL;
79
80 char *ld_command = "ld";
81 char *nm_command = "nm";
82
83 static FILE *output_file;
84 static const char *current_src_dir;
85 static int nb_res_files;
86 static char **res_files;
87
88 /* execution mode */
89 enum exec_mode_values
90 {
91     MODE_NONE,
92     MODE_DLL,
93     MODE_EXE,
94     MODE_DEF,
95     MODE_DEBUG,
96     MODE_RELAY16,
97     MODE_RELAY32
98 };
99
100 static enum exec_mode_values exec_mode = MODE_NONE;
101
102 /* set the dll file name from the input file name */
103 static void set_dll_file_name( const char *name, DLLSPEC *spec )
104 {
105     char *p;
106
107     if (spec->file_name) return;
108
109     if ((p = strrchr( name, '\\' ))) name = p + 1;
110     if ((p = strrchr( name, '/' ))) name = p + 1;
111     spec->file_name = xmalloc( strlen(name) + 5 );
112     strcpy( spec->file_name, name );
113     if ((p = strrchr( spec->file_name, '.' )))
114     {
115         if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
116     }
117 }
118
119 /* set the dll subsystem */
120 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
121 {
122     char *major, *minor, *str = xstrdup( subsystem );
123
124     if ((major = strchr( str, ':' ))) *major++ = 0;
125     if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
126     else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
127     else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
128     else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
129     if (major)
130     {
131         if ((minor = strchr( major, '.' )))
132         {
133             *minor++ = 0;
134             spec->subsystem_minor = atoi( minor );
135         }
136         spec->subsystem_major = atoi( major );
137     }
138     free( str );
139 }
140
141 /* cleanup on program exit */
142 static void cleanup(void)
143 {
144     if (output_file_name) unlink( output_file_name );
145 }
146
147 /* clean things up when aborting on a signal */
148 static void exit_on_signal( int sig )
149 {
150     exit(1);  /* this will call atexit functions */
151 }
152
153 /*******************************************************************
154  *         command-line option handling
155  */
156 static const char usage_str[] =
157 "Usage: winebuild [OPTIONS] [FILES]\n\n"
158 "Options:\n"
159 "    -C --source-dir=DIR     Look for source files in DIR\n"
160 "    -d --delay-lib=LIB      Import the specified library in delayed mode\n"
161 "    -D SYM                  Ignored for C flags compatibility\n"
162 "    -E --export=FILE        Export the symbols defined in the .spec or .def file\n"
163 "    -e --entry=FUNC         Set the DLL entry point function (default: DllMain)\n"
164 "    -f FLAGS                Compiler flags (only -fPIC is supported)\n"
165 "    -F --filename=DLLFILE   Set the DLL filename (default: from input file name)\n"
166 "    -h --help               Display this help message\n"
167 "    -H --heap=SIZE          Set the heap size for a Win16 dll\n"
168 "    -i --ignore=SYM[,SYM]   Ignore specified symbols when resolving imports\n"
169 "    -I DIR                  Ignored for C flags compatibility\n"
170 "    -k --kill-at            Kill stdcall decorations in generated .def files\n"
171 "    -K FLAGS                Compiler flags (only -KPIC is supported)\n"
172 "       --ld-cmd=LD          Command to use for linking (default: ld)\n"
173 "    -l --library=LIB        Import the specified library\n"
174 "    -L --library-path=DIR   Look for imports libraries in DIR\n"
175 "    -M --main-module=MODULE Set the name of the main module for a Win16 dll\n"
176 "       --nm-cmd=NM          Command to use to get undefined symbols (default: nm)\n"
177 "    -N --dll-name=DLLNAME   Set the DLL name (default: from input file name)\n"
178 "    -o --output=NAME        Set the output file name (default: stdout)\n"
179 "    -r --res=RSRC.RES       Load resources from RSRC.RES\n"
180 "       --subsystem=SUBSYS   Set the subsystem (one of native, windows, console)\n"
181 "       --version            Print the version and exit\n"
182 "    -w --warnings           Turn on warnings\n"
183 "\nMode options:\n"
184 "       --dll                Build a .c file from a .spec or .def file\n"
185 "       --def                Build a .def file from a .spec file\n"
186 "       --exe                Build a .c file for an executable\n"
187 "       --debug [FILES]      Build a .c file with the debug channels declarations\n"
188 "       --relay16            Build the 16-bit relay assembly routines\n"
189 "       --relay32            Build the 32-bit relay assembly routines\n\n"
190 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
191
192 enum long_options_values
193 {
194     LONG_OPT_DLL = 1,
195     LONG_OPT_DEF,
196     LONG_OPT_EXE,
197     LONG_OPT_DEBUG,
198     LONG_OPT_LDCMD,
199     LONG_OPT_NMCMD,
200     LONG_OPT_RELAY16,
201     LONG_OPT_RELAY32,
202     LONG_OPT_SUBSYSTEM,
203     LONG_OPT_VERSION
204 };
205
206 static const char short_options[] = "C:D:E:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
207
208 static const struct option long_options[] =
209 {
210     { "dll",      0, 0, LONG_OPT_DLL },
211     { "def",      0, 0, LONG_OPT_DEF },
212     { "exe",      0, 0, LONG_OPT_EXE },
213     { "debug",    0, 0, LONG_OPT_DEBUG },
214     { "ld-cmd",   1, 0, LONG_OPT_LDCMD },
215     { "nm-cmd",   1, 0, LONG_OPT_NMCMD },
216     { "relay16",  0, 0, LONG_OPT_RELAY16 },
217     { "relay32",  0, 0, LONG_OPT_RELAY32 },
218     { "subsystem",1, 0, LONG_OPT_SUBSYSTEM },
219     { "version",  0, 0, LONG_OPT_VERSION },
220     /* aliases for short options */
221     { "source-dir",    1, 0, 'C' },
222     { "delay-lib",     1, 0, 'd' },
223     { "export",        1, 0, 'E' },
224     { "entry",         1, 0, 'e' },
225     { "filename",      1, 0, 'F' },
226     { "help",          0, 0, 'h' },
227     { "heap",          1, 0, 'H' },
228     { "ignore",        1, 0, 'i' },
229     { "kill-at",       0, 0, 'k' },
230     { "library",       1, 0, 'l' },
231     { "library-path",  1, 0, 'L' },
232     { "main-module",   1, 0, 'M' },
233     { "dll-name",      1, 0, 'N' },
234     { "output",        1, 0, 'o' },
235     { "res",           1, 0, 'r' },
236     { "warnings",      0, 0, 'w' },
237     { NULL,            0, 0, 0 }
238 };
239
240 static void usage( int exit_code )
241 {
242     fprintf( stderr, "%s", usage_str );
243     exit( exit_code );
244 }
245
246 static void set_exec_mode( enum exec_mode_values mode )
247 {
248     if (exec_mode != MODE_NONE) usage(1);
249     exec_mode = mode;
250 }
251
252 /* parse options from the argv array and remove all the recognized ones */
253 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
254 {
255     char *p;
256     int optc;
257
258     while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
259     {
260         switch(optc)
261         {
262         case 'C':
263             current_src_dir = optarg;
264             break;
265         case 'D':
266             /* ignored */
267             break;
268         case 'E':
269             spec_file_name = xstrdup( optarg );
270             set_dll_file_name( optarg, spec );
271             break;
272         case 'F':
273             spec->file_name = xstrdup( optarg );
274             break;
275         case 'H':
276             if (!isdigit(optarg[0]))
277                 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
278             spec->heap_size = atoi(optarg);
279             if (spec->heap_size > 65535)
280                 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
281             break;
282         case 'I':
283             /* ignored */
284             break;
285         case 'K':
286             /* ignored, because cc generates correct code. */
287             break;
288         case 'L':
289             lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
290             lib_path[nb_lib_paths++] = xstrdup( optarg );
291             break;
292         case 'M':
293             spec->owner_name = xstrdup( optarg );
294             spec->type = SPEC_WIN16;
295             break;
296         case 'N':
297             spec->dll_name = xstrdup( optarg );
298             break;
299         case 'd':
300             add_delayed_import( optarg );
301             break;
302         case 'e':
303             spec->init_func = xstrdup( optarg );
304             if ((p = strchr( spec->init_func, '@' ))) *p = 0;  /* kill stdcall decoration */
305             break;
306         case 'f':
307             if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
308             /* ignore all other flags */
309             break;
310         case 'h':
311             usage(0);
312             break;
313         case 'i':
314             {
315                 char *str = xstrdup( optarg );
316                 char *token = strtok( str, "," );
317                 while (token)
318                 {
319                     add_ignore_symbol( token );
320                     token = strtok( NULL, "," );
321                 }
322                 free( str );
323             }
324             break;
325         case 'k':
326             kill_at = 1;
327             break;
328         case 'l':
329             add_import_dll( optarg, NULL );
330             break;
331         case 'o':
332             if (unlink( optarg ) == -1 && errno != ENOENT)
333                 fatal_error( "Unable to create output file '%s'\n", optarg );
334             if (!(output_file = fopen( optarg, "w" )))
335                 fatal_error( "Unable to create output file '%s'\n", optarg );
336             output_file_name = xstrdup(optarg);
337             atexit( cleanup );  /* make sure we remove the output file on exit */
338             break;
339         case 'r':
340             res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
341             res_files[nb_res_files++] = xstrdup( optarg );
342             break;
343         case 'w':
344             display_warnings = 1;
345             break;
346         case LONG_OPT_DLL:
347             set_exec_mode( MODE_DLL );
348             break;
349         case LONG_OPT_DEF:
350             set_exec_mode( MODE_DEF );
351             break;
352         case LONG_OPT_EXE:
353             set_exec_mode( MODE_EXE );
354             if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
355             break;
356         case LONG_OPT_DEBUG:
357             set_exec_mode( MODE_DEBUG );
358             break;
359         case LONG_OPT_LDCMD:
360             ld_command = xstrdup( optarg );
361             break;
362         case LONG_OPT_NMCMD:
363             nm_command = xstrdup( optarg );
364             break;
365         case LONG_OPT_RELAY16:
366             set_exec_mode( MODE_RELAY16 );
367             break;
368         case LONG_OPT_RELAY32:
369             set_exec_mode( MODE_RELAY32 );
370             break;
371         case LONG_OPT_SUBSYSTEM:
372             set_subsystem( optarg, spec );
373             break;
374         case LONG_OPT_VERSION:
375             printf( "winebuild version " PACKAGE_VERSION "\n" );
376             exit(0);
377         case '?':
378             usage(1);
379             break;
380         }
381     }
382
383     if (spec->file_name && !strchr( spec->file_name, '.' ))
384         strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
385
386     return &argv[optind];
387 }
388
389
390 /* load all specified resource files */
391 static void load_resources( char *argv[], DLLSPEC *spec )
392 {
393     int i;
394     char **ptr, **last;
395
396     switch (spec->type)
397     {
398     case SPEC_WIN16:
399         for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
400         break;
401
402     case SPEC_WIN32:
403         for (i = 0; i < nb_res_files; i++)
404         {
405             if (!load_res32_file( res_files[i], spec ))
406                 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
407         }
408
409         /* load any resource file found in the remaining arguments */
410         for (ptr = last = argv; *ptr; ptr++)
411         {
412             if (!load_res32_file( *ptr, spec ))
413                 *last++ = *ptr; /* not a resource file, keep it in the list */
414         }
415         *last = NULL;
416         break;
417     }
418 }
419
420 /* add input files that look like import libs to the import list */
421 static void load_import_libs( char *argv[] )
422 {
423     char **ptr, **last;
424
425     for (ptr = last = argv; *ptr; ptr++)
426     {
427         if (strendswith( *ptr, ".def" ))
428             add_import_dll( NULL, *ptr );
429         else
430             *last++ = *ptr; /* not an import dll, keep it in the list */
431     }
432     *last = NULL;
433 }
434
435 static int parse_input_file( DLLSPEC *spec )
436 {
437     FILE *input_file = open_input_file( NULL, spec_file_name );
438     char *extension = strrchr( spec_file_name, '.' );
439     int result;
440
441     if (extension && !strcmp( extension, ".def" ))
442         result = parse_def_file( input_file, spec );
443     else
444         result = parse_spec_file( input_file, spec );
445     close_input_file( input_file );
446     return result;
447 }
448
449
450 /*******************************************************************
451  *         main
452  */
453 int main(int argc, char **argv)
454 {
455     DLLSPEC *spec = alloc_dll_spec();
456
457 #ifdef SIGHUP
458     signal( SIGHUP, exit_on_signal );
459 #endif
460     signal( SIGTERM, exit_on_signal );
461     signal( SIGINT, exit_on_signal );
462
463     output_file = stdout;
464     argv = parse_options( argc, argv, spec );
465
466     /* we only support relay debugging on i386 */
467     debugging = (target_cpu == CPU_x86);
468
469     switch(exec_mode)
470     {
471     case MODE_DLL:
472         spec->characteristics |= IMAGE_FILE_DLL;
473         load_resources( argv, spec );
474         load_import_libs( argv );
475         if (!spec_file_name) fatal_error( "missing .spec file\n" );
476         if (!parse_input_file( spec )) break;
477         switch (spec->type)
478         {
479             case SPEC_WIN16:
480                 if (argv[0])
481                     fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
482                 BuildSpec16File( output_file, spec );
483                 break;
484             case SPEC_WIN32:
485                 read_undef_symbols( argv );
486                 BuildSpec32File( output_file, spec );
487                 break;
488             default: assert(0);
489         }
490         break;
491     case MODE_EXE:
492         if (spec->type == SPEC_WIN16) fatal_error( "Cannot build 16-bit exe files\n" );
493         if (!spec->file_name) fatal_error( "executable must be named via the -F option\n" );
494         load_resources( argv, spec );
495         load_import_libs( argv );
496         if (spec_file_name && !parse_input_file( spec )) break;
497         read_undef_symbols( argv );
498         BuildSpec32File( output_file, spec );
499         break;
500     case MODE_DEF:
501         if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
502         if (spec->type == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
503         if (!spec_file_name) fatal_error( "missing .spec file\n" );
504         if (!parse_input_file( spec )) break;
505         BuildDef32File( output_file, spec );
506         break;
507     case MODE_DEBUG:
508         BuildDebugFile( output_file, current_src_dir, argv );
509         break;
510     case MODE_RELAY16:
511         if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
512         BuildRelays16( output_file );
513         break;
514     case MODE_RELAY32:
515         if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
516         BuildRelays32( output_file );
517         break;
518     default:
519         usage(1);
520         break;
521     }
522     if (nb_errors) exit(1);
523     if (output_file_name)
524     {
525         fclose( output_file );
526         output_file_name = NULL;
527     }
528     return 0;
529 }