widl.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * IDL Compiler
  3. *
  4. * Copyright 2002 Ove Kaaven
  5. * based on WRC code by Bertho Stultiens
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include "config.h"
  22. #include "wine/port.h"
  23. #include <errno.h>
  24. #include <limits.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #ifdef HAVE_UNISTD_H
  28. # include <unistd.h>
  29. #endif
  30. #include <string.h>
  31. #include <assert.h>
  32. #include <ctype.h>
  33. #include <signal.h>
  34. #ifdef HAVE_GETOPT_H
  35. # include <getopt.h>
  36. #endif
  37. #include "widl.h"
  38. #include "utils.h"
  39. #include "parser.h"
  40. #include "wine/wpp.h"
  41. #include "header.h"
  42. static const char usage[] =
  43. "Usage: widl [options...] infile.idl\n"
  44. " or: widl [options...] --dlldata-only name1 [name2...]\n"
  45. " --acf=file Use ACF file\n"
  46. " -app_config Ignored, present for midl compatibility\n"
  47. " -b arch Set the target architecture\n"
  48. " -c Generate client stub\n"
  49. " -d n Set debug level to 'n'\n"
  50. " -D id[=val] Define preprocessor identifier id=val\n"
  51. " -E Preprocess only\n"
  52. " --help Display this help and exit\n"
  53. " -h Generate headers\n"
  54. " -H file Name of header file (default is infile.h)\n"
  55. " -I path Set include search dir to path (multiple -I allowed)\n"
  56. " --local-stubs=file Write empty stubs for call_as/local methods to file\n"
  57. " -m32, -m64 Set the target architecture (Win32 or Win64)\n"
  58. " -N Do not preprocess input\n"
  59. " --nostdinc Do not search the standard include path\n"
  60. " --ns_prefix Prefix namespaces with ABI namespace\n"
  61. " --oldnames Use old naming conventions\n"
  62. " -o, --output=NAME Set the output file name\n"
  63. " -Otype Type of stubs to generate (-Os, -Oi, -Oif)\n"
  64. " -p Generate proxy\n"
  65. " --prefix-all=p Prefix names of client stubs / server functions with 'p'\n"
  66. " --prefix-client=p Prefix names of client stubs with 'p'\n"
  67. " --prefix-server=p Prefix names of server functions with 'p'\n"
  68. " -r Generate registration script\n"
  69. " -robust Ignored, present for midl compatibility\n"
  70. " --sysroot=DIR Prefix include paths with DIR\n"
  71. " -s Generate server stub\n"
  72. " -t Generate typelib\n"
  73. " -u Generate interface identifiers file\n"
  74. " -V Print version and exit\n"
  75. " -W Enable pedantic warnings\n"
  76. " --win32, --win64 Set the target architecture (Win32 or Win64)\n"
  77. " --win32-align n Set win32 structure alignment to 'n'\n"
  78. " --win64-align n Set win64 structure alignment to 'n'\n"
  79. " --winrt Enable Windows Runtime mode\n"
  80. "Debug level 'n' is a bitmask with following meaning:\n"
  81. " * 0x01 Tell which resource is parsed (verbose mode)\n"
  82. " * 0x02 Dump internal structures\n"
  83. " * 0x04 Create a parser trace (yydebug=1)\n"
  84. " * 0x08 Preprocessor messages\n"
  85. " * 0x10 Preprocessor lex messages\n"
  86. " * 0x20 Preprocessor yacc trace\n"
  87. ;
  88. static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
  89. "Copyright 2002 Ove Kaaven\n";
  90. #ifdef __i386__
  91. enum target_cpu target_cpu = CPU_x86;
  92. #elif defined(__x86_64__)
  93. enum target_cpu target_cpu = CPU_x86_64;
  94. #elif defined(__powerpc64__)
  95. enum target_cpu target_cpu = CPU_POWERPC64;
  96. #elif defined(__powerpc__)
  97. enum target_cpu target_cpu = CPU_POWERPC;
  98. #elif defined(__arm__)
  99. enum target_cpu target_cpu = CPU_ARM;
  100. #elif defined(__aarch64__)
  101. enum target_cpu target_cpu = CPU_ARM64;
  102. #else
  103. #error Unsupported CPU
  104. #endif
  105. int debuglevel = DEBUGLEVEL_NONE;
  106. int parser_debug, yy_flex_debug;
  107. int pedantic = 0;
  108. int do_everything = 1;
  109. static int preprocess_only = 0;
  110. int do_header = 0;
  111. int do_typelib = 0;
  112. int do_proxies = 0;
  113. int do_client = 0;
  114. int do_server = 0;
  115. int do_regscript = 0;
  116. int do_idfile = 0;
  117. int do_dlldata = 0;
  118. static int no_preprocess = 0;
  119. int old_names = 0;
  120. int win32_packing = 8;
  121. int win64_packing = 8;
  122. int winrt_mode = 0;
  123. int use_abi_namespace = 0;
  124. static int stdinc = 1;
  125. static enum stub_mode stub_mode = MODE_Os;
  126. char *input_name;
  127. char *input_idl_name;
  128. char *acf_name;
  129. char *header_name;
  130. char *local_stubs_name;
  131. char *header_token;
  132. char *typelib_name;
  133. char *dlldata_name;
  134. char *proxy_name;
  135. char *proxy_token;
  136. char *client_name;
  137. char *client_token;
  138. char *server_name;
  139. char *server_token;
  140. char *regscript_name;
  141. char *regscript_token;
  142. static char *idfile_name;
  143. char *temp_name;
  144. const char *prefix_client = "";
  145. const char *prefix_server = "";
  146. static const char *includedir;
  147. int line_number = 1;
  148. static FILE *idfile;
  149. unsigned int pointer_size = 0;
  150. time_t now;
  151. enum {
  152. OLDNAMES_OPTION = CHAR_MAX + 1,
  153. ACF_OPTION,
  154. APP_CONFIG_OPTION,
  155. DLLDATA_OPTION,
  156. DLLDATA_ONLY_OPTION,
  157. LOCAL_STUBS_OPTION,
  158. NOSTDINC_OPTION,
  159. PREFIX_ALL_OPTION,
  160. PREFIX_CLIENT_OPTION,
  161. PREFIX_SERVER_OPTION,
  162. PRINT_HELP,
  163. RT_NS_PREFIX,
  164. RT_OPTION,
  165. ROBUST_OPTION,
  166. SYSROOT_OPTION,
  167. WIN32_OPTION,
  168. WIN64_OPTION,
  169. WIN32_ALIGN_OPTION,
  170. WIN64_ALIGN_OPTION
  171. };
  172. static const char short_options[] =
  173. "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
  174. static const struct option long_options[] = {
  175. { "acf", 1, NULL, ACF_OPTION },
  176. { "app_config", 0, NULL, APP_CONFIG_OPTION },
  177. { "dlldata", 1, NULL, DLLDATA_OPTION },
  178. { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
  179. { "help", 0, NULL, PRINT_HELP },
  180. { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
  181. { "nostdinc", 0, NULL, NOSTDINC_OPTION },
  182. { "ns_prefix", 0, NULL, RT_NS_PREFIX },
  183. { "oldnames", 0, NULL, OLDNAMES_OPTION },
  184. { "output", 0, NULL, 'o' },
  185. { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
  186. { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
  187. { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
  188. { "robust", 0, NULL, ROBUST_OPTION },
  189. { "sysroot", 1, NULL, SYSROOT_OPTION },
  190. { "target", 0, NULL, 'b' },
  191. { "winrt", 0, NULL, RT_OPTION },
  192. { "win32", 0, NULL, WIN32_OPTION },
  193. { "win64", 0, NULL, WIN64_OPTION },
  194. { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
  195. { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
  196. { NULL, 0, NULL, 0 }
  197. };
  198. static void rm_tempfile(void);
  199. enum stub_mode get_stub_mode(void)
  200. {
  201. /* old-style interpreted stubs are not supported on 64-bit */
  202. if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
  203. return stub_mode;
  204. }
  205. static char *make_token(const char *name)
  206. {
  207. char *token;
  208. char *slash;
  209. int i;
  210. slash = strrchr(name, '/');
  211. if(!slash)
  212. slash = strrchr(name, '\\');
  213. if (slash) name = slash + 1;
  214. token = xstrdup(name);
  215. for (i=0; token[i]; i++) {
  216. if (!isalnum(token[i])) token[i] = '_';
  217. else token[i] = tolower(token[i]);
  218. }
  219. return token;
  220. }
  221. /* duplicate a basename into a valid C token */
  222. static char *dup_basename_token(const char *name, const char *ext)
  223. {
  224. char *p, *ret = dup_basename( name, ext );
  225. /* map invalid characters to '_' */
  226. for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
  227. return ret;
  228. }
  229. static void add_widl_version_define(void)
  230. {
  231. char version_str[32];
  232. unsigned int version;
  233. const char *p = PACKAGE_VERSION;
  234. /* major */
  235. version = atoi(p) * 0x10000;
  236. p = strchr(p, '.');
  237. /* minor */
  238. if (p)
  239. {
  240. version += atoi(p + 1) * 0x100;
  241. p = strchr(p + 1, '.');
  242. }
  243. /* build */
  244. if (p)
  245. version += atoi(p + 1);
  246. snprintf(version_str, sizeof(version_str), "__WIDL__=0x%x", version);
  247. wpp_add_cmdline_define(version_str);
  248. }
  249. /* set the target platform */
  250. static void set_target( const char *target )
  251. {
  252. static const struct
  253. {
  254. const char *name;
  255. enum target_cpu cpu;
  256. } cpu_names[] =
  257. {
  258. { "i386", CPU_x86 },
  259. { "i486", CPU_x86 },
  260. { "i586", CPU_x86 },
  261. { "i686", CPU_x86 },
  262. { "i786", CPU_x86 },
  263. { "amd64", CPU_x86_64 },
  264. { "x86_64", CPU_x86_64 },
  265. { "powerpc", CPU_POWERPC },
  266. { "powerpc64", CPU_POWERPC64 },
  267. { "powerpc64le", CPU_POWERPC64 },
  268. { "arm", CPU_ARM },
  269. { "armv5", CPU_ARM },
  270. { "armv6", CPU_ARM },
  271. { "armv7", CPU_ARM },
  272. { "armv7a", CPU_ARM },
  273. { "arm64", CPU_ARM64 },
  274. { "aarch64", CPU_ARM64 },
  275. };
  276. unsigned int i;
  277. char *p, *spec = xstrdup( target );
  278. /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
  279. if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
  280. *p++ = 0;
  281. for (i = 0; i < ARRAY_SIZE( cpu_names ); i++)
  282. {
  283. if (!strcmp( cpu_names[i].name, spec ))
  284. {
  285. target_cpu = cpu_names[i].cpu;
  286. free( spec );
  287. return;
  288. }
  289. }
  290. error( "Unrecognized CPU '%s'\n", spec );
  291. }
  292. /* clean things up when aborting on a signal */
  293. static void exit_on_signal( int sig )
  294. {
  295. exit(1); /* this will call the atexit functions */
  296. }
  297. static void set_everything(int x)
  298. {
  299. do_header = x;
  300. do_typelib = x;
  301. do_proxies = x;
  302. do_client = x;
  303. do_server = x;
  304. do_regscript = x;
  305. do_idfile = x;
  306. do_dlldata = x;
  307. }
  308. void start_cplusplus_guard(FILE *fp)
  309. {
  310. fprintf(fp, "#ifdef __cplusplus\n");
  311. fprintf(fp, "extern \"C\" {\n");
  312. fprintf(fp, "#endif\n\n");
  313. }
  314. void end_cplusplus_guard(FILE *fp)
  315. {
  316. fprintf(fp, "#ifdef __cplusplus\n");
  317. fprintf(fp, "}\n");
  318. fprintf(fp, "#endif\n\n");
  319. }
  320. typedef struct
  321. {
  322. char *filename;
  323. struct list link;
  324. } filename_node_t;
  325. static void add_filename_node(struct list *list, const char *name)
  326. {
  327. filename_node_t *node = xmalloc(sizeof *node);
  328. node->filename = dup_basename( name, ".idl" );
  329. list_add_tail(list, &node->link);
  330. }
  331. static void free_filename_nodes(struct list *list)
  332. {
  333. filename_node_t *node, *next;
  334. LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
  335. list_remove(&node->link);
  336. free(node->filename);
  337. free(node);
  338. }
  339. }
  340. static void write_dlldata_list(struct list *filenames, int define_proxy_delegation)
  341. {
  342. FILE *dlldata;
  343. filename_node_t *node;
  344. dlldata = fopen(dlldata_name, "w");
  345. if (!dlldata)
  346. error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
  347. fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
  348. fprintf(dlldata, "- Do not edit ***/\n\n");
  349. if (define_proxy_delegation)
  350. fprintf(dlldata, "#define PROXY_DELEGATION\n");
  351. fprintf(dlldata, "#include <objbase.h>\n");
  352. fprintf(dlldata, "#include <rpcproxy.h>\n\n");
  353. start_cplusplus_guard(dlldata);
  354. LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
  355. fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
  356. fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
  357. fprintf(dlldata, "/* Start of list */\n");
  358. LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
  359. fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
  360. fprintf(dlldata, "/* End of list */\n");
  361. fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
  362. fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
  363. end_cplusplus_guard(dlldata);
  364. fclose(dlldata);
  365. }
  366. static char *eat_space(char *s)
  367. {
  368. while (isspace((unsigned char) *s))
  369. ++s;
  370. return s;
  371. }
  372. void write_dlldata(const statement_list_t *stmts)
  373. {
  374. struct list filenames = LIST_INIT(filenames);
  375. int define_proxy_delegation = 0;
  376. filename_node_t *node;
  377. FILE *dlldata;
  378. if (!do_dlldata || !need_proxy_file(stmts))
  379. return;
  380. define_proxy_delegation = need_proxy_delegation(stmts);
  381. dlldata = fopen(dlldata_name, "r");
  382. if (dlldata) {
  383. static const char marker[] = "REFERENCE_PROXY_FILE";
  384. static const char delegation_define[] = "#define PROXY_DELEGATION";
  385. char *line = NULL;
  386. size_t len = 0;
  387. while (widl_getline(&line, &len, dlldata)) {
  388. char *start, *end;
  389. start = eat_space(line);
  390. if (strncmp(start, marker, sizeof marker - 1) == 0) {
  391. start = eat_space(start + sizeof marker - 1);
  392. if (*start != '(')
  393. continue;
  394. end = start = eat_space(start + 1);
  395. while (*end && *end != ')')
  396. ++end;
  397. if (*end != ')')
  398. continue;
  399. while (isspace((unsigned char) end[-1]))
  400. --end;
  401. *end = '\0';
  402. if (start < end)
  403. add_filename_node(&filenames, start);
  404. }else if (!define_proxy_delegation && strncmp(start, delegation_define, sizeof(delegation_define)-1)) {
  405. define_proxy_delegation = 1;
  406. }
  407. }
  408. if (ferror(dlldata))
  409. error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
  410. free(line);
  411. fclose(dlldata);
  412. }
  413. LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
  414. if (strcmp(proxy_token, node->filename) == 0) {
  415. /* We're already in the list, no need to regenerate this file. */
  416. free_filename_nodes(&filenames);
  417. return;
  418. }
  419. add_filename_node(&filenames, proxy_token);
  420. write_dlldata_list(&filenames, define_proxy_delegation);
  421. free_filename_nodes(&filenames);
  422. }
  423. static void write_id_guid(FILE *f, const char *type, const char *guid_prefix, const char *name, const UUID *uuid)
  424. {
  425. if (!uuid) return;
  426. fprintf(f, "MIDL_DEFINE_GUID(%s, %s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
  427. "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
  428. type, guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
  429. uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
  430. uuid->Data4[6], uuid->Data4[7]);
  431. }
  432. static void write_id_data_stmts(const statement_list_t *stmts)
  433. {
  434. const statement_t *stmt;
  435. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  436. {
  437. if (stmt->type == STMT_TYPE)
  438. {
  439. const type_t *type = stmt->u.type;
  440. if (type_get_type(type) == TYPE_INTERFACE)
  441. {
  442. const UUID *uuid;
  443. if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
  444. continue;
  445. uuid = get_attrp(type->attrs, ATTR_UUID);
  446. write_id_guid(idfile, "IID", is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
  447. type->name, uuid);
  448. if (type_iface_get_async_iface(type))
  449. {
  450. uuid = get_attrp(type_iface_get_async_iface(type)->attrs, ATTR_UUID);
  451. write_id_guid(idfile, "IID", "IID", type_iface_get_async_iface(type)->name, uuid);
  452. }
  453. }
  454. else if (type_get_type(type) == TYPE_COCLASS)
  455. {
  456. const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
  457. write_id_guid(idfile, "CLSID", "CLSID", type->name, uuid);
  458. }
  459. }
  460. else if (stmt->type == STMT_LIBRARY)
  461. {
  462. const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
  463. write_id_guid(idfile, "IID", "LIBID", stmt->u.lib->name, uuid);
  464. write_id_data_stmts(stmt->u.lib->stmts);
  465. }
  466. }
  467. }
  468. void write_id_data(const statement_list_t *stmts)
  469. {
  470. if (!do_idfile) return;
  471. idfile = fopen(idfile_name, "w");
  472. if (! idfile) {
  473. error("Could not open %s for output\n", idfile_name);
  474. return;
  475. }
  476. fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
  477. fprintf(idfile, "from %s - Do not edit ***/\n\n", input_idl_name);
  478. fprintf(idfile, "#include <rpc.h>\n");
  479. fprintf(idfile, "#include <rpcndr.h>\n\n");
  480. fprintf(idfile, "#ifdef _MIDL_USE_GUIDDEF_\n\n");
  481. fprintf(idfile, "#ifndef INITGUID\n");
  482. fprintf(idfile, "#define INITGUID\n");
  483. fprintf(idfile, "#include <guiddef.h>\n");
  484. fprintf(idfile, "#undef INITGUID\n");
  485. fprintf(idfile, "#else\n");
  486. fprintf(idfile, "#include <guiddef.h>\n");
  487. fprintf(idfile, "#endif\n\n");
  488. fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
  489. fprintf(idfile, " DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)\n\n");
  490. fprintf(idfile, "#elif defined(__cplusplus)\n\n");
  491. fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
  492. fprintf(idfile, " EXTERN_C const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
  493. fprintf(idfile, "#else\n\n");
  494. fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
  495. fprintf(idfile, " const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
  496. fprintf(idfile, "#endif\n\n");
  497. start_cplusplus_guard(idfile);
  498. write_id_data_stmts(stmts);
  499. fprintf(idfile, "\n");
  500. end_cplusplus_guard(idfile);
  501. fprintf(idfile, "#undef MIDL_DEFINE_GUID\n" );
  502. fclose(idfile);
  503. }
  504. static void init_argv0_dir( const char *argv0 )
  505. {
  506. #ifndef _WIN32
  507. char *p, *dir;
  508. #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
  509. dir = realpath( "/proc/self/exe", NULL );
  510. #elif defined (__FreeBSD__) || defined(__DragonFly__)
  511. dir = realpath( "/proc/curproc/file", NULL );
  512. #else
  513. dir = realpath( argv0, NULL );
  514. #endif
  515. if (!dir) return;
  516. if (!(p = strrchr( dir, '/' ))) return;
  517. if (p == dir) p++;
  518. *p = 0;
  519. includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
  520. free( dir );
  521. #endif
  522. }
  523. int main(int argc,char *argv[])
  524. {
  525. int i, optc;
  526. int ret = 0;
  527. int opti = 0;
  528. char *output_name = NULL;
  529. const char *sysroot = "";
  530. signal( SIGTERM, exit_on_signal );
  531. signal( SIGINT, exit_on_signal );
  532. #ifdef SIGHUP
  533. signal( SIGHUP, exit_on_signal );
  534. #endif
  535. init_argv0_dir( argv[0] );
  536. now = time(NULL);
  537. while((optc = getopt_long_only(argc, argv, short_options, long_options, &opti)) != EOF) {
  538. switch(optc) {
  539. case DLLDATA_OPTION:
  540. dlldata_name = xstrdup(optarg);
  541. break;
  542. case DLLDATA_ONLY_OPTION:
  543. do_everything = 0;
  544. do_dlldata = 1;
  545. break;
  546. case LOCAL_STUBS_OPTION:
  547. do_everything = 0;
  548. local_stubs_name = xstrdup(optarg);
  549. break;
  550. case NOSTDINC_OPTION:
  551. stdinc = 0;
  552. break;
  553. case OLDNAMES_OPTION:
  554. old_names = 1;
  555. break;
  556. case PREFIX_ALL_OPTION:
  557. prefix_client = xstrdup(optarg);
  558. prefix_server = xstrdup(optarg);
  559. break;
  560. case PREFIX_CLIENT_OPTION:
  561. prefix_client = xstrdup(optarg);
  562. break;
  563. case PREFIX_SERVER_OPTION:
  564. prefix_server = xstrdup(optarg);
  565. break;
  566. case PRINT_HELP:
  567. fprintf(stderr, "%s", usage);
  568. return 0;
  569. case RT_OPTION:
  570. winrt_mode = 1;
  571. break;
  572. case RT_NS_PREFIX:
  573. use_abi_namespace = 1;
  574. break;
  575. case SYSROOT_OPTION:
  576. sysroot = xstrdup(optarg);
  577. break;
  578. case WIN32_OPTION:
  579. pointer_size = 4;
  580. break;
  581. case WIN64_OPTION:
  582. pointer_size = 8;
  583. break;
  584. case WIN32_ALIGN_OPTION:
  585. win32_packing = strtol(optarg, NULL, 0);
  586. if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
  587. error("Packing must be one of 2, 4 or 8\n");
  588. break;
  589. case WIN64_ALIGN_OPTION:
  590. win64_packing = strtol(optarg, NULL, 0);
  591. if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
  592. error("Packing must be one of 2, 4 or 8\n");
  593. break;
  594. case ACF_OPTION:
  595. acf_name = xstrdup(optarg);
  596. break;
  597. case APP_CONFIG_OPTION:
  598. /* widl does not distinguish between app_mode and default mode,
  599. but we ignore this option for midl compatibility */
  600. break;
  601. case ROBUST_OPTION:
  602. /* FIXME: Support robust option */
  603. break;
  604. case 'b':
  605. set_target( optarg );
  606. break;
  607. case 'c':
  608. do_everything = 0;
  609. do_client = 1;
  610. break;
  611. case 'C':
  612. client_name = xstrdup(optarg);
  613. break;
  614. case 'd':
  615. debuglevel = strtol(optarg, NULL, 0);
  616. break;
  617. case 'D':
  618. wpp_add_cmdline_define(optarg);
  619. break;
  620. case 'E':
  621. do_everything = 0;
  622. preprocess_only = 1;
  623. break;
  624. case 'h':
  625. do_everything = 0;
  626. do_header = 1;
  627. break;
  628. case 'H':
  629. header_name = xstrdup(optarg);
  630. break;
  631. case 'I':
  632. wpp_add_include_path(optarg);
  633. break;
  634. case 'm':
  635. if (!strcmp( optarg, "32" )) pointer_size = 4;
  636. else if (!strcmp( optarg, "64" )) pointer_size = 8;
  637. break;
  638. case 'N':
  639. no_preprocess = 1;
  640. break;
  641. case 'o':
  642. output_name = xstrdup(optarg);
  643. break;
  644. case 'O':
  645. if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
  646. else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
  647. else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
  648. else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
  649. else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
  650. else error( "Invalid argument '-O%s'\n", optarg );
  651. break;
  652. case 'p':
  653. do_everything = 0;
  654. do_proxies = 1;
  655. break;
  656. case 'P':
  657. proxy_name = xstrdup(optarg);
  658. break;
  659. case 'r':
  660. do_everything = 0;
  661. do_regscript = 1;
  662. break;
  663. case 's':
  664. do_everything = 0;
  665. do_server = 1;
  666. break;
  667. case 'S':
  668. server_name = xstrdup(optarg);
  669. break;
  670. case 't':
  671. do_everything = 0;
  672. do_typelib = 1;
  673. break;
  674. case 'T':
  675. typelib_name = xstrdup(optarg);
  676. break;
  677. case 'u':
  678. do_everything = 0;
  679. do_idfile = 1;
  680. break;
  681. case 'U':
  682. idfile_name = xstrdup(optarg);
  683. break;
  684. case 'V':
  685. printf("%s", version_string);
  686. return 0;
  687. case 'W':
  688. pedantic = 1;
  689. break;
  690. default:
  691. fprintf(stderr, "%s", usage);
  692. return 1;
  693. }
  694. }
  695. if (stdinc)
  696. {
  697. static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
  698. if (includedir)
  699. {
  700. wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
  701. wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
  702. }
  703. for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
  704. {
  705. if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
  706. wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
  707. wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
  708. }
  709. }
  710. switch (target_cpu)
  711. {
  712. case CPU_x86:
  713. if (pointer_size == 8) target_cpu = CPU_x86_64;
  714. else pointer_size = 4;
  715. break;
  716. case CPU_x86_64:
  717. if (pointer_size == 4) target_cpu = CPU_x86;
  718. else pointer_size = 8;
  719. break;
  720. case CPU_ARM64:
  721. if (pointer_size == 4) error( "Cannot build 32-bit code for this CPU\n" );
  722. pointer_size = 8;
  723. break;
  724. case CPU_POWERPC64:
  725. if (pointer_size == 4) error( "Cannot build 32-bit code for this CPU\n" );
  726. pointer_size = 8;
  727. break;
  728. default:
  729. if (pointer_size == 8) error( "Cannot build 64-bit code for this CPU\n" );
  730. pointer_size = 4;
  731. break;
  732. }
  733. /* if nothing specified, try to guess output type from the output file name */
  734. if (output_name && do_everything && !do_header && !do_typelib && !do_proxies &&
  735. !do_client && !do_server && !do_regscript && !do_idfile && !do_dlldata)
  736. {
  737. do_everything = 0;
  738. if (strendswith( output_name, ".h" )) do_header = 1;
  739. else if (strendswith( output_name, ".tlb" )) do_typelib = 1;
  740. else if (strendswith( output_name, "_p.c" )) do_proxies = 1;
  741. else if (strendswith( output_name, "_c.c" )) do_client = 1;
  742. else if (strendswith( output_name, "_s.c" )) do_server = 1;
  743. else if (strendswith( output_name, "_i.c" )) do_idfile = 1;
  744. else if (strendswith( output_name, "_r.res" )) do_regscript = 1;
  745. else if (strendswith( output_name, "_t.res" )) do_typelib = 1;
  746. else if (strendswith( output_name, "dlldata.c" )) do_dlldata = 1;
  747. else do_everything = 1;
  748. }
  749. if(do_everything) {
  750. set_everything(TRUE);
  751. }
  752. if (do_header + do_typelib + do_proxies + do_client +
  753. do_server + do_regscript + do_idfile + do_dlldata == 1 && output_name)
  754. {
  755. if (do_header && !header_name) header_name = output_name;
  756. else if (do_typelib && !typelib_name) typelib_name = output_name;
  757. else if (do_proxies && !proxy_name) proxy_name = output_name;
  758. else if (do_client && !client_name) client_name = output_name;
  759. else if (do_server && !server_name) server_name = output_name;
  760. else if (do_regscript && !regscript_name) regscript_name = output_name;
  761. else if (do_idfile && !idfile_name) idfile_name = output_name;
  762. else if (do_dlldata && !dlldata_name) dlldata_name = output_name;
  763. }
  764. if (!dlldata_name && do_dlldata)
  765. dlldata_name = xstrdup("dlldata.c");
  766. if(optind < argc) {
  767. if (do_dlldata && !do_everything) {
  768. struct list filenames = LIST_INIT(filenames);
  769. for ( ; optind < argc; ++optind)
  770. add_filename_node(&filenames, argv[optind]);
  771. write_dlldata_list(&filenames, 0 /* FIXME */ );
  772. free_filename_nodes(&filenames);
  773. return 0;
  774. }
  775. else if (optind != argc - 1) {
  776. fprintf(stderr, "%s", usage);
  777. return 1;
  778. }
  779. else
  780. input_idl_name = input_name = xstrdup(argv[optind]);
  781. }
  782. else {
  783. fprintf(stderr, "%s", usage);
  784. return 1;
  785. }
  786. if(debuglevel)
  787. {
  788. setbuf(stdout, NULL);
  789. setbuf(stderr, NULL);
  790. }
  791. parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
  792. yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
  793. wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
  794. (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
  795. (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
  796. if (!header_name) {
  797. header_name = dup_basename(input_name, ".idl");
  798. strcat(header_name, ".h");
  799. }
  800. if (!typelib_name && do_typelib) {
  801. typelib_name = dup_basename(input_name, ".idl");
  802. strcat(typelib_name, ".tlb");
  803. }
  804. if (!proxy_name && do_proxies) {
  805. proxy_name = dup_basename(input_name, ".idl");
  806. strcat(proxy_name, "_p.c");
  807. }
  808. if (!client_name && do_client) {
  809. client_name = dup_basename(input_name, ".idl");
  810. strcat(client_name, "_c.c");
  811. }
  812. if (!server_name && do_server) {
  813. server_name = dup_basename(input_name, ".idl");
  814. strcat(server_name, "_s.c");
  815. }
  816. if (!regscript_name && do_regscript) {
  817. regscript_name = dup_basename(input_name, ".idl");
  818. strcat(regscript_name, "_r.rgs");
  819. }
  820. if (!idfile_name && do_idfile) {
  821. idfile_name = dup_basename(input_name, ".idl");
  822. strcat(idfile_name, "_i.c");
  823. }
  824. if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
  825. if (do_client) client_token = dup_basename_token(client_name,"_c.c");
  826. if (do_server) server_token = dup_basename_token(server_name,"_s.c");
  827. if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
  828. add_widl_version_define();
  829. wpp_add_cmdline_define("_WIN32=1");
  830. atexit(rm_tempfile);
  831. if (!no_preprocess)
  832. {
  833. chat("Starting preprocess\n");
  834. if (!preprocess_only)
  835. {
  836. FILE *output;
  837. int fd;
  838. char *name = xmalloc( strlen(header_name) + 8 );
  839. strcpy( name, header_name );
  840. strcat( name, ".XXXXXX" );
  841. if ((fd = mkstemps( name, 0 )) == -1)
  842. error("Could not generate a temp name from %s\n", name);
  843. temp_name = name;
  844. if (!(output = fdopen(fd, "wt")))
  845. error("Could not open fd %s for writing\n", name);
  846. ret = wpp_parse( input_name, output );
  847. fclose( output );
  848. }
  849. else
  850. {
  851. ret = wpp_parse( input_name, stdout );
  852. }
  853. if(ret) exit(1);
  854. if(preprocess_only) exit(0);
  855. if(!(parser_in = fopen(temp_name, "r"))) {
  856. fprintf(stderr, "Could not open %s for input\n", temp_name);
  857. return 1;
  858. }
  859. }
  860. else {
  861. if(!(parser_in = fopen(input_name, "r"))) {
  862. fprintf(stderr, "Could not open %s for input\n", input_name);
  863. return 1;
  864. }
  865. }
  866. header_token = make_token(header_name);
  867. init_types();
  868. ret = parser_parse();
  869. fclose(parser_in);
  870. if(ret) {
  871. exit(1);
  872. }
  873. /* Everything has been done successfully, don't delete any files. */
  874. set_everything(FALSE);
  875. local_stubs_name = NULL;
  876. return 0;
  877. }
  878. static void rm_tempfile(void)
  879. {
  880. abort_import();
  881. if(temp_name)
  882. unlink(temp_name);
  883. if (do_header)
  884. unlink(header_name);
  885. if (local_stubs_name)
  886. unlink(local_stubs_name);
  887. if (do_client)
  888. unlink(client_name);
  889. if (do_server)
  890. unlink(server_name);
  891. if (do_regscript)
  892. unlink(regscript_name);
  893. if (do_idfile)
  894. unlink(idfile_name);
  895. if (do_proxies)
  896. unlink(proxy_name);
  897. if (do_typelib)
  898. unlink(typelib_name);
  899. }