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"
39 ORDDEF *EntryPoints[MAX_ORDINALS];
40 ORDDEF *Ordinals[MAX_ORDINALS];
41 ORDDEF *Names[MAX_ORDINALS];
43 SPEC_MODE SpecMode = SPEC_MODE_DLL;
44 SPEC_TYPE SpecType = SPEC_WIN32;
46 int Base = MAX_ORDINALS;
51 int nb_entry_points = 0;
53 int nb_debug_channels = 0;
56 int display_warnings = 0;
59 /* we only support relay debugging on i386 */
60 #if defined(__i386__) && !defined(NO_TRACE_MSGS)
66 char *owner_name = NULL;
67 char *dll_name = NULL;
68 char *dll_file_name = NULL;
69 const char *init_func = NULL;
70 char **debug_channels = NULL;
71 char **lib_path = NULL;
73 char *input_file_name = NULL;
74 const char *output_file_name = NULL;
76 static FILE *input_file;
77 static FILE *output_file;
78 static const char *current_src_dir;
79 static int nb_res_files;
80 static char **res_files;
94 static enum exec_mode_values exec_mode = MODE_NONE;
96 /* set the dll file name from the input file name */
97 static void set_dll_file_name( const char *name )
101 if (dll_file_name) return;
103 if ((p = strrchr( name, '\\' ))) name = p + 1;
104 if ((p = strrchr( name, '/' ))) name = p + 1;
105 dll_file_name = xmalloc( strlen(name) + 5 );
106 strcpy( dll_file_name, name );
107 if ((p = strrchr( dll_file_name, '.' )) && !strcmp( p, ".spec" )) *p = 0;
108 if (!strchr( dll_file_name, '.' )) strcat( dll_file_name, ".dll" );
111 /* cleanup on program exit */
112 static void cleanup(void)
114 if (output_file_name) unlink( output_file_name );
118 /*******************************************************************
119 * command-line option handling
121 static const char usage_str[] =
122 "Usage: winebuild [OPTIONS] [FILES]\n\n"
124 " -C --source-dir=DIR Look for source files in DIR\n"
125 " -d --delay-lib=LIB Import the specified library in delayed mode\n"
126 " -D SYM Ignored for C flags compatibility\n"
127 " -e --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
128 " -f FLAGS Compiler flags (only -fPIC is supported)\n"
129 " -F --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
130 " -h --help Display this help message\n"
131 " -H --heap=SIZE Set the heap size for a Win16 dll\n"
132 " -i --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
133 " -I DIR Ignored for C flags compatibility\n"
134 " -k --kill-at Kill stdcall decorations in generated .def files\n"
135 " -K FLAGS Compiler flags (only -KPIC is supported)\n"
136 " -l --library=LIB Import the specified library\n"
137 " -L --library-path=DIR Look for imports libraries in DIR\n"
138 " -m --exe-mode=MODE Set the executable mode (cui|gui|cuiw|guiw)\n"
139 " -M --main-module=MODULE Set the name of the main module for a Win16 dll\n"
140 " -N --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
141 " -o --output=NAME Set the output file name (default: stdout)\n"
142 " -r --res=RSRC.RES Load resources from RSRC.RES\n"
143 " --version Print the version and exit\n"
144 " -w --warnings Turn on warnings\n"
146 " --spec=FILE.SPEC Build a .c file from a spec file\n"
147 " --def=FILE.SPEC Build a .def file from a spec file\n"
148 " --exe=NAME Build a .c file for the named executable\n"
149 " --debug [FILES] Build a .c file with the debug channels declarations\n"
150 " --relay16 Build the 16-bit relay assembly routines\n"
151 " --relay32 Build the 32-bit relay assembly routines\n\n"
152 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
154 enum long_options_values
165 static const char short_options[] = "C:D:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
167 static const struct option long_options[] =
169 { "spec", 1, 0, LONG_OPT_SPEC },
170 { "def", 1, 0, LONG_OPT_DEF },
171 { "exe", 1, 0, LONG_OPT_EXE },
172 { "debug", 0, 0, LONG_OPT_DEBUG },
173 { "relay16", 0, 0, LONG_OPT_RELAY16 },
174 { "relay32", 0, 0, LONG_OPT_RELAY32 },
175 { "version", 0, 0, LONG_OPT_VERSION },
176 /* aliases for short options */
177 { "source-dir", 1, 0, 'C' },
178 { "delay-lib", 1, 0, 'd' },
179 { "entry", 1, 0, 'e' },
180 { "filename", 1, 0, 'F' },
181 { "help", 0, 0, 'h' },
182 { "heap", 1, 0, 'H' },
183 { "ignore", 1, 0, 'i' },
184 { "kill-at", 0, 0, 'k' },
185 { "library", 1, 0, 'l' },
186 { "library-path", 1, 0, 'L' },
187 { "exe-mode", 1, 0, 'm' },
188 { "main-module", 1, 0, 'M' },
189 { "dll-name", 1, 0, 'N' },
190 { "output", 1, 0, 'o' },
191 { "res", 1, 0, 'r' },
192 { "warnings", 0, 0, 'w' },
196 static void usage( int exit_code )
198 fprintf( stderr, "%s", usage_str );
202 static void set_exec_mode( enum exec_mode_values mode )
204 if (exec_mode != MODE_NONE) usage(1);
208 /* parse options from the argv array and remove all the recognized ones */
209 static char **parse_options( int argc, char **argv )
214 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
219 current_src_dir = optarg;
225 dll_file_name = xstrdup( optarg );
228 if (!isdigit(optarg[0]))
229 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
230 DLLHeapSize = atoi(optarg);
231 if (DLLHeapSize > 65535)
232 fatal_error( "Invalid heap size %d, maximum is 65535\n", DLLHeapSize );
238 /* ignored, because cc generates correct code. */
241 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
242 lib_path[nb_lib_paths++] = xstrdup( optarg );
245 owner_name = xstrdup( optarg );
246 SpecType = SPEC_WIN16;
249 dll_name = xstrdup( optarg );
252 add_import_dll( optarg, 1 );
255 init_func = xstrdup( optarg );
256 if ((p = strchr( init_func, '@' ))) *p = 0; /* kill stdcall decoration */
259 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
260 /* ignore all other flags */
267 char *str = xstrdup( optarg );
268 char *token = strtok( str, "," );
271 add_ignore_symbol( token );
272 token = strtok( NULL, "," );
281 add_import_dll( optarg, 0 );
284 if (!strcmp( optarg, "gui" )) SpecMode = SPEC_MODE_GUIEXE;
285 else if (!strcmp( optarg, "cui" )) SpecMode = SPEC_MODE_CUIEXE;
286 else if (!strcmp( optarg, "guiw" )) SpecMode = SPEC_MODE_GUIEXE_UNICODE;
287 else if (!strcmp( optarg, "cuiw" )) SpecMode = SPEC_MODE_CUIEXE_UNICODE;
291 if (unlink( optarg ) == -1 && errno != ENOENT)
292 fatal_error( "Unable to create output file '%s'\n", optarg );
293 if (!(output_file = fopen( optarg, "w" )))
294 fatal_error( "Unable to create output file '%s'\n", optarg );
295 output_file_name = xstrdup(optarg);
296 atexit( cleanup ); /* make sure we remove the output file on exit */
299 res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
300 res_files[nb_res_files++] = xstrdup( optarg );
303 display_warnings = 1;
306 set_exec_mode( MODE_SPEC );
307 input_file = open_input_file( NULL, optarg );
308 set_dll_file_name( optarg );
311 set_exec_mode( MODE_DEF );
312 input_file = open_input_file( NULL, optarg );
313 set_dll_file_name( optarg );
316 set_exec_mode( MODE_EXE );
317 if ((p = strrchr( optarg, '/' ))) p++;
319 dll_file_name = xmalloc( strlen(p) + 5 );
320 strcpy( dll_file_name, p );
321 if (!strchr( dll_file_name, '.' )) strcat( dll_file_name, ".exe" );
322 if (SpecMode == SPEC_MODE_DLL) SpecMode = SPEC_MODE_GUIEXE;
325 set_exec_mode( MODE_DEBUG );
327 case LONG_OPT_RELAY16:
328 set_exec_mode( MODE_RELAY16 );
330 case LONG_OPT_RELAY32:
331 set_exec_mode( MODE_RELAY32 );
333 case LONG_OPT_VERSION:
334 printf( "winebuild version " PACKAGE_VERSION "\n" );
341 return &argv[optind];
345 /* load all specified resource files */
346 static void load_resources( char *argv[] )
354 for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i] );
358 for (i = 0; i < nb_res_files; i++)
360 if (!load_res32_file( res_files[i] ))
361 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
364 /* load any resource file found in the remaining arguments */
365 for (ptr = last = argv; *ptr; ptr++)
367 if (!load_res32_file( *ptr ))
368 *last++ = *ptr; /* not a resource file, keep it in the list */
375 /*******************************************************************
378 int main(int argc, char **argv)
380 output_file = stdout;
381 argv = parse_options( argc, argv );
386 load_resources( argv );
387 if (!ParseTopLevel( input_file )) break;
392 fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
393 BuildSpec16File( output_file );
396 read_undef_symbols( argv );
397 BuildSpec32File( output_file );
403 if (SpecType == SPEC_WIN16) fatal_error( "Cannot build 16-bit exe files\n" );
404 load_resources( argv );
405 read_undef_symbols( argv );
406 BuildSpec32File( output_file );
409 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
410 if (SpecType == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
411 if (!ParseTopLevel( input_file )) break;
412 BuildDef32File( output_file );
415 BuildDebugFile( output_file, current_src_dir, argv );
418 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
419 BuildRelays16( output_file );
422 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
423 BuildRelays32( output_file );
429 if (nb_errors) exit(1);
430 if (output_file_name)
432 fclose( output_file );
433 output_file_name = NULL;