dpnet/tests: Add a trailing '\n' to some ok() calls.
[wine.git] / tools / winebuild / main.c
blob807cf61a76b43ddcd3d158f126357b2927a4333c
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 link_ext_symbols = 0;
48 int force_pointer_size = 0;
49 int unwind_tables = 0;
51 #ifdef __i386__
52 enum target_cpu target_cpu = CPU_x86;
53 #elif defined(__x86_64__)
54 enum target_cpu target_cpu = CPU_x86_64;
55 #elif defined(__powerpc__)
56 enum target_cpu target_cpu = CPU_POWERPC;
57 #elif defined(__arm__)
58 enum target_cpu target_cpu = CPU_ARM;
59 #elif defined(__aarch64__)
60 enum target_cpu target_cpu = CPU_ARM64;
61 #else
62 #error Unsupported CPU
63 #endif
65 #ifdef __APPLE__
66 enum target_platform target_platform = PLATFORM_APPLE;
67 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
68 enum target_platform target_platform = PLATFORM_FREEBSD;
69 #elif defined(__sun)
70 enum target_platform target_platform = PLATFORM_SOLARIS;
71 #elif defined(_WIN32)
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 int fake_module;
87 struct strarray *as_command = NULL;
88 struct strarray *cc_command = NULL;
89 struct strarray *ld_command = NULL;
90 struct strarray *nm_command = NULL;
91 char *cpu_option = NULL;
93 #ifdef __thumb__
94 int thumb_mode = 1;
95 #else
96 int thumb_mode = 0;
97 #endif
99 static int nb_res_files;
100 static char **res_files;
102 /* execution mode */
103 enum exec_mode_values
105 MODE_NONE,
106 MODE_DLL,
107 MODE_EXE,
108 MODE_DEF,
109 MODE_IMPLIB,
110 MODE_RESOURCES
113 static enum exec_mode_values exec_mode = MODE_NONE;
115 static const struct
117 const char *name;
118 enum target_platform platform;
119 } platform_names[] =
121 { "macos", PLATFORM_APPLE },
122 { "darwin", PLATFORM_APPLE },
123 { "freebsd", PLATFORM_FREEBSD },
124 { "solaris", PLATFORM_SOLARIS },
125 { "mingw32", PLATFORM_WINDOWS },
126 { "windows", PLATFORM_WINDOWS },
127 { "winnt", PLATFORM_WINDOWS }
130 /* set the dll file name from the input file name */
131 static void set_dll_file_name( const char *name, DLLSPEC *spec )
133 char *p;
135 if (spec->file_name) return;
137 if ((p = strrchr( name, '\\' ))) name = p + 1;
138 if ((p = strrchr( name, '/' ))) name = p + 1;
139 spec->file_name = xmalloc( strlen(name) + 5 );
140 strcpy( spec->file_name, name );
141 if ((p = strrchr( spec->file_name, '.' )))
143 if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
147 /* set the dll name from the file name */
148 static void init_dll_name( DLLSPEC *spec )
150 if (!spec->file_name && output_file_name)
152 char *p;
153 spec->file_name = xstrdup( output_file_name );
154 if ((p = strrchr( spec->file_name, '.' ))) *p = 0;
156 if (!spec->dll_name && spec->file_name) /* set default name from file name */
158 char *p;
159 spec->dll_name = xstrdup( spec->file_name );
160 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
164 /* set the dll subsystem */
165 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
167 char *major, *minor, *str = xstrdup( subsystem );
169 if ((major = strchr( str, ':' ))) *major++ = 0;
170 if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
171 else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
172 else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
173 else if (!strcmp( str, "wince" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CE_GUI;
174 else if (!strcmp( str, "win16" )) spec->type = SPEC_WIN16;
175 else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
176 if (major)
178 if ((minor = strchr( major, '.' )))
180 *minor++ = 0;
181 spec->subsystem_minor = atoi( minor );
183 spec->subsystem_major = atoi( major );
185 free( str );
188 /* set the target CPU and platform */
189 static void set_target( const char *target )
191 unsigned int i;
192 char *p, *platform, *spec = xstrdup( target );
194 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
196 target_alias = xstrdup( target );
198 /* get the CPU part */
200 if ((p = strchr( spec, '-' )))
202 *p++ = 0;
203 if ((target_cpu = get_cpu_from_name( spec )) == -1)
204 fatal_error( "Unrecognized CPU '%s'\n", spec );
205 platform = p;
206 if ((p = strrchr( p, '-' ))) platform = p + 1;
208 else if (!strcmp( spec, "mingw32" ))
210 target_cpu = CPU_x86;
211 platform = spec;
213 else
214 fatal_error( "Invalid target specification '%s'\n", target );
216 /* get the OS part */
218 target_platform = PLATFORM_UNSPECIFIED; /* default value */
219 for (i = 0; i < sizeof(platform_names)/sizeof(platform_names[0]); i++)
221 if (!strncmp( platform_names[i].name, platform, strlen(platform_names[i].name) ))
223 target_platform = platform_names[i].platform;
224 break;
228 free( spec );
231 /* cleanup on program exit */
232 static void cleanup(void)
234 if (output_file_name) unlink( output_file_name );
237 /* clean things up when aborting on a signal */
238 static void exit_on_signal( int sig )
240 exit(1); /* this will call atexit functions */
243 /*******************************************************************
244 * command-line option handling
246 static const char usage_str[] =
247 "Usage: winebuild [OPTIONS] [FILES]\n\n"
248 "Options:\n"
249 " --as-cmd=AS Command to use for assembling (default: as)\n"
250 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
251 " --cc-cmd=CC C compiler to use for assembling (default: fall back to --as-cmd)\n"
252 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
253 " -D SYM Ignored for C flags compatibility\n"
254 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
255 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
256 " --external-symbols Allow linking to external symbols\n"
257 " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
258 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
259 " --fake-module Create a fake binary module\n"
260 " -h, --help Display this help message\n"
261 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
262 " -I DIR Ignored for C flags compatibility\n"
263 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
264 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
265 " --large-address-aware Support an address space larger than 2Gb\n"
266 " --ld-cmd=LD Command to use for linking (default: ld)\n"
267 " -l, --library=LIB Import the specified library\n"
268 " -L, --library-path=DIR Look for imports libraries in DIR\n"
269 " -m16, -m32, -m64 Force building 16-bit, 32-bit resp. 64-bit code\n"
270 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
271 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
272 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
273 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
274 " -o, --output=NAME Set the output file name (default: stdout)\n"
275 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
276 " --save-temps Do not delete the generated intermediate files\n"
277 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console, wince)\n"
278 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
279 " -v, --verbose Display the programs invoked\n"
280 " --version Print the version and exit\n"
281 " -w, --warnings Turn on warnings\n"
282 "\nMode options:\n"
283 " --dll Build a .c file from a .spec or .def file\n"
284 " --def Build a .def file from a .spec file\n"
285 " --exe Build a .c file for an executable\n"
286 " --implib Build an import library\n"
287 " --resources Build a .o file for the resource files\n\n"
288 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
290 enum long_options_values
292 LONG_OPT_DLL = 1,
293 LONG_OPT_DEF,
294 LONG_OPT_EXE,
295 LONG_OPT_IMPLIB,
296 LONG_OPT_ASCMD,
297 LONG_OPT_CCCMD,
298 LONG_OPT_EXTERNAL_SYMS,
299 LONG_OPT_FAKE_MODULE,
300 LONG_OPT_LARGE_ADDRESS_AWARE,
301 LONG_OPT_LDCMD,
302 LONG_OPT_NMCMD,
303 LONG_OPT_NXCOMPAT,
304 LONG_OPT_RESOURCES,
305 LONG_OPT_SAVE_TEMPS,
306 LONG_OPT_SUBSYSTEM,
307 LONG_OPT_VERSION
310 static const char short_options[] = "C:D:E:F:H:I:K:L:M:N:b:d:e:f:hkl:m:o:r:u:vw";
312 static const struct option long_options[] =
314 { "dll", 0, 0, LONG_OPT_DLL },
315 { "def", 0, 0, LONG_OPT_DEF },
316 { "exe", 0, 0, LONG_OPT_EXE },
317 { "implib", 0, 0, LONG_OPT_IMPLIB },
318 { "as-cmd", 1, 0, LONG_OPT_ASCMD },
319 { "cc-cmd", 1, 0, LONG_OPT_CCCMD },
320 { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS },
321 { "fake-module", 0, 0, LONG_OPT_FAKE_MODULE },
322 { "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE },
323 { "ld-cmd", 1, 0, LONG_OPT_LDCMD },
324 { "nm-cmd", 1, 0, LONG_OPT_NMCMD },
325 { "nxcompat", 1, 0, LONG_OPT_NXCOMPAT },
326 { "resources", 0, 0, LONG_OPT_RESOURCES },
327 { "save-temps", 0, 0, LONG_OPT_SAVE_TEMPS },
328 { "subsystem", 1, 0, LONG_OPT_SUBSYSTEM },
329 { "version", 0, 0, LONG_OPT_VERSION },
330 /* aliases for short options */
331 { "target", 1, 0, 'b' },
332 { "delay-lib", 1, 0, 'd' },
333 { "export", 1, 0, 'E' },
334 { "entry", 1, 0, 'e' },
335 { "filename", 1, 0, 'F' },
336 { "help", 0, 0, 'h' },
337 { "heap", 1, 0, 'H' },
338 { "kill-at", 0, 0, 'k' },
339 { "library", 1, 0, 'l' },
340 { "library-path", 1, 0, 'L' },
341 { "main-module", 1, 0, 'M' },
342 { "dll-name", 1, 0, 'N' },
343 { "output", 1, 0, 'o' },
344 { "res", 1, 0, 'r' },
345 { "undefined", 1, 0, 'u' },
346 { "verbose", 0, 0, 'v' },
347 { "warnings", 0, 0, 'w' },
348 { NULL, 0, 0, 0 }
351 static void usage( int exit_code )
353 fprintf( stderr, "%s", usage_str );
354 exit( exit_code );
357 static void set_exec_mode( enum exec_mode_values mode )
359 if (exec_mode != MODE_NONE) usage(1);
360 exec_mode = mode;
363 /* parse options from the argv array and remove all the recognized ones */
364 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
366 char *p;
367 int optc;
368 int save_temps = 0;
370 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
372 switch(optc)
374 case 'D':
375 /* ignored */
376 break;
377 case 'E':
378 spec_file_name = xstrdup( optarg );
379 set_dll_file_name( optarg, spec );
380 break;
381 case 'F':
382 spec->file_name = xstrdup( optarg );
383 break;
384 case 'H':
385 if (!isdigit(optarg[0]))
386 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
387 spec->heap_size = atoi(optarg);
388 if (spec->heap_size > 65535)
389 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
390 break;
391 case 'I':
392 /* ignored */
393 break;
394 case 'K':
395 /* ignored, because cc generates correct code. */
396 break;
397 case 'L':
398 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
399 lib_path[nb_lib_paths++] = xstrdup( optarg );
400 break;
401 case 'm':
402 if (!strcmp( optarg, "16" )) spec->type = SPEC_WIN16;
403 else if (!strcmp( optarg, "32" )) force_pointer_size = 4;
404 else if (!strcmp( optarg, "64" )) force_pointer_size = 8;
405 else if (!strcmp( optarg, "arm" )) thumb_mode = 0;
406 else if (!strcmp( optarg, "thumb" )) thumb_mode = 1;
407 else if (!strncmp( optarg, "cpu=", 4 )) cpu_option = xstrdup( optarg + 4 );
408 else fatal_error( "Unknown -m option '%s'\n", optarg );
409 break;
410 case 'M':
411 spec->main_module = xstrdup( optarg );
412 break;
413 case 'N':
414 spec->dll_name = xstrdup( optarg );
415 break;
416 case 'b':
417 set_target( optarg );
418 break;
419 case 'd':
420 add_delayed_import( optarg );
421 break;
422 case 'e':
423 spec->init_func = xstrdup( optarg );
424 if ((p = strchr( spec->init_func, '@' ))) *p = 0; /* kill stdcall decoration */
425 break;
426 case 'f':
427 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
428 else if (!strcmp( optarg, "asynchronous-unwind-tables")) unwind_tables = 1;
429 else if (!strcmp( optarg, "no-asynchronous-unwind-tables")) unwind_tables = 0;
430 /* ignore all other flags */
431 break;
432 case 'h':
433 usage(0);
434 break;
435 case 'k':
436 kill_at = 1;
437 break;
438 case 'l':
439 add_import_dll( optarg, NULL );
440 break;
441 case 'o':
443 char *ext = strrchr( optarg, '.' );
445 if (unlink( optarg ) == -1 && errno != ENOENT)
446 fatal_error( "Unable to create output file '%s'\n", optarg );
447 if (ext && !strcmp( ext, ".o" ))
449 output_file_source_name = get_temp_file_name( optarg, ".s" );
450 if (!(output_file = fopen( output_file_source_name, "w" )))
451 fatal_error( "Unable to create output file '%s'\n", optarg );
453 else
455 if (!(output_file = fopen( optarg, "w" )))
456 fatal_error( "Unable to create output file '%s'\n", optarg );
458 output_file_name = xstrdup(optarg);
459 atexit( cleanup ); /* make sure we remove the output file on exit */
461 break;
462 case 'r':
463 res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
464 res_files[nb_res_files++] = xstrdup( optarg );
465 break;
466 case 'u':
467 add_extra_ld_symbol( optarg );
468 break;
469 case 'v':
470 verbose++;
471 break;
472 case 'w':
473 display_warnings = 1;
474 break;
475 case LONG_OPT_DLL:
476 set_exec_mode( MODE_DLL );
477 break;
478 case LONG_OPT_DEF:
479 set_exec_mode( MODE_DEF );
480 break;
481 case LONG_OPT_EXE:
482 set_exec_mode( MODE_EXE );
483 if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
484 break;
485 case LONG_OPT_IMPLIB:
486 set_exec_mode( MODE_IMPLIB );
487 break;
488 case LONG_OPT_ASCMD:
489 as_command = strarray_fromstring( optarg, " " );
490 break;
491 case LONG_OPT_CCCMD:
492 cc_command = strarray_fromstring( optarg, " " );
493 break;
494 case LONG_OPT_FAKE_MODULE:
495 fake_module = 1;
496 break;
497 case LONG_OPT_EXTERNAL_SYMS:
498 link_ext_symbols = 1;
499 break;
500 case LONG_OPT_LARGE_ADDRESS_AWARE:
501 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
502 break;
503 case LONG_OPT_LDCMD:
504 ld_command = strarray_fromstring( optarg, " " );
505 break;
506 case LONG_OPT_NMCMD:
507 nm_command = strarray_fromstring( optarg, " " );
508 break;
509 case LONG_OPT_NXCOMPAT:
510 if (optarg[0] == 'n' || optarg[0] == 'N')
511 spec->dll_characteristics &= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
512 break;
513 case LONG_OPT_RESOURCES:
514 set_exec_mode( MODE_RESOURCES );
515 break;
516 case LONG_OPT_SAVE_TEMPS:
517 save_temps = 1;
518 break;
519 case LONG_OPT_SUBSYSTEM:
520 set_subsystem( optarg, spec );
521 break;
522 case LONG_OPT_VERSION:
523 printf( "winebuild version " PACKAGE_VERSION "\n" );
524 exit(0);
525 case '?':
526 usage(1);
527 break;
531 if (!save_temps) atexit( cleanup_tmp_files );
533 if (spec->file_name && !strchr( spec->file_name, '.' ))
534 strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
535 init_dll_name( spec );
537 switch (target_cpu)
539 case CPU_x86:
540 if (force_pointer_size == 8) target_cpu = CPU_x86_64;
541 break;
542 case CPU_x86_64:
543 if (force_pointer_size == 4) target_cpu = CPU_x86;
544 break;
545 default:
546 if (force_pointer_size == 8)
547 fatal_error( "Cannot build 64-bit code for this CPU\n" );
548 break;
551 return &argv[optind];
555 /* load all specified resource files */
556 static void load_resources( char *argv[], DLLSPEC *spec )
558 int i;
559 char **ptr, **last;
561 switch (spec->type)
563 case SPEC_WIN16:
564 for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
565 break;
567 case SPEC_WIN32:
568 for (i = 0; i < nb_res_files; i++)
570 if (!load_res32_file( res_files[i], spec ))
571 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
574 /* load any resource file found in the remaining arguments */
575 for (ptr = last = argv; *ptr; ptr++)
577 if (!load_res32_file( *ptr, spec ))
578 *last++ = *ptr; /* not a resource file, keep it in the list */
580 *last = NULL;
581 break;
585 /* add input files that look like import libs to the import list */
586 static void load_import_libs( char *argv[] )
588 char **ptr, **last;
590 for (ptr = last = argv; *ptr; ptr++)
592 if (strendswith( *ptr, ".def" ))
593 add_import_dll( NULL, *ptr );
594 else
595 *last++ = *ptr; /* not an import dll, keep it in the list */
597 *last = NULL;
600 static int parse_input_file( DLLSPEC *spec )
602 FILE *input_file = open_input_file( NULL, spec_file_name );
603 char *extension = strrchr( spec_file_name, '.' );
604 int result;
606 spec->src_name = xstrdup( input_file_name );
607 if (extension && !strcmp( extension, ".def" ))
608 result = parse_def_file( input_file, spec );
609 else
610 result = parse_spec_file( input_file, spec );
611 close_input_file( input_file );
612 return result;
616 /*******************************************************************
617 * main
619 int main(int argc, char **argv)
621 DLLSPEC *spec = alloc_dll_spec();
623 #ifdef SIGHUP
624 signal( SIGHUP, exit_on_signal );
625 #endif
626 signal( SIGTERM, exit_on_signal );
627 signal( SIGINT, exit_on_signal );
629 output_file = stdout;
630 argv = parse_options( argc, argv, spec );
632 switch(exec_mode)
634 case MODE_DLL:
635 if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE)
636 spec->characteristics |= IMAGE_FILE_DLL;
637 /* fall through */
638 case MODE_EXE:
639 load_resources( argv, spec );
640 load_import_libs( argv );
641 if (spec_file_name && !parse_input_file( spec )) break;
642 if (fake_module)
644 if (spec->type == SPEC_WIN16) output_fake_module16( spec );
645 else output_fake_module( spec );
646 break;
648 read_undef_symbols( spec, argv );
649 switch (spec->type)
651 case SPEC_WIN16:
652 output_spec16_file( spec );
653 break;
654 case SPEC_WIN32:
655 BuildSpec32File( spec );
656 break;
657 default: assert(0);
659 break;
660 case MODE_DEF:
661 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
662 if (!spec_file_name) fatal_error( "missing .spec file\n" );
663 if (!parse_input_file( spec )) break;
664 output_def_file( spec, 1 );
665 break;
666 case MODE_IMPLIB:
667 if (!spec_file_name) fatal_error( "missing .spec file\n" );
668 if (!parse_input_file( spec )) break;
669 output_import_lib( spec, argv );
670 break;
671 case MODE_RESOURCES:
672 load_resources( argv, spec );
673 output_res_o_file( spec );
674 break;
675 default:
676 usage(1);
677 break;
679 if (nb_errors) exit(1);
680 if (output_file_name)
682 if (fclose( output_file ) < 0) fatal_perror( "fclose" );
683 if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
684 output_file_name = NULL;
686 return 0;