ddraw: Validate structure pointers and sizes in IDirect3D3::FindDevice.
[wine/testsucceed.git] / tools / winebuild / main.c
blob0e662f817f17451bead4ceff62c9fcfb27f140c4
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 "build.h"
41 int UsePIC = 0;
42 int nb_lib_paths = 0;
43 int nb_errors = 0;
44 int display_warnings = 0;
45 int kill_at = 0;
46 int verbose = 0;
47 int save_temps = 0;
48 int link_ext_symbols = 0;
49 int force_pointer_size = 0;
50 int unwind_tables = 0;
52 #ifdef __i386__
53 enum target_cpu target_cpu = CPU_x86;
54 #elif defined(__x86_64__)
55 enum target_cpu target_cpu = CPU_x86_64;
56 #elif defined(__sparc__)
57 enum target_cpu target_cpu = CPU_SPARC;
58 #elif defined(__ALPHA__)
59 enum target_cpu target_cpu = CPU_ALPHA;
60 #elif defined(__powerpc__)
61 enum target_cpu target_cpu = CPU_POWERPC;
62 #elif defined(__arm__)
63 enum target_cpu target_cpu = CPU_ARM;
64 #else
65 #error Unsupported CPU
66 #endif
68 #ifdef __APPLE__
69 enum target_platform target_platform = PLATFORM_APPLE;
70 #elif defined(__FreeBSD__)
71 enum target_platform target_platform = PLATFORM_FREEBSD;
72 #elif defined(__sun)
73 enum target_platform target_platform = PLATFORM_SOLARIS;
74 #elif defined(_WIN32)
75 enum target_platform target_platform = PLATFORM_WINDOWS;
76 #else
77 enum target_platform target_platform = PLATFORM_UNSPECIFIED;
78 #endif
80 char *target_alias = NULL;
81 char **lib_path = NULL;
83 char *input_file_name = NULL;
84 char *spec_file_name = NULL;
85 FILE *output_file = NULL;
86 const char *output_file_name = NULL;
87 static const char *output_file_source_name;
88 static int fake_module;
90 char *as_command = NULL;
91 char *ld_command = NULL;
92 char *nm_command = NULL;
94 static int nb_res_files;
95 static char **res_files;
97 /* execution mode */
98 enum exec_mode_values
100 MODE_NONE,
101 MODE_DLL,
102 MODE_EXE,
103 MODE_DEF,
104 MODE_IMPLIB,
105 MODE_RESOURCES
108 static enum exec_mode_values exec_mode = MODE_NONE;
110 static const struct
112 const char *name;
113 enum target_platform platform;
114 } platform_names[] =
116 { "macos", PLATFORM_APPLE },
117 { "darwin", PLATFORM_APPLE },
118 { "freebsd", PLATFORM_FREEBSD },
119 { "solaris", PLATFORM_SOLARIS },
120 { "mingw32", PLATFORM_WINDOWS },
121 { "windows", PLATFORM_WINDOWS },
122 { "winnt", PLATFORM_WINDOWS }
125 /* set the dll file name from the input file name */
126 static void set_dll_file_name( const char *name, DLLSPEC *spec )
128 char *p;
130 if (spec->file_name) return;
132 if ((p = strrchr( name, '\\' ))) name = p + 1;
133 if ((p = strrchr( name, '/' ))) name = p + 1;
134 spec->file_name = xmalloc( strlen(name) + 5 );
135 strcpy( spec->file_name, name );
136 if ((p = strrchr( spec->file_name, '.' )))
138 if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
142 /* set the dll name from the file name */
143 static void init_dll_name( DLLSPEC *spec )
145 if (!spec->file_name)
147 char *p;
148 spec->file_name = xstrdup( output_file_name );
149 if ((p = strrchr( spec->file_name, '.' ))) *p = 0;
151 if (!spec->dll_name) /* set default name from file name */
153 char *p;
154 spec->dll_name = xstrdup( spec->file_name );
155 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
159 /* set the dll subsystem */
160 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
162 char *major, *minor, *str = xstrdup( subsystem );
164 if ((major = strchr( str, ':' ))) *major++ = 0;
165 if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
166 else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
167 else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
168 else if (!strcmp( str, "win16" )) spec->type = SPEC_WIN16;
169 else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
170 if (major)
172 if ((minor = strchr( major, '.' )))
174 *minor++ = 0;
175 spec->subsystem_minor = atoi( minor );
177 spec->subsystem_major = atoi( major );
179 free( str );
182 /* set the target CPU and platform */
183 static void set_target( const char *target )
185 unsigned int i;
186 char *p, *platform, *spec = xstrdup( target );
188 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
190 target_alias = xstrdup( target );
192 /* get the CPU part */
194 if (!(p = strchr( spec, '-' ))) fatal_error( "Invalid target specification '%s'\n", target );
195 *p++ = 0;
196 if ((target_cpu = get_cpu_from_name( spec )) == -1)
197 fatal_error( "Unrecognized CPU '%s'\n", spec );
198 platform = p;
199 if ((p = strrchr( p, '-' ))) platform = p + 1;
201 /* get the OS part */
203 target_platform = PLATFORM_UNSPECIFIED; /* default value */
204 for (i = 0; i < sizeof(platform_names)/sizeof(platform_names[0]); i++)
206 if (!strncmp( platform_names[i].name, platform, strlen(platform_names[i].name) ))
208 target_platform = platform_names[i].platform;
209 break;
213 free( spec );
216 /* cleanup on program exit */
217 static void cleanup(void)
219 if (output_file_name) unlink( output_file_name );
222 /* clean things up when aborting on a signal */
223 static void exit_on_signal( int sig )
225 exit(1); /* this will call atexit functions */
228 /*******************************************************************
229 * command-line option handling
231 static const char usage_str[] =
232 "Usage: winebuild [OPTIONS] [FILES]\n\n"
233 "Options:\n"
234 " --as-cmd=AS Command to use for assembling (default: as)\n"
235 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
236 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
237 " -D SYM Ignored for C flags compatibility\n"
238 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
239 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
240 " --external-symbols Allow linking to external symbols\n"
241 " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
242 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
243 " --fake-module Create a fake binary module\n"
244 " -h, --help Display this help message\n"
245 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
246 " -i, --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
247 " -I DIR Ignored for C flags compatibility\n"
248 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
249 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
250 " --large-address-aware Support an address space larger than 2Gb\n"
251 " --ld-cmd=LD Command to use for linking (default: ld)\n"
252 " -l, --library=LIB Import the specified library\n"
253 " -L, --library-path=DIR Look for imports libraries in DIR\n"
254 " -m32, -m64 Force building 32-bit resp. 64-bit code\n"
255 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
256 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
257 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
258 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
259 " -o, --output=NAME Set the output file name (default: stdout)\n"
260 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
261 " --save-temps Do not delete the generated intermediate files\n"
262 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console)\n"
263 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
264 " -v, --verbose Display the programs invoked\n"
265 " --version Print the version and exit\n"
266 " -w, --warnings Turn on warnings\n"
267 "\nMode options:\n"
268 " --dll Build a .c file from a .spec or .def file\n"
269 " --def Build a .def file from a .spec file\n"
270 " --exe Build a .c file for an executable\n"
271 " --implib Build an import library\n"
272 " --resources Build a .o file for the resource files\n\n"
273 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
275 enum long_options_values
277 LONG_OPT_DLL = 1,
278 LONG_OPT_DEF,
279 LONG_OPT_EXE,
280 LONG_OPT_IMPLIB,
281 LONG_OPT_ASCMD,
282 LONG_OPT_EXTERNAL_SYMS,
283 LONG_OPT_FAKE_MODULE,
284 LONG_OPT_LARGE_ADDRESS_AWARE,
285 LONG_OPT_LDCMD,
286 LONG_OPT_NMCMD,
287 LONG_OPT_NXCOMPAT,
288 LONG_OPT_RESOURCES,
289 LONG_OPT_SAVE_TEMPS,
290 LONG_OPT_SUBSYSTEM,
291 LONG_OPT_VERSION
294 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";
296 static const struct option long_options[] =
298 { "dll", 0, 0, LONG_OPT_DLL },
299 { "def", 0, 0, LONG_OPT_DEF },
300 { "exe", 0, 0, LONG_OPT_EXE },
301 { "implib", 0, 0, LONG_OPT_IMPLIB },
302 { "as-cmd", 1, 0, LONG_OPT_ASCMD },
303 { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS },
304 { "fake-module", 0, 0, LONG_OPT_FAKE_MODULE },
305 { "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE },
306 { "ld-cmd", 1, 0, LONG_OPT_LDCMD },
307 { "nm-cmd", 1, 0, LONG_OPT_NMCMD },
308 { "nxcompat", 1, 0, LONG_OPT_NXCOMPAT },
309 { "resources", 0, 0, LONG_OPT_RESOURCES },
310 { "save-temps", 0, 0, LONG_OPT_SAVE_TEMPS },
311 { "subsystem", 1, 0, LONG_OPT_SUBSYSTEM },
312 { "version", 0, 0, LONG_OPT_VERSION },
313 /* aliases for short options */
314 { "target", 1, 0, 'b' },
315 { "delay-lib", 1, 0, 'd' },
316 { "export", 1, 0, 'E' },
317 { "entry", 1, 0, 'e' },
318 { "filename", 1, 0, 'F' },
319 { "help", 0, 0, 'h' },
320 { "heap", 1, 0, 'H' },
321 { "ignore", 1, 0, 'i' },
322 { "kill-at", 0, 0, 'k' },
323 { "library", 1, 0, 'l' },
324 { "library-path", 1, 0, 'L' },
325 { "main-module", 1, 0, 'M' },
326 { "dll-name", 1, 0, 'N' },
327 { "output", 1, 0, 'o' },
328 { "res", 1, 0, 'r' },
329 { "undefined", 1, 0, 'u' },
330 { "verbose", 0, 0, 'v' },
331 { "warnings", 0, 0, 'w' },
332 { NULL, 0, 0, 0 }
335 static void usage( int exit_code )
337 fprintf( stderr, "%s", usage_str );
338 exit( exit_code );
341 static void set_exec_mode( enum exec_mode_values mode )
343 if (exec_mode != MODE_NONE) usage(1);
344 exec_mode = mode;
347 /* parse options from the argv array and remove all the recognized ones */
348 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
350 char *p;
351 int optc;
353 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
355 switch(optc)
357 case 'D':
358 /* ignored */
359 break;
360 case 'E':
361 spec_file_name = xstrdup( optarg );
362 set_dll_file_name( optarg, spec );
363 break;
364 case 'F':
365 spec->file_name = xstrdup( optarg );
366 break;
367 case 'H':
368 if (!isdigit(optarg[0]))
369 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
370 spec->heap_size = atoi(optarg);
371 if (spec->heap_size > 65535)
372 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
373 break;
374 case 'I':
375 /* ignored */
376 break;
377 case 'K':
378 /* ignored, because cc generates correct code. */
379 break;
380 case 'L':
381 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
382 lib_path[nb_lib_paths++] = xstrdup( optarg );
383 break;
384 case 'm':
385 if (strcmp( optarg, "32" ) && strcmp( optarg, "64" ))
386 fatal_error( "Invalid -m option '%s', expected -m32 or -m64\n", optarg );
387 if (!strcmp( optarg, "32" )) force_pointer_size = 4;
388 else force_pointer_size = 8;
389 break;
390 case 'M':
391 spec->main_module = xstrdup( optarg );
392 break;
393 case 'N':
394 spec->dll_name = xstrdup( optarg );
395 break;
396 case 'b':
397 set_target( optarg );
398 break;
399 case 'd':
400 add_delayed_import( optarg );
401 break;
402 case 'e':
403 spec->init_func = xstrdup( optarg );
404 if ((p = strchr( spec->init_func, '@' ))) *p = 0; /* kill stdcall decoration */
405 break;
406 case 'f':
407 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
408 else if (!strcmp( optarg, "asynchronous-unwind-tables")) unwind_tables = 1;
409 else if (!strcmp( optarg, "no-asynchronous-unwind-tables")) unwind_tables = 0;
410 /* ignore all other flags */
411 break;
412 case 'h':
413 usage(0);
414 break;
415 case 'i':
417 char *str = xstrdup( optarg );
418 char *token = strtok( str, "," );
419 while (token)
421 add_ignore_symbol( token );
422 token = strtok( NULL, "," );
424 free( str );
426 break;
427 case 'k':
428 kill_at = 1;
429 break;
430 case 'l':
431 add_import_dll( optarg, NULL );
432 break;
433 case 'o':
435 char *ext = strrchr( optarg, '.' );
437 if (unlink( optarg ) == -1 && errno != ENOENT)
438 fatal_error( "Unable to create output file '%s'\n", optarg );
439 if (ext && !strcmp( ext, ".o" ))
441 output_file_source_name = get_temp_file_name( optarg, ".s" );
442 if (!(output_file = fopen( output_file_source_name, "w" )))
443 fatal_error( "Unable to create output file '%s'\n", optarg );
445 else
447 if (!(output_file = fopen( optarg, "w" )))
448 fatal_error( "Unable to create output file '%s'\n", optarg );
450 output_file_name = xstrdup(optarg);
451 atexit( cleanup ); /* make sure we remove the output file on exit */
453 break;
454 case 'r':
455 res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
456 res_files[nb_res_files++] = xstrdup( optarg );
457 break;
458 case 'u':
459 add_extra_ld_symbol( optarg );
460 break;
461 case 'v':
462 verbose++;
463 break;
464 case 'w':
465 display_warnings = 1;
466 break;
467 case LONG_OPT_DLL:
468 set_exec_mode( MODE_DLL );
469 break;
470 case LONG_OPT_DEF:
471 set_exec_mode( MODE_DEF );
472 break;
473 case LONG_OPT_EXE:
474 set_exec_mode( MODE_EXE );
475 if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
476 break;
477 case LONG_OPT_IMPLIB:
478 set_exec_mode( MODE_IMPLIB );
479 break;
480 case LONG_OPT_ASCMD:
481 as_command = xstrdup( optarg );
482 break;
483 case LONG_OPT_FAKE_MODULE:
484 fake_module = 1;
485 break;
486 case LONG_OPT_EXTERNAL_SYMS:
487 link_ext_symbols = 1;
488 break;
489 case LONG_OPT_LARGE_ADDRESS_AWARE:
490 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
491 break;
492 case LONG_OPT_LDCMD:
493 ld_command = xstrdup( optarg );
494 break;
495 case LONG_OPT_NMCMD:
496 nm_command = xstrdup( optarg );
497 break;
498 case LONG_OPT_NXCOMPAT:
499 if (optarg[0] == 'n' || optarg[0] == 'N')
500 spec->dll_characteristics &= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
501 break;
502 case LONG_OPT_RESOURCES:
503 set_exec_mode( MODE_RESOURCES );
504 break;
505 case LONG_OPT_SAVE_TEMPS:
506 save_temps = 1;
507 break;
508 case LONG_OPT_SUBSYSTEM:
509 set_subsystem( optarg, spec );
510 break;
511 case LONG_OPT_VERSION:
512 printf( "winebuild version " PACKAGE_VERSION "\n" );
513 exit(0);
514 case '?':
515 usage(1);
516 break;
520 if (spec->file_name && !strchr( spec->file_name, '.' ))
521 strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
522 init_dll_name( spec );
524 switch (target_cpu)
526 case CPU_x86:
527 if (force_pointer_size == 8) target_cpu = CPU_x86_64;
528 break;
529 case CPU_x86_64:
530 if (force_pointer_size == 4) target_cpu = CPU_x86;
531 break;
532 default:
533 if (force_pointer_size == 8)
534 fatal_error( "Cannot build 64-bit code for this CPU\n" );
535 break;
538 return &argv[optind];
542 /* load all specified resource files */
543 static void load_resources( char *argv[], DLLSPEC *spec )
545 int i;
546 char **ptr, **last;
548 switch (spec->type)
550 case SPEC_WIN16:
551 for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
552 break;
554 case SPEC_WIN32:
555 for (i = 0; i < nb_res_files; i++)
557 if (!load_res32_file( res_files[i], spec ))
558 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
561 /* load any resource file found in the remaining arguments */
562 for (ptr = last = argv; *ptr; ptr++)
564 if (!load_res32_file( *ptr, spec ))
565 *last++ = *ptr; /* not a resource file, keep it in the list */
567 *last = NULL;
568 break;
572 /* add input files that look like import libs to the import list */
573 static void load_import_libs( char *argv[] )
575 char **ptr, **last;
577 for (ptr = last = argv; *ptr; ptr++)
579 if (strendswith( *ptr, ".def" ))
580 add_import_dll( NULL, *ptr );
581 else
582 *last++ = *ptr; /* not an import dll, keep it in the list */
584 *last = NULL;
587 static int parse_input_file( DLLSPEC *spec )
589 FILE *input_file = open_input_file( NULL, spec_file_name );
590 char *extension = strrchr( spec_file_name, '.' );
591 int result;
593 spec->src_name = xstrdup( input_file_name );
594 if (extension && !strcmp( extension, ".def" ))
595 result = parse_def_file( input_file, spec );
596 else
597 result = parse_spec_file( input_file, spec );
598 close_input_file( input_file );
599 return result;
603 /*******************************************************************
604 * main
606 int main(int argc, char **argv)
608 DLLSPEC *spec = alloc_dll_spec();
610 #ifdef SIGHUP
611 signal( SIGHUP, exit_on_signal );
612 #endif
613 signal( SIGTERM, exit_on_signal );
614 signal( SIGINT, exit_on_signal );
616 output_file = stdout;
617 argv = parse_options( argc, argv, spec );
619 switch(exec_mode)
621 case MODE_DLL:
622 if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE)
623 spec->characteristics |= IMAGE_FILE_DLL;
624 /* fall through */
625 case MODE_EXE:
626 load_resources( argv, spec );
627 load_import_libs( argv );
628 if (spec_file_name && !parse_input_file( spec )) break;
629 if (fake_module)
631 if (spec->type == SPEC_WIN16) output_fake_module16( spec );
632 else output_fake_module( spec );
633 break;
635 read_undef_symbols( spec, argv );
636 switch (spec->type)
638 case SPEC_WIN16:
639 output_spec16_file( spec );
640 break;
641 case SPEC_WIN32:
642 BuildSpec32File( spec );
643 break;
644 default: assert(0);
646 break;
647 case MODE_DEF:
648 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
649 if (!spec_file_name) fatal_error( "missing .spec file\n" );
650 if (!parse_input_file( spec )) break;
651 output_def_file( spec, 1 );
652 break;
653 case MODE_IMPLIB:
654 if (!spec_file_name) fatal_error( "missing .spec file\n" );
655 if (!parse_input_file( spec )) break;
656 output_import_lib( spec, argv );
657 break;
658 case MODE_RESOURCES:
659 load_resources( argv, spec );
660 output_res_o_file( spec );
661 break;
662 default:
663 usage(1);
664 break;
666 if (nb_errors) exit(1);
667 if (output_file_name)
669 if (fclose( output_file ) < 0) fatal_perror( "fclose" );
670 if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
671 output_file_name = NULL;
673 return 0;