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;
48 /* we only support relay debugging on i386 */
55 char **debug_channels
= NULL
;
56 char **lib_path
= NULL
;
58 char *input_file_name
= NULL
;
59 char *spec_file_name
= NULL
;
60 const char *output_file_name
= NULL
;
62 char *ld_command
= "ld";
63 char *nm_command
= "nm";
65 static FILE *output_file
;
66 static const char *current_src_dir
;
67 static int nb_res_files
;
68 static char **res_files
;
83 static enum exec_mode_values exec_mode
= MODE_NONE
;
85 /* set the dll file name from the input file name */
86 static void set_dll_file_name( const char *name
, DLLSPEC
*spec
)
90 if (spec
->file_name
) return;
92 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
93 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
94 spec
->file_name
= xmalloc( strlen(name
) + 5 );
95 strcpy( spec
->file_name
, name
);
96 if ((p
= strrchr( spec
->file_name
, '.' )))
98 if (!strcmp( p
, ".spec" ) || !strcmp( p
, ".def" )) *p
= 0;
102 /* set the dll subsystem */
103 static void set_subsystem( const char *subsystem
, DLLSPEC
*spec
)
105 char *major
, *minor
, *str
= xstrdup( subsystem
);
107 if ((major
= strchr( str
, ':' ))) *major
++ = 0;
108 if (!strcmp( str
, "native" )) spec
->subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
109 else if (!strcmp( str
, "windows" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
110 else if (!strcmp( str
, "console" )) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
111 else fatal_error( "Invalid subsystem name '%s'\n", subsystem
);
114 if ((minor
= strchr( major
, '.' )))
117 spec
->subsystem_minor
= atoi( minor
);
119 spec
->subsystem_major
= atoi( major
);
124 /* cleanup on program exit */
125 static void cleanup(void)
127 if (output_file_name
) unlink( output_file_name
);
130 /* clean things up when aborting on a signal */
131 static void exit_on_signal( int sig
)
133 exit(1); /* this will call atexit functions */
136 /*******************************************************************
137 * command-line option handling
139 static const char usage_str
[] =
140 "Usage: winebuild [OPTIONS] [FILES]\n\n"
142 " -C --source-dir=DIR Look for source files in DIR\n"
143 " -d --delay-lib=LIB Import the specified library in delayed mode\n"
144 " -D SYM Ignored for C flags compatibility\n"
145 " -E --export=FILE Export the symbols defined in the .spec or .def file\n"
146 " -e --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
147 " -f FLAGS Compiler flags (only -fPIC is supported)\n"
148 " -F --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
149 " -h --help Display this help message\n"
150 " -H --heap=SIZE Set the heap size for a Win16 dll\n"
151 " -i --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
152 " -I DIR Ignored for C flags compatibility\n"
153 " -k --kill-at Kill stdcall decorations in generated .def files\n"
154 " -K FLAGS Compiler flags (only -KPIC is supported)\n"
155 " --ld-cmd=LD Command to use for linking (default: ld)\n"
156 " -l --library=LIB Import the specified library\n"
157 " -L --library-path=DIR Look for imports libraries in DIR\n"
158 " -M --main-module=MODULE Set the name of the main module for a Win16 dll\n"
159 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
160 " -N --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
161 " -o --output=NAME Set the output file name (default: stdout)\n"
162 " -r --res=RSRC.RES Load resources from RSRC.RES\n"
163 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console)\n"
164 " --version Print the version and exit\n"
165 " -w --warnings Turn on warnings\n"
167 " --dll Build a .c file from a .spec or .def file\n"
168 " --def Build a .def file from a .spec file\n"
169 " --exe Build a .c file for an executable\n"
170 " --debug [FILES] Build a .c file with the debug channels declarations\n"
171 " --relay16 Build the 16-bit relay assembly routines\n"
172 " --relay32 Build the 32-bit relay assembly routines\n"
173 " --pedll Build a .c file for PE dll\n\n"
174 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
176 enum long_options_values
191 static const char short_options
[] = "C:D:E:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
193 static const struct option long_options
[] =
195 { "dll", 0, 0, LONG_OPT_DLL
},
196 { "def", 0, 0, LONG_OPT_DEF
},
197 { "exe", 0, 0, LONG_OPT_EXE
},
198 { "debug", 0, 0, LONG_OPT_DEBUG
},
199 { "ld-cmd", 1, 0, LONG_OPT_LDCMD
},
200 { "nm-cmd", 1, 0, LONG_OPT_NMCMD
},
201 { "relay16", 0, 0, LONG_OPT_RELAY16
},
202 { "relay32", 0, 0, LONG_OPT_RELAY32
},
203 { "subsystem",1, 0, LONG_OPT_SUBSYSTEM
},
204 { "version", 0, 0, LONG_OPT_VERSION
},
205 { "pedll", 1, 0, LONG_OPT_PEDLL
},
206 /* aliases for short options */
207 { "source-dir", 1, 0, 'C' },
208 { "delay-lib", 1, 0, 'd' },
209 { "export", 1, 0, 'E' },
210 { "entry", 1, 0, 'e' },
211 { "filename", 1, 0, 'F' },
212 { "help", 0, 0, 'h' },
213 { "heap", 1, 0, 'H' },
214 { "ignore", 1, 0, 'i' },
215 { "kill-at", 0, 0, 'k' },
216 { "library", 1, 0, 'l' },
217 { "library-path", 1, 0, 'L' },
218 { "main-module", 1, 0, 'M' },
219 { "dll-name", 1, 0, 'N' },
220 { "output", 1, 0, 'o' },
221 { "res", 1, 0, 'r' },
222 { "warnings", 0, 0, 'w' },
226 static void usage( int exit_code
)
228 fprintf( stderr
, "%s", usage_str
);
232 static void set_exec_mode( enum exec_mode_values mode
)
234 if (exec_mode
!= MODE_NONE
) usage(1);
238 /* parse options from the argv array and remove all the recognized ones */
239 static char **parse_options( int argc
, char **argv
, DLLSPEC
*spec
)
244 while ((optc
= getopt_long( argc
, argv
, short_options
, long_options
, NULL
)) != -1)
249 current_src_dir
= optarg
;
255 spec_file_name
= xstrdup( optarg
);
256 set_dll_file_name( optarg
, spec
);
259 spec
->file_name
= xstrdup( optarg
);
262 if (!isdigit(optarg
[0]))
263 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg
);
264 spec
->heap_size
= atoi(optarg
);
265 if (spec
->heap_size
> 65535)
266 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec
->heap_size
);
272 /* ignored, because cc generates correct code. */
275 lib_path
= xrealloc( lib_path
, (nb_lib_paths
+1) * sizeof(*lib_path
) );
276 lib_path
[nb_lib_paths
++] = xstrdup( optarg
);
279 spec
->owner_name
= xstrdup( optarg
);
280 spec
->type
= SPEC_WIN16
;
283 spec
->dll_name
= xstrdup( optarg
);
286 add_import_dll( optarg
, 1 );
289 spec
->init_func
= xstrdup( optarg
);
290 if ((p
= strchr( spec
->init_func
, '@' ))) *p
= 0; /* kill stdcall decoration */
293 if (!strcmp( optarg
, "PIC") || !strcmp( optarg
, "pic")) UsePIC
= 1;
294 /* ignore all other flags */
301 char *str
= xstrdup( optarg
);
302 char *token
= strtok( str
, "," );
305 add_ignore_symbol( token
);
306 token
= strtok( NULL
, "," );
315 add_import_dll( optarg
, 0 );
318 if (unlink( optarg
) == -1 && errno
!= ENOENT
)
319 fatal_error( "Unable to create output file '%s'\n", optarg
);
320 if (!(output_file
= fopen( optarg
, "w" )))
321 fatal_error( "Unable to create output file '%s'\n", optarg
);
322 output_file_name
= xstrdup(optarg
);
323 atexit( cleanup
); /* make sure we remove the output file on exit */
326 res_files
= xrealloc( res_files
, (nb_res_files
+1) * sizeof(*res_files
) );
327 res_files
[nb_res_files
++] = xstrdup( optarg
);
330 display_warnings
= 1;
333 set_exec_mode( MODE_DLL
);
336 set_exec_mode( MODE_DEF
);
339 set_exec_mode( MODE_EXE
);
340 if (!spec
->subsystem
) spec
->subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
343 set_exec_mode( MODE_DEBUG
);
346 ld_command
= xstrdup( optarg
);
349 nm_command
= xstrdup( optarg
);
351 case LONG_OPT_RELAY16
:
352 set_exec_mode( MODE_RELAY16
);
354 case LONG_OPT_RELAY32
:
355 set_exec_mode( MODE_RELAY32
);
357 case LONG_OPT_SUBSYSTEM
:
358 set_subsystem( optarg
, spec
);
360 case LONG_OPT_VERSION
:
361 printf( "winebuild version " PACKAGE_VERSION
"\n" );
364 set_exec_mode( MODE_PEDLL
);
365 spec_file_name
= xstrdup( optarg
);
366 set_dll_file_name( optarg
, spec
);
374 if (spec
->file_name
&& !strchr( spec
->file_name
, '.' ))
375 strcat( spec
->file_name
, exec_mode
== MODE_EXE
? ".exe" : ".dll" );
377 return &argv
[optind
];
381 /* load all specified resource files */
382 static void load_resources( char *argv
[], DLLSPEC
*spec
)
390 for (i
= 0; i
< nb_res_files
; i
++) load_res16_file( res_files
[i
], spec
);
394 for (i
= 0; i
< nb_res_files
; i
++)
396 if (!load_res32_file( res_files
[i
], spec
))
397 fatal_error( "%s is not a valid Win32 resource file\n", res_files
[i
] );
400 /* load any resource file found in the remaining arguments */
401 for (ptr
= last
= argv
; *ptr
; ptr
++)
403 if (!load_res32_file( *ptr
, spec
))
404 *last
++ = *ptr
; /* not a resource file, keep it in the list */
411 static int parse_input_file( DLLSPEC
*spec
)
413 FILE *input_file
= open_input_file( NULL
, spec_file_name
);
414 char *extension
= strrchr( spec_file_name
, '.' );
417 if (extension
&& !strcmp( extension
, ".def" ))
418 result
= parse_def_file( input_file
, spec
);
420 result
= parse_spec_file( input_file
, spec
);
421 close_input_file( input_file
);
426 /*******************************************************************
429 int main(int argc
, char **argv
)
431 DLLSPEC
*spec
= alloc_dll_spec();
434 signal( SIGHUP
, exit_on_signal
);
436 signal( SIGTERM
, exit_on_signal
);
437 signal( SIGINT
, exit_on_signal
);
439 output_file
= stdout
;
440 argv
= parse_options( argc
, argv
, spec
);
445 spec
->characteristics
|= IMAGE_FILE_DLL
;
446 load_resources( argv
, spec
);
447 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
448 if (!parse_input_file( spec
)) break;
452 fatal_error( "Win16 specs are not supported in ReactOS version of winebuild\n" );
455 read_undef_symbols( argv
);
456 BuildSpec32File( output_file
, spec
);
462 if (spec
->type
== SPEC_WIN16
) fatal_error( "Cannot build 16-bit exe files\n" );
463 if (!spec
->file_name
) fatal_error( "executable must be named via the -F option\n" );
464 load_resources( argv
, spec
);
465 if (spec_file_name
&& !parse_input_file( spec
)) break;
466 read_undef_symbols( argv
);
467 BuildSpec32File( output_file
, spec
);
470 if (argv
[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[0] );
471 if (spec
->type
== SPEC_WIN16
) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
472 if (!spec_file_name
) fatal_error( "missing .spec file\n" );
473 if (!parse_input_file( spec
)) break;
474 BuildDef32File( output_file
, spec
);
477 BuildDebugFile( output_file
, current_src_dir
, argv
);
480 fatal_error( "Win16 relays are not supported in ReactOS version of winebuild\n" );
483 fatal_error( "Win32 relays are not supported in ReactOS version of winebuild\n" );
486 if (argv
[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv
[0] );
487 if (!parse_input_file( spec
)) break;
488 BuildPedllFile( output_file
, spec
);
494 if (nb_errors
) exit(1);
495 if (output_file_name
)
497 fclose( output_file
);
498 output_file_name
= NULL
;