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
;
99 static int nb_res_files
;
100 static char **res_files
;
103 enum exec_mode_values
113 static enum exec_mode_values exec_mode
= MODE_NONE
;
118 enum target_platform platform
;
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
)
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
)
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 */
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
);
178 if ((minor
= strchr( major
, '.' )))
181 spec
->subsystem_minor
= atoi( minor
);
183 spec
->subsystem_major
= atoi( major
);
188 /* set the target CPU and platform */
189 static void set_target( const char *target
)
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
, '-' )))
203 if ((target_cpu
= get_cpu_from_name( spec
)) == -1)
204 fatal_error( "Unrecognized CPU '%s'\n", spec
);
206 if ((p
= strrchr( p
, '-' ))) platform
= p
+ 1;
208 else if (!strcmp( spec
, "mingw32" ))
210 target_cpu
= CPU_x86
;
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
;
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"
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"
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
298 LONG_OPT_EXTERNAL_SYMS
,
299 LONG_OPT_FAKE_MODULE
,
300 LONG_OPT_LARGE_ADDRESS_AWARE
,
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' },
351 static void usage( int exit_code
)
353 fprintf( stderr
, "%s", usage_str
);
357 static void set_exec_mode( enum exec_mode_values mode
)
359 if (exec_mode
!= MODE_NONE
) usage(1);
363 /* parse options from the argv array and remove all the recognized ones */
364 static char **parse_options( int argc
, char **argv
, DLLSPEC
*spec
)
370 while ((optc
= getopt_long( argc
, argv
, short_options
, long_options
, NULL
)) != -1)
378 spec_file_name
= xstrdup( optarg
);
379 set_dll_file_name( optarg
, spec
);
382 spec
->file_name
= xstrdup( optarg
);
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
);
395 /* ignored, because cc generates correct code. */
398 lib_path
= xrealloc( lib_path
, (nb_lib_paths
+1) * sizeof(*lib_path
) );
399 lib_path
[nb_lib_paths
++] = xstrdup( optarg
);
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
);
411 spec
->main_module
= xstrdup( optarg
);
414 spec
->dll_name
= xstrdup( optarg
);
417 set_target( optarg
);
420 add_delayed_import( optarg
);
423 spec
->init_func
= xstrdup( optarg
);
424 if ((p
= strchr( spec
->init_func
, '@' ))) *p
= 0; /* kill stdcall decoration */
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 */
439 add_import_dll( optarg
, NULL
);
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
);
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 */
463 res_files
= xrealloc( res_files
, (nb_res_files
+1) * sizeof(*res_files
) );
464 res_files
[nb_res_files
++] = xstrdup( optarg
);
467 add_extra_ld_symbol( optarg
);
473 display_warnings
= 1;
476 set_exec_mode( MODE_DLL
);
479 set_exec_mode( MODE_DEF
);
482 set_exec_mode( MODE_EXE
);
483 if (!spec
->subsystem
) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
485 case LONG_OPT_IMPLIB
:
486 set_exec_mode( MODE_IMPLIB
);
489 as_command
= strarray_fromstring( optarg
, " " );
492 cc_command
= strarray_fromstring( optarg
, " " );
494 case LONG_OPT_FAKE_MODULE
:
497 case LONG_OPT_EXTERNAL_SYMS
:
498 link_ext_symbols
= 1;
500 case LONG_OPT_LARGE_ADDRESS_AWARE
:
501 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
504 ld_command
= strarray_fromstring( optarg
, " " );
507 nm_command
= strarray_fromstring( optarg
, " " );
509 case LONG_OPT_NXCOMPAT
:
510 if (optarg
[0] == 'n' || optarg
[0] == 'N')
511 spec
->dll_characteristics
&= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
513 case LONG_OPT_RESOURCES
:
514 set_exec_mode( MODE_RESOURCES
);
516 case LONG_OPT_SAVE_TEMPS
:
519 case LONG_OPT_SUBSYSTEM
:
520 set_subsystem( optarg
, spec
);
522 case LONG_OPT_VERSION
:
523 printf( "winebuild version " PACKAGE_VERSION
"\n" );
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
);
540 if (force_pointer_size
== 8) target_cpu
= CPU_x86_64
;
543 if (force_pointer_size
== 4) target_cpu
= CPU_x86
;
546 if (force_pointer_size
== 8)
547 fatal_error( "Cannot build 64-bit code for this CPU\n" );
551 return &argv
[optind
];
555 /* load all specified resource files */
556 static void load_resources( char *argv
[], DLLSPEC
*spec
)
564 for (i
= 0; i
< nb_res_files
; i
++) load_res16_file( res_files
[i
], spec
);
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 */
585 /* add input files that look like import libs to the import list */
586 static void load_import_libs( char *argv
[] )
590 for (ptr
= last
= argv
; *ptr
; ptr
++)
592 if (strendswith( *ptr
, ".def" ))
593 add_import_dll( NULL
, *ptr
);
595 *last
++ = *ptr
; /* not an import dll, keep it in the list */
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
, '.' );
606 spec
->src_name
= xstrdup( input_file_name
);
607 if (extension
&& !strcmp( extension
, ".def" ))
608 result
= parse_def_file( input_file
, spec
);
610 result
= parse_spec_file( input_file
, spec
);
611 close_input_file( input_file
);
616 /*******************************************************************
619 int main(int argc
, char **argv
)
621 DLLSPEC
*spec
= alloc_dll_spec();
624 signal( SIGHUP
, exit_on_signal
);
626 signal( SIGTERM
, exit_on_signal
);
627 signal( SIGINT
, exit_on_signal
);
629 output_file
= stdout
;
630 argv
= parse_options( argc
, argv
, spec
);
635 if (spec
->subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
636 spec
->characteristics
|= IMAGE_FILE_DLL
;
639 load_resources( argv
, spec
);
640 load_import_libs( argv
);
641 if (spec_file_name
&& !parse_input_file( spec
)) break;
644 if (spec
->type
== SPEC_WIN16
) output_fake_module16( spec
);
645 else output_fake_module( spec
);
648 read_undef_symbols( spec
, argv
);
652 output_spec16_file( spec
);
655 BuildSpec32File( spec
);
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 );
667 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
668 if (!parse_input_file( spec
)) break;
669 output_import_lib( spec
, argv
);
672 load_resources( argv
, spec
);
673 output_res_o_file( spec
);
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
;