winegcc.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  1. /*
  2. * MinGW wrapper: makes gcc behave like MinGW.
  3. *
  4. * Copyright 2000 Manuel Novoa III
  5. * Copyright 2000 Francois Gouget
  6. * Copyright 2002 Dimitrie O. Paun
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. *
  22. * DESCRIPTION
  23. *
  24. * all options for gcc start with '-' and are for the most part
  25. * single options (no parameters as separate argument).
  26. * There are of course exceptions to this rule, so here is an
  27. * exhaustive list of options that do take parameters (potentially)
  28. * as a separate argument:
  29. *
  30. * Compiler:
  31. * -x language
  32. * -o filename
  33. * -aux-info filename
  34. *
  35. * Preprocessor:
  36. * -D name
  37. * -U name
  38. * -I dir
  39. * -MF file
  40. * -MT target
  41. * -MQ target
  42. * (all -i.* arg)
  43. * -include file
  44. * -imacros file
  45. * -idirafter dir
  46. * -iwithprefix dir
  47. * -iwithprefixbefore dir
  48. * -isystem dir
  49. * -A predicate=answer
  50. *
  51. * Linking:
  52. * -l library
  53. * -Xlinker option
  54. * -u symbol
  55. *
  56. * Misc:
  57. * -b machine
  58. * -V version
  59. * -G num (see NOTES below)
  60. *
  61. * NOTES
  62. * There is -G option for compatibility with System V that
  63. * takes no parameters. This makes "-G num" parsing ambiguous.
  64. * This option is synonymous to -shared, and as such we will
  65. * not support it for now.
  66. *
  67. * Special interest options
  68. *
  69. * Assembler Option
  70. * -Wa,option
  71. *
  72. * Linker Options
  73. * object-file-name -llibrary -nostartfiles -nodefaultlibs
  74. * -nostdlib -s -static -static-libgcc -static-libstdc++
  75. * -shared -shared-libgcc -symbolic -Wl,option
  76. * -Xlinker option -u symbol --image-base -fuse-ld
  77. *
  78. * Directory Options
  79. * -Bprefix -Idir -I- -Ldir -specs=file
  80. *
  81. * Target Options
  82. * -b machine -V version
  83. *
  84. * Please note that the Target Options are relevant to everything:
  85. * compiler, linker, assembler, preprocessor.
  86. *
  87. */
  88. #include "config.h"
  89. #include <assert.h>
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <signal.h>
  93. #include <stdarg.h>
  94. #include <string.h>
  95. #include <errno.h>
  96. #include <ctype.h>
  97. #include <limits.h>
  98. #include <sys/types.h>
  99. #ifdef HAVE_SYS_SYSCTL_H
  100. # include <sys/sysctl.h>
  101. #endif
  102. #include "utils.h"
  103. static const char* app_loader_template =
  104. "#!/bin/sh\n"
  105. "\n"
  106. "appname=\"%s\"\n"
  107. "# determine the application directory\n"
  108. "appdir=''\n"
  109. "case \"$0\" in\n"
  110. " */*)\n"
  111. " # $0 contains a path, use it\n"
  112. " appdir=`dirname \"$0\"`\n"
  113. " ;;\n"
  114. " *)\n"
  115. " # no directory in $0, search in PATH\n"
  116. " saved_ifs=$IFS\n"
  117. " IFS=:\n"
  118. " for d in $PATH\n"
  119. " do\n"
  120. " IFS=$saved_ifs\n"
  121. " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
  122. " done\n"
  123. " ;;\n"
  124. "esac\n"
  125. "\n"
  126. "# figure out the full app path\n"
  127. "if [ -n \"$appdir\" ]; then\n"
  128. " apppath=\"$appdir/$appname\"\n"
  129. " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
  130. " export WINEDLLPATH\n"
  131. "else\n"
  132. " apppath=\"$appname\"\n"
  133. "fi\n"
  134. "\n"
  135. "# determine the WINELOADER\n"
  136. "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
  137. "\n"
  138. "# and try to start the app\n"
  139. "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
  140. ;
  141. static const char *output_file_name;
  142. static const char *output_debug_file;
  143. static const char *output_implib;
  144. static int keep_generated = 0;
  145. static struct strarray tmp_files;
  146. #ifdef HAVE_SIGSET_T
  147. static sigset_t signal_mask;
  148. #endif
  149. static const char *bindir;
  150. static const char *libdir;
  151. static const char *includedir;
  152. enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };
  153. struct options
  154. {
  155. enum processor processor;
  156. struct target target;
  157. const char *target_alias;
  158. const char *version;
  159. int shared;
  160. int use_msvcrt;
  161. int nostdinc;
  162. int nostdlib;
  163. int nostartfiles;
  164. int nodefaultlibs;
  165. int noshortwchar;
  166. int unix_lib;
  167. int gui_app;
  168. int unicode_app;
  169. int win16_app;
  170. int compile_only;
  171. int force_pointer_size;
  172. int large_address_aware;
  173. int wine_builtin;
  174. int fake_module;
  175. int unwind_tables;
  176. int strip;
  177. int pic;
  178. const char* wine_objdir;
  179. const char* winebuild;
  180. const char* output_name;
  181. const char* image_base;
  182. const char* section_align;
  183. const char* file_align;
  184. const char* sysroot;
  185. const char* isysroot;
  186. const char* lib_suffix;
  187. const char* subsystem;
  188. const char* entry_point;
  189. const char* prelink;
  190. const char* debug_file;
  191. const char* out_implib;
  192. struct strarray prefix;
  193. struct strarray lib_dirs;
  194. struct strarray args;
  195. struct strarray linker_args;
  196. struct strarray compiler_args;
  197. struct strarray winebuild_args;
  198. struct strarray files;
  199. struct strarray delayimports;
  200. };
  201. static void cleanup_output_files(void)
  202. {
  203. if (output_file_name) unlink( output_file_name );
  204. if (output_debug_file) unlink( output_debug_file );
  205. if (output_implib) unlink( output_implib );
  206. }
  207. static void clean_temp_files(void)
  208. {
  209. unsigned int i;
  210. if (keep_generated) return;
  211. for (i = 0; i < tmp_files.count; i++)
  212. unlink(tmp_files.str[i]);
  213. }
  214. /* clean things up when aborting on a signal */
  215. static void exit_on_signal( int sig )
  216. {
  217. exit(1); /* this will call the atexit functions */
  218. }
  219. static char* get_temp_file(const char* prefix, const char* suffix)
  220. {
  221. int fd;
  222. char *tmp;
  223. #ifdef HAVE_SIGPROCMASK
  224. sigset_t old_set;
  225. /* block signals while manipulating the temp files list */
  226. sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
  227. #endif
  228. fd = make_temp_file( prefix, suffix, &tmp );
  229. close( fd );
  230. strarray_add(&tmp_files, tmp);
  231. #ifdef HAVE_SIGPROCMASK
  232. sigprocmask( SIG_SETMASK, &old_set, NULL );
  233. #endif
  234. return tmp;
  235. }
  236. static int is_pe_target( const struct options *opts )
  237. {
  238. switch (opts->target.platform)
  239. {
  240. case PLATFORM_MINGW:
  241. case PLATFORM_CYGWIN:
  242. case PLATFORM_WINDOWS:
  243. return 1;
  244. default:
  245. return 0;
  246. }
  247. }
  248. enum tool
  249. {
  250. TOOL_CC,
  251. TOOL_CXX,
  252. TOOL_CPP,
  253. TOOL_LD,
  254. TOOL_OBJCOPY,
  255. };
  256. static const struct
  257. {
  258. const char *base;
  259. const char *llvm_base;
  260. const char *deflt;
  261. } tool_names[] =
  262. {
  263. { "gcc", "clang --driver-mode=gcc", CC },
  264. { "g++", "clang --driver-mode=g++", CXX },
  265. { "cpp", "clang --driver-mode=cpp", CPP },
  266. { "ld", "ld.lld", LD },
  267. { "objcopy", "llvm-objcopy" },
  268. };
  269. static struct strarray build_tool_name( struct options *opts, enum tool tool )
  270. {
  271. const char *base = tool_names[tool].base;
  272. const char *llvm_base = tool_names[tool].llvm_base;
  273. const char *deflt = tool_names[tool].deflt;
  274. const char *path;
  275. struct strarray ret = empty_strarray;
  276. char* str;
  277. if (opts->target_alias && opts->version)
  278. {
  279. str = strmake("%s-%s-%s", opts->target_alias, base, opts->version);
  280. }
  281. else if (opts->target_alias)
  282. {
  283. str = strmake("%s-%s", opts->target_alias, base);
  284. }
  285. else if (opts->version)
  286. {
  287. str = strmake("%s-%s", base, opts->version);
  288. }
  289. else
  290. str = xstrdup((deflt && *deflt) ? deflt : base);
  291. if ((path = find_binary( opts->prefix, str ))) return strarray_fromstring( path, " " );
  292. if (!opts->version) str = xstrdup( llvm_base );
  293. else str = strmake( "%s-%s", llvm_base, opts->version );
  294. path = find_binary( opts->prefix, str );
  295. if (!path)
  296. {
  297. error( "Could not find %s\n", base );
  298. return ret;
  299. }
  300. ret = strarray_fromstring( path, " " );
  301. if (!strncmp( llvm_base, "clang", 5 ))
  302. {
  303. if (opts->target_alias)
  304. {
  305. strarray_add( &ret, "-target" );
  306. strarray_add( &ret, opts->target_alias );
  307. }
  308. strarray_add( &ret, "-Wno-unused-command-line-argument" );
  309. strarray_add( &ret, "-fuse-ld=lld" );
  310. }
  311. return ret;
  312. }
  313. static struct strarray get_translator(struct options *opts)
  314. {
  315. enum tool tool;
  316. switch(opts->processor)
  317. {
  318. case proc_cpp:
  319. tool = TOOL_CPP;
  320. break;
  321. case proc_cc:
  322. case proc_as:
  323. tool = TOOL_CC;
  324. break;
  325. case proc_cxx:
  326. tool = TOOL_CXX;
  327. break;
  328. default:
  329. assert(0);
  330. }
  331. return build_tool_name( opts, tool );
  332. }
  333. static int try_link( struct strarray prefix, struct strarray link_tool, const char *cflags )
  334. {
  335. const char *in = get_temp_file( "try_link", ".c" );
  336. const char *out = get_temp_file( "try_link", ".out" );
  337. const char *err = get_temp_file( "try_link", ".err" );
  338. struct strarray link = empty_strarray;
  339. int sout = -1, serr = -1;
  340. int ret;
  341. create_file( in, 0644, "int main(void){return 1;}\n" );
  342. strarray_addall( &link, link_tool );
  343. strarray_add( &link, "-o" );
  344. strarray_add( &link, out );
  345. strarray_addall( &link, strarray_fromstring( cflags, " " ) );
  346. strarray_add( &link, in );
  347. sout = dup( fileno(stdout) );
  348. freopen( err, "w", stdout );
  349. serr = dup( fileno(stderr) );
  350. freopen( err, "w", stderr );
  351. ret = spawn( prefix, link, 1 );
  352. if (sout >= 0)
  353. {
  354. dup2( sout, fileno(stdout) );
  355. close( sout );
  356. }
  357. if (serr >= 0)
  358. {
  359. dup2( serr, fileno(stderr) );
  360. close( serr );
  361. }
  362. return ret;
  363. }
  364. static struct strarray get_link_args( struct options *opts, const char *output_name )
  365. {
  366. struct strarray link_args = get_translator( opts );
  367. struct strarray flags = empty_strarray;
  368. strarray_addall( &link_args, opts->linker_args );
  369. if (verbose > 1) strarray_add( &flags, "-v" );
  370. switch (opts->target.platform)
  371. {
  372. case PLATFORM_APPLE:
  373. strarray_add( &flags, opts->unix_lib ? "-dynamiclib" : "-bundle" );
  374. strarray_add( &flags, "-multiply_defined" );
  375. strarray_add( &flags, "suppress" );
  376. if (opts->image_base)
  377. {
  378. strarray_add( &flags, "-image_base" );
  379. strarray_add( &flags, opts->image_base );
  380. }
  381. if (opts->strip) strarray_add( &flags, "-Wl,-x" );
  382. if (opts->unix_lib)
  383. {
  384. strarray_add( &flags, "-install_name" );
  385. strarray_add( &flags, strmake( "@loader_path/%s.so", output_name ) );
  386. }
  387. strarray_addall( &link_args, flags );
  388. return link_args;
  389. case PLATFORM_SOLARIS:
  390. {
  391. char *mapfile = get_temp_file( output_name, ".map" );
  392. const char *align = opts->section_align ? opts->section_align : "0x1000";
  393. create_file( mapfile, 0644, "text = A%s;\ndata = A%s;\n", align, align );
  394. strarray_add( &flags, strmake("-Wl,-M,%s", mapfile) );
  395. strarray_add( &tmp_files, mapfile );
  396. }
  397. break;
  398. case PLATFORM_ANDROID:
  399. /* the Android loader requires a soname for all libraries */
  400. strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
  401. break;
  402. case PLATFORM_MINGW:
  403. case PLATFORM_CYGWIN:
  404. if (opts->shared || opts->win16_app)
  405. {
  406. strarray_add( &flags, "-shared" );
  407. strarray_add( &flags, "-Wl,--kill-at" );
  408. }
  409. else strarray_add( &flags, opts->gui_app ? "-mwindows" : "-mconsole" );
  410. if (opts->unicode_app) strarray_add( &flags, "-municode" );
  411. if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
  412. if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
  413. if (opts->subsystem) strarray_add( &flags, strmake("-Wl,--subsystem,%s", opts->subsystem ));
  414. strarray_add( &flags, "-Wl,--nxcompat" );
  415. if (opts->image_base) strarray_add( &flags, strmake("-Wl,--image-base,%s", opts->image_base ));
  416. if (opts->large_address_aware && opts->target.cpu == CPU_i386)
  417. strarray_add( &flags, "-Wl,--large-address-aware" );
  418. /* make sure we don't need a libgcc_s dll on Windows */
  419. if (!opts->nodefaultlibs && !opts->use_msvcrt)
  420. strarray_add( &flags, "-static-libgcc" );
  421. if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
  422. strarray_add(&link_args, strmake("-Wl,-pdb,%s", opts->debug_file));
  423. if (opts->out_implib)
  424. strarray_add(&link_args, strmake("-Wl,--out-implib,%s", opts->out_implib));
  425. if (!try_link( opts->prefix, link_args, "-Wl,--file-alignment,0x1000" ))
  426. strarray_add( &link_args, strmake( "-Wl,--file-alignment,%s",
  427. opts->file_align ? opts->file_align : "0x1000" ));
  428. else if (!try_link( opts->prefix, link_args, "-Wl,-Xlink=-filealign:0x1000" ))
  429. /* lld from llvm 10 does not support mingw style --file-alignment,
  430. * but it's possible to use msvc syntax */
  431. strarray_add( &link_args, strmake( "-Wl,-Xlink=-filealign:%s",
  432. opts->file_align ? opts->file_align : "0x1000" ));
  433. strarray_addall( &link_args, flags );
  434. return link_args;
  435. case PLATFORM_WINDOWS:
  436. if (opts->shared || opts->win16_app)
  437. {
  438. strarray_add( &flags, "-shared" );
  439. strarray_add( &flags, "-Wl,-kill-at" );
  440. }
  441. if (opts->unicode_app) strarray_add( &flags, "-municode" );
  442. if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
  443. if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
  444. if (opts->image_base) strarray_add( &flags, strmake("-Wl,-base:%s", opts->image_base ));
  445. if (opts->subsystem)
  446. strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->subsystem ));
  447. else
  448. strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->gui_app ? "windows" : "console" ));
  449. if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
  450. {
  451. strarray_add(&link_args, "-Wl,-debug");
  452. strarray_add(&link_args, strmake("-Wl,-pdb:%s", opts->debug_file));
  453. }
  454. else if (!opts->strip)
  455. strarray_add(&link_args, "-Wl,-debug:dwarf");
  456. if (opts->out_implib)
  457. strarray_add(&link_args, strmake("-Wl,-implib:%s", opts->out_implib));
  458. strarray_add( &link_args, strmake( "-Wl,-filealign:%s", opts->file_align ? opts->file_align : "0x1000" ));
  459. strarray_addall( &link_args, flags );
  460. return link_args;
  461. default:
  462. if (opts->image_base)
  463. {
  464. if (!try_link( opts->prefix, link_args, strmake("-Wl,-Ttext-segment=%s", opts->image_base)) )
  465. strarray_add( &flags, strmake("-Wl,-Ttext-segment=%s", opts->image_base) );
  466. else
  467. opts->prelink = PRELINK;
  468. }
  469. if (!try_link( opts->prefix, link_args, "-Wl,-z,max-page-size=0x1000"))
  470. strarray_add( &flags, "-Wl,-z,max-page-size=0x1000");
  471. if (opts->unix_lib) strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
  472. break;
  473. }
  474. /* generic Unix shared library flags */
  475. strarray_add( &link_args, "-shared" );
  476. strarray_add( &link_args, "-Wl,-Bsymbolic" );
  477. if (!opts->noshortwchar && opts->target.cpu == CPU_ARM)
  478. strarray_add( &flags, "-Wl,--no-wchar-size-warning" );
  479. if (!try_link( opts->prefix, link_args, "-Wl,-z,defs" ))
  480. strarray_add( &flags, "-Wl,-z,defs" );
  481. strarray_addall( &link_args, flags );
  482. return link_args;
  483. }
  484. static const char *get_multiarch_dir( struct target target )
  485. {
  486. switch (target.cpu)
  487. {
  488. case CPU_i386: return "/i386-linux-gnu";
  489. case CPU_x86_64: return "/x86_64-linux-gnu";
  490. case CPU_ARM: return "/arm-linux-gnueabi";
  491. case CPU_ARM64: return "/aarch64-linux-gnu";
  492. }
  493. assert(0);
  494. return NULL;
  495. }
  496. static char *get_lib_dir( struct options *opts )
  497. {
  498. const char *stdlibpath[] = { libdir, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
  499. const char *bit_suffix, *other_bit_suffix, *build_multiarch, *target_multiarch, *winecrt0;
  500. const char *root = opts->sysroot ? opts->sysroot : "";
  501. unsigned int i;
  502. struct stat st;
  503. size_t build_len, target_len;
  504. bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "64" : "32";
  505. other_bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "32" : "64";
  506. winecrt0 = strmake( "/wine%s/libwinecrt0.a", get_arch_dir( opts->target ));
  507. build_multiarch = get_multiarch_dir( get_default_target() );
  508. target_multiarch = get_multiarch_dir( opts->target );
  509. build_len = strlen( build_multiarch );
  510. target_len = strlen( target_multiarch );
  511. for (i = 0; i < ARRAY_SIZE(stdlibpath); i++)
  512. {
  513. const char *root = (i && opts->sysroot) ? opts->sysroot : "";
  514. char *p, *buffer;
  515. if (!stdlibpath[i]) continue;
  516. buffer = xmalloc( strlen(root) + strlen(stdlibpath[i]) +
  517. strlen("/arm-linux-gnueabi") + strlen(winecrt0) + 1 );
  518. strcpy( buffer, root );
  519. strcat( buffer, stdlibpath[i] );
  520. p = buffer + strlen(buffer);
  521. while (p > buffer && p[-1] == '/') p--;
  522. strcpy( p, winecrt0 );
  523. if (!stat( buffer, &st )) goto found;
  524. if (p > buffer + 2 && (!memcmp( p - 2, "32", 2 ) || !memcmp( p - 2, "64", 2 )))
  525. {
  526. p -= 2;
  527. strcpy( p, winecrt0 );
  528. if (!stat( buffer, &st )) goto found;
  529. }
  530. strcpy( p, bit_suffix );
  531. strcat( p, winecrt0 );
  532. if (!stat( buffer, &st )) goto found;
  533. strcpy( p, target_multiarch );
  534. strcat( p, winecrt0 );
  535. if (!stat( buffer, &st )) goto found;
  536. strcpy( buffer, root );
  537. strcat( buffer, stdlibpath[i] );
  538. p = buffer + strlen(buffer);
  539. while (p > buffer && p[-1] == '/') p--;
  540. strcpy( p, winecrt0 );
  541. /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
  542. while (p > buffer)
  543. {
  544. p--;
  545. while (p > buffer && *p != '/') p--;
  546. if (*p != '/') break;
  547. /* try s/$build_cpu/$target_cpu/ on multiarch */
  548. if (get_default_target().cpu != opts->target.cpu &&
  549. !memcmp( p, build_multiarch, build_len ) && p[build_len] == '/')
  550. {
  551. memmove( p + target_len, p + build_len, strlen( p + build_len ) + 1 );
  552. memcpy( p, target_multiarch, target_len );
  553. if (!stat( buffer, &st )) goto found;
  554. memmove( p + build_len, p + target_len, strlen( p + target_len ) + 1 );
  555. memcpy( p, build_multiarch, build_len );
  556. }
  557. if (memcmp( p + 1, "lib", 3 )) continue;
  558. if (p[4] == '/')
  559. {
  560. memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
  561. memcpy( p + 4, bit_suffix, 2 );
  562. if (!stat( buffer, &st )) goto found;
  563. memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
  564. }
  565. else if (!memcmp( p + 4, other_bit_suffix, 2 ) && p[6] == '/')
  566. {
  567. memcpy( p + 4, bit_suffix, 2 );
  568. if (!stat( buffer, &st )) goto found;
  569. memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
  570. if (!stat( buffer, &st )) goto found;
  571. memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
  572. memcpy( p + 4, other_bit_suffix, 2 );
  573. }
  574. }
  575. free( buffer );
  576. continue;
  577. found:
  578. buffer[strlen(buffer) - strlen(winecrt0)] = 0;
  579. return buffer;
  580. }
  581. return strmake( "%s%s", root, LIBDIR );
  582. }
  583. static void init_argv0_dir( const char *argv0 )
  584. {
  585. #ifndef _WIN32
  586. char *dir;
  587. #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
  588. dir = realpath( "/proc/self/exe", NULL );
  589. #elif defined (__FreeBSD__) || defined(__DragonFly__)
  590. static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
  591. size_t path_size = PATH_MAX;
  592. char *path = malloc( path_size );
  593. if (path && !sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
  594. dir = realpath( path, NULL );
  595. free( path );
  596. #else
  597. dir = realpath( argv0, NULL );
  598. #endif
  599. if (!dir) return;
  600. bindir = get_dirname( dir );
  601. includedir = strmake( "%s/%s", bindir, BIN_TO_INCLUDEDIR );
  602. libdir = strmake( "%s/%s", bindir, BIN_TO_LIBDIR );
  603. #endif
  604. }
  605. static void compile(struct options* opts, const char* lang)
  606. {
  607. struct strarray comp_args = get_translator(opts);
  608. unsigned int i, j;
  609. int gcc_defs = 0;
  610. struct strarray gcc;
  611. struct strarray gpp;
  612. if (opts->force_pointer_size)
  613. strarray_add( &comp_args, strmake("-m%u", 8 * opts->force_pointer_size ) );
  614. switch(opts->processor)
  615. {
  616. case proc_cpp: gcc_defs = 1; break;
  617. case proc_as: gcc_defs = 0; break;
  618. /* Note: if the C compiler is gcc we assume the C++ compiler is too */
  619. /* mixing different C and C++ compilers isn't supported in configure anyway */
  620. case proc_cc:
  621. case proc_cxx:
  622. gcc = build_tool_name(opts, TOOL_CC);
  623. gpp = build_tool_name(opts, TOOL_CXX);
  624. for ( j = 0; !gcc_defs && j < comp_args.count; j++ )
  625. {
  626. const char *cc = comp_args.str[j];
  627. for (i = 0; !gcc_defs && i < gcc.count; i++)
  628. gcc_defs = gcc.str[i][0] != '-' && strendswith(cc, gcc.str[i]);
  629. for (i = 0; !gcc_defs && i < gpp.count; i++)
  630. gcc_defs = gpp.str[i][0] != '-' && strendswith(cc, gpp.str[i]);
  631. }
  632. break;
  633. }
  634. if (opts->target.platform == PLATFORM_WINDOWS ||
  635. opts->target.platform == PLATFORM_CYGWIN ||
  636. opts->target.platform == PLATFORM_MINGW)
  637. goto no_compat_defines;
  638. if (opts->processor != proc_cpp)
  639. {
  640. if (gcc_defs && !opts->wine_objdir && !opts->noshortwchar)
  641. {
  642. strarray_add(&comp_args, "-fshort-wchar");
  643. strarray_add(&comp_args, "-DWINE_UNICODE_NATIVE");
  644. }
  645. strarray_add(&comp_args, "-D_REENTRANT");
  646. if (opts->pic)
  647. strarray_add(&comp_args, "-fPIC");
  648. else
  649. strarray_add(&comp_args, "-fno-PIC");
  650. }
  651. if (get_target_ptr_size( opts->target ) == 8)
  652. {
  653. strarray_add(&comp_args, "-DWIN64");
  654. strarray_add(&comp_args, "-D_WIN64");
  655. strarray_add(&comp_args, "-D__WIN64");
  656. strarray_add(&comp_args, "-D__WIN64__");
  657. }
  658. strarray_add(&comp_args, "-DWIN32");
  659. strarray_add(&comp_args, "-D_WIN32");
  660. strarray_add(&comp_args, "-D__WIN32");
  661. strarray_add(&comp_args, "-D__WIN32__");
  662. strarray_add(&comp_args, "-D__WINNT");
  663. strarray_add(&comp_args, "-D__WINNT__");
  664. if (gcc_defs)
  665. {
  666. switch (opts->target.cpu)
  667. {
  668. case CPU_x86_64:
  669. case CPU_ARM64:
  670. strarray_add(&comp_args, "-D__stdcall=__attribute__((ms_abi))");
  671. strarray_add(&comp_args, "-D__cdecl=__stdcall");
  672. strarray_add(&comp_args, "-D__fastcall=__stdcall");
  673. break;
  674. case CPU_i386:
  675. strarray_add(&comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
  676. strarray_add(&comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
  677. strarray_add(&comp_args, "-D__fastcall=__attribute__((__fastcall__))");
  678. break;
  679. case CPU_ARM:
  680. strarray_add(&comp_args, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
  681. strarray_add(&comp_args, "-D__cdecl=__stdcall");
  682. strarray_add(&comp_args, "-D__fastcall=__stdcall");
  683. break;
  684. }
  685. strarray_add(&comp_args, "-D_stdcall=__stdcall");
  686. strarray_add(&comp_args, "-D_cdecl=__cdecl");
  687. strarray_add(&comp_args, "-D_fastcall=__fastcall");
  688. strarray_add(&comp_args, "-D__declspec(x)=__declspec_##x");
  689. strarray_add(&comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
  690. strarray_add(&comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
  691. strarray_add(&comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
  692. strarray_add(&comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
  693. strarray_add(&comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
  694. strarray_add(&comp_args, "-D__declspec_naked=__attribute__((naked))");
  695. strarray_add(&comp_args, "-D__declspec_noinline=__attribute__((noinline))");
  696. strarray_add(&comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
  697. strarray_add(&comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
  698. strarray_add(&comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
  699. strarray_add(&comp_args, "-D__declspec_selectany=__attribute__((weak))");
  700. strarray_add(&comp_args, "-D__declspec_thread=__thread");
  701. }
  702. strarray_add(&comp_args, "-D__int8=char");
  703. strarray_add(&comp_args, "-D__int16=short");
  704. strarray_add(&comp_args, "-D__int32=int");
  705. if (get_target_ptr_size( opts->target ) == 8)
  706. strarray_add(&comp_args, "-D__int64=long");
  707. else
  708. strarray_add(&comp_args, "-D__int64=long long");
  709. no_compat_defines:
  710. strarray_add(&comp_args, "-D__WINE__");
  711. /* options we handle explicitly */
  712. if (opts->compile_only)
  713. strarray_add(&comp_args, "-c");
  714. if (opts->output_name)
  715. {
  716. strarray_add(&comp_args, "-o");
  717. strarray_add(&comp_args, opts->output_name);
  718. }
  719. /* the rest of the pass-through parameters */
  720. strarray_addall(&comp_args, opts->compiler_args);
  721. /* the language option, if any */
  722. if (lang && strcmp(lang, "-xnone"))
  723. strarray_add(&comp_args, lang);
  724. /* last, but not least, the files */
  725. for ( j = 0; j < opts->files.count; j++ )
  726. {
  727. if (opts->files.str[j][0] != '-' || !opts->files.str[j][1]) /* not an option or bare '-' (i.e. stdin) */
  728. strarray_add(&comp_args, opts->files.str[j]);
  729. }
  730. /* standard includes come last in the include search path */
  731. if (!opts->wine_objdir && !opts->nostdinc)
  732. {
  733. const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
  734. const char *root = opts->isysroot ? opts->isysroot : opts->sysroot ? opts->sysroot : "";
  735. const char *isystem = gcc_defs ? "-isystem" : "-I";
  736. const char *idirafter = gcc_defs ? "-idirafter" : "-I";
  737. if (opts->use_msvcrt)
  738. {
  739. if (includedir) strarray_add( &comp_args, strmake( "%s%s/wine/msvcrt", isystem, includedir ));
  740. for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
  741. {
  742. if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
  743. strarray_add(&comp_args, strmake( "%s%s%s/wine/msvcrt", isystem, root, incl_dirs[j] ));
  744. }
  745. strarray_add(&comp_args, "-D__MSVCRT__");
  746. }
  747. if (includedir)
  748. {
  749. strarray_add( &comp_args, strmake( "%s%s/wine/windows", isystem, includedir ));
  750. strarray_add( &comp_args, strmake( "%s%s", idirafter, includedir ));
  751. }
  752. for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
  753. {
  754. if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
  755. strarray_add(&comp_args, strmake( "%s%s%s/wine/windows", isystem, root, incl_dirs[j] ));
  756. strarray_add(&comp_args, strmake( "%s%s%s", idirafter, root, incl_dirs[j] ));
  757. }
  758. }
  759. else if (opts->wine_objdir)
  760. strarray_add(&comp_args, strmake("-I%s/include", opts->wine_objdir) );
  761. spawn(opts->prefix, comp_args, 0);
  762. }
  763. static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
  764. {
  765. struct options copts;
  766. /* make a copy so we don't change any of the initial stuff */
  767. /* a shallow copy is exactly what we want in this case */
  768. copts = *opts;
  769. copts.output_name = get_temp_file(get_basename_noext(file), ".o");
  770. copts.compile_only = 1;
  771. copts.files = empty_strarray;
  772. strarray_add(&copts.files, file);
  773. compile(&copts, lang);
  774. return copts.output_name;
  775. }
  776. /* return the initial set of options needed to run winebuild */
  777. static struct strarray get_winebuild_args(struct options *opts)
  778. {
  779. const char* winebuild = getenv("WINEBUILD");
  780. const char *binary = NULL;
  781. struct strarray spec_args = empty_strarray;
  782. unsigned int i;
  783. if (opts->winebuild)
  784. binary = opts->winebuild;
  785. else if (opts->wine_objdir)
  786. binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
  787. else if (winebuild)
  788. binary = find_binary( opts->prefix, winebuild );
  789. else if (bindir)
  790. binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
  791. else
  792. binary = find_binary( opts->prefix, "winebuild" );
  793. if (!binary) error( "Could not find winebuild\n" );
  794. strarray_add( &spec_args, binary );
  795. if (verbose) strarray_add( &spec_args, "-v" );
  796. if (keep_generated) strarray_add( &spec_args, "--save-temps" );
  797. if (opts->target_alias)
  798. {
  799. strarray_add( &spec_args, "--target" );
  800. strarray_add( &spec_args, opts->target_alias );
  801. }
  802. for (i = 0; i < opts->prefix.count; i++)
  803. strarray_add( &spec_args, strmake( "-B%s", opts->prefix.str[i] ));
  804. strarray_addall( &spec_args, opts->winebuild_args );
  805. if (opts->unwind_tables) strarray_add( &spec_args, "-fasynchronous-unwind-tables" );
  806. else strarray_add( &spec_args, "-fno-asynchronous-unwind-tables" );
  807. return spec_args;
  808. }
  809. static void fixup_constructors( struct options *opts, const char *file )
  810. {
  811. struct strarray args = get_winebuild_args( opts );
  812. strarray_add( &args, "--fixup-ctors" );
  813. strarray_add( &args, file );
  814. spawn( opts->prefix, args, 0 );
  815. }
  816. static void make_wine_builtin( struct options *opts, const char *file )
  817. {
  818. struct strarray args = get_winebuild_args( opts );
  819. strarray_add( &args, "--builtin" );
  820. strarray_add( &args, file );
  821. spawn( opts->prefix, args, 0 );
  822. }
  823. /* check if there is a static lib associated to a given dll */
  824. static char *find_static_lib( const char *dll )
  825. {
  826. char *lib = strmake("%s.a", dll);
  827. if (get_file_type(lib) == file_arh) return lib;
  828. free( lib );
  829. return NULL;
  830. }
  831. static const char *find_libgcc(struct strarray prefix, struct strarray link_tool)
  832. {
  833. const char *out = get_temp_file( "find_libgcc", ".out" );
  834. const char *err = get_temp_file( "find_libgcc", ".err" );
  835. struct strarray link = empty_strarray;
  836. int sout = -1, serr = -1;
  837. char *libgcc, *p;
  838. struct stat st;
  839. size_t cnt;
  840. int ret;
  841. strarray_addall( &link, link_tool );
  842. strarray_add( &link, "-print-libgcc-file-name" );
  843. sout = dup( fileno(stdout) );
  844. freopen( out, "w", stdout );
  845. serr = dup( fileno(stderr) );
  846. freopen( err, "w", stderr );
  847. ret = spawn( prefix, link, 1 );
  848. if (sout >= 0)
  849. {
  850. dup2( sout, fileno(stdout) );
  851. close( sout );
  852. }
  853. if (serr >= 0)
  854. {
  855. dup2( serr, fileno(stderr) );
  856. close( serr );
  857. }
  858. if (ret || stat(out, &st) || !st.st_size) return NULL;
  859. libgcc = xmalloc(st.st_size + 1);
  860. sout = open(out, O_RDONLY);
  861. if (sout == -1) return NULL;
  862. cnt = read(sout, libgcc, st.st_size);
  863. close(sout);
  864. libgcc[cnt] = 0;
  865. if ((p = strchr(libgcc, '\n'))) *p = 0;
  866. return libgcc;
  867. }
  868. /* add specified library to the list of files */
  869. static void add_library( struct options *opts, struct strarray lib_dirs,
  870. struct strarray *files, const char *library )
  871. {
  872. char *static_lib, *fullname = 0;
  873. switch(get_lib_type(opts->target, lib_dirs, library, "lib", opts->lib_suffix, &fullname))
  874. {
  875. case file_arh:
  876. strarray_add(files, strmake("-a%s", fullname));
  877. break;
  878. case file_dll:
  879. strarray_add(files, strmake("-d%s", fullname));
  880. if ((static_lib = find_static_lib(fullname)))
  881. {
  882. strarray_add(files, strmake("-a%s",static_lib));
  883. free(static_lib);
  884. }
  885. break;
  886. case file_so:
  887. default:
  888. /* keep it anyway, the linker may know what to do with it */
  889. strarray_add(files, strmake("-l%s", library));
  890. break;
  891. }
  892. free(fullname);
  893. }
  894. /* run winebuild to generate the .spec.o file */
  895. static const char *build_spec_obj( struct options *opts, const char *spec_file, const char *output_file,
  896. struct strarray files, struct strarray lib_dirs, const char *entry_point )
  897. {
  898. unsigned int i;
  899. int is_pe = is_pe_target( opts );
  900. struct strarray spec_args = get_winebuild_args( opts );
  901. struct strarray tool;
  902. const char *spec_o_name, *output_name;
  903. /* get the filename from the path */
  904. output_name = get_basename( output_file );
  905. tool = build_tool_name( opts, TOOL_CC );
  906. strarray_add( &spec_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
  907. if (!is_pe)
  908. {
  909. tool = build_tool_name( opts, TOOL_LD );
  910. strarray_add( &spec_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
  911. }
  912. spec_o_name = get_temp_file(output_name, ".spec.o");
  913. if (opts->force_pointer_size)
  914. strarray_add(&spec_args, strmake("-m%u", 8 * opts->force_pointer_size ));
  915. if (opts->pic && !is_pe) strarray_add(&spec_args, "-fPIC");
  916. strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
  917. if (opts->fake_module)
  918. {
  919. strarray_add(&spec_args, "--fake-module");
  920. strarray_add(&spec_args, "-o");
  921. strarray_add(&spec_args, output_file);
  922. }
  923. else
  924. {
  925. strarray_add(&spec_args, "-o");
  926. strarray_add(&spec_args, spec_o_name);
  927. }
  928. if (spec_file)
  929. {
  930. strarray_add(&spec_args, "-E");
  931. strarray_add(&spec_args, spec_file);
  932. }
  933. if (!opts->shared)
  934. {
  935. strarray_add(&spec_args, "-F");
  936. strarray_add(&spec_args, output_name);
  937. strarray_add(&spec_args, "--subsystem");
  938. strarray_add(&spec_args, opts->gui_app ? "windows" : "console");
  939. if (opts->large_address_aware) strarray_add( &spec_args, "--large-address-aware" );
  940. }
  941. if (opts->target.platform == PLATFORM_WINDOWS) strarray_add(&spec_args, "--safeseh");
  942. if (entry_point)
  943. {
  944. strarray_add(&spec_args, "--entry");
  945. strarray_add(&spec_args, entry_point);
  946. }
  947. if (opts->subsystem)
  948. {
  949. strarray_add(&spec_args, "--subsystem");
  950. strarray_add(&spec_args, opts->subsystem);
  951. }
  952. for (i = 0; i < lib_dirs.count; i++)
  953. strarray_add(&spec_args, strmake("-L%s", lib_dirs.str[i]));
  954. if (!is_pe)
  955. {
  956. for (i = 0; i < opts->delayimports.count; i++)
  957. strarray_add(&spec_args, strmake("-d%s", opts->delayimports.str[i]));
  958. }
  959. /* add resource files */
  960. for (i = 0; i < files.count; i++)
  961. if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
  962. /* add other files */
  963. strarray_add(&spec_args, "--");
  964. for (i = 0; i < files.count; i++)
  965. {
  966. switch(files.str[i][1])
  967. {
  968. case 'd':
  969. case 'a':
  970. case 'o':
  971. strarray_add(&spec_args, files.str[i] + 2);
  972. break;
  973. }
  974. }
  975. spawn(opts->prefix, spec_args, 0);
  976. return spec_o_name;
  977. }
  978. static void build(struct options* opts)
  979. {
  980. struct strarray lib_dirs = empty_strarray;
  981. struct strarray files = empty_strarray;
  982. struct strarray link_args;
  983. char *output_file, *output_path;
  984. const char *spec_o_name = NULL, *libgcc = NULL;
  985. const char *output_name, *spec_file, *lang;
  986. int generate_app_loader = 1;
  987. const char *crt_lib = NULL, *entry_point = NULL;
  988. int is_pe = is_pe_target( opts );
  989. unsigned int j;
  990. /* NOTE: for the files array we'll use the following convention:
  991. * -axxx: xxx is an archive (.a)
  992. * -dxxx: xxx is a DLL (.def)
  993. * -lxxx: xxx is an unsorted library
  994. * -oxxx: xxx is an object (.o)
  995. * -rxxx: xxx is a resource (.res)
  996. * -sxxx: xxx is a shared lib (.so)
  997. * -xlll: lll is the language (c, c++, etc.)
  998. */
  999. output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
  1000. /* 'winegcc -o app xxx.exe.so' only creates the load script */
  1001. if (opts->files.count == 1 && strendswith(opts->files.str[0], ".exe.so"))
  1002. {
  1003. create_file(output_file, 0755, app_loader_template, opts->files.str[0]);
  1004. return;
  1005. }
  1006. /* generate app loader only for .exe */
  1007. if (opts->shared || is_pe || strendswith(output_file, ".so"))
  1008. generate_app_loader = 0;
  1009. if (strendswith(output_file, ".fake")) opts->fake_module = 1;
  1010. /* normalize the filename a bit: strip .so, ensure it has proper ext */
  1011. if (!strchr(get_basename( output_file ), '.'))
  1012. output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
  1013. else if (strendswith(output_file, ".so"))
  1014. output_file[strlen(output_file) - 3] = 0;
  1015. output_path = is_pe ? output_file : strmake( "%s.so", output_file );
  1016. /* get the filename from the path */
  1017. output_name = get_basename( output_file );
  1018. /* prepare the linking path */
  1019. if (!opts->wine_objdir)
  1020. {
  1021. char *lib_dir = get_lib_dir( opts );
  1022. strarray_addall( &lib_dirs, opts->lib_dirs );
  1023. strarray_add( &lib_dirs, strmake( "%s/wine%s", lib_dir, get_arch_dir( opts->target )));
  1024. strarray_add( &lib_dirs, lib_dir );
  1025. }
  1026. else
  1027. {
  1028. strarray_add(&lib_dirs, strmake("%s/dlls", opts->wine_objdir));
  1029. strarray_addall(&lib_dirs, opts->lib_dirs);
  1030. }
  1031. /* mark the files with their appropriate type */
  1032. spec_file = lang = 0;
  1033. for ( j = 0; j < opts->files.count; j++ )
  1034. {
  1035. const char* file = opts->files.str[j];
  1036. if (file[0] != '-')
  1037. {
  1038. switch(get_file_type(file))
  1039. {
  1040. case file_def:
  1041. case file_spec:
  1042. if (spec_file)
  1043. error("Only one spec file can be specified\n");
  1044. spec_file = file;
  1045. break;
  1046. case file_rc:
  1047. /* FIXME: invoke wrc to build it */
  1048. error("Can't compile .rc file at the moment: %s\n", file);
  1049. break;
  1050. case file_res:
  1051. strarray_add(&files, strmake("-r%s", file));
  1052. break;
  1053. case file_obj:
  1054. strarray_add(&files, strmake("-o%s", file));
  1055. break;
  1056. case file_arh:
  1057. if (opts->use_msvcrt)
  1058. {
  1059. char *p = get_basename( file );
  1060. if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
  1061. }
  1062. strarray_add(&files, strmake("-a%s", file));
  1063. break;
  1064. case file_so:
  1065. strarray_add(&files, strmake("-s%s", file));
  1066. break;
  1067. case file_na:
  1068. error("File does not exist: %s\n", file);
  1069. break;
  1070. default:
  1071. file = compile_to_object(opts, file, lang);
  1072. strarray_add(&files, strmake("-o%s", file));
  1073. break;
  1074. }
  1075. }
  1076. else if (file[1] == 'l')
  1077. add_library(opts, lib_dirs, &files, file + 2 );
  1078. else if (file[1] == 'x')
  1079. lang = file;
  1080. else if(file[1] == 'W')
  1081. strarray_add(&files, file);
  1082. }
  1083. /* add the default libraries, if needed */
  1084. if (!opts->wine_objdir && !opts->nodefaultlibs)
  1085. {
  1086. if (opts->gui_app)
  1087. {
  1088. add_library(opts, lib_dirs, &files, "shell32");
  1089. add_library(opts, lib_dirs, &files, "comdlg32");
  1090. add_library(opts, lib_dirs, &files, "gdi32");
  1091. }
  1092. add_library(opts, lib_dirs, &files, "advapi32");
  1093. add_library(opts, lib_dirs, &files, "user32");
  1094. add_library(opts, lib_dirs, &files, "winecrt0");
  1095. if (opts->use_msvcrt)
  1096. {
  1097. if (!crt_lib)
  1098. {
  1099. if (strncmp( output_name, "msvcr", 5 ) &&
  1100. strncmp( output_name, "ucrt", 4 ) &&
  1101. strcmp( output_name, "crtdll.dll" ))
  1102. add_library(opts, lib_dirs, &files, "ucrtbase");
  1103. }
  1104. else strarray_add(&files, strmake("-a%s", crt_lib));
  1105. }
  1106. if (opts->win16_app) add_library(opts, lib_dirs, &files, "kernel");
  1107. add_library(opts, lib_dirs, &files, "kernel32");
  1108. add_library(opts, lib_dirs, &files, "ntdll");
  1109. }
  1110. /* set default entry point, if needed */
  1111. if (!opts->entry_point)
  1112. {
  1113. if (opts->subsystem && !opts->unix_lib && !strcmp( opts->subsystem, "native" ))
  1114. entry_point = (is_pe && opts->target.cpu == CPU_i386) ? "DriverEntry@8" : "DriverEntry";
  1115. else if (opts->use_msvcrt && !opts->shared && !opts->win16_app)
  1116. entry_point = opts->unicode_app ? "wmainCRTStartup" : "mainCRTStartup";
  1117. }
  1118. else entry_point = opts->entry_point;
  1119. /* run winebuild to generate the .spec.o file */
  1120. if (spec_file || !opts->unix_lib)
  1121. spec_o_name = build_spec_obj( opts, spec_file, output_file, files, lib_dirs, entry_point );
  1122. if (opts->fake_module) return; /* nothing else to do */
  1123. /* link everything together now */
  1124. link_args = get_link_args( opts, output_name );
  1125. if (opts->nodefaultlibs || opts->use_msvcrt)
  1126. {
  1127. switch (opts->target.platform)
  1128. {
  1129. case PLATFORM_MINGW:
  1130. case PLATFORM_CYGWIN:
  1131. libgcc = find_libgcc( opts->prefix, link_args );
  1132. if (!libgcc) libgcc = "-lgcc";
  1133. break;
  1134. default:
  1135. break;
  1136. }
  1137. }
  1138. strarray_add(&link_args, "-o");
  1139. strarray_add(&link_args, output_path);
  1140. for ( j = 0; j < lib_dirs.count; j++ )
  1141. strarray_add(&link_args, strmake("-L%s", lib_dirs.str[j]));
  1142. if (is_pe && opts->use_msvcrt && !entry_point && (opts->shared || opts->win16_app))
  1143. entry_point = opts->target.cpu == CPU_i386 ? "DllMainCRTStartup@12" : "DllMainCRTStartup";
  1144. if (is_pe && entry_point)
  1145. {
  1146. if (opts->target.platform == PLATFORM_WINDOWS)
  1147. strarray_add(&link_args, strmake("-Wl,-entry:%s", entry_point));
  1148. else
  1149. strarray_add(&link_args, strmake("-Wl,--entry,%s%s",
  1150. is_pe && opts->target.cpu == CPU_i386 ? "_" : "",
  1151. entry_point));
  1152. }
  1153. if (spec_o_name) strarray_add(&link_args, spec_o_name);
  1154. if (is_pe)
  1155. {
  1156. for (j = 0; j < opts->delayimports.count; j++)
  1157. {
  1158. if (opts->target.platform == PLATFORM_WINDOWS)
  1159. strarray_add(&link_args, strmake("-Wl,-delayload:%s", opts->delayimports.str[j]));
  1160. else
  1161. strarray_add(&link_args, strmake("-Wl,-delayload,%s",opts->delayimports.str[j]));
  1162. }
  1163. }
  1164. for ( j = 0; j < files.count; j++ )
  1165. {
  1166. const char* name = files.str[j] + 2;
  1167. switch(files.str[j][1])
  1168. {
  1169. case 'l':
  1170. strarray_add(&link_args, strmake("-l%s", name));
  1171. break;
  1172. case 's':
  1173. case 'o':
  1174. strarray_add(&link_args, name);
  1175. break;
  1176. case 'a':
  1177. if (!opts->use_msvcrt && !opts->lib_suffix && strchr(name, '/'))
  1178. {
  1179. /* turn the path back into -Ldir -lfoo options
  1180. * this makes sure that we use the specified libs even
  1181. * when mingw adds its own import libs to the link */
  1182. const char *p = get_basename( name );
  1183. if (is_pe)
  1184. {
  1185. if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
  1186. {
  1187. strarray_add(&link_args, strmake("-L%s", get_dirname(name) ));
  1188. strarray_add(&link_args, strmake("-l%s", get_basename_noext( p + 3 )));
  1189. break;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. /* don't link to ntdll or ntoskrnl in non-msvcrt mode
  1195. * since they export CRT functions */
  1196. if (!strcmp( p, "libntdll.a" )) break;
  1197. if (!strcmp( p, "libntoskrnl.a" )) break;
  1198. }
  1199. }
  1200. strarray_add(&link_args, name);
  1201. break;
  1202. case 'W':
  1203. strarray_add(&link_args, files.str[j]);
  1204. break;
  1205. }
  1206. }
  1207. if (!opts->nostdlib && !is_pe)
  1208. {
  1209. strarray_add(&link_args, "-lm");
  1210. strarray_add(&link_args, "-lc");
  1211. }
  1212. if (libgcc) strarray_add(&link_args, libgcc);
  1213. output_file_name = output_path;
  1214. output_debug_file = opts->debug_file;
  1215. output_implib = opts->out_implib;
  1216. atexit( cleanup_output_files );
  1217. spawn(opts->prefix, link_args, 0);
  1218. if (opts->debug_file && !strendswith(opts->debug_file, ".pdb"))
  1219. {
  1220. struct strarray tool, objcopy = build_tool_name(opts, TOOL_OBJCOPY);
  1221. tool = empty_strarray;
  1222. strarray_addall( &tool, objcopy );
  1223. strarray_add(&tool, "--only-keep-debug");
  1224. strarray_add(&tool, output_path);
  1225. strarray_add(&tool, opts->debug_file);
  1226. spawn(opts->prefix, tool, 1);
  1227. tool = empty_strarray;
  1228. strarray_addall( &tool, objcopy );
  1229. strarray_add(&tool, "--strip-debug");
  1230. strarray_add(&tool, output_path);
  1231. spawn(opts->prefix, tool, 1);
  1232. tool = empty_strarray;
  1233. strarray_addall( &tool, objcopy );
  1234. strarray_add(&tool, "--add-gnu-debuglink");
  1235. strarray_add(&tool, opts->debug_file);
  1236. strarray_add(&tool, output_path);
  1237. spawn(opts->prefix, tool, 0);
  1238. }
  1239. if (opts->unix_lib) return;
  1240. if (opts->out_implib && !is_pe)
  1241. {
  1242. struct strarray tool, implib_args;
  1243. if (!spec_file)
  1244. error("--out-implib requires a .spec or .def file\n");
  1245. implib_args = get_winebuild_args( opts );
  1246. tool = build_tool_name( opts, TOOL_CC );
  1247. strarray_add( &implib_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
  1248. tool = build_tool_name( opts, TOOL_LD );
  1249. strarray_add( &implib_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
  1250. strarray_add(&implib_args, "--implib");
  1251. strarray_add(&implib_args, "-o");
  1252. strarray_add(&implib_args, opts->out_implib);
  1253. strarray_add(&implib_args, "--export");
  1254. strarray_add(&implib_args, spec_file);
  1255. spawn(opts->prefix, implib_args, 0);
  1256. }
  1257. /* set the base address with prelink if linker support is not present */
  1258. if (opts->prelink && !opts->target_alias)
  1259. {
  1260. if (opts->prelink[0] && strcmp(opts->prelink,"false"))
  1261. {
  1262. struct strarray prelink_args = empty_strarray;
  1263. strarray_add(&prelink_args, opts->prelink);
  1264. strarray_add(&prelink_args, "--reloc-only");
  1265. strarray_add(&prelink_args, opts->image_base);
  1266. strarray_add(&prelink_args, output_path);
  1267. spawn(opts->prefix, prelink_args, 1);
  1268. }
  1269. }
  1270. if (!is_pe) fixup_constructors( opts, output_path );
  1271. else if (opts->wine_builtin) make_wine_builtin( opts, output_path );
  1272. /* create the loader script */
  1273. if (generate_app_loader)
  1274. create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
  1275. }
  1276. static void forward( struct options *opts )
  1277. {
  1278. struct strarray args = get_translator(opts);
  1279. strarray_addall(&args, opts->compiler_args);
  1280. strarray_addall(&args, opts->linker_args);
  1281. spawn(opts->prefix, args, 0);
  1282. }
  1283. static int is_linker_arg(const char* arg)
  1284. {
  1285. static const char* link_switches[] =
  1286. {
  1287. "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
  1288. "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
  1289. "-fprofile-generate", "-fprofile-use"
  1290. };
  1291. unsigned int j;
  1292. switch (arg[1])
  1293. {
  1294. case 'R':
  1295. case 'z':
  1296. case 'u':
  1297. return 1;
  1298. case 'W':
  1299. if (strncmp("-Wl,", arg, 4) == 0) return 1;
  1300. break;
  1301. case 'X':
  1302. if (strcmp("-Xlinker", arg) == 0) return 1;
  1303. break;
  1304. case 'a':
  1305. if (strcmp("-arch", arg) == 0) return 1;
  1306. break;
  1307. case 'f':
  1308. if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
  1309. break;
  1310. case 'r':
  1311. if (strncmp("-rtlib=", arg, 7) == 0) return 1;
  1312. break;
  1313. }
  1314. for (j = 0; j < ARRAY_SIZE(link_switches); j++)
  1315. if (strcmp(link_switches[j], arg) == 0) return 1;
  1316. return 0;
  1317. }
  1318. static void parse_target_option( struct options *opts, const char *target )
  1319. {
  1320. opts->target_alias = xstrdup( target );
  1321. if (!parse_target( target, &opts->target )) error( "Invalid target specification '%s'\n", target );
  1322. }
  1323. static int is_option( struct options *opts, int i, const char *option, const char **option_arg )
  1324. {
  1325. if (!strcmp( opts->args.str[i], option ))
  1326. {
  1327. if (opts->args.count == i) error( "option %s requires an argument\n", opts->args.str[i] );
  1328. *option_arg = opts->args.str[i + 1];
  1329. return 1;
  1330. }
  1331. if (!strncmp( opts->args.str[i], option, strlen(option) ) && opts->args.str[i][strlen(option)] == '=')
  1332. {
  1333. *option_arg = opts->args.str[i] + strlen(option) + 1;
  1334. return 1;
  1335. }
  1336. return 0;
  1337. }
  1338. int main(int argc, char **argv)
  1339. {
  1340. int i, c, next_is_arg = 0, linking = 1;
  1341. int raw_compiler_arg, raw_linker_arg, raw_winebuild_arg;
  1342. const char* option_arg;
  1343. struct options opts;
  1344. char* lang = 0;
  1345. char* str;
  1346. #ifdef SIGHUP
  1347. signal( SIGHUP, exit_on_signal );
  1348. #endif
  1349. signal( SIGTERM, exit_on_signal );
  1350. signal( SIGINT, exit_on_signal );
  1351. #ifdef HAVE_SIGADDSET
  1352. sigemptyset( &signal_mask );
  1353. sigaddset( &signal_mask, SIGHUP );
  1354. sigaddset( &signal_mask, SIGTERM );
  1355. sigaddset( &signal_mask, SIGINT );
  1356. #endif
  1357. init_argv0_dir( argv[0] );
  1358. /* setup tmp file removal at exit */
  1359. atexit(clean_temp_files);
  1360. /* initialize options */
  1361. memset(&opts, 0, sizeof(opts));
  1362. opts.target = init_argv0_target( argv[0] );
  1363. opts.pic = 1;
  1364. /* determine the processor type */
  1365. if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
  1366. else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
  1367. for (i = 1; i < argc; i++)
  1368. {
  1369. char *input_buffer = NULL, *iter, *opt, *out;
  1370. struct stat st;
  1371. int fd;
  1372. if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
  1373. {
  1374. strarray_add( &opts.args, argv[i] );
  1375. continue;
  1376. }
  1377. if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s\n", argv[i] + 1 );
  1378. if (st.st_size)
  1379. {
  1380. input_buffer = xmalloc( st.st_size + 1 );
  1381. if (read( fd, input_buffer, st.st_size ) != st.st_size) error( "Cannot read %s\n", argv[i] + 1 );
  1382. }
  1383. close( fd );
  1384. for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
  1385. {
  1386. char quote = 0;
  1387. while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
  1388. if (iter == input_buffer + st.st_size) break;
  1389. opt = out = iter;
  1390. while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
  1391. {
  1392. if (*iter == quote)
  1393. {
  1394. iter++;
  1395. quote = 0;
  1396. }
  1397. else if (*iter == '\'' || *iter == '"') quote = *iter++;
  1398. else
  1399. {
  1400. if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
  1401. *out++ = *iter++;
  1402. }
  1403. }
  1404. *out = 0;
  1405. strarray_add( &opts.args, opt );
  1406. }
  1407. }
  1408. /* parse options */
  1409. for (i = 0; i < opts.args.count; i++)
  1410. {
  1411. if (opts.args.str[i][0] == '-' && opts.args.str[i][1]) /* option, except '-' alone is stdin, which is a file */
  1412. {
  1413. /* determine if this switch is followed by a separate argument */
  1414. next_is_arg = 0;
  1415. option_arg = 0;
  1416. switch(opts.args.str[i][1])
  1417. {
  1418. case 'x': case 'o': case 'D': case 'U':
  1419. case 'I': case 'A': case 'l': case 'u':
  1420. case 'b': case 'V': case 'G': case 'L':
  1421. case 'B': case 'R': case 'z':
  1422. if (opts.args.str[i][2]) option_arg = &opts.args.str[i][2];
  1423. else next_is_arg = 1;
  1424. break;
  1425. case 'i':
  1426. next_is_arg = 1;
  1427. break;
  1428. case 'a':
  1429. if (strcmp("-aux-info", opts.args.str[i]) == 0)
  1430. next_is_arg = 1;
  1431. if (strcmp("-arch", opts.args.str[i]) == 0)
  1432. next_is_arg = 1;
  1433. break;
  1434. case 'X':
  1435. if (strcmp("-Xlinker", opts.args.str[i]) == 0)
  1436. next_is_arg = 1;
  1437. break;
  1438. case 'M':
  1439. c = opts.args.str[i][2];
  1440. if (c == 'F' || c == 'T' || c == 'Q')
  1441. {
  1442. if (opts.args.str[i][3]) option_arg = &opts.args.str[i][3];
  1443. else next_is_arg = 1;
  1444. }
  1445. break;
  1446. case 'f':
  1447. if (strcmp("-framework", opts.args.str[i]) == 0)
  1448. next_is_arg = 1;
  1449. break;
  1450. case 't':
  1451. next_is_arg = strcmp("-target", opts.args.str[i]) == 0;
  1452. break;
  1453. case '-':
  1454. next_is_arg = (strcmp("--param", opts.args.str[i]) == 0 ||
  1455. strcmp("--sysroot", opts.args.str[i]) == 0 ||
  1456. strcmp("--target", opts.args.str[i]) == 0 ||
  1457. strcmp("--wine-objdir", opts.args.str[i]) == 0 ||
  1458. strcmp("--winebuild", opts.args.str[i]) == 0 ||
  1459. strcmp("--lib-suffix", opts.args.str[i]) == 0);
  1460. break;
  1461. }
  1462. if (next_is_arg)
  1463. {
  1464. if (i + 1 >= opts.args.count) error("option -%c requires an argument\n", opts.args.str[i][1]);
  1465. option_arg = opts.args.str[i+1];
  1466. }
  1467. /* determine what options go 'as is' to the linker & the compiler */
  1468. raw_linker_arg = is_linker_arg(opts.args.str[i]);
  1469. raw_compiler_arg = !raw_linker_arg;
  1470. raw_winebuild_arg = 0;
  1471. /* do a bit of semantic analysis */
  1472. switch (opts.args.str[i][1])
  1473. {
  1474. case 'B':
  1475. str = strdup(option_arg);
  1476. if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
  1477. strarray_add(&opts.prefix, str);
  1478. raw_linker_arg = 1;
  1479. break;
  1480. case 'b':
  1481. parse_target_option( &opts, option_arg );
  1482. raw_compiler_arg = 0;
  1483. break;
  1484. case 'V':
  1485. opts.version = xstrdup( option_arg );
  1486. raw_compiler_arg = 0;
  1487. break;
  1488. case 'c': /* compile or assemble */
  1489. raw_compiler_arg = 0;
  1490. if (opts.args.str[i][2] == 0) opts.compile_only = 1;
  1491. /* fall through */
  1492. case 'S': /* generate assembler code */
  1493. case 'E': /* preprocess only */
  1494. if (opts.args.str[i][2] == 0) linking = 0;
  1495. break;
  1496. case 'f':
  1497. if (strcmp("-fno-short-wchar", opts.args.str[i]) == 0)
  1498. opts.noshortwchar = 1;
  1499. else if (!strcmp("-fasynchronous-unwind-tables", opts.args.str[i]))
  1500. opts.unwind_tables = 1;
  1501. else if (!strcmp("-fno-asynchronous-unwind-tables", opts.args.str[i]))
  1502. opts.unwind_tables = 0;
  1503. else if (!strcmp("-fPIC", opts.args.str[i]) || !strcmp("-fpic", opts.args.str[i]))
  1504. opts.pic = 1;
  1505. else if (!strcmp("-fno-PIC", opts.args.str[i]) || !strcmp("-fno-pic", opts.args.str[i]))
  1506. opts.pic = 0;
  1507. break;
  1508. case 'i':
  1509. if (!strcmp( "-isysroot", opts.args.str[i] )) opts.isysroot = opts.args.str[i + 1];
  1510. break;
  1511. case 'l':
  1512. strarray_add(&opts.files, strmake("-l%s", option_arg));
  1513. raw_compiler_arg = 0;
  1514. break;
  1515. case 'L':
  1516. strarray_add(&opts.lib_dirs, option_arg);
  1517. raw_compiler_arg = 0;
  1518. break;
  1519. case 'M': /* map file generation */
  1520. linking = 0;
  1521. break;
  1522. case 'm':
  1523. if (strcmp("-mno-cygwin", opts.args.str[i]) == 0)
  1524. {
  1525. opts.use_msvcrt = 1;
  1526. raw_compiler_arg = 0;
  1527. raw_winebuild_arg = 1;
  1528. }
  1529. if (strcmp("-mcygwin", opts.args.str[i]) == 0)
  1530. {
  1531. opts.use_msvcrt = 0;
  1532. raw_compiler_arg = 0;
  1533. }
  1534. else if (strcmp("-mwindows", opts.args.str[i]) == 0)
  1535. {
  1536. opts.gui_app = 1;
  1537. raw_compiler_arg = 0;
  1538. }
  1539. else if (strcmp("-mconsole", opts.args.str[i]) == 0)
  1540. {
  1541. opts.gui_app = 0;
  1542. raw_compiler_arg = 0;
  1543. }
  1544. else if (strcmp("-municode", opts.args.str[i]) == 0)
  1545. {
  1546. opts.unicode_app = 1;
  1547. raw_compiler_arg = 0;
  1548. raw_winebuild_arg = 1;
  1549. }
  1550. else if (strcmp("-mthreads", opts.args.str[i]) == 0)
  1551. {
  1552. raw_compiler_arg = 0;
  1553. }
  1554. else if (strcmp("-munix", opts.args.str[i]) == 0)
  1555. {
  1556. opts.unix_lib = 1;
  1557. raw_compiler_arg = 0;
  1558. raw_winebuild_arg = 1;
  1559. }
  1560. else if (strcmp("-m16", opts.args.str[i]) == 0)
  1561. {
  1562. opts.win16_app = 1;
  1563. raw_compiler_arg = 0;
  1564. raw_winebuild_arg = 1;
  1565. }
  1566. else if (strcmp("-m32", opts.args.str[i]) == 0)
  1567. {
  1568. set_target_ptr_size( &opts.target, 4 );
  1569. opts.force_pointer_size = 4;
  1570. raw_linker_arg = 1;
  1571. }
  1572. else if (strcmp("-m64", opts.args.str[i]) == 0)
  1573. {
  1574. set_target_ptr_size( &opts.target, 8 );
  1575. opts.force_pointer_size = 8;
  1576. raw_linker_arg = 1;
  1577. }
  1578. else if (!strcmp("-marm", opts.args.str[i] ) || !strcmp("-mthumb", opts.args.str[i] ))
  1579. {
  1580. raw_linker_arg = 1;
  1581. raw_winebuild_arg = 1;
  1582. }
  1583. else if (!strncmp("-mcpu=", opts.args.str[i], 6) ||
  1584. !strncmp("-mfpu=", opts.args.str[i], 6) ||
  1585. !strncmp("-march=", opts.args.str[i], 7) ||
  1586. !strncmp("-mfloat-abi=", opts.args.str[i], 12))
  1587. raw_winebuild_arg = 1;
  1588. break;
  1589. case 'n':
  1590. if (strcmp("-nostdinc", opts.args.str[i]) == 0)
  1591. opts.nostdinc = 1;
  1592. else if (strcmp("-nodefaultlibs", opts.args.str[i]) == 0)
  1593. opts.nodefaultlibs = 1;
  1594. else if (strcmp("-nostdlib", opts.args.str[i]) == 0)
  1595. opts.nostdlib = 1;
  1596. else if (strcmp("-nostartfiles", opts.args.str[i]) == 0)
  1597. opts.nostartfiles = 1;
  1598. break;
  1599. case 'o':
  1600. opts.output_name = option_arg;
  1601. raw_compiler_arg = 0;
  1602. break;
  1603. case 'p':
  1604. if (strcmp("-pthread", opts.args.str[i]) == 0)
  1605. {
  1606. raw_compiler_arg = 1;
  1607. raw_linker_arg = 1;
  1608. }
  1609. break;
  1610. case 's':
  1611. if (strcmp("-static", opts.args.str[i]) == 0)
  1612. linking = -1;
  1613. else if(strcmp("-save-temps", opts.args.str[i]) == 0)
  1614. keep_generated = 1;
  1615. else if (strncmp("-specs=", opts.args.str[i], 7) == 0)
  1616. raw_linker_arg = 1;
  1617. else if(strcmp("-shared", opts.args.str[i]) == 0)
  1618. {
  1619. opts.shared = 1;
  1620. raw_compiler_arg = raw_linker_arg = 0;
  1621. }
  1622. else if (strcmp("-s", opts.args.str[i]) == 0 && opts.target.platform == PLATFORM_APPLE)
  1623. {
  1624. /* On Mac, change -s into -Wl,-x. ld's -s switch
  1625. * is deprecated, and it doesn't work on Tiger with
  1626. * MH_BUNDLEs anyway
  1627. */
  1628. opts.strip = 1;
  1629. raw_linker_arg = 0;
  1630. }
  1631. break;
  1632. case 't':
  1633. if (is_option( &opts, i, "-target", &option_arg ))
  1634. {
  1635. parse_target_option( &opts, option_arg );
  1636. raw_compiler_arg = raw_linker_arg = 0;
  1637. }
  1638. break;
  1639. case 'v':
  1640. if (opts.args.str[i][2] == 0) verbose++;
  1641. break;
  1642. case 'W':
  1643. if (strncmp("-Wl,", opts.args.str[i], 4) == 0)
  1644. {
  1645. unsigned int j;
  1646. struct strarray Wl = strarray_fromstring(opts.args.str[i] + 4, ",");
  1647. for (j = 0; j < Wl.count; j++)
  1648. {
  1649. if (!strcmp(Wl.str[j], "--image-base") && j < Wl.count - 1)
  1650. {
  1651. opts.image_base = strdup( Wl.str[++j] );
  1652. continue;
  1653. }
  1654. if (!strcmp(Wl.str[j], "--section-alignment") && j < Wl.count - 1)
  1655. {
  1656. opts.section_align = strdup( Wl.str[++j] );
  1657. continue;
  1658. }
  1659. if (!strcmp(Wl.str[j], "--file-alignment") && j < Wl.count - 1)
  1660. {
  1661. opts.file_align = strdup( Wl.str[++j] );
  1662. continue;
  1663. }
  1664. if (!strcmp(Wl.str[j], "--large-address-aware"))
  1665. {
  1666. opts.large_address_aware = 1;
  1667. continue;
  1668. }
  1669. if (!strcmp(Wl.str[j], "--wine-builtin"))
  1670. {
  1671. opts.wine_builtin = 1;
  1672. continue;
  1673. }
  1674. if (!strcmp(Wl.str[j], "--subsystem") && j < Wl.count - 1)
  1675. {
  1676. opts.subsystem = strdup( Wl.str[++j] );
  1677. continue;
  1678. }
  1679. if (!strcmp(Wl.str[j], "--entry") && j < Wl.count - 1)
  1680. {
  1681. opts.entry_point = strdup( Wl.str[++j] );
  1682. continue;
  1683. }
  1684. if (!strcmp(Wl.str[j], "-delayload") && j < Wl.count - 1)
  1685. {
  1686. strarray_add( &opts.delayimports, Wl.str[++j] );
  1687. continue;
  1688. }
  1689. if (!strcmp(Wl.str[j], "--debug-file") && j < Wl.count - 1)
  1690. {
  1691. opts.debug_file = strdup( Wl.str[++j] );
  1692. continue;
  1693. }
  1694. if (!strcmp(Wl.str[j], "--whole-archive") ||
  1695. !strcmp(Wl.str[j], "--no-whole-archive") ||
  1696. !strcmp(Wl.str[j], "--start-group") ||
  1697. !strcmp(Wl.str[j], "--end-group"))
  1698. {
  1699. strarray_add( &opts.files, strmake( "-Wl,%s", Wl.str[j] ));
  1700. continue;
  1701. }
  1702. if (!strcmp(Wl.str[j], "--out-implib"))
  1703. {
  1704. opts.out_implib = strdup( Wl.str[++j] );
  1705. continue;
  1706. }
  1707. if (!strcmp(Wl.str[j], "-static")) linking = -1;
  1708. strarray_add(&opts.linker_args, strmake("-Wl,%s",Wl.str[j]));
  1709. }
  1710. raw_compiler_arg = raw_linker_arg = 0;
  1711. }
  1712. else if (strncmp("-Wb,", opts.args.str[i], 4) == 0)
  1713. {
  1714. unsigned int j;
  1715. struct strarray Wb = strarray_fromstring(opts.args.str[i] + 4, ",");
  1716. for (j = 0; j < Wb.count; j++)
  1717. {
  1718. if (!strcmp(Wb.str[j], "--fake-module")) opts.fake_module = 1;
  1719. else strarray_add( &opts.winebuild_args, Wb.str[j] );
  1720. }
  1721. raw_compiler_arg = raw_linker_arg = 0;
  1722. }
  1723. break;
  1724. case 'x':
  1725. lang = strmake("-x%s", option_arg);
  1726. strarray_add(&opts.files, lang);
  1727. /* we'll pass these flags ourselves, explicitly */
  1728. raw_compiler_arg = raw_linker_arg = 0;
  1729. break;
  1730. case '-':
  1731. if (strcmp("-static", opts.args.str[i]+1) == 0)
  1732. linking = -1;
  1733. else if (is_option( &opts, i, "--sysroot", &option_arg ))
  1734. {
  1735. opts.sysroot = option_arg;
  1736. raw_linker_arg = 1;
  1737. }
  1738. else if (is_option( &opts, i, "--target", &option_arg ))
  1739. {
  1740. parse_target_option( &opts, option_arg );
  1741. raw_compiler_arg = raw_linker_arg = 0;
  1742. }
  1743. else if (is_option( &opts, i, "--wine-objdir", &option_arg ))
  1744. {
  1745. opts.wine_objdir = option_arg;
  1746. raw_compiler_arg = raw_linker_arg = 0;
  1747. }
  1748. else if (is_option( &opts, i, "--winebuild", &option_arg ))
  1749. {
  1750. opts.winebuild = option_arg;
  1751. raw_compiler_arg = raw_linker_arg = 0;
  1752. }
  1753. else if (is_option( &opts, i, "--lib-suffix", &option_arg ))
  1754. {
  1755. opts.lib_suffix = option_arg;
  1756. raw_compiler_arg = raw_linker_arg = 0;
  1757. }
  1758. break;
  1759. }
  1760. /* put the arg into the appropriate bucket */
  1761. if (raw_linker_arg)
  1762. {
  1763. strarray_add( &opts.linker_args, opts.args.str[i] );
  1764. if (next_is_arg && (i + 1 < opts.args.count))
  1765. strarray_add( &opts.linker_args, opts.args.str[i + 1] );
  1766. }
  1767. if (raw_compiler_arg)
  1768. {
  1769. strarray_add( &opts.compiler_args, opts.args.str[i] );
  1770. if (next_is_arg && (i + 1 < opts.args.count))
  1771. strarray_add( &opts.compiler_args, opts.args.str[i + 1] );
  1772. }
  1773. if (raw_winebuild_arg)
  1774. {
  1775. strarray_add( &opts.winebuild_args, opts.args.str[i] );
  1776. if (next_is_arg && (i + 1 < opts.args.count))
  1777. strarray_add( &opts.winebuild_args, opts.args.str[i + 1] );
  1778. }
  1779. /* skip the next token if it's an argument */
  1780. if (next_is_arg) i++;
  1781. }
  1782. else
  1783. {
  1784. strarray_add( &opts.files, opts.args.str[i] );
  1785. }
  1786. }
  1787. if (opts.force_pointer_size) set_target_ptr_size( &opts.target, opts.force_pointer_size );
  1788. if (opts.processor == proc_cpp) linking = 0;
  1789. if (linking == -1) error("Static linking is not supported\n");
  1790. if (!opts.wine_objdir && is_pe_target( &opts )) opts.use_msvcrt = 1;
  1791. if (opts.files.count == 0 && !opts.fake_module) forward(&opts);
  1792. else if (linking) build(&opts);
  1793. else compile(&opts, lang);
  1794. output_file_name = NULL;
  1795. output_debug_file = NULL;
  1796. output_implib = NULL;
  1797. return 0;
  1798. }