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
39 int display_warnings
= 0;
42 int link_ext_symbols
= 0;
43 int force_pointer_size
= 0;
44 int unwind_tables
= 0;
48 int prefer_native
= 0;
51 struct target target
= { 0 };
53 char *target_alias
= NULL
;
55 char *input_file_name
= NULL
;
56 char *spec_file_name
= NULL
;
57 FILE *output_file
= NULL
;
58 const char *output_file_name
= NULL
;
59 static int save_temps
;
60 static int fake_module
;
61 static DLLSPEC
*main_spec
;
63 static const struct strarray empty_strarray
;
64 struct strarray lib_path
= { 0 };
65 struct strarray tools_path
= { 0 };
66 struct strarray as_command
= { 0 };
67 struct strarray cc_command
= { 0 };
68 struct strarray ld_command
= { 0 };
69 struct strarray nm_command
= { 0 };
70 char *cpu_option
= NULL
;
71 char *fpu_option
= NULL
;
72 char *arch_option
= NULL
;
74 const char *float_abi_option
= "soft";
76 const char *float_abi_option
= "softfp";
85 static struct strarray res_files
;
101 static enum exec_mode_values exec_mode
= MODE_NONE
;
104 /* set the dll file name from the input file name */
105 static void set_dll_file_name( const char *name
, DLLSPEC
*spec
)
109 if (spec
->file_name
) return;
111 name
= get_basename( name
);
112 spec
->file_name
= xmalloc( strlen(name
) + 5 );
113 strcpy( spec
->file_name
, name
);
114 if ((p
= strrchr( spec
->file_name
, '.' )))
116 if (!strcmp( p
, ".spec" ) || !strcmp( p
, ".def" )) *p
= 0;
120 /* set the dll name from the file name */
121 static void init_dll_name( DLLSPEC
*spec
)
123 if (!spec
->file_name
&& output_file_name
)
126 spec
->file_name
= xstrdup( output_file_name
);
127 if ((p
= strrchr( spec
->file_name
, '.' ))) *p
= 0;
129 if (!spec
->dll_name
&& spec
->file_name
) /* set default name from file name */
132 spec
->dll_name
= xstrdup( spec
->file_name
);
133 if ((p
= strrchr( spec
->dll_name
, '.' ))) *p
= 0;
135 if (spec
->dll_name
) spec
->c_name
= make_c_identifier( spec
->dll_name
);
138 /* set the dll subsystem */
139 static void set_subsystem( const char *subsystem
, DLLSPEC
*spec
)
141 char *major
, *minor
, *str
= xstrdup( subsystem
);
143 if ((major
= strchr( str
, ':' ))) *major
++ = 0;
144 if (!strcmp( str
, "native" )) spec
->subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
145 else if (!strcmp( str
, "windows" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
146 else if (!strcmp( str
, "console" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
147 else if (!strcmp( str
, "wince" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
;
148 else if (!strcmp( str
, "win16" )) spec
->type
= SPEC_WIN16
;
149 else fatal_error( "Invalid subsystem name '%s'\n", subsystem
);
152 if ((minor
= strchr( major
, '.' )))
155 spec
->subsystem_minor
= atoi( minor
);
157 spec
->subsystem_major
= atoi( major
);
162 /* set the syscall table id */
163 static void set_syscall_table( const char *id
, DLLSPEC
*spec
)
165 int val
= atoi( id
);
167 if (val
< 0 || val
> 3) fatal_error( "Invalid syscall table id '%s', must be 0-3\n", id
);
168 spec
->syscall_table
= val
;
171 /* set the target CPU and platform */
172 static void set_target( const char *name
)
174 target_alias
= xstrdup( name
);
176 if (!parse_target( name
, &target
)) fatal_error( "Unrecognized target '%s'\n", name
);
177 if (target
.cpu
== CPU_ARM
&& is_pe()) thumb_mode
= 1;
180 /* cleanup on program exit */
181 static void cleanup(void)
183 if (output_file_name
) unlink( output_file_name
);
186 /* clean things up when aborting on a signal */
187 static void exit_on_signal( int sig
)
189 exit(1); /* this will call atexit functions */
192 /*******************************************************************
193 * command-line option handling
195 static const char usage_str
[] =
196 "Usage: winebuild [OPTIONS] [FILES]\n\n"
198 " --as-cmd=AS Command to use for assembling (default: as)\n"
199 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
200 " -B PREFIX Look for build tools in the PREFIX directory\n"
201 " --cc-cmd=CC C compiler to use for assembling (default: fall back to --as-cmd)\n"
202 " --data-only Generate a data-only dll (i.e. without any executable code)\n"
203 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
204 " -D SYM Ignored for C flags compatibility\n"
205 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
206 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
207 " --external-symbols Allow linking to external symbols\n"
208 " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
209 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
210 " --fake-module Create a fake binary module\n"
211 " -h, --help Display this help message\n"
212 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
213 " -I DIR Ignored for C flags compatibility\n"
214 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
215 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
216 " --large-address-aware Support an address space larger than 2Gb\n"
217 " --ld-cmd=LD Command to use for linking (default: ld)\n"
218 " -l, --library=LIB Import the specified library\n"
219 " -L, --library-path=DIR Look for imports libraries in DIR\n"
220 " -m16, -m32, -m64 Force building 16-bit, 32-bit resp. 64-bit code\n"
221 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
222 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
223 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
224 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
225 " -o, --output=NAME Set the output file name (default: stdout)\n"
226 " --prefer-native Set the flag to prefer loading native at run time\n"
227 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
228 " --safeseh Mark object files as SEH compatible\n"
229 " --save-temps Do not delete the generated intermediate files\n"
230 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console, wince)\n"
231 " --syscall-table=ID Set the syscall table id (between 0 and 3)\n"
232 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
233 " -v, --verbose Display the programs invoked\n"
234 " --version Print the version and exit\n"
235 " -w, --warnings Turn on warnings\n"
237 " --dll Build a library from a .spec file and object files\n"
238 " --def Build a .def file from a .spec file\n"
239 " --exe Build an executable from object files\n"
240 " --implib Build an import library\n"
241 " --staticlib Build a static library\n"
242 " --resources Build a .o or .res file for the resource files\n\n"
243 " --builtin Mark a library as a Wine builtin\n"
244 " --fixup-ctors Fixup the constructors data after the module has been built\n"
245 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
247 enum long_options_values
257 LONG_OPT_EXTERNAL_SYMS
,
258 LONG_OPT_FAKE_MODULE
,
259 LONG_OPT_FIXUP_CTORS
,
260 LONG_OPT_LARGE_ADDRESS_AWARE
,
264 LONG_OPT_PREFER_NATIVE
,
270 LONG_OPT_SYSCALL_TABLE
,
274 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";
276 static const struct long_option long_options
[] =
279 { "dll", 0, LONG_OPT_DLL
},
280 { "def", 0, LONG_OPT_DEF
},
281 { "exe", 0, LONG_OPT_EXE
},
282 { "implib", 0, LONG_OPT_IMPLIB
},
283 { "staticlib", 0, LONG_OPT_STATICLIB
},
284 { "builtin", 0, LONG_OPT_BUILTIN
},
285 { "resources", 0, LONG_OPT_RESOURCES
},
286 { "fixup-ctors", 0, LONG_OPT_FIXUP_CTORS
},
287 /* other long options */
288 { "as-cmd", 1, LONG_OPT_ASCMD
},
289 { "cc-cmd", 1, LONG_OPT_CCCMD
},
290 { "data-only", 0, LONG_OPT_DATA_ONLY
},
291 { "external-symbols", 0, LONG_OPT_EXTERNAL_SYMS
},
292 { "fake-module", 0, LONG_OPT_FAKE_MODULE
},
293 { "large-address-aware", 0, LONG_OPT_LARGE_ADDRESS_AWARE
},
294 { "ld-cmd", 1, LONG_OPT_LDCMD
},
295 { "nm-cmd", 1, LONG_OPT_NMCMD
},
296 { "nxcompat", 1, LONG_OPT_NXCOMPAT
},
297 { "prefer-native", 0, LONG_OPT_PREFER_NATIVE
},
298 { "safeseh", 0, LONG_OPT_SAFE_SEH
},
299 { "save-temps", 0, LONG_OPT_SAVE_TEMPS
},
300 { "subsystem", 1, LONG_OPT_SUBSYSTEM
},
301 { "syscall-table", 1, LONG_OPT_SYSCALL_TABLE
},
302 { "version", 0, LONG_OPT_VERSION
},
303 /* aliases for short options */
304 { "target", 1, 'b' },
305 { "delay-lib", 1, 'd' },
306 { "export", 1, 'E' },
308 { "filename", 1, 'F' },
311 { "kill-at", 0, 'k' },
312 { "library", 1, 'l' },
313 { "library-path", 1, 'L' },
314 { "main-module", 1, 'M' },
315 { "dll-name", 1, 'N' },
316 { "output", 1, 'o' },
318 { "undefined", 1, 'u' },
319 { "verbose", 0, 'v' },
320 { "warnings", 0, 'w' },
324 static void usage( int exit_code
)
326 fprintf( stderr
, "%s", usage_str
);
330 static void set_exec_mode( enum exec_mode_values mode
)
332 if (exec_mode
!= MODE_NONE
) usage(1);
336 /* get the default entry point for a given spec file */
337 static const char *get_default_entry_point( const DLLSPEC
*spec
)
339 if (spec
->characteristics
& IMAGE_FILE_DLL
) return "DllMain";
340 if (spec
->subsystem
== IMAGE_SUBSYSTEM_NATIVE
) return "DriverEntry";
341 if (spec
->type
== SPEC_WIN16
)
343 add_spec_extra_ld_symbol("WinMain16");
344 return "__wine_spec_exe16_entry";
346 else if (spec
->unicode_app
)
348 /* __wine_spec_exe_wentry always calls wmain */
349 add_spec_extra_ld_symbol("wmain");
350 if (spec
->subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
351 add_spec_extra_ld_symbol("wWinMain");
352 return "__wine_spec_exe_wentry";
356 /* __wine_spec_exe_entry always calls main */
357 add_spec_extra_ld_symbol("main");
358 if (spec
->subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
359 add_spec_extra_ld_symbol("WinMain");
360 return "__wine_spec_exe_entry";
364 static void option_callback( int optc
, char *optarg
)
371 strarray_add( &tools_path
, xstrdup( optarg
));
377 spec_file_name
= xstrdup( optarg
);
378 set_dll_file_name( optarg
, main_spec
);
381 main_spec
->file_name
= xstrdup( optarg
);
384 if (!isdigit(optarg
[0]))
385 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg
);
386 main_spec
->heap_size
= atoi(optarg
);
387 if (main_spec
->heap_size
> 65535)
388 fatal_error( "Invalid heap size %d, maximum is 65535\n", main_spec
->heap_size
);
394 /* ignored, because cc generates correct code. */
397 strarray_add( &lib_path
, xstrdup( optarg
));
400 if (!strcmp( optarg
, "16" )) main_spec
->type
= SPEC_WIN16
;
401 else if (!strcmp( optarg
, "32" )) force_pointer_size
= 4;
402 else if (!strcmp( optarg
, "64" )) force_pointer_size
= 8;
403 else if (!strcmp( optarg
, "arm" )) thumb_mode
= 0;
404 else if (!strcmp( optarg
, "thumb" )) thumb_mode
= 1;
405 else if (!strcmp( optarg
, "no-cygwin" )) use_msvcrt
= 1;
406 else if (!strcmp( optarg
, "unix" )) unix_lib
= 1;
407 else if (!strcmp( optarg
, "unicode" )) main_spec
->unicode_app
= 1;
408 else if (!strncmp( optarg
, "cpu=", 4 )) cpu_option
= xstrdup( optarg
+ 4 );
409 else if (!strncmp( optarg
, "fpu=", 4 )) fpu_option
= xstrdup( optarg
+ 4 );
410 else if (!strncmp( optarg
, "arch=", 5 )) arch_option
= xstrdup( optarg
+ 5 );
411 else if (!strncmp( optarg
, "float-abi=", 10 )) float_abi_option
= xstrdup( optarg
+ 10 );
412 else fatal_error( "Unknown -m option '%s'\n", optarg
);
415 main_spec
->main_module
= xstrdup( optarg
);
418 main_spec
->dll_name
= xstrdup( optarg
);
421 set_target( optarg
);
424 add_delayed_import( optarg
);
427 main_spec
->init_func
= xstrdup( optarg
);
428 if ((p
= strchr( main_spec
->init_func
, '@' ))) *p
= 0; /* kill stdcall decoration */
431 if (!strcmp( optarg
, "PIC") || !strcmp( optarg
, "pic")) UsePIC
= 1;
432 else if (!strcmp( optarg
, "asynchronous-unwind-tables")) unwind_tables
= 1;
433 else if (!strcmp( optarg
, "no-asynchronous-unwind-tables")) unwind_tables
= 0;
434 /* ignore all other flags */
443 add_import_dll( optarg
, NULL
);
446 if (unlink( optarg
) == -1 && errno
!= ENOENT
)
447 fatal_error( "Unable to create output file '%s'\n", optarg
);
448 output_file_name
= xstrdup( optarg
);
451 strarray_add( &res_files
, xstrdup( optarg
));
454 add_extra_ld_symbol( optarg
);
460 display_warnings
= 1;
463 set_exec_mode( MODE_DLL
);
466 set_exec_mode( MODE_DEF
);
469 set_exec_mode( MODE_EXE
);
470 if (!main_spec
->subsystem
) main_spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
472 case LONG_OPT_IMPLIB
:
473 set_exec_mode( MODE_IMPLIB
);
475 case LONG_OPT_STATICLIB
:
476 set_exec_mode( MODE_STATICLIB
);
478 case LONG_OPT_BUILTIN
:
479 set_exec_mode( MODE_BUILTIN
);
481 case LONG_OPT_FIXUP_CTORS
:
482 set_exec_mode( MODE_FIXUP_CTORS
);
485 as_command
= strarray_fromstring( optarg
, " " );
488 cc_command
= strarray_fromstring( optarg
, " " );
490 case LONG_OPT_DATA_ONLY
:
493 case LONG_OPT_FAKE_MODULE
:
496 case LONG_OPT_EXTERNAL_SYMS
:
497 link_ext_symbols
= 1;
499 case LONG_OPT_LARGE_ADDRESS_AWARE
:
500 main_spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
503 ld_command
= strarray_fromstring( optarg
, " " );
506 nm_command
= strarray_fromstring( optarg
, " " );
508 case LONG_OPT_NXCOMPAT
:
509 if (optarg
[0] == 'n' || optarg
[0] == 'N')
510 main_spec
->dll_characteristics
&= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
512 case LONG_OPT_SAFE_SEH
:
515 case LONG_OPT_PREFER_NATIVE
:
517 main_spec
->dll_characteristics
|= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE
;
519 case LONG_OPT_RESOURCES
:
520 set_exec_mode( MODE_RESOURCES
);
522 case LONG_OPT_SAVE_TEMPS
:
525 case LONG_OPT_SUBSYSTEM
:
526 set_subsystem( optarg
, main_spec
);
528 case LONG_OPT_SYSCALL_TABLE
:
529 set_syscall_table( optarg
, main_spec
);
531 case LONG_OPT_VERSION
:
532 printf( "winebuild version " PACKAGE_VERSION
"\n" );
535 fprintf( stderr
, "winebuild: %s\n\n", optarg
);
542 /* load all specified resource files */
543 static struct strarray
load_resources( struct strarray files
, DLLSPEC
*spec
)
545 struct strarray ret
= empty_strarray
;
551 for (i
= 0; i
< res_files
.count
; i
++) load_res16_file( res_files
.str
[i
], spec
);
555 for (i
= 0; i
< res_files
.count
; i
++)
557 if (!load_res32_file( res_files
.str
[i
], spec
))
558 fatal_error( "%s is not a valid Win32 resource file\n", res_files
.str
[i
] );
561 /* load any resource file found in the remaining arguments */
562 for (i
= 0; i
< files
.count
; i
++)
564 if (!load_res32_file( files
.str
[i
], spec
))
565 strarray_add( &ret
, files
.str
[i
] ); /* not a resource file, keep it in the list */
572 /* add input files that look like import libs to the import list */
573 static struct strarray
load_import_libs( struct strarray files
)
575 struct strarray ret
= empty_strarray
;
578 for (i
= 0; i
< files
.count
; i
++)
580 if (strendswith( files
.str
[i
], ".def" ))
581 add_import_dll( NULL
, files
.str
[i
] );
583 strarray_add( &ret
, files
.str
[i
] ); /* not an import dll, keep it in the list */
588 static int parse_input_file( DLLSPEC
*spec
)
590 FILE *input_file
= open_input_file( NULL
, spec_file_name
);
593 spec
->src_name
= xstrdup( input_file_name
);
594 if (strendswith( spec_file_name
, ".def" ))
595 result
= parse_def_file( input_file
, spec
);
597 result
= parse_spec_file( input_file
, spec
);
598 close_input_file( input_file
);
603 /*******************************************************************
606 int main(int argc
, char **argv
)
608 struct strarray files
;
609 DLLSPEC
*spec
= main_spec
= alloc_dll_spec();
612 signal( SIGHUP
, exit_on_signal
);
614 signal( SIGTERM
, exit_on_signal
);
615 signal( SIGINT
, exit_on_signal
);
617 target
= init_argv0_target( argv
[0] );
618 if (target
.platform
== PLATFORM_CYGWIN
) target
.platform
= PLATFORM_MINGW
;
620 files
= parse_options( argc
, argv
, short_options
, long_options
, 0, option_callback
);
622 atexit( cleanup
); /* make sure we remove the output file on exit */
623 if (!save_temps
) atexit( cleanup_tmp_files
);
625 if (spec
->file_name
&& !strchr( spec
->file_name
, '.' ))
626 strcat( spec
->file_name
, exec_mode
== MODE_EXE
? ".exe" : ".dll" );
627 init_dll_name( spec
);
629 if (force_pointer_size
) set_target_ptr_size( &target
, force_pointer_size
);
634 if (spec
->subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
635 spec
->characteristics
|= IMAGE_FILE_DLL
;
638 files
= load_resources( files
, spec
);
639 if (spec_file_name
&& !parse_input_file( spec
)) break;
640 if (!spec
->init_func
&& !unix_lib
) spec
->init_func
= xstrdup( get_default_entry_point( spec
));
644 output_fake_module( spec
);
649 output_data_module( spec
);
654 files
= load_import_libs( files
);
655 read_undef_symbols( spec
, files
);
656 resolve_imports( spec
);
658 if (spec
->type
== SPEC_WIN16
) output_spec16_file( spec
);
659 else output_spec32_file( spec
);
662 if (files
.count
) fatal_error( "file argument '%s' not allowed in this mode\n", files
.str
[0] );
663 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
664 if (!parse_input_file( spec
)) break;
666 output_def_file( spec
, 0 );
670 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
671 if (!parse_input_file( spec
)) break;
672 output_static_lib( spec
, files
);
675 output_static_lib( NULL
, files
);
678 if (!files
.count
) fatal_error( "missing file argument for --builtin option\n" );
679 make_builtin_files( files
);
681 case MODE_FIXUP_CTORS
:
682 if (!files
.count
) fatal_error( "missing file argument for --fixup-ctors option\n" );
683 fixup_constructors( files
);
686 files
= load_resources( files
, spec
);
687 output_res_o_file( spec
);
693 if (nb_errors
) exit(1);
694 output_file_name
= NULL
;