widl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 const struct
  199. {
  200. const char *name;
  201. enum target_cpu cpu;
  202. } cpu_names[] =
  203. {
  204. { "i386", CPU_x86 },
  205. { "i486", CPU_x86 },
  206. { "i586", CPU_x86 },
  207. { "i686", CPU_x86 },
  208. { "i786", CPU_x86 },
  209. { "amd64", CPU_x86_64 },
  210. { "x86_64", CPU_x86_64 },
  211. { "powerpc", CPU_POWERPC },
  212. { "powerpc64", CPU_POWERPC64 },
  213. { "powerpc64le", CPU_POWERPC64 },
  214. { "arm", CPU_ARM },
  215. { "armv5", CPU_ARM },
  216. { "armv6", CPU_ARM },
  217. { "armv7", CPU_ARM },
  218. { "armv7a", CPU_ARM },
  219. { "arm64", CPU_ARM64 },
  220. { "aarch64", CPU_ARM64 },
  221. };
  222. static void rm_tempfile(void);
  223. enum stub_mode get_stub_mode(void)
  224. {
  225. /* old-style interpreted stubs are not supported on 64-bit */
  226. if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
  227. return stub_mode;
  228. }
  229. static char *make_token(const char *name)
  230. {
  231. char *token;
  232. char *slash;
  233. int i;
  234. slash = strrchr(name, '/');
  235. if(!slash)
  236. slash = strrchr(name, '\\');
  237. if (slash) name = slash + 1;
  238. token = xstrdup(name);
  239. for (i=0; token[i]; i++) {
  240. if (!isalnum(token[i])) token[i] = '_';
  241. else token[i] = tolower(token[i]);
  242. }
  243. return token;
  244. }
  245. /* duplicate a basename into a valid C token */
  246. static char *dup_basename_token(const char *name, const char *ext)
  247. {
  248. char *p, *ret = dup_basename( name, ext );
  249. /* map invalid characters to '_' */
  250. for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
  251. return ret;
  252. }
  253. static void add_widl_version_define(void)
  254. {
  255. char version_str[32];
  256. unsigned int version;
  257. const char *p = PACKAGE_VERSION;
  258. /* major */
  259. version = atoi(p) * 0x10000;
  260. p = strchr(p, '.');
  261. /* minor */
  262. if (p)
  263. {
  264. version += atoi(p + 1) * 0x100;
  265. p = strchr(p + 1, '.');
  266. }
  267. /* build */
  268. if (p)
  269. version += atoi(p + 1);
  270. snprintf(version_str, sizeof(version_str), "__WIDL__=0x%x", version);
  271. wpp_add_cmdline_define(version_str);
  272. }
  273. static void set_cpu( const char *cpu, int error_out )
  274. {
  275. unsigned int i;
  276. for (i = 0; i < ARRAY_SIZE( cpu_names ); i++)
  277. {
  278. if (!strcmp( cpu_names[i].name, cpu ))
  279. {
  280. target_cpu = cpu_names[i].cpu;
  281. return;
  282. }
  283. }
  284. if (error_out)
  285. error( "Unrecognized CPU '%s'\n", cpu );
  286. }
  287. /* Set the target platform based on a potential prefix of the executable name.
  288. * If not found, or not matching a known CPU name, just proceed silently. */
  289. static void init_argv0_target( const char *argv0 )
  290. {
  291. char *p, *name;
  292. if ((p = strrchr(argv0, '/')) != NULL)
  293. argv0 = p + 1;
  294. if ((p = strrchr(argv0, '\\')) != NULL)
  295. argv0 = p + 1;
  296. name = xstrdup( argv0 );
  297. if (!(p = strchr(name, '-')))
  298. {
  299. free( name );
  300. return;
  301. }
  302. *p = 0;
  303. set_cpu( name, 0 );
  304. free( name );
  305. }
  306. /* set the target platform */
  307. static void set_target( const char *target )
  308. {
  309. char *p, *spec = xstrdup( target );
  310. /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
  311. if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
  312. *p++ = 0;
  313. set_cpu( spec, 1 );
  314. free( spec );
  315. }
  316. /* clean things up when aborting on a signal */
  317. static void exit_on_signal( int sig )
  318. {
  319. exit(1); /* this will call the atexit functions */
  320. }
  321. static void set_everything(int x)
  322. {
  323. do_header = x;
  324. do_typelib = x;
  325. do_proxies = x;
  326. do_client = x;
  327. do_server = x;
  328. do_regscript = x;
  329. do_idfile = x;
  330. do_dlldata = x;
  331. }
  332. void start_cplusplus_guard(FILE *fp)
  333. {
  334. fprintf(fp, "#ifdef __cplusplus\n");
  335. fprintf(fp, "extern \"C\" {\n");
  336. fprintf(fp, "#endif\n\n");
  337. }
  338. void end_cplusplus_guard(FILE *fp)
  339. {
  340. fprintf(fp, "#ifdef __cplusplus\n");
  341. fprintf(fp, "}\n");
  342. fprintf(fp, "#endif\n\n");
  343. }
  344. typedef struct
  345. {
  346. char *filename;
  347. struct list link;
  348. } filename_node_t;
  349. static void add_filename_node(struct list *list, const char *name)
  350. {
  351. filename_node_t *node = xmalloc(sizeof *node);
  352. node->filename = dup_basename( name, ".idl" );
  353. list_add_tail(list, &node->link);
  354. }
  355. static void free_filename_nodes(struct list *list)
  356. {
  357. filename_node_t *node, *next;
  358. LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
  359. list_remove(&node->link);
  360. free(node->filename);
  361. free(node);
  362. }
  363. }
  364. static void write_dlldata_list(struct list *filenames, int define_proxy_delegation)
  365. {
  366. FILE *dlldata;
  367. filename_node_t *node;
  368. dlldata = fopen(dlldata_name, "w");
  369. if (!dlldata)
  370. error("couldn't open %s: %s\n", dlldata_name, strerror(errno));
  371. fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
  372. fprintf(dlldata, "- Do not edit ***/\n\n");
  373. if (define_proxy_delegation)
  374. fprintf(dlldata, "#define PROXY_DELEGATION\n");
  375. fprintf(dlldata, "#include <objbase.h>\n");
  376. fprintf(dlldata, "#include <rpcproxy.h>\n\n");
  377. start_cplusplus_guard(dlldata);
  378. LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
  379. fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);
  380. fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
  381. fprintf(dlldata, "/* Start of list */\n");
  382. LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
  383. fprintf(dlldata, " REFERENCE_PROXY_FILE(%s),\n", node->filename);
  384. fprintf(dlldata, "/* End of list */\n");
  385. fprintf(dlldata, "PROXYFILE_LIST_END\n\n");
  386. fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
  387. end_cplusplus_guard(dlldata);
  388. fclose(dlldata);
  389. }
  390. static char *eat_space(char *s)
  391. {
  392. while (isspace((unsigned char) *s))
  393. ++s;
  394. return s;
  395. }
  396. void write_dlldata(const statement_list_t *stmts)
  397. {
  398. struct list filenames = LIST_INIT(filenames);
  399. int define_proxy_delegation = 0;
  400. filename_node_t *node;
  401. FILE *dlldata;
  402. if (!do_dlldata || !need_proxy_file(stmts))
  403. return;
  404. define_proxy_delegation = need_proxy_delegation(stmts);
  405. dlldata = fopen(dlldata_name, "r");
  406. if (dlldata) {
  407. static const char marker[] = "REFERENCE_PROXY_FILE";
  408. static const char delegation_define[] = "#define PROXY_DELEGATION";
  409. char *line = NULL;
  410. size_t len = 0;
  411. while (widl_getline(&line, &len, dlldata)) {
  412. char *start, *end;
  413. start = eat_space(line);
  414. if (strncmp(start, marker, sizeof marker - 1) == 0) {
  415. start = eat_space(start + sizeof marker - 1);
  416. if (*start != '(')
  417. continue;
  418. end = start = eat_space(start + 1);
  419. while (*end && *end != ')')
  420. ++end;
  421. if (*end != ')')
  422. continue;
  423. while (isspace((unsigned char) end[-1]))
  424. --end;
  425. *end = '\0';
  426. if (start < end)
  427. add_filename_node(&filenames, start);
  428. }else if (!define_proxy_delegation && strncmp(start, delegation_define, sizeof(delegation_define)-1)) {
  429. define_proxy_delegation = 1;
  430. }
  431. }
  432. if (ferror(dlldata))
  433. error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));
  434. free(line);
  435. fclose(dlldata);
  436. }
  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);
  441. return;
  442. }
  443. add_filename_node(&filenames, proxy_token);
  444. write_dlldata_list(&filenames, define_proxy_delegation);
  445. free_filename_nodes(&filenames);
  446. }
  447. static void write_id_guid(FILE *f, const char *type, const char *guid_prefix, const char *name, const UUID *uuid)
  448. {
  449. if (!uuid) return;
  450. fprintf(f, "MIDL_DEFINE_GUID(%s, %s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
  451. "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
  452. type, guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
  453. uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
  454. uuid->Data4[6], uuid->Data4[7]);
  455. }
  456. static void write_id_data_stmts(const statement_list_t *stmts)
  457. {
  458. const statement_t *stmt;
  459. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  460. {
  461. if (stmt->type == STMT_TYPE)
  462. {
  463. const type_t *type = stmt->u.type;
  464. if (type_get_type(type) == TYPE_INTERFACE)
  465. {
  466. const UUID *uuid;
  467. if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
  468. continue;
  469. uuid = get_attrp(type->attrs, ATTR_UUID);
  470. write_id_guid(idfile, "IID", is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
  471. type->name, uuid);
  472. if (type_iface_get_async_iface(type))
  473. {
  474. uuid = get_attrp(type_iface_get_async_iface(type)->attrs, ATTR_UUID);
  475. write_id_guid(idfile, "IID", "IID", type_iface_get_async_iface(type)->name, uuid);
  476. }
  477. }
  478. else if (type_get_type(type) == TYPE_COCLASS)
  479. {
  480. const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
  481. write_id_guid(idfile, "CLSID", "CLSID", type->name, uuid);
  482. }
  483. }
  484. else if (stmt->type == STMT_LIBRARY)
  485. {
  486. const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
  487. write_id_guid(idfile, "IID", "LIBID", stmt->u.lib->name, uuid);
  488. write_id_data_stmts(stmt->u.lib->stmts);
  489. }
  490. }
  491. }
  492. void write_id_data(const statement_list_t *stmts)
  493. {
  494. if (!do_idfile) return;
  495. idfile = fopen(idfile_name, "w");
  496. if (! idfile) {
  497. error("Could not open %s for output\n", idfile_name);
  498. return;
  499. }
  500. fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
  501. fprintf(idfile, "from %s - Do not edit ***/\n\n", input_idl_name);
  502. fprintf(idfile, "#include <rpc.h>\n");
  503. fprintf(idfile, "#include <rpcndr.h>\n\n");
  504. fprintf(idfile, "#ifdef _MIDL_USE_GUIDDEF_\n\n");
  505. fprintf(idfile, "#ifndef INITGUID\n");
  506. fprintf(idfile, "#define INITGUID\n");
  507. fprintf(idfile, "#include <guiddef.h>\n");
  508. fprintf(idfile, "#undef INITGUID\n");
  509. fprintf(idfile, "#else\n");
  510. fprintf(idfile, "#include <guiddef.h>\n");
  511. fprintf(idfile, "#endif\n\n");
  512. fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
  513. fprintf(idfile, " DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)\n\n");
  514. fprintf(idfile, "#elif defined(__cplusplus)\n\n");
  515. fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
  516. fprintf(idfile, " EXTERN_C const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
  517. fprintf(idfile, "#else\n\n");
  518. fprintf(idfile, "#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \\\n");
  519. fprintf(idfile, " const type DECLSPEC_SELECTANY name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}\n\n");
  520. fprintf(idfile, "#endif\n\n");
  521. start_cplusplus_guard(idfile);
  522. write_id_data_stmts(stmts);
  523. fprintf(idfile, "\n");
  524. end_cplusplus_guard(idfile);
  525. fprintf(idfile, "#undef MIDL_DEFINE_GUID\n" );
  526. fclose(idfile);
  527. }
  528. static void init_argv0_dir( const char *argv0 )
  529. {
  530. #ifndef _WIN32
  531. char *p, *dir;
  532. #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
  533. dir = realpath( "/proc/self/exe", NULL );
  534. #elif defined (__FreeBSD__) || defined(__DragonFly__)
  535. dir = realpath( "/proc/curproc/file", NULL );
  536. #else
  537. dir = realpath( argv0, NULL );
  538. #endif
  539. if (!dir) return;
  540. if (!(p = strrchr( dir, '/' ))) return;
  541. if (p == dir) p++;
  542. *p = 0;
  543. includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
  544. free( dir );
  545. #endif
  546. }
  547. int main(int argc,char *argv[])
  548. {
  549. int i, optc;
  550. int ret = 0;
  551. int opti = 0;
  552. char *output_name = NULL;
  553. const char *sysroot = "";
  554. signal( SIGTERM, exit_on_signal );
  555. signal( SIGINT, exit_on_signal );
  556. #ifdef SIGHUP
  557. signal( SIGHUP, exit_on_signal );
  558. #endif
  559. init_argv0_dir( argv[0] );
  560. init_argv0_target( argv[0] );
  561. now = time(NULL);
  562. while((optc = getopt_long_only(argc, argv, short_options, long_options, &opti)) != EOF) {
  563. switch(optc) {
  564. case DLLDATA_OPTION:
  565. dlldata_name = xstrdup(optarg);
  566. break;
  567. case DLLDATA_ONLY_OPTION:
  568. do_everything = 0;
  569. do_dlldata = 1;
  570. break;
  571. case LOCAL_STUBS_OPTION:
  572. do_everything = 0;
  573. local_stubs_name = xstrdup(optarg);
  574. break;
  575. case NOSTDINC_OPTION:
  576. stdinc = 0;
  577. break;
  578. case OLDNAMES_OPTION:
  579. old_names = 1;
  580. break;
  581. case PREFIX_ALL_OPTION:
  582. prefix_client = xstrdup(optarg);
  583. prefix_server = xstrdup(optarg);
  584. break;
  585. case PREFIX_CLIENT_OPTION:
  586. prefix_client = xstrdup(optarg);
  587. break;
  588. case PREFIX_SERVER_OPTION:
  589. prefix_server = xstrdup(optarg);
  590. break;
  591. case PRINT_HELP:
  592. fprintf(stderr, "%s", usage);
  593. return 0;
  594. case RT_OPTION:
  595. winrt_mode = 1;
  596. break;
  597. case RT_NS_PREFIX:
  598. use_abi_namespace = 1;
  599. break;
  600. case SYSROOT_OPTION:
  601. sysroot = xstrdup(optarg);
  602. break;
  603. case WIN32_OPTION:
  604. pointer_size = 4;
  605. break;
  606. case WIN64_OPTION:
  607. pointer_size = 8;
  608. break;
  609. case WIN32_ALIGN_OPTION:
  610. win32_packing = strtol(optarg, NULL, 0);
  611. if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
  612. error("Packing must be one of 2, 4 or 8\n");
  613. break;
  614. case WIN64_ALIGN_OPTION:
  615. win64_packing = strtol(optarg, NULL, 0);
  616. if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
  617. error("Packing must be one of 2, 4 or 8\n");
  618. break;
  619. case ACF_OPTION:
  620. acf_name = xstrdup(optarg);
  621. break;
  622. case APP_CONFIG_OPTION:
  623. /* widl does not distinguish between app_mode and default mode,
  624. but we ignore this option for midl compatibility */
  625. break;
  626. case ROBUST_OPTION:
  627. /* FIXME: Support robust option */
  628. break;
  629. case 'b':
  630. set_target( optarg );
  631. break;
  632. case 'c':
  633. do_everything = 0;
  634. do_client = 1;
  635. break;
  636. case 'C':
  637. client_name = xstrdup(optarg);
  638. break;
  639. case 'd':
  640. debuglevel = strtol(optarg, NULL, 0);
  641. break;
  642. case 'D':
  643. wpp_add_cmdline_define(optarg);
  644. break;
  645. case 'E':
  646. do_everything = 0;
  647. preprocess_only = 1;
  648. break;
  649. case 'h':
  650. do_everything = 0;
  651. do_header = 1;
  652. break;
  653. case 'H':
  654. header_name = xstrdup(optarg);
  655. break;
  656. case 'I':
  657. wpp_add_include_path(optarg);
  658. break;
  659. case 'm':
  660. if (!strcmp( optarg, "32" )) pointer_size = 4;
  661. else if (!strcmp( optarg, "64" )) pointer_size = 8;
  662. break;
  663. case 'N':
  664. no_preprocess = 1;
  665. break;
  666. case 'o':
  667. output_name = xstrdup(optarg);
  668. break;
  669. case 'O':
  670. if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
  671. else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
  672. else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
  673. else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
  674. else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
  675. else error( "Invalid argument '-O%s'\n", optarg );
  676. break;
  677. case 'p':
  678. do_everything = 0;
  679. do_proxies = 1;
  680. break;
  681. case 'P':
  682. proxy_name = xstrdup(optarg);
  683. break;
  684. case 'r':
  685. do_everything = 0;
  686. do_regscript = 1;
  687. break;
  688. case 's':
  689. do_everything = 0;
  690. do_server = 1;
  691. break;
  692. case 'S':
  693. server_name = xstrdup(optarg);
  694. break;
  695. case 't':
  696. do_everything = 0;
  697. do_typelib = 1;
  698. break;
  699. case 'T':
  700. typelib_name = xstrdup(optarg);
  701. break;
  702. case 'u':
  703. do_everything = 0;
  704. do_idfile = 1;
  705. break;
  706. case 'U':
  707. idfile_name = xstrdup(optarg);
  708. break;
  709. case 'V':
  710. printf("%s", version_string);
  711. return 0;
  712. case 'W':
  713. pedantic = 1;
  714. break;
  715. default:
  716. fprintf(stderr, "%s", usage);
  717. return 1;
  718. }
  719. }
  720. if (stdinc)
  721. {
  722. static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
  723. if (includedir)
  724. {
  725. wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
  726. wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
  727. }
  728. for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
  729. {
  730. if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
  731. wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
  732. wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
  733. }
  734. }
  735. switch (target_cpu)
  736. {
  737. case CPU_x86:
  738. if (pointer_size == 8) target_cpu = CPU_x86_64;
  739. else pointer_size = 4;
  740. break;
  741. case CPU_x86_64:
  742. if (pointer_size == 4) target_cpu = CPU_x86;
  743. else pointer_size = 8;
  744. break;
  745. case CPU_ARM:
  746. if (pointer_size == 8) target_cpu = CPU_ARM64;
  747. else pointer_size = 4;
  748. break;
  749. case CPU_ARM64:
  750. if (pointer_size == 4) target_cpu = CPU_ARM;
  751. pointer_size = 8;
  752. break;
  753. case CPU_POWERPC64:
  754. if (pointer_size == 4) error( "Cannot build 32-bit code for this CPU\n" );
  755. pointer_size = 8;
  756. break;
  757. default:
  758. if (pointer_size == 8) error( "Cannot build 64-bit code for this CPU\n" );
  759. pointer_size = 4;
  760. break;
  761. }
  762. /* if nothing specified, try to guess output type from the output file name */
  763. if (output_name && do_everything && !do_header && !do_typelib && !do_proxies &&
  764. !do_client && !do_server && !do_regscript && !do_idfile && !do_dlldata)
  765. {
  766. do_everything = 0;
  767. if (strendswith( output_name, ".h" )) do_header = 1;
  768. else if (strendswith( output_name, ".tlb" )) do_typelib = 1;
  769. else if (strendswith( output_name, "_p.c" )) do_proxies = 1;
  770. else if (strendswith( output_name, "_c.c" )) do_client = 1;
  771. else if (strendswith( output_name, "_s.c" )) do_server = 1;
  772. else if (strendswith( output_name, "_i.c" )) do_idfile = 1;
  773. else if (strendswith( output_name, "_r.res" )) do_regscript = 1;
  774. else if (strendswith( output_name, "_t.res" )) do_typelib = 1;
  775. else if (strendswith( output_name, "dlldata.c" )) do_dlldata = 1;
  776. else do_everything = 1;
  777. }
  778. if(do_everything) {
  779. set_everything(TRUE);
  780. }
  781. if (do_header + do_typelib + do_proxies + do_client +
  782. do_server + do_regscript + do_idfile + do_dlldata == 1 && output_name)
  783. {
  784. if (do_header && !header_name) header_name = output_name;
  785. else if (do_typelib && !typelib_name) typelib_name = output_name;
  786. else if (do_proxies && !proxy_name) proxy_name = output_name;
  787. else if (do_client && !client_name) client_name = output_name;
  788. else if (do_server && !server_name) server_name = output_name;
  789. else if (do_regscript && !regscript_name) regscript_name = output_name;
  790. else if (do_idfile && !idfile_name) idfile_name = output_name;
  791. else if (do_dlldata && !dlldata_name) dlldata_name = output_name;
  792. }
  793. if (!dlldata_name && do_dlldata)
  794. dlldata_name = xstrdup("dlldata.c");
  795. if(optind < argc) {
  796. if (do_dlldata && !do_everything) {
  797. struct list filenames = LIST_INIT(filenames);
  798. for ( ; optind < argc; ++optind)
  799. add_filename_node(&filenames, argv[optind]);
  800. write_dlldata_list(&filenames, 0 /* FIXME */ );
  801. free_filename_nodes(&filenames);
  802. return 0;
  803. }
  804. else if (optind != argc - 1) {
  805. fprintf(stderr, "%s", usage);
  806. return 1;
  807. }
  808. else
  809. input_idl_name = input_name = xstrdup(argv[optind]);
  810. }
  811. else {
  812. fprintf(stderr, "%s", usage);
  813. return 1;
  814. }
  815. if(debuglevel)
  816. {
  817. setbuf(stdout, NULL);
  818. setbuf(stderr, NULL);
  819. }
  820. parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
  821. yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
  822. wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
  823. (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
  824. (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
  825. if (!header_name) {
  826. header_name = dup_basename(input_name, ".idl");
  827. strcat(header_name, ".h");
  828. }
  829. if (!typelib_name && do_typelib) {
  830. typelib_name = dup_basename(input_name, ".idl");
  831. strcat(typelib_name, ".tlb");
  832. }
  833. if (!proxy_name && do_proxies) {
  834. proxy_name = dup_basename(input_name, ".idl");
  835. strcat(proxy_name, "_p.c");
  836. }
  837. if (!client_name && do_client) {
  838. client_name = dup_basename(input_name, ".idl");
  839. strcat(client_name, "_c.c");
  840. }
  841. if (!server_name && do_server) {
  842. server_name = dup_basename(input_name, ".idl");
  843. strcat(server_name, "_s.c");
  844. }
  845. if (!regscript_name && do_regscript) {
  846. regscript_name = dup_basename(input_name, ".idl");
  847. strcat(regscript_name, "_r.rgs");
  848. }
  849. if (!idfile_name && do_idfile) {
  850. idfile_name = dup_basename(input_name, ".idl");
  851. strcat(idfile_name, "_i.c");
  852. }
  853. if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
  854. if (do_client) client_token = dup_basename_token(client_name,"_c.c");
  855. if (do_server) server_token = dup_basename_token(server_name,"_s.c");
  856. if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
  857. add_widl_version_define();
  858. wpp_add_cmdline_define("_WIN32=1");
  859. atexit(rm_tempfile);
  860. if (!no_preprocess)
  861. {
  862. chat("Starting preprocess\n");
  863. if (!preprocess_only)
  864. {
  865. FILE *output;
  866. int fd;
  867. char *name = xmalloc( strlen(header_name) + 8 );
  868. strcpy( name, header_name );
  869. strcat( name, ".XXXXXX" );
  870. if ((fd = mkstemps( name, 0 )) == -1)
  871. error("Could not generate a temp name from %s\n", name);
  872. temp_name = name;
  873. if (!(output = fdopen(fd, "wt")))
  874. error("Could not open fd %s for writing\n", name);
  875. ret = wpp_parse( input_name, output );
  876. fclose( output );
  877. }
  878. else
  879. {
  880. ret = wpp_parse( input_name, stdout );
  881. }
  882. if(ret) exit(1);
  883. if(preprocess_only) exit(0);
  884. if(!(parser_in = fopen(temp_name, "r"))) {
  885. fprintf(stderr, "Could not open %s for input\n", temp_name);
  886. return 1;
  887. }
  888. }
  889. else {
  890. if(!(parser_in = fopen(input_name, "r"))) {
  891. fprintf(stderr, "Could not open %s for input\n", input_name);
  892. return 1;
  893. }
  894. }
  895. header_token = make_token(header_name);
  896. init_types();
  897. ret = parser_parse();
  898. fclose(parser_in);
  899. if(ret) {
  900. exit(1);
  901. }
  902. /* Everything has been done successfully, don't delete any files. */
  903. set_everything(FALSE);
  904. local_stubs_name = NULL;
  905. return 0;
  906. }
  907. static void rm_tempfile(void)
  908. {
  909. abort_import();
  910. if(temp_name)
  911. unlink(temp_name);
  912. if (do_header)
  913. unlink(header_name);
  914. if (local_stubs_name)
  915. unlink(local_stubs_name);
  916. if (do_client)
  917. unlink(client_name);
  918. if (do_server)
  919. unlink(server_name);
  920. if (do_regscript)
  921. unlink(regscript_name);
  922. if (do_idfile)
  923. unlink(idfile_name);
  924. if (do_proxies)
  925. unlink(proxy_name);
  926. if (do_typelib)
  927. unlink(typelib_name);
  928. }