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
38 int display_warnings
= 0;
41 int link_ext_symbols
= 0;
42 int force_pointer_size
= 0;
43 int unwind_tables
= 0;
47 int prefer_native
= 0;
50 struct target target
= { 0 };
52 char *target_alias
= NULL
;
54 char *input_file_name
= NULL
;
55 char *spec_file_name
= NULL
;
56 FILE *output_file
= NULL
;
57 const char *output_file_name
= NULL
;
58 static int save_temps
;
59 static int fake_module
;
60 static DLLSPEC
*main_spec
;
62 static const struct strarray empty_strarray
;
63 struct strarray lib_path
= { 0 };
64 struct strarray tools_path
= { 0 };
65 struct strarray as_command
= { 0 };
66 struct strarray cc_command
= { 0 };
67 struct strarray ld_command
= { 0 };
68 struct strarray nm_command
= { 0 };
69 char *cpu_option
= NULL
;
70 char *fpu_option
= NULL
;
71 char *arch_option
= NULL
;
73 const char *float_abi_option
= "soft";
75 const char *float_abi_option
= "softfp";
84 static struct strarray res_files
;
100 static enum exec_mode_values exec_mode
= MODE_NONE
;
103 /* set the dll file name from the input file name */
104 static void set_dll_file_name( const char *name
, DLLSPEC
*spec
)
108 if (spec
->file_name
) return;
110 name
= get_basename( name
);
111 spec
->file_name
= xmalloc( strlen(name
) + 5 );
112 strcpy( spec
->file_name
, name
);
113 if ((p
= strrchr( spec
->file_name
, '.' )))
115 if (!strcmp( p
, ".spec" ) || !strcmp( p
, ".def" )) *p
= 0;
119 /* set the dll name from the file name */
120 static void init_dll_name( DLLSPEC
*spec
)
122 if (!spec
->file_name
&& output_file_name
)
125 spec
->file_name
= xstrdup( output_file_name
);
126 if ((p
= strrchr( spec
->file_name
, '.' ))) *p
= 0;
128 if (!spec
->dll_name
&& spec
->file_name
) /* set default name from file name */
131 spec
->dll_name
= xstrdup( spec
->file_name
);
132 if ((p
= strrchr( spec
->dll_name
, '.' ))) *p
= 0;
134 if (spec
->dll_name
) spec
->c_name
= make_c_identifier( spec
->dll_name
);
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
, "wince" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
;
147 else if (!strcmp( str
, "win16" )) spec
->type
= SPEC_WIN16
;
148 else fatal_error( "Invalid subsystem name '%s'\n", subsystem
);
151 if ((minor
= strchr( major
, '.' )))
154 spec
->subsystem_minor
= atoi( minor
);
156 spec
->subsystem_major
= atoi( major
);
161 /* set the syscall table id */
162 static void set_syscall_table( const char *id
, DLLSPEC
*spec
)
164 int val
= atoi( id
);
166 if (val
< 0 || val
> 3) fatal_error( "Invalid syscall table id '%s', must be 0-3\n", id
);
167 spec
->syscall_table
= val
;
170 /* set the target CPU and platform */
171 static void set_target( const char *name
)
173 target_alias
= xstrdup( name
);
175 if (!parse_target( name
, &target
)) fatal_error( "Unrecognized target '%s'\n", name
);
176 thumb_mode
= target
.cpu
== CPU_ARM
&& is_pe();
177 if (is_pe()) unwind_tables
= 1;
180 /* cleanup on program exit */
181 static void cleanup(void)
183 if (output_file_name
) unlink( output_file_name
);
184 if (!save_temps
) remove_temp_files();
187 /* clean things up when aborting on a signal */
188 static void exit_on_signal( int sig
)
190 exit(1); /* this will call atexit functions */
193 /*******************************************************************
194 * command-line option handling
196 static const char usage_str
[] =
197 "Usage: winebuild [OPTIONS] [FILES]\n\n"
199 " --as-cmd=AS Command to use for assembling (default: as)\n"
200 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
201 " -B PREFIX Look for build tools in the PREFIX directory\n"
202 " --cc-cmd=CC C compiler to use for assembling (default: fall back to --as-cmd)\n"
203 " --data-only Generate a data-only dll (i.e. without any executable code)\n"
204 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
205 " -D SYM Ignored for C flags compatibility\n"
206 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
207 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
208 " --external-symbols Allow linking to external symbols\n"
209 " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
210 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
211 " --fake-module Create a fake binary module\n"
212 " -h, --help Display this help message\n"
213 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
214 " -I DIR Ignored for C flags compatibility\n"
215 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
216 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
217 " --large-address-aware Support an address space larger than 2Gb\n"
218 " --ld-cmd=LD Command to use for linking (default: ld)\n"
219 " -l, --library=LIB Import the specified library\n"
220 " -L, --library-path=DIR Look for imports libraries in DIR\n"
221 " -m16, -m32, -m64 Force building 16-bit, 32-bit resp. 64-bit code\n"
222 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
223 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
224 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
225 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
226 " -o, --output=NAME Set the output file name (default: stdout)\n"
227 " --prefer-native Set the flag to prefer loading native at run time\n"
228 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
229 " --safeseh Mark object files as SEH compatible\n"
230 " --save-temps Do not delete the generated intermediate files\n"
231 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console, wince)\n"
232 " --syscall-table=ID Set the syscall table id (between 0 and 3)\n"
233 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
234 " -v, --verbose Display the programs invoked\n"
235 " --version Print the version and exit\n"
236 " -w, --warnings Turn on warnings\n"
237 " --without-dlltool Generate import library without using dlltool\n"
239 " --dll Build a library from a .spec file and object files\n"
240 " --def Build a .def file from a .spec file\n"
241 " --exe Build an executable from object files\n"
242 " --implib Build an import library\n"
243 " --staticlib Build a static library\n"
244 " --resources Build a .o or .res file for the resource files\n\n"
245 " --builtin Mark a library as a Wine builtin\n"
246 " --fixup-ctors Fixup the constructors data after the module has been built\n"
247 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
249 enum long_options_values
259 LONG_OPT_EXTERNAL_SYMS
,
260 LONG_OPT_FAKE_MODULE
,
261 LONG_OPT_FIXUP_CTORS
,
262 LONG_OPT_LARGE_ADDRESS_AWARE
,
266 LONG_OPT_PREFER_NATIVE
,
272 LONG_OPT_SYSCALL_TABLE
,
274 LONG_OPT_WITHOUT_DLLTOOL
,
277 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";
279 static const struct long_option long_options
[] =
282 { "dll", 0, LONG_OPT_DLL
},
283 { "def", 0, LONG_OPT_DEF
},
284 { "exe", 0, LONG_OPT_EXE
},
285 { "implib", 0, LONG_OPT_IMPLIB
},
286 { "staticlib", 0, LONG_OPT_STATICLIB
},
287 { "builtin", 0, LONG_OPT_BUILTIN
},
288 { "resources", 0, LONG_OPT_RESOURCES
},
289 { "fixup-ctors", 0, LONG_OPT_FIXUP_CTORS
},
290 /* other long options */
291 { "as-cmd", 1, LONG_OPT_ASCMD
},
292 { "cc-cmd", 1, LONG_OPT_CCCMD
},
293 { "data-only", 0, LONG_OPT_DATA_ONLY
},
294 { "external-symbols", 0, LONG_OPT_EXTERNAL_SYMS
},
295 { "fake-module", 0, LONG_OPT_FAKE_MODULE
},
296 { "large-address-aware", 0, LONG_OPT_LARGE_ADDRESS_AWARE
},
297 { "ld-cmd", 1, LONG_OPT_LDCMD
},
298 { "nm-cmd", 1, LONG_OPT_NMCMD
},
299 { "nxcompat", 1, LONG_OPT_NXCOMPAT
},
300 { "prefer-native", 0, LONG_OPT_PREFER_NATIVE
},
301 { "safeseh", 0, LONG_OPT_SAFE_SEH
},
302 { "save-temps", 0, LONG_OPT_SAVE_TEMPS
},
303 { "subsystem", 1, LONG_OPT_SUBSYSTEM
},
304 { "syscall-table", 1, LONG_OPT_SYSCALL_TABLE
},
305 { "version", 0, LONG_OPT_VERSION
},
306 { "without-dlltool", 0, LONG_OPT_WITHOUT_DLLTOOL
},
307 /* aliases for short options */
308 { "target", 1, 'b' },
309 { "delay-lib", 1, 'd' },
310 { "export", 1, 'E' },
312 { "filename", 1, 'F' },
315 { "kill-at", 0, 'k' },
316 { "library", 1, 'l' },
317 { "library-path", 1, 'L' },
318 { "main-module", 1, 'M' },
319 { "dll-name", 1, 'N' },
320 { "output", 1, 'o' },
322 { "undefined", 1, 'u' },
323 { "verbose", 0, 'v' },
324 { "warnings", 0, 'w' },
328 static void usage( int exit_code
)
330 fprintf( stderr
, "%s", usage_str
);
334 static void set_exec_mode( enum exec_mode_values mode
)
336 if (exec_mode
!= MODE_NONE
) usage(1);
340 /* get the default entry point for a given spec file */
341 static const char *get_default_entry_point( const DLLSPEC
*spec
)
343 if (spec
->characteristics
& IMAGE_FILE_DLL
)
345 add_spec_extra_ld_symbol("DllMain");
346 return "__wine_spec_dll_entry";
348 if (spec
->subsystem
== IMAGE_SUBSYSTEM_NATIVE
) return "DriverEntry";
349 if (spec
->type
== SPEC_WIN16
)
351 add_spec_extra_ld_symbol("WinMain16");
352 return "__wine_spec_exe16_entry";
354 else if (spec
->unicode_app
)
356 /* __wine_spec_exe_wentry always calls wmain */
357 add_spec_extra_ld_symbol("wmain");
358 if (spec
->subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
359 add_spec_extra_ld_symbol("wWinMain");
360 return "__wine_spec_exe_wentry";
364 /* __wine_spec_exe_entry always calls main */
365 add_spec_extra_ld_symbol("main");
366 if (spec
->subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
367 add_spec_extra_ld_symbol("WinMain");
368 return "__wine_spec_exe_entry";
372 static void option_callback( int optc
, char *optarg
)
379 strarray_add( &tools_path
, xstrdup( optarg
));
385 spec_file_name
= xstrdup( optarg
);
386 set_dll_file_name( optarg
, main_spec
);
389 main_spec
->file_name
= xstrdup( optarg
);
392 if (!isdigit(optarg
[0]))
393 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg
);
394 main_spec
->heap_size
= atoi(optarg
);
395 if (main_spec
->heap_size
> 65535)
396 fatal_error( "Invalid heap size %d, maximum is 65535\n", main_spec
->heap_size
);
402 /* ignored, because cc generates correct code. */
405 strarray_add( &lib_path
, xstrdup( optarg
));
408 if (!strcmp( optarg
, "16" )) main_spec
->type
= SPEC_WIN16
;
409 else if (!strcmp( optarg
, "32" )) force_pointer_size
= 4;
410 else if (!strcmp( optarg
, "64" )) force_pointer_size
= 8;
411 else if (!strcmp( optarg
, "arm" )) thumb_mode
= 0;
412 else if (!strcmp( optarg
, "thumb" )) thumb_mode
= 1;
413 else if (!strcmp( optarg
, "no-cygwin" )) use_msvcrt
= 1;
414 else if (!strcmp( optarg
, "unicode" )) main_spec
->unicode_app
= 1;
415 else if (!strncmp( optarg
, "cpu=", 4 )) cpu_option
= xstrdup( optarg
+ 4 );
416 else if (!strncmp( optarg
, "fpu=", 4 )) fpu_option
= xstrdup( optarg
+ 4 );
417 else if (!strncmp( optarg
, "arch=", 5 )) arch_option
= xstrdup( optarg
+ 5 );
418 else if (!strncmp( optarg
, "float-abi=", 10 )) float_abi_option
= xstrdup( optarg
+ 10 );
419 else fatal_error( "Unknown -m option '%s'\n", optarg
);
422 main_spec
->main_module
= xstrdup( optarg
);
425 main_spec
->dll_name
= xstrdup( optarg
);
428 set_target( optarg
);
431 add_delayed_import( optarg
);
434 main_spec
->init_func
= xstrdup( optarg
);
435 if ((p
= strchr( main_spec
->init_func
, '@' ))) *p
= 0; /* kill stdcall decoration */
438 if (!strcmp( optarg
, "PIC") || !strcmp( optarg
, "pic")) UsePIC
= 1;
439 else if (!strcmp( optarg
, "asynchronous-unwind-tables")) unwind_tables
= 1;
440 else if (!strcmp( optarg
, "no-asynchronous-unwind-tables")) unwind_tables
= 0;
441 /* ignore all other flags */
450 add_import_dll( optarg
, NULL
);
453 if (unlink( optarg
) == -1 && errno
!= ENOENT
)
454 fatal_error( "Unable to create output file '%s'\n", optarg
);
455 output_file_name
= xstrdup( optarg
);
458 strarray_add( &res_files
, xstrdup( optarg
));
461 add_extra_ld_symbol( optarg
);
467 display_warnings
= 1;
470 set_exec_mode( MODE_DLL
);
473 set_exec_mode( MODE_DEF
);
476 set_exec_mode( MODE_EXE
);
477 if (!main_spec
->subsystem
) main_spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
479 case LONG_OPT_IMPLIB
:
480 set_exec_mode( MODE_IMPLIB
);
482 case LONG_OPT_STATICLIB
:
483 set_exec_mode( MODE_STATICLIB
);
485 case LONG_OPT_BUILTIN
:
486 set_exec_mode( MODE_BUILTIN
);
488 case LONG_OPT_FIXUP_CTORS
:
489 set_exec_mode( MODE_FIXUP_CTORS
);
492 as_command
= strarray_fromstring( optarg
, " " );
495 cc_command
= strarray_fromstring( optarg
, " " );
497 case LONG_OPT_DATA_ONLY
:
500 case LONG_OPT_FAKE_MODULE
:
503 case LONG_OPT_EXTERNAL_SYMS
:
504 link_ext_symbols
= 1;
506 case LONG_OPT_LARGE_ADDRESS_AWARE
:
507 main_spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
510 ld_command
= strarray_fromstring( optarg
, " " );
513 nm_command
= strarray_fromstring( optarg
, " " );
515 case LONG_OPT_NXCOMPAT
:
516 if (optarg
[0] == 'n' || optarg
[0] == 'N')
517 main_spec
->dll_characteristics
&= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
519 case LONG_OPT_SAFE_SEH
:
522 case LONG_OPT_PREFER_NATIVE
:
524 main_spec
->dll_characteristics
|= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE
;
526 case LONG_OPT_RESOURCES
:
527 set_exec_mode( MODE_RESOURCES
);
529 case LONG_OPT_SAVE_TEMPS
:
532 case LONG_OPT_SUBSYSTEM
:
533 set_subsystem( optarg
, main_spec
);
535 case LONG_OPT_SYSCALL_TABLE
:
536 set_syscall_table( optarg
, main_spec
);
538 case LONG_OPT_VERSION
:
539 printf( "winebuild version " PACKAGE_VERSION
"\n" );
541 case LONG_OPT_WITHOUT_DLLTOOL
:
545 fprintf( stderr
, "winebuild: %s\n\n", optarg
);
552 /* load all specified resource files */
553 static struct strarray
load_resources( struct strarray files
, DLLSPEC
*spec
)
555 struct strarray ret
= empty_strarray
;
561 for (i
= 0; i
< res_files
.count
; i
++) load_res16_file( res_files
.str
[i
], spec
);
565 for (i
= 0; i
< res_files
.count
; i
++)
567 if (!load_res32_file( res_files
.str
[i
], spec
))
568 fatal_error( "%s is not a valid Win32 resource file\n", res_files
.str
[i
] );
571 /* load any resource file found in the remaining arguments */
572 for (i
= 0; i
< files
.count
; i
++)
574 if (!load_res32_file( files
.str
[i
], spec
))
575 strarray_add( &ret
, files
.str
[i
] ); /* not a resource file, keep it in the list */
582 /* add input files that look like import libs to the import list */
583 static struct strarray
load_import_libs( struct strarray files
)
585 struct strarray ret
= empty_strarray
;
588 for (i
= 0; i
< files
.count
; i
++)
590 if (strendswith( files
.str
[i
], ".def" ))
591 add_import_dll( NULL
, files
.str
[i
] );
593 strarray_add( &ret
, files
.str
[i
] ); /* not an import dll, keep it in the list */
598 static int parse_input_file( DLLSPEC
*spec
)
600 FILE *input_file
= open_input_file( NULL
, spec_file_name
);
603 spec
->src_name
= xstrdup( input_file_name
);
604 if (strendswith( spec_file_name
, ".def" ))
605 result
= parse_def_file( input_file
, spec
);
607 result
= parse_spec_file( input_file
, spec
);
608 close_input_file( input_file
);
613 /*******************************************************************
616 int main(int argc
, char **argv
)
618 struct strarray files
;
619 DLLSPEC
*spec
= main_spec
= alloc_dll_spec();
621 init_signals( exit_on_signal
);
622 target
= init_argv0_target( argv
[0] );
623 if (target
.platform
== PLATFORM_CYGWIN
) target
.platform
= PLATFORM_MINGW
;
624 if (is_pe()) unwind_tables
= 1;
626 files
= parse_options( argc
, argv
, short_options
, long_options
, 0, option_callback
);
628 atexit( cleanup
); /* make sure we remove the output file on exit */
630 if (spec
->file_name
&& !strchr( spec
->file_name
, '.' ))
631 strcat( spec
->file_name
, exec_mode
== MODE_EXE
? ".exe" : ".dll" );
632 init_dll_name( spec
);
634 if (force_pointer_size
) set_target_ptr_size( &target
, force_pointer_size
);
639 if (spec
->subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
640 spec
->characteristics
|= IMAGE_FILE_DLL
;
643 if (get_ptr_size() == 4)
644 spec
->characteristics
|= IMAGE_FILE_32BIT_MACHINE
;
646 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
648 files
= load_resources( files
, spec
);
649 if (spec_file_name
&& !parse_input_file( spec
)) break;
650 if (!spec
->init_func
) spec
->init_func
= xstrdup( get_default_entry_point( spec
));
654 output_fake_module( spec
);
659 output_data_module( spec
);
664 files
= load_import_libs( files
);
665 read_undef_symbols( spec
, files
);
666 resolve_imports( spec
);
668 if (spec
->type
== SPEC_WIN16
) output_spec16_file( spec
);
669 else output_spec32_file( spec
);
672 if (files
.count
) fatal_error( "file argument '%s' not allowed in this mode\n", files
.str
[0] );
673 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
674 if (!parse_input_file( spec
)) break;
676 output_def_file( spec
, 0 );
680 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
681 if (!parse_input_file( spec
)) break;
682 output_import_lib( spec
, files
);
685 output_static_lib( output_file_name
, files
, 1 );
688 if (!files
.count
) fatal_error( "missing file argument for --builtin option\n" );
689 make_builtin_files( files
);
691 case MODE_FIXUP_CTORS
:
692 if (!files
.count
) fatal_error( "missing file argument for --fixup-ctors option\n" );
693 fixup_constructors( files
);
696 files
= load_resources( files
, spec
);
697 output_res_o_file( spec
);
703 if (nb_errors
) exit(1);
704 output_file_name
= NULL
;