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
23 #include "wine/port.h"
46 /* future options to reserve characters for: */
47 /* A = ACF input filename */
48 /* J = do not search standard include path */
49 /* w = select win16/win32 output (?) */
51 static const char usage[] =
52 "Usage: widl [options...] infile.idl\n"
53 " or: widl [options...] --dlldata-only name1 [name2...]\n"
54 " -b arch Set the target architecture\n"
55 " -c Generate client stub\n"
56 " -d n Set debug level to 'n'\n"
57 " -D id[=val] Define preprocessor identifier id=val\n"
58 " -E Preprocess only\n"
59 " -h Generate headers\n"
60 " -H file Name of header file (default is infile.h)\n"
61 " -I path Set include search dir to path (multiple -I allowed)\n"
62 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
63 " -m32, -m64 Set the kind of typelib to build (Win32 or Win64)\n"
64 " -N Do not preprocess input\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 " -s Generate server stub\n"
74 " -t Generate typelib\n"
75 " -u Generate interface identifiers file\n"
76 " -V Print version and exit\n"
77 " -W Enable pedantic warnings\n"
78 " --win32 Only generate 32-bit code\n"
79 " --win64 Only generate 64-bit code\n"
80 " --win32-align n Set win32 structure alignment to 'n'\n"
81 " --win64-align n Set win64 structure alignment to 'n'\n"
82 "Debug level 'n' is a bitmask with following meaning:\n"
83 " * 0x01 Tell which resource is parsed (verbose mode)\n"
84 " * 0x02 Dump internal structures\n"
85 " * 0x04 Create a parser trace (yydebug=1)\n"
86 " * 0x08 Preprocessor messages\n"
87 " * 0x10 Preprocessor lex messages\n"
88 " * 0x20 Preprocessor yacc trace\n"
91 static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
92 "Copyright 2002 Ove Kaaven\n";
94 int debuglevel = DEBUGLEVEL_NONE;
95 int parser_debug, yy_flex_debug;
98 int do_everything = 1;
99 static int preprocess_only = 0;
105 int do_regscript = 0;
108 static int no_preprocess = 0;
112 int win32_packing = 8;
113 int win64_packing = 8;
114 static enum stub_mode stub_mode = MODE_Os;
118 char *local_stubs_name;
128 char *regscript_name;
129 char *regscript_token;
130 static char *idfile_name;
132 const char *prefix_client = "";
133 const char *prefix_server = "";
139 unsigned int pointer_size = 0;
140 syskind_t typelib_kind = sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32;
145 OLDNAMES_OPTION = CHAR_MAX + 1,
150 PREFIX_CLIENT_OPTION,
151 PREFIX_SERVER_OPTION,
158 static const char short_options[] =
159 "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
160 static const struct option long_options[] = {
161 { "dlldata", 1, NULL, DLLDATA_OPTION },
162 { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
163 { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
164 { "oldnames", 0, NULL, OLDNAMES_OPTION },
165 { "output", 0, NULL, 'o' },
166 { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
167 { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
168 { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
169 { "win32", 0, NULL, WIN32_OPTION },
170 { "win64", 0, NULL, WIN64_OPTION },
171 { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
172 { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
176 static void rm_tempfile(void);
178 enum stub_mode get_stub_mode(void)
180 /* old-style interpreted stubs are not supported on 64-bit */
181 if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
185 static char *make_token(const char *name)
191 slash = strrchr(name, '/');
193 slash = strrchr(name, '\\');
195 if (slash) name = slash + 1;
197 token = xstrdup(name);
198 for (i=0; token[i]; i++) {
199 if (!isalnum(token[i])) token[i] = '_';
200 else token[i] = tolower(token[i]);
205 /* duplicate a basename into a valid C token */
206 static char *dup_basename_token(const char *name, const char *ext)
208 char *p, *ret = dup_basename( name, ext );
209 /* map invalid characters to '_' */
210 for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
214 static void add_widl_version_define(void)
216 unsigned int version;
217 const char *p = PACKAGE_VERSION;
220 version = atoi(p) * 0x10000;
226 version += atoi(p + 1) * 0x100;
227 p = strchr(p + 1, '.');
232 version += atoi(p + 1);
236 char version_str[11];
237 snprintf(version_str, sizeof(version_str), "0x%x", version);
238 wpp_add_define("__WIDL__", version_str);
241 wpp_add_define("__WIDL__", NULL);
244 /* set the target platform */
245 static void set_target( const char *target )
253 { "i386", SYS_WIN32 },
254 { "i486", SYS_WIN32 },
255 { "i586", SYS_WIN32 },
256 { "i686", SYS_WIN32 },
257 { "i786", SYS_WIN32 },
258 { "amd64", SYS_WIN64 },
259 { "x86_64", SYS_WIN64 },
260 { "sparc", SYS_WIN32 },
261 { "alpha", SYS_WIN32 },
262 { "powerpc", SYS_WIN32 },
267 char *p, *spec = xstrdup( target );
269 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
271 if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
273 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
275 if (!strcmp( cpu_names[i].name, spec ))
277 typelib_kind = cpu_names[i].kind;
282 error( "Unrecognized CPU '%s'\n", spec );
285 /* clean things up when aborting on a signal */
286 static void exit_on_signal( int sig )
288 exit(1); /* this will call the atexit functions */
291 static void set_everything(int x)
303 void start_cplusplus_guard(FILE *fp)
305 fprintf(fp, "#ifdef __cplusplus\n");
306 fprintf(fp, "extern \"C\" {\n");
307 fprintf(fp, "#endif\n\n");
310 void end_cplusplus_guard(FILE *fp)
312 fprintf(fp, "#ifdef __cplusplus\n");
314 fprintf(fp, "#endif\n\n");
323 static void add_filename_node(struct list *list, const char *name)
325 filename_node_t *node = xmalloc(sizeof *node);
326 node->filename = dup_basename( name, ".idl" );
327 list_add_tail(list, &node->link);
330 static void free_filename_nodes(struct list *list)
332 filename_node_t *node, *next;
333 LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
334 list_remove(&node->link);
335 free(node->filename);
340 static void write_dlldata_list(struct list *filenames)
343 filename_node_t *node;
345 dlldata = fopen(dlldata_name, "w");
347 error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
349 fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
350 fprintf(dlldata, "- Do not edit ***/\n\n");
351 fprintf(dlldata, "#include <objbase.h>\n");
352 fprintf(dlldata, "#include <rpcproxy.h>\n\n");
353 start_cplusplus_guard(dlldata);
355 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
356 fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
358 fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
359 fprintf(dlldata, "/* Start of list */\n");
360 LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
361 fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
362 fprintf(dlldata, "/* End of list */\n");
363 fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
365 fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
366 end_cplusplus_guard(dlldata);
370 static char *eat_space(char *s)
372 while (isspace((unsigned char) *s))
377 void write_dlldata(const statement_list_t *stmts)
379 struct list filenames = LIST_INIT(filenames);
380 filename_node_t *node;
383 if (!do_dlldata || !need_proxy_file(stmts))
386 dlldata = fopen(dlldata_name, "r");
388 static char marker[] = "REFERENCE_PROXY_FILE";
392 while (widl_getline(&line, &len, dlldata)) {
394 start = eat_space(line);
395 if (strncmp(start, marker, sizeof marker - 1) == 0) {
396 start = eat_space(start + sizeof marker - 1);
399 end = start = eat_space(start + 1);
400 while (*end && *end != ')')
404 while (isspace((unsigned char) end[-1]))
408 add_filename_node(&filenames, start);
413 error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
419 LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
420 if (strcmp(proxy_token, node->filename) == 0) {
421 /* We're already in the list, no need to regenerate this file. */
422 free_filename_nodes(&filenames);
426 add_filename_node(&filenames, proxy_token);
427 write_dlldata_list(&filenames);
428 free_filename_nodes(&filenames);
431 static void write_id_data_stmts(const statement_list_t *stmts)
433 const statement_t *stmt;
434 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
436 if (stmt->type == STMT_TYPE)
438 const type_t *type = stmt->u.type;
439 if (type_get_type(type) == TYPE_INTERFACE)
442 if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
444 uuid = get_attrp(type->attrs, ATTR_UUID);
445 write_guid(idfile, is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
448 else if (type_get_type(type) == TYPE_COCLASS)
450 const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
451 write_guid(idfile, "CLSID", type->name, uuid);
454 else if (stmt->type == STMT_LIBRARY)
456 const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
457 write_guid(idfile, "LIBID", stmt->u.lib->name, uuid);
458 write_id_data_stmts(stmt->u.lib->stmts);
463 void write_id_data(const statement_list_t *stmts)
465 if (!do_idfile) return;
467 idfile = fopen(idfile_name, "w");
469 error("Could not open %s for output\n", idfile_name);
473 fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
474 fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
475 fprintf(idfile, "#include <rpc.h>\n");
476 fprintf(idfile, "#include <rpcndr.h>\n\n");
477 fprintf(idfile, "#include <initguid.h>\n\n");
478 start_cplusplus_guard(idfile);
480 write_id_data_stmts(stmts);
482 fprintf(idfile, "\n");
483 end_cplusplus_guard(idfile);
488 int main(int argc,char *argv[])
493 char *output_name = NULL;
495 signal( SIGTERM, exit_on_signal );
496 signal( SIGINT, exit_on_signal );
498 signal( SIGHUP, exit_on_signal );
503 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
506 dlldata_name = xstrdup(optarg);
508 case DLLDATA_ONLY_OPTION:
512 case LOCAL_STUBS_OPTION:
514 local_stubs_name = xstrdup(optarg);
516 case OLDNAMES_OPTION:
519 case PREFIX_ALL_OPTION:
520 prefix_client = xstrdup(optarg);
521 prefix_server = xstrdup(optarg);
523 case PREFIX_CLIENT_OPTION:
524 prefix_client = xstrdup(optarg);
526 case PREFIX_SERVER_OPTION:
527 prefix_server = xstrdup(optarg);
537 case WIN32_ALIGN_OPTION:
538 win32_packing = strtol(optarg, NULL, 0);
539 if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
540 error("Packing must be one of 2, 4 or 8\n");
542 case WIN64_ALIGN_OPTION:
543 win64_packing = strtol(optarg, NULL, 0);
544 if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
545 error("Packing must be one of 2, 4 or 8\n");
548 set_target( optarg );
555 client_name = xstrdup(optarg);
558 debuglevel = strtol(optarg, NULL, 0);
561 wpp_add_cmdline_define(optarg);
572 header_name = xstrdup(optarg);
575 wpp_add_include_path(optarg);
578 if (!strcmp( optarg, "32" )) typelib_kind = SYS_WIN32;
579 else if (!strcmp( optarg, "64" )) typelib_kind = SYS_WIN64;
580 else error( "Invalid -m argument '%s'\n", optarg );
586 output_name = xstrdup(optarg);
589 if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
590 else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
591 else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
592 else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
593 else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
594 else error( "Invalid argument '-O%s'\n", optarg );
601 proxy_name = xstrdup(optarg);
612 server_name = xstrdup(optarg);
619 typelib_name = xstrdup(optarg);
626 idfile_name = xstrdup(optarg);
629 printf("%s", version_string);
635 fprintf(stderr, "%s", usage);
641 set_everything(TRUE);
644 if (!output_name) output_name = dup_basename(input_name, ".idl");
646 if (!do_everything &&
647 do_header + do_typelib + do_proxies + do_client +
648 do_server + do_regscript + do_idfile + do_dlldata == 1)
650 if (do_header) header_name = output_name;
651 else if (do_typelib) typelib_name = output_name;
652 else if (do_proxies) proxy_name = output_name;
653 else if (do_client) client_name = output_name;
654 else if (do_server) server_name = output_name;
655 else if (do_regscript) regscript_name = output_name;
656 else if (do_idfile) idfile_name = output_name;
657 else if (do_dlldata) dlldata_name = output_name;
660 if (!dlldata_name && do_dlldata)
661 dlldata_name = xstrdup("dlldata.c");
664 if (do_dlldata && !do_everything) {
665 struct list filenames = LIST_INIT(filenames);
666 for ( ; optind < argc; ++optind)
667 add_filename_node(&filenames, argv[optind]);
669 write_dlldata_list(&filenames);
670 free_filename_nodes(&filenames);
673 else if (optind != argc - 1) {
674 fprintf(stderr, "%s", usage);
678 input_name = xstrdup(argv[optind]);
681 fprintf(stderr, "%s", usage);
687 setbuf(stdout, NULL);
688 setbuf(stderr, NULL);
691 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
692 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
694 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
695 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
696 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
699 header_name = dup_basename(input_name, ".idl");
700 strcat(header_name, ".h");
703 if (!typelib_name && do_typelib) {
704 typelib_name = dup_basename(input_name, ".idl");
705 strcat(typelib_name, ".tlb");
708 if (!proxy_name && do_proxies) {
709 proxy_name = dup_basename(input_name, ".idl");
710 strcat(proxy_name, "_p.c");
713 if (!client_name && do_client) {
714 client_name = dup_basename(input_name, ".idl");
715 strcat(client_name, "_c.c");
718 if (!server_name && do_server) {
719 server_name = dup_basename(input_name, ".idl");
720 strcat(server_name, "_s.c");
723 if (!regscript_name && do_regscript) {
724 regscript_name = dup_basename(input_name, ".idl");
725 strcat(regscript_name, "_r.rgs");
728 if (!idfile_name && do_idfile) {
729 idfile_name = dup_basename(input_name, ".idl");
730 strcat(idfile_name, "_i.c");
733 if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
734 if (do_client) client_token = dup_basename_token(client_name,"_c.c");
735 if (do_server) server_token = dup_basename_token(server_name,"_s.c");
736 if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
738 add_widl_version_define();
739 wpp_add_define("_WIN32", NULL);
744 chat("Starting preprocess\n");
746 if (!preprocess_only)
750 char *name = xmalloc( strlen(header_name) + 8 );
752 strcpy( name, header_name );
753 strcat( name, ".XXXXXX" );
755 if ((fd = mkstemps( name, 0 )) == -1)
756 error("Could not generate a temp name from %s\n", name);
759 if (!(output = fdopen(fd, "wt")))
760 error("Could not open fd %s for writing\n", name);
762 ret = wpp_parse( input_name, output );
767 ret = wpp_parse( input_name, stdout );
771 if(preprocess_only) exit(0);
772 if(!(parser_in = fopen(temp_name, "r"))) {
773 fprintf(stderr, "Could not open %s for input\n", temp_name);
778 if(!(parser_in = fopen(input_name, "r"))) {
779 fprintf(stderr, "Could not open %s for input\n", input_name);
784 header_token = make_token(header_name);
787 ret = parser_parse();
795 /* Everything has been done successfully, don't delete any files. */
796 set_everything(FALSE);
797 local_stubs_name = NULL;
802 static void rm_tempfile(void)
809 if (local_stubs_name)
810 unlink(local_stubs_name);
816 unlink(regscript_name);
822 unlink(typelib_name);