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 " -app_config Ignored, present for midl compatibility\n"
55 " -b arch Set the target architecture\n"
56 " -c Generate client stub\n"
57 " -d n Set debug level to 'n'\n"
58 " -D id[=val] Define preprocessor identifier id=val\n"
59 " -E Preprocess only\n"
60 " --help Display this help and exit\n"
61 " -h Generate headers\n"
62 " -H file Name of header file (default is infile.h)\n"
63 " -I path Set include search dir to path (multiple -I allowed)\n"
64 " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
65 " -m32, -m64 Set the kind of typelib to build (Win32 or Win64)\n"
66 " -N Do not preprocess input\n"
67 " --oldnames Use old naming conventions\n"
68 " -o, --output=NAME Set the output file name\n"
69 " -Otype Type of stubs to generate (-Os, -Oi, -Oif)\n"
70 " -p Generate proxy\n"
71 " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
72 " --prefix-client=p Prefix names of client stubs with 'p'\n"
73 " --prefix-server=p Prefix names of server functions with 'p'\n"
74 " -r Generate registration script\n"
75 " --rt Enable WinRT's language extensions for IDL\n"
76 " -s Generate server stub\n"
77 " -t Generate typelib\n"
78 " -u Generate interface identifiers file\n"
79 " -V Print version and exit\n"
80 " -W Enable pedantic warnings\n"
81 " --win32 Only generate 32-bit code\n"
82 " --win64 Only generate 64-bit code\n"
83 " --win32-align n Set win32 structure alignment to 'n'\n"
84 " --win64-align n Set win64 structure alignment to 'n'\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 static int preprocess_only
= 0;
108 int do_regscript
= 0;
111 static int no_preprocess
= 0;
115 int win32_packing
= 8;
116 int win64_packing
= 8;
117 int do_rt_extension
= 0;
118 static enum stub_mode stub_mode
= MODE_Os
;
121 char *input_idl_name
;
123 char *local_stubs_name
;
133 char *regscript_name
;
134 char *regscript_token
;
135 static char *idfile_name
;
137 const char *prefix_client
= "";
138 const char *prefix_server
= "";
144 unsigned int pointer_size
= 0;
145 syskind_t typelib_kind
= sizeof(void*) == 8 ? SYS_WIN64
: SYS_WIN32
;
150 OLDNAMES_OPTION
= CHAR_MAX
+ 1,
155 PREFIX_CLIENT_OPTION
,
156 PREFIX_SERVER_OPTION
,
166 static const char short_options
[] =
167 "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
168 static const struct option long_options
[] = {
169 { "dlldata", 1, NULL
, DLLDATA_OPTION
},
170 { "dlldata-only", 0, NULL
, DLLDATA_ONLY_OPTION
},
171 { "help", 0, NULL
, PRINT_HELP
},
172 { "local-stubs", 1, NULL
, LOCAL_STUBS_OPTION
},
173 { "oldnames", 0, NULL
, OLDNAMES_OPTION
},
174 { "output", 0, NULL
, 'o' },
175 { "prefix-all", 1, NULL
, PREFIX_ALL_OPTION
},
176 { "prefix-client", 1, NULL
, PREFIX_CLIENT_OPTION
},
177 { "prefix-server", 1, NULL
, PREFIX_SERVER_OPTION
},
178 { "rt", 0, NULL
, RT_OPTION
},
179 { "win32", 0, NULL
, WIN32_OPTION
},
180 { "win64", 0, NULL
, WIN64_OPTION
},
181 { "win32-align", 1, NULL
, WIN32_ALIGN_OPTION
},
182 { "win64-align", 1, NULL
, WIN64_ALIGN_OPTION
},
183 { "app_config", 0, NULL
, APP_CONFIG_OPTION
},
187 static void rm_tempfile(void);
189 enum stub_mode
get_stub_mode(void)
191 /* old-style interpreted stubs are not supported on 64-bit */
192 if (stub_mode
== MODE_Oi
&& pointer_size
== 8) return MODE_Oif
;
196 static char *make_token(const char *name
)
202 slash
= strrchr(name
, '/');
204 slash
= strrchr(name
, '\\');
206 if (slash
) name
= slash
+ 1;
208 token
= xstrdup(name
);
209 for (i
=0; token
[i
]; i
++) {
210 if (!isalnum(token
[i
])) token
[i
] = '_';
211 else token
[i
] = tolower(token
[i
]);
216 /* duplicate a basename into a valid C token */
217 static char *dup_basename_token(const char *name
, const char *ext
)
219 char *p
, *ret
= dup_basename( name
, ext
);
220 /* map invalid characters to '_' */
221 for (p
= ret
; *p
; p
++) if (!isalnum(*p
)) *p
= '_';
225 static void add_widl_version_define(void)
227 unsigned int version
;
228 const char *p
= PACKAGE_VERSION
;
231 version
= atoi(p
) * 0x10000;
237 version
+= atoi(p
+ 1) * 0x100;
238 p
= strchr(p
+ 1, '.');
243 version
+= atoi(p
+ 1);
247 char version_str
[11];
248 snprintf(version_str
, sizeof(version_str
), "0x%x", version
);
249 wpp_add_define("__WIDL__", version_str
);
252 wpp_add_define("__WIDL__", NULL
);
255 /* set the target platform */
256 static void set_target( const char *target
)
264 { "i386", SYS_WIN32
},
265 { "i486", SYS_WIN32
},
266 { "i586", SYS_WIN32
},
267 { "i686", SYS_WIN32
},
268 { "i786", SYS_WIN32
},
269 { "amd64", SYS_WIN64
},
270 { "x86_64", SYS_WIN64
},
271 { "powerpc", SYS_WIN32
},
272 { "arm", SYS_WIN32
},
273 { "aarch64", SYS_WIN64
}
277 char *p
, *spec
= xstrdup( target
);
279 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
281 if (!(p
= strchr( spec
, '-' ))) error( "Invalid target specification '%s'\n", target
);
283 for (i
= 0; i
< sizeof(cpu_names
)/sizeof(cpu_names
[0]); i
++)
285 if (!strcmp( cpu_names
[i
].name
, spec
))
287 typelib_kind
= cpu_names
[i
].kind
;
292 error( "Unrecognized CPU '%s'\n", spec
);
295 /* clean things up when aborting on a signal */
296 static void exit_on_signal( int sig
)
298 exit(1); /* this will call the atexit functions */
301 static void set_everything(int x
)
313 void start_cplusplus_guard(FILE *fp
)
315 fprintf(fp
, "#ifdef __cplusplus\n");
316 fprintf(fp
, "extern \"C\" {\n");
317 fprintf(fp
, "#endif\n\n");
320 void end_cplusplus_guard(FILE *fp
)
322 fprintf(fp
, "#ifdef __cplusplus\n");
324 fprintf(fp
, "#endif\n\n");
333 static void add_filename_node(struct list
*list
, const char *name
)
335 filename_node_t
*node
= xmalloc(sizeof *node
);
336 node
->filename
= dup_basename( name
, ".idl" );
337 list_add_tail(list
, &node
->link
);
340 static void free_filename_nodes(struct list
*list
)
342 filename_node_t
*node
, *next
;
343 LIST_FOR_EACH_ENTRY_SAFE(node
, next
, list
, filename_node_t
, link
) {
344 list_remove(&node
->link
);
345 free(node
->filename
);
350 static void write_dlldata_list(struct list
*filenames
, int define_proxy_delegation
)
353 filename_node_t
*node
;
355 dlldata
= fopen(dlldata_name
, "w");
357 error("couldn't open %s: %s\n", dlldata_name
, strerror(errno
));
359 fprintf(dlldata
, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION
);
360 fprintf(dlldata
, "- Do not edit ***/\n\n");
361 if (define_proxy_delegation
)
362 fprintf(dlldata
, "#define PROXY_DELEGATION\n");
363 fprintf(dlldata
, "#include <objbase.h>\n");
364 fprintf(dlldata
, "#include <rpcproxy.h>\n\n");
365 start_cplusplus_guard(dlldata
);
367 LIST_FOR_EACH_ENTRY(node
, filenames
, filename_node_t
, link
)
368 fprintf(dlldata
, "EXTERN_PROXY_FILE(%s)\n", node
->filename
);
370 fprintf(dlldata
, "\nPROXYFILE_LIST_START\n");
371 fprintf(dlldata
, "/* Start of list */\n");
372 LIST_FOR_EACH_ENTRY(node
, filenames
, filename_node_t
, link
)
373 fprintf(dlldata
, " REFERENCE_PROXY_FILE(%s),\n", node
->filename
);
374 fprintf(dlldata
, "/* End of list */\n");
375 fprintf(dlldata
, "PROXYFILE_LIST_END\n\n");
377 fprintf(dlldata
, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
378 end_cplusplus_guard(dlldata
);
382 static char *eat_space(char *s
)
384 while (isspace((unsigned char) *s
))
389 void write_dlldata(const statement_list_t
*stmts
)
391 struct list filenames
= LIST_INIT(filenames
);
392 int define_proxy_delegation
= 0;
393 filename_node_t
*node
;
396 if (!do_dlldata
|| !need_proxy_file(stmts
))
399 define_proxy_delegation
= need_proxy_delegation(stmts
);
401 dlldata
= fopen(dlldata_name
, "r");
403 static char marker
[] = "REFERENCE_PROXY_FILE";
404 static const char delegation_define
[] = "#define PROXY_DELEGATION";
408 while (widl_getline(&line
, &len
, dlldata
)) {
410 start
= eat_space(line
);
411 if (strncmp(start
, marker
, sizeof marker
- 1) == 0) {
412 start
= eat_space(start
+ sizeof marker
- 1);
415 end
= start
= eat_space(start
+ 1);
416 while (*end
&& *end
!= ')')
420 while (isspace((unsigned char) end
[-1]))
424 add_filename_node(&filenames
, start
);
425 }else if (!define_proxy_delegation
&& strncmp(start
, delegation_define
, sizeof(delegation_define
)-1)) {
426 define_proxy_delegation
= 1;
431 error("couldn't read from %s: %s\n", dlldata_name
, strerror(errno
));
437 LIST_FOR_EACH_ENTRY(node
, &filenames
, filename_node_t
, link
)
438 if (strcmp(proxy_token
, node
->filename
) == 0) {
439 /* We're already in the list, no need to regenerate this file. */
440 free_filename_nodes(&filenames
);
444 add_filename_node(&filenames
, proxy_token
);
445 write_dlldata_list(&filenames
, define_proxy_delegation
);
446 free_filename_nodes(&filenames
);
449 static void write_id_data_stmts(const statement_list_t
*stmts
)
451 const statement_t
*stmt
;
452 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
454 if (stmt
->type
== STMT_TYPE
)
456 const type_t
*type
= stmt
->u
.type
;
457 if (type_get_type(type
) == TYPE_INTERFACE
)
460 if (!is_object(type
) && !is_attr(type
->attrs
, ATTR_DISPINTERFACE
))
462 uuid
= get_attrp(type
->attrs
, ATTR_UUID
);
463 write_guid(idfile
, is_attr(type
->attrs
, ATTR_DISPINTERFACE
) ? "DIID" : "IID",
466 else if (type_get_type(type
) == TYPE_COCLASS
)
468 const UUID
*uuid
= get_attrp(type
->attrs
, ATTR_UUID
);
469 write_guid(idfile
, "CLSID", type
->name
, uuid
);
472 else if (stmt
->type
== STMT_LIBRARY
)
474 const UUID
*uuid
= get_attrp(stmt
->u
.lib
->attrs
, ATTR_UUID
);
475 write_guid(idfile
, "LIBID", stmt
->u
.lib
->name
, uuid
);
476 write_id_data_stmts(stmt
->u
.lib
->stmts
);
481 void write_id_data(const statement_list_t
*stmts
)
483 if (!do_idfile
) return;
485 idfile
= fopen(idfile_name
, "w");
487 error("Could not open %s for output\n", idfile_name
);
491 fprintf(idfile
, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION
);
492 fprintf(idfile
, "from %s - Do not edit ***/\n\n", input_idl_name
);
493 fprintf(idfile
, "#include <rpc.h>\n");
494 fprintf(idfile
, "#include <rpcndr.h>\n\n");
495 fprintf(idfile
, "#include <initguid.h>\n\n");
496 start_cplusplus_guard(idfile
);
498 write_id_data_stmts(stmts
);
500 fprintf(idfile
, "\n");
501 end_cplusplus_guard(idfile
);
506 int main(int argc
,char *argv
[])
511 char *output_name
= NULL
;
513 signal( SIGTERM
, exit_on_signal
);
514 signal( SIGINT
, exit_on_signal
);
516 signal( SIGHUP
, exit_on_signal
);
521 while((optc
= getopt_long_only(argc
, argv
, short_options
, long_options
, &opti
)) != EOF
) {
524 dlldata_name
= xstrdup(optarg
);
526 case DLLDATA_ONLY_OPTION
:
530 case LOCAL_STUBS_OPTION
:
532 local_stubs_name
= xstrdup(optarg
);
534 case OLDNAMES_OPTION
:
537 case PREFIX_ALL_OPTION
:
538 prefix_client
= xstrdup(optarg
);
539 prefix_server
= xstrdup(optarg
);
541 case PREFIX_CLIENT_OPTION
:
542 prefix_client
= xstrdup(optarg
);
544 case PREFIX_SERVER_OPTION
:
545 prefix_server
= xstrdup(optarg
);
548 fprintf(stderr
, "%s", usage
);
561 case WIN32_ALIGN_OPTION
:
562 win32_packing
= strtol(optarg
, NULL
, 0);
563 if(win32_packing
!= 2 && win32_packing
!= 4 && win32_packing
!= 8)
564 error("Packing must be one of 2, 4 or 8\n");
566 case WIN64_ALIGN_OPTION
:
567 win64_packing
= strtol(optarg
, NULL
, 0);
568 if(win64_packing
!= 2 && win64_packing
!= 4 && win64_packing
!= 8)
569 error("Packing must be one of 2, 4 or 8\n");
571 case APP_CONFIG_OPTION
:
572 /* widl does not distinguish between app_mode and default mode,
573 but we ignore this option for midl compatibility */
576 set_target( optarg
);
583 client_name
= xstrdup(optarg
);
586 debuglevel
= strtol(optarg
, NULL
, 0);
589 wpp_add_cmdline_define(optarg
);
600 header_name
= xstrdup(optarg
);
603 wpp_add_include_path(optarg
);
606 if (!strcmp( optarg
, "32" )) typelib_kind
= SYS_WIN32
;
607 else if (!strcmp( optarg
, "64" )) typelib_kind
= SYS_WIN64
;
608 else error( "Invalid -m argument '%s'\n", optarg
);
614 output_name
= xstrdup(optarg
);
617 if (!strcmp( optarg
, "s" )) stub_mode
= MODE_Os
;
618 else if (!strcmp( optarg
, "i" )) stub_mode
= MODE_Oi
;
619 else if (!strcmp( optarg
, "ic" )) stub_mode
= MODE_Oif
;
620 else if (!strcmp( optarg
, "if" )) stub_mode
= MODE_Oif
;
621 else if (!strcmp( optarg
, "icf" )) stub_mode
= MODE_Oif
;
622 else error( "Invalid argument '-O%s'\n", optarg
);
629 proxy_name
= xstrdup(optarg
);
640 server_name
= xstrdup(optarg
);
647 typelib_name
= xstrdup(optarg
);
654 idfile_name
= xstrdup(optarg
);
657 printf("%s", version_string
);
663 fprintf(stderr
, "%s", usage
);
668 #ifdef DEFAULT_INCLUDE_DIR
669 wpp_add_include_path(DEFAULT_INCLUDE_DIR
);
673 set_everything(TRUE
);
676 if (!output_name
) output_name
= dup_basename(input_name
, ".idl");
678 if (!do_everything
&&
679 do_header
+ do_typelib
+ do_proxies
+ do_client
+
680 do_server
+ do_regscript
+ do_idfile
+ do_dlldata
== 1)
682 if (do_header
) header_name
= output_name
;
683 else if (do_typelib
) typelib_name
= output_name
;
684 else if (do_proxies
) proxy_name
= output_name
;
685 else if (do_client
) client_name
= output_name
;
686 else if (do_server
) server_name
= output_name
;
687 else if (do_regscript
) regscript_name
= output_name
;
688 else if (do_idfile
) idfile_name
= output_name
;
689 else if (do_dlldata
) dlldata_name
= output_name
;
692 if (!dlldata_name
&& do_dlldata
)
693 dlldata_name
= xstrdup("dlldata.c");
696 if (do_dlldata
&& !do_everything
) {
697 struct list filenames
= LIST_INIT(filenames
);
698 for ( ; optind
< argc
; ++optind
)
699 add_filename_node(&filenames
, argv
[optind
]);
701 write_dlldata_list(&filenames
, 0 /* FIXME */ );
702 free_filename_nodes(&filenames
);
705 else if (optind
!= argc
- 1) {
706 fprintf(stderr
, "%s", usage
);
710 input_idl_name
= input_name
= xstrdup(argv
[optind
]);
713 fprintf(stderr
, "%s", usage
);
719 setbuf(stdout
, NULL
);
720 setbuf(stderr
, NULL
);
723 parser_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
724 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
726 wpp_set_debug( (debuglevel
& DEBUGLEVEL_PPLEX
) != 0,
727 (debuglevel
& DEBUGLEVEL_PPTRACE
) != 0,
728 (debuglevel
& DEBUGLEVEL_PPMSG
) != 0 );
731 header_name
= dup_basename(input_name
, ".idl");
732 strcat(header_name
, ".h");
735 if (!typelib_name
&& do_typelib
) {
736 typelib_name
= dup_basename(input_name
, ".idl");
737 strcat(typelib_name
, ".tlb");
740 if (!proxy_name
&& do_proxies
) {
741 proxy_name
= dup_basename(input_name
, ".idl");
742 strcat(proxy_name
, "_p.c");
745 if (!client_name
&& do_client
) {
746 client_name
= dup_basename(input_name
, ".idl");
747 strcat(client_name
, "_c.c");
750 if (!server_name
&& do_server
) {
751 server_name
= dup_basename(input_name
, ".idl");
752 strcat(server_name
, "_s.c");
755 if (!regscript_name
&& do_regscript
) {
756 regscript_name
= dup_basename(input_name
, ".idl");
757 strcat(regscript_name
, "_r.rgs");
760 if (!idfile_name
&& do_idfile
) {
761 idfile_name
= dup_basename(input_name
, ".idl");
762 strcat(idfile_name
, "_i.c");
765 if (do_proxies
) proxy_token
= dup_basename_token(proxy_name
,"_p.c");
766 if (do_client
) client_token
= dup_basename_token(client_name
,"_c.c");
767 if (do_server
) server_token
= dup_basename_token(server_name
,"_s.c");
768 if (do_regscript
) regscript_token
= dup_basename_token(regscript_name
,"_r.rgs");
770 add_widl_version_define();
771 wpp_add_define("_WIN32", NULL
);
776 chat("Starting preprocess\n");
778 if (!preprocess_only
)
782 char *name
= xmalloc( strlen(header_name
) + 8 );
784 strcpy( name
, header_name
);
785 strcat( name
, ".XXXXXX" );
787 if ((fd
= mkstemps( name
, 0 )) == -1)
788 error("Could not generate a temp name from %s\n", name
);
791 if (!(output
= fdopen(fd
, "wt")))
792 error("Could not open fd %s for writing\n", name
);
794 ret
= wpp_parse( input_name
, output
);
799 ret
= wpp_parse( input_name
, stdout
);
803 if(preprocess_only
) exit(0);
804 if(!(parser_in
= fopen(temp_name
, "r"))) {
805 fprintf(stderr
, "Could not open %s for input\n", temp_name
);
810 if(!(parser_in
= fopen(input_name
, "r"))) {
811 fprintf(stderr
, "Could not open %s for input\n", input_name
);
816 header_token
= make_token(header_name
);
819 ret
= parser_parse();
827 /* Everything has been done successfully, don't delete any files. */
828 set_everything(FALSE
);
829 local_stubs_name
= NULL
;
834 static void rm_tempfile(void)
841 if (local_stubs_name
)
842 unlink(local_stubs_name
);
848 unlink(regscript_name
);
854 unlink(typelib_name
);