winegcc.c 61 KB

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