main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * Main function
  3. *
  4. * Copyright 1993 Robert J. Amstadt
  5. * Copyright 1995 Martin von Loewis
  6. * Copyright 1995, 1996, 1997 Alexandre Julliard
  7. * Copyright 1997 Eric Youngdale
  8. * Copyright 1999 Ulrich Weigand
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include "config.h"
  25. #include <assert.h>
  26. #include <stdio.h>
  27. #include <signal.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <ctype.h>
  32. #include "build.h"
  33. int UsePIC = 0;
  34. int nb_errors = 0;
  35. int display_warnings = 0;
  36. int kill_at = 0;
  37. int verbose = 0;
  38. int link_ext_symbols = 0;
  39. int force_pointer_size = 0;
  40. int unwind_tables = 0;
  41. int use_msvcrt = 0;
  42. int unix_lib = 0;
  43. int safe_seh = 0;
  44. int prefer_native = 0;
  45. int data_only = 0;
  46. struct target target = { 0 };
  47. char *target_alias = NULL;
  48. char *input_file_name = NULL;
  49. char *spec_file_name = NULL;
  50. FILE *output_file = NULL;
  51. const char *output_file_name = NULL;
  52. static int save_temps;
  53. static int fake_module;
  54. static DLLSPEC *main_spec;
  55. static const struct strarray empty_strarray;
  56. struct strarray lib_path = { 0 };
  57. struct strarray tools_path = { 0 };
  58. struct strarray as_command = { 0 };
  59. struct strarray cc_command = { 0 };
  60. struct strarray ld_command = { 0 };
  61. struct strarray nm_command = { 0 };
  62. char *cpu_option = NULL;
  63. char *fpu_option = NULL;
  64. char *arch_option = NULL;
  65. #ifdef __SOFTFP__
  66. const char *float_abi_option = "soft";
  67. #else
  68. const char *float_abi_option = "softfp";
  69. #endif
  70. #ifdef __thumb__
  71. int thumb_mode = 1;
  72. #else
  73. int thumb_mode = 0;
  74. #endif
  75. static struct strarray res_files;
  76. /* execution mode */
  77. enum exec_mode_values
  78. {
  79. MODE_NONE,
  80. MODE_DLL,
  81. MODE_EXE,
  82. MODE_DEF,
  83. MODE_IMPLIB,
  84. MODE_STATICLIB,
  85. MODE_BUILTIN,
  86. MODE_FIXUP_CTORS,
  87. MODE_RESOURCES
  88. };
  89. static enum exec_mode_values exec_mode = MODE_NONE;
  90. /* set the dll file name from the input file name */
  91. static void set_dll_file_name( const char *name, DLLSPEC *spec )
  92. {
  93. char *p;
  94. if (spec->file_name) return;
  95. name = get_basename( name );
  96. spec->file_name = xmalloc( strlen(name) + 5 );
  97. strcpy( spec->file_name, name );
  98. if ((p = strrchr( spec->file_name, '.' )))
  99. {
  100. if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
  101. }
  102. }
  103. /* set the dll name from the file name */
  104. static void init_dll_name( DLLSPEC *spec )
  105. {
  106. if (!spec->file_name && output_file_name)
  107. {
  108. char *p;
  109. spec->file_name = xstrdup( output_file_name );
  110. if ((p = strrchr( spec->file_name, '.' ))) *p = 0;
  111. }
  112. if (!spec->dll_name && spec->file_name) /* set default name from file name */
  113. {
  114. char *p;
  115. spec->dll_name = xstrdup( spec->file_name );
  116. if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
  117. }
  118. if (spec->dll_name) spec->c_name = make_c_identifier( spec->dll_name );
  119. }
  120. /* set the dll subsystem */
  121. static void set_subsystem( const char *subsystem, DLLSPEC *spec )
  122. {
  123. char *major, *minor, *str = xstrdup( subsystem );
  124. if ((major = strchr( str, ':' ))) *major++ = 0;
  125. if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
  126. else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
  127. else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
  128. else if (!strcmp( str, "wince" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CE_GUI;
  129. else if (!strcmp( str, "win16" )) spec->type = SPEC_WIN16;
  130. else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
  131. if (major)
  132. {
  133. if ((minor = strchr( major, '.' )))
  134. {
  135. *minor++ = 0;
  136. spec->subsystem_minor = atoi( minor );
  137. }
  138. spec->subsystem_major = atoi( major );
  139. }
  140. free( str );
  141. }
  142. /* set the syscall table id */
  143. static void set_syscall_table( const char *id, DLLSPEC *spec )
  144. {
  145. int val = atoi( id );
  146. if (val < 0 || val > 3) fatal_error( "Invalid syscall table id '%s', must be 0-3\n", id );
  147. spec->syscall_table = val;
  148. }
  149. /* set the target CPU and platform */
  150. static void set_target( const char *name )
  151. {
  152. target_alias = xstrdup( name );
  153. if (!parse_target( name, &target )) fatal_error( "Unrecognized target '%s'\n", name );
  154. if (target.cpu == CPU_ARM && is_pe()) thumb_mode = 1;
  155. }
  156. /* cleanup on program exit */
  157. static void cleanup(void)
  158. {
  159. if (output_file_name) unlink( output_file_name );
  160. }
  161. /* clean things up when aborting on a signal */
  162. static void exit_on_signal( int sig )
  163. {
  164. exit(1); /* this will call atexit functions */
  165. }
  166. /*******************************************************************
  167. * command-line option handling
  168. */
  169. static const char usage_str[] =
  170. "Usage: winebuild [OPTIONS] [FILES]\n\n"
  171. "Options:\n"
  172. " --as-cmd=AS Command to use for assembling (default: as)\n"
  173. " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
  174. " -B PREFIX Look for build tools in the PREFIX directory\n"
  175. " --cc-cmd=CC C compiler to use for assembling (default: fall back to --as-cmd)\n"
  176. " --data-only Generate a data-only dll (i.e. without any executable code)\n"
  177. " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
  178. " -D SYM Ignored for C flags compatibility\n"
  179. " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
  180. " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
  181. " --external-symbols Allow linking to external symbols\n"
  182. " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
  183. " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
  184. " --fake-module Create a fake binary module\n"
  185. " -h, --help Display this help message\n"
  186. " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
  187. " -I DIR Ignored for C flags compatibility\n"
  188. " -k, --kill-at Kill stdcall decorations in generated .def files\n"
  189. " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
  190. " --large-address-aware Support an address space larger than 2Gb\n"
  191. " --ld-cmd=LD Command to use for linking (default: ld)\n"
  192. " -l, --library=LIB Import the specified library\n"
  193. " -L, --library-path=DIR Look for imports libraries in DIR\n"
  194. " -m16, -m32, -m64 Force building 16-bit, 32-bit resp. 64-bit code\n"
  195. " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
  196. " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
  197. " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
  198. " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
  199. " -o, --output=NAME Set the output file name (default: stdout)\n"
  200. " --prefer-native Set the flag to prefer loading native at run time\n"
  201. " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
  202. " --safeseh Mark object files as SEH compatible\n"
  203. " --save-temps Do not delete the generated intermediate files\n"
  204. " --subsystem=SUBSYS Set the subsystem (one of native, windows, console, wince)\n"
  205. " --syscall-table=ID Set the syscall table id (between 0 and 3)\n"
  206. " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
  207. " -v, --verbose Display the programs invoked\n"
  208. " --version Print the version and exit\n"
  209. " -w, --warnings Turn on warnings\n"
  210. "\nMode options:\n"
  211. " --dll Build a library from a .spec file and object files\n"
  212. " --def Build a .def file from a .spec file\n"
  213. " --exe Build an executable from object files\n"
  214. " --implib Build an import library\n"
  215. " --staticlib Build a static library\n"
  216. " --resources Build a .o or .res file for the resource files\n\n"
  217. " --builtin Mark a library as a Wine builtin\n"
  218. " --fixup-ctors Fixup the constructors data after the module has been built\n"
  219. "The mode options are mutually exclusive; you must specify one and only one.\n\n";
  220. enum long_options_values
  221. {
  222. LONG_OPT_DLL = 1,
  223. LONG_OPT_DEF,
  224. LONG_OPT_EXE,
  225. LONG_OPT_IMPLIB,
  226. LONG_OPT_BUILTIN,
  227. LONG_OPT_ASCMD,
  228. LONG_OPT_CCCMD,
  229. LONG_OPT_DATA_ONLY,
  230. LONG_OPT_EXTERNAL_SYMS,
  231. LONG_OPT_FAKE_MODULE,
  232. LONG_OPT_FIXUP_CTORS,
  233. LONG_OPT_LARGE_ADDRESS_AWARE,
  234. LONG_OPT_LDCMD,
  235. LONG_OPT_NMCMD,
  236. LONG_OPT_NXCOMPAT,
  237. LONG_OPT_PREFER_NATIVE,
  238. LONG_OPT_RESOURCES,
  239. LONG_OPT_SAFE_SEH,
  240. LONG_OPT_SAVE_TEMPS,
  241. LONG_OPT_STATICLIB,
  242. LONG_OPT_SUBSYSTEM,
  243. LONG_OPT_SYSCALL_TABLE,
  244. LONG_OPT_VERSION
  245. };
  246. static const char short_options[] = "B:C:D:E:F:H:I:K:L:M:N:b:d:e:f:hkl:m:o:r:u:vw";
  247. static const struct long_option long_options[] =
  248. {
  249. /* mode options */
  250. { "dll", 0, LONG_OPT_DLL },
  251. { "def", 0, LONG_OPT_DEF },
  252. { "exe", 0, LONG_OPT_EXE },
  253. { "implib", 0, LONG_OPT_IMPLIB },
  254. { "staticlib", 0, LONG_OPT_STATICLIB },
  255. { "builtin", 0, LONG_OPT_BUILTIN },
  256. { "resources", 0, LONG_OPT_RESOURCES },
  257. { "fixup-ctors", 0, LONG_OPT_FIXUP_CTORS },
  258. /* other long options */
  259. { "as-cmd", 1, LONG_OPT_ASCMD },
  260. { "cc-cmd", 1, LONG_OPT_CCCMD },
  261. { "data-only", 0, LONG_OPT_DATA_ONLY },
  262. { "external-symbols", 0, LONG_OPT_EXTERNAL_SYMS },
  263. { "fake-module", 0, LONG_OPT_FAKE_MODULE },
  264. { "large-address-aware", 0, LONG_OPT_LARGE_ADDRESS_AWARE },
  265. { "ld-cmd", 1, LONG_OPT_LDCMD },
  266. { "nm-cmd", 1, LONG_OPT_NMCMD },
  267. { "nxcompat", 1, LONG_OPT_NXCOMPAT },
  268. { "prefer-native", 0, LONG_OPT_PREFER_NATIVE },
  269. { "safeseh", 0, LONG_OPT_SAFE_SEH },
  270. { "save-temps", 0, LONG_OPT_SAVE_TEMPS },
  271. { "subsystem", 1, LONG_OPT_SUBSYSTEM },
  272. { "syscall-table", 1, LONG_OPT_SYSCALL_TABLE },
  273. { "version", 0, LONG_OPT_VERSION },
  274. /* aliases for short options */
  275. { "target", 1, 'b' },
  276. { "delay-lib", 1, 'd' },
  277. { "export", 1, 'E' },
  278. { "entry", 1, 'e' },
  279. { "filename", 1, 'F' },
  280. { "help", 0, 'h' },
  281. { "heap", 1, 'H' },
  282. { "kill-at", 0, 'k' },
  283. { "library", 1, 'l' },
  284. { "library-path", 1, 'L' },
  285. { "main-module", 1, 'M' },
  286. { "dll-name", 1, 'N' },
  287. { "output", 1, 'o' },
  288. { "res", 1, 'r' },
  289. { "undefined", 1, 'u' },
  290. { "verbose", 0, 'v' },
  291. { "warnings", 0, 'w' },
  292. { NULL }
  293. };
  294. static void usage( int exit_code )
  295. {
  296. fprintf( stderr, "%s", usage_str );
  297. exit( exit_code );
  298. }
  299. static void set_exec_mode( enum exec_mode_values mode )
  300. {
  301. if (exec_mode != MODE_NONE) usage(1);
  302. exec_mode = mode;
  303. }
  304. /* get the default entry point for a given spec file */
  305. static const char *get_default_entry_point( const DLLSPEC *spec )
  306. {
  307. if (spec->characteristics & IMAGE_FILE_DLL) return "DllMain";
  308. if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "DriverEntry";
  309. if (spec->type == SPEC_WIN16)
  310. {
  311. add_spec_extra_ld_symbol("WinMain16");
  312. return "__wine_spec_exe16_entry";
  313. }
  314. else if (spec->unicode_app)
  315. {
  316. /* __wine_spec_exe_wentry always calls wmain */
  317. add_spec_extra_ld_symbol("wmain");
  318. if (spec->subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
  319. add_spec_extra_ld_symbol("wWinMain");
  320. return "__wine_spec_exe_wentry";
  321. }
  322. else
  323. {
  324. /* __wine_spec_exe_entry always calls main */
  325. add_spec_extra_ld_symbol("main");
  326. if (spec->subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
  327. add_spec_extra_ld_symbol("WinMain");
  328. return "__wine_spec_exe_entry";
  329. }
  330. }
  331. static void option_callback( int optc, char *optarg )
  332. {
  333. char *p;
  334. switch (optc)
  335. {
  336. case 'B':
  337. strarray_add( &tools_path, xstrdup( optarg ));
  338. break;
  339. case 'D':
  340. /* ignored */
  341. break;
  342. case 'E':
  343. spec_file_name = xstrdup( optarg );
  344. set_dll_file_name( optarg, main_spec );
  345. break;
  346. case 'F':
  347. main_spec->file_name = xstrdup( optarg );
  348. break;
  349. case 'H':
  350. if (!isdigit(optarg[0]))
  351. fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
  352. main_spec->heap_size = atoi(optarg);
  353. if (main_spec->heap_size > 65535)
  354. fatal_error( "Invalid heap size %d, maximum is 65535\n", main_spec->heap_size );
  355. break;
  356. case 'I':
  357. /* ignored */
  358. break;
  359. case 'K':
  360. /* ignored, because cc generates correct code. */
  361. break;
  362. case 'L':
  363. strarray_add( &lib_path, xstrdup( optarg ));
  364. break;
  365. case 'm':
  366. if (!strcmp( optarg, "16" )) main_spec->type = SPEC_WIN16;
  367. else if (!strcmp( optarg, "32" )) force_pointer_size = 4;
  368. else if (!strcmp( optarg, "64" )) force_pointer_size = 8;
  369. else if (!strcmp( optarg, "arm" )) thumb_mode = 0;
  370. else if (!strcmp( optarg, "thumb" )) thumb_mode = 1;
  371. else if (!strcmp( optarg, "no-cygwin" )) use_msvcrt = 1;
  372. else if (!strcmp( optarg, "unix" )) unix_lib = 1;
  373. else if (!strcmp( optarg, "unicode" )) main_spec->unicode_app = 1;
  374. else if (!strncmp( optarg, "cpu=", 4 )) cpu_option = xstrdup( optarg + 4 );
  375. else if (!strncmp( optarg, "fpu=", 4 )) fpu_option = xstrdup( optarg + 4 );
  376. else if (!strncmp( optarg, "arch=", 5 )) arch_option = xstrdup( optarg + 5 );
  377. else if (!strncmp( optarg, "float-abi=", 10 )) float_abi_option = xstrdup( optarg + 10 );
  378. else fatal_error( "Unknown -m option '%s'\n", optarg );
  379. break;
  380. case 'M':
  381. main_spec->main_module = xstrdup( optarg );
  382. break;
  383. case 'N':
  384. main_spec->dll_name = xstrdup( optarg );
  385. break;
  386. case 'b':
  387. set_target( optarg );
  388. break;
  389. case 'd':
  390. add_delayed_import( optarg );
  391. break;
  392. case 'e':
  393. main_spec->init_func = xstrdup( optarg );
  394. if ((p = strchr( main_spec->init_func, '@' ))) *p = 0; /* kill stdcall decoration */
  395. break;
  396. case 'f':
  397. if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
  398. else if (!strcmp( optarg, "asynchronous-unwind-tables")) unwind_tables = 1;
  399. else if (!strcmp( optarg, "no-asynchronous-unwind-tables")) unwind_tables = 0;
  400. /* ignore all other flags */
  401. break;
  402. case 'h':
  403. usage(0);
  404. break;
  405. case 'k':
  406. kill_at = 1;
  407. break;
  408. case 'l':
  409. add_import_dll( optarg, NULL );
  410. break;
  411. case 'o':
  412. if (unlink( optarg ) == -1 && errno != ENOENT)
  413. fatal_error( "Unable to create output file '%s'\n", optarg );
  414. output_file_name = xstrdup( optarg );
  415. break;
  416. case 'r':
  417. strarray_add( &res_files, xstrdup( optarg ));
  418. break;
  419. case 'u':
  420. add_extra_ld_symbol( optarg );
  421. break;
  422. case 'v':
  423. verbose++;
  424. break;
  425. case 'w':
  426. display_warnings = 1;
  427. break;
  428. case LONG_OPT_DLL:
  429. set_exec_mode( MODE_DLL );
  430. break;
  431. case LONG_OPT_DEF:
  432. set_exec_mode( MODE_DEF );
  433. break;
  434. case LONG_OPT_EXE:
  435. set_exec_mode( MODE_EXE );
  436. if (!main_spec->subsystem) main_spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
  437. break;
  438. case LONG_OPT_IMPLIB:
  439. set_exec_mode( MODE_IMPLIB );
  440. break;
  441. case LONG_OPT_STATICLIB:
  442. set_exec_mode( MODE_STATICLIB );
  443. break;
  444. case LONG_OPT_BUILTIN:
  445. set_exec_mode( MODE_BUILTIN );
  446. break;
  447. case LONG_OPT_FIXUP_CTORS:
  448. set_exec_mode( MODE_FIXUP_CTORS );
  449. break;
  450. case LONG_OPT_ASCMD:
  451. as_command = strarray_fromstring( optarg, " " );
  452. break;
  453. case LONG_OPT_CCCMD:
  454. cc_command = strarray_fromstring( optarg, " " );
  455. break;
  456. case LONG_OPT_DATA_ONLY:
  457. data_only = 1;
  458. break;
  459. case LONG_OPT_FAKE_MODULE:
  460. fake_module = 1;
  461. break;
  462. case LONG_OPT_EXTERNAL_SYMS:
  463. link_ext_symbols = 1;
  464. break;
  465. case LONG_OPT_LARGE_ADDRESS_AWARE:
  466. main_spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  467. break;
  468. case LONG_OPT_LDCMD:
  469. ld_command = strarray_fromstring( optarg, " " );
  470. break;
  471. case LONG_OPT_NMCMD:
  472. nm_command = strarray_fromstring( optarg, " " );
  473. break;
  474. case LONG_OPT_NXCOMPAT:
  475. if (optarg[0] == 'n' || optarg[0] == 'N')
  476. main_spec->dll_characteristics &= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
  477. break;
  478. case LONG_OPT_SAFE_SEH:
  479. safe_seh = 1;
  480. break;
  481. case LONG_OPT_PREFER_NATIVE:
  482. prefer_native = 1;
  483. main_spec->dll_characteristics |= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE;
  484. break;
  485. case LONG_OPT_RESOURCES:
  486. set_exec_mode( MODE_RESOURCES );
  487. break;
  488. case LONG_OPT_SAVE_TEMPS:
  489. save_temps = 1;
  490. break;
  491. case LONG_OPT_SUBSYSTEM:
  492. set_subsystem( optarg, main_spec );
  493. break;
  494. case LONG_OPT_SYSCALL_TABLE:
  495. set_syscall_table( optarg, main_spec );
  496. break;
  497. case LONG_OPT_VERSION:
  498. printf( "winebuild version " PACKAGE_VERSION "\n" );
  499. exit(0);
  500. case '?':
  501. fprintf( stderr, "winebuild: %s\n\n", optarg );
  502. usage(1);
  503. break;
  504. }
  505. }
  506. /* load all specified resource files */
  507. static struct strarray load_resources( struct strarray files, DLLSPEC *spec )
  508. {
  509. struct strarray ret = empty_strarray;
  510. int i;
  511. switch (spec->type)
  512. {
  513. case SPEC_WIN16:
  514. for (i = 0; i < res_files.count; i++) load_res16_file( res_files.str[i], spec );
  515. return files;
  516. case SPEC_WIN32:
  517. for (i = 0; i < res_files.count; i++)
  518. {
  519. if (!load_res32_file( res_files.str[i], spec ))
  520. fatal_error( "%s is not a valid Win32 resource file\n", res_files.str[i] );
  521. }
  522. /* load any resource file found in the remaining arguments */
  523. for (i = 0; i < files.count; i++)
  524. {
  525. if (!load_res32_file( files.str[i], spec ))
  526. strarray_add( &ret, files.str[i] ); /* not a resource file, keep it in the list */
  527. }
  528. break;
  529. }
  530. return ret;
  531. }
  532. /* add input files that look like import libs to the import list */
  533. static struct strarray load_import_libs( struct strarray files )
  534. {
  535. struct strarray ret = empty_strarray;
  536. int i;
  537. for (i = 0; i < files.count; i++)
  538. {
  539. if (strendswith( files.str[i], ".def" ))
  540. add_import_dll( NULL, files.str[i] );
  541. else
  542. strarray_add( &ret, files.str[i] ); /* not an import dll, keep it in the list */
  543. }
  544. return ret;
  545. }
  546. static int parse_input_file( DLLSPEC *spec )
  547. {
  548. FILE *input_file = open_input_file( NULL, spec_file_name );
  549. int result;
  550. spec->src_name = xstrdup( input_file_name );
  551. if (strendswith( spec_file_name, ".def" ))
  552. result = parse_def_file( input_file, spec );
  553. else
  554. result = parse_spec_file( input_file, spec );
  555. close_input_file( input_file );
  556. return result;
  557. }
  558. /*******************************************************************
  559. * main
  560. */
  561. int main(int argc, char **argv)
  562. {
  563. struct strarray files;
  564. DLLSPEC *spec = main_spec = alloc_dll_spec();
  565. #ifdef SIGHUP
  566. signal( SIGHUP, exit_on_signal );
  567. #endif
  568. signal( SIGTERM, exit_on_signal );
  569. signal( SIGINT, exit_on_signal );
  570. target = init_argv0_target( argv[0] );
  571. if (target.platform == PLATFORM_CYGWIN) target.platform = PLATFORM_MINGW;
  572. files = parse_options( argc, argv, short_options, long_options, 0, option_callback );
  573. atexit( cleanup ); /* make sure we remove the output file on exit */
  574. if (!save_temps) atexit( cleanup_tmp_files );
  575. if (spec->file_name && !strchr( spec->file_name, '.' ))
  576. strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
  577. init_dll_name( spec );
  578. if (force_pointer_size) set_target_ptr_size( &target, force_pointer_size );
  579. switch(exec_mode)
  580. {
  581. case MODE_DLL:
  582. if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE)
  583. spec->characteristics |= IMAGE_FILE_DLL;
  584. /* fall through */
  585. case MODE_EXE:
  586. files = load_resources( files, spec );
  587. if (spec_file_name && !parse_input_file( spec )) break;
  588. if (!spec->init_func && !unix_lib) spec->init_func = xstrdup( get_default_entry_point( spec ));
  589. if (fake_module)
  590. {
  591. output_fake_module( spec );
  592. break;
  593. }
  594. if (data_only)
  595. {
  596. output_data_module( spec );
  597. break;
  598. }
  599. if (!is_pe())
  600. {
  601. files = load_import_libs( files );
  602. read_undef_symbols( spec, files );
  603. resolve_imports( spec );
  604. }
  605. if (spec->type == SPEC_WIN16) output_spec16_file( spec );
  606. else output_spec32_file( spec );
  607. break;
  608. case MODE_DEF:
  609. if (files.count) fatal_error( "file argument '%s' not allowed in this mode\n", files.str[0] );
  610. if (!spec_file_name) fatal_error( "missing .spec file\n" );
  611. if (!parse_input_file( spec )) break;
  612. open_output_file();
  613. output_def_file( spec, 0 );
  614. close_output_file();
  615. break;
  616. case MODE_IMPLIB:
  617. if (!spec_file_name) fatal_error( "missing .spec file\n" );
  618. if (!parse_input_file( spec )) break;
  619. output_static_lib( spec, files );
  620. break;
  621. case MODE_STATICLIB:
  622. output_static_lib( NULL, files );
  623. break;
  624. case MODE_BUILTIN:
  625. if (!files.count) fatal_error( "missing file argument for --builtin option\n" );
  626. make_builtin_files( files );
  627. break;
  628. case MODE_FIXUP_CTORS:
  629. if (!files.count) fatal_error( "missing file argument for --fixup-ctors option\n" );
  630. fixup_constructors( files );
  631. break;
  632. case MODE_RESOURCES:
  633. files = load_resources( files, spec );
  634. output_res_o_file( spec );
  635. break;
  636. default:
  637. usage(1);
  638. break;
  639. }
  640. if (nb_errors) exit(1);
  641. output_file_name = NULL;
  642. return 0;
  643. }