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