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
26 #include "wine/port.h"
43 int display_warnings
= 0;
46 int link_ext_symbols
= 0;
47 int force_pointer_size
= 0;
48 int unwind_tables
= 0;
52 int prefer_native
= 0;
55 enum target_cpu target_cpu
= CPU_x86
;
56 #elif defined(__x86_64__)
57 enum target_cpu target_cpu
= CPU_x86_64
;
58 #elif defined(__powerpc__)
59 enum target_cpu target_cpu
= CPU_POWERPC
;
60 #elif defined(__arm__)
61 enum target_cpu target_cpu
= CPU_ARM
;
62 #elif defined(__aarch64__)
63 enum target_cpu target_cpu
= CPU_ARM64
;
65 #error Unsupported CPU
69 enum target_platform target_platform
= PLATFORM_APPLE
;
70 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
71 enum target_platform target_platform
= PLATFORM_FREEBSD
;
73 enum target_platform target_platform
= PLATFORM_SOLARIS
;
75 enum target_platform target_platform
= PLATFORM_MINGW
;
77 enum target_platform target_platform
= PLATFORM_UNSPECIFIED
;
80 char *target_alias
= NULL
;
82 char *input_file_name
= NULL
;
83 char *spec_file_name
= NULL
;
84 FILE *output_file
= NULL
;
85 const char *output_file_name
= NULL
;
86 static int fake_module
;
88 struct strarray lib_path
= { 0 };
89 struct strarray tools_path
= { 0 };
90 struct strarray as_command
= { 0 };
91 struct strarray cc_command
= { 0 };
92 struct strarray ld_command
= { 0 };
93 struct strarray nm_command
= { 0 };
94 char *cpu_option
= NULL
;
95 char *fpu_option
= NULL
;
96 char *arch_option
= NULL
;
98 const char *float_abi_option
= "soft";
100 const char *float_abi_option
= "softfp";
109 static struct strarray res_files
;
112 enum exec_mode_values
125 static enum exec_mode_values exec_mode
= MODE_NONE
;
130 enum target_platform platform
;
133 { "macos", PLATFORM_APPLE
},
134 { "darwin", PLATFORM_APPLE
},
135 { "freebsd", PLATFORM_FREEBSD
},
136 { "solaris", PLATFORM_SOLARIS
},
137 { "mingw32", PLATFORM_MINGW
},
138 { "windows-gnu", PLATFORM_MINGW
},
139 { "windows", PLATFORM_WINDOWS
},
140 { "winnt", PLATFORM_MINGW
}
143 /* set the dll file name from the input file name */
144 static void set_dll_file_name( const char *name
, DLLSPEC
*spec
)
148 if (spec
->file_name
) return;
150 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
151 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
152 spec
->file_name
= xmalloc( strlen(name
) + 5 );
153 strcpy( spec
->file_name
, name
);
154 if ((p
= strrchr( spec
->file_name
, '.' )))
156 if (!strcmp( p
, ".spec" ) || !strcmp( p
, ".def" )) *p
= 0;
160 /* set the dll name from the file name */
161 static void init_dll_name( DLLSPEC
*spec
)
163 if (!spec
->file_name
&& output_file_name
)
166 spec
->file_name
= xstrdup( output_file_name
);
167 if ((p
= strrchr( spec
->file_name
, '.' ))) *p
= 0;
169 if (!spec
->dll_name
&& spec
->file_name
) /* set default name from file name */
172 spec
->dll_name
= xstrdup( spec
->file_name
);
173 if ((p
= strrchr( spec
->dll_name
, '.' ))) *p
= 0;
175 if (spec
->dll_name
) spec
->c_name
= make_c_identifier( spec
->dll_name
);
178 /* set the dll subsystem */
179 static void set_subsystem( const char *subsystem
, DLLSPEC
*spec
)
181 char *major
, *minor
, *str
= xstrdup( subsystem
);
183 if ((major
= strchr( str
, ':' ))) *major
++ = 0;
184 if (!strcmp( str
, "native" )) spec
->subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
185 else if (!strcmp( str
, "windows" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
186 else if (!strcmp( str
, "console" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
187 else if (!strcmp( str
, "wince" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
;
188 else if (!strcmp( str
, "win16" )) spec
->type
= SPEC_WIN16
;
189 else fatal_error( "Invalid subsystem name '%s'\n", subsystem
);
192 if ((minor
= strchr( major
, '.' )))
195 spec
->subsystem_minor
= atoi( minor
);
197 spec
->subsystem_major
= atoi( major
);
202 /* set the target CPU and platform */
203 static void set_target( const char *target
)
206 char *p
, *spec
= xstrdup( target
);
208 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
210 target_alias
= xstrdup( target
);
212 /* get the CPU part */
214 if ((p
= strchr( spec
, '-' )))
219 cpu
= get_cpu_from_name( spec
);
220 if (cpu
== -1) fatal_error( "Unrecognized CPU '%s'\n", spec
);
223 else if (!strcmp( spec
, "mingw32" ))
225 target_cpu
= CPU_x86
;
229 fatal_error( "Invalid target specification '%s'\n", target
);
231 /* get the OS part */
233 target_platform
= PLATFORM_UNSPECIFIED
; /* default value */
236 for (i
= 0; i
< ARRAY_SIZE(platform_names
); i
++)
238 if (!strncmp( platform_names
[i
].name
, p
, strlen(platform_names
[i
].name
) ))
240 target_platform
= platform_names
[i
].platform
;
244 if (target_platform
!= PLATFORM_UNSPECIFIED
|| !(p
= strchr( p
, '-' ))) break;
250 if (target_cpu
== CPU_ARM
&& is_pe()) thumb_mode
= 1;
253 /* cleanup on program exit */
254 static void cleanup(void)
256 if (output_file_name
) unlink( output_file_name
);
259 /* clean things up when aborting on a signal */
260 static void exit_on_signal( int sig
)
262 exit(1); /* this will call atexit functions */
265 /*******************************************************************
266 * command-line option handling
268 static const char usage_str
[] =
269 "Usage: winebuild [OPTIONS] [FILES]\n\n"
271 " --as-cmd=AS Command to use for assembling (default: as)\n"
272 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
273 " -B PREFIX Look for build tools in the PREFIX directory\n"
274 " --cc-cmd=CC C compiler to use for assembling (default: fall back to --as-cmd)\n"
275 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
276 " -D SYM Ignored for C flags compatibility\n"
277 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
278 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
279 " --external-symbols Allow linking to external symbols\n"
280 " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
281 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
282 " --fake-module Create a fake binary module\n"
283 " -h, --help Display this help message\n"
284 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
285 " -I DIR Ignored for C flags compatibility\n"
286 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
287 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
288 " --large-address-aware Support an address space larger than 2Gb\n"
289 " --ld-cmd=LD Command to use for linking (default: ld)\n"
290 " -l, --library=LIB Import the specified library\n"
291 " -L, --library-path=DIR Look for imports libraries in DIR\n"
292 " -m16, -m32, -m64 Force building 16-bit, 32-bit resp. 64-bit code\n"
293 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
294 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
295 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
296 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
297 " -o, --output=NAME Set the output file name (default: stdout)\n"
298 " --prefer-native Set the flag to prefer loading native at run time\n"
299 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
300 " --safeseh Mark object files as SEH compatible\n"
301 " --save-temps Do not delete the generated intermediate files\n"
302 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console, wince)\n"
303 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
304 " -v, --verbose Display the programs invoked\n"
305 " --version Print the version and exit\n"
306 " -w, --warnings Turn on warnings\n"
308 " --dll Build a library from a .spec file and object files\n"
309 " --def Build a .def file from a .spec file\n"
310 " --exe Build an executable from object files\n"
311 " --implib Build an import library\n"
312 " --staticlib Build a static library\n"
313 " --resources Build a .o or .res file for the resource files\n\n"
314 " --builtin Mark a library as a Wine builtin\n"
315 " --fixup-ctors Fixup the constructors data after the module has been built\n"
316 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
318 enum long_options_values
327 LONG_OPT_EXTERNAL_SYMS
,
328 LONG_OPT_FAKE_MODULE
,
329 LONG_OPT_FIXUP_CTORS
,
330 LONG_OPT_LARGE_ADDRESS_AWARE
,
334 LONG_OPT_PREFER_NATIVE
,
343 static const char short_options
[] = "B:C:D:E:F:H:I:K:L:M:N:b:d:e:f:hkl:m:o:r:u:vw";
345 static const struct option long_options
[] =
347 { "dll", 0, 0, LONG_OPT_DLL
},
348 { "def", 0, 0, LONG_OPT_DEF
},
349 { "exe", 0, 0, LONG_OPT_EXE
},
350 { "implib", 0, 0, LONG_OPT_IMPLIB
},
351 { "staticlib", 0, 0, LONG_OPT_STATICLIB
},
352 { "builtin", 0, 0, LONG_OPT_BUILTIN
},
353 { "as-cmd", 1, 0, LONG_OPT_ASCMD
},
354 { "cc-cmd", 1, 0, LONG_OPT_CCCMD
},
355 { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS
},
356 { "fake-module", 0, 0, LONG_OPT_FAKE_MODULE
},
357 { "fixup-ctors", 0, 0, LONG_OPT_FIXUP_CTORS
},
358 { "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE
},
359 { "ld-cmd", 1, 0, LONG_OPT_LDCMD
},
360 { "nm-cmd", 1, 0, LONG_OPT_NMCMD
},
361 { "nxcompat", 1, 0, LONG_OPT_NXCOMPAT
},
362 { "prefer-native", 0, 0, LONG_OPT_PREFER_NATIVE
},
363 { "resources", 0, 0, LONG_OPT_RESOURCES
},
364 { "safeseh", 0, 0, LONG_OPT_SAFE_SEH
},
365 { "save-temps", 0, 0, LONG_OPT_SAVE_TEMPS
},
366 { "subsystem", 1, 0, LONG_OPT_SUBSYSTEM
},
367 { "version", 0, 0, LONG_OPT_VERSION
},
368 /* aliases for short options */
369 { "target", 1, 0, 'b' },
370 { "delay-lib", 1, 0, 'd' },
371 { "export", 1, 0, 'E' },
372 { "entry", 1, 0, 'e' },
373 { "filename", 1, 0, 'F' },
374 { "help", 0, 0, 'h' },
375 { "heap", 1, 0, 'H' },
376 { "kill-at", 0, 0, 'k' },
377 { "library", 1, 0, 'l' },
378 { "library-path", 1, 0, 'L' },
379 { "main-module", 1, 0, 'M' },
380 { "dll-name", 1, 0, 'N' },
381 { "output", 1, 0, 'o' },
382 { "res", 1, 0, 'r' },
383 { "undefined", 1, 0, 'u' },
384 { "verbose", 0, 0, 'v' },
385 { "warnings", 0, 0, 'w' },
389 static void usage( int exit_code
)
391 fprintf( stderr
, "%s", usage_str
);
395 static void set_exec_mode( enum exec_mode_values mode
)
397 if (exec_mode
!= MODE_NONE
) usage(1);
401 /* get the default entry point for a given spec file */
402 static const char *get_default_entry_point( const DLLSPEC
*spec
)
404 if (spec
->characteristics
& IMAGE_FILE_DLL
) return "DllMain";
405 if (spec
->subsystem
== IMAGE_SUBSYSTEM_NATIVE
) return "DriverEntry";
406 if (spec
->type
== SPEC_WIN16
)
408 add_spec_extra_ld_symbol("WinMain16");
409 return "__wine_spec_exe16_entry";
411 else if (spec
->unicode_app
)
413 /* __wine_spec_exe_wentry always calls wmain */
414 add_spec_extra_ld_symbol("wmain");
415 if (spec
->subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
416 add_spec_extra_ld_symbol("wWinMain");
417 return "__wine_spec_exe_wentry";
421 /* __wine_spec_exe_entry always calls main */
422 add_spec_extra_ld_symbol("main");
423 if (spec
->subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
424 add_spec_extra_ld_symbol("WinMain");
425 return "__wine_spec_exe_entry";
429 /* parse options from the argv array and remove all the recognized ones */
430 static char **parse_options( int argc
, char **argv
, DLLSPEC
*spec
)
436 while ((optc
= getopt_long( argc
, argv
, short_options
, long_options
, NULL
)) != -1)
441 strarray_add( &tools_path
, xstrdup( optarg
), NULL
);
447 spec_file_name
= xstrdup( optarg
);
448 set_dll_file_name( optarg
, spec
);
451 spec
->file_name
= xstrdup( optarg
);
454 if (!isdigit(optarg
[0]))
455 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg
);
456 spec
->heap_size
= atoi(optarg
);
457 if (spec
->heap_size
> 65535)
458 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec
->heap_size
);
464 /* ignored, because cc generates correct code. */
467 strarray_add( &lib_path
, xstrdup( optarg
), NULL
);
470 if (!strcmp( optarg
, "16" )) spec
->type
= SPEC_WIN16
;
471 else if (!strcmp( optarg
, "32" )) force_pointer_size
= 4;
472 else if (!strcmp( optarg
, "64" )) force_pointer_size
= 8;
473 else if (!strcmp( optarg
, "arm" )) thumb_mode
= 0;
474 else if (!strcmp( optarg
, "thumb" )) thumb_mode
= 1;
475 else if (!strcmp( optarg
, "no-cygwin" )) use_msvcrt
= 1;
476 else if (!strcmp( optarg
, "unix" )) unix_lib
= 1;
477 else if (!strcmp( optarg
, "unicode" )) spec
->unicode_app
= 1;
478 else if (!strncmp( optarg
, "cpu=", 4 )) cpu_option
= xstrdup( optarg
+ 4 );
479 else if (!strncmp( optarg
, "fpu=", 4 )) fpu_option
= xstrdup( optarg
+ 4 );
480 else if (!strncmp( optarg
, "arch=", 5 )) arch_option
= xstrdup( optarg
+ 5 );
481 else if (!strncmp( optarg
, "float-abi=", 10 )) float_abi_option
= xstrdup( optarg
+ 10 );
482 else fatal_error( "Unknown -m option '%s'\n", optarg
);
485 spec
->main_module
= xstrdup( optarg
);
488 spec
->dll_name
= xstrdup( optarg
);
491 set_target( optarg
);
494 add_delayed_import( optarg
);
497 spec
->init_func
= xstrdup( optarg
);
498 if ((p
= strchr( spec
->init_func
, '@' ))) *p
= 0; /* kill stdcall decoration */
501 if (!strcmp( optarg
, "PIC") || !strcmp( optarg
, "pic")) UsePIC
= 1;
502 else if (!strcmp( optarg
, "asynchronous-unwind-tables")) unwind_tables
= 1;
503 else if (!strcmp( optarg
, "no-asynchronous-unwind-tables")) unwind_tables
= 0;
504 /* ignore all other flags */
513 add_import_dll( optarg
, NULL
);
516 if (unlink( optarg
) == -1 && errno
!= ENOENT
)
517 fatal_error( "Unable to create output file '%s'\n", optarg
);
518 output_file_name
= xstrdup( optarg
);
521 strarray_add( &res_files
, xstrdup( optarg
), NULL
);
524 add_extra_ld_symbol( optarg
);
530 display_warnings
= 1;
533 set_exec_mode( MODE_DLL
);
536 set_exec_mode( MODE_DEF
);
539 set_exec_mode( MODE_EXE
);
540 if (!spec
->subsystem
) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
542 case LONG_OPT_IMPLIB
:
543 set_exec_mode( MODE_IMPLIB
);
545 case LONG_OPT_STATICLIB
:
546 set_exec_mode( MODE_STATICLIB
);
548 case LONG_OPT_BUILTIN
:
549 set_exec_mode( MODE_BUILTIN
);
551 case LONG_OPT_FIXUP_CTORS
:
552 set_exec_mode( MODE_FIXUP_CTORS
);
555 as_command
= strarray_fromstring( optarg
, " " );
558 cc_command
= strarray_fromstring( optarg
, " " );
560 case LONG_OPT_FAKE_MODULE
:
563 case LONG_OPT_EXTERNAL_SYMS
:
564 link_ext_symbols
= 1;
566 case LONG_OPT_LARGE_ADDRESS_AWARE
:
567 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
570 ld_command
= strarray_fromstring( optarg
, " " );
573 nm_command
= strarray_fromstring( optarg
, " " );
575 case LONG_OPT_NXCOMPAT
:
576 if (optarg
[0] == 'n' || optarg
[0] == 'N')
577 spec
->dll_characteristics
&= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
579 case LONG_OPT_SAFE_SEH
:
582 case LONG_OPT_PREFER_NATIVE
:
584 spec
->dll_characteristics
|= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE
;
586 case LONG_OPT_RESOURCES
:
587 set_exec_mode( MODE_RESOURCES
);
589 case LONG_OPT_SAVE_TEMPS
:
592 case LONG_OPT_SUBSYSTEM
:
593 set_subsystem( optarg
, spec
);
595 case LONG_OPT_VERSION
:
596 printf( "winebuild version " PACKAGE_VERSION
"\n" );
604 if (!save_temps
) atexit( cleanup_tmp_files
);
606 if (spec
->file_name
&& !strchr( spec
->file_name
, '.' ))
607 strcat( spec
->file_name
, exec_mode
== MODE_EXE
? ".exe" : ".dll" );
608 init_dll_name( spec
);
613 if (force_pointer_size
== 8) target_cpu
= CPU_x86_64
;
616 if (force_pointer_size
== 4) target_cpu
= CPU_x86
;
619 if (force_pointer_size
== 8)
620 fatal_error( "Cannot build 64-bit code for this CPU\n" );
624 return &argv
[optind
];
628 /* load all specified resource files */
629 static void load_resources( char *argv
[], DLLSPEC
*spec
)
637 for (i
= 0; i
< res_files
.count
; i
++) load_res16_file( res_files
.str
[i
], spec
);
641 for (i
= 0; i
< res_files
.count
; i
++)
643 if (!load_res32_file( res_files
.str
[i
], spec
))
644 fatal_error( "%s is not a valid Win32 resource file\n", res_files
.str
[i
] );
647 /* load any resource file found in the remaining arguments */
648 for (ptr
= last
= argv
; *ptr
; ptr
++)
650 if (!load_res32_file( *ptr
, spec
))
651 *last
++ = *ptr
; /* not a resource file, keep it in the list */
658 /* add input files that look like import libs to the import list */
659 static void load_import_libs( char *argv
[] )
663 for (ptr
= last
= argv
; *ptr
; ptr
++)
665 if (strendswith( *ptr
, ".def" ))
666 add_import_dll( NULL
, *ptr
);
668 *last
++ = *ptr
; /* not an import dll, keep it in the list */
673 static int parse_input_file( DLLSPEC
*spec
)
675 FILE *input_file
= open_input_file( NULL
, spec_file_name
);
676 char *extension
= strrchr( spec_file_name
, '.' );
679 spec
->src_name
= xstrdup( input_file_name
);
680 if (extension
&& !strcmp( extension
, ".def" ))
681 result
= parse_def_file( input_file
, spec
);
683 result
= parse_spec_file( input_file
, spec
);
684 close_input_file( input_file
);
689 /*******************************************************************
692 int main(int argc
, char **argv
)
694 DLLSPEC
*spec
= alloc_dll_spec();
697 signal( SIGHUP
, exit_on_signal
);
699 signal( SIGTERM
, exit_on_signal
);
700 signal( SIGINT
, exit_on_signal
);
702 argv
= parse_options( argc
, argv
, spec
);
703 atexit( cleanup
); /* make sure we remove the output file on exit */
708 if (spec
->subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
709 spec
->characteristics
|= IMAGE_FILE_DLL
;
712 load_resources( argv
, spec
);
713 if (spec_file_name
&& !parse_input_file( spec
)) break;
714 if (!spec
->init_func
&& !unix_lib
) spec
->init_func
= xstrdup( get_default_entry_point( spec
));
718 if (spec
->type
== SPEC_WIN16
) output_fake_module16( spec
);
719 else output_fake_module( spec
);
724 load_import_libs( argv
);
725 read_undef_symbols( spec
, argv
);
726 resolve_imports( spec
);
728 if (spec
->type
== SPEC_WIN16
) output_spec16_file( spec
);
729 else output_spec32_file( spec
);
732 if (argv
[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[0] );
733 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
734 if (!parse_input_file( spec
)) break;
736 output_def_file( spec
, 0 );
740 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
741 if (!parse_input_file( spec
)) break;
742 output_static_lib( spec
, argv
);
745 output_static_lib( NULL
, argv
);
748 if (!argv
[0]) fatal_error( "missing file argument for --builtin option\n" );
749 make_builtin_files( argv
);
751 case MODE_FIXUP_CTORS
:
752 if (!argv
[0]) fatal_error( "missing file argument for --fixup-ctors option\n" );
753 fixup_constructors( argv
);
756 load_resources( argv
, spec
);
757 output_res_o_file( spec
);
763 if (nb_errors
) exit(1);
764 output_file_name
= NULL
;