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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
44 int nb_debug_channels = 0;
47 int display_warnings = 0;
52 enum target_cpu target_cpu = CPU_x86;
53 #elif defined(__sparc__)
54 enum target_cpu target_cpu = CPU_SPARC;
55 #elif defined(__ALPHA__)
56 enum target_cpu target_cpu = CPU_ALPHA;
57 #elif defined(__powerpc__)
58 enum target_cpu target_cpu = CPU_POWERPC;
60 #error Unsupported CPU
64 enum target_platform target_platform = PLATFORM_APPLE;
65 #elif defined(__svr4__)
66 enum target_platform target_platform = PLATFORM_SVR4;
67 #elif defined(_WINDOWS)
68 enum target_platform target_platform = PLATFORM_WINDOWS;
70 enum target_platform target_platform = PLATFORM_UNSPECIFIED;
73 char **debug_channels = NULL;
74 char **lib_path = NULL;
76 char *input_file_name = NULL;
77 char *spec_file_name = NULL;
78 const char *output_file_name = NULL;
80 char *ld_command = "ld";
81 char *nm_command = "nm";
83 static FILE *output_file;
84 static const char *current_src_dir;
85 static int nb_res_files;
86 static char **res_files;
100 static enum exec_mode_values exec_mode = MODE_NONE;
102 /* set the dll file name from the input file name */
103 static void set_dll_file_name( const char *name, DLLSPEC *spec )
107 if (spec->file_name) return;
109 if ((p = strrchr( name, '\\' ))) name = p + 1;
110 if ((p = strrchr( name, '/' ))) name = p + 1;
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 subsystem */
120 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
122 char *major, *minor, *str = xstrdup( subsystem );
124 if ((major = strchr( str, ':' ))) *major++ = 0;
125 if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
126 else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
127 else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
128 else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
131 if ((minor = strchr( major, '.' )))
134 spec->subsystem_minor = atoi( minor );
136 spec->subsystem_major = atoi( major );
141 /* cleanup on program exit */
142 static void cleanup(void)
144 if (output_file_name) unlink( output_file_name );
147 /* clean things up when aborting on a signal */
148 static void exit_on_signal( int sig )
150 exit(1); /* this will call atexit functions */
153 /*******************************************************************
154 * command-line option handling
156 static const char usage_str[] =
157 "Usage: winebuild [OPTIONS] [FILES]\n\n"
159 " -C --source-dir=DIR Look for source files in DIR\n"
160 " -d --delay-lib=LIB Import the specified library in delayed mode\n"
161 " -D SYM Ignored for C flags compatibility\n"
162 " -E --export=FILE Export the symbols defined in the .spec or .def file\n"
163 " -e --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
164 " -f FLAGS Compiler flags (only -fPIC is supported)\n"
165 " -F --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
166 " -h --help Display this help message\n"
167 " -H --heap=SIZE Set the heap size for a Win16 dll\n"
168 " -i --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
169 " -I DIR Ignored for C flags compatibility\n"
170 " -k --kill-at Kill stdcall decorations in generated .def files\n"
171 " -K FLAGS Compiler flags (only -KPIC is supported)\n"
172 " --ld-cmd=LD Command to use for linking (default: ld)\n"
173 " -l --library=LIB Import the specified library\n"
174 " -L --library-path=DIR Look for imports libraries in DIR\n"
175 " -M --main-module=MODULE Set the name of the main module for a Win16 dll\n"
176 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
177 " -N --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
178 " -o --output=NAME Set the output file name (default: stdout)\n"
179 " -r --res=RSRC.RES Load resources from RSRC.RES\n"
180 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console)\n"
181 " --version Print the version and exit\n"
182 " -w --warnings Turn on warnings\n"
184 " --dll Build a .c file from a .spec or .def file\n"
185 " --def Build a .def file from a .spec file\n"
186 " --exe Build a .c file for an executable\n"
187 " --debug [FILES] Build a .c file with the debug channels declarations\n"
188 " --relay16 Build the 16-bit relay assembly routines\n"
189 " --relay32 Build the 32-bit relay assembly routines\n\n"
190 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
192 enum long_options_values
206 static const char short_options[] = "C:D:E:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
208 static const struct option long_options[] =
210 { "dll", 0, 0, LONG_OPT_DLL },
211 { "def", 0, 0, LONG_OPT_DEF },
212 { "exe", 0, 0, LONG_OPT_EXE },
213 { "debug", 0, 0, LONG_OPT_DEBUG },
214 { "ld-cmd", 1, 0, LONG_OPT_LDCMD },
215 { "nm-cmd", 1, 0, LONG_OPT_NMCMD },
216 { "relay16", 0, 0, LONG_OPT_RELAY16 },
217 { "relay32", 0, 0, LONG_OPT_RELAY32 },
218 { "subsystem",1, 0, LONG_OPT_SUBSYSTEM },
219 { "version", 0, 0, LONG_OPT_VERSION },
220 /* aliases for short options */
221 { "source-dir", 1, 0, 'C' },
222 { "delay-lib", 1, 0, 'd' },
223 { "export", 1, 0, 'E' },
224 { "entry", 1, 0, 'e' },
225 { "filename", 1, 0, 'F' },
226 { "help", 0, 0, 'h' },
227 { "heap", 1, 0, 'H' },
228 { "ignore", 1, 0, 'i' },
229 { "kill-at", 0, 0, 'k' },
230 { "library", 1, 0, 'l' },
231 { "library-path", 1, 0, 'L' },
232 { "main-module", 1, 0, 'M' },
233 { "dll-name", 1, 0, 'N' },
234 { "output", 1, 0, 'o' },
235 { "res", 1, 0, 'r' },
236 { "warnings", 0, 0, 'w' },
240 static void usage( int exit_code )
242 fprintf( stderr, "%s", usage_str );
246 static void set_exec_mode( enum exec_mode_values mode )
248 if (exec_mode != MODE_NONE) usage(1);
252 /* parse options from the argv array and remove all the recognized ones */
253 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
258 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
263 current_src_dir = optarg;
269 spec_file_name = xstrdup( optarg );
270 set_dll_file_name( optarg, spec );
273 spec->file_name = xstrdup( optarg );
276 if (!isdigit(optarg[0]))
277 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
278 spec->heap_size = atoi(optarg);
279 if (spec->heap_size > 65535)
280 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
286 /* ignored, because cc generates correct code. */
289 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
290 lib_path[nb_lib_paths++] = xstrdup( optarg );
293 spec->owner_name = xstrdup( optarg );
294 spec->type = SPEC_WIN16;
297 spec->dll_name = xstrdup( optarg );
300 add_delayed_import( optarg );
303 spec->init_func = xstrdup( optarg );
304 if ((p = strchr( spec->init_func, '@' ))) *p = 0; /* kill stdcall decoration */
307 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
308 /* ignore all other flags */
315 char *str = xstrdup( optarg );
316 char *token = strtok( str, "," );
319 add_ignore_symbol( token );
320 token = strtok( NULL, "," );
329 add_import_dll( optarg, NULL );
332 if (unlink( optarg ) == -1 && errno != ENOENT)
333 fatal_error( "Unable to create output file '%s'\n", optarg );
334 if (!(output_file = fopen( optarg, "w" )))
335 fatal_error( "Unable to create output file '%s'\n", optarg );
336 output_file_name = xstrdup(optarg);
337 atexit( cleanup ); /* make sure we remove the output file on exit */
340 res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
341 res_files[nb_res_files++] = xstrdup( optarg );
344 display_warnings = 1;
347 set_exec_mode( MODE_DLL );
350 set_exec_mode( MODE_DEF );
353 set_exec_mode( MODE_EXE );
354 if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
357 set_exec_mode( MODE_DEBUG );
360 ld_command = xstrdup( optarg );
363 nm_command = xstrdup( optarg );
365 case LONG_OPT_RELAY16:
366 set_exec_mode( MODE_RELAY16 );
368 case LONG_OPT_RELAY32:
369 set_exec_mode( MODE_RELAY32 );
371 case LONG_OPT_SUBSYSTEM:
372 set_subsystem( optarg, spec );
374 case LONG_OPT_VERSION:
375 printf( "winebuild version " PACKAGE_VERSION "\n" );
383 if (spec->file_name && !strchr( spec->file_name, '.' ))
384 strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
386 return &argv[optind];
390 /* load all specified resource files */
391 static void load_resources( char *argv[], DLLSPEC *spec )
399 for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
403 for (i = 0; i < nb_res_files; i++)
405 if (!load_res32_file( res_files[i], spec ))
406 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
409 /* load any resource file found in the remaining arguments */
410 for (ptr = last = argv; *ptr; ptr++)
412 if (!load_res32_file( *ptr, spec ))
413 *last++ = *ptr; /* not a resource file, keep it in the list */
420 /* add input files that look like import libs to the import list */
421 static void load_import_libs( char *argv[] )
425 for (ptr = last = argv; *ptr; ptr++)
427 if (strendswith( *ptr, ".def" ))
428 add_import_dll( NULL, *ptr );
430 *last++ = *ptr; /* not an import dll, keep it in the list */
435 static int parse_input_file( DLLSPEC *spec )
437 FILE *input_file = open_input_file( NULL, spec_file_name );
438 char *extension = strrchr( spec_file_name, '.' );
441 if (extension && !strcmp( extension, ".def" ))
442 result = parse_def_file( input_file, spec );
444 result = parse_spec_file( input_file, spec );
445 close_input_file( input_file );
450 /*******************************************************************
453 int main(int argc, char **argv)
455 DLLSPEC *spec = alloc_dll_spec();
458 signal( SIGHUP, exit_on_signal );
460 signal( SIGTERM, exit_on_signal );
461 signal( SIGINT, exit_on_signal );
463 output_file = stdout;
464 argv = parse_options( argc, argv, spec );
466 /* we only support relay debugging on i386 */
467 debugging = (target_cpu == CPU_x86);
472 spec->characteristics |= IMAGE_FILE_DLL;
473 load_resources( argv, spec );
474 load_import_libs( argv );
475 if (!spec_file_name) fatal_error( "missing .spec file\n" );
476 if (!parse_input_file( spec )) break;
481 fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
482 BuildSpec16File( output_file, spec );
485 read_undef_symbols( argv );
486 BuildSpec32File( output_file, spec );
492 if (spec->type == SPEC_WIN16) fatal_error( "Cannot build 16-bit exe files\n" );
493 if (!spec->file_name) fatal_error( "executable must be named via the -F option\n" );
494 load_resources( argv, spec );
495 load_import_libs( argv );
496 if (spec_file_name && !parse_input_file( spec )) break;
497 read_undef_symbols( argv );
498 BuildSpec32File( output_file, spec );
501 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
502 if (spec->type == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
503 if (!spec_file_name) fatal_error( "missing .spec file\n" );
504 if (!parse_input_file( spec )) break;
505 BuildDef32File( output_file, spec );
508 BuildDebugFile( output_file, current_src_dir, argv );
511 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
512 BuildRelays16( output_file );
515 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
516 BuildRelays32( output_file );
522 if (nb_errors) exit(1);
523 if (output_file_name)
525 fclose( output_file );
526 output_file_name = NULL;