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 = alignment of structures */
48 /* A = ACF input filename */
49 /* J = do not search standard include path */
50 /* O = generate interpreted stubs */
51 /* w = select win16/win32 output (?) */
53 static const char usage
[] =
54 "Usage: widl [options...] infile.idl\n"
55 " or: widl [options...] --dlldata-only name1 [name2...]\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
;
101 int do_everything
= 1;
102 int preprocess_only
= 0;
110 int no_preprocess
= 0;
117 char *local_stubs_name
;
130 const char *prefix_client
= "";
131 const char *prefix_server
= "";
138 size_t pointer_size
= 0;
139 syskind_t typelib_kind
= sizeof(void*) == 8 ? SYS_WIN64
: SYS_WIN32
;
144 OLDNAMES_OPTION
= CHAR_MAX
+ 1,
149 PREFIX_CLIENT_OPTION
,
150 PREFIX_SERVER_OPTION
,
155 static const char short_options
[] =
156 "b:cC:d:D:EhH:I:m:NpP:sS:tT:uU:VW";
157 static const struct option long_options
[] = {
158 { "dlldata", 1, 0, DLLDATA_OPTION
},
159 { "dlldata-only", 0, 0, DLLDATA_ONLY_OPTION
},
160 { "local-stubs", 1, 0, LOCAL_STUBS_OPTION
},
161 { "oldnames", 0, 0, OLDNAMES_OPTION
},
162 { "prefix-all", 1, 0, PREFIX_ALL_OPTION
},
163 { "prefix-client", 1, 0, PREFIX_CLIENT_OPTION
},
164 { "prefix-server", 1, 0, PREFIX_SERVER_OPTION
},
165 { "win32", 0, 0, WIN32_OPTION
},
166 { "win64", 0, 0, WIN64_OPTION
},
170 static void rm_tempfile(void);
172 static char *make_token(const char *name
)
178 slash
= strrchr(name
, '/');
180 slash
= strrchr(name
, '\\');
182 if (slash
) name
= slash
+ 1;
184 token
= xstrdup(name
);
185 for (i
=0; token
[i
]; i
++) {
186 if (!isalnum(token
[i
])) token
[i
] = '_';
187 else token
[i
] = toupper(token
[i
]);
192 /* duplicate a basename into a valid C token */
193 static char *dup_basename_token(const char *name
, const char *ext
)
195 char *p
, *ret
= dup_basename( name
, ext
);
196 /* map invalid characters to '_' */
197 for (p
= ret
; *p
; p
++) if (!isalnum(*p
)) *p
= '_';
201 static void add_widl_version_define(void)
203 unsigned int version
;
204 const char *p
= PACKAGE_VERSION
;
207 version
= atoi(p
) * 0x10000;
213 version
+= atoi(p
+ 1) * 0x100;
214 p
= strchr(p
+ 1, '.');
219 version
+= atoi(p
+ 1);
223 char version_str
[11];
224 snprintf(version_str
, sizeof(version_str
), "0x%x", version
);
225 wpp_add_define("__WIDL__", version_str
);
228 wpp_add_define("__WIDL__", NULL
);
231 /* set the target platform */
232 static void set_target( const char *target
)
240 { "i386", SYS_WIN32
},
241 { "i486", SYS_WIN32
},
242 { "i586", SYS_WIN32
},
243 { "i686", SYS_WIN32
},
244 { "i786", SYS_WIN32
},
245 { "x86_64", SYS_WIN64
},
246 { "sparc", SYS_WIN32
},
247 { "alpha", SYS_WIN32
},
248 { "powerpc", SYS_WIN32
}
252 char *p
, *spec
= xstrdup( target
);
254 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
256 if (!(p
= strchr( spec
, '-' ))) error( "Invalid target specification '%s'\n", target
);
258 for (i
= 0; i
< sizeof(cpu_names
)/sizeof(cpu_names
[0]); i
++)
260 if (!strcmp( cpu_names
[i
].name
, spec
))
262 typelib_kind
= cpu_names
[i
].kind
;
267 error( "Unrecognized CPU '%s'\n", spec
);
270 /* clean things up when aborting on a signal */
271 static void exit_on_signal( int sig
)
273 exit(1); /* this will call the atexit functions */
276 static void set_everything(int x
)
287 void start_cplusplus_guard(FILE *fp
)
289 fprintf(fp
, "#ifdef __cplusplus\n");
290 fprintf(fp
, "extern \"C\" {\n");
291 fprintf(fp
, "#endif\n\n");
294 void end_cplusplus_guard(FILE *fp
)
296 fprintf(fp
, "#ifdef __cplusplus\n");
298 fprintf(fp
, "#endif\n\n");
307 static void add_filename_node(struct list
*list
, const char *name
)
309 filename_node_t
*node
= xmalloc(sizeof *node
);
310 node
->filename
= dup_basename( name
, ".idl" );
311 list_add_tail(list
, &node
->link
);
314 static void free_filename_nodes(struct list
*list
)
316 filename_node_t
*node
, *next
;
317 LIST_FOR_EACH_ENTRY_SAFE(node
, next
, list
, filename_node_t
, link
) {
318 list_remove(&node
->link
);
319 free(node
->filename
);
324 static void write_dlldata_list(struct list
*filenames
)
327 filename_node_t
*node
;
329 dlldata
= fopen(dlldata_name
, "w");
331 error("couldn't open %s: %s\n", dlldata_name
, strerror(errno
));
333 fprintf(dlldata
, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION
);
334 fprintf(dlldata
, "- Do not edit ***/\n\n");
335 fprintf(dlldata
, "#include <objbase.h>\n");
336 fprintf(dlldata
, "#include <rpcproxy.h>\n\n");
337 start_cplusplus_guard(dlldata
);
339 LIST_FOR_EACH_ENTRY(node
, filenames
, filename_node_t
, link
)
340 fprintf(dlldata
, "EXTERN_PROXY_FILE(%s)\n", node
->filename
);
342 fprintf(dlldata
, "\nPROXYFILE_LIST_START\n");
343 fprintf(dlldata
, "/* Start of list */\n");
344 LIST_FOR_EACH_ENTRY(node
, filenames
, filename_node_t
, link
)
345 fprintf(dlldata
, " REFERENCE_PROXY_FILE(%s),\n", node
->filename
);
346 fprintf(dlldata
, "/* End of list */\n");
347 fprintf(dlldata
, "PROXYFILE_LIST_END\n\n");
349 fprintf(dlldata
, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
350 end_cplusplus_guard(dlldata
);
354 static char *eat_space(char *s
)
356 while (isspace((unsigned char) *s
))
361 void write_dlldata(const statement_list_t
*stmts
)
363 struct list filenames
= LIST_INIT(filenames
);
364 filename_node_t
*node
;
367 if (!do_dlldata
|| !need_proxy_file(stmts
))
370 dlldata
= fopen(dlldata_name
, "r");
372 static char marker
[] = "REFERENCE_PROXY_FILE";
376 while (widl_getline(&line
, &len
, dlldata
)) {
378 start
= eat_space(line
);
379 if (strncmp(start
, marker
, sizeof marker
- 1) == 0) {
380 start
= eat_space(start
+ sizeof marker
- 1);
383 end
= start
= eat_space(start
+ 1);
384 while (*end
&& *end
!= ')')
388 while (isspace((unsigned char) end
[-1]))
392 add_filename_node(&filenames
, start
);
397 error("couldn't read from %s: %s\n", dlldata_name
, strerror(errno
));
403 LIST_FOR_EACH_ENTRY(node
, &filenames
, filename_node_t
, link
)
404 if (strcmp(proxy_token
, node
->filename
) == 0) {
405 /* We're already in the list, no need to regenerate this file. */
406 free_filename_nodes(&filenames
);
410 add_filename_node(&filenames
, proxy_token
);
411 write_dlldata_list(&filenames
);
412 free_filename_nodes(&filenames
);
415 static void write_id_data_stmts(const statement_list_t
*stmts
)
417 const statement_t
*stmt
;
418 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
420 if (stmt
->type
== STMT_TYPE
)
422 const type_t
*type
= stmt
->u
.type
;
423 if (type_get_type(type
) == TYPE_INTERFACE
)
426 if (!is_object(type
->attrs
) && !is_attr(type
->attrs
, ATTR_DISPINTERFACE
))
428 uuid
= get_attrp(type
->attrs
, ATTR_UUID
);
429 write_guid(idfile
, is_attr(type
->attrs
, ATTR_DISPINTERFACE
) ? "DIID" : "IID",
432 else if (type_get_type(type
) == TYPE_COCLASS
)
434 const UUID
*uuid
= get_attrp(type
->attrs
, ATTR_UUID
);
435 write_guid(idfile
, "CLSID", type
->name
, uuid
);
438 else if (stmt
->type
== STMT_LIBRARY
)
440 const UUID
*uuid
= get_attrp(stmt
->u
.lib
->attrs
, ATTR_UUID
);
441 write_guid(idfile
, "LIBID", stmt
->u
.lib
->name
, uuid
);
442 write_id_data_stmts(stmt
->u
.lib
->stmts
);
447 void write_id_data(const statement_list_t
*stmts
)
449 if (!do_idfile
) return;
451 idfile_token
= make_token(idfile_name
);
453 idfile
= fopen(idfile_name
, "w");
455 error("Could not open %s for output\n", idfile_name
);
459 fprintf(idfile
, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION
);
460 fprintf(idfile
, "from %s - Do not edit ***/\n\n", input_name
);
461 fprintf(idfile
, "#include <rpc.h>\n");
462 fprintf(idfile
, "#include <rpcndr.h>\n\n");
463 fprintf(idfile
, "#include <initguid.h>\n\n");
464 start_cplusplus_guard(idfile
);
466 write_id_data_stmts(stmts
);
468 fprintf(idfile
, "\n");
469 end_cplusplus_guard(idfile
);
474 int main(int argc
,char *argv
[])
482 signal( SIGTERM
, exit_on_signal
);
483 signal( SIGINT
, exit_on_signal
);
485 signal( SIGHUP
, exit_on_signal
);
490 while((optc
= getopt_long(argc
, argv
, short_options
, long_options
, &opti
)) != EOF
) {
493 dlldata_name
= xstrdup(optarg
);
495 case DLLDATA_ONLY_OPTION
:
499 case LOCAL_STUBS_OPTION
:
501 local_stubs_name
= xstrdup(optarg
);
503 case OLDNAMES_OPTION
:
506 case PREFIX_ALL_OPTION
:
507 prefix_client
= xstrdup(optarg
);
508 prefix_server
= xstrdup(optarg
);
510 case PREFIX_CLIENT_OPTION
:
511 prefix_client
= xstrdup(optarg
);
513 case PREFIX_SERVER_OPTION
:
514 prefix_server
= xstrdup(optarg
);
525 set_target( optarg
);
532 client_name
= xstrdup(optarg
);
535 debuglevel
= strtol(optarg
, NULL
, 0);
538 wpp_add_cmdline_define(optarg
);
549 header_name
= xstrdup(optarg
);
552 wpp_add_include_path(optarg
);
555 if (!strcmp( optarg
, "32" )) typelib_kind
= SYS_WIN32
;
556 else if (!strcmp( optarg
, "64" )) typelib_kind
= SYS_WIN64
;
557 else error( "Invalid -m argument '%s'\n", optarg
);
567 proxy_name
= xstrdup(optarg
);
574 server_name
= xstrdup(optarg
);
581 typelib_name
= xstrdup(optarg
);
588 idfile_name
= xstrdup(optarg
);
591 printf("%s", version_string
);
597 fprintf(stderr
, "%s", usage
);
603 set_everything(TRUE
);
606 if (!dlldata_name
&& do_dlldata
)
607 dlldata_name
= xstrdup("dlldata.c");
610 if (do_dlldata
&& !do_everything
) {
611 struct list filenames
= LIST_INIT(filenames
);
612 for ( ; optind
< argc
; ++optind
)
613 add_filename_node(&filenames
, argv
[optind
]);
615 write_dlldata_list(&filenames
);
616 free_filename_nodes(&filenames
);
619 else if (optind
!= argc
- 1) {
620 fprintf(stderr
, "%s", usage
);
624 input_name
= xstrdup(argv
[optind
]);
627 fprintf(stderr
, "%s", usage
);
637 parser_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
638 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
640 wpp_set_debug( (debuglevel
& DEBUGLEVEL_PPLEX
) != 0,
641 (debuglevel
& DEBUGLEVEL_PPTRACE
) != 0,
642 (debuglevel
& DEBUGLEVEL_PPMSG
) != 0 );
645 header_name
= dup_basename(input_name
, ".idl");
646 strcat(header_name
, ".h");
649 if (!typelib_name
&& do_typelib
) {
650 typelib_name
= dup_basename(input_name
, ".idl");
651 strcat(typelib_name
, ".tlb");
654 if (!proxy_name
&& do_proxies
) {
655 proxy_name
= dup_basename(input_name
, ".idl");
656 strcat(proxy_name
, "_p.c");
659 if (!client_name
&& do_client
) {
660 client_name
= dup_basename(input_name
, ".idl");
661 strcat(client_name
, "_c.c");
664 if (!server_name
&& do_server
) {
665 server_name
= dup_basename(input_name
, ".idl");
666 strcat(server_name
, "_s.c");
669 if (!idfile_name
&& do_idfile
) {
670 idfile_name
= dup_basename(input_name
, ".idl");
671 strcat(idfile_name
, "_i.c");
674 if (do_proxies
) proxy_token
= dup_basename_token(proxy_name
,"_p.c");
675 if (do_client
) client_token
= dup_basename_token(client_name
,"_c.c");
676 if (do_server
) server_token
= dup_basename_token(server_name
,"_s.c");
678 add_widl_version_define();
683 chat("Starting preprocess\n");
685 if (!preprocess_only
)
687 ret
= wpp_parse_temp( input_name
, header_name
, &temp_name
);
691 ret
= wpp_parse( input_name
, stdout
);
695 if(preprocess_only
) exit(0);
696 if(!(parser_in
= fopen(temp_name
, "r"))) {
697 fprintf(stderr
, "Could not open %s for input\n", temp_name
);
702 if(!(parser_in
= fopen(input_name
, "r"))) {
703 fprintf(stderr
, "Could not open %s for input\n", input_name
);
708 header_token
= make_token(header_name
);
711 ret
= parser_parse();
719 /* Everything has been done successfully, don't delete any files. */
720 set_everything(FALSE
);
721 local_stubs_name
= NULL
;
726 static void rm_tempfile(void)
733 if (local_stubs_name
)
734 unlink(local_stubs_name
);
744 unlink(typelib_name
);