winegcc.c 68 KB

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