main.c 22 KB

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