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"
44 int display_warnings
= 0;
47 int link_ext_symbols
= 0;
48 int force_pointer_size
= 0;
49 int unwind_tables
= 0;
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
;
62 #error Unsupported CPU
66 enum target_platform target_platform
= PLATFORM_APPLE
;
67 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
68 enum target_platform target_platform
= PLATFORM_FREEBSD
;
70 enum target_platform target_platform
= PLATFORM_SOLARIS
;
72 enum target_platform target_platform
= PLATFORM_WINDOWS
;
74 enum target_platform target_platform
= PLATFORM_UNSPECIFIED
;
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
;
92 char *arch_option
= NULL
;
100 static int nb_res_files
;
101 static char **res_files
;
104 enum exec_mode_values
114 static enum exec_mode_values exec_mode
= MODE_NONE
;
119 enum target_platform platform
;
122 { "macos", PLATFORM_APPLE
},
123 { "darwin", PLATFORM_APPLE
},
124 { "freebsd", PLATFORM_FREEBSD
},
125 { "solaris", PLATFORM_SOLARIS
},
126 { "mingw32", PLATFORM_WINDOWS
},
127 { "windows", PLATFORM_WINDOWS
},
128 { "winnt", PLATFORM_WINDOWS
}
131 /* set the dll file name from the input file name */
132 static void set_dll_file_name( const char *name
, DLLSPEC
*spec
)
136 if (spec
->file_name
) return;
138 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
139 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
140 spec
->file_name
= xmalloc( strlen(name
) + 5 );
141 strcpy( spec
->file_name
, name
);
142 if ((p
= strrchr( spec
->file_name
, '.' )))
144 if (!strcmp( p
, ".spec" ) || !strcmp( p
, ".def" )) *p
= 0;
148 /* set the dll name from the file name */
149 static void init_dll_name( DLLSPEC
*spec
)
151 if (!spec
->file_name
&& output_file_name
)
154 spec
->file_name
= xstrdup( output_file_name
);
155 if ((p
= strrchr( spec
->file_name
, '.' ))) *p
= 0;
157 if (!spec
->dll_name
&& spec
->file_name
) /* set default name from file name */
160 spec
->dll_name
= xstrdup( spec
->file_name
);
161 if ((p
= strrchr( spec
->dll_name
, '.' ))) *p
= 0;
165 /* set the dll subsystem */
166 static void set_subsystem( const char *subsystem
, DLLSPEC
*spec
)
168 char *major
, *minor
, *str
= xstrdup( subsystem
);
170 if ((major
= strchr( str
, ':' ))) *major
++ = 0;
171 if (!strcmp( str
, "native" )) spec
->subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
172 else if (!strcmp( str
, "windows" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
173 else if (!strcmp( str
, "console" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
174 else if (!strcmp( str
, "wince" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
;
175 else if (!strcmp( str
, "win16" )) spec
->type
= SPEC_WIN16
;
176 else fatal_error( "Invalid subsystem name '%s'\n", subsystem
);
179 if ((minor
= strchr( major
, '.' )))
182 spec
->subsystem_minor
= atoi( minor
);
184 spec
->subsystem_major
= atoi( major
);
189 /* set the target CPU and platform */
190 static void set_target( const char *target
)
193 char *p
, *platform
, *spec
= xstrdup( target
);
195 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
197 target_alias
= xstrdup( target
);
199 /* get the CPU part */
201 if ((p
= strchr( spec
, '-' )))
206 cpu
= get_cpu_from_name( spec
);
207 if (cpu
== -1) fatal_error( "Unrecognized CPU '%s'\n", spec
);
210 if ((p
= strrchr( p
, '-' ))) platform
= p
+ 1;
212 else if (!strcmp( spec
, "mingw32" ))
214 target_cpu
= CPU_x86
;
218 fatal_error( "Invalid target specification '%s'\n", target
);
220 /* get the OS part */
222 target_platform
= PLATFORM_UNSPECIFIED
; /* default value */
223 for (i
= 0; i
< sizeof(platform_names
)/sizeof(platform_names
[0]); i
++)
225 if (!strncmp( platform_names
[i
].name
, platform
, strlen(platform_names
[i
].name
) ))
227 target_platform
= platform_names
[i
].platform
;
235 /* cleanup on program exit */
236 static void cleanup(void)
238 if (output_file_name
) unlink( output_file_name
);
241 /* clean things up when aborting on a signal */
242 static void exit_on_signal( int sig
)
244 exit(1); /* this will call atexit functions */
247 /*******************************************************************
248 * command-line option handling
250 static const char usage_str
[] =
251 "Usage: winebuild [OPTIONS] [FILES]\n\n"
253 " --as-cmd=AS Command to use for assembling (default: as)\n"
254 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
255 " --cc-cmd=CC C compiler to use for assembling (default: fall back to --as-cmd)\n"
256 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
257 " -D SYM Ignored for C flags compatibility\n"
258 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
259 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
260 " --external-symbols Allow linking to external symbols\n"
261 " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
262 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
263 " --fake-module Create a fake binary module\n"
264 " -h, --help Display this help message\n"
265 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
266 " -I DIR Ignored for C flags compatibility\n"
267 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
268 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
269 " --large-address-aware Support an address space larger than 2Gb\n"
270 " --ld-cmd=LD Command to use for linking (default: ld)\n"
271 " -l, --library=LIB Import the specified library\n"
272 " -L, --library-path=DIR Look for imports libraries in DIR\n"
273 " -m16, -m32, -m64 Force building 16-bit, 32-bit resp. 64-bit code\n"
274 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
275 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
276 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
277 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
278 " -o, --output=NAME Set the output file name (default: stdout)\n"
279 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
280 " --save-temps Do not delete the generated intermediate files\n"
281 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console, wince)\n"
282 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
283 " -v, --verbose Display the programs invoked\n"
284 " --version Print the version and exit\n"
285 " -w, --warnings Turn on warnings\n"
287 " --dll Build a .c file from a .spec or .def file\n"
288 " --def Build a .def file from a .spec file\n"
289 " --exe Build a .c file for an executable\n"
290 " --implib Build an import library\n"
291 " --resources Build a .o file for the resource files\n\n"
292 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
294 enum long_options_values
302 LONG_OPT_EXTERNAL_SYMS
,
303 LONG_OPT_FAKE_MODULE
,
304 LONG_OPT_LARGE_ADDRESS_AWARE
,
314 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";
316 static const struct option long_options
[] =
318 { "dll", 0, 0, LONG_OPT_DLL
},
319 { "def", 0, 0, LONG_OPT_DEF
},
320 { "exe", 0, 0, LONG_OPT_EXE
},
321 { "implib", 0, 0, LONG_OPT_IMPLIB
},
322 { "as-cmd", 1, 0, LONG_OPT_ASCMD
},
323 { "cc-cmd", 1, 0, LONG_OPT_CCCMD
},
324 { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS
},
325 { "fake-module", 0, 0, LONG_OPT_FAKE_MODULE
},
326 { "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE
},
327 { "ld-cmd", 1, 0, LONG_OPT_LDCMD
},
328 { "nm-cmd", 1, 0, LONG_OPT_NMCMD
},
329 { "nxcompat", 1, 0, LONG_OPT_NXCOMPAT
},
330 { "resources", 0, 0, LONG_OPT_RESOURCES
},
331 { "save-temps", 0, 0, LONG_OPT_SAVE_TEMPS
},
332 { "subsystem", 1, 0, LONG_OPT_SUBSYSTEM
},
333 { "version", 0, 0, LONG_OPT_VERSION
},
334 /* aliases for short options */
335 { "target", 1, 0, 'b' },
336 { "delay-lib", 1, 0, 'd' },
337 { "export", 1, 0, 'E' },
338 { "entry", 1, 0, 'e' },
339 { "filename", 1, 0, 'F' },
340 { "help", 0, 0, 'h' },
341 { "heap", 1, 0, 'H' },
342 { "kill-at", 0, 0, 'k' },
343 { "library", 1, 0, 'l' },
344 { "library-path", 1, 0, 'L' },
345 { "main-module", 1, 0, 'M' },
346 { "dll-name", 1, 0, 'N' },
347 { "output", 1, 0, 'o' },
348 { "res", 1, 0, 'r' },
349 { "undefined", 1, 0, 'u' },
350 { "verbose", 0, 0, 'v' },
351 { "warnings", 0, 0, 'w' },
355 static void usage( int exit_code
)
357 fprintf( stderr
, "%s", usage_str
);
361 static void set_exec_mode( enum exec_mode_values mode
)
363 if (exec_mode
!= MODE_NONE
) usage(1);
367 /* parse options from the argv array and remove all the recognized ones */
368 static char **parse_options( int argc
, char **argv
, DLLSPEC
*spec
)
374 while ((optc
= getopt_long( argc
, argv
, short_options
, long_options
, NULL
)) != -1)
382 spec_file_name
= xstrdup( optarg
);
383 set_dll_file_name( optarg
, spec
);
386 spec
->file_name
= xstrdup( optarg
);
389 if (!isdigit(optarg
[0]))
390 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg
);
391 spec
->heap_size
= atoi(optarg
);
392 if (spec
->heap_size
> 65535)
393 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec
->heap_size
);
399 /* ignored, because cc generates correct code. */
402 lib_path
= xrealloc( lib_path
, (nb_lib_paths
+1) * sizeof(*lib_path
) );
403 lib_path
[nb_lib_paths
++] = xstrdup( optarg
);
406 if (!strcmp( optarg
, "16" )) spec
->type
= SPEC_WIN16
;
407 else if (!strcmp( optarg
, "32" )) force_pointer_size
= 4;
408 else if (!strcmp( optarg
, "64" )) force_pointer_size
= 8;
409 else if (!strcmp( optarg
, "arm" )) thumb_mode
= 0;
410 else if (!strcmp( optarg
, "thumb" )) thumb_mode
= 1;
411 else if (!strncmp( optarg
, "cpu=", 4 )) cpu_option
= xstrdup( optarg
+ 4 );
412 else if (!strncmp( optarg
, "arch=", 5 )) arch_option
= xstrdup( optarg
+ 5 );
413 else fatal_error( "Unknown -m option '%s'\n", optarg
);
416 spec
->main_module
= xstrdup( optarg
);
419 spec
->dll_name
= xstrdup( optarg
);
422 set_target( optarg
);
425 add_delayed_import( optarg
);
428 spec
->init_func
= xstrdup( optarg
);
429 if ((p
= strchr( spec
->init_func
, '@' ))) *p
= 0; /* kill stdcall decoration */
432 if (!strcmp( optarg
, "PIC") || !strcmp( optarg
, "pic")) UsePIC
= 1;
433 else if (!strcmp( optarg
, "asynchronous-unwind-tables")) unwind_tables
= 1;
434 else if (!strcmp( optarg
, "no-asynchronous-unwind-tables")) unwind_tables
= 0;
435 /* ignore all other flags */
444 add_import_dll( optarg
, NULL
);
448 char *ext
= strrchr( optarg
, '.' );
450 if (unlink( optarg
) == -1 && errno
!= ENOENT
)
451 fatal_error( "Unable to create output file '%s'\n", optarg
);
452 if (ext
&& !strcmp( ext
, ".o" ))
454 output_file_source_name
= get_temp_file_name( optarg
, ".s" );
455 if (!(output_file
= fopen( output_file_source_name
, "w" )))
456 fatal_error( "Unable to create output file '%s'\n", optarg
);
460 if (!(output_file
= fopen( optarg
, "w" )))
461 fatal_error( "Unable to create output file '%s'\n", optarg
);
463 output_file_name
= xstrdup(optarg
);
464 atexit( cleanup
); /* make sure we remove the output file on exit */
468 res_files
= xrealloc( res_files
, (nb_res_files
+1) * sizeof(*res_files
) );
469 res_files
[nb_res_files
++] = xstrdup( optarg
);
472 add_extra_ld_symbol( optarg
);
478 display_warnings
= 1;
481 set_exec_mode( MODE_DLL
);
484 set_exec_mode( MODE_DEF
);
487 set_exec_mode( MODE_EXE
);
488 if (!spec
->subsystem
) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
490 case LONG_OPT_IMPLIB
:
491 set_exec_mode( MODE_IMPLIB
);
494 as_command
= strarray_fromstring( optarg
, " " );
497 cc_command
= strarray_fromstring( optarg
, " " );
499 case LONG_OPT_FAKE_MODULE
:
502 case LONG_OPT_EXTERNAL_SYMS
:
503 link_ext_symbols
= 1;
505 case LONG_OPT_LARGE_ADDRESS_AWARE
:
506 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
509 ld_command
= strarray_fromstring( optarg
, " " );
512 nm_command
= strarray_fromstring( optarg
, " " );
514 case LONG_OPT_NXCOMPAT
:
515 if (optarg
[0] == 'n' || optarg
[0] == 'N')
516 spec
->dll_characteristics
&= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
518 case LONG_OPT_RESOURCES
:
519 set_exec_mode( MODE_RESOURCES
);
521 case LONG_OPT_SAVE_TEMPS
:
524 case LONG_OPT_SUBSYSTEM
:
525 set_subsystem( optarg
, spec
);
527 case LONG_OPT_VERSION
:
528 printf( "winebuild version " PACKAGE_VERSION
"\n" );
536 if (!save_temps
) atexit( cleanup_tmp_files
);
538 if (spec
->file_name
&& !strchr( spec
->file_name
, '.' ))
539 strcat( spec
->file_name
, exec_mode
== MODE_EXE
? ".exe" : ".dll" );
540 init_dll_name( spec
);
545 if (force_pointer_size
== 8) target_cpu
= CPU_x86_64
;
548 if (force_pointer_size
== 4) target_cpu
= CPU_x86
;
551 if (force_pointer_size
== 8)
552 fatal_error( "Cannot build 64-bit code for this CPU\n" );
556 return &argv
[optind
];
560 /* load all specified resource files */
561 static void load_resources( char *argv
[], DLLSPEC
*spec
)
569 for (i
= 0; i
< nb_res_files
; i
++) load_res16_file( res_files
[i
], spec
);
573 for (i
= 0; i
< nb_res_files
; i
++)
575 if (!load_res32_file( res_files
[i
], spec
))
576 fatal_error( "%s is not a valid Win32 resource file\n", res_files
[i
] );
579 /* load any resource file found in the remaining arguments */
580 for (ptr
= last
= argv
; *ptr
; ptr
++)
582 if (!load_res32_file( *ptr
, spec
))
583 *last
++ = *ptr
; /* not a resource file, keep it in the list */
590 /* add input files that look like import libs to the import list */
591 static void load_import_libs( char *argv
[] )
595 for (ptr
= last
= argv
; *ptr
; ptr
++)
597 if (strendswith( *ptr
, ".def" ))
598 add_import_dll( NULL
, *ptr
);
600 *last
++ = *ptr
; /* not an import dll, keep it in the list */
605 static int parse_input_file( DLLSPEC
*spec
)
607 FILE *input_file
= open_input_file( NULL
, spec_file_name
);
608 char *extension
= strrchr( spec_file_name
, '.' );
611 spec
->src_name
= xstrdup( input_file_name
);
612 if (extension
&& !strcmp( extension
, ".def" ))
613 result
= parse_def_file( input_file
, spec
);
615 result
= parse_spec_file( input_file
, spec
);
616 close_input_file( input_file
);
621 /*******************************************************************
624 int main(int argc
, char **argv
)
626 DLLSPEC
*spec
= alloc_dll_spec();
629 signal( SIGHUP
, exit_on_signal
);
631 signal( SIGTERM
, exit_on_signal
);
632 signal( SIGINT
, exit_on_signal
);
634 output_file
= stdout
;
635 argv
= parse_options( argc
, argv
, spec
);
640 if (spec
->subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
641 spec
->characteristics
|= IMAGE_FILE_DLL
;
644 load_resources( argv
, spec
);
645 load_import_libs( argv
);
646 if (spec_file_name
&& !parse_input_file( spec
)) break;
649 if (spec
->type
== SPEC_WIN16
) output_fake_module16( spec
);
650 else output_fake_module( spec
);
653 read_undef_symbols( spec
, argv
);
657 output_spec16_file( spec
);
660 BuildSpec32File( spec
);
666 if (argv
[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[0] );
667 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
668 if (!parse_input_file( spec
)) break;
669 output_def_file( spec
, 1 );
672 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
673 if (!parse_input_file( spec
)) break;
674 output_import_lib( spec
, argv
);
677 load_resources( argv
, spec
);
678 output_res_o_file( spec
);
684 if (nb_errors
) exit(1);
685 if (output_file_name
)
687 if (fclose( output_file
) < 0) fatal_perror( "fclose" );
688 if (output_file_source_name
) assemble_file( output_file_source_name
, output_file_name
);
689 output_file_name
= NULL
;