devenum/tests: Use wide character string literals.
[wine.git] / tools / widl / widl.c
bloba99eed4e9ddd4ff1457db2e68eeee123cb292ee5
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
5 * based on WRC code by Bertho Stultiens
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <signal.h>
36 #ifdef HAVE_GETOPT_H
37 # include <getopt.h>
38 #endif
40 #include "widl.h"
41 #include "utils.h"
42 #include "parser.h"
43 #include "wine/wpp.h"
44 #include "header.h"
46 static const char usage[] =
47 "Usage: widl [options...] infile.idl\n"
48 " or: widl [options...] --dlldata-only name1 [name2...]\n"
49 " --acf=file Use ACF file\n"
50 " -app_config Ignored, present for midl compatibility\n"
51 " -b arch Set the target architecture\n"
52 " -c Generate client stub\n"
53 " -d n Set debug level to 'n'\n"
54 " -D id[=val] Define preprocessor identifier id=val\n"
55 " -E Preprocess only\n"
56 " --help Display this help and exit\n"
57 " -h Generate headers\n"
58 " -H file Name of header file (default is infile.h)\n"
59 " -I path Set include search dir to path (multiple -I allowed)\n"
60 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
61 " -m32, -m64 Set the target architecture (Win32 or Win64)\n"
62 " -N Do not preprocess input\n"
63 " --nostdinc Do not search the standard include path\n"
64 " --ns_prefix Prefix namespaces with ABI namespace\n"
65 " --oldnames Use old naming conventions\n"
66 " -o, --output=NAME Set the output file name\n"
67 " -Otype Type of stubs to generate (-Os, -Oi, -Oif)\n"
68 " -p Generate proxy\n"
69 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
70 " --prefix-client=p Prefix names of client stubs with 'p'\n"
71 " --prefix-server=p Prefix names of server functions with 'p'\n"
72 " -r Generate registration script\n"
73 " -robust Ignored, present for midl compatibility\n"
74 " --sysroot=DIR Prefix include paths with DIR\n"
75 " -s Generate server stub\n"
76 " -t Generate typelib\n"
77 " -u Generate interface identifiers file\n"
78 " -V Print version and exit\n"
79 " -W Enable pedantic warnings\n"
80 " --win32, --win64 Set the target architecture (Win32 or Win64)\n"
81 " --win32-align n Set win32 structure alignment to 'n'\n"
82 " --win64-align n Set win64 structure alignment to 'n'\n"
83 " --winrt Enable Windows Runtime mode\n"
84 "Debug level 'n' is a bitmask with following meaning:\n"
85 " * 0x01 Tell which resource is parsed (verbose mode)\n"
86 " * 0x02 Dump internal structures\n"
87 " * 0x04 Create a parser trace (yydebug=1)\n"
88 " * 0x08 Preprocessor messages\n"
89 " * 0x10 Preprocessor lex messages\n"
90 " * 0x20 Preprocessor yacc trace\n"
93 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
94 "Copyright 2002 Ove Kaaven\n";
96 #ifdef __i386__
97 enum target_cpu target_cpu = CPU_x86;
98 #elif defined(__x86_64__)
99 enum target_cpu target_cpu = CPU_x86_64;
100 #elif defined(__powerpc__)
101 enum target_cpu target_cpu = CPU_POWERPC;
102 #elif defined(__arm__)
103 enum target_cpu target_cpu = CPU_ARM;
104 #elif defined(__aarch64__)
105 enum target_cpu target_cpu = CPU_ARM64;
106 #else
107 #error Unsupported CPU
108 #endif
110 int debuglevel = DEBUGLEVEL_NONE;
111 int parser_debug, yy_flex_debug;
113 int pedantic = 0;
114 int do_everything = 1;
115 static int preprocess_only = 0;
116 int do_header = 0;
117 int do_typelib = 0;
118 int do_proxies = 0;
119 int do_client = 0;
120 int do_server = 0;
121 int do_regscript = 0;
122 int do_idfile = 0;
123 int do_dlldata = 0;
124 static int no_preprocess = 0;
125 int old_names = 0;
126 int win32_packing = 8;
127 int win64_packing = 8;
128 int winrt_mode = 0;
129 int use_abi_namespace = 0;
130 static int stdinc = 1;
131 static enum stub_mode stub_mode = MODE_Os;
133 char *input_name;
134 char *input_idl_name;
135 char *acf_name;
136 char *header_name;
137 char *local_stubs_name;
138 char *header_token;
139 char *typelib_name;
140 char *dlldata_name;
141 char *proxy_name;
142 char *proxy_token;
143 char *client_name;
144 char *client_token;
145 char *server_name;
146 char *server_token;
147 char *regscript_name;
148 char *regscript_token;
149 static char *idfile_name;
150 char *temp_name;
151 const char *prefix_client = "";
152 const char *prefix_server = "";
153 static const char *includedir;
155 int line_number = 1;
157 static FILE *idfile;
159 unsigned int pointer_size = 0;
161 time_t now;
163 enum {
164 OLDNAMES_OPTION = CHAR_MAX + 1,
165 ACF_OPTION,
166 APP_CONFIG_OPTION,
167 DLLDATA_OPTION,
168 DLLDATA_ONLY_OPTION,
169 LOCAL_STUBS_OPTION,
170 NOSTDINC_OPTION,
171 PREFIX_ALL_OPTION,
172 PREFIX_CLIENT_OPTION,
173 PREFIX_SERVER_OPTION,
174 PRINT_HELP,
175 RT_NS_PREFIX,
176 RT_OPTION,
177 ROBUST_OPTION,
178 SYSROOT_OPTION,
179 WIN32_OPTION,
180 WIN64_OPTION,
181 WIN32_ALIGN_OPTION,
182 WIN64_ALIGN_OPTION
185 static const char short_options[] =
186 "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
187 static const struct option long_options[] = {
188 { "acf", 1, NULL, ACF_OPTION },
189 { "app_config", 0, NULL, APP_CONFIG_OPTION },
190 { "dlldata", 1, NULL, DLLDATA_OPTION },
191 { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
192 { "help", 0, NULL, PRINT_HELP },
193 { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
194 { "nostdinc", 0, NULL, NOSTDINC_OPTION },
195 { "ns_prefix", 0, NULL, RT_NS_PREFIX },
196 { "oldnames", 0, NULL, OLDNAMES_OPTION },
197 { "output", 0, NULL, 'o' },
198 { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
199 { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
200 { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
201 { "robust", 0, NULL, ROBUST_OPTION },
202 { "sysroot", 1, NULL, SYSROOT_OPTION },
203 { "target", 0, NULL, 'b' },
204 { "winrt", 0, NULL, RT_OPTION },
205 { "win32", 0, NULL, WIN32_OPTION },
206 { "win64", 0, NULL, WIN64_OPTION },
207 { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
208 { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
209 { NULL, 0, NULL, 0 }
212 static void rm_tempfile(void);
214 enum stub_mode get_stub_mode(void)
216 /* old-style interpreted stubs are not supported on 64-bit */
217 if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
218 return stub_mode;
221 static char *make_token(const char *name)
223 char *token;
224 char *slash;
225 int i;
227 slash = strrchr(name, '/');
228 if(!slash)
229 slash = strrchr(name, '\\');
231 if (slash) name = slash + 1;
233 token = xstrdup(name);
234 for (i=0; token[i]; i++) {
235 if (!isalnum(token[i])) token[i] = '_';
236 else token[i] = tolower(token[i]);
238 return token;
241 /* duplicate a basename into a valid C token */
242 static char *dup_basename_token(const char *name, const char *ext)
244 char *p, *ret = dup_basename( name, ext );
245 /* map invalid characters to '_' */
246 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
247 return ret;
250 static void add_widl_version_define(void)
252 unsigned int version;
253 const char *p = PACKAGE_VERSION;
255 /* major */
256 version = atoi(p) * 0x10000;
257 p = strchr(p, '.');
259 /* minor */
260 if (p)
262 version += atoi(p + 1) * 0x100;
263 p = strchr(p + 1, '.');
266 /* build */
267 if (p)
268 version += atoi(p + 1);
270 if (version != 0)
272 char version_str[11];
273 snprintf(version_str, sizeof(version_str), "0x%x", version);
274 wpp_add_define("__WIDL__", version_str);
276 else
277 wpp_add_define("__WIDL__", NULL);
280 /* set the target platform */
281 static void set_target( const char *target )
283 static const struct
285 const char *name;
286 enum target_cpu cpu;
287 } cpu_names[] =
289 { "i386", CPU_x86 },
290 { "i486", CPU_x86 },
291 { "i586", CPU_x86 },
292 { "i686", CPU_x86 },
293 { "i786", CPU_x86 },
294 { "amd64", CPU_x86_64 },
295 { "x86_64", CPU_x86_64 },
296 { "powerpc", CPU_POWERPC },
297 { "arm", CPU_ARM },
298 { "armv5", CPU_ARM },
299 { "armv6", CPU_ARM },
300 { "armv7", CPU_ARM },
301 { "armv7a", CPU_ARM },
302 { "arm64", CPU_ARM64 },
303 { "aarch64", CPU_ARM64 },
306 unsigned int i;
307 char *p, *spec = xstrdup( target );
309 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
311 if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
312 *p++ = 0;
313 for (i = 0; i < ARRAY_SIZE( cpu_names ); i++)
315 if (!strcmp( cpu_names[i].name, spec ))
317 target_cpu = cpu_names[i].cpu;
318 free( spec );
319 return;
322 error( "Unrecognized CPU '%s'\n", spec );
325 /* clean things up when aborting on a signal */
326 static void exit_on_signal( int sig )
328 exit(1); /* this will call the atexit functions */
331 static void set_everything(int x)
333 do_header = x;
334 do_typelib = x;
335 do_proxies = x;
336 do_client = x;
337 do_server = x;
338 do_regscript = x;
339 do_idfile = x;
340 do_dlldata = x;
343 void start_cplusplus_guard(FILE *fp)
345 fprintf(fp, "#ifdef __cplusplus\n");
346 fprintf(fp, "extern \"C\" {\n");
347 fprintf(fp, "#endif\n\n");
350 void end_cplusplus_guard(FILE *fp)
352 fprintf(fp, "#ifdef __cplusplus\n");
353 fprintf(fp, "}\n");
354 fprintf(fp, "#endif\n\n");
357 typedef struct
359 char *filename;
360 struct list link;
361 } filename_node_t;
363 static void add_filename_node(struct list *list, const char *name)
365 filename_node_t *node = xmalloc(sizeof *node);
366 node->filename = dup_basename( name, ".idl" );
367 list_add_tail(list, &node->link);
370 static void free_filename_nodes(struct list *list)
372 filename_node_t *node, *next;
373 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
374 list_remove(&node->link);
375 free(node->filename);
376 free(node);
380 static void write_dlldata_list(struct list *filenames, int define_proxy_delegation)
382 FILE *dlldata;
383 filename_node_t *node;
385 dlldata = fopen(dlldata_name, "w");
386 if (!dlldata)
387 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
389 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
390 fprintf(dlldata, "- Do not edit ***/\n\n");
391 if (define_proxy_delegation)
392 fprintf(dlldata, "#define PROXY_DELEGATION\n");
393 fprintf(dlldata, "#include <objbase.h>\n");
394 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
395 start_cplusplus_guard(dlldata);
397 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
398 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
400 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
401 fprintf(dlldata, "/* Start of list */\n");
402 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
403 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
404 fprintf(dlldata, "/* End of list */\n");
405 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
407 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
408 end_cplusplus_guard(dlldata);
409 fclose(dlldata);
412 static char *eat_space(char *s)
414 while (isspace((unsigned char) *s))
415 ++s;
416 return s;
419 void write_dlldata(const statement_list_t *stmts)
421 struct list filenames = LIST_INIT(filenames);
422 int define_proxy_delegation = 0;
423 filename_node_t *node;
424 FILE *dlldata;
426 if (!do_dlldata || !need_proxy_file(stmts))
427 return;
429 define_proxy_delegation = need_proxy_delegation(stmts);
431 dlldata = fopen(dlldata_name, "r");
432 if (dlldata) {
433 static const char marker[] = "REFERENCE_PROXY_FILE";
434 static const char delegation_define[] = "#define PROXY_DELEGATION";
435 char *line = NULL;
436 size_t len = 0;
438 while (widl_getline(&line, &len, dlldata)) {
439 char *start, *end;
440 start = eat_space(line);
441 if (strncmp(start, marker, sizeof marker - 1) == 0) {
442 start = eat_space(start + sizeof marker - 1);
443 if (*start != '(')
444 continue;
445 end = start = eat_space(start + 1);
446 while (*end && *end != ')')
447 ++end;
448 if (*end != ')')
449 continue;
450 while (isspace((unsigned char) end[-1]))
451 --end;
452 *end = '\0';
453 if (start < end)
454 add_filename_node(&filenames, start);
455 }else if (!define_proxy_delegation && strncmp(start, delegation_define, sizeof(delegation_define)-1)) {
456 define_proxy_delegation = 1;
460 if (ferror(dlldata))
461 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
463 free(line);
464 fclose(dlldata);
467 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
468 if (strcmp(proxy_token, node->filename) == 0) {
469 /* We're already in the list, no need to regenerate this file. */
470 free_filename_nodes(&filenames);
471 return;
474 add_filename_node(&filenames, proxy_token);
475 write_dlldata_list(&filenames, define_proxy_delegation);
476 free_filename_nodes(&filenames);
479 static void write_id_guid(FILE *f, const char *type, const char *guid_prefix, const char *name, const UUID *uuid)
481 if (!uuid) return;
482 fprintf(f, "MIDL_DEFINE_GUID(%s, %s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
483 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
484 type, guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
485 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
486 uuid->Data4[6], uuid->Data4[7]);
489 static void write_id_data_stmts(const statement_list_t *stmts)
491 const statement_t *stmt;
492 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
494 if (stmt->type == STMT_TYPE)
496 const type_t *type = stmt->u.type;
497 if (type_get_type(type) == TYPE_INTERFACE)
499 const UUID *uuid;
500 if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
501 continue;
502 uuid = get_attrp(type->attrs, ATTR_UUID);
503 write_id_guid(idfile, "IID", is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
504 type->name, uuid);
505 if (type_iface_get_async_iface(type))
507 uuid = get_attrp(type_iface_get_async_iface(type)->attrs, ATTR_UUID);
508 write_id_guid(idfile, "IID", "IID", type_iface_get_async_iface(type)->name, uuid);
511 else if (type_get_type(type) == TYPE_COCLASS)
513 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
514 write_id_guid(idfile, "CLSID", "CLSID", type->name, uuid);
517 else if (stmt->type == STMT_LIBRARY)
519 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
520 write_id_guid(idfile, "IID", "LIBID", stmt->u.lib->name, uuid);
521 write_id_data_stmts(stmt->u.lib->stmts);
526 void write_id_data(const statement_list_t *stmts)
528 if (!do_idfile) return;
530 idfile = fopen(idfile_name, "w");
531 if (! idfile) {
532 error("Could not open %s for output\n", idfile_name);
533 return;
536 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
537 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_idl_name);
538 fprintf(idfile, "#include <rpc.h>\n");
539 fprintf(idfile, "#include <rpcndr.h>\n\n");
541 fprintf(idfile, "#ifdef _MIDL_USE_GUIDDEF_\n\n");
543 fprintf(idfile, "#ifndef INITGUID\n");
544 fprintf(idfile, "#define INITGUID\n");
545 fprintf(idfile, "#include <guiddef.h>\n");
546 fprintf(idfile, "#undef INITGUID\n");
547 fprintf(idfile, "#else\n");
548 fprintf(idfile, "#include <guiddef.h>\n");
549 fprintf(idfile, "#endif\n\n");
551 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
552 fprintf(idfile, " DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)\n\n");
554 fprintf(idfile, "#elif defined(__cplusplus)\n\n");
556 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
557 fprintf(idfile, " EXTERN_C const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
559 fprintf(idfile, "#else\n\n");
561 fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
562 fprintf(idfile, " const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
564 fprintf(idfile, "#endif\n\n");
565 start_cplusplus_guard(idfile);
567 write_id_data_stmts(stmts);
569 fprintf(idfile, "\n");
570 end_cplusplus_guard(idfile);
571 fprintf(idfile, "#undef MIDL_DEFINE_GUID\n" );
573 fclose(idfile);
576 static void init_argv0_dir( const char *argv0 )
578 #ifndef _WIN32
579 char *p, *dir;
581 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
582 dir = realpath( "/proc/self/exe", NULL );
583 #elif defined (__FreeBSD__) || defined(__DragonFly__)
584 dir = realpath( "/proc/curproc/file", NULL );
585 #else
586 dir = realpath( argv0, NULL );
587 #endif
588 if (!dir) return;
589 if (!(p = strrchr( dir, '/' ))) return;
590 if (p == dir) p++;
591 *p = 0;
592 includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
593 free( dir );
594 #endif
597 int main(int argc,char *argv[])
599 int i, optc;
600 int ret = 0;
601 int opti = 0;
602 char *output_name = NULL;
603 const char *sysroot = "";
605 signal( SIGTERM, exit_on_signal );
606 signal( SIGINT, exit_on_signal );
607 #ifdef SIGHUP
608 signal( SIGHUP, exit_on_signal );
609 #endif
610 init_argv0_dir( argv[0] );
612 now = time(NULL);
614 while((optc = getopt_long_only(argc, argv, short_options, long_options, &opti)) != EOF) {
615 switch(optc) {
616 case DLLDATA_OPTION:
617 dlldata_name = xstrdup(optarg);
618 break;
619 case DLLDATA_ONLY_OPTION:
620 do_everything = 0;
621 do_dlldata = 1;
622 break;
623 case LOCAL_STUBS_OPTION:
624 do_everything = 0;
625 local_stubs_name = xstrdup(optarg);
626 break;
627 case NOSTDINC_OPTION:
628 stdinc = 0;
629 break;
630 case OLDNAMES_OPTION:
631 old_names = 1;
632 break;
633 case PREFIX_ALL_OPTION:
634 prefix_client = xstrdup(optarg);
635 prefix_server = xstrdup(optarg);
636 break;
637 case PREFIX_CLIENT_OPTION:
638 prefix_client = xstrdup(optarg);
639 break;
640 case PREFIX_SERVER_OPTION:
641 prefix_server = xstrdup(optarg);
642 break;
643 case PRINT_HELP:
644 fprintf(stderr, "%s", usage);
645 return 0;
646 case RT_OPTION:
647 winrt_mode = 1;
648 break;
649 case RT_NS_PREFIX:
650 use_abi_namespace = 1;
651 break;
652 case SYSROOT_OPTION:
653 sysroot = xstrdup(optarg);
654 break;
655 case WIN32_OPTION:
656 pointer_size = 4;
657 break;
658 case WIN64_OPTION:
659 pointer_size = 8;
660 break;
661 case WIN32_ALIGN_OPTION:
662 win32_packing = strtol(optarg, NULL, 0);
663 if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
664 error("Packing must be one of 2, 4 or 8\n");
665 break;
666 case WIN64_ALIGN_OPTION:
667 win64_packing = strtol(optarg, NULL, 0);
668 if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
669 error("Packing must be one of 2, 4 or 8\n");
670 break;
671 case ACF_OPTION:
672 acf_name = xstrdup(optarg);
673 break;
674 case APP_CONFIG_OPTION:
675 /* widl does not distinguish between app_mode and default mode,
676 but we ignore this option for midl compatibility */
677 break;
678 case ROBUST_OPTION:
679 /* FIXME: Support robust option */
680 break;
681 case 'b':
682 set_target( optarg );
683 break;
684 case 'c':
685 do_everything = 0;
686 do_client = 1;
687 break;
688 case 'C':
689 client_name = xstrdup(optarg);
690 break;
691 case 'd':
692 debuglevel = strtol(optarg, NULL, 0);
693 break;
694 case 'D':
695 wpp_add_cmdline_define(optarg);
696 break;
697 case 'E':
698 do_everything = 0;
699 preprocess_only = 1;
700 break;
701 case 'h':
702 do_everything = 0;
703 do_header = 1;
704 break;
705 case 'H':
706 header_name = xstrdup(optarg);
707 break;
708 case 'I':
709 wpp_add_include_path(optarg);
710 break;
711 case 'm':
712 if (!strcmp( optarg, "32" )) pointer_size = 4;
713 else if (!strcmp( optarg, "64" )) pointer_size = 8;
714 break;
715 case 'N':
716 no_preprocess = 1;
717 break;
718 case 'o':
719 output_name = xstrdup(optarg);
720 break;
721 case 'O':
722 if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
723 else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
724 else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
725 else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
726 else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
727 else error( "Invalid argument '-O%s'\n", optarg );
728 break;
729 case 'p':
730 do_everything = 0;
731 do_proxies = 1;
732 break;
733 case 'P':
734 proxy_name = xstrdup(optarg);
735 break;
736 case 'r':
737 do_everything = 0;
738 do_regscript = 1;
739 break;
740 case 's':
741 do_everything = 0;
742 do_server = 1;
743 break;
744 case 'S':
745 server_name = xstrdup(optarg);
746 break;
747 case 't':
748 do_everything = 0;
749 do_typelib = 1;
750 break;
751 case 'T':
752 typelib_name = xstrdup(optarg);
753 break;
754 case 'u':
755 do_everything = 0;
756 do_idfile = 1;
757 break;
758 case 'U':
759 idfile_name = xstrdup(optarg);
760 break;
761 case 'V':
762 printf("%s", version_string);
763 return 0;
764 case 'W':
765 pedantic = 1;
766 break;
767 default:
768 fprintf(stderr, "%s", usage);
769 return 1;
773 if (stdinc)
775 static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
777 if (includedir)
779 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
780 wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
782 for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
784 if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
785 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
786 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
790 switch (target_cpu)
792 case CPU_x86:
793 if (pointer_size == 8) target_cpu = CPU_x86_64;
794 else pointer_size = 4;
795 break;
796 case CPU_x86_64:
797 if (pointer_size == 4) target_cpu = CPU_x86;
798 else pointer_size = 8;
799 break;
800 case CPU_ARM64:
801 if (pointer_size == 4) error( "Cannot build 32-bit code for this CPU\n" );
802 pointer_size = 8;
803 break;
804 default:
805 if (pointer_size == 8) error( "Cannot build 64-bit code for this CPU\n" );
806 pointer_size = 4;
807 break;
810 /* if nothing specified, try to guess output type from the output file name */
811 if (output_name && do_everything && !do_header && !do_typelib && !do_proxies &&
812 !do_client && !do_server && !do_regscript && !do_idfile && !do_dlldata)
814 do_everything = 0;
815 if (strendswith( output_name, ".h" )) do_header = 1;
816 else if (strendswith( output_name, ".tlb" )) do_typelib = 1;
817 else if (strendswith( output_name, "_p.c" )) do_proxies = 1;
818 else if (strendswith( output_name, "_c.c" )) do_client = 1;
819 else if (strendswith( output_name, "_s.c" )) do_server = 1;
820 else if (strendswith( output_name, "_i.c" )) do_idfile = 1;
821 else if (strendswith( output_name, "_r.res" )) do_regscript = 1;
822 else if (strendswith( output_name, "_t.res" )) do_typelib = 1;
823 else if (strendswith( output_name, "dlldata.c" )) do_dlldata = 1;
824 else do_everything = 1;
827 if(do_everything) {
828 set_everything(TRUE);
831 if (!output_name) output_name = dup_basename(input_name, ".idl");
833 if (do_header + do_typelib + do_proxies + do_client +
834 do_server + do_regscript + do_idfile + do_dlldata == 1)
836 if (do_header) header_name = output_name;
837 else if (do_typelib) typelib_name = output_name;
838 else if (do_proxies) proxy_name = output_name;
839 else if (do_client) client_name = output_name;
840 else if (do_server) server_name = output_name;
841 else if (do_regscript) regscript_name = output_name;
842 else if (do_idfile) idfile_name = output_name;
843 else if (do_dlldata) dlldata_name = output_name;
846 if (!dlldata_name && do_dlldata)
847 dlldata_name = xstrdup("dlldata.c");
849 if(optind < argc) {
850 if (do_dlldata && !do_everything) {
851 struct list filenames = LIST_INIT(filenames);
852 for ( ; optind < argc; ++optind)
853 add_filename_node(&filenames, argv[optind]);
855 write_dlldata_list(&filenames, 0 /* FIXME */ );
856 free_filename_nodes(&filenames);
857 return 0;
859 else if (optind != argc - 1) {
860 fprintf(stderr, "%s", usage);
861 return 1;
863 else
864 input_idl_name = input_name = xstrdup(argv[optind]);
866 else {
867 fprintf(stderr, "%s", usage);
868 return 1;
871 if(debuglevel)
873 setbuf(stdout, NULL);
874 setbuf(stderr, NULL);
877 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
878 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
880 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
881 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
882 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
884 if (!header_name) {
885 header_name = dup_basename(input_name, ".idl");
886 strcat(header_name, ".h");
889 if (!typelib_name && do_typelib) {
890 typelib_name = dup_basename(input_name, ".idl");
891 strcat(typelib_name, ".tlb");
894 if (!proxy_name && do_proxies) {
895 proxy_name = dup_basename(input_name, ".idl");
896 strcat(proxy_name, "_p.c");
899 if (!client_name && do_client) {
900 client_name = dup_basename(input_name, ".idl");
901 strcat(client_name, "_c.c");
904 if (!server_name && do_server) {
905 server_name = dup_basename(input_name, ".idl");
906 strcat(server_name, "_s.c");
909 if (!regscript_name && do_regscript) {
910 regscript_name = dup_basename(input_name, ".idl");
911 strcat(regscript_name, "_r.rgs");
914 if (!idfile_name && do_idfile) {
915 idfile_name = dup_basename(input_name, ".idl");
916 strcat(idfile_name, "_i.c");
919 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
920 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
921 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
922 if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
924 add_widl_version_define();
925 wpp_add_define("_WIN32", NULL);
927 atexit(rm_tempfile);
928 if (!no_preprocess)
930 chat("Starting preprocess\n");
932 if (!preprocess_only)
934 FILE *output;
935 int fd;
936 char *name = xmalloc( strlen(header_name) + 8 );
938 strcpy( name, header_name );
939 strcat( name, ".XXXXXX" );
941 if ((fd = mkstemps( name, 0 )) == -1)
942 error("Could not generate a temp name from %s\n", name);
944 temp_name = name;
945 if (!(output = fdopen(fd, "wt")))
946 error("Could not open fd %s for writing\n", name);
948 ret = wpp_parse( input_name, output );
949 fclose( output );
951 else
953 ret = wpp_parse( input_name, stdout );
956 if(ret) exit(1);
957 if(preprocess_only) exit(0);
958 if(!(parser_in = fopen(temp_name, "r"))) {
959 fprintf(stderr, "Could not open %s for input\n", temp_name);
960 return 1;
963 else {
964 if(!(parser_in = fopen(input_name, "r"))) {
965 fprintf(stderr, "Could not open %s for input\n", input_name);
966 return 1;
970 header_token = make_token(header_name);
972 init_types();
973 ret = parser_parse();
975 fclose(parser_in);
977 if(ret) {
978 exit(1);
981 /* Everything has been done successfully, don't delete any files. */
982 set_everything(FALSE);
983 local_stubs_name = NULL;
985 return 0;
988 static void rm_tempfile(void)
990 abort_import();
991 if(temp_name)
992 unlink(temp_name);
993 if (do_header)
994 unlink(header_name);
995 if (local_stubs_name)
996 unlink(local_stubs_name);
997 if (do_client)
998 unlink(client_name);
999 if (do_server)
1000 unlink(server_name);
1001 if (do_regscript)
1002 unlink(regscript_name);
1003 if (do_idfile)
1004 unlink(idfile_name);
1005 if (do_proxies)
1006 unlink(proxy_name);
1007 if (do_typelib)
1008 unlink(typelib_name);