winebuild: Add support for generating stand-alone 16-bit modules.
[wine/multimedia.git] / tools / winebuild / main.c
blobcab5243c7e480acdcac9792549bc558833b20b23
1 /*
2 * Main function
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
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
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
39 #include "windef.h"
40 #include "winbase.h"
41 #include "build.h"
43 int UsePIC = 0;
44 int nb_lib_paths = 0;
45 int nb_errors = 0;
46 int display_warnings = 0;
47 int kill_at = 0;
48 int verbose = 0;
49 int save_temps = 0;
50 int link_ext_symbols = 0;
51 int force_pointer_size = 0;
53 #ifdef __i386__
54 enum target_cpu target_cpu = CPU_x86;
55 #elif defined(__x86_64__)
56 enum target_cpu target_cpu = CPU_x86_64;
57 #elif defined(__sparc__)
58 enum target_cpu target_cpu = CPU_SPARC;
59 #elif defined(__ALPHA__)
60 enum target_cpu target_cpu = CPU_ALPHA;
61 #elif defined(__powerpc__)
62 enum target_cpu target_cpu = CPU_POWERPC;
63 #else
64 #error Unsupported CPU
65 #endif
67 #ifdef __APPLE__
68 enum target_platform target_platform = PLATFORM_APPLE;
69 #elif defined(__sun)
70 enum target_platform target_platform = PLATFORM_SOLARIS;
71 #elif defined(_WINDOWS)
72 enum target_platform target_platform = PLATFORM_WINDOWS;
73 #else
74 enum target_platform target_platform = PLATFORM_UNSPECIFIED;
75 #endif
77 char *target_alias = NULL;
78 char **lib_path = NULL;
80 char *input_file_name = NULL;
81 char *spec_file_name = NULL;
82 FILE *output_file = NULL;
83 const char *output_file_name = NULL;
84 static const char *output_file_source_name;
85 static char *main_module; /* FIXME: to be removed */
87 char *as_command = NULL;
88 char *ld_command = NULL;
89 char *nm_command = NULL;
91 static int nb_res_files;
92 static char **res_files;
94 /* execution mode */
95 enum exec_mode_values
97 MODE_NONE,
98 MODE_DLL,
99 MODE_EXE,
100 MODE_DEF,
101 MODE_RELAY16,
102 MODE_RELAY32
105 static enum exec_mode_values exec_mode = MODE_NONE;
107 static const struct
109 const char *name;
110 enum target_platform platform;
111 } platform_names[] =
113 { "macos", PLATFORM_APPLE },
114 { "darwin", PLATFORM_APPLE },
115 { "solaris", PLATFORM_SOLARIS },
116 { "windows", PLATFORM_WINDOWS },
117 { "winnt", PLATFORM_WINDOWS }
120 /* set the dll file name from the input file name */
121 static void set_dll_file_name( const char *name, DLLSPEC *spec )
123 char *p;
125 if (spec->file_name) return;
127 if ((p = strrchr( name, '\\' ))) name = p + 1;
128 if ((p = strrchr( name, '/' ))) name = p + 1;
129 spec->file_name = xmalloc( strlen(name) + 5 );
130 strcpy( spec->file_name, name );
131 if ((p = strrchr( spec->file_name, '.' )))
133 if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
137 /* set the dll subsystem */
138 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
140 char *major, *minor, *str = xstrdup( subsystem );
142 if ((major = strchr( str, ':' ))) *major++ = 0;
143 if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
144 else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
145 else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
146 else if (!strcmp( str, "win16" )) spec->type = SPEC_WIN16;
147 else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
148 if (major)
150 if ((minor = strchr( major, '.' )))
152 *minor++ = 0;
153 spec->subsystem_minor = atoi( minor );
155 spec->subsystem_major = atoi( major );
157 free( str );
160 /* set the target CPU and platform */
161 static void set_target( const char *target )
163 unsigned int i;
164 char *p, *platform, *spec = xstrdup( target );
166 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
168 target_alias = xstrdup( target );
170 /* get the CPU part */
172 if (!(p = strchr( spec, '-' ))) fatal_error( "Invalid target specification '%s'\n", target );
173 *p++ = 0;
174 if ((target_cpu = get_cpu_from_name( spec )) == -1)
175 fatal_error( "Unrecognized CPU '%s'\n", spec );
176 platform = p;
177 if ((p = strrchr( p, '-' ))) platform = p + 1;
179 /* get the OS part */
181 target_platform = PLATFORM_UNSPECIFIED; /* default value */
182 for (i = 0; i < sizeof(platform_names)/sizeof(platform_names[0]); i++)
184 if (!strncmp( platform_names[i].name, platform, strlen(platform_names[i].name) ))
186 target_platform = platform_names[i].platform;
187 break;
191 free( spec );
194 /* cleanup on program exit */
195 static void cleanup(void)
197 if (output_file_name) unlink( output_file_name );
200 /* clean things up when aborting on a signal */
201 static void exit_on_signal( int sig )
203 exit(1); /* this will call atexit functions */
206 /*******************************************************************
207 * command-line option handling
209 static const char usage_str[] =
210 "Usage: winebuild [OPTIONS] [FILES]\n\n"
211 "Options:\n"
212 " --as-cmd=AS Command to use for assembling (default: as)\n"
213 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
214 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
215 " -D SYM Ignored for C flags compatibility\n"
216 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
217 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
218 " --external-symbols Allow linking to external symbols\n"
219 " -f FLAGS Compiler flags (only -fPIC is supported)\n"
220 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
221 " -h, --help Display this help message\n"
222 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
223 " -i, --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
224 " -I DIR Ignored for C flags compatibility\n"
225 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
226 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
227 " --ld-cmd=LD Command to use for linking (default: ld)\n"
228 " -l, --library=LIB Import the specified library\n"
229 " -L, --library-path=DIR Look for imports libraries in DIR\n"
230 " -m32, -m64 Force building 32-bit resp. 64-bit code\n"
231 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
232 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
233 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
234 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
235 " -o, --output=NAME Set the output file name (default: stdout)\n"
236 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
237 " --save-temps Do not delete the generated intermediate files\n"
238 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console)\n"
239 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
240 " -v, --verbose Display the programs invoked\n"
241 " --version Print the version and exit\n"
242 " -w, --warnings Turn on warnings\n"
243 "\nMode options:\n"
244 " --dll Build a .c file from a .spec or .def file\n"
245 " --def Build a .def file from a .spec file\n"
246 " --exe Build a .c file for an executable\n"
247 " --relay16 Build the 16-bit relay assembly routines\n"
248 " --relay32 Build the 32-bit relay assembly routines\n\n"
249 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
251 enum long_options_values
253 LONG_OPT_DLL = 1,
254 LONG_OPT_DEF,
255 LONG_OPT_EXE,
256 LONG_OPT_ASCMD,
257 LONG_OPT_EXTERNAL_SYMS,
258 LONG_OPT_LDCMD,
259 LONG_OPT_NMCMD,
260 LONG_OPT_NXCOMPAT,
261 LONG_OPT_RELAY16,
262 LONG_OPT_RELAY32,
263 LONG_OPT_SAVE_TEMPS,
264 LONG_OPT_SUBSYSTEM,
265 LONG_OPT_VERSION
268 static const char short_options[] = "C:D:E:F:H:I:K:L:M:N:b:d:e:f:hi:kl:m:o:r:u:vw";
270 static const struct option long_options[] =
272 { "dll", 0, 0, LONG_OPT_DLL },
273 { "def", 0, 0, LONG_OPT_DEF },
274 { "exe", 0, 0, LONG_OPT_EXE },
275 { "as-cmd", 1, 0, LONG_OPT_ASCMD },
276 { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS },
277 { "ld-cmd", 1, 0, LONG_OPT_LDCMD },
278 { "nm-cmd", 1, 0, LONG_OPT_NMCMD },
279 { "nxcompat", 1, 0, LONG_OPT_NXCOMPAT },
280 { "relay16", 0, 0, LONG_OPT_RELAY16 },
281 { "relay32", 0, 0, LONG_OPT_RELAY32 },
282 { "save-temps", 0, 0, LONG_OPT_SAVE_TEMPS },
283 { "subsystem", 1, 0, LONG_OPT_SUBSYSTEM },
284 { "version", 0, 0, LONG_OPT_VERSION },
285 /* aliases for short options */
286 { "target", 1, 0, 'b' },
287 { "delay-lib", 1, 0, 'd' },
288 { "export", 1, 0, 'E' },
289 { "entry", 1, 0, 'e' },
290 { "filename", 1, 0, 'F' },
291 { "help", 0, 0, 'h' },
292 { "heap", 1, 0, 'H' },
293 { "ignore", 1, 0, 'i' },
294 { "kill-at", 0, 0, 'k' },
295 { "library", 1, 0, 'l' },
296 { "library-path", 1, 0, 'L' },
297 { "main-module", 1, 0, 'M' },
298 { "dll-name", 1, 0, 'N' },
299 { "output", 1, 0, 'o' },
300 { "res", 1, 0, 'r' },
301 { "undefined", 1, 0, 'u' },
302 { "verbose", 0, 0, 'v' },
303 { "warnings", 0, 0, 'w' },
304 { NULL, 0, 0, 0 }
307 static void usage( int exit_code )
309 fprintf( stderr, "%s", usage_str );
310 exit( exit_code );
313 static void set_exec_mode( enum exec_mode_values mode )
315 if (exec_mode != MODE_NONE) usage(1);
316 exec_mode = mode;
319 /* parse options from the argv array and remove all the recognized ones */
320 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
322 char *p;
323 int optc;
325 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
327 switch(optc)
329 case 'D':
330 /* ignored */
331 break;
332 case 'E':
333 spec_file_name = xstrdup( optarg );
334 set_dll_file_name( optarg, spec );
335 break;
336 case 'F':
337 spec->file_name = xstrdup( optarg );
338 break;
339 case 'H':
340 if (!isdigit(optarg[0]))
341 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
342 spec->heap_size = atoi(optarg);
343 if (spec->heap_size > 65535)
344 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
345 break;
346 case 'I':
347 /* ignored */
348 break;
349 case 'K':
350 /* ignored, because cc generates correct code. */
351 break;
352 case 'L':
353 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
354 lib_path[nb_lib_paths++] = xstrdup( optarg );
355 break;
356 case 'm':
357 if (strcmp( optarg, "32" ) && strcmp( optarg, "64" ))
358 fatal_error( "Invalid -m option '%s', expected -m32 or -m64\n", optarg );
359 if (!strcmp( optarg, "32" )) force_pointer_size = 4;
360 else force_pointer_size = 8;
361 break;
362 case 'M':
363 spec->type = SPEC_WIN16;
364 main_module = xstrdup( optarg );
365 break;
366 case 'N':
367 spec->dll_name = xstrdup( optarg );
368 break;
369 case 'b':
370 set_target( optarg );
371 break;
372 case 'd':
373 add_delayed_import( optarg );
374 break;
375 case 'e':
376 spec->init_func = xstrdup( optarg );
377 if ((p = strchr( spec->init_func, '@' ))) *p = 0; /* kill stdcall decoration */
378 break;
379 case 'f':
380 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
381 /* ignore all other flags */
382 break;
383 case 'h':
384 usage(0);
385 break;
386 case 'i':
388 char *str = xstrdup( optarg );
389 char *token = strtok( str, "," );
390 while (token)
392 add_ignore_symbol( token );
393 token = strtok( NULL, "," );
395 free( str );
397 break;
398 case 'k':
399 kill_at = 1;
400 break;
401 case 'l':
402 add_import_dll( optarg, NULL );
403 break;
404 case 'o':
406 char *ext = strrchr( optarg, '.' );
408 if (unlink( optarg ) == -1 && errno != ENOENT)
409 fatal_error( "Unable to create output file '%s'\n", optarg );
410 if (ext && !strcmp( ext, ".o" ))
412 output_file_source_name = get_temp_file_name( optarg, ".s" );
413 if (!(output_file = fopen( output_file_source_name, "w" )))
414 fatal_error( "Unable to create output file '%s'\n", optarg );
416 else
418 if (!(output_file = fopen( optarg, "w" )))
419 fatal_error( "Unable to create output file '%s'\n", optarg );
421 output_file_name = xstrdup(optarg);
422 atexit( cleanup ); /* make sure we remove the output file on exit */
424 break;
425 case 'r':
426 res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
427 res_files[nb_res_files++] = xstrdup( optarg );
428 break;
429 case 'u':
430 add_extra_ld_symbol( optarg );
431 break;
432 case 'v':
433 verbose++;
434 break;
435 case 'w':
436 display_warnings = 1;
437 break;
438 case LONG_OPT_DLL:
439 set_exec_mode( MODE_DLL );
440 break;
441 case LONG_OPT_DEF:
442 set_exec_mode( MODE_DEF );
443 break;
444 case LONG_OPT_EXE:
445 set_exec_mode( MODE_EXE );
446 if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
447 break;
448 case LONG_OPT_ASCMD:
449 as_command = xstrdup( optarg );
450 break;
451 case LONG_OPT_EXTERNAL_SYMS:
452 link_ext_symbols = 1;
453 break;
454 case LONG_OPT_LDCMD:
455 ld_command = xstrdup( optarg );
456 break;
457 case LONG_OPT_NMCMD:
458 nm_command = xstrdup( optarg );
459 break;
460 case LONG_OPT_NXCOMPAT:
461 if (optarg[0] == 'n' || optarg[0] == 'N')
462 spec->dll_characteristics &= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
463 break;
464 case LONG_OPT_RELAY16:
465 set_exec_mode( MODE_RELAY16 );
466 break;
467 case LONG_OPT_RELAY32:
468 set_exec_mode( MODE_RELAY32 );
469 break;
470 case LONG_OPT_SAVE_TEMPS:
471 save_temps = 1;
472 break;
473 case LONG_OPT_SUBSYSTEM:
474 set_subsystem( optarg, spec );
475 break;
476 case LONG_OPT_VERSION:
477 printf( "winebuild version " PACKAGE_VERSION "\n" );
478 exit(0);
479 case '?':
480 usage(1);
481 break;
485 if (spec->file_name && !strchr( spec->file_name, '.' ))
486 strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
488 switch (target_cpu)
490 case CPU_x86:
491 if (force_pointer_size == 8) target_cpu = CPU_x86_64;
492 break;
493 case CPU_x86_64:
494 if (force_pointer_size == 4) target_cpu = CPU_x86;
495 break;
496 default:
497 if (force_pointer_size == 8)
498 fatal_error( "Cannot build 64-bit code for this CPU\n" );
499 break;
502 return &argv[optind];
506 /* load all specified resource files */
507 static void load_resources( char *argv[], DLLSPEC *spec )
509 int i;
510 char **ptr, **last;
512 switch (spec->type)
514 case SPEC_WIN16:
515 for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
516 break;
518 case SPEC_WIN32:
519 for (i = 0; i < nb_res_files; i++)
521 if (!load_res32_file( res_files[i], spec ))
522 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
525 /* load any resource file found in the remaining arguments */
526 for (ptr = last = argv; *ptr; ptr++)
528 if (!load_res32_file( *ptr, spec ))
529 *last++ = *ptr; /* not a resource file, keep it in the list */
531 *last = NULL;
532 break;
536 /* add input files that look like import libs to the import list */
537 static void load_import_libs( char *argv[] )
539 char **ptr, **last;
541 for (ptr = last = argv; *ptr; ptr++)
543 if (strendswith( *ptr, ".def" ))
544 add_import_dll( NULL, *ptr );
545 else
546 *last++ = *ptr; /* not an import dll, keep it in the list */
548 *last = NULL;
551 static int parse_input_file( DLLSPEC *spec )
553 FILE *input_file = open_input_file( NULL, spec_file_name );
554 char *extension = strrchr( spec_file_name, '.' );
555 int result;
557 spec->src_name = xstrdup( input_file_name );
558 if (extension && !strcmp( extension, ".def" ))
559 result = parse_def_file( input_file, spec );
560 else
561 result = parse_spec_file( input_file, spec );
562 close_input_file( input_file );
563 return result;
567 /*******************************************************************
568 * main
570 int main(int argc, char **argv)
572 DLLSPEC *spec = alloc_dll_spec();
574 #ifdef SIGHUP
575 signal( SIGHUP, exit_on_signal );
576 #endif
577 signal( SIGTERM, exit_on_signal );
578 signal( SIGINT, exit_on_signal );
580 output_file = stdout;
581 argv = parse_options( argc, argv, spec );
583 switch(exec_mode)
585 case MODE_DLL:
586 if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE)
587 spec->characteristics |= IMAGE_FILE_DLL;
588 if (!spec_file_name) fatal_error( "missing .spec file\n" );
589 /* fall through */
590 case MODE_EXE:
591 load_resources( argv, spec );
592 load_import_libs( argv );
593 if (spec_file_name && !parse_input_file( spec )) break;
594 switch (spec->type)
596 case SPEC_WIN16:
597 if (!main_module)
599 read_undef_symbols( spec, argv );
600 output_spec16_file( spec );
602 else
603 BuildSpec16File( spec );
604 break;
605 case SPEC_WIN32:
606 read_undef_symbols( spec, argv );
607 BuildSpec32File( spec );
608 break;
609 default: assert(0);
611 break;
612 case MODE_DEF:
613 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
614 if (spec->type == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
615 if (!spec_file_name) fatal_error( "missing .spec file\n" );
616 if (!parse_input_file( spec )) break;
617 BuildDef32File( spec );
618 break;
619 case MODE_RELAY16:
620 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
621 BuildRelays16();
622 break;
623 case MODE_RELAY32:
624 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
625 BuildRelays32();
626 break;
627 default:
628 usage(1);
629 break;
631 if (nb_errors) exit(1);
632 if (output_file_name)
634 if (fclose( output_file ) < 0) fatal_perror( "fclose" );
635 if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
636 output_file_name = NULL;
638 return 0;