cross-compile fixes for winebuild
[reactos.git] / reactos / tools / winebuild / main.c
1 /*
2 * Main function
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <ctype.h>
33 #ifdef HAVE_GETOPT_H
34 # include <getopt.h>
35 #endif
36
37 #include "build.h"
38
39 ORDDEF *EntryPoints[MAX_ORDINALS];
40 ORDDEF *Ordinals[MAX_ORDINALS];
41 ORDDEF *Names[MAX_ORDINALS];
42
43 SPEC_MODE SpecMode = SPEC_MODE_DLL;
44 SPEC_TYPE SpecType = SPEC_WIN32;
45
46 int Base = MAX_ORDINALS;
47 int Limit = 0;
48 int DLLHeapSize = 0;
49 int UsePIC = 0;
50 int stack_size = 0;
51 int nb_entry_points = 0;
52 int nb_names = 0;
53 int nb_debug_channels = 0;
54 int nb_lib_paths = 0;
55 int nb_errors = 0;
56 int display_warnings = 0;
57 int kill_at = 0;
58
59 /* we only support relay debugging on i386 */
60 #if defined(__i386__) && !defined(NO_TRACE_MSGS)
61 int debugging = 1;
62 #else
63 int debugging = 0;
64 #endif
65
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;
72
73 char *input_file_name = NULL;
74 const char *output_file_name = NULL;
75
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;
81
82 /* execution mode */
83 enum exec_mode_values
84 {
85 MODE_NONE,
86 MODE_SPEC,
87 MODE_EXE,
88 MODE_DEF,
89 MODE_DEBUG,
90 MODE_RELAY16,
91 MODE_RELAY32
92 };
93
94 static enum exec_mode_values exec_mode = MODE_NONE;
95
96 /* set the dll file name from the input file name */
97 static void set_dll_file_name( const char *name )
98 {
99 char *p;
100
101 if (dll_file_name) return;
102
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" );
109 }
110
111 /* cleanup on program exit */
112 static void cleanup(void)
113 {
114 if (output_file_name) unlink( output_file_name );
115 }
116
117
118 /*******************************************************************
119 * command-line option handling
120 */
121 static const char usage_str[] =
122 "Usage: winebuild [OPTIONS] [FILES]\n\n"
123 "Options:\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"
145 "\nMode options:\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";
153
154 enum long_options_values
155 {
156 LONG_OPT_SPEC = 1,
157 LONG_OPT_DEF,
158 LONG_OPT_EXE,
159 LONG_OPT_DEBUG,
160 LONG_OPT_RELAY16,
161 LONG_OPT_RELAY32,
162 LONG_OPT_VERSION
163 };
164
165 static const char short_options[] = "C:D:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
166
167 static const struct option long_options[] =
168 {
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' },
193 { NULL, 0, 0, 0 }
194 };
195
196 static void usage( int exit_code )
197 {
198 fprintf( stderr, "%s", usage_str );
199 exit( exit_code );
200 }
201
202 static void set_exec_mode( enum exec_mode_values mode )
203 {
204 if (exec_mode != MODE_NONE) usage(1);
205 exec_mode = mode;
206 }
207
208 /* parse options from the argv array and remove all the recognized ones */
209 static char **parse_options( int argc, char **argv )
210 {
211 char *p;
212 int optc;
213
214 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
215 {
216 switch(optc)
217 {
218 case 'C':
219 current_src_dir = optarg;
220 break;
221 case 'D':
222 /* ignored */
223 break;
224 case 'F':
225 dll_file_name = xstrdup( optarg );
226 break;
227 case 'H':
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 );
233 break;
234 case 'I':
235 /* ignored */
236 break;
237 case 'K':
238 /* ignored, because cc generates correct code. */
239 break;
240 case 'L':
241 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
242 lib_path[nb_lib_paths++] = xstrdup( optarg );
243 break;
244 case 'M':
245 owner_name = xstrdup( optarg );
246 SpecType = SPEC_WIN16;
247 break;
248 case 'N':
249 dll_name = xstrdup( optarg );
250 break;
251 case 'd':
252 add_import_dll( optarg, 1 );
253 break;
254 case 'e':
255 init_func = xstrdup( optarg );
256 if ((p = strchr( init_func, '@' ))) *p = 0; /* kill stdcall decoration */
257 break;
258 case 'f':
259 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
260 /* ignore all other flags */
261 break;
262 case 'h':
263 usage(0);
264 break;
265 case 'i':
266 {
267 char *str = xstrdup( optarg );
268 char *token = strtok( str, "," );
269 while (token)
270 {
271 add_ignore_symbol( token );
272 token = strtok( NULL, "," );
273 }
274 free( str );
275 }
276 break;
277 case 'k':
278 kill_at = 1;
279 break;
280 case 'l':
281 add_import_dll( optarg, 0 );
282 break;
283 case 'm':
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;
288 else usage(1);
289 break;
290 case 'o':
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 */
297 break;
298 case 'r':
299 res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
300 res_files[nb_res_files++] = xstrdup( optarg );
301 break;
302 case 'w':
303 display_warnings = 1;
304 break;
305 case LONG_OPT_SPEC:
306 set_exec_mode( MODE_SPEC );
307 input_file = open_input_file( NULL, optarg );
308 set_dll_file_name( optarg );
309 break;
310 case LONG_OPT_DEF:
311 set_exec_mode( MODE_DEF );
312 input_file = open_input_file( NULL, optarg );
313 set_dll_file_name( optarg );
314 break;
315 case LONG_OPT_EXE:
316 set_exec_mode( MODE_EXE );
317 if ((p = strrchr( optarg, '/' ))) p++;
318 else p = optarg;
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;
323 break;
324 case LONG_OPT_DEBUG:
325 set_exec_mode( MODE_DEBUG );
326 break;
327 case LONG_OPT_RELAY16:
328 set_exec_mode( MODE_RELAY16 );
329 break;
330 case LONG_OPT_RELAY32:
331 set_exec_mode( MODE_RELAY32 );
332 break;
333 case LONG_OPT_VERSION:
334 printf( "winebuild version " PACKAGE_VERSION "\n" );
335 exit(0);
336 case '?':
337 usage(1);
338 break;
339 }
340 }
341 return &argv[optind];
342 }
343
344
345 /* load all specified resource files */
346 static void load_resources( char *argv[] )
347 {
348 int i;
349 char **ptr, **last;
350
351 switch (SpecType)
352 {
353 case SPEC_WIN16:
354 for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i] );
355 break;
356
357 case SPEC_WIN32:
358 for (i = 0; i < nb_res_files; i++)
359 {
360 if (!load_res32_file( res_files[i] ))
361 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
362 }
363
364 /* load any resource file found in the remaining arguments */
365 for (ptr = last = argv; *ptr; ptr++)
366 {
367 if (!load_res32_file( *ptr ))
368 *last++ = *ptr; /* not a resource file, keep it in the list */
369 }
370 *last = NULL;
371 break;
372 }
373 }
374
375 /*******************************************************************
376 * main
377 */
378 int main(int argc, char **argv)
379 {
380 output_file = stdout;
381 argv = parse_options( argc, argv );
382
383 switch(exec_mode)
384 {
385 case MODE_SPEC:
386 load_resources( argv );
387 if (!ParseTopLevel( input_file )) break;
388 switch (SpecType)
389 {
390 case SPEC_WIN16:
391 #if defined(__REACTOS__)
392 fatal_error( "Win16 specs are not supported in ReactOS version of winebuild\n" );
393 #else
394 if (argv[0])
395 fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
396 BuildSpec16File( output_file );
397 #endif
398 break;
399 case SPEC_WIN32:
400 read_undef_symbols( argv );
401 BuildSpec32File( output_file );
402 break;
403 default: assert(0);
404 }
405 break;
406 case MODE_EXE:
407 if (SpecType == SPEC_WIN16) fatal_error( "Cannot build 16-bit exe files\n" );
408 load_resources( argv );
409 read_undef_symbols( argv );
410 BuildSpec32File( output_file );
411 break;
412 case MODE_DEF:
413 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
414 if (SpecType == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
415 if (!ParseTopLevel( input_file )) break;
416 BuildDef32File( output_file );
417 break;
418 case MODE_DEBUG:
419 BuildDebugFile( output_file, current_src_dir, argv );
420 break;
421 case MODE_RELAY16:
422 #if defined(__REACTOS__)
423 fatal_error( "Win16 relays are not supported in ReactOS version of winebuild\n" );
424 #else
425 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
426 BuildRelays16( output_file );
427 #endif
428 break;
429 case MODE_RELAY32:
430 #if defined(__REACTOS__)
431 fatal_error( "Win32 relays are not supported in ReactOS version of winebuild\n" );
432 #else
433 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
434 BuildRelays32( output_file );
435 #endif
436 break;
437 default:
438 usage(1);
439 break;
440 }
441 if (nb_errors) exit(1);
442 if (output_file_name)
443 {
444 fclose( output_file );
445 output_file_name = NULL;
446 }
447 return 0;
448 }