widl: Add a structure packing command line option.
[wine.git] / tools / widl / widl.c
blob38e05cf5b8cdf02dd100f514f9a259430cf33d7b
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 /* future options to reserve characters for: */
47 /* A = ACF input filename */
48 /* J = do not search standard include path */
49 /* O = generate interpreted stubs */
50 /* w = select win16/win32 output (?) */
52 static const char usage[] =
53 "Usage: widl [options...] infile.idl\n"
54 " or: widl [options...] --dlldata-only name1 [name2...]\n"
55 " -a n Set structure alignment to 'n'\n"
56 " -b arch Set the target architecture\n"
57 " -c Generate client stub\n"
58 " -C file Name of client stub file (default is infile_c.c)\n"
59 " -d n Set debug level to 'n'\n"
60 " -D id[=val] Define preprocessor identifier id=val\n"
61 " --dlldata=file Name of the dlldata file (default is dlldata.c)\n"
62 " -E Preprocess only\n"
63 " -h Generate headers\n"
64 " -H file Name of header file (default is infile.h)\n"
65 " -I path Set include search dir to path (multiple -I allowed)\n"
66 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
67 " -m32, -m64 Set the kind of typelib to build (Win32 or Win64)\n"
68 " -N Do not preprocess input\n"
69 " --oldnames Use old naming conventions\n"
70 " -p Generate proxy\n"
71 " -P file Name of proxy file (default is infile_p.c)\n"
72 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
73 " --prefix-client=p Prefix names of client stubs with 'p'\n"
74 " --prefix-server=p Prefix names of server functions with 'p'\n"
75 " -s Generate server stub\n"
76 " -S file Name of server stub file (default is infile_s.c)\n"
77 " -t Generate typelib\n"
78 " -T file Name of typelib file (default is infile.tlb)\n"
79 " -u Generate interface identifiers file\n"
80 " -U file Name of interface identifiers file (default is infile_i.c)\n"
81 " -V Print version and exit\n"
82 " -W Enable pedantic warnings\n"
83 " --win32 Only generate 32-bit code\n"
84 " --win64 Only generate 64-bit code\n"
85 "Debug level 'n' is a bitmask with following meaning:\n"
86 " * 0x01 Tell which resource is parsed (verbose mode)\n"
87 " * 0x02 Dump internal structures\n"
88 " * 0x04 Create a parser trace (yydebug=1)\n"
89 " * 0x08 Preprocessor messages\n"
90 " * 0x10 Preprocessor lex messages\n"
91 " * 0x20 Preprocessor yacc trace\n"
94 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
95 "Copyright 2002 Ove Kaaven\n";
97 int debuglevel = DEBUGLEVEL_NONE;
98 int parser_debug, yy_flex_debug;
100 int pedantic = 0;
101 int do_everything = 1;
102 int preprocess_only = 0;
103 int do_header = 0;
104 int do_typelib = 0;
105 int do_proxies = 0;
106 int do_client = 0;
107 int do_server = 0;
108 int do_idfile = 0;
109 int do_dlldata = 0;
110 int no_preprocess = 0;
111 int old_names = 0;
112 int do_win32 = 1;
113 int do_win64 = 1;
114 int packing = 8;
116 char *input_name;
117 char *header_name;
118 char *local_stubs_name;
119 char *header_token;
120 char *typelib_name;
121 char *dlldata_name;
122 char *proxy_name;
123 char *proxy_token;
124 char *client_name;
125 char *client_token;
126 char *server_name;
127 char *server_token;
128 char *idfile_name;
129 char *idfile_token;
130 char *temp_name;
131 const char *prefix_client = "";
132 const char *prefix_server = "";
134 int line_number = 1;
136 FILE *header;
137 FILE *idfile;
139 size_t pointer_size = 0;
140 syskind_t typelib_kind = sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32;
142 time_t now;
144 enum {
145 OLDNAMES_OPTION = CHAR_MAX + 1,
146 DLLDATA_OPTION,
147 DLLDATA_ONLY_OPTION,
148 LOCAL_STUBS_OPTION,
149 PREFIX_ALL_OPTION,
150 PREFIX_CLIENT_OPTION,
151 PREFIX_SERVER_OPTION,
152 WIN32_OPTION,
153 WIN64_OPTION
156 static const char short_options[] =
157 "a:b:cC:d:D:EhH:I:m:NpP:sS:tT:uU:VW";
158 static const struct option long_options[] = {
159 { "dlldata", 1, 0, DLLDATA_OPTION },
160 { "dlldata-only", 0, 0, DLLDATA_ONLY_OPTION },
161 { "local-stubs", 1, 0, LOCAL_STUBS_OPTION },
162 { "oldnames", 0, 0, OLDNAMES_OPTION },
163 { "prefix-all", 1, 0, PREFIX_ALL_OPTION },
164 { "prefix-client", 1, 0, PREFIX_CLIENT_OPTION },
165 { "prefix-server", 1, 0, PREFIX_SERVER_OPTION },
166 { "win32", 0, 0, WIN32_OPTION },
167 { "win64", 0, 0, WIN64_OPTION },
168 { 0, 0, 0, 0 }
171 static void rm_tempfile(void);
173 static char *make_token(const char *name)
175 char *token;
176 char *slash;
177 int i;
179 slash = strrchr(name, '/');
180 if(!slash)
181 slash = strrchr(name, '\\');
183 if (slash) name = slash + 1;
185 token = xstrdup(name);
186 for (i=0; token[i]; i++) {
187 if (!isalnum(token[i])) token[i] = '_';
188 else token[i] = toupper(token[i]);
190 return token;
193 /* duplicate a basename into a valid C token */
194 static char *dup_basename_token(const char *name, const char *ext)
196 char *p, *ret = dup_basename( name, ext );
197 /* map invalid characters to '_' */
198 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
199 return ret;
202 static void add_widl_version_define(void)
204 unsigned int version;
205 const char *p = PACKAGE_VERSION;
207 /* major */
208 version = atoi(p) * 0x10000;
209 p = strchr(p, '.');
211 /* minor */
212 if (p)
214 version += atoi(p + 1) * 0x100;
215 p = strchr(p + 1, '.');
218 /* build */
219 if (p)
220 version += atoi(p + 1);
222 if (version != 0)
224 char version_str[11];
225 snprintf(version_str, sizeof(version_str), "0x%x", version);
226 wpp_add_define("__WIDL__", version_str);
228 else
229 wpp_add_define("__WIDL__", NULL);
232 /* set the target platform */
233 static void set_target( const char *target )
235 static const struct
237 const char *name;
238 syskind_t kind;
239 } cpu_names[] =
241 { "i386", SYS_WIN32 },
242 { "i486", SYS_WIN32 },
243 { "i586", SYS_WIN32 },
244 { "i686", SYS_WIN32 },
245 { "i786", SYS_WIN32 },
246 { "x86_64", SYS_WIN64 },
247 { "sparc", SYS_WIN32 },
248 { "alpha", SYS_WIN32 },
249 { "powerpc", SYS_WIN32 }
252 unsigned int i;
253 char *p, *spec = xstrdup( target );
255 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
257 if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
258 *p++ = 0;
259 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
261 if (!strcmp( cpu_names[i].name, spec ))
263 typelib_kind = cpu_names[i].kind;
264 free( spec );
265 return;
268 error( "Unrecognized CPU '%s'\n", spec );
271 /* clean things up when aborting on a signal */
272 static void exit_on_signal( int sig )
274 exit(1); /* this will call the atexit functions */
277 static void set_everything(int x)
279 do_header = x;
280 do_typelib = x;
281 do_proxies = x;
282 do_client = x;
283 do_server = x;
284 do_idfile = x;
285 do_dlldata = x;
288 void start_cplusplus_guard(FILE *fp)
290 fprintf(fp, "#ifdef __cplusplus\n");
291 fprintf(fp, "extern \"C\" {\n");
292 fprintf(fp, "#endif\n\n");
295 void end_cplusplus_guard(FILE *fp)
297 fprintf(fp, "#ifdef __cplusplus\n");
298 fprintf(fp, "}\n");
299 fprintf(fp, "#endif\n\n");
302 typedef struct
304 char *filename;
305 struct list link;
306 } filename_node_t;
308 static void add_filename_node(struct list *list, const char *name)
310 filename_node_t *node = xmalloc(sizeof *node);
311 node->filename = dup_basename( name, ".idl" );
312 list_add_tail(list, &node->link);
315 static void free_filename_nodes(struct list *list)
317 filename_node_t *node, *next;
318 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
319 list_remove(&node->link);
320 free(node->filename);
321 free(node);
325 static void write_dlldata_list(struct list *filenames)
327 FILE *dlldata;
328 filename_node_t *node;
330 dlldata = fopen(dlldata_name, "w");
331 if (!dlldata)
332 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
334 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
335 fprintf(dlldata, "- Do not edit ***/\n\n");
336 fprintf(dlldata, "#include <objbase.h>\n");
337 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
338 start_cplusplus_guard(dlldata);
340 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
341 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
343 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
344 fprintf(dlldata, "/* Start of list */\n");
345 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
346 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
347 fprintf(dlldata, "/* End of list */\n");
348 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
350 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
351 end_cplusplus_guard(dlldata);
352 fclose(dlldata);
355 static char *eat_space(char *s)
357 while (isspace((unsigned char) *s))
358 ++s;
359 return s;
362 void write_dlldata(const statement_list_t *stmts)
364 struct list filenames = LIST_INIT(filenames);
365 filename_node_t *node;
366 FILE *dlldata;
368 if (!do_dlldata || !need_proxy_file(stmts))
369 return;
371 dlldata = fopen(dlldata_name, "r");
372 if (dlldata) {
373 static char marker[] = "REFERENCE_PROXY_FILE";
374 char *line = NULL;
375 size_t len = 0;
377 while (widl_getline(&line, &len, dlldata)) {
378 char *start, *end;
379 start = eat_space(line);
380 if (strncmp(start, marker, sizeof marker - 1) == 0) {
381 start = eat_space(start + sizeof marker - 1);
382 if (*start != '(')
383 continue;
384 end = start = eat_space(start + 1);
385 while (*end && *end != ')')
386 ++end;
387 if (*end != ')')
388 continue;
389 while (isspace((unsigned char) end[-1]))
390 --end;
391 *end = '\0';
392 if (start < end)
393 add_filename_node(&filenames, start);
397 if (ferror(dlldata))
398 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
400 free(line);
401 fclose(dlldata);
404 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
405 if (strcmp(proxy_token, node->filename) == 0) {
406 /* We're already in the list, no need to regenerate this file. */
407 free_filename_nodes(&filenames);
408 return;
411 add_filename_node(&filenames, proxy_token);
412 write_dlldata_list(&filenames);
413 free_filename_nodes(&filenames);
416 static void write_id_data_stmts(const statement_list_t *stmts)
418 const statement_t *stmt;
419 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
421 if (stmt->type == STMT_TYPE)
423 const type_t *type = stmt->u.type;
424 if (type_get_type(type) == TYPE_INTERFACE)
426 const UUID *uuid;
427 if (!is_object(type->attrs) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
428 continue;
429 uuid = get_attrp(type->attrs, ATTR_UUID);
430 write_guid(idfile, is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
431 type->name, uuid);
433 else if (type_get_type(type) == TYPE_COCLASS)
435 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
436 write_guid(idfile, "CLSID", type->name, uuid);
439 else if (stmt->type == STMT_LIBRARY)
441 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
442 write_guid(idfile, "LIBID", stmt->u.lib->name, uuid);
443 write_id_data_stmts(stmt->u.lib->stmts);
448 void write_id_data(const statement_list_t *stmts)
450 if (!do_idfile) return;
452 idfile_token = make_token(idfile_name);
454 idfile = fopen(idfile_name, "w");
455 if (! idfile) {
456 error("Could not open %s for output\n", idfile_name);
457 return;
460 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
461 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
462 fprintf(idfile, "#include <rpc.h>\n");
463 fprintf(idfile, "#include <rpcndr.h>\n\n");
464 fprintf(idfile, "#include <initguid.h>\n\n");
465 start_cplusplus_guard(idfile);
467 write_id_data_stmts(stmts);
469 fprintf(idfile, "\n");
470 end_cplusplus_guard(idfile);
472 fclose(idfile);
475 int main(int argc,char *argv[])
477 extern char* optarg;
478 extern int optind;
479 int optc;
480 int ret = 0;
481 int opti = 0;
483 signal( SIGTERM, exit_on_signal );
484 signal( SIGINT, exit_on_signal );
485 #ifdef SIGHUP
486 signal( SIGHUP, exit_on_signal );
487 #endif
489 now = time(NULL);
491 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
492 switch(optc) {
493 case DLLDATA_OPTION:
494 dlldata_name = xstrdup(optarg);
495 break;
496 case DLLDATA_ONLY_OPTION:
497 do_everything = 0;
498 do_dlldata = 1;
499 break;
500 case LOCAL_STUBS_OPTION:
501 do_everything = 0;
502 local_stubs_name = xstrdup(optarg);
503 break;
504 case OLDNAMES_OPTION:
505 old_names = 1;
506 break;
507 case PREFIX_ALL_OPTION:
508 prefix_client = xstrdup(optarg);
509 prefix_server = xstrdup(optarg);
510 break;
511 case PREFIX_CLIENT_OPTION:
512 prefix_client = xstrdup(optarg);
513 break;
514 case PREFIX_SERVER_OPTION:
515 prefix_server = xstrdup(optarg);
516 break;
517 case WIN32_OPTION:
518 do_win32 = 1;
519 do_win64 = 0;
520 break;
521 case WIN64_OPTION:
522 do_win32 = 0;
523 do_win64 = 1;
524 break;
525 case 'a':
526 packing = strtol(optarg, NULL, 0);
527 if(packing != 2 && packing != 4 && packing != 8)
528 error("Packing must be one of 2, 4 or 8\n");
529 break;
530 case 'b':
531 set_target( optarg );
532 break;
533 case 'c':
534 do_everything = 0;
535 do_client = 1;
536 break;
537 case 'C':
538 client_name = xstrdup(optarg);
539 break;
540 case 'd':
541 debuglevel = strtol(optarg, NULL, 0);
542 break;
543 case 'D':
544 wpp_add_cmdline_define(optarg);
545 break;
546 case 'E':
547 do_everything = 0;
548 preprocess_only = 1;
549 break;
550 case 'h':
551 do_everything = 0;
552 do_header = 1;
553 break;
554 case 'H':
555 header_name = xstrdup(optarg);
556 break;
557 case 'I':
558 wpp_add_include_path(optarg);
559 break;
560 case 'm':
561 if (!strcmp( optarg, "32" )) typelib_kind = SYS_WIN32;
562 else if (!strcmp( optarg, "64" )) typelib_kind = SYS_WIN64;
563 else error( "Invalid -m argument '%s'\n", optarg );
564 break;
565 case 'N':
566 no_preprocess = 1;
567 break;
568 case 'p':
569 do_everything = 0;
570 do_proxies = 1;
571 break;
572 case 'P':
573 proxy_name = xstrdup(optarg);
574 break;
575 case 's':
576 do_everything = 0;
577 do_server = 1;
578 break;
579 case 'S':
580 server_name = xstrdup(optarg);
581 break;
582 case 't':
583 do_everything = 0;
584 do_typelib = 1;
585 break;
586 case 'T':
587 typelib_name = xstrdup(optarg);
588 break;
589 case 'u':
590 do_everything = 0;
591 do_idfile = 1;
592 break;
593 case 'U':
594 idfile_name = xstrdup(optarg);
595 break;
596 case 'V':
597 printf("%s", version_string);
598 return 0;
599 case 'W':
600 pedantic = 1;
601 break;
602 default:
603 fprintf(stderr, "%s", usage);
604 return 1;
608 if(do_everything) {
609 set_everything(TRUE);
612 if (!dlldata_name && do_dlldata)
613 dlldata_name = xstrdup("dlldata.c");
615 if(optind < argc) {
616 if (do_dlldata && !do_everything) {
617 struct list filenames = LIST_INIT(filenames);
618 for ( ; optind < argc; ++optind)
619 add_filename_node(&filenames, argv[optind]);
621 write_dlldata_list(&filenames);
622 free_filename_nodes(&filenames);
623 return 0;
625 else if (optind != argc - 1) {
626 fprintf(stderr, "%s", usage);
627 return 1;
629 else
630 input_name = xstrdup(argv[optind]);
632 else {
633 fprintf(stderr, "%s", usage);
634 return 1;
637 if(debuglevel)
639 setbuf(stdout,0);
640 setbuf(stderr,0);
643 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
644 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
646 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
647 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
648 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
650 if (!header_name) {
651 header_name = dup_basename(input_name, ".idl");
652 strcat(header_name, ".h");
655 if (!typelib_name && do_typelib) {
656 typelib_name = dup_basename(input_name, ".idl");
657 strcat(typelib_name, ".tlb");
660 if (!proxy_name && do_proxies) {
661 proxy_name = dup_basename(input_name, ".idl");
662 strcat(proxy_name, "_p.c");
665 if (!client_name && do_client) {
666 client_name = dup_basename(input_name, ".idl");
667 strcat(client_name, "_c.c");
670 if (!server_name && do_server) {
671 server_name = dup_basename(input_name, ".idl");
672 strcat(server_name, "_s.c");
675 if (!idfile_name && do_idfile) {
676 idfile_name = dup_basename(input_name, ".idl");
677 strcat(idfile_name, "_i.c");
680 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
681 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
682 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
684 add_widl_version_define();
686 atexit(rm_tempfile);
687 if (!no_preprocess)
689 chat("Starting preprocess\n");
691 if (!preprocess_only)
693 ret = wpp_parse_temp( input_name, header_name, &temp_name );
695 else
697 ret = wpp_parse( input_name, stdout );
700 if(ret) exit(1);
701 if(preprocess_only) exit(0);
702 if(!(parser_in = fopen(temp_name, "r"))) {
703 fprintf(stderr, "Could not open %s for input\n", temp_name);
704 return 1;
707 else {
708 if(!(parser_in = fopen(input_name, "r"))) {
709 fprintf(stderr, "Could not open %s for input\n", input_name);
710 return 1;
714 header_token = make_token(header_name);
716 init_types();
717 ret = parser_parse();
719 fclose(parser_in);
721 if(ret) {
722 exit(1);
725 /* Everything has been done successfully, don't delete any files. */
726 set_everything(FALSE);
727 local_stubs_name = NULL;
729 return 0;
732 static void rm_tempfile(void)
734 abort_import();
735 if(temp_name)
736 unlink(temp_name);
737 if (do_header)
738 unlink(header_name);
739 if (local_stubs_name)
740 unlink(local_stubs_name);
741 if (do_client)
742 unlink(client_name);
743 if (do_server)
744 unlink(server_name);
745 if (do_idfile)
746 unlink(idfile_name);
747 if (do_proxies)
748 unlink(proxy_name);
749 if (do_typelib)
750 unlink(typelib_name);