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