winegcc.c 70 KB

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