winegcc.c 71 KB

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