Made the DLL version clash error message more verbose.
[wine.git] / tools / wrc / wrc.c
blob7255eaafa29c714197659233e818023c4955aec7
1 /*
3 * Copyright Martin von Loewis, 1994
4 * Copyrignt 1998 Bertho A. Stultiens (BS)
6 * 30-Apr-2000 BS - Integrated a new preprocessor (-E and -N)
7 * 20-Jun-1998 BS - Added -L option to prevent case conversion
8 * of embedded filenames.
10 * 08-Jun-1998 BS - Added -A option to generate autoregister code
11 * for winelib operation.
13 * 21-May-1998 BS - Removed the CPP option. Its internal now.
14 * - Added implementations for defines and includes
15 * on the commandline.
17 * 30-Apr-1998 BS - The options now contain nearly the entire alphabet.
18 * Seems to be a sign for too much freedom. I implemeted
19 * most of them as a user choice possibility for things
20 * that I do not know what to put there by default.
21 * - -l and -L options are now known as -t and -T.
23 * 23-Apr-1998 BS - Finally gave up on backward compatibility on the
24 * commandline (after a blessing from the newsgroup).
25 * So, I changed the lot.
27 * 17-Apr-1998 BS - Added many new command-line options but took care
28 * that it would not break old scripts (sigh).
30 * 16-Apr-1998 BS - There is not much left of the original source...
31 * I had to rewrite most of it because the parser
32 * changed completely with all the types etc..
36 #include "config.h"
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <assert.h>
43 #include <ctype.h>
44 #include <signal.h>
46 #include "wrc.h"
47 #include "utils.h"
48 #include "writeres.h"
49 #include "readres.h"
50 #include "dumpres.h"
51 #include "genres.h"
52 #include "newstruc.h"
53 #include "preproc.h"
54 #include "parser.h"
56 static char usage[] =
57 "Usage: wrc [options...] [infile[.rc|.res]]\n"
58 " -a n Alignment of resource (win16 only, default is 4)\n"
59 " -A Auto register resources (only with gcc 2.7 and better)\n"
60 " -b Create an assembly array from a binary .res file\n"
61 " -B x Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
62 " (win32 only; default is n[ative] which equals "
63 #ifdef WORDS_BIGENDIAN
64 "big"
65 #else
66 "little"
67 #endif
68 "-endian)\n"
69 " -c Add 'const' prefix to C constants\n"
70 " -C cp Set the resource's codepage to cp (default is 0)\n"
71 " -d n Set debug level to 'n'\n"
72 " -D id[=val] Define preprocessor identifier id=val\n"
73 " -e Disable recognition of win32 keywords in 16bit compile\n"
74 " -E Preprocess only\n"
75 " -g Add symbols to the global c namespace\n"
76 " -h Also generate a .h file\n"
77 " -H file Same as -h but written to file\n"
78 " -I path Set include search dir to path (multiple -I allowed)\n"
79 " -l lan Set default language to lan (default is neutral {0, 0})\n"
80 " -L Leave case of embedded filenames as is\n"
81 " -n Do not generate .s file\n"
82 " -N Do not preprocess input\n"
83 " -o file Output to file (default is infile.[res|s|h]\n"
84 " -p prefix Give a prefix for the generated names\n"
85 " -r Create binary .res file (compile only)\n"
86 " -s Add structure with win32/16 (PE/NE) resource directory\n"
87 " -t Generate indirect loadable resource tables\n"
88 " -T Generate only indirect loadable resources tables\n"
89 " -V Print version end exit\n"
90 " -w 16|32 Select win16 or win32 output (default is win32)\n"
91 " -W Enable pedantic warnings\n"
92 "Input is taken from stdin if no sourcefile specified.\n"
93 "Debug level 'n' is a bitmask with following meaning:\n"
94 " * 0x01 Tell which resource is parsed (verbose mode)\n"
95 " * 0x02 Dump internal structures\n"
96 " * 0x04 Create a parser trace (yydebug=1)\n"
97 " * 0x08 Preprocessor messages\n"
98 " * 0x10 Preprocessor lex messages\n"
99 " * 0x20 Preprocessor yacc trace\n"
100 "The -o option only applies to the final destination file, which is\n"
101 "in case of normal compile a .s file. You must use the '-H header.h'\n"
102 "option to override the header-filename.\n"
103 "If no input filename is given and the output name is not overridden\n"
104 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
107 char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
108 "Copyright 1998-2000 Bertho A. Stultiens\n"
109 " 1994 Martin von Loewis\n";
112 * Default prefix for resource names used in the C array.
113 * Option '-p name' sets it to 'name'
115 #ifdef NEED_UNDERSCORE_PREFIX
116 char *prefix = "__Resource";
117 #else
118 char *prefix = "_Resource";
119 #endif
122 * Set if compiling in 32bit mode (default).
124 int win32 = 1;
127 * Set when generated C variables should be prefixed with 'const'
129 int constant = 0;
132 * Create a .res file from the source and exit (-r option).
134 int create_res = 0;
137 * debuglevel == DEBUGLEVEL_NONE Don't bother
138 * debuglevel & DEBUGLEVEL_CHAT Say whats done
139 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
140 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
141 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
142 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
143 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
145 int debuglevel = DEBUGLEVEL_NONE;
148 * Recognize win32 keywords if set (-w 32 enforces this),
149 * otherwise set with -e option.
151 int extensions = 1;
154 * Set when creating C array from .res file (-b option).
156 int binary = 0;
159 * Set when an additional C-header is to be created in compile (-h option).
161 int create_header = 0;
164 * Set when the NE/PE resource directory should be dumped into
165 * the output file.
167 int create_dir = 0;
170 * Set when all symbols should be added to the global namespace (-g option)
172 int global = 0;
175 * Set when indirect loadable resource tables should be created (-t)
177 int indirect = 0;
180 * Set when _only_ indirect loadable resource tables should be created (-T)
182 int indirect_only = 0;
185 * NE segment resource aligment (-a option)
187 int alignment = 4;
188 int alignment_pwr;
191 * Cleared when the assembly file must be suppressed (-n option)
193 int create_s = 1;
196 * Language setting for resources (-l option)
198 language_t *currentlanguage = NULL;
201 * The codepage to write in win32 PE resource segment (-C option)
203 DWORD codepage = 0;
206 * Set when extra warnings should be generated (-W option)
208 int pedantic = 0;
211 * Set when autoregister code must be added to the output (-A option)
213 int auto_register = 0;
216 * Set when the case of embedded filenames should not be converted
217 * to lower case (-L option)
219 int leave_case = 0;
222 * The output byte-order of resources (set with -B)
224 int byteorder = WRC_BO_NATIVE;
227 * Set when _only_ to run the preprocessor (-E option)
229 int preprocess_only = 0;
232 * Set when _not_ to run the preprocessor (-N option)
234 int no_preprocess = 0;
236 char *output_name; /* The name given by the -o option */
237 char *input_name; /* The name given on the command-line */
238 char *header_name; /* The name given by the -H option */
239 char *temp_name; /* Temporary file for preprocess pipe */
241 int line_number = 1; /* The current line */
242 int char_number = 1; /* The current char pos within the line */
244 char *cmdline; /* The entire commandline */
245 time_t now; /* The time of start of wrc */
247 resource_t *resource_top; /* The top of the parsed resources */
249 int getopt (int argc, char *const *argv, const char *optstring);
250 static void rm_tempfile(void);
251 static void segvhandler(int sig);
253 int main(int argc,char *argv[])
255 extern char* optarg;
256 extern int optind;
257 int optc;
258 int lose = 0;
259 int ret;
260 int a;
261 int i;
262 int cmdlen;
264 signal(SIGSEGV, segvhandler);
266 now = time(NULL);
268 /* First rebuild the commandline to put in destination */
269 /* Could be done through env[], but not all OS-es support it */
270 cmdlen = 4; /* for "wrc " */
271 for(i = 1; i < argc; i++)
272 cmdlen += strlen(argv[i]) + 1;
273 cmdline = (char *)xmalloc(cmdlen);
274 strcpy(cmdline, "wrc ");
275 for(i = 1; i < argc; i++)
277 strcat(cmdline, argv[i]);
278 if(i < argc-1)
279 strcat(cmdline, " ");
282 while((optc = getopt(argc, argv, "a:AbB:cC:d:D:eEghH:I:l:LnNo:p:rstTVw:W")) != EOF)
284 switch(optc)
286 case 'a':
287 alignment = atoi(optarg);
288 break;
289 case 'A':
290 auto_register = 1;
291 break;
292 case 'b':
293 binary = 1;
294 break;
295 case 'B':
296 switch(optarg[0])
298 case 'n':
299 case 'N':
300 byteorder = WRC_BO_NATIVE;
301 break;
302 case 'l':
303 case 'L':
304 byteorder = WRC_BO_LITTLE;
305 break;
306 case 'b':
307 case 'B':
308 byteorder = WRC_BO_BIG;
309 break;
310 default:
311 fprintf(stderr, "Byteordering must be n[ative], l[ittle] or b[ig]\n");
312 lose++;
314 break;
315 case 'c':
316 constant = 1;
317 break;
318 case 'C':
319 codepage = strtol(optarg, NULL, 0);
320 break;
321 case 'd':
322 debuglevel = strtol(optarg, NULL, 0);
323 break;
324 case 'D':
325 add_cmdline_define(optarg);
326 break;
327 case 'e':
328 extensions = 0;
329 break;
330 case 'E':
331 preprocess_only = 1;
332 break;
333 case 'g':
334 global = 1;
335 break;
336 case 'H':
337 header_name = strdup(optarg);
338 /* Fall through */
339 case 'h':
340 create_header = 1;
341 break;
342 case 'I':
343 add_include_path(optarg);
344 break;
345 case 'l':
347 int lan;
348 lan = strtol(optarg, NULL, 0);
349 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
351 break;
352 case 'L':
353 leave_case = 1;
354 break;
355 case 'n':
356 create_s = 0;
357 break;
358 case 'N':
359 no_preprocess = 1;
360 break;
361 case 'o':
362 output_name = strdup(optarg);
363 break;
364 case 'p':
365 #ifdef NEED_UNDERSCORE_PREFIX
366 prefix = (char *)xmalloc(strlen(optarg)+2);
367 prefix[0] = '_';
368 strcpy(prefix+1, optarg);
369 #else
370 prefix = xstrdup(optarg);
371 #endif
372 break;
373 case 'r':
374 create_res = 1;
375 break;
376 case 's':
377 create_dir = 1;
378 break;
379 case 'T':
380 indirect_only = 1;
381 /* Fall through */
382 case 't':
383 indirect = 1;
384 break;
385 case 'V':
386 printf(version_string);
387 exit(0);
388 break;
389 case 'w':
390 if(!strcmp(optarg, "16"))
391 win32 = 0;
392 else if(!strcmp(optarg, "32"))
393 win32 = 1;
394 else
395 lose++;
396 break;
397 case 'W':
398 pedantic = 1;
399 break;
400 default:
401 lose++;
402 break;
406 if(lose)
408 fprintf(stderr, usage);
409 return 1;
412 /* Check the command line options for invalid combinations */
413 if(win32)
415 if(!extensions)
417 warning("Option -e ignored with 32bit compile\n");
418 extensions = 1;
422 if(create_res)
424 if(constant)
426 warning("Option -c ignored with compile to .res\n");
427 constant = 0;
430 if(create_header)
432 warning("Option -[h|H] ignored with compile to .res\n");
433 create_header = 0;
436 if(indirect)
438 warning("Option -l ignored with compile to .res\n");
439 indirect = 0;
442 if(indirect_only)
444 warning("Option -L ignored with compile to .res\n");
445 indirect_only = 0;
448 if(global)
450 warning("Option -g ignored with compile to .res\n");
451 global = 0;
454 if(create_dir)
456 error("Option -r and -s cannot be used together\n");
459 if(binary)
461 error("Option -r and -b cannot be used together\n");
465 if(byteorder != WRC_BO_NATIVE)
467 if(binary)
468 error("Forced byteordering not supported for binary resources\n");
471 if(preprocess_only)
473 if(constant)
475 warning("Option -c ignored with preprocess only\n");
476 constant = 0;
479 if(create_header)
481 warning("Option -[h|H] ignored with preprocess only\n");
482 create_header = 0;
485 if(indirect)
487 warning("Option -l ignored with preprocess only\n");
488 indirect = 0;
491 if(indirect_only)
493 error("Option -E and -L cannot be used together\n");
496 if(global)
498 warning("Option -g ignored with preprocess only\n");
499 global = 0;
502 if(create_dir)
504 warning("Option -s ignored with preprocess only\n");
505 create_dir = 0;
508 if(binary)
510 error("Option -E and -b cannot be used together\n");
513 if(no_preprocess)
515 error("Option -E and -N cannot be used together\n");
519 #if !defined(HAVE_WINE_CONSTRUCTOR)
520 if(auto_register)
522 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
523 auto_register = 0;
525 #endif
527 /* Set alignment power */
528 a = alignment;
529 for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
531 a >>= 1;
533 if(a != 1)
535 error("Alignment must be between 1 and 1024");
537 if((1 << alignment_pwr) != alignment)
539 error("Alignment must be a power of 2");
542 /* Kill io buffering when some kind of debuglevel is enabled */
543 if(debuglevel)
545 setbuf(stdout,0);
546 setbuf(stderr,0);
549 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
550 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
551 ppdebug = debuglevel & DEBUGLEVEL_PPTRACE ? 1 : 0;
552 pp_flex_debug = debuglevel & DEBUGLEVEL_PPLEX ? 1 : 0;
554 /* Set the default defined stuff */
555 add_cmdline_define("__WRC__=" WRC_STRINGIZE(WRC_MAJOR_VERSION));
556 add_cmdline_define("__WRC_MINOR__=" WRC_STRINGIZE(WRC_MINOR_VERSION));
557 add_cmdline_define("__WRC_MICRO__=" WRC_STRINGIZE(WRC_MICRO_VERSION));
558 add_cmdline_define("__WRC_PATCH__=" WRC_STRINGIZE(WRC_MICRO_VERSION));
560 add_cmdline_define("RC_INVOKED=1");
562 if(win32)
564 add_cmdline_define("__WIN32__=1");
565 add_cmdline_define("__FLAT__=1");
568 add_special_define("__FILE__");
569 add_special_define("__LINE__");
570 add_special_define("__DATE__");
571 add_special_define("__TIME__");
573 /* Check if the user set a language, else set default */
574 if(!currentlanguage)
575 currentlanguage = new_language(0, 0);
577 /* Check for input file on command-line */
578 if(optind < argc)
580 input_name = argv[optind];
583 if(binary && !input_name)
585 error("Binary mode requires .res file as input\n");
588 /* Generate appropriate outfile names */
589 if(!output_name && !preprocess_only)
591 output_name = dup_basename(input_name, binary ? ".res" : ".rc");
592 strcat(output_name, create_res ? ".res" : ".s");
595 if(!header_name && !create_res)
597 header_name = dup_basename(input_name, binary ? ".res" : ".rc");
598 strcat(header_name, ".h");
601 /* Run the preprocessor on the input */
602 if(!no_preprocess && !binary)
604 char *real_name;
606 * Preprocess the input to a temp-file, or stdout if
607 * no output was given.
610 chat("Starting preprocess");
612 if(preprocess_only && !output_name)
614 ppout = stdout;
616 else if(preprocess_only && output_name)
618 if(!(ppout = fopen(output_name, "wb")))
619 error("Could not open %s for writing\n", output_name);
621 else
623 if(!(temp_name = tmpnam(NULL)))
624 error("Could nor generate a temp-name\n");
625 temp_name = xstrdup(temp_name);
626 if(!(ppout = fopen(temp_name, "wb")))
627 error("Could not create a temp-file\n");
629 atexit(rm_tempfile);
632 real_name = input_name; /* Because it gets overwritten */
634 if(!input_name)
635 ppin = stdin;
636 else
638 if(!(ppin = fopen(input_name, "rb")))
639 error("Could not open %s\n", input_name);
642 fprintf(ppout, "# 1 \"%s\" 1\n", input_name ? input_name : "");
644 ret = ppparse();
646 input_name = real_name;
648 if(input_name)
649 fclose(ppin);
651 fclose(ppout);
653 input_name = temp_name;
655 if(ret)
656 exit(1); /* Error during preprocess */
658 if(preprocess_only)
659 exit(0);
662 if(!binary)
664 /* Go from .rc to .res or .s */
665 chat("Starting parse");
667 if(!(yyin = fopen(input_name, "rb")))
668 error("Could not open %s for input\n", input_name);
670 ret = yyparse();
672 if(input_name)
673 fclose(yyin);
675 if(ret)
677 /* Error during parse */
678 exit(1);
681 if(debuglevel & DEBUGLEVEL_DUMP)
682 dump_resources(resource_top);
684 /* Convert the internal lists to binary data */
685 resources2res(resource_top);
687 if(create_res)
689 chat("Writing .res-file");
690 write_resfile(output_name, resource_top);
692 else
694 if(create_s)
696 chat("Writing .s-file");
697 write_s_file(output_name, resource_top);
699 if(create_header)
701 chat("Writing .h-file");
702 write_h_file(header_name, resource_top);
707 else
709 /* Go from .res to .s */
710 chat("Reading .res-file");
711 resource_top = read_resfile(input_name);
712 if(create_s)
714 chat("Writing .s-file");
715 write_s_file(output_name, resource_top);
717 if(create_header)
719 chat("Writing .h-file");
720 write_h_file(header_name, resource_top);
724 return 0;
728 static void rm_tempfile(void)
730 if(temp_name)
731 unlink(temp_name);
734 static void segvhandler(int sig)
736 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
737 fflush(stdout);
738 fflush(stderr);
739 abort();