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