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
42 int nb_debug_channels
= 0;
45 int display_warnings
= 0;
50 enum target_cpu target_cpu
= CPU_x86
;
51 #elif defined(__sparc__)
52 enum target_cpu target_cpu
= CPU_SPARC
;
53 #elif defined(__ALPHA__)
54 enum target_cpu target_cpu
= CPU_ALPHA
;
55 #elif defined(__powerpc__)
56 enum target_cpu target_cpu
= CPU_POWERPC
;
58 #error Unsupported CPU
62 enum target_platform target_platform
= PLATFORM_APPLE
;
63 #elif defined(__svr4__)
64 enum target_platform target_platform
= PLATFORM_SVR4
;
65 #elif defined(_WINDOWS)
66 enum target_platform target_platform
= PLATFORM_WINDOWS
;
68 enum target_platform target_platform
= PLATFORM_UNSPECIFIED
;
71 char **debug_channels
= NULL
;
72 char **lib_path
= NULL
;
74 char *input_file_name
= NULL
;
75 char *spec_file_name
= NULL
;
76 const char *output_file_name
= NULL
;
78 char *ld_command
= "ld";
79 char *nm_command
= "nm";
81 static FILE *output_file
;
82 static const char *current_src_dir
;
83 static int nb_res_files
;
84 static char **res_files
;
99 static enum exec_mode_values exec_mode
= MODE_NONE
;
101 /* set the dll file name from the input file name */
102 static void set_dll_file_name( const char *name
, DLLSPEC
*spec
)
106 if (spec
->file_name
) return;
108 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
109 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
110 spec
->file_name
= xmalloc( strlen(name
) + 5 );
111 strcpy( spec
->file_name
, name
);
112 if ((p
= strrchr( spec
->file_name
, '.' )))
114 if (!strcmp( p
, ".spec" ) || !strcmp( p
, ".def" )) *p
= 0;
118 /* set the dll subsystem */
119 static void set_subsystem( const char *subsystem
, DLLSPEC
*spec
)
121 char *major
, *minor
, *str
= xstrdup( subsystem
);
123 if ((major
= strchr( str
, ':' ))) *major
++ = 0;
124 if (!strcmp( str
, "native" )) spec
->subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
125 else if (!strcmp( str
, "windows" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
126 else if (!strcmp( str
, "console" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
127 else fatal_error( "Invalid subsystem name '%s'\n", subsystem
);
130 if ((minor
= strchr( major
, '.' )))
133 spec
->subsystem_minor
= atoi( minor
);
135 spec
->subsystem_major
= atoi( major
);
140 /* cleanup on program exit */
141 static void cleanup(void)
143 if (output_file_name
) unlink( output_file_name
);
146 /* clean things up when aborting on a signal */
147 static void exit_on_signal( int sig
)
149 exit(1); /* this will call atexit functions */
152 /*******************************************************************
153 * command-line option handling
155 static const char usage_str
[] =
156 "Usage: winebuild [OPTIONS] [FILES]\n\n"
158 " -C --source-dir=DIR Look for source files in DIR\n"
159 " -d --delay-lib=LIB Import the specified library in delayed mode\n"
160 " -D SYM Ignored for C flags compatibility\n"
161 " -E --export=FILE Export the symbols defined in the .spec or .def file\n"
162 " -e --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
163 " -f FLAGS Compiler flags (only -fPIC is supported)\n"
164 " -F --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
165 " -h --help Display this help message\n"
166 " -H --heap=SIZE Set the heap size for a Win16 dll\n"
167 " -i --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
168 " -I DIR Ignored for C flags compatibility\n"
169 " -k --kill-at Kill stdcall decorations in generated .def files\n"
170 " -K FLAGS Compiler flags (only -KPIC is supported)\n"
171 " --ld-cmd=LD Command to use for linking (default: ld)\n"
172 " -l --library=LIB Import the specified library\n"
173 " -L --library-path=DIR Look for imports libraries in DIR\n"
174 " -M --main-module=MODULE Set the name of the main module for a Win16 dll\n"
175 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
176 " -N --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
177 " -o --output=NAME Set the output file name (default: stdout)\n"
178 " -r --res=RSRC.RES Load resources from RSRC.RES\n"
179 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console)\n"
180 " --version Print the version and exit\n"
181 " -w --warnings Turn on warnings\n"
183 " --dll Build a .c file from a .spec or .def file\n"
184 " --def Build a .def file from a .spec file\n"
185 " --exe Build a .c file for an executable\n"
186 " --debug [FILES] Build a .c file with the debug channels declarations\n"
187 " --relay16 Build the 16-bit relay assembly routines\n"
188 " --relay32 Build the 32-bit relay assembly routines\n"
189 " --pedll Build a .c file for PE dll\n\n"
190 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
192 enum long_options_values
207 static const char short_options
[] = "C:D:E:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
209 static const struct option long_options
[] =
211 { "dll", 0, 0, LONG_OPT_DLL
},
212 { "def", 0, 0, LONG_OPT_DEF
},
213 { "exe", 0, 0, LONG_OPT_EXE
},
214 { "debug", 0, 0, LONG_OPT_DEBUG
},
215 { "ld-cmd", 1, 0, LONG_OPT_LDCMD
},
216 { "nm-cmd", 1, 0, LONG_OPT_NMCMD
},
217 { "relay16", 0, 0, LONG_OPT_RELAY16
},
218 { "relay32", 0, 0, LONG_OPT_RELAY32
},
219 { "subsystem",1, 0, LONG_OPT_SUBSYSTEM
},
220 { "version", 0, 0, LONG_OPT_VERSION
},
221 { "pedll", 1, 0, LONG_OPT_PEDLL
},
222 /* aliases for short options */
223 { "source-dir", 1, 0, 'C' },
224 { "delay-lib", 1, 0, 'd' },
225 { "export", 1, 0, 'E' },
226 { "entry", 1, 0, 'e' },
227 { "filename", 1, 0, 'F' },
228 { "help", 0, 0, 'h' },
229 { "heap", 1, 0, 'H' },
230 { "ignore", 1, 0, 'i' },
231 { "kill-at", 0, 0, 'k' },
232 { "library", 1, 0, 'l' },
233 { "library-path", 1, 0, 'L' },
234 { "main-module", 1, 0, 'M' },
235 { "dll-name", 1, 0, 'N' },
236 { "output", 1, 0, 'o' },
237 { "res", 1, 0, 'r' },
238 { "warnings", 0, 0, 'w' },
242 static void usage( int exit_code
)
244 fprintf( stderr
, "%s", usage_str
);
248 static void set_exec_mode( enum exec_mode_values mode
)
250 if (exec_mode
!= MODE_NONE
) usage(1);
254 /* parse options from the argv array and remove all the recognized ones */
255 static char **parse_options( int argc
, char **argv
, DLLSPEC
*spec
)
260 while ((optc
= getopt_long( argc
, argv
, short_options
, long_options
, NULL
)) != -1)
265 current_src_dir
= optarg
;
271 spec_file_name
= xstrdup( optarg
);
272 set_dll_file_name( optarg
, spec
);
275 spec
->file_name
= xstrdup( optarg
);
278 if (!isdigit(optarg
[0]))
279 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg
);
280 spec
->heap_size
= atoi(optarg
);
281 if (spec
->heap_size
> 65535)
282 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec
->heap_size
);
288 /* ignored, because cc generates correct code. */
291 lib_path
= xrealloc( lib_path
, (nb_lib_paths
+1) * sizeof(*lib_path
) );
292 lib_path
[nb_lib_paths
++] = xstrdup( optarg
);
295 spec
->owner_name
= xstrdup( optarg
);
296 spec
->type
= SPEC_WIN16
;
299 spec
->dll_name
= xstrdup( optarg
);
302 add_delayed_import( optarg
);
305 spec
->init_func
= xstrdup( optarg
);
306 if ((p
= strchr( spec
->init_func
, '@' ))) *p
= 0; /* kill stdcall decoration */
309 if (!strcmp( optarg
, "PIC") || !strcmp( optarg
, "pic")) UsePIC
= 1;
310 /* ignore all other flags */
317 char *str
= xstrdup( optarg
);
318 char *token
= strtok( str
, "," );
321 add_ignore_symbol( token
);
322 token
= strtok( NULL
, "," );
331 add_import_dll( optarg
, NULL
);
334 if (unlink( optarg
) == -1 && errno
!= ENOENT
)
335 fatal_error( "Unable to create output file '%s'\n", optarg
);
336 if (!(output_file
= fopen( optarg
, "w" )))
337 fatal_error( "Unable to create output file '%s'\n", optarg
);
338 output_file_name
= xstrdup(optarg
);
339 atexit( cleanup
); /* make sure we remove the output file on exit */
342 res_files
= xrealloc( res_files
, (nb_res_files
+1) * sizeof(*res_files
) );
343 res_files
[nb_res_files
++] = xstrdup( optarg
);
346 display_warnings
= 1;
349 set_exec_mode( MODE_DLL
);
352 set_exec_mode( MODE_DEF
);
355 set_exec_mode( MODE_EXE
);
356 if (!spec
->subsystem
) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
359 set_exec_mode( MODE_DEBUG
);
362 ld_command
= xstrdup( optarg
);
365 nm_command
= xstrdup( optarg
);
367 case LONG_OPT_RELAY16
:
368 set_exec_mode( MODE_RELAY16
);
370 case LONG_OPT_RELAY32
:
371 set_exec_mode( MODE_RELAY32
);
373 case LONG_OPT_SUBSYSTEM
:
374 set_subsystem( optarg
, spec
);
376 case LONG_OPT_VERSION
:
377 printf( "winebuild version " PACKAGE_VERSION
"\n" );
380 set_exec_mode( MODE_PEDLL
);
381 spec_file_name
= xstrdup( optarg
);
382 set_dll_file_name( optarg
, spec
);
390 if (spec
->file_name
&& !strchr( spec
->file_name
, '.' ))
391 strcat( spec
->file_name
, exec_mode
== MODE_EXE
? ".exe" : ".dll" );
393 return &argv
[optind
];
397 /* load all specified resource files */
398 static void load_resources( char *argv
[], DLLSPEC
*spec
)
406 for (i
= 0; i
< nb_res_files
; i
++) load_res16_file( res_files
[i
], spec
);
410 for (i
= 0; i
< nb_res_files
; i
++)
412 if (!load_res32_file( res_files
[i
], spec
))
413 fatal_error( "%s is not a valid Win32 resource file\n", res_files
[i
] );
416 /* load any resource file found in the remaining arguments */
417 for (ptr
= last
= argv
; *ptr
; ptr
++)
419 if (!load_res32_file( *ptr
, spec
))
420 *last
++ = *ptr
; /* not a resource file, keep it in the list */
427 /* add input files that look like import libs to the import list */
428 static void load_import_libs( char *argv
[] )
432 for (ptr
= last
= argv
; *ptr
; ptr
++)
434 if (strendswith( *ptr
, ".def" ))
435 add_import_dll( NULL
, *ptr
);
437 *last
++ = *ptr
; /* not an import dll, keep it in the list */
442 static int parse_input_file( DLLSPEC
*spec
)
444 FILE *input_file
= open_input_file( NULL
, spec_file_name
);
445 char *extension
= strrchr( spec_file_name
, '.' );
448 if (extension
&& !strcmp( extension
, ".def" ))
449 result
= parse_def_file( input_file
, spec
);
451 result
= parse_spec_file( input_file
, spec
);
452 close_input_file( input_file
);
457 /*******************************************************************
460 int main(int argc
, char **argv
)
462 DLLSPEC
*spec
= alloc_dll_spec();
465 signal( SIGHUP
, exit_on_signal
);
467 signal( SIGTERM
, exit_on_signal
);
468 signal( SIGINT
, exit_on_signal
);
470 output_file
= stdout
;
471 argv
= parse_options( argc
, argv
, spec
);
473 /* we only support relay debugging on i386 */
474 debugging
= (target_cpu
== CPU_x86
);
479 spec
->characteristics
|= IMAGE_FILE_DLL
;
480 load_resources( argv
, spec
);
481 load_import_libs( argv
);
482 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
483 if (!parse_input_file( spec
)) break;
487 fatal_error( "Win16 specs are not supported in ReactOS version of winebuild\n" );
490 read_undef_symbols( argv
);
491 BuildSpec32File( output_file
, spec
);
497 if (spec
->type
== SPEC_WIN16
) fatal_error( "Cannot build 16-bit exe files\n" );
498 if (!spec
->file_name
) fatal_error( "executable must be named via the -F option\n" );
499 load_resources( argv
, spec
);
500 load_import_libs( argv
);
501 if (spec_file_name
&& !parse_input_file( spec
)) break;
502 read_undef_symbols( argv
);
503 BuildSpec32File( output_file
, spec
);
506 if (argv
[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[0] );
507 if (spec
->type
== SPEC_WIN16
) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
508 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
509 if (!parse_input_file( spec
)) break;
510 BuildDef32File( output_file
, spec
);
513 BuildDebugFile( output_file
, current_src_dir
, argv
);
516 fatal_error( "Win16 relays are not supported in ReactOS version of winebuild\n" );
519 fatal_error( "Win32 relays are not supported in ReactOS version of winebuild\n" );
522 if (argv
[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[0] );
523 if (!parse_input_file( spec
)) break;
524 BuildPedllFile( output_file
, spec
);
530 if (nb_errors
) exit(1);
531 if (output_file_name
)
533 fclose( output_file
);
534 output_file_name
= NULL
;