Tools.cpp 202 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621
  1. //===--- Tools.cpp - Tools Implementations --------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "Tools.h"
  10. #include "clang/Driver/Action.h"
  11. #include "clang/Driver/Arg.h"
  12. #include "clang/Driver/ArgList.h"
  13. #include "clang/Driver/Driver.h"
  14. #include "clang/Driver/DriverDiagnostic.h"
  15. #include "clang/Driver/Compilation.h"
  16. #include "clang/Driver/Job.h"
  17. #include "clang/Driver/ObjCRuntime.h"
  18. #include "clang/Driver/Option.h"
  19. #include "clang/Driver/Options.h"
  20. #include "clang/Driver/ToolChain.h"
  21. #include "clang/Driver/Util.h"
  22. #include "llvm/ADT/SmallString.h"
  23. #include "llvm/ADT/StringSwitch.h"
  24. #include "llvm/ADT/Twine.h"
  25. #include "llvm/Support/FileSystem.h"
  26. #include "llvm/Support/Format.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include "llvm/Support/Host.h"
  29. #include "llvm/Support/Process.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "InputInfo.h"
  32. #include "ToolChains.h"
  33. using namespace clang::driver;
  34. using namespace clang::driver::tools;
  35. using namespace clang;
  36. /// CheckPreprocessingOptions - Perform some validation of preprocessing
  37. /// arguments that is shared with gcc.
  38. static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
  39. if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
  40. if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
  41. D.Diag(diag::err_drv_argument_only_allowed_with)
  42. << A->getAsString(Args) << "-E";
  43. }
  44. /// CheckCodeGenerationOptions - Perform some validation of code generation
  45. /// arguments that is shared with gcc.
  46. static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
  47. // In gcc, only ARM checks this, but it seems reasonable to check universally.
  48. if (Args.hasArg(options::OPT_static))
  49. if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
  50. options::OPT_mdynamic_no_pic))
  51. D.Diag(diag::err_drv_argument_not_allowed_with)
  52. << A->getAsString(Args) << "-static";
  53. }
  54. // Quote target names for inclusion in GNU Make dependency files.
  55. // Only the characters '$', '#', ' ', '\t' are quoted.
  56. static void QuoteTarget(StringRef Target,
  57. SmallVectorImpl<char> &Res) {
  58. for (unsigned i = 0, e = Target.size(); i != e; ++i) {
  59. switch (Target[i]) {
  60. case ' ':
  61. case '\t':
  62. // Escape the preceding backslashes
  63. for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
  64. Res.push_back('\\');
  65. // Escape the space/tab
  66. Res.push_back('\\');
  67. break;
  68. case '$':
  69. Res.push_back('$');
  70. break;
  71. case '#':
  72. Res.push_back('\\');
  73. break;
  74. default:
  75. break;
  76. }
  77. Res.push_back(Target[i]);
  78. }
  79. }
  80. static void addDirectoryList(const ArgList &Args,
  81. ArgStringList &CmdArgs,
  82. const char *ArgName,
  83. const char *EnvVar) {
  84. const char *DirList = ::getenv(EnvVar);
  85. if (!DirList)
  86. return; // Nothing to do.
  87. StringRef Dirs(DirList);
  88. if (Dirs.empty()) // Empty string should not add '.'.
  89. return;
  90. StringRef::size_type Delim;
  91. while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) {
  92. if (Delim == 0) { // Leading colon.
  93. CmdArgs.push_back(ArgName);
  94. CmdArgs.push_back(".");
  95. } else {
  96. CmdArgs.push_back(ArgName);
  97. CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
  98. }
  99. Dirs = Dirs.substr(Delim + 1);
  100. }
  101. if (Dirs.empty()) { // Trailing colon.
  102. CmdArgs.push_back(ArgName);
  103. CmdArgs.push_back(".");
  104. } else { // Add the last path.
  105. CmdArgs.push_back(ArgName);
  106. CmdArgs.push_back(Args.MakeArgString(Dirs));
  107. }
  108. }
  109. static void AddLinkerInputs(const ToolChain &TC,
  110. const InputInfoList &Inputs, const ArgList &Args,
  111. ArgStringList &CmdArgs) {
  112. const Driver &D = TC.getDriver();
  113. // Add extra linker input arguments which are not treated as inputs
  114. // (constructed via -Xarch_).
  115. Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
  116. for (InputInfoList::const_iterator
  117. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  118. const InputInfo &II = *it;
  119. if (!TC.HasNativeLLVMSupport()) {
  120. // Don't try to pass LLVM inputs unless we have native support.
  121. if (II.getType() == types::TY_LLVM_IR ||
  122. II.getType() == types::TY_LTO_IR ||
  123. II.getType() == types::TY_LLVM_BC ||
  124. II.getType() == types::TY_LTO_BC)
  125. D.Diag(diag::err_drv_no_linker_llvm_support)
  126. << TC.getTripleString();
  127. }
  128. // Add filenames immediately.
  129. if (II.isFilename()) {
  130. CmdArgs.push_back(II.getFilename());
  131. continue;
  132. }
  133. // Otherwise, this is a linker input argument.
  134. const Arg &A = II.getInputArg();
  135. // Handle reserved library options.
  136. if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
  137. TC.AddCXXStdlibLibArgs(Args, CmdArgs);
  138. } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
  139. TC.AddCCKextLibArgs(Args, CmdArgs);
  140. } else
  141. A.renderAsInput(Args, CmdArgs);
  142. }
  143. // LIBRARY_PATH - included following the user specified library paths.
  144. addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
  145. }
  146. /// \brief Determine whether Objective-C automated reference counting is
  147. /// enabled.
  148. static bool isObjCAutoRefCount(const ArgList &Args) {
  149. return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
  150. }
  151. /// \brief Determine whether we are linking the ObjC runtime.
  152. static bool isObjCRuntimeLinked(const ArgList &Args) {
  153. if (isObjCAutoRefCount(Args))
  154. return true;
  155. return Args.hasArg(options::OPT_fobjc_link_runtime);
  156. }
  157. static void addProfileRT(const ToolChain &TC, const ArgList &Args,
  158. ArgStringList &CmdArgs,
  159. llvm::Triple Triple) {
  160. if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
  161. Args.hasArg(options::OPT_fprofile_generate) ||
  162. Args.hasArg(options::OPT_fcreate_profile) ||
  163. Args.hasArg(options::OPT_coverage)))
  164. return;
  165. // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
  166. // the link line. We cannot do the same thing because unlike gcov there is a
  167. // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
  168. // not supported by old linkers.
  169. std::string ProfileRT =
  170. std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a";
  171. CmdArgs.push_back(Args.MakeArgString(ProfileRT));
  172. }
  173. void Clang::AddPreprocessingOptions(Compilation &C,
  174. const Driver &D,
  175. const ArgList &Args,
  176. ArgStringList &CmdArgs,
  177. const InputInfo &Output,
  178. const InputInfoList &Inputs) const {
  179. Arg *A;
  180. CheckPreprocessingOptions(D, Args);
  181. Args.AddLastArg(CmdArgs, options::OPT_C);
  182. Args.AddLastArg(CmdArgs, options::OPT_CC);
  183. // Handle dependency file generation.
  184. if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
  185. (A = Args.getLastArg(options::OPT_MD)) ||
  186. (A = Args.getLastArg(options::OPT_MMD))) {
  187. // Determine the output location.
  188. const char *DepFile;
  189. if (Output.getType() == types::TY_Dependencies) {
  190. DepFile = Output.getFilename();
  191. } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
  192. DepFile = MF->getValue(Args);
  193. C.addFailureResultFile(DepFile);
  194. } else if (A->getOption().matches(options::OPT_M) ||
  195. A->getOption().matches(options::OPT_MM)) {
  196. DepFile = "-";
  197. } else {
  198. DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
  199. C.addFailureResultFile(DepFile);
  200. }
  201. CmdArgs.push_back("-dependency-file");
  202. CmdArgs.push_back(DepFile);
  203. // Add a default target if one wasn't specified.
  204. if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
  205. const char *DepTarget;
  206. // If user provided -o, that is the dependency target, except
  207. // when we are only generating a dependency file.
  208. Arg *OutputOpt = Args.getLastArg(options::OPT_o);
  209. if (OutputOpt && Output.getType() != types::TY_Dependencies) {
  210. DepTarget = OutputOpt->getValue(Args);
  211. } else {
  212. // Otherwise derive from the base input.
  213. //
  214. // FIXME: This should use the computed output file location.
  215. SmallString<128> P(Inputs[0].getBaseInput());
  216. llvm::sys::path::replace_extension(P, "o");
  217. DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
  218. }
  219. CmdArgs.push_back("-MT");
  220. SmallString<128> Quoted;
  221. QuoteTarget(DepTarget, Quoted);
  222. CmdArgs.push_back(Args.MakeArgString(Quoted));
  223. }
  224. if (A->getOption().matches(options::OPT_M) ||
  225. A->getOption().matches(options::OPT_MD))
  226. CmdArgs.push_back("-sys-header-deps");
  227. }
  228. if (Args.hasArg(options::OPT_MG)) {
  229. if (!A || A->getOption().matches(options::OPT_MD) ||
  230. A->getOption().matches(options::OPT_MMD))
  231. D.Diag(diag::err_drv_mg_requires_m_or_mm);
  232. CmdArgs.push_back("-MG");
  233. }
  234. Args.AddLastArg(CmdArgs, options::OPT_MP);
  235. // Convert all -MQ <target> args to -MT <quoted target>
  236. for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
  237. options::OPT_MQ),
  238. ie = Args.filtered_end(); it != ie; ++it) {
  239. const Arg *A = *it;
  240. A->claim();
  241. if (A->getOption().matches(options::OPT_MQ)) {
  242. CmdArgs.push_back("-MT");
  243. SmallString<128> Quoted;
  244. QuoteTarget(A->getValue(Args), Quoted);
  245. CmdArgs.push_back(Args.MakeArgString(Quoted));
  246. // -MT flag - no change
  247. } else {
  248. A->render(Args, CmdArgs);
  249. }
  250. }
  251. // Add -i* options, and automatically translate to
  252. // -include-pch/-include-pth for transparent PCH support. It's
  253. // wonky, but we include looking for .gch so we can support seamless
  254. // replacement into a build system already set up to be generating
  255. // .gch files.
  256. bool RenderedImplicitInclude = false;
  257. for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
  258. ie = Args.filtered_end(); it != ie; ++it) {
  259. const Arg *A = it;
  260. if (A->getOption().matches(options::OPT_include)) {
  261. bool IsFirstImplicitInclude = !RenderedImplicitInclude;
  262. RenderedImplicitInclude = true;
  263. // Use PCH if the user requested it.
  264. bool UsePCH = D.CCCUsePCH;
  265. bool FoundPTH = false;
  266. bool FoundPCH = false;
  267. llvm::sys::Path P(A->getValue(Args));
  268. bool Exists;
  269. if (UsePCH) {
  270. P.appendSuffix("pch");
  271. if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
  272. FoundPCH = true;
  273. else
  274. P.eraseSuffix();
  275. }
  276. if (!FoundPCH) {
  277. P.appendSuffix("pth");
  278. if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
  279. FoundPTH = true;
  280. else
  281. P.eraseSuffix();
  282. }
  283. if (!FoundPCH && !FoundPTH) {
  284. P.appendSuffix("gch");
  285. if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
  286. FoundPCH = UsePCH;
  287. FoundPTH = !UsePCH;
  288. }
  289. else
  290. P.eraseSuffix();
  291. }
  292. if (FoundPCH || FoundPTH) {
  293. if (IsFirstImplicitInclude) {
  294. A->claim();
  295. if (UsePCH)
  296. CmdArgs.push_back("-include-pch");
  297. else
  298. CmdArgs.push_back("-include-pth");
  299. CmdArgs.push_back(Args.MakeArgString(P.str()));
  300. continue;
  301. } else {
  302. // Ignore the PCH if not first on command line and emit warning.
  303. D.Diag(diag::warn_drv_pch_not_first_include)
  304. << P.str() << A->getAsString(Args);
  305. }
  306. }
  307. }
  308. // Not translated, render as usual.
  309. A->claim();
  310. A->render(Args, CmdArgs);
  311. }
  312. Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
  313. Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
  314. options::OPT_index_header_map);
  315. // Add -Wp, and -Xassembler if using the preprocessor.
  316. // FIXME: There is a very unfortunate problem here, some troubled
  317. // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
  318. // really support that we would have to parse and then translate
  319. // those options. :(
  320. Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
  321. options::OPT_Xpreprocessor);
  322. // -I- is a deprecated GCC feature, reject it.
  323. if (Arg *A = Args.getLastArg(options::OPT_I_))
  324. D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
  325. // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
  326. // -isysroot to the CC1 invocation.
  327. StringRef sysroot = C.getSysRoot();
  328. if (sysroot != "") {
  329. if (!Args.hasArg(options::OPT_isysroot)) {
  330. CmdArgs.push_back("-isysroot");
  331. CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
  332. }
  333. }
  334. // If a module path was provided, pass it along. Otherwise, use a temporary
  335. // directory.
  336. if (Arg *A = Args.getLastArg(options::OPT_fmodule_cache_path)) {
  337. A->claim();
  338. A->render(Args, CmdArgs);
  339. } else {
  340. SmallString<128> DefaultModuleCache;
  341. llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
  342. DefaultModuleCache);
  343. llvm::sys::path::append(DefaultModuleCache, "clang-module-cache");
  344. CmdArgs.push_back("-fmodule-cache-path");
  345. CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
  346. }
  347. // Parse additional include paths from environment variables.
  348. // FIXME: We should probably sink the logic for handling these from the
  349. // frontend into the driver. It will allow deleting 4 otherwise unused flags.
  350. // CPATH - included following the user specified includes (but prior to
  351. // builtin and standard includes).
  352. addDirectoryList(Args, CmdArgs, "-I", "CPATH");
  353. // C_INCLUDE_PATH - system includes enabled when compiling C.
  354. addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
  355. // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
  356. addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
  357. // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
  358. addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
  359. // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
  360. addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
  361. // Add C++ include arguments, if needed.
  362. if (types::isCXX(Inputs[0].getType()))
  363. getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
  364. // Add system include arguments.
  365. getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
  366. }
  367. /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
  368. //
  369. // FIXME: tblgen this.
  370. static const char *getARMTargetCPU(const ArgList &Args,
  371. const llvm::Triple &Triple) {
  372. // FIXME: Warn on inconsistent use of -mcpu and -march.
  373. // If we have -mcpu=, use that.
  374. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
  375. return A->getValue(Args);
  376. StringRef MArch;
  377. if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  378. // Otherwise, if we have -march= choose the base CPU for that arch.
  379. MArch = A->getValue(Args);
  380. } else {
  381. // Otherwise, use the Arch from the triple.
  382. MArch = Triple.getArchName();
  383. }
  384. return llvm::StringSwitch<const char *>(MArch)
  385. .Cases("armv2", "armv2a","arm2")
  386. .Case("armv3", "arm6")
  387. .Case("armv3m", "arm7m")
  388. .Cases("armv4", "armv4t", "arm7tdmi")
  389. .Cases("armv5", "armv5t", "arm10tdmi")
  390. .Cases("armv5e", "armv5te", "arm1022e")
  391. .Case("armv5tej", "arm926ej-s")
  392. .Cases("armv6", "armv6k", "arm1136jf-s")
  393. .Case("armv6j", "arm1136j-s")
  394. .Cases("armv6z", "armv6zk", "arm1176jzf-s")
  395. .Case("armv6t2", "arm1156t2-s")
  396. .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
  397. .Cases("armv7r", "armv7-r", "cortex-r4")
  398. .Cases("armv7m", "armv7-m", "cortex-m3")
  399. .Case("ep9312", "ep9312")
  400. .Case("iwmmxt", "iwmmxt")
  401. .Case("xscale", "xscale")
  402. .Cases("armv6m", "armv6-m", "cortex-m0")
  403. // If all else failed, return the most base CPU LLVM supports.
  404. .Default("arm7tdmi");
  405. }
  406. /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
  407. /// CPU.
  408. //
  409. // FIXME: This is redundant with -mcpu, why does LLVM use this.
  410. // FIXME: tblgen this, or kill it!
  411. static const char *getLLVMArchSuffixForARM(StringRef CPU) {
  412. return llvm::StringSwitch<const char *>(CPU)
  413. .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
  414. .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
  415. .Cases("arm920", "arm920t", "arm922t", "v4t")
  416. .Cases("arm940t", "ep9312","v4t")
  417. .Cases("arm10tdmi", "arm1020t", "v5")
  418. .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
  419. .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
  420. .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
  421. .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
  422. .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
  423. .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
  424. .Cases("cortex-a8", "cortex-a9", "v7")
  425. .Case("cortex-m3", "v7m")
  426. .Case("cortex-m4", "v7m")
  427. .Case("cortex-m0", "v6m")
  428. .Default("");
  429. }
  430. // FIXME: Move to target hook.
  431. static bool isSignedCharDefault(const llvm::Triple &Triple) {
  432. switch (Triple.getArch()) {
  433. default:
  434. return true;
  435. case llvm::Triple::arm:
  436. case llvm::Triple::ppc:
  437. case llvm::Triple::ppc64:
  438. if (Triple.isOSDarwin())
  439. return true;
  440. return false;
  441. }
  442. }
  443. // Handle -mfpu=.
  444. //
  445. // FIXME: Centralize feature selection, defaulting shouldn't be also in the
  446. // frontend target.
  447. static void addFPUArgs(const Driver &D, const Arg *A, const ArgList &Args,
  448. ArgStringList &CmdArgs) {
  449. StringRef FPU = A->getValue(Args);
  450. // Set the target features based on the FPU.
  451. if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
  452. // Disable any default FPU support.
  453. CmdArgs.push_back("-target-feature");
  454. CmdArgs.push_back("-vfp2");
  455. CmdArgs.push_back("-target-feature");
  456. CmdArgs.push_back("-vfp3");
  457. CmdArgs.push_back("-target-feature");
  458. CmdArgs.push_back("-neon");
  459. } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
  460. CmdArgs.push_back("-target-feature");
  461. CmdArgs.push_back("+vfp3");
  462. CmdArgs.push_back("-target-feature");
  463. CmdArgs.push_back("+d16");
  464. CmdArgs.push_back("-target-feature");
  465. CmdArgs.push_back("-neon");
  466. } else if (FPU == "vfp") {
  467. CmdArgs.push_back("-target-feature");
  468. CmdArgs.push_back("+vfp2");
  469. CmdArgs.push_back("-target-feature");
  470. CmdArgs.push_back("-neon");
  471. } else if (FPU == "vfp3" || FPU == "vfpv3") {
  472. CmdArgs.push_back("-target-feature");
  473. CmdArgs.push_back("+vfp3");
  474. CmdArgs.push_back("-target-feature");
  475. CmdArgs.push_back("-neon");
  476. } else if (FPU == "neon") {
  477. CmdArgs.push_back("-target-feature");
  478. CmdArgs.push_back("+neon");
  479. } else
  480. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  481. }
  482. // Handle -mfpmath=.
  483. static void addFPMathArgs(const Driver &D, const Arg *A, const ArgList &Args,
  484. ArgStringList &CmdArgs, StringRef CPU) {
  485. StringRef FPMath = A->getValue(Args);
  486. // Set the target features based on the FPMath.
  487. if (FPMath == "neon") {
  488. CmdArgs.push_back("-target-feature");
  489. CmdArgs.push_back("+neonfp");
  490. if (CPU != "cortex-a8" && CPU != "cortex-a9" && CPU != "cortex-a9-mp")
  491. D.Diag(diag::err_drv_invalid_feature) << "-mfpmath=neon" << CPU;
  492. } else if (FPMath == "vfp" || FPMath == "vfp2" || FPMath == "vfp3" ||
  493. FPMath == "vfp4") {
  494. CmdArgs.push_back("-target-feature");
  495. CmdArgs.push_back("-neonfp");
  496. // FIXME: Add warnings when disabling a feature not present for a given CPU.
  497. } else
  498. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  499. }
  500. // Select the float ABI as determined by -msoft-float, -mhard-float, and
  501. // -mfloat-abi=.
  502. static StringRef getARMFloatABI(const Driver &D,
  503. const ArgList &Args,
  504. const llvm::Triple &Triple) {
  505. StringRef FloatABI;
  506. if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
  507. options::OPT_mhard_float,
  508. options::OPT_mfloat_abi_EQ)) {
  509. if (A->getOption().matches(options::OPT_msoft_float))
  510. FloatABI = "soft";
  511. else if (A->getOption().matches(options::OPT_mhard_float))
  512. FloatABI = "hard";
  513. else {
  514. FloatABI = A->getValue(Args);
  515. if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
  516. D.Diag(diag::err_drv_invalid_mfloat_abi)
  517. << A->getAsString(Args);
  518. FloatABI = "soft";
  519. }
  520. }
  521. }
  522. // If unspecified, choose the default based on the platform.
  523. if (FloatABI.empty()) {
  524. switch (Triple.getOS()) {
  525. case llvm::Triple::Darwin:
  526. case llvm::Triple::MacOSX:
  527. case llvm::Triple::IOS: {
  528. // Darwin defaults to "softfp" for v6 and v7.
  529. //
  530. // FIXME: Factor out an ARM class so we can cache the arch somewhere.
  531. StringRef ArchName =
  532. getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
  533. if (ArchName.startswith("v6") || ArchName.startswith("v7"))
  534. FloatABI = "softfp";
  535. else
  536. FloatABI = "soft";
  537. break;
  538. }
  539. case llvm::Triple::Linux: {
  540. if (Triple.getEnvironment() == llvm::Triple::GNUEABI) {
  541. FloatABI = "softfp";
  542. break;
  543. }
  544. }
  545. // fall through
  546. default:
  547. switch(Triple.getEnvironment()) {
  548. case llvm::Triple::GNUEABI:
  549. FloatABI = "softfp";
  550. break;
  551. case llvm::Triple::EABI:
  552. // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
  553. FloatABI = "softfp";
  554. break;
  555. case llvm::Triple::ANDROIDEABI: {
  556. StringRef ArchName =
  557. getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
  558. if (ArchName.startswith("v7"))
  559. FloatABI = "softfp";
  560. else
  561. FloatABI = "soft";
  562. break;
  563. }
  564. default:
  565. // Assume "soft", but warn the user we are guessing.
  566. FloatABI = "soft";
  567. D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
  568. break;
  569. }
  570. }
  571. }
  572. return FloatABI;
  573. }
  574. void Clang::AddARMTargetArgs(const ArgList &Args,
  575. ArgStringList &CmdArgs,
  576. bool KernelOrKext) const {
  577. const Driver &D = getToolChain().getDriver();
  578. llvm::Triple Triple = getToolChain().getTriple();
  579. // Select the ABI to use.
  580. //
  581. // FIXME: Support -meabi.
  582. const char *ABIName = 0;
  583. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
  584. ABIName = A->getValue(Args);
  585. } else {
  586. // Select the default based on the platform.
  587. switch(Triple.getEnvironment()) {
  588. case llvm::Triple::ANDROIDEABI:
  589. case llvm::Triple::GNUEABI:
  590. ABIName = "aapcs-linux";
  591. break;
  592. case llvm::Triple::EABI:
  593. ABIName = "aapcs";
  594. break;
  595. default:
  596. ABIName = "apcs-gnu";
  597. }
  598. }
  599. CmdArgs.push_back("-target-abi");
  600. CmdArgs.push_back(ABIName);
  601. // Set the CPU based on -march= and -mcpu=.
  602. CmdArgs.push_back("-target-cpu");
  603. CmdArgs.push_back(getARMTargetCPU(Args, Triple));
  604. // Determine floating point ABI from the options & target defaults.
  605. StringRef FloatABI = getARMFloatABI(D, Args, Triple);
  606. if (FloatABI == "soft") {
  607. // Floating point operations and argument passing are soft.
  608. //
  609. // FIXME: This changes CPP defines, we need -target-soft-float.
  610. CmdArgs.push_back("-msoft-float");
  611. CmdArgs.push_back("-mfloat-abi");
  612. CmdArgs.push_back("soft");
  613. } else if (FloatABI == "softfp") {
  614. // Floating point operations are hard, but argument passing is soft.
  615. CmdArgs.push_back("-mfloat-abi");
  616. CmdArgs.push_back("soft");
  617. } else {
  618. // Floating point operations and argument passing are hard.
  619. assert(FloatABI == "hard" && "Invalid float abi!");
  620. CmdArgs.push_back("-mfloat-abi");
  621. CmdArgs.push_back("hard");
  622. }
  623. // Set appropriate target features for floating point mode.
  624. //
  625. // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
  626. // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
  627. // stripped out by the ARM target.
  628. // Use software floating point operations?
  629. if (FloatABI == "soft") {
  630. CmdArgs.push_back("-target-feature");
  631. CmdArgs.push_back("+soft-float");
  632. }
  633. // Use software floating point argument passing?
  634. if (FloatABI != "hard") {
  635. CmdArgs.push_back("-target-feature");
  636. CmdArgs.push_back("+soft-float-abi");
  637. }
  638. // Honor -mfpu=.
  639. if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
  640. addFPUArgs(D, A, Args, CmdArgs);
  641. // Honor -mfpmath=.
  642. if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
  643. addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
  644. // Setting -msoft-float effectively disables NEON because of the GCC
  645. // implementation, although the same isn't true of VFP or VFP3.
  646. if (FloatABI == "soft") {
  647. CmdArgs.push_back("-target-feature");
  648. CmdArgs.push_back("-neon");
  649. }
  650. // Kernel code has more strict alignment requirements.
  651. if (KernelOrKext) {
  652. CmdArgs.push_back("-backend-option");
  653. CmdArgs.push_back("-arm-long-calls");
  654. CmdArgs.push_back("-backend-option");
  655. CmdArgs.push_back("-arm-strict-align");
  656. // The kext linker doesn't know how to deal with movw/movt.
  657. CmdArgs.push_back("-backend-option");
  658. CmdArgs.push_back("-arm-darwin-use-movt=0");
  659. }
  660. // Setting -mno-global-merge disables the codegen global merge pass. Setting
  661. // -mglobal-merge has no effect as the pass is enabled by default.
  662. if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
  663. options::OPT_mno_global_merge)) {
  664. if (A->getOption().matches(options::OPT_mno_global_merge))
  665. CmdArgs.push_back("-mno-global-merge");
  666. }
  667. }
  668. // Get default architecture.
  669. static const char* getMipsArchFromCPU(StringRef CPUName) {
  670. if (CPUName == "mips32" || CPUName == "mips32r2")
  671. return "mips";
  672. assert((CPUName == "mips64" || CPUName == "mips64r2") &&
  673. "Unexpected cpu name.");
  674. return "mips64";
  675. }
  676. // Check that ArchName is a known Mips architecture name.
  677. static bool checkMipsArchName(StringRef ArchName) {
  678. return ArchName == "mips" ||
  679. ArchName == "mipsel" ||
  680. ArchName == "mips64" ||
  681. ArchName == "mips64el";
  682. }
  683. // Get default target cpu.
  684. static const char* getMipsCPUFromArch(StringRef ArchName) {
  685. if (ArchName == "mips" || ArchName == "mipsel")
  686. return "mips32";
  687. assert((ArchName == "mips64" || ArchName == "mips64el") &&
  688. "Unexpected arch name.");
  689. return "mips64";
  690. }
  691. // Get default ABI.
  692. static const char* getMipsABIFromArch(StringRef ArchName) {
  693. if (ArchName == "mips" || ArchName == "mipsel")
  694. return "o32";
  695. assert((ArchName == "mips64" || ArchName == "mips64el") &&
  696. "Unexpected arch name.");
  697. return "n64";
  698. }
  699. // Get CPU and ABI names. They are not independent
  700. // so we have to calculate them together.
  701. static void getMipsCPUAndABI(const ArgList &Args,
  702. const ToolChain &TC,
  703. StringRef &CPUName,
  704. StringRef &ABIName) {
  705. StringRef ArchName;
  706. // Select target cpu and architecture.
  707. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
  708. CPUName = A->getValue(Args);
  709. ArchName = getMipsArchFromCPU(CPUName);
  710. }
  711. else {
  712. ArchName = Args.MakeArgString(TC.getArchName());
  713. if (!checkMipsArchName(ArchName))
  714. TC.getDriver().Diag(diag::err_drv_invalid_arch_name) << ArchName;
  715. else
  716. CPUName = getMipsCPUFromArch(ArchName);
  717. }
  718. // Select the ABI to use.
  719. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
  720. ABIName = A->getValue(Args);
  721. else
  722. ABIName = getMipsABIFromArch(ArchName);
  723. }
  724. void Clang::AddMIPSTargetArgs(const ArgList &Args,
  725. ArgStringList &CmdArgs) const {
  726. const Driver &D = getToolChain().getDriver();
  727. StringRef CPUName;
  728. StringRef ABIName;
  729. getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
  730. CmdArgs.push_back("-target-cpu");
  731. CmdArgs.push_back(CPUName.data());
  732. CmdArgs.push_back("-target-abi");
  733. CmdArgs.push_back(ABIName.data());
  734. // Select the float ABI as determined by -msoft-float, -mhard-float,
  735. // and -mfloat-abi=.
  736. StringRef FloatABI;
  737. if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
  738. options::OPT_mhard_float,
  739. options::OPT_mfloat_abi_EQ)) {
  740. if (A->getOption().matches(options::OPT_msoft_float))
  741. FloatABI = "soft";
  742. else if (A->getOption().matches(options::OPT_mhard_float))
  743. FloatABI = "hard";
  744. else {
  745. FloatABI = A->getValue(Args);
  746. if (FloatABI != "soft" && FloatABI != "single" && FloatABI != "hard") {
  747. D.Diag(diag::err_drv_invalid_mfloat_abi)
  748. << A->getAsString(Args);
  749. FloatABI = "hard";
  750. }
  751. }
  752. }
  753. // If unspecified, choose the default based on the platform.
  754. if (FloatABI.empty()) {
  755. // Assume "hard", because it's a default value used by gcc.
  756. // When we start to recognize specific target MIPS processors,
  757. // we will be able to select the default more correctly.
  758. FloatABI = "hard";
  759. }
  760. if (FloatABI == "soft") {
  761. // Floating point operations and argument passing are soft.
  762. CmdArgs.push_back("-msoft-float");
  763. CmdArgs.push_back("-mfloat-abi");
  764. CmdArgs.push_back("soft");
  765. // FIXME: Note, this is a hack. We need to pass the selected float
  766. // mode to the MipsTargetInfoBase to define appropriate macros there.
  767. // Now it is the only method.
  768. CmdArgs.push_back("-target-feature");
  769. CmdArgs.push_back("+soft-float");
  770. }
  771. else if (FloatABI == "single") {
  772. // Restrict the use of hardware floating-point
  773. // instructions to 32-bit operations.
  774. CmdArgs.push_back("-target-feature");
  775. CmdArgs.push_back("+single-float");
  776. }
  777. else {
  778. // Floating point operations and argument passing are hard.
  779. assert(FloatABI == "hard" && "Invalid float abi!");
  780. CmdArgs.push_back("-mfloat-abi");
  781. CmdArgs.push_back("hard");
  782. }
  783. }
  784. void Clang::AddSparcTargetArgs(const ArgList &Args,
  785. ArgStringList &CmdArgs) const {
  786. const Driver &D = getToolChain().getDriver();
  787. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  788. CmdArgs.push_back("-target-cpu");
  789. CmdArgs.push_back(A->getValue(Args));
  790. }
  791. // Select the float ABI as determined by -msoft-float, -mhard-float, and
  792. StringRef FloatABI;
  793. if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
  794. options::OPT_mhard_float)) {
  795. if (A->getOption().matches(options::OPT_msoft_float))
  796. FloatABI = "soft";
  797. else if (A->getOption().matches(options::OPT_mhard_float))
  798. FloatABI = "hard";
  799. }
  800. // If unspecified, choose the default based on the platform.
  801. if (FloatABI.empty()) {
  802. switch (getToolChain().getTriple().getOS()) {
  803. default:
  804. // Assume "soft", but warn the user we are guessing.
  805. FloatABI = "soft";
  806. D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
  807. break;
  808. }
  809. }
  810. if (FloatABI == "soft") {
  811. // Floating point operations and argument passing are soft.
  812. //
  813. // FIXME: This changes CPP defines, we need -target-soft-float.
  814. CmdArgs.push_back("-msoft-float");
  815. CmdArgs.push_back("-target-feature");
  816. CmdArgs.push_back("+soft-float");
  817. } else {
  818. assert(FloatABI == "hard" && "Invalid float abi!");
  819. CmdArgs.push_back("-mhard-float");
  820. }
  821. }
  822. void Clang::AddX86TargetArgs(const ArgList &Args,
  823. ArgStringList &CmdArgs) const {
  824. if (!Args.hasFlag(options::OPT_mred_zone,
  825. options::OPT_mno_red_zone,
  826. true) ||
  827. Args.hasArg(options::OPT_mkernel) ||
  828. Args.hasArg(options::OPT_fapple_kext))
  829. CmdArgs.push_back("-disable-red-zone");
  830. if (Args.hasFlag(options::OPT_msoft_float,
  831. options::OPT_mno_soft_float,
  832. false))
  833. CmdArgs.push_back("-no-implicit-float");
  834. const char *CPUName = 0;
  835. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  836. if (StringRef(A->getValue(Args)) == "native") {
  837. // FIXME: Reject attempts to use -march=native unless the target matches
  838. // the host.
  839. //
  840. // FIXME: We should also incorporate the detected target features for use
  841. // with -native.
  842. std::string CPU = llvm::sys::getHostCPUName();
  843. if (!CPU.empty())
  844. CPUName = Args.MakeArgString(CPU);
  845. } else
  846. CPUName = A->getValue(Args);
  847. }
  848. // Select the default CPU if none was given (or detection failed).
  849. if (!CPUName) {
  850. // FIXME: Need target hooks.
  851. if (getToolChain().getTriple().isOSDarwin()) {
  852. if (getToolChain().getArch() == llvm::Triple::x86_64)
  853. CPUName = "core2";
  854. else if (getToolChain().getArch() == llvm::Triple::x86)
  855. CPUName = "yonah";
  856. } else if (getToolChain().getOS().startswith("haiku")) {
  857. if (getToolChain().getArch() == llvm::Triple::x86_64)
  858. CPUName = "x86-64";
  859. else if (getToolChain().getArch() == llvm::Triple::x86)
  860. CPUName = "i586";
  861. } else if (getToolChain().getOS().startswith("openbsd")) {
  862. if (getToolChain().getArch() == llvm::Triple::x86_64)
  863. CPUName = "x86-64";
  864. else if (getToolChain().getArch() == llvm::Triple::x86)
  865. CPUName = "i486";
  866. } else if (getToolChain().getOS().startswith("freebsd")) {
  867. if (getToolChain().getArch() == llvm::Triple::x86_64)
  868. CPUName = "x86-64";
  869. else if (getToolChain().getArch() == llvm::Triple::x86)
  870. CPUName = "i486";
  871. } else if (getToolChain().getOS().startswith("netbsd")) {
  872. if (getToolChain().getArch() == llvm::Triple::x86_64)
  873. CPUName = "x86-64";
  874. else if (getToolChain().getArch() == llvm::Triple::x86)
  875. CPUName = "i486";
  876. } else {
  877. if (getToolChain().getArch() == llvm::Triple::x86_64)
  878. CPUName = "x86-64";
  879. else if (getToolChain().getArch() == llvm::Triple::x86)
  880. CPUName = "pentium4";
  881. }
  882. }
  883. if (CPUName) {
  884. CmdArgs.push_back("-target-cpu");
  885. CmdArgs.push_back(CPUName);
  886. }
  887. // The required algorithm here is slightly strange: the options are applied
  888. // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
  889. // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
  890. // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
  891. // former correctly, but not the latter; handle directly-overridden
  892. // attributes here.
  893. llvm::StringMap<unsigned> PrevFeature;
  894. std::vector<const char*> Features;
  895. for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
  896. ie = Args.filtered_end(); it != ie; ++it) {
  897. StringRef Name = (*it)->getOption().getName();
  898. (*it)->claim();
  899. // Skip over "-m".
  900. assert(Name.startswith("-m") && "Invalid feature name.");
  901. Name = Name.substr(2);
  902. bool IsNegative = Name.startswith("no-");
  903. if (IsNegative)
  904. Name = Name.substr(3);
  905. unsigned& Prev = PrevFeature[Name];
  906. if (Prev)
  907. Features[Prev - 1] = 0;
  908. Prev = Features.size() + 1;
  909. Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
  910. }
  911. for (unsigned i = 0; i < Features.size(); i++) {
  912. if (Features[i]) {
  913. CmdArgs.push_back("-target-feature");
  914. CmdArgs.push_back(Features[i]);
  915. }
  916. }
  917. }
  918. static Arg* getLastHexagonArchArg (const ArgList &Args)
  919. {
  920. Arg * A = NULL;
  921. for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
  922. it != ie; ++it) {
  923. if ((*it)->getOption().matches(options::OPT_march_EQ) ||
  924. (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
  925. A = *it;
  926. A->claim();
  927. }
  928. else if ((*it)->getOption().matches(options::OPT_m_Joined)){
  929. StringRef Value = (*it)->getValue(Args,0);
  930. if (Value.startswith("v")) {
  931. A = *it;
  932. A->claim();
  933. }
  934. }
  935. }
  936. return A;
  937. }
  938. static StringRef getHexagonTargetCPU(const ArgList &Args)
  939. {
  940. Arg *A;
  941. llvm::StringRef WhichHexagon;
  942. // Select the default CPU (v4) if none was given or detection failed.
  943. if ((A = getLastHexagonArchArg (Args))) {
  944. WhichHexagon = A->getValue(Args);
  945. if (WhichHexagon == "")
  946. return "v4";
  947. else
  948. return WhichHexagon;
  949. }
  950. else
  951. return "v4";
  952. }
  953. void Clang::AddHexagonTargetArgs(const ArgList &Args,
  954. ArgStringList &CmdArgs) const {
  955. llvm::Triple Triple = getToolChain().getTriple();
  956. CmdArgs.push_back("-target-cpu");
  957. CmdArgs.push_back(Args.MakeArgString("hexagon" + getHexagonTargetCPU(Args)));
  958. CmdArgs.push_back("-fno-signed-char");
  959. CmdArgs.push_back("-nobuiltininc");
  960. if (Args.hasArg(options::OPT_mqdsp6_compat))
  961. CmdArgs.push_back("-mqdsp6-compat");
  962. if (Arg *A = Args.getLastArg(options::OPT_G,
  963. options::OPT_msmall_data_threshold_EQ)) {
  964. std::string SmallDataThreshold="-small-data-threshold=";
  965. SmallDataThreshold += A->getValue(Args);
  966. CmdArgs.push_back ("-mllvm");
  967. CmdArgs.push_back(Args.MakeArgString(SmallDataThreshold));
  968. A->claim();
  969. }
  970. CmdArgs.push_back ("-mllvm");
  971. CmdArgs.push_back ("-machine-sink-split=0");
  972. }
  973. static bool
  974. shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
  975. const llvm::Triple &Triple) {
  976. // We use the zero-cost exception tables for Objective-C if the non-fragile
  977. // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
  978. // later.
  979. if (objcABIVersion >= 2)
  980. return true;
  981. if (!Triple.isOSDarwin())
  982. return false;
  983. return (!Triple.isMacOSXVersionLT(10,5) &&
  984. (Triple.getArch() == llvm::Triple::x86_64 ||
  985. Triple.getArch() == llvm::Triple::arm));
  986. }
  987. /// addExceptionArgs - Adds exception related arguments to the driver command
  988. /// arguments. There's a master flag, -fexceptions and also language specific
  989. /// flags to enable/disable C++ and Objective-C exceptions.
  990. /// This makes it possible to for example disable C++ exceptions but enable
  991. /// Objective-C exceptions.
  992. static void addExceptionArgs(const ArgList &Args, types::ID InputType,
  993. const llvm::Triple &Triple,
  994. bool KernelOrKext,
  995. unsigned objcABIVersion,
  996. ArgStringList &CmdArgs) {
  997. if (KernelOrKext) {
  998. // -mkernel and -fapple-kext imply no exceptions, so claim exception related
  999. // arguments now to avoid warnings about unused arguments.
  1000. Args.ClaimAllArgs(options::OPT_fexceptions);
  1001. Args.ClaimAllArgs(options::OPT_fno_exceptions);
  1002. Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
  1003. Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
  1004. Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
  1005. Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
  1006. return;
  1007. }
  1008. // Exceptions are enabled by default.
  1009. bool ExceptionsEnabled = true;
  1010. // This keeps track of whether exceptions were explicitly turned on or off.
  1011. bool DidHaveExplicitExceptionFlag = false;
  1012. if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
  1013. options::OPT_fno_exceptions)) {
  1014. if (A->getOption().matches(options::OPT_fexceptions))
  1015. ExceptionsEnabled = true;
  1016. else
  1017. ExceptionsEnabled = false;
  1018. DidHaveExplicitExceptionFlag = true;
  1019. }
  1020. bool ShouldUseExceptionTables = false;
  1021. // Exception tables and cleanups can be enabled with -fexceptions even if the
  1022. // language itself doesn't support exceptions.
  1023. if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
  1024. ShouldUseExceptionTables = true;
  1025. // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
  1026. // is not necessarily sensible, but follows GCC.
  1027. if (types::isObjC(InputType) &&
  1028. Args.hasFlag(options::OPT_fobjc_exceptions,
  1029. options::OPT_fno_objc_exceptions,
  1030. true)) {
  1031. CmdArgs.push_back("-fobjc-exceptions");
  1032. ShouldUseExceptionTables |=
  1033. shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
  1034. }
  1035. if (types::isCXX(InputType)) {
  1036. bool CXXExceptionsEnabled = ExceptionsEnabled;
  1037. if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
  1038. options::OPT_fno_cxx_exceptions,
  1039. options::OPT_fexceptions,
  1040. options::OPT_fno_exceptions)) {
  1041. if (A->getOption().matches(options::OPT_fcxx_exceptions))
  1042. CXXExceptionsEnabled = true;
  1043. else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
  1044. CXXExceptionsEnabled = false;
  1045. }
  1046. if (CXXExceptionsEnabled) {
  1047. CmdArgs.push_back("-fcxx-exceptions");
  1048. ShouldUseExceptionTables = true;
  1049. }
  1050. }
  1051. if (ShouldUseExceptionTables)
  1052. CmdArgs.push_back("-fexceptions");
  1053. }
  1054. static bool ShouldDisableCFI(const ArgList &Args,
  1055. const ToolChain &TC) {
  1056. bool Default = true;
  1057. if (TC.getTriple().isOSDarwin()) {
  1058. // The native darwin assembler doesn't support cfi directives, so
  1059. // we disable them if we think the .s file will be passed to it.
  1060. Default = Args.hasFlag(options::OPT_integrated_as,
  1061. options::OPT_no_integrated_as,
  1062. TC.IsIntegratedAssemblerDefault());
  1063. }
  1064. return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
  1065. options::OPT_fno_dwarf2_cfi_asm,
  1066. Default);
  1067. }
  1068. static bool ShouldDisableDwarfDirectory(const ArgList &Args,
  1069. const ToolChain &TC) {
  1070. bool IsIADefault = TC.IsIntegratedAssemblerDefault();
  1071. bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
  1072. options::OPT_no_integrated_as,
  1073. IsIADefault);
  1074. bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
  1075. options::OPT_fno_dwarf_directory_asm,
  1076. UseIntegratedAs);
  1077. return !UseDwarfDirectory;
  1078. }
  1079. /// \brief Check whether the given input tree contains any compilation actions.
  1080. static bool ContainsCompileAction(const Action *A) {
  1081. if (isa<CompileJobAction>(A))
  1082. return true;
  1083. for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
  1084. if (ContainsCompileAction(*it))
  1085. return true;
  1086. return false;
  1087. }
  1088. /// \brief Check if -relax-all should be passed to the internal assembler.
  1089. /// This is done by default when compiling non-assembler source with -O0.
  1090. static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
  1091. bool RelaxDefault = true;
  1092. if (Arg *A = Args.getLastArg(options::OPT_O_Group))
  1093. RelaxDefault = A->getOption().matches(options::OPT_O0);
  1094. if (RelaxDefault) {
  1095. RelaxDefault = false;
  1096. for (ActionList::const_iterator it = C.getActions().begin(),
  1097. ie = C.getActions().end(); it != ie; ++it) {
  1098. if (ContainsCompileAction(*it)) {
  1099. RelaxDefault = true;
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
  1105. RelaxDefault);
  1106. }
  1107. /// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
  1108. /// This needs to be called before we add the C run-time (malloc, etc).
  1109. static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
  1110. ArgStringList &CmdArgs) {
  1111. if (!Args.hasFlag(options::OPT_faddress_sanitizer,
  1112. options::OPT_fno_address_sanitizer, false))
  1113. return;
  1114. if(TC.getTriple().getEnvironment() == llvm::Triple::ANDROIDEABI) {
  1115. if (!Args.hasArg(options::OPT_shared)) {
  1116. // For an executable, we add a .preinit_array stub.
  1117. CmdArgs.push_back("-u");
  1118. CmdArgs.push_back("__asan_preinit");
  1119. CmdArgs.push_back("-lasan");
  1120. }
  1121. CmdArgs.push_back("-lasan_preload");
  1122. CmdArgs.push_back("-ldl");
  1123. } else {
  1124. if (!Args.hasArg(options::OPT_shared)) {
  1125. // LibAsan is "libclang_rt.asan-<ArchName>.a" in the Linux library
  1126. // resource directory.
  1127. SmallString<128> LibAsan(TC.getDriver().ResourceDir);
  1128. llvm::sys::path::append(LibAsan, "lib", "linux",
  1129. (Twine("libclang_rt.asan-") +
  1130. TC.getArchName() + ".a"));
  1131. CmdArgs.push_back(Args.MakeArgString(LibAsan));
  1132. CmdArgs.push_back("-lpthread");
  1133. CmdArgs.push_back("-ldl");
  1134. CmdArgs.push_back("-export-dynamic");
  1135. }
  1136. }
  1137. }
  1138. static bool shouldUseFramePointer(const ArgList &Args,
  1139. const llvm::Triple &Triple) {
  1140. if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
  1141. options::OPT_fomit_frame_pointer))
  1142. return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
  1143. // Don't use a frame pointer on linux x86 and x86_64 if optimizing.
  1144. if ((Triple.getArch() == llvm::Triple::x86_64 ||
  1145. Triple.getArch() == llvm::Triple::x86) &&
  1146. Triple.getOS() == llvm::Triple::Linux) {
  1147. if (Arg *A = Args.getLastArg(options::OPT_O_Group))
  1148. if (!A->getOption().matches(options::OPT_O0))
  1149. return false;
  1150. }
  1151. return true;
  1152. }
  1153. void Clang::ConstructJob(Compilation &C, const JobAction &JA,
  1154. const InputInfo &Output,
  1155. const InputInfoList &Inputs,
  1156. const ArgList &Args,
  1157. const char *LinkingOutput) const {
  1158. bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
  1159. options::OPT_fapple_kext);
  1160. const Driver &D = getToolChain().getDriver();
  1161. ArgStringList CmdArgs;
  1162. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  1163. // Invoke ourselves in -cc1 mode.
  1164. //
  1165. // FIXME: Implement custom jobs for internal actions.
  1166. CmdArgs.push_back("-cc1");
  1167. // Add the "effective" target triple.
  1168. CmdArgs.push_back("-triple");
  1169. std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  1170. CmdArgs.push_back(Args.MakeArgString(TripleStr));
  1171. // Select the appropriate action.
  1172. bool IsRewriter = false;
  1173. bool IsModernRewriter = false;
  1174. if (isa<AnalyzeJobAction>(JA)) {
  1175. assert(JA.getType() == types::TY_Plist && "Invalid output type.");
  1176. CmdArgs.push_back("-analyze");
  1177. } else if (isa<MigrateJobAction>(JA)) {
  1178. CmdArgs.push_back("-migrate");
  1179. } else if (isa<PreprocessJobAction>(JA)) {
  1180. if (Output.getType() == types::TY_Dependencies)
  1181. CmdArgs.push_back("-Eonly");
  1182. else
  1183. CmdArgs.push_back("-E");
  1184. } else if (isa<AssembleJobAction>(JA)) {
  1185. CmdArgs.push_back("-emit-obj");
  1186. if (UseRelaxAll(C, Args))
  1187. CmdArgs.push_back("-mrelax-all");
  1188. // When using an integrated assembler, translate -Wa, and -Xassembler
  1189. // options.
  1190. for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
  1191. options::OPT_Xassembler),
  1192. ie = Args.filtered_end(); it != ie; ++it) {
  1193. const Arg *A = *it;
  1194. A->claim();
  1195. for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
  1196. StringRef Value = A->getValue(Args, i);
  1197. if (Value == "-force_cpusubtype_ALL") {
  1198. // Do nothing, this is the default and we don't support anything else.
  1199. } else if (Value == "-L") {
  1200. CmdArgs.push_back("-msave-temp-labels");
  1201. } else if (Value == "--fatal-warnings") {
  1202. CmdArgs.push_back("-mllvm");
  1203. CmdArgs.push_back("-fatal-assembler-warnings");
  1204. } else if (Value == "--noexecstack") {
  1205. CmdArgs.push_back("-mnoexecstack");
  1206. } else {
  1207. D.Diag(diag::err_drv_unsupported_option_argument)
  1208. << A->getOption().getName() << Value;
  1209. }
  1210. }
  1211. }
  1212. // Also ignore explicit -force_cpusubtype_ALL option.
  1213. (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
  1214. } else if (isa<PrecompileJobAction>(JA)) {
  1215. // Use PCH if the user requested it.
  1216. bool UsePCH = D.CCCUsePCH;
  1217. if (UsePCH)
  1218. CmdArgs.push_back("-emit-pch");
  1219. else
  1220. CmdArgs.push_back("-emit-pth");
  1221. } else {
  1222. assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
  1223. if (JA.getType() == types::TY_Nothing) {
  1224. CmdArgs.push_back("-fsyntax-only");
  1225. } else if (JA.getType() == types::TY_LLVM_IR ||
  1226. JA.getType() == types::TY_LTO_IR) {
  1227. CmdArgs.push_back("-emit-llvm");
  1228. } else if (JA.getType() == types::TY_LLVM_BC ||
  1229. JA.getType() == types::TY_LTO_BC) {
  1230. CmdArgs.push_back("-emit-llvm-bc");
  1231. } else if (JA.getType() == types::TY_PP_Asm) {
  1232. CmdArgs.push_back("-S");
  1233. } else if (JA.getType() == types::TY_AST) {
  1234. CmdArgs.push_back("-emit-pch");
  1235. } else if (JA.getType() == types::TY_RewrittenObjC) {
  1236. CmdArgs.push_back("-rewrite-objc");
  1237. IsModernRewriter = true;
  1238. } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
  1239. CmdArgs.push_back("-rewrite-objc");
  1240. IsRewriter = true;
  1241. } else {
  1242. assert(JA.getType() == types::TY_PP_Asm &&
  1243. "Unexpected output type!");
  1244. }
  1245. }
  1246. // The make clang go fast button.
  1247. CmdArgs.push_back("-disable-free");
  1248. // Disable the verification pass in -asserts builds.
  1249. #ifdef NDEBUG
  1250. CmdArgs.push_back("-disable-llvm-verifier");
  1251. #endif
  1252. // Set the main file name, so that debug info works even with
  1253. // -save-temps.
  1254. CmdArgs.push_back("-main-file-name");
  1255. CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
  1256. // Some flags which affect the language (via preprocessor
  1257. // defines). See darwin::CC1::AddCPPArgs.
  1258. if (Args.hasArg(options::OPT_static))
  1259. CmdArgs.push_back("-static-define");
  1260. if (isa<AnalyzeJobAction>(JA)) {
  1261. // Enable region store model by default.
  1262. CmdArgs.push_back("-analyzer-store=region");
  1263. // Treat blocks as analysis entry points.
  1264. CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
  1265. CmdArgs.push_back("-analyzer-eagerly-assume");
  1266. CmdArgs.push_back("-analyzer-ipa=inlining");
  1267. // Add default argument set.
  1268. if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
  1269. CmdArgs.push_back("-analyzer-checker=core");
  1270. if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
  1271. CmdArgs.push_back("-analyzer-checker=unix");
  1272. if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
  1273. CmdArgs.push_back("-analyzer-checker=osx");
  1274. CmdArgs.push_back("-analyzer-checker=deadcode");
  1275. // Enable the following experimental checkers for testing.
  1276. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
  1277. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
  1278. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
  1279. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
  1280. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
  1281. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
  1282. }
  1283. // Set the output format. The default is plist, for (lame) historical
  1284. // reasons.
  1285. CmdArgs.push_back("-analyzer-output");
  1286. if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
  1287. CmdArgs.push_back(A->getValue(Args));
  1288. else
  1289. CmdArgs.push_back("plist");
  1290. // Disable the presentation of standard compiler warnings when
  1291. // using --analyze. We only want to show static analyzer diagnostics
  1292. // or frontend errors.
  1293. CmdArgs.push_back("-w");
  1294. // Add -Xanalyzer arguments when running as analyzer.
  1295. Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
  1296. }
  1297. CheckCodeGenerationOptions(D, Args);
  1298. // Perform argument translation for LLVM backend. This
  1299. // takes some care in reconciling with llvm-gcc. The
  1300. // issue is that llvm-gcc translates these options based on
  1301. // the values in cc1, whereas we are processing based on
  1302. // the driver arguments.
  1303. // This comes from the default translation the driver + cc1
  1304. // would do to enable flag_pic.
  1305. //
  1306. // FIXME: Centralize this code.
  1307. Arg *LastPICArg = 0;
  1308. for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) {
  1309. if ((*I)->getOption().matches(options::OPT_fPIC) ||
  1310. (*I)->getOption().matches(options::OPT_fno_PIC) ||
  1311. (*I)->getOption().matches(options::OPT_fpic) ||
  1312. (*I)->getOption().matches(options::OPT_fno_pic) ||
  1313. (*I)->getOption().matches(options::OPT_fPIE) ||
  1314. (*I)->getOption().matches(options::OPT_fno_PIE) ||
  1315. (*I)->getOption().matches(options::OPT_fpie) ||
  1316. (*I)->getOption().matches(options::OPT_fno_pie)) {
  1317. LastPICArg = *I;
  1318. (*I)->claim();
  1319. }
  1320. }
  1321. bool PICDisabled = false;
  1322. bool PICEnabled = false;
  1323. bool PICForPIE = false;
  1324. if (LastPICArg) {
  1325. PICForPIE = (LastPICArg->getOption().matches(options::OPT_fPIE) ||
  1326. LastPICArg->getOption().matches(options::OPT_fpie));
  1327. PICEnabled = (PICForPIE ||
  1328. LastPICArg->getOption().matches(options::OPT_fPIC) ||
  1329. LastPICArg->getOption().matches(options::OPT_fpic));
  1330. PICDisabled = !PICEnabled;
  1331. }
  1332. // Note that these flags are trump-cards. Regardless of the order w.r.t. the
  1333. // PIC or PIE options above, if these show up, PIC is disabled.
  1334. if (Args.hasArg(options::OPT_mkernel))
  1335. PICDisabled = true;
  1336. if (Args.hasArg(options::OPT_static))
  1337. PICDisabled = true;
  1338. bool DynamicNoPIC = Args.hasArg(options::OPT_mdynamic_no_pic);
  1339. // Select the relocation model.
  1340. const char *Model = getToolChain().GetForcedPicModel();
  1341. if (!Model) {
  1342. if (DynamicNoPIC)
  1343. Model = "dynamic-no-pic";
  1344. else if (PICDisabled)
  1345. Model = "static";
  1346. else if (PICEnabled)
  1347. Model = "pic";
  1348. else
  1349. Model = getToolChain().GetDefaultRelocationModel();
  1350. }
  1351. StringRef ModelStr = Model ? Model : "";
  1352. if (Model && ModelStr != "pic") {
  1353. CmdArgs.push_back("-mrelocation-model");
  1354. CmdArgs.push_back(Model);
  1355. }
  1356. // Infer the __PIC__ and __PIE__ values.
  1357. if (ModelStr == "pic" && PICForPIE) {
  1358. CmdArgs.push_back("-pie-level");
  1359. CmdArgs.push_back((LastPICArg &&
  1360. LastPICArg->getOption().matches(options::OPT_fPIE)) ?
  1361. "2" : "1");
  1362. } else if (ModelStr == "pic" || ModelStr == "dynamic-no-pic") {
  1363. CmdArgs.push_back("-pic-level");
  1364. CmdArgs.push_back(((ModelStr != "dynamic-no-pic" && LastPICArg &&
  1365. LastPICArg->getOption().matches(options::OPT_fPIC)) ||
  1366. getToolChain().getTriple().isOSDarwin()) ? "2" : "1");
  1367. }
  1368. if (!Args.hasFlag(options::OPT_fmerge_all_constants,
  1369. options::OPT_fno_merge_all_constants))
  1370. CmdArgs.push_back("-fno-merge-all-constants");
  1371. // LLVM Code Generator Options.
  1372. if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
  1373. CmdArgs.push_back("-mregparm");
  1374. CmdArgs.push_back(A->getValue(Args));
  1375. }
  1376. if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
  1377. CmdArgs.push_back("-mrtd");
  1378. if (shouldUseFramePointer(Args, getToolChain().getTriple()))
  1379. CmdArgs.push_back("-mdisable-fp-elim");
  1380. if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
  1381. options::OPT_fno_zero_initialized_in_bss))
  1382. CmdArgs.push_back("-mno-zero-initialized-in-bss");
  1383. if (!Args.hasFlag(options::OPT_fstrict_aliasing,
  1384. options::OPT_fno_strict_aliasing,
  1385. getToolChain().IsStrictAliasingDefault()))
  1386. CmdArgs.push_back("-relaxed-aliasing");
  1387. if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
  1388. false))
  1389. CmdArgs.push_back("-fstrict-enums");
  1390. if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
  1391. options::OPT_fno_optimize_sibling_calls))
  1392. CmdArgs.push_back("-mdisable-tail-calls");
  1393. // Handle various floating point optimization flags, mapping them to the
  1394. // appropriate LLVM code generation flags. The pattern for all of these is to
  1395. // default off the codegen optimizations, and if any flag enables them and no
  1396. // flag disables them after the flag enabling them, enable the codegen
  1397. // optimization. This is complicated by several "umbrella" flags.
  1398. if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
  1399. options::OPT_ffinite_math_only,
  1400. options::OPT_fno_finite_math_only,
  1401. options::OPT_fhonor_infinities,
  1402. options::OPT_fno_honor_infinities))
  1403. if (A->getOption().getID() != options::OPT_fno_finite_math_only &&
  1404. A->getOption().getID() != options::OPT_fhonor_infinities)
  1405. CmdArgs.push_back("-menable-no-infs");
  1406. if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
  1407. options::OPT_ffinite_math_only,
  1408. options::OPT_fno_finite_math_only,
  1409. options::OPT_fhonor_nans,
  1410. options::OPT_fno_honor_nans))
  1411. if (A->getOption().getID() != options::OPT_fno_finite_math_only &&
  1412. A->getOption().getID() != options::OPT_fhonor_nans)
  1413. CmdArgs.push_back("-menable-no-nans");
  1414. // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
  1415. bool MathErrno = getToolChain().IsMathErrnoDefault();
  1416. if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
  1417. options::OPT_fmath_errno,
  1418. options::OPT_fno_math_errno))
  1419. MathErrno = A->getOption().getID() == options::OPT_fmath_errno;
  1420. if (MathErrno)
  1421. CmdArgs.push_back("-fmath-errno");
  1422. // There are several flags which require disabling very specific
  1423. // optimizations. Any of these being disabled forces us to turn off the
  1424. // entire set of LLVM optimizations, so collect them through all the flag
  1425. // madness.
  1426. bool AssociativeMath = false;
  1427. if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
  1428. options::OPT_funsafe_math_optimizations,
  1429. options::OPT_fno_unsafe_math_optimizations,
  1430. options::OPT_fassociative_math,
  1431. options::OPT_fno_associative_math))
  1432. if (A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  1433. A->getOption().getID() != options::OPT_fno_associative_math)
  1434. AssociativeMath = true;
  1435. bool ReciprocalMath = false;
  1436. if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
  1437. options::OPT_funsafe_math_optimizations,
  1438. options::OPT_fno_unsafe_math_optimizations,
  1439. options::OPT_freciprocal_math,
  1440. options::OPT_fno_reciprocal_math))
  1441. if (A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  1442. A->getOption().getID() != options::OPT_fno_reciprocal_math)
  1443. ReciprocalMath = true;
  1444. bool SignedZeros = true;
  1445. if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
  1446. options::OPT_funsafe_math_optimizations,
  1447. options::OPT_fno_unsafe_math_optimizations,
  1448. options::OPT_fsigned_zeros,
  1449. options::OPT_fno_signed_zeros))
  1450. if (A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  1451. A->getOption().getID() != options::OPT_fsigned_zeros)
  1452. SignedZeros = false;
  1453. bool TrappingMath = true;
  1454. if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
  1455. options::OPT_funsafe_math_optimizations,
  1456. options::OPT_fno_unsafe_math_optimizations,
  1457. options::OPT_ftrapping_math,
  1458. options::OPT_fno_trapping_math))
  1459. if (A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  1460. A->getOption().getID() != options::OPT_ftrapping_math)
  1461. TrappingMath = false;
  1462. if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
  1463. !TrappingMath)
  1464. CmdArgs.push_back("-menable-unsafe-fp-math");
  1465. // We separately look for the '-ffast-math' flag, and if we find it, tell the
  1466. // frontend to provide the appropriate preprocessor macros. This is distinct
  1467. // from enabling any optimizations as it induces a language change which must
  1468. // survive serialization and deserialization, etc.
  1469. if (Args.hasArg(options::OPT_ffast_math))
  1470. CmdArgs.push_back("-ffast-math");
  1471. // Decide whether to use verbose asm. Verbose assembly is the default on
  1472. // toolchains which have the integrated assembler on by default.
  1473. bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
  1474. if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
  1475. IsVerboseAsmDefault) ||
  1476. Args.hasArg(options::OPT_dA))
  1477. CmdArgs.push_back("-masm-verbose");
  1478. if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
  1479. CmdArgs.push_back("-mdebug-pass");
  1480. CmdArgs.push_back("Structure");
  1481. }
  1482. if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
  1483. CmdArgs.push_back("-mdebug-pass");
  1484. CmdArgs.push_back("Arguments");
  1485. }
  1486. // Enable -mconstructor-aliases except on darwin, where we have to
  1487. // work around a linker bug; see <rdar://problem/7651567>.
  1488. if (!getToolChain().getTriple().isOSDarwin())
  1489. CmdArgs.push_back("-mconstructor-aliases");
  1490. // Darwin's kernel doesn't support guard variables; just die if we
  1491. // try to use them.
  1492. if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
  1493. CmdArgs.push_back("-fforbid-guard-variables");
  1494. if (Args.hasArg(options::OPT_mms_bitfields)) {
  1495. CmdArgs.push_back("-mms-bitfields");
  1496. }
  1497. // This is a coarse approximation of what llvm-gcc actually does, both
  1498. // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
  1499. // complicated ways.
  1500. bool AsynchronousUnwindTables =
  1501. Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
  1502. options::OPT_fno_asynchronous_unwind_tables,
  1503. getToolChain().IsUnwindTablesDefault() &&
  1504. !KernelOrKext);
  1505. if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
  1506. AsynchronousUnwindTables))
  1507. CmdArgs.push_back("-munwind-tables");
  1508. if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
  1509. CmdArgs.push_back("-mlimit-float-precision");
  1510. CmdArgs.push_back(A->getValue(Args));
  1511. }
  1512. // FIXME: Handle -mtune=.
  1513. (void) Args.hasArg(options::OPT_mtune_EQ);
  1514. if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
  1515. CmdArgs.push_back("-mcode-model");
  1516. CmdArgs.push_back(A->getValue(Args));
  1517. }
  1518. // Add target specific cpu and features flags.
  1519. switch(getToolChain().getTriple().getArch()) {
  1520. default:
  1521. break;
  1522. case llvm::Triple::arm:
  1523. case llvm::Triple::thumb:
  1524. AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
  1525. break;
  1526. case llvm::Triple::mips:
  1527. case llvm::Triple::mipsel:
  1528. case llvm::Triple::mips64:
  1529. case llvm::Triple::mips64el:
  1530. AddMIPSTargetArgs(Args, CmdArgs);
  1531. break;
  1532. case llvm::Triple::sparc:
  1533. AddSparcTargetArgs(Args, CmdArgs);
  1534. break;
  1535. case llvm::Triple::x86:
  1536. case llvm::Triple::x86_64:
  1537. AddX86TargetArgs(Args, CmdArgs);
  1538. break;
  1539. case llvm::Triple::hexagon:
  1540. AddHexagonTargetArgs(Args, CmdArgs);
  1541. break;
  1542. }
  1543. // Pass the linker version in use.
  1544. if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
  1545. CmdArgs.push_back("-target-linker-version");
  1546. CmdArgs.push_back(A->getValue(Args));
  1547. }
  1548. // -mno-omit-leaf-frame-pointer is the default on Darwin.
  1549. if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
  1550. options::OPT_mno_omit_leaf_frame_pointer,
  1551. !getToolChain().getTriple().isOSDarwin()))
  1552. CmdArgs.push_back("-momit-leaf-frame-pointer");
  1553. // Explicitly error on some things we know we don't support and can't just
  1554. // ignore.
  1555. types::ID InputType = Inputs[0].getType();
  1556. if (!Args.hasArg(options::OPT_fallow_unsupported)) {
  1557. Arg *Unsupported;
  1558. if (types::isCXX(InputType) &&
  1559. getToolChain().getTriple().isOSDarwin() &&
  1560. getToolChain().getTriple().getArch() == llvm::Triple::x86) {
  1561. if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
  1562. (Unsupported = Args.getLastArg(options::OPT_mkernel)))
  1563. D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
  1564. << Unsupported->getOption().getName();
  1565. }
  1566. }
  1567. Args.AddAllArgs(CmdArgs, options::OPT_v);
  1568. Args.AddLastArg(CmdArgs, options::OPT_H);
  1569. if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
  1570. CmdArgs.push_back("-header-include-file");
  1571. CmdArgs.push_back(D.CCPrintHeadersFilename ?
  1572. D.CCPrintHeadersFilename : "-");
  1573. }
  1574. Args.AddLastArg(CmdArgs, options::OPT_P);
  1575. Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
  1576. if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
  1577. CmdArgs.push_back("-diagnostic-log-file");
  1578. CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
  1579. D.CCLogDiagnosticsFilename : "-");
  1580. }
  1581. // Special case debug options to only pass -g to clang. This is
  1582. // wrong.
  1583. Args.ClaimAllArgs(options::OPT_g_Group);
  1584. if (Arg *A = Args.getLastArg(options::OPT_g_Group))
  1585. if (!A->getOption().matches(options::OPT_g0)) {
  1586. CmdArgs.push_back("-g");
  1587. }
  1588. if (Args.hasArg(options::OPT_gline_tables_only))
  1589. CmdArgs.push_back("-gline-tables-only");
  1590. Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
  1591. Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
  1592. Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
  1593. if (Args.hasArg(options::OPT_ftest_coverage) ||
  1594. Args.hasArg(options::OPT_coverage))
  1595. CmdArgs.push_back("-femit-coverage-notes");
  1596. if (Args.hasArg(options::OPT_fprofile_arcs) ||
  1597. Args.hasArg(options::OPT_coverage))
  1598. CmdArgs.push_back("-femit-coverage-data");
  1599. if (C.getArgs().hasArg(options::OPT_c) ||
  1600. C.getArgs().hasArg(options::OPT_S)) {
  1601. if (Output.isFilename()) {
  1602. CmdArgs.push_back("-coverage-file");
  1603. CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
  1604. }
  1605. }
  1606. // Pass options for controlling the default header search paths.
  1607. if (Args.hasArg(options::OPT_nostdinc)) {
  1608. CmdArgs.push_back("-nostdsysteminc");
  1609. CmdArgs.push_back("-nobuiltininc");
  1610. } else {
  1611. if (Args.hasArg(options::OPT_nostdlibinc))
  1612. CmdArgs.push_back("-nostdsysteminc");
  1613. Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
  1614. Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
  1615. }
  1616. // Pass the path to compiler resource files.
  1617. CmdArgs.push_back("-resource-dir");
  1618. CmdArgs.push_back(D.ResourceDir.c_str());
  1619. Args.AddLastArg(CmdArgs, options::OPT_working_directory);
  1620. bool ARCMTEnabled = false;
  1621. if (!Args.hasArg(options::OPT_fno_objc_arc)) {
  1622. if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
  1623. options::OPT_ccc_arcmt_modify,
  1624. options::OPT_ccc_arcmt_migrate)) {
  1625. ARCMTEnabled = true;
  1626. switch (A->getOption().getID()) {
  1627. default:
  1628. llvm_unreachable("missed a case");
  1629. case options::OPT_ccc_arcmt_check:
  1630. CmdArgs.push_back("-arcmt-check");
  1631. break;
  1632. case options::OPT_ccc_arcmt_modify:
  1633. CmdArgs.push_back("-arcmt-modify");
  1634. break;
  1635. case options::OPT_ccc_arcmt_migrate:
  1636. CmdArgs.push_back("-arcmt-migrate");
  1637. CmdArgs.push_back("-mt-migrate-directory");
  1638. CmdArgs.push_back(A->getValue(Args));
  1639. Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
  1640. Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
  1641. break;
  1642. }
  1643. }
  1644. }
  1645. if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
  1646. if (ARCMTEnabled) {
  1647. D.Diag(diag::err_drv_argument_not_allowed_with)
  1648. << A->getAsString(Args) << "-ccc-arcmt-migrate";
  1649. }
  1650. CmdArgs.push_back("-mt-migrate-directory");
  1651. CmdArgs.push_back(A->getValue(Args));
  1652. if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
  1653. options::OPT_objcmt_migrate_subscripting)) {
  1654. // None specified, means enable them all.
  1655. CmdArgs.push_back("-objcmt-migrate-literals");
  1656. CmdArgs.push_back("-objcmt-migrate-subscripting");
  1657. } else {
  1658. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
  1659. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
  1660. }
  1661. }
  1662. // Add preprocessing options like -I, -D, etc. if we are using the
  1663. // preprocessor.
  1664. //
  1665. // FIXME: Support -fpreprocessed
  1666. if (types::getPreprocessedType(InputType) != types::TY_INVALID)
  1667. AddPreprocessingOptions(C, D, Args, CmdArgs, Output, Inputs);
  1668. // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
  1669. // that "The compiler can only warn and ignore the option if not recognized".
  1670. // When building with ccache, it will pass -D options to clang even on
  1671. // preprocessed inputs and configure concludes that -fPIC is not supported.
  1672. Args.ClaimAllArgs(options::OPT_D);
  1673. // Manually translate -O to -O2 and -O4 to -O3; let clang reject
  1674. // others.
  1675. if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
  1676. if (A->getOption().matches(options::OPT_O4))
  1677. CmdArgs.push_back("-O3");
  1678. else if (A->getOption().matches(options::OPT_O) &&
  1679. A->getValue(Args)[0] == '\0')
  1680. CmdArgs.push_back("-O2");
  1681. else
  1682. A->render(Args, CmdArgs);
  1683. }
  1684. Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
  1685. Args.AddLastArg(CmdArgs, options::OPT_pedantic);
  1686. Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
  1687. Args.AddLastArg(CmdArgs, options::OPT_w);
  1688. // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
  1689. // (-ansi is equivalent to -std=c89).
  1690. //
  1691. // If a std is supplied, only add -trigraphs if it follows the
  1692. // option.
  1693. if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
  1694. if (Std->getOption().matches(options::OPT_ansi))
  1695. if (types::isCXX(InputType))
  1696. CmdArgs.push_back("-std=c++98");
  1697. else
  1698. CmdArgs.push_back("-std=c89");
  1699. else
  1700. Std->render(Args, CmdArgs);
  1701. if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
  1702. options::OPT_trigraphs))
  1703. if (A != Std)
  1704. A->render(Args, CmdArgs);
  1705. } else {
  1706. // Honor -std-default.
  1707. //
  1708. // FIXME: Clang doesn't correctly handle -std= when the input language
  1709. // doesn't match. For the time being just ignore this for C++ inputs;
  1710. // eventually we want to do all the standard defaulting here instead of
  1711. // splitting it between the driver and clang -cc1.
  1712. if (!types::isCXX(InputType))
  1713. Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
  1714. "-std=", /*Joined=*/true);
  1715. Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
  1716. }
  1717. // Map the bizarre '-Wwrite-strings' flag to a more sensible
  1718. // '-fconst-strings'; this better indicates its actual behavior.
  1719. if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
  1720. false)) {
  1721. // For perfect compatibility with GCC, we do this even in the presence of
  1722. // '-w'. This flag names something other than a warning for GCC.
  1723. CmdArgs.push_back("-fconst-strings");
  1724. }
  1725. // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
  1726. // during C++ compilation, which it is by default. GCC keeps this define even
  1727. // in the presence of '-w', match this behavior bug-for-bug.
  1728. if (types::isCXX(InputType) &&
  1729. Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
  1730. true)) {
  1731. CmdArgs.push_back("-fdeprecated-macro");
  1732. }
  1733. // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
  1734. if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
  1735. if (Asm->getOption().matches(options::OPT_fasm))
  1736. CmdArgs.push_back("-fgnu-keywords");
  1737. else
  1738. CmdArgs.push_back("-fno-gnu-keywords");
  1739. }
  1740. if (ShouldDisableCFI(Args, getToolChain()))
  1741. CmdArgs.push_back("-fno-dwarf2-cfi-asm");
  1742. if (ShouldDisableDwarfDirectory(Args, getToolChain()))
  1743. CmdArgs.push_back("-fno-dwarf-directory-asm");
  1744. if (const char *pwd = ::getenv("PWD")) {
  1745. // GCC also verifies that stat(pwd) and stat(".") have the same inode
  1746. // number. Not doing those because stats are slow, but we could.
  1747. if (llvm::sys::path::is_absolute(pwd)) {
  1748. std::string CompDir = pwd;
  1749. CmdArgs.push_back("-fdebug-compilation-dir");
  1750. CmdArgs.push_back(Args.MakeArgString(CompDir));
  1751. }
  1752. }
  1753. if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
  1754. options::OPT_ftemplate_depth_EQ)) {
  1755. CmdArgs.push_back("-ftemplate-depth");
  1756. CmdArgs.push_back(A->getValue(Args));
  1757. }
  1758. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
  1759. CmdArgs.push_back("-fconstexpr-depth");
  1760. CmdArgs.push_back(A->getValue(Args));
  1761. }
  1762. if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
  1763. options::OPT_Wlarge_by_value_copy_def)) {
  1764. if (A->getNumValues()) {
  1765. StringRef bytes = A->getValue(Args);
  1766. CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
  1767. } else
  1768. CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
  1769. }
  1770. if (Arg *A = Args.getLastArg(options::OPT_fbounds_checking,
  1771. options::OPT_fbounds_checking_EQ)) {
  1772. if (A->getNumValues()) {
  1773. StringRef val = A->getValue(Args);
  1774. CmdArgs.push_back(Args.MakeArgString("-fbounds-checking=" + val));
  1775. } else
  1776. CmdArgs.push_back("-fbounds-checking=1");
  1777. }
  1778. if (Args.hasArg(options::OPT__relocatable_pch))
  1779. CmdArgs.push_back("-relocatable-pch");
  1780. if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
  1781. CmdArgs.push_back("-fconstant-string-class");
  1782. CmdArgs.push_back(A->getValue(Args));
  1783. }
  1784. if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
  1785. CmdArgs.push_back("-ftabstop");
  1786. CmdArgs.push_back(A->getValue(Args));
  1787. }
  1788. CmdArgs.push_back("-ferror-limit");
  1789. if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
  1790. CmdArgs.push_back(A->getValue(Args));
  1791. else
  1792. CmdArgs.push_back("19");
  1793. if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
  1794. CmdArgs.push_back("-fmacro-backtrace-limit");
  1795. CmdArgs.push_back(A->getValue(Args));
  1796. }
  1797. if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
  1798. CmdArgs.push_back("-ftemplate-backtrace-limit");
  1799. CmdArgs.push_back(A->getValue(Args));
  1800. }
  1801. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
  1802. CmdArgs.push_back("-fconstexpr-backtrace-limit");
  1803. CmdArgs.push_back(A->getValue(Args));
  1804. }
  1805. // Pass -fmessage-length=.
  1806. CmdArgs.push_back("-fmessage-length");
  1807. if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
  1808. CmdArgs.push_back(A->getValue(Args));
  1809. } else {
  1810. // If -fmessage-length=N was not specified, determine whether this is a
  1811. // terminal and, if so, implicitly define -fmessage-length appropriately.
  1812. unsigned N = llvm::sys::Process::StandardErrColumns();
  1813. CmdArgs.push_back(Args.MakeArgString(Twine(N)));
  1814. }
  1815. if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
  1816. CmdArgs.push_back("-fvisibility");
  1817. CmdArgs.push_back(A->getValue(Args));
  1818. }
  1819. Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
  1820. // -fhosted is default.
  1821. if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
  1822. KernelOrKext)
  1823. CmdArgs.push_back("-ffreestanding");
  1824. // Forward -f (flag) options which we can pass directly.
  1825. Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
  1826. Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
  1827. Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
  1828. Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
  1829. Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info);
  1830. Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
  1831. Args.AddLastArg(CmdArgs, options::OPT_faltivec);
  1832. // Report and error for -faltivec on anything other then PowerPC.
  1833. if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
  1834. if (!(getToolChain().getTriple().getArch() == llvm::Triple::ppc ||
  1835. getToolChain().getTriple().getArch() == llvm::Triple::ppc64))
  1836. D.Diag(diag::err_drv_argument_only_allowed_with)
  1837. << A->getAsString(Args) << "ppc/ppc64";
  1838. if (getToolChain().SupportsProfiling())
  1839. Args.AddLastArg(CmdArgs, options::OPT_pg);
  1840. if (Args.hasFlag(options::OPT_faddress_sanitizer,
  1841. options::OPT_fno_address_sanitizer, false))
  1842. CmdArgs.push_back("-faddress-sanitizer");
  1843. if (Args.hasFlag(options::OPT_fthread_sanitizer,
  1844. options::OPT_fno_thread_sanitizer, false))
  1845. CmdArgs.push_back("-fthread-sanitizer");
  1846. // -flax-vector-conversions is default.
  1847. if (!Args.hasFlag(options::OPT_flax_vector_conversions,
  1848. options::OPT_fno_lax_vector_conversions))
  1849. CmdArgs.push_back("-fno-lax-vector-conversions");
  1850. if (Args.getLastArg(options::OPT_fapple_kext))
  1851. CmdArgs.push_back("-fapple-kext");
  1852. Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
  1853. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
  1854. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
  1855. Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
  1856. Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
  1857. if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
  1858. CmdArgs.push_back("-ftrapv-handler");
  1859. CmdArgs.push_back(A->getValue(Args));
  1860. }
  1861. Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
  1862. // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
  1863. // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
  1864. if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
  1865. options::OPT_fno_wrapv)) {
  1866. if (A->getOption().matches(options::OPT_fwrapv))
  1867. CmdArgs.push_back("-fwrapv");
  1868. } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
  1869. options::OPT_fno_strict_overflow)) {
  1870. if (A->getOption().matches(options::OPT_fno_strict_overflow))
  1871. CmdArgs.push_back("-fwrapv");
  1872. }
  1873. Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
  1874. Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
  1875. Args.AddLastArg(CmdArgs, options::OPT_pthread);
  1876. // -stack-protector=0 is default.
  1877. unsigned StackProtectorLevel = 0;
  1878. if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
  1879. options::OPT_fstack_protector_all,
  1880. options::OPT_fstack_protector)) {
  1881. if (A->getOption().matches(options::OPT_fstack_protector))
  1882. StackProtectorLevel = 1;
  1883. else if (A->getOption().matches(options::OPT_fstack_protector_all))
  1884. StackProtectorLevel = 2;
  1885. } else {
  1886. StackProtectorLevel =
  1887. getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
  1888. }
  1889. if (StackProtectorLevel) {
  1890. CmdArgs.push_back("-stack-protector");
  1891. CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
  1892. }
  1893. // Translate -mstackrealign
  1894. if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
  1895. false)) {
  1896. CmdArgs.push_back("-backend-option");
  1897. CmdArgs.push_back("-force-align-stack");
  1898. }
  1899. if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
  1900. false)) {
  1901. CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
  1902. }
  1903. if (Args.hasArg(options::OPT_mstack_alignment)) {
  1904. StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
  1905. CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
  1906. }
  1907. // Forward -f options with positive and negative forms; we translate
  1908. // these by hand.
  1909. if (Args.hasArg(options::OPT_mkernel)) {
  1910. if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
  1911. CmdArgs.push_back("-fapple-kext");
  1912. if (!Args.hasArg(options::OPT_fbuiltin))
  1913. CmdArgs.push_back("-fno-builtin");
  1914. Args.ClaimAllArgs(options::OPT_fno_builtin);
  1915. }
  1916. // -fbuiltin is default.
  1917. else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
  1918. CmdArgs.push_back("-fno-builtin");
  1919. if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
  1920. options::OPT_fno_assume_sane_operator_new))
  1921. CmdArgs.push_back("-fno-assume-sane-operator-new");
  1922. // -fblocks=0 is default.
  1923. if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
  1924. getToolChain().IsBlocksDefault()) ||
  1925. (Args.hasArg(options::OPT_fgnu_runtime) &&
  1926. Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
  1927. !Args.hasArg(options::OPT_fno_blocks))) {
  1928. CmdArgs.push_back("-fblocks");
  1929. if (!Args.hasArg(options::OPT_fgnu_runtime) &&
  1930. !getToolChain().hasBlocksRuntime())
  1931. CmdArgs.push_back("-fblocks-runtime-optional");
  1932. }
  1933. // -fmodules enables modules (off by default). However, for C++/Objective-C++,
  1934. // users must also pass -fcxx-modules. The latter flag will disappear once the
  1935. // modules implementation is solid for C++/Objective-C++ programs as well.
  1936. if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
  1937. bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
  1938. options::OPT_fno_cxx_modules,
  1939. false);
  1940. if (AllowedInCXX || !types::isCXX(InputType))
  1941. CmdArgs.push_back("-fmodules");
  1942. }
  1943. // -faccess-control is default.
  1944. if (Args.hasFlag(options::OPT_fno_access_control,
  1945. options::OPT_faccess_control,
  1946. false))
  1947. CmdArgs.push_back("-fno-access-control");
  1948. // -felide-constructors is the default.
  1949. if (Args.hasFlag(options::OPT_fno_elide_constructors,
  1950. options::OPT_felide_constructors,
  1951. false))
  1952. CmdArgs.push_back("-fno-elide-constructors");
  1953. // -frtti is default.
  1954. if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
  1955. KernelOrKext)
  1956. CmdArgs.push_back("-fno-rtti");
  1957. // -fshort-enums=0 is default for all architectures except Hexagon.
  1958. if (Args.hasFlag(options::OPT_fshort_enums,
  1959. options::OPT_fno_short_enums,
  1960. getToolChain().getTriple().getArch() ==
  1961. llvm::Triple::hexagon))
  1962. CmdArgs.push_back("-fshort-enums");
  1963. // -fsigned-char is default.
  1964. if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
  1965. isSignedCharDefault(getToolChain().getTriple())))
  1966. CmdArgs.push_back("-fno-signed-char");
  1967. // -fthreadsafe-static is default.
  1968. if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
  1969. options::OPT_fno_threadsafe_statics))
  1970. CmdArgs.push_back("-fno-threadsafe-statics");
  1971. // -fuse-cxa-atexit is default.
  1972. if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
  1973. options::OPT_fno_use_cxa_atexit,
  1974. getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
  1975. getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
  1976. getToolChain().getTriple().getArch() != llvm::Triple::hexagon) ||
  1977. KernelOrKext)
  1978. CmdArgs.push_back("-fno-use-cxa-atexit");
  1979. // -fms-extensions=0 is default.
  1980. if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
  1981. getToolChain().getTriple().getOS() == llvm::Triple::Win32))
  1982. CmdArgs.push_back("-fms-extensions");
  1983. // -fms-compatibility=0 is default.
  1984. if (Args.hasFlag(options::OPT_fms_compatibility,
  1985. options::OPT_fno_ms_compatibility,
  1986. (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
  1987. Args.hasFlag(options::OPT_fms_extensions,
  1988. options::OPT_fno_ms_extensions,
  1989. true))))
  1990. CmdArgs.push_back("-fms-compatibility");
  1991. // -fmsc-version=1300 is default.
  1992. if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
  1993. getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
  1994. Args.hasArg(options::OPT_fmsc_version)) {
  1995. StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
  1996. if (msc_ver.empty())
  1997. CmdArgs.push_back("-fmsc-version=1300");
  1998. else
  1999. CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
  2000. }
  2001. // -fborland-extensions=0 is default.
  2002. if (Args.hasFlag(options::OPT_fborland_extensions,
  2003. options::OPT_fno_borland_extensions, false))
  2004. CmdArgs.push_back("-fborland-extensions");
  2005. // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
  2006. // needs it.
  2007. if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
  2008. options::OPT_fno_delayed_template_parsing,
  2009. getToolChain().getTriple().getOS() == llvm::Triple::Win32))
  2010. CmdArgs.push_back("-fdelayed-template-parsing");
  2011. // -fgnu-keywords default varies depending on language; only pass if
  2012. // specified.
  2013. if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
  2014. options::OPT_fno_gnu_keywords))
  2015. A->render(Args, CmdArgs);
  2016. if (Args.hasFlag(options::OPT_fgnu89_inline,
  2017. options::OPT_fno_gnu89_inline,
  2018. false))
  2019. CmdArgs.push_back("-fgnu89-inline");
  2020. if (Args.hasArg(options::OPT_fno_inline))
  2021. CmdArgs.push_back("-fno-inline");
  2022. if (Args.hasArg(options::OPT_fno_inline_functions))
  2023. CmdArgs.push_back("-fno-inline-functions");
  2024. // -fobjc-nonfragile-abi=0 is default.
  2025. ObjCRuntime objCRuntime;
  2026. unsigned objcABIVersion = 0;
  2027. bool NeXTRuntimeIsDefault
  2028. = (IsRewriter || IsModernRewriter ||
  2029. getToolChain().getTriple().isOSDarwin());
  2030. if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
  2031. NeXTRuntimeIsDefault)) {
  2032. objCRuntime.setKind(ObjCRuntime::NeXT);
  2033. } else {
  2034. CmdArgs.push_back("-fgnu-runtime");
  2035. objCRuntime.setKind(ObjCRuntime::GNU);
  2036. }
  2037. getToolChain().configureObjCRuntime(objCRuntime);
  2038. if (objCRuntime.HasARC)
  2039. CmdArgs.push_back("-fobjc-runtime-has-arc");
  2040. if (objCRuntime.HasWeak)
  2041. CmdArgs.push_back("-fobjc-runtime-has-weak");
  2042. if (objCRuntime.HasTerminate)
  2043. CmdArgs.push_back("-fobjc-runtime-has-terminate");
  2044. // Compute the Objective-C ABI "version" to use. Version numbers are
  2045. // slightly confusing for historical reasons:
  2046. // 1 - Traditional "fragile" ABI
  2047. // 2 - Non-fragile ABI, version 1
  2048. // 3 - Non-fragile ABI, version 2
  2049. objcABIVersion = 1;
  2050. // If -fobjc-abi-version= is present, use that to set the version.
  2051. if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
  2052. if (StringRef(A->getValue(Args)) == "1")
  2053. objcABIVersion = 1;
  2054. else if (StringRef(A->getValue(Args)) == "2")
  2055. objcABIVersion = 2;
  2056. else if (StringRef(A->getValue(Args)) == "3")
  2057. objcABIVersion = 3;
  2058. else
  2059. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  2060. } else {
  2061. // Otherwise, determine if we are using the non-fragile ABI.
  2062. bool NonFragileABIIsDefault =
  2063. (IsModernRewriter ||
  2064. (!IsRewriter && getToolChain().IsObjCNonFragileABIDefault()));
  2065. if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
  2066. options::OPT_fno_objc_nonfragile_abi,
  2067. NonFragileABIIsDefault)) {
  2068. // Determine the non-fragile ABI version to use.
  2069. #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
  2070. unsigned NonFragileABIVersion = 1;
  2071. #else
  2072. unsigned NonFragileABIVersion = 2;
  2073. #endif
  2074. if (Arg *A = Args.getLastArg(
  2075. options::OPT_fobjc_nonfragile_abi_version_EQ)) {
  2076. if (StringRef(A->getValue(Args)) == "1")
  2077. NonFragileABIVersion = 1;
  2078. else if (StringRef(A->getValue(Args)) == "2")
  2079. NonFragileABIVersion = 2;
  2080. else
  2081. D.Diag(diag::err_drv_clang_unsupported)
  2082. << A->getAsString(Args);
  2083. }
  2084. objcABIVersion = 1 + NonFragileABIVersion;
  2085. } else {
  2086. objcABIVersion = 1;
  2087. }
  2088. }
  2089. if (objcABIVersion == 1) {
  2090. CmdArgs.push_back("-fobjc-fragile-abi");
  2091. } else {
  2092. // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
  2093. // legacy is the default.
  2094. if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
  2095. options::OPT_fno_objc_legacy_dispatch,
  2096. getToolChain().IsObjCLegacyDispatchDefault())) {
  2097. if (getToolChain().UseObjCMixedDispatch())
  2098. CmdArgs.push_back("-fobjc-dispatch-method=mixed");
  2099. else
  2100. CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
  2101. }
  2102. }
  2103. // -fobjc-default-synthesize-properties=1 is default. This only has an effect
  2104. // if the nonfragile objc abi is used.
  2105. if (getToolChain().IsObjCDefaultSynthPropertiesDefault()) {
  2106. CmdArgs.push_back("-fobjc-default-synthesize-properties");
  2107. }
  2108. // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
  2109. // NOTE: This logic is duplicated in ToolChains.cpp.
  2110. bool ARC = isObjCAutoRefCount(Args);
  2111. if (ARC) {
  2112. if (!getToolChain().SupportsObjCARC())
  2113. D.Diag(diag::err_arc_unsupported);
  2114. CmdArgs.push_back("-fobjc-arc");
  2115. // FIXME: It seems like this entire block, and several around it should be
  2116. // wrapped in isObjC, but for now we just use it here as this is where it
  2117. // was being used previously.
  2118. if (types::isCXX(InputType) && types::isObjC(InputType)) {
  2119. if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
  2120. CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
  2121. else
  2122. CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
  2123. }
  2124. // Allow the user to enable full exceptions code emission.
  2125. // We define off for Objective-CC, on for Objective-C++.
  2126. if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
  2127. options::OPT_fno_objc_arc_exceptions,
  2128. /*default*/ types::isCXX(InputType)))
  2129. CmdArgs.push_back("-fobjc-arc-exceptions");
  2130. }
  2131. // -fobjc-infer-related-result-type is the default, except in the Objective-C
  2132. // rewriter.
  2133. if (IsRewriter || IsModernRewriter)
  2134. CmdArgs.push_back("-fno-objc-infer-related-result-type");
  2135. // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
  2136. // takes precedence.
  2137. const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
  2138. if (!GCArg)
  2139. GCArg = Args.getLastArg(options::OPT_fobjc_gc);
  2140. if (GCArg) {
  2141. if (ARC) {
  2142. D.Diag(diag::err_drv_objc_gc_arr)
  2143. << GCArg->getAsString(Args);
  2144. } else if (getToolChain().SupportsObjCGC()) {
  2145. GCArg->render(Args, CmdArgs);
  2146. } else {
  2147. // FIXME: We should move this to a hard error.
  2148. D.Diag(diag::warn_drv_objc_gc_unsupported)
  2149. << GCArg->getAsString(Args);
  2150. }
  2151. }
  2152. // Add exception args.
  2153. addExceptionArgs(Args, InputType, getToolChain().getTriple(),
  2154. KernelOrKext, objcABIVersion, CmdArgs);
  2155. if (getToolChain().UseSjLjExceptions())
  2156. CmdArgs.push_back("-fsjlj-exceptions");
  2157. // C++ "sane" operator new.
  2158. if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
  2159. options::OPT_fno_assume_sane_operator_new))
  2160. CmdArgs.push_back("-fno-assume-sane-operator-new");
  2161. // -fconstant-cfstrings is default, and may be subject to argument translation
  2162. // on Darwin.
  2163. if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
  2164. options::OPT_fno_constant_cfstrings) ||
  2165. !Args.hasFlag(options::OPT_mconstant_cfstrings,
  2166. options::OPT_mno_constant_cfstrings))
  2167. CmdArgs.push_back("-fno-constant-cfstrings");
  2168. // -fshort-wchar default varies depending on platform; only
  2169. // pass if specified.
  2170. if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
  2171. A->render(Args, CmdArgs);
  2172. // -fno-pascal-strings is default, only pass non-default. If the tool chain
  2173. // happened to translate to -mpascal-strings, we want to back translate here.
  2174. //
  2175. // FIXME: This is gross; that translation should be pulled from the
  2176. // tool chain.
  2177. if (Args.hasFlag(options::OPT_fpascal_strings,
  2178. options::OPT_fno_pascal_strings,
  2179. false) ||
  2180. Args.hasFlag(options::OPT_mpascal_strings,
  2181. options::OPT_mno_pascal_strings,
  2182. false))
  2183. CmdArgs.push_back("-fpascal-strings");
  2184. // Honor -fpack-struct= and -fpack-struct, if given. Note that
  2185. // -fno-pack-struct doesn't apply to -fpack-struct=.
  2186. if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
  2187. std::string PackStructStr = "-fpack-struct=";
  2188. PackStructStr += A->getValue(Args);
  2189. CmdArgs.push_back(Args.MakeArgString(PackStructStr));
  2190. } else if (Args.hasFlag(options::OPT_fpack_struct,
  2191. options::OPT_fno_pack_struct, false)) {
  2192. CmdArgs.push_back("-fpack-struct=1");
  2193. }
  2194. if (Args.hasArg(options::OPT_mkernel) ||
  2195. Args.hasArg(options::OPT_fapple_kext)) {
  2196. if (!Args.hasArg(options::OPT_fcommon))
  2197. CmdArgs.push_back("-fno-common");
  2198. Args.ClaimAllArgs(options::OPT_fno_common);
  2199. }
  2200. // -fcommon is default, only pass non-default.
  2201. else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
  2202. CmdArgs.push_back("-fno-common");
  2203. // -fsigned-bitfields is default, and clang doesn't yet support
  2204. // -funsigned-bitfields.
  2205. if (!Args.hasFlag(options::OPT_fsigned_bitfields,
  2206. options::OPT_funsigned_bitfields))
  2207. D.Diag(diag::warn_drv_clang_unsupported)
  2208. << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
  2209. // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
  2210. if (!Args.hasFlag(options::OPT_ffor_scope,
  2211. options::OPT_fno_for_scope))
  2212. D.Diag(diag::err_drv_clang_unsupported)
  2213. << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
  2214. // -fcaret-diagnostics is default.
  2215. if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
  2216. options::OPT_fno_caret_diagnostics, true))
  2217. CmdArgs.push_back("-fno-caret-diagnostics");
  2218. // -fdiagnostics-fixit-info is default, only pass non-default.
  2219. if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
  2220. options::OPT_fno_diagnostics_fixit_info))
  2221. CmdArgs.push_back("-fno-diagnostics-fixit-info");
  2222. // Enable -fdiagnostics-show-option by default.
  2223. if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
  2224. options::OPT_fno_diagnostics_show_option))
  2225. CmdArgs.push_back("-fdiagnostics-show-option");
  2226. if (const Arg *A =
  2227. Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
  2228. CmdArgs.push_back("-fdiagnostics-show-category");
  2229. CmdArgs.push_back(A->getValue(Args));
  2230. }
  2231. if (const Arg *A =
  2232. Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
  2233. CmdArgs.push_back("-fdiagnostics-format");
  2234. CmdArgs.push_back(A->getValue(Args));
  2235. }
  2236. if (Arg *A = Args.getLastArg(
  2237. options::OPT_fdiagnostics_show_note_include_stack,
  2238. options::OPT_fno_diagnostics_show_note_include_stack)) {
  2239. if (A->getOption().matches(
  2240. options::OPT_fdiagnostics_show_note_include_stack))
  2241. CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
  2242. else
  2243. CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
  2244. }
  2245. // Color diagnostics are the default, unless the terminal doesn't support
  2246. // them.
  2247. if (Args.hasFlag(options::OPT_fcolor_diagnostics,
  2248. options::OPT_fno_color_diagnostics,
  2249. llvm::sys::Process::StandardErrHasColors()))
  2250. CmdArgs.push_back("-fcolor-diagnostics");
  2251. if (!Args.hasFlag(options::OPT_fshow_source_location,
  2252. options::OPT_fno_show_source_location))
  2253. CmdArgs.push_back("-fno-show-source-location");
  2254. if (!Args.hasFlag(options::OPT_fshow_column,
  2255. options::OPT_fno_show_column,
  2256. true))
  2257. CmdArgs.push_back("-fno-show-column");
  2258. if (!Args.hasFlag(options::OPT_fspell_checking,
  2259. options::OPT_fno_spell_checking))
  2260. CmdArgs.push_back("-fno-spell-checking");
  2261. // Silently ignore -fasm-blocks for now.
  2262. (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
  2263. false);
  2264. if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
  2265. A->render(Args, CmdArgs);
  2266. // -fdollars-in-identifiers default varies depending on platform and
  2267. // language; only pass if specified.
  2268. if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
  2269. options::OPT_fno_dollars_in_identifiers)) {
  2270. if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
  2271. CmdArgs.push_back("-fdollars-in-identifiers");
  2272. else
  2273. CmdArgs.push_back("-fno-dollars-in-identifiers");
  2274. }
  2275. // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
  2276. // practical purposes.
  2277. if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
  2278. options::OPT_fno_unit_at_a_time)) {
  2279. if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
  2280. D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
  2281. }
  2282. if (Args.hasFlag(options::OPT_fapple_pragma_pack,
  2283. options::OPT_fno_apple_pragma_pack, false))
  2284. CmdArgs.push_back("-fapple-pragma-pack");
  2285. // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
  2286. //
  2287. // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
  2288. #if 0
  2289. if (getToolChain().getTriple().isOSDarwin() &&
  2290. (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
  2291. getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
  2292. if (!Args.hasArg(options::OPT_fbuiltin_strcat))
  2293. CmdArgs.push_back("-fno-builtin-strcat");
  2294. if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
  2295. CmdArgs.push_back("-fno-builtin-strcpy");
  2296. }
  2297. #endif
  2298. // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
  2299. if (Arg *A = Args.getLastArg(options::OPT_traditional,
  2300. options::OPT_traditional_cpp)) {
  2301. if (isa<PreprocessJobAction>(JA))
  2302. CmdArgs.push_back("-traditional-cpp");
  2303. else
  2304. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  2305. }
  2306. Args.AddLastArg(CmdArgs, options::OPT_dM);
  2307. Args.AddLastArg(CmdArgs, options::OPT_dD);
  2308. // Handle serialized diagnostics.
  2309. if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
  2310. CmdArgs.push_back("-serialize-diagnostic-file");
  2311. CmdArgs.push_back(Args.MakeArgString(A->getValue(Args)));
  2312. }
  2313. // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
  2314. // parser.
  2315. Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
  2316. for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
  2317. ie = Args.filtered_end(); it != ie; ++it) {
  2318. (*it)->claim();
  2319. // We translate this by hand to the -cc1 argument, since nightly test uses
  2320. // it and developers have been trained to spell it with -mllvm.
  2321. if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
  2322. CmdArgs.push_back("-disable-llvm-optzns");
  2323. else
  2324. (*it)->render(Args, CmdArgs);
  2325. }
  2326. if (Output.getType() == types::TY_Dependencies) {
  2327. // Handled with other dependency code.
  2328. } else if (Output.isFilename()) {
  2329. CmdArgs.push_back("-o");
  2330. CmdArgs.push_back(Output.getFilename());
  2331. } else {
  2332. assert(Output.isNothing() && "Invalid output.");
  2333. }
  2334. for (InputInfoList::const_iterator
  2335. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  2336. const InputInfo &II = *it;
  2337. CmdArgs.push_back("-x");
  2338. CmdArgs.push_back(types::getTypeName(II.getType()));
  2339. if (II.isFilename())
  2340. CmdArgs.push_back(II.getFilename());
  2341. else
  2342. II.getInputArg().renderAsInput(Args, CmdArgs);
  2343. }
  2344. Args.AddAllArgs(CmdArgs, options::OPT_undef);
  2345. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  2346. // Optionally embed the -cc1 level arguments into the debug info, for build
  2347. // analysis.
  2348. if (getToolChain().UseDwarfDebugFlags()) {
  2349. ArgStringList OriginalArgs;
  2350. for (ArgList::const_iterator it = Args.begin(),
  2351. ie = Args.end(); it != ie; ++it)
  2352. (*it)->render(Args, OriginalArgs);
  2353. SmallString<256> Flags;
  2354. Flags += Exec;
  2355. for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
  2356. Flags += " ";
  2357. Flags += OriginalArgs[i];
  2358. }
  2359. CmdArgs.push_back("-dwarf-debug-flags");
  2360. CmdArgs.push_back(Args.MakeArgString(Flags.str()));
  2361. }
  2362. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  2363. if (Arg *A = Args.getLastArg(options::OPT_pg))
  2364. if (Args.hasArg(options::OPT_fomit_frame_pointer))
  2365. D.Diag(diag::err_drv_argument_not_allowed_with)
  2366. << "-fomit-frame-pointer" << A->getAsString(Args);
  2367. // Claim some arguments which clang supports automatically.
  2368. // -fpch-preprocess is used with gcc to add a special marker in the output to
  2369. // include the PCH file. Clang's PTH solution is completely transparent, so we
  2370. // do not need to deal with it at all.
  2371. Args.ClaimAllArgs(options::OPT_fpch_preprocess);
  2372. // Claim some arguments which clang doesn't support, but we don't
  2373. // care to warn the user about.
  2374. Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
  2375. Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
  2376. // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
  2377. Args.ClaimAllArgs(options::OPT_use_gold_plugin);
  2378. Args.ClaimAllArgs(options::OPT_emit_llvm);
  2379. }
  2380. void ClangAs::AddARMTargetArgs(const ArgList &Args,
  2381. ArgStringList &CmdArgs) const {
  2382. const Driver &D = getToolChain().getDriver();
  2383. llvm::Triple Triple = getToolChain().getTriple();
  2384. // Set the CPU based on -march= and -mcpu=.
  2385. CmdArgs.push_back("-target-cpu");
  2386. CmdArgs.push_back(getARMTargetCPU(Args, Triple));
  2387. // Honor -mfpu=.
  2388. if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
  2389. addFPUArgs(D, A, Args, CmdArgs);
  2390. // Honor -mfpmath=.
  2391. if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ))
  2392. addFPMathArgs(D, A, Args, CmdArgs, getARMTargetCPU(Args, Triple));
  2393. }
  2394. void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
  2395. const InputInfo &Output,
  2396. const InputInfoList &Inputs,
  2397. const ArgList &Args,
  2398. const char *LinkingOutput) const {
  2399. ArgStringList CmdArgs;
  2400. assert(Inputs.size() == 1 && "Unexpected number of inputs.");
  2401. const InputInfo &Input = Inputs[0];
  2402. // Don't warn about "clang -w -c foo.s"
  2403. Args.ClaimAllArgs(options::OPT_w);
  2404. // and "clang -emit-llvm -c foo.s"
  2405. Args.ClaimAllArgs(options::OPT_emit_llvm);
  2406. // and "clang -use-gold-plugin -c foo.s"
  2407. Args.ClaimAllArgs(options::OPT_use_gold_plugin);
  2408. // Invoke ourselves in -cc1as mode.
  2409. //
  2410. // FIXME: Implement custom jobs for internal actions.
  2411. CmdArgs.push_back("-cc1as");
  2412. // Add the "effective" target triple.
  2413. CmdArgs.push_back("-triple");
  2414. std::string TripleStr =
  2415. getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
  2416. CmdArgs.push_back(Args.MakeArgString(TripleStr));
  2417. // Set the output mode, we currently only expect to be used as a real
  2418. // assembler.
  2419. CmdArgs.push_back("-filetype");
  2420. CmdArgs.push_back("obj");
  2421. if (UseRelaxAll(C, Args))
  2422. CmdArgs.push_back("-relax-all");
  2423. // Add target specific cpu and features flags.
  2424. switch(getToolChain().getTriple().getArch()) {
  2425. default:
  2426. break;
  2427. case llvm::Triple::arm:
  2428. case llvm::Triple::thumb:
  2429. AddARMTargetArgs(Args, CmdArgs);
  2430. break;
  2431. }
  2432. // Ignore explicit -force_cpusubtype_ALL option.
  2433. (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
  2434. // Determine the original source input.
  2435. const Action *SourceAction = &JA;
  2436. while (SourceAction->getKind() != Action::InputClass) {
  2437. assert(!SourceAction->getInputs().empty() && "unexpected root action!");
  2438. SourceAction = SourceAction->getInputs()[0];
  2439. }
  2440. // Forward -g, assuming we are dealing with an actual assembly file.
  2441. if (SourceAction->getType() == types::TY_Asm ||
  2442. SourceAction->getType() == types::TY_PP_Asm) {
  2443. Args.ClaimAllArgs(options::OPT_g_Group);
  2444. if (Arg *A = Args.getLastArg(options::OPT_g_Group))
  2445. if (!A->getOption().matches(options::OPT_g0))
  2446. CmdArgs.push_back("-g");
  2447. }
  2448. // Optionally embed the -cc1as level arguments into the debug info, for build
  2449. // analysis.
  2450. if (getToolChain().UseDwarfDebugFlags()) {
  2451. ArgStringList OriginalArgs;
  2452. for (ArgList::const_iterator it = Args.begin(),
  2453. ie = Args.end(); it != ie; ++it)
  2454. (*it)->render(Args, OriginalArgs);
  2455. SmallString<256> Flags;
  2456. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  2457. Flags += Exec;
  2458. for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
  2459. Flags += " ";
  2460. Flags += OriginalArgs[i];
  2461. }
  2462. CmdArgs.push_back("-dwarf-debug-flags");
  2463. CmdArgs.push_back(Args.MakeArgString(Flags.str()));
  2464. }
  2465. // FIXME: Add -static support, once we have it.
  2466. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  2467. options::OPT_Xassembler);
  2468. Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
  2469. assert(Output.isFilename() && "Unexpected lipo output.");
  2470. CmdArgs.push_back("-o");
  2471. CmdArgs.push_back(Output.getFilename());
  2472. assert(Input.isFilename() && "Invalid input.");
  2473. CmdArgs.push_back(Input.getFilename());
  2474. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  2475. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  2476. }
  2477. void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
  2478. const InputInfo &Output,
  2479. const InputInfoList &Inputs,
  2480. const ArgList &Args,
  2481. const char *LinkingOutput) const {
  2482. const Driver &D = getToolChain().getDriver();
  2483. ArgStringList CmdArgs;
  2484. for (ArgList::const_iterator
  2485. it = Args.begin(), ie = Args.end(); it != ie; ++it) {
  2486. Arg *A = *it;
  2487. if (A->getOption().hasForwardToGCC()) {
  2488. // Don't forward any -g arguments to assembly steps.
  2489. if (isa<AssembleJobAction>(JA) &&
  2490. A->getOption().matches(options::OPT_g_Group))
  2491. continue;
  2492. // It is unfortunate that we have to claim here, as this means
  2493. // we will basically never report anything interesting for
  2494. // platforms using a generic gcc, even if we are just using gcc
  2495. // to get to the assembler.
  2496. A->claim();
  2497. A->render(Args, CmdArgs);
  2498. }
  2499. }
  2500. RenderExtraToolArgs(JA, CmdArgs);
  2501. // If using a driver driver, force the arch.
  2502. const std::string &Arch = getToolChain().getArchName();
  2503. if (getToolChain().getTriple().isOSDarwin()) {
  2504. CmdArgs.push_back("-arch");
  2505. // FIXME: Remove these special cases.
  2506. if (Arch == "powerpc")
  2507. CmdArgs.push_back("ppc");
  2508. else if (Arch == "powerpc64")
  2509. CmdArgs.push_back("ppc64");
  2510. else
  2511. CmdArgs.push_back(Args.MakeArgString(Arch));
  2512. }
  2513. // Try to force gcc to match the tool chain we want, if we recognize
  2514. // the arch.
  2515. //
  2516. // FIXME: The triple class should directly provide the information we want
  2517. // here.
  2518. if (Arch == "i386" || Arch == "powerpc")
  2519. CmdArgs.push_back("-m32");
  2520. else if (Arch == "x86_64" || Arch == "powerpc64")
  2521. CmdArgs.push_back("-m64");
  2522. if (Output.isFilename()) {
  2523. CmdArgs.push_back("-o");
  2524. CmdArgs.push_back(Output.getFilename());
  2525. } else {
  2526. assert(Output.isNothing() && "Unexpected output");
  2527. CmdArgs.push_back("-fsyntax-only");
  2528. }
  2529. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  2530. options::OPT_Xassembler);
  2531. // Only pass -x if gcc will understand it; otherwise hope gcc
  2532. // understands the suffix correctly. The main use case this would go
  2533. // wrong in is for linker inputs if they happened to have an odd
  2534. // suffix; really the only way to get this to happen is a command
  2535. // like '-x foobar a.c' which will treat a.c like a linker input.
  2536. //
  2537. // FIXME: For the linker case specifically, can we safely convert
  2538. // inputs into '-Wl,' options?
  2539. for (InputInfoList::const_iterator
  2540. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  2541. const InputInfo &II = *it;
  2542. // Don't try to pass LLVM or AST inputs to a generic gcc.
  2543. if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
  2544. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  2545. D.Diag(diag::err_drv_no_linker_llvm_support)
  2546. << getToolChain().getTripleString();
  2547. else if (II.getType() == types::TY_AST)
  2548. D.Diag(diag::err_drv_no_ast_support)
  2549. << getToolChain().getTripleString();
  2550. if (types::canTypeBeUserSpecified(II.getType())) {
  2551. CmdArgs.push_back("-x");
  2552. CmdArgs.push_back(types::getTypeName(II.getType()));
  2553. }
  2554. if (II.isFilename())
  2555. CmdArgs.push_back(II.getFilename());
  2556. else {
  2557. const Arg &A = II.getInputArg();
  2558. // Reverse translate some rewritten options.
  2559. if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
  2560. CmdArgs.push_back("-lstdc++");
  2561. continue;
  2562. }
  2563. // Don't render as input, we need gcc to do the translations.
  2564. A.render(Args, CmdArgs);
  2565. }
  2566. }
  2567. const std::string customGCCName = D.getCCCGenericGCCName();
  2568. const char *GCCName;
  2569. if (!customGCCName.empty())
  2570. GCCName = customGCCName.c_str();
  2571. else if (D.CCCIsCXX) {
  2572. GCCName = "g++";
  2573. } else
  2574. GCCName = "gcc";
  2575. const char *Exec =
  2576. Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
  2577. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  2578. }
  2579. void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
  2580. ArgStringList &CmdArgs) const {
  2581. CmdArgs.push_back("-E");
  2582. }
  2583. void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
  2584. ArgStringList &CmdArgs) const {
  2585. // The type is good enough.
  2586. }
  2587. void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
  2588. ArgStringList &CmdArgs) const {
  2589. const Driver &D = getToolChain().getDriver();
  2590. // If -flto, etc. are present then make sure not to force assembly output.
  2591. if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
  2592. JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
  2593. CmdArgs.push_back("-c");
  2594. else {
  2595. if (JA.getType() != types::TY_PP_Asm)
  2596. D.Diag(diag::err_drv_invalid_gcc_output_type)
  2597. << getTypeName(JA.getType());
  2598. CmdArgs.push_back("-S");
  2599. }
  2600. }
  2601. void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
  2602. ArgStringList &CmdArgs) const {
  2603. CmdArgs.push_back("-c");
  2604. }
  2605. void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
  2606. ArgStringList &CmdArgs) const {
  2607. // The types are (hopefully) good enough.
  2608. }
  2609. // Hexagon tools start.
  2610. void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
  2611. ArgStringList &CmdArgs) const {
  2612. }
  2613. void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  2614. const InputInfo &Output,
  2615. const InputInfoList &Inputs,
  2616. const ArgList &Args,
  2617. const char *LinkingOutput) const {
  2618. const Driver &D = getToolChain().getDriver();
  2619. ArgStringList CmdArgs;
  2620. std::string MarchString = "-march=";
  2621. MarchString += getHexagonTargetCPU(Args);
  2622. CmdArgs.push_back(Args.MakeArgString(MarchString));
  2623. RenderExtraToolArgs(JA, CmdArgs);
  2624. if (Output.isFilename()) {
  2625. CmdArgs.push_back("-o");
  2626. CmdArgs.push_back(Output.getFilename());
  2627. } else {
  2628. assert(Output.isNothing() && "Unexpected output");
  2629. CmdArgs.push_back("-fsyntax-only");
  2630. }
  2631. // Only pass -x if gcc will understand it; otherwise hope gcc
  2632. // understands the suffix correctly. The main use case this would go
  2633. // wrong in is for linker inputs if they happened to have an odd
  2634. // suffix; really the only way to get this to happen is a command
  2635. // like '-x foobar a.c' which will treat a.c like a linker input.
  2636. //
  2637. // FIXME: For the linker case specifically, can we safely convert
  2638. // inputs into '-Wl,' options?
  2639. for (InputInfoList::const_iterator
  2640. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  2641. const InputInfo &II = *it;
  2642. // Don't try to pass LLVM or AST inputs to a generic gcc.
  2643. if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
  2644. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  2645. D.Diag(clang::diag::err_drv_no_linker_llvm_support)
  2646. << getToolChain().getTripleString();
  2647. else if (II.getType() == types::TY_AST)
  2648. D.Diag(clang::diag::err_drv_no_ast_support)
  2649. << getToolChain().getTripleString();
  2650. if (II.isFilename())
  2651. CmdArgs.push_back(II.getFilename());
  2652. else
  2653. // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
  2654. II.getInputArg().render(Args, CmdArgs);
  2655. }
  2656. const char *GCCName = "hexagon-as";
  2657. const char *Exec =
  2658. Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
  2659. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  2660. }
  2661. void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
  2662. ArgStringList &CmdArgs) const {
  2663. // The types are (hopefully) good enough.
  2664. }
  2665. void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
  2666. const InputInfo &Output,
  2667. const InputInfoList &Inputs,
  2668. const ArgList &Args,
  2669. const char *LinkingOutput) const {
  2670. const Driver &D = getToolChain().getDriver();
  2671. ArgStringList CmdArgs;
  2672. for (ArgList::const_iterator
  2673. it = Args.begin(), ie = Args.end(); it != ie; ++it) {
  2674. Arg *A = *it;
  2675. if (A->getOption().hasForwardToGCC()) {
  2676. // Don't forward any -g arguments to assembly steps.
  2677. if (isa<AssembleJobAction>(JA) &&
  2678. A->getOption().matches(options::OPT_g_Group))
  2679. continue;
  2680. // It is unfortunate that we have to claim here, as this means
  2681. // we will basically never report anything interesting for
  2682. // platforms using a generic gcc, even if we are just using gcc
  2683. // to get to the assembler.
  2684. A->claim();
  2685. A->render(Args, CmdArgs);
  2686. }
  2687. }
  2688. RenderExtraToolArgs(JA, CmdArgs);
  2689. // Add Arch Information
  2690. Arg *A;
  2691. if ((A = getLastHexagonArchArg(Args))) {
  2692. if (A->getOption().matches(options::OPT_m_Joined))
  2693. A->render(Args, CmdArgs);
  2694. else
  2695. CmdArgs.push_back (Args.MakeArgString("-m" + getHexagonTargetCPU(Args)));
  2696. }
  2697. else {
  2698. CmdArgs.push_back (Args.MakeArgString("-m" + getHexagonTargetCPU(Args)));
  2699. }
  2700. CmdArgs.push_back("-mqdsp6-compat");
  2701. const char *GCCName;
  2702. if (C.getDriver().CCCIsCXX)
  2703. GCCName = "hexagon-g++";
  2704. else
  2705. GCCName = "hexagon-gcc";
  2706. const char *Exec =
  2707. Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
  2708. if (Output.isFilename()) {
  2709. CmdArgs.push_back("-o");
  2710. CmdArgs.push_back(Output.getFilename());
  2711. }
  2712. for (InputInfoList::const_iterator
  2713. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  2714. const InputInfo &II = *it;
  2715. // Don't try to pass LLVM or AST inputs to a generic gcc.
  2716. if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
  2717. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  2718. D.Diag(clang::diag::err_drv_no_linker_llvm_support)
  2719. << getToolChain().getTripleString();
  2720. else if (II.getType() == types::TY_AST)
  2721. D.Diag(clang::diag::err_drv_no_ast_support)
  2722. << getToolChain().getTripleString();
  2723. if (II.isFilename())
  2724. CmdArgs.push_back(II.getFilename());
  2725. else
  2726. // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
  2727. II.getInputArg().render(Args, CmdArgs);
  2728. }
  2729. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  2730. }
  2731. // Hexagon tools end.
  2732. const char *darwin::CC1::getCC1Name(types::ID Type) const {
  2733. switch (Type) {
  2734. default:
  2735. llvm_unreachable("Unexpected type for Darwin CC1 tool.");
  2736. case types::TY_Asm:
  2737. case types::TY_C: case types::TY_CHeader:
  2738. case types::TY_PP_C: case types::TY_PP_CHeader:
  2739. return "cc1";
  2740. case types::TY_ObjC: case types::TY_ObjCHeader:
  2741. case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
  2742. case types::TY_PP_ObjCHeader:
  2743. return "cc1obj";
  2744. case types::TY_CXX: case types::TY_CXXHeader:
  2745. case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
  2746. return "cc1plus";
  2747. case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
  2748. case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
  2749. case types::TY_PP_ObjCXXHeader:
  2750. return "cc1objplus";
  2751. }
  2752. }
  2753. void darwin::CC1::anchor() {}
  2754. const char *darwin::CC1::getBaseInputName(const ArgList &Args,
  2755. const InputInfoList &Inputs) {
  2756. return Args.MakeArgString(
  2757. llvm::sys::path::filename(Inputs[0].getBaseInput()));
  2758. }
  2759. const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
  2760. const InputInfoList &Inputs) {
  2761. const char *Str = getBaseInputName(Args, Inputs);
  2762. if (const char *End = strrchr(Str, '.'))
  2763. return Args.MakeArgString(std::string(Str, End));
  2764. return Str;
  2765. }
  2766. const char *
  2767. darwin::CC1::getDependencyFileName(const ArgList &Args,
  2768. const InputInfoList &Inputs) {
  2769. // FIXME: Think about this more.
  2770. std::string Res;
  2771. if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
  2772. std::string Str(OutputOpt->getValue(Args));
  2773. Res = Str.substr(0, Str.rfind('.'));
  2774. } else {
  2775. Res = darwin::CC1::getBaseInputStem(Args, Inputs);
  2776. }
  2777. return Args.MakeArgString(Res + ".d");
  2778. }
  2779. void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
  2780. for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
  2781. it != ie;) {
  2782. StringRef Option = *it;
  2783. bool RemoveOption = false;
  2784. // Erase both -fmodule-cache-path and its argument.
  2785. if (Option.equals("-fmodule-cache-path") && it+2 != ie) {
  2786. it = CmdArgs.erase(it, it+2);
  2787. ie = CmdArgs.end();
  2788. continue;
  2789. }
  2790. // Remove unsupported -f options.
  2791. if (Option.startswith("-f")) {
  2792. // Remove -f/-fno- to reduce the number of cases.
  2793. if (Option.startswith("-fno-"))
  2794. Option = Option.substr(5);
  2795. else
  2796. Option = Option.substr(2);
  2797. RemoveOption = llvm::StringSwitch<bool>(Option)
  2798. .Case("altivec", true)
  2799. .Case("modules", true)
  2800. .Case("diagnostics-show-note-include-stack", true)
  2801. .Default(false);
  2802. }
  2803. // Handle machine specific options.
  2804. if (Option.startswith("-m")) {
  2805. RemoveOption = llvm::StringSwitch<bool>(Option)
  2806. .Case("-mthumb", true)
  2807. .Case("-mno-thumb", true)
  2808. .Case("-mno-fused-madd", true)
  2809. .Case("-mlong-branch", true)
  2810. .Case("-mlongcall", true)
  2811. .Case("-mcpu=G4", true)
  2812. .Case("-mcpu=G5", true)
  2813. .Default(false);
  2814. }
  2815. // Handle warning options.
  2816. if (Option.startswith("-W")) {
  2817. // Remove -W/-Wno- to reduce the number of cases.
  2818. if (Option.startswith("-Wno-"))
  2819. Option = Option.substr(5);
  2820. else
  2821. Option = Option.substr(2);
  2822. RemoveOption = llvm::StringSwitch<bool>(Option)
  2823. .Case("address-of-temporary", true)
  2824. .Case("ambiguous-member-template", true)
  2825. .Case("analyzer-incompatible-plugin", true)
  2826. .Case("array-bounds", true)
  2827. .Case("array-bounds-pointer-arithmetic", true)
  2828. .Case("bind-to-temporary-copy", true)
  2829. .Case("bitwise-op-parentheses", true)
  2830. .Case("bool-conversions", true)
  2831. .Case("builtin-macro-redefined", true)
  2832. .Case("c++-hex-floats", true)
  2833. .Case("c++0x-compat", true)
  2834. .Case("c++0x-extensions", true)
  2835. .Case("c++0x-narrowing", true)
  2836. .Case("c++11-compat", true)
  2837. .Case("c++11-extensions", true)
  2838. .Case("c++11-narrowing", true)
  2839. .Case("conditional-uninitialized", true)
  2840. .Case("constant-conversion", true)
  2841. .Case("conversion-null", true)
  2842. .Case("CFString-literal", true)
  2843. .Case("constant-logical-operand", true)
  2844. .Case("custom-atomic-properties", true)
  2845. .Case("default-arg-special-member", true)
  2846. .Case("delegating-ctor-cycles", true)
  2847. .Case("delete-non-virtual-dtor", true)
  2848. .Case("deprecated-implementations", true)
  2849. .Case("deprecated-writable-strings", true)
  2850. .Case("distributed-object-modifiers", true)
  2851. .Case("duplicate-method-arg", true)
  2852. .Case("dynamic-class-memaccess", true)
  2853. .Case("enum-compare", true)
  2854. .Case("exit-time-destructors", true)
  2855. .Case("gnu", true)
  2856. .Case("gnu-designator", true)
  2857. .Case("header-hygiene", true)
  2858. .Case("idiomatic-parentheses", true)
  2859. .Case("ignored-qualifiers", true)
  2860. .Case("implicit-atomic-properties", true)
  2861. .Case("incompatible-pointer-types", true)
  2862. .Case("incomplete-implementation", true)
  2863. .Case("initializer-overrides", true)
  2864. .Case("invalid-noreturn", true)
  2865. .Case("invalid-token-paste", true)
  2866. .Case("language-extension-token", true)
  2867. .Case("literal-conversion", true)
  2868. .Case("literal-range", true)
  2869. .Case("local-type-template-args", true)
  2870. .Case("logical-op-parentheses", true)
  2871. .Case("method-signatures", true)
  2872. .Case("microsoft", true)
  2873. .Case("mismatched-tags", true)
  2874. .Case("missing-method-return-type", true)
  2875. .Case("non-pod-varargs", true)
  2876. .Case("nonfragile-abi2", true)
  2877. .Case("null-arithmetic", true)
  2878. .Case("null-dereference", true)
  2879. .Case("out-of-line-declaration", true)
  2880. .Case("overriding-method-mismatch", true)
  2881. .Case("readonly-setter-attrs", true)
  2882. .Case("return-stack-address", true)
  2883. .Case("self-assign", true)
  2884. .Case("semicolon-before-method-body", true)
  2885. .Case("sentinel", true)
  2886. .Case("shift-overflow", true)
  2887. .Case("shift-sign-overflow", true)
  2888. .Case("sign-conversion", true)
  2889. .Case("sizeof-array-argument", true)
  2890. .Case("sizeof-pointer-memaccess", true)
  2891. .Case("string-compare", true)
  2892. .Case("super-class-method-mismatch", true)
  2893. .Case("tautological-compare", true)
  2894. .Case("typedef-redefinition", true)
  2895. .Case("typename-missing", true)
  2896. .Case("undefined-reinterpret-cast", true)
  2897. .Case("unknown-warning-option", true)
  2898. .Case("unnamed-type-template-args", true)
  2899. .Case("unneeded-internal-declaration", true)
  2900. .Case("unneeded-member-function", true)
  2901. .Case("unused-comparison", true)
  2902. .Case("unused-exception-parameter", true)
  2903. .Case("unused-member-function", true)
  2904. .Case("unused-result", true)
  2905. .Case("vector-conversions", true)
  2906. .Case("vla", true)
  2907. .Case("used-but-marked-unused", true)
  2908. .Case("weak-vtables", true)
  2909. .Default(false);
  2910. } // if (Option.startswith("-W"))
  2911. if (RemoveOption) {
  2912. it = CmdArgs.erase(it);
  2913. ie = CmdArgs.end();
  2914. } else {
  2915. ++it;
  2916. }
  2917. }
  2918. }
  2919. void darwin::CC1::AddCC1Args(const ArgList &Args,
  2920. ArgStringList &CmdArgs) const {
  2921. const Driver &D = getToolChain().getDriver();
  2922. CheckCodeGenerationOptions(D, Args);
  2923. // Derived from cc1 spec.
  2924. if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
  2925. !Args.hasArg(options::OPT_mdynamic_no_pic))
  2926. CmdArgs.push_back("-fPIC");
  2927. if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
  2928. getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
  2929. if (!Args.hasArg(options::OPT_fbuiltin_strcat))
  2930. CmdArgs.push_back("-fno-builtin-strcat");
  2931. if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
  2932. CmdArgs.push_back("-fno-builtin-strcpy");
  2933. }
  2934. if (Args.hasArg(options::OPT_g_Flag) &&
  2935. !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
  2936. CmdArgs.push_back("-feliminate-unused-debug-symbols");
  2937. }
  2938. void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
  2939. const InputInfoList &Inputs,
  2940. const ArgStringList &OutputArgs) const {
  2941. const Driver &D = getToolChain().getDriver();
  2942. // Derived from cc1_options spec.
  2943. if (Args.hasArg(options::OPT_fast) ||
  2944. Args.hasArg(options::OPT_fastf) ||
  2945. Args.hasArg(options::OPT_fastcp))
  2946. CmdArgs.push_back("-O3");
  2947. if (Arg *A = Args.getLastArg(options::OPT_pg))
  2948. if (Args.hasArg(options::OPT_fomit_frame_pointer))
  2949. D.Diag(diag::err_drv_argument_not_allowed_with)
  2950. << A->getAsString(Args) << "-fomit-frame-pointer";
  2951. AddCC1Args(Args, CmdArgs);
  2952. if (!Args.hasArg(options::OPT_Q))
  2953. CmdArgs.push_back("-quiet");
  2954. CmdArgs.push_back("-dumpbase");
  2955. CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
  2956. Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
  2957. Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
  2958. Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
  2959. // FIXME: The goal is to use the user provided -o if that is our
  2960. // final output, otherwise to drive from the original input
  2961. // name. Find a clean way to go about this.
  2962. if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
  2963. Args.hasArg(options::OPT_o)) {
  2964. Arg *OutputOpt = Args.getLastArg(options::OPT_o);
  2965. CmdArgs.push_back("-auxbase-strip");
  2966. CmdArgs.push_back(OutputOpt->getValue(Args));
  2967. } else {
  2968. CmdArgs.push_back("-auxbase");
  2969. CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
  2970. }
  2971. Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
  2972. Args.AddAllArgs(CmdArgs, options::OPT_O);
  2973. // FIXME: -Wall is getting some special treatment. Investigate.
  2974. Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
  2975. Args.AddLastArg(CmdArgs, options::OPT_w);
  2976. Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
  2977. options::OPT_trigraphs);
  2978. if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
  2979. // Honor -std-default.
  2980. Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
  2981. "-std=", /*Joined=*/true);
  2982. }
  2983. if (Args.hasArg(options::OPT_v))
  2984. CmdArgs.push_back("-version");
  2985. if (Args.hasArg(options::OPT_pg) &&
  2986. getToolChain().SupportsProfiling())
  2987. CmdArgs.push_back("-p");
  2988. Args.AddLastArg(CmdArgs, options::OPT_p);
  2989. // The driver treats -fsyntax-only specially.
  2990. if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
  2991. getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
  2992. // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
  2993. // used to inhibit the default -fno-builtin-str{cat,cpy}.
  2994. //
  2995. // FIXME: Should we grow a better way to deal with "removing" args?
  2996. for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
  2997. options::OPT_fsyntax_only),
  2998. ie = Args.filtered_end(); it != ie; ++it) {
  2999. if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
  3000. !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
  3001. (*it)->claim();
  3002. (*it)->render(Args, CmdArgs);
  3003. }
  3004. }
  3005. } else
  3006. Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
  3007. // Claim Clang only -f options, they aren't worth warning about.
  3008. Args.ClaimAllArgs(options::OPT_f_clang_Group);
  3009. Args.AddAllArgs(CmdArgs, options::OPT_undef);
  3010. if (Args.hasArg(options::OPT_Qn))
  3011. CmdArgs.push_back("-fno-ident");
  3012. // FIXME: This isn't correct.
  3013. //Args.AddLastArg(CmdArgs, options::OPT__help)
  3014. //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
  3015. CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
  3016. // FIXME: Still don't get what is happening here. Investigate.
  3017. Args.AddAllArgs(CmdArgs, options::OPT__param);
  3018. if (Args.hasArg(options::OPT_fmudflap) ||
  3019. Args.hasArg(options::OPT_fmudflapth)) {
  3020. CmdArgs.push_back("-fno-builtin");
  3021. CmdArgs.push_back("-fno-merge-constants");
  3022. }
  3023. if (Args.hasArg(options::OPT_coverage)) {
  3024. CmdArgs.push_back("-fprofile-arcs");
  3025. CmdArgs.push_back("-ftest-coverage");
  3026. }
  3027. if (types::isCXX(Inputs[0].getType()))
  3028. CmdArgs.push_back("-D__private_extern__=extern");
  3029. }
  3030. void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
  3031. const InputInfoList &Inputs,
  3032. const ArgStringList &OutputArgs) const {
  3033. // Derived from cpp_options
  3034. AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
  3035. CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
  3036. AddCC1Args(Args, CmdArgs);
  3037. // NOTE: The code below has some commonality with cpp_options, but
  3038. // in classic gcc style ends up sending things in different
  3039. // orders. This may be a good merge candidate once we drop pedantic
  3040. // compatibility.
  3041. Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
  3042. Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
  3043. options::OPT_trigraphs);
  3044. if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
  3045. // Honor -std-default.
  3046. Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
  3047. "-std=", /*Joined=*/true);
  3048. }
  3049. Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
  3050. Args.AddLastArg(CmdArgs, options::OPT_w);
  3051. // The driver treats -fsyntax-only specially.
  3052. Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
  3053. // Claim Clang only -f options, they aren't worth warning about.
  3054. Args.ClaimAllArgs(options::OPT_f_clang_Group);
  3055. if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
  3056. !Args.hasArg(options::OPT_fno_working_directory))
  3057. CmdArgs.push_back("-fworking-directory");
  3058. Args.AddAllArgs(CmdArgs, options::OPT_O);
  3059. Args.AddAllArgs(CmdArgs, options::OPT_undef);
  3060. if (Args.hasArg(options::OPT_save_temps))
  3061. CmdArgs.push_back("-fpch-preprocess");
  3062. }
  3063. void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
  3064. ArgStringList &CmdArgs,
  3065. const InputInfoList &Inputs) const {
  3066. const Driver &D = getToolChain().getDriver();
  3067. CheckPreprocessingOptions(D, Args);
  3068. // Derived from cpp_unique_options.
  3069. // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
  3070. Args.AddLastArg(CmdArgs, options::OPT_C);
  3071. Args.AddLastArg(CmdArgs, options::OPT_CC);
  3072. if (!Args.hasArg(options::OPT_Q))
  3073. CmdArgs.push_back("-quiet");
  3074. Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
  3075. Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
  3076. Args.AddLastArg(CmdArgs, options::OPT_v);
  3077. Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
  3078. Args.AddLastArg(CmdArgs, options::OPT_P);
  3079. // FIXME: Handle %I properly.
  3080. if (getToolChain().getArchName() == "x86_64") {
  3081. CmdArgs.push_back("-imultilib");
  3082. CmdArgs.push_back("x86_64");
  3083. }
  3084. if (Args.hasArg(options::OPT_MD)) {
  3085. CmdArgs.push_back("-MD");
  3086. CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
  3087. }
  3088. if (Args.hasArg(options::OPT_MMD)) {
  3089. CmdArgs.push_back("-MMD");
  3090. CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
  3091. }
  3092. Args.AddLastArg(CmdArgs, options::OPT_M);
  3093. Args.AddLastArg(CmdArgs, options::OPT_MM);
  3094. Args.AddAllArgs(CmdArgs, options::OPT_MF);
  3095. Args.AddLastArg(CmdArgs, options::OPT_MG);
  3096. Args.AddLastArg(CmdArgs, options::OPT_MP);
  3097. Args.AddAllArgs(CmdArgs, options::OPT_MQ);
  3098. Args.AddAllArgs(CmdArgs, options::OPT_MT);
  3099. if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
  3100. (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
  3101. if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
  3102. CmdArgs.push_back("-MQ");
  3103. CmdArgs.push_back(OutputOpt->getValue(Args));
  3104. }
  3105. }
  3106. Args.AddLastArg(CmdArgs, options::OPT_remap);
  3107. if (Args.hasArg(options::OPT_g3))
  3108. CmdArgs.push_back("-dD");
  3109. Args.AddLastArg(CmdArgs, options::OPT_H);
  3110. AddCPPArgs(Args, CmdArgs);
  3111. Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
  3112. Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
  3113. for (InputInfoList::const_iterator
  3114. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  3115. const InputInfo &II = *it;
  3116. CmdArgs.push_back(II.getFilename());
  3117. }
  3118. Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
  3119. options::OPT_Xpreprocessor);
  3120. if (Args.hasArg(options::OPT_fmudflap)) {
  3121. CmdArgs.push_back("-D_MUDFLAP");
  3122. CmdArgs.push_back("-include");
  3123. CmdArgs.push_back("mf-runtime.h");
  3124. }
  3125. if (Args.hasArg(options::OPT_fmudflapth)) {
  3126. CmdArgs.push_back("-D_MUDFLAP");
  3127. CmdArgs.push_back("-D_MUDFLAPTH");
  3128. CmdArgs.push_back("-include");
  3129. CmdArgs.push_back("mf-runtime.h");
  3130. }
  3131. }
  3132. void darwin::CC1::AddCPPArgs(const ArgList &Args,
  3133. ArgStringList &CmdArgs) const {
  3134. // Derived from cpp spec.
  3135. if (Args.hasArg(options::OPT_static)) {
  3136. // The gcc spec is broken here, it refers to dynamic but
  3137. // that has been translated. Start by being bug compatible.
  3138. // if (!Args.hasArg(arglist.parser.dynamicOption))
  3139. CmdArgs.push_back("-D__STATIC__");
  3140. } else
  3141. CmdArgs.push_back("-D__DYNAMIC__");
  3142. if (Args.hasArg(options::OPT_pthread))
  3143. CmdArgs.push_back("-D_REENTRANT");
  3144. }
  3145. void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
  3146. const InputInfo &Output,
  3147. const InputInfoList &Inputs,
  3148. const ArgList &Args,
  3149. const char *LinkingOutput) const {
  3150. ArgStringList CmdArgs;
  3151. assert(Inputs.size() == 1 && "Unexpected number of inputs!");
  3152. CmdArgs.push_back("-E");
  3153. if (Args.hasArg(options::OPT_traditional) ||
  3154. Args.hasArg(options::OPT_traditional_cpp))
  3155. CmdArgs.push_back("-traditional-cpp");
  3156. ArgStringList OutputArgs;
  3157. assert(Output.isFilename() && "Unexpected CC1 output.");
  3158. OutputArgs.push_back("-o");
  3159. OutputArgs.push_back(Output.getFilename());
  3160. if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
  3161. AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
  3162. } else {
  3163. AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
  3164. CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
  3165. }
  3166. Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
  3167. RemoveCC1UnsupportedArgs(CmdArgs);
  3168. const char *CC1Name = getCC1Name(Inputs[0].getType());
  3169. const char *Exec =
  3170. Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
  3171. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3172. }
  3173. void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
  3174. const InputInfo &Output,
  3175. const InputInfoList &Inputs,
  3176. const ArgList &Args,
  3177. const char *LinkingOutput) const {
  3178. const Driver &D = getToolChain().getDriver();
  3179. ArgStringList CmdArgs;
  3180. assert(Inputs.size() == 1 && "Unexpected number of inputs!");
  3181. // Silence warning about unused --serialize-diagnostics
  3182. Args.ClaimAllArgs(options::OPT__serialize_diags);
  3183. types::ID InputType = Inputs[0].getType();
  3184. if (const Arg *A = Args.getLastArg(options::OPT_traditional))
  3185. D.Diag(diag::err_drv_argument_only_allowed_with)
  3186. << A->getAsString(Args) << "-E";
  3187. if (JA.getType() == types::TY_LLVM_IR ||
  3188. JA.getType() == types::TY_LTO_IR)
  3189. CmdArgs.push_back("-emit-llvm");
  3190. else if (JA.getType() == types::TY_LLVM_BC ||
  3191. JA.getType() == types::TY_LTO_BC)
  3192. CmdArgs.push_back("-emit-llvm-bc");
  3193. else if (Output.getType() == types::TY_AST)
  3194. D.Diag(diag::err_drv_no_ast_support)
  3195. << getToolChain().getTripleString();
  3196. else if (JA.getType() != types::TY_PP_Asm &&
  3197. JA.getType() != types::TY_PCH)
  3198. D.Diag(diag::err_drv_invalid_gcc_output_type)
  3199. << getTypeName(JA.getType());
  3200. ArgStringList OutputArgs;
  3201. if (Output.getType() != types::TY_PCH) {
  3202. OutputArgs.push_back("-o");
  3203. if (Output.isNothing())
  3204. OutputArgs.push_back("/dev/null");
  3205. else
  3206. OutputArgs.push_back(Output.getFilename());
  3207. }
  3208. // There is no need for this level of compatibility, but it makes
  3209. // diffing easier.
  3210. bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
  3211. Args.hasArg(options::OPT_S));
  3212. if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
  3213. AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
  3214. if (OutputArgsEarly) {
  3215. AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
  3216. } else {
  3217. AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
  3218. CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
  3219. }
  3220. } else {
  3221. CmdArgs.push_back("-fpreprocessed");
  3222. for (InputInfoList::const_iterator
  3223. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  3224. const InputInfo &II = *it;
  3225. // Reject AST inputs.
  3226. if (II.getType() == types::TY_AST) {
  3227. D.Diag(diag::err_drv_no_ast_support)
  3228. << getToolChain().getTripleString();
  3229. return;
  3230. }
  3231. CmdArgs.push_back(II.getFilename());
  3232. }
  3233. if (OutputArgsEarly) {
  3234. AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
  3235. } else {
  3236. AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
  3237. CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
  3238. }
  3239. }
  3240. if (Output.getType() == types::TY_PCH) {
  3241. assert(Output.isFilename() && "Invalid PCH output.");
  3242. CmdArgs.push_back("-o");
  3243. // NOTE: gcc uses a temp .s file for this, but there doesn't seem
  3244. // to be a good reason.
  3245. const char *TmpPath = C.getArgs().MakeArgString(
  3246. D.GetTemporaryPath("cc", "s"));
  3247. C.addTempFile(TmpPath);
  3248. CmdArgs.push_back(TmpPath);
  3249. // If we're emitting a pch file with the last 4 characters of ".pth"
  3250. // and falling back to llvm-gcc we want to use ".gch" instead.
  3251. std::string OutputFile(Output.getFilename());
  3252. size_t loc = OutputFile.rfind(".pth");
  3253. if (loc != std::string::npos)
  3254. OutputFile.replace(loc, 4, ".gch");
  3255. const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
  3256. CmdArgs.push_back(Tmp);
  3257. }
  3258. RemoveCC1UnsupportedArgs(CmdArgs);
  3259. const char *CC1Name = getCC1Name(Inputs[0].getType());
  3260. const char *Exec =
  3261. Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
  3262. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3263. }
  3264. void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  3265. const InputInfo &Output,
  3266. const InputInfoList &Inputs,
  3267. const ArgList &Args,
  3268. const char *LinkingOutput) const {
  3269. ArgStringList CmdArgs;
  3270. assert(Inputs.size() == 1 && "Unexpected number of inputs.");
  3271. const InputInfo &Input = Inputs[0];
  3272. // Determine the original source input.
  3273. const Action *SourceAction = &JA;
  3274. while (SourceAction->getKind() != Action::InputClass) {
  3275. assert(!SourceAction->getInputs().empty() && "unexpected root action!");
  3276. SourceAction = SourceAction->getInputs()[0];
  3277. }
  3278. // Forward -g, assuming we are dealing with an actual assembly file.
  3279. if (SourceAction->getType() == types::TY_Asm ||
  3280. SourceAction->getType() == types::TY_PP_Asm) {
  3281. if (Args.hasArg(options::OPT_gstabs))
  3282. CmdArgs.push_back("--gstabs");
  3283. else if (Args.hasArg(options::OPT_g_Group))
  3284. CmdArgs.push_back("-g");
  3285. }
  3286. // Derived from asm spec.
  3287. AddDarwinArch(Args, CmdArgs);
  3288. // Use -force_cpusubtype_ALL on x86 by default.
  3289. if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
  3290. getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
  3291. Args.hasArg(options::OPT_force__cpusubtype__ALL))
  3292. CmdArgs.push_back("-force_cpusubtype_ALL");
  3293. if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
  3294. (Args.hasArg(options::OPT_mkernel) ||
  3295. Args.hasArg(options::OPT_static) ||
  3296. Args.hasArg(options::OPT_fapple_kext)))
  3297. CmdArgs.push_back("-static");
  3298. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  3299. options::OPT_Xassembler);
  3300. assert(Output.isFilename() && "Unexpected lipo output.");
  3301. CmdArgs.push_back("-o");
  3302. CmdArgs.push_back(Output.getFilename());
  3303. assert(Input.isFilename() && "Invalid input.");
  3304. CmdArgs.push_back(Input.getFilename());
  3305. // asm_final spec is empty.
  3306. const char *Exec =
  3307. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  3308. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3309. }
  3310. void darwin::DarwinTool::anchor() {}
  3311. void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
  3312. ArgStringList &CmdArgs) const {
  3313. StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
  3314. // Derived from darwin_arch spec.
  3315. CmdArgs.push_back("-arch");
  3316. CmdArgs.push_back(Args.MakeArgString(ArchName));
  3317. // FIXME: Is this needed anymore?
  3318. if (ArchName == "arm")
  3319. CmdArgs.push_back("-force_cpusubtype_ALL");
  3320. }
  3321. void darwin::Link::AddLinkArgs(Compilation &C,
  3322. const ArgList &Args,
  3323. ArgStringList &CmdArgs) const {
  3324. const Driver &D = getToolChain().getDriver();
  3325. const toolchains::Darwin &DarwinTC = getDarwinToolChain();
  3326. unsigned Version[3] = { 0, 0, 0 };
  3327. if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
  3328. bool HadExtra;
  3329. if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
  3330. Version[1], Version[2], HadExtra) ||
  3331. HadExtra)
  3332. D.Diag(diag::err_drv_invalid_version_number)
  3333. << A->getAsString(Args);
  3334. }
  3335. // Newer linkers support -demangle, pass it if supported and not disabled by
  3336. // the user.
  3337. if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) {
  3338. // Don't pass -demangle to ld_classic.
  3339. //
  3340. // FIXME: This is a temporary workaround, ld should be handling this.
  3341. bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
  3342. Args.hasArg(options::OPT_static));
  3343. if (getToolChain().getArch() == llvm::Triple::x86) {
  3344. for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
  3345. options::OPT_Wl_COMMA),
  3346. ie = Args.filtered_end(); it != ie; ++it) {
  3347. const Arg *A = *it;
  3348. for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
  3349. if (StringRef(A->getValue(Args, i)) == "-kext")
  3350. UsesLdClassic = true;
  3351. }
  3352. }
  3353. if (!UsesLdClassic)
  3354. CmdArgs.push_back("-demangle");
  3355. }
  3356. // If we are using LTO, then automatically create a temporary file path for
  3357. // the linker to use, so that it's lifetime will extend past a possible
  3358. // dsymutil step.
  3359. if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
  3360. const char *TmpPath = C.getArgs().MakeArgString(
  3361. D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
  3362. C.addTempFile(TmpPath);
  3363. CmdArgs.push_back("-object_path_lto");
  3364. CmdArgs.push_back(TmpPath);
  3365. }
  3366. // Derived from the "link" spec.
  3367. Args.AddAllArgs(CmdArgs, options::OPT_static);
  3368. if (!Args.hasArg(options::OPT_static))
  3369. CmdArgs.push_back("-dynamic");
  3370. if (Args.hasArg(options::OPT_fgnu_runtime)) {
  3371. // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
  3372. // here. How do we wish to handle such things?
  3373. }
  3374. if (!Args.hasArg(options::OPT_dynamiclib)) {
  3375. AddDarwinArch(Args, CmdArgs);
  3376. // FIXME: Why do this only on this path?
  3377. Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
  3378. Args.AddLastArg(CmdArgs, options::OPT_bundle);
  3379. Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
  3380. Args.AddAllArgs(CmdArgs, options::OPT_client__name);
  3381. Arg *A;
  3382. if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
  3383. (A = Args.getLastArg(options::OPT_current__version)) ||
  3384. (A = Args.getLastArg(options::OPT_install__name)))
  3385. D.Diag(diag::err_drv_argument_only_allowed_with)
  3386. << A->getAsString(Args) << "-dynamiclib";
  3387. Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
  3388. Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
  3389. Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
  3390. } else {
  3391. CmdArgs.push_back("-dylib");
  3392. Arg *A;
  3393. if ((A = Args.getLastArg(options::OPT_bundle)) ||
  3394. (A = Args.getLastArg(options::OPT_bundle__loader)) ||
  3395. (A = Args.getLastArg(options::OPT_client__name)) ||
  3396. (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
  3397. (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
  3398. (A = Args.getLastArg(options::OPT_private__bundle)))
  3399. D.Diag(diag::err_drv_argument_not_allowed_with)
  3400. << A->getAsString(Args) << "-dynamiclib";
  3401. Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
  3402. "-dylib_compatibility_version");
  3403. Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
  3404. "-dylib_current_version");
  3405. AddDarwinArch(Args, CmdArgs);
  3406. Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
  3407. "-dylib_install_name");
  3408. }
  3409. Args.AddLastArg(CmdArgs, options::OPT_all__load);
  3410. Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
  3411. Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
  3412. if (DarwinTC.isTargetIPhoneOS())
  3413. Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
  3414. Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
  3415. Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
  3416. Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
  3417. Args.AddLastArg(CmdArgs, options::OPT_dynamic);
  3418. Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
  3419. Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
  3420. Args.AddAllArgs(CmdArgs, options::OPT_force__load);
  3421. Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
  3422. Args.AddAllArgs(CmdArgs, options::OPT_image__base);
  3423. Args.AddAllArgs(CmdArgs, options::OPT_init);
  3424. // Add the deployment target.
  3425. VersionTuple TargetVersion = DarwinTC.getTargetVersion();
  3426. // If we had an explicit -mios-simulator-version-min argument, honor that,
  3427. // otherwise use the traditional deployment targets. We can't just check the
  3428. // is-sim attribute because existing code follows this path, and the linker
  3429. // may not handle the argument.
  3430. //
  3431. // FIXME: We may be able to remove this, once we can verify no one depends on
  3432. // it.
  3433. if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
  3434. CmdArgs.push_back("-ios_simulator_version_min");
  3435. else if (DarwinTC.isTargetIPhoneOS())
  3436. CmdArgs.push_back("-iphoneos_version_min");
  3437. else
  3438. CmdArgs.push_back("-macosx_version_min");
  3439. CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
  3440. Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
  3441. Args.AddLastArg(CmdArgs, options::OPT_multi__module);
  3442. Args.AddLastArg(CmdArgs, options::OPT_single__module);
  3443. Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
  3444. Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
  3445. if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
  3446. options::OPT_fno_pie,
  3447. options::OPT_fno_PIE)) {
  3448. if (A->getOption().matches(options::OPT_fpie) ||
  3449. A->getOption().matches(options::OPT_fPIE))
  3450. CmdArgs.push_back("-pie");
  3451. else
  3452. CmdArgs.push_back("-no_pie");
  3453. }
  3454. Args.AddLastArg(CmdArgs, options::OPT_prebind);
  3455. Args.AddLastArg(CmdArgs, options::OPT_noprebind);
  3456. Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
  3457. Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
  3458. Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
  3459. Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
  3460. Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
  3461. Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
  3462. Args.AddAllArgs(CmdArgs, options::OPT_segprot);
  3463. Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
  3464. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
  3465. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
  3466. Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
  3467. Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
  3468. Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
  3469. Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
  3470. // Give --sysroot= preference, over the Apple specific behavior to also use
  3471. // --isysroot as the syslibroot.
  3472. StringRef sysroot = C.getSysRoot();
  3473. if (sysroot != "") {
  3474. CmdArgs.push_back("-syslibroot");
  3475. CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
  3476. } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
  3477. CmdArgs.push_back("-syslibroot");
  3478. CmdArgs.push_back(A->getValue(Args));
  3479. }
  3480. Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
  3481. Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
  3482. Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
  3483. Args.AddAllArgs(CmdArgs, options::OPT_undefined);
  3484. Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
  3485. Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
  3486. Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
  3487. Args.AddAllArgs(CmdArgs, options::OPT_y);
  3488. Args.AddLastArg(CmdArgs, options::OPT_w);
  3489. Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
  3490. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
  3491. Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
  3492. Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
  3493. Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
  3494. Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
  3495. Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
  3496. Args.AddLastArg(CmdArgs, options::OPT_whyload);
  3497. Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
  3498. Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
  3499. Args.AddLastArg(CmdArgs, options::OPT_dylinker);
  3500. Args.AddLastArg(CmdArgs, options::OPT_Mach);
  3501. }
  3502. void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
  3503. const InputInfo &Output,
  3504. const InputInfoList &Inputs,
  3505. const ArgList &Args,
  3506. const char *LinkingOutput) const {
  3507. assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
  3508. // The logic here is derived from gcc's behavior; most of which
  3509. // comes from specs (starting with link_command). Consult gcc for
  3510. // more information.
  3511. ArgStringList CmdArgs;
  3512. /// Hack(tm) to ignore linking errors when we are doing ARC migration.
  3513. if (Args.hasArg(options::OPT_ccc_arcmt_check,
  3514. options::OPT_ccc_arcmt_migrate)) {
  3515. for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
  3516. (*I)->claim();
  3517. const char *Exec =
  3518. Args.MakeArgString(getToolChain().GetProgramPath("touch"));
  3519. CmdArgs.push_back(Output.getFilename());
  3520. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3521. return;
  3522. }
  3523. // I'm not sure why this particular decomposition exists in gcc, but
  3524. // we follow suite for ease of comparison.
  3525. AddLinkArgs(C, Args, CmdArgs);
  3526. Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
  3527. Args.AddAllArgs(CmdArgs, options::OPT_s);
  3528. Args.AddAllArgs(CmdArgs, options::OPT_t);
  3529. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  3530. Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
  3531. Args.AddAllArgs(CmdArgs, options::OPT_A);
  3532. Args.AddLastArg(CmdArgs, options::OPT_e);
  3533. Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
  3534. Args.AddAllArgs(CmdArgs, options::OPT_r);
  3535. // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
  3536. // members of static archive libraries which implement Objective-C classes or
  3537. // categories.
  3538. if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
  3539. CmdArgs.push_back("-ObjC");
  3540. CmdArgs.push_back("-o");
  3541. CmdArgs.push_back(Output.getFilename());
  3542. if (!Args.hasArg(options::OPT_A) &&
  3543. !Args.hasArg(options::OPT_nostdlib) &&
  3544. !Args.hasArg(options::OPT_nostartfiles)) {
  3545. // Derived from startfile spec.
  3546. if (Args.hasArg(options::OPT_dynamiclib)) {
  3547. // Derived from darwin_dylib1 spec.
  3548. if (getDarwinToolChain().isTargetIOSSimulator()) {
  3549. // The simulator doesn't have a versioned crt1 file.
  3550. CmdArgs.push_back("-ldylib1.o");
  3551. } else if (getDarwinToolChain().isTargetIPhoneOS()) {
  3552. if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
  3553. CmdArgs.push_back("-ldylib1.o");
  3554. } else {
  3555. if (getDarwinToolChain().isMacosxVersionLT(10, 5))
  3556. CmdArgs.push_back("-ldylib1.o");
  3557. else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
  3558. CmdArgs.push_back("-ldylib1.10.5.o");
  3559. }
  3560. } else {
  3561. if (Args.hasArg(options::OPT_bundle)) {
  3562. if (!Args.hasArg(options::OPT_static)) {
  3563. // Derived from darwin_bundle1 spec.
  3564. if (getDarwinToolChain().isTargetIOSSimulator()) {
  3565. // The simulator doesn't have a versioned crt1 file.
  3566. CmdArgs.push_back("-lbundle1.o");
  3567. } else if (getDarwinToolChain().isTargetIPhoneOS()) {
  3568. if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
  3569. CmdArgs.push_back("-lbundle1.o");
  3570. } else {
  3571. if (getDarwinToolChain().isMacosxVersionLT(10, 6))
  3572. CmdArgs.push_back("-lbundle1.o");
  3573. }
  3574. }
  3575. } else {
  3576. if (Args.hasArg(options::OPT_pg) &&
  3577. getToolChain().SupportsProfiling()) {
  3578. if (Args.hasArg(options::OPT_static) ||
  3579. Args.hasArg(options::OPT_object) ||
  3580. Args.hasArg(options::OPT_preload)) {
  3581. CmdArgs.push_back("-lgcrt0.o");
  3582. } else {
  3583. CmdArgs.push_back("-lgcrt1.o");
  3584. // darwin_crt2 spec is empty.
  3585. }
  3586. } else {
  3587. if (Args.hasArg(options::OPT_static) ||
  3588. Args.hasArg(options::OPT_object) ||
  3589. Args.hasArg(options::OPT_preload)) {
  3590. CmdArgs.push_back("-lcrt0.o");
  3591. } else {
  3592. // Derived from darwin_crt1 spec.
  3593. if (getDarwinToolChain().isTargetIOSSimulator()) {
  3594. // The simulator doesn't have a versioned crt1 file.
  3595. CmdArgs.push_back("-lcrt1.o");
  3596. } else if (getDarwinToolChain().isTargetIPhoneOS()) {
  3597. if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
  3598. CmdArgs.push_back("-lcrt1.o");
  3599. else
  3600. CmdArgs.push_back("-lcrt1.3.1.o");
  3601. } else {
  3602. if (getDarwinToolChain().isMacosxVersionLT(10, 5))
  3603. CmdArgs.push_back("-lcrt1.o");
  3604. else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
  3605. CmdArgs.push_back("-lcrt1.10.5.o");
  3606. else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
  3607. CmdArgs.push_back("-lcrt1.10.6.o");
  3608. // darwin_crt2 spec is empty.
  3609. }
  3610. }
  3611. }
  3612. }
  3613. }
  3614. if (!getDarwinToolChain().isTargetIPhoneOS() &&
  3615. Args.hasArg(options::OPT_shared_libgcc) &&
  3616. getDarwinToolChain().isMacosxVersionLT(10, 5)) {
  3617. const char *Str =
  3618. Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
  3619. CmdArgs.push_back(Str);
  3620. }
  3621. }
  3622. Args.AddAllArgs(CmdArgs, options::OPT_L);
  3623. // If we're building a dynamic lib with -faddress-sanitizer, unresolved
  3624. // symbols may appear. Mark all of them as dynamic_lookup.
  3625. // Linking executables is handled in lib/Driver/ToolChains.cpp.
  3626. if (Args.hasFlag(options::OPT_faddress_sanitizer,
  3627. options::OPT_fno_address_sanitizer, false)) {
  3628. if (Args.hasArg(options::OPT_dynamiclib) ||
  3629. Args.hasArg(options::OPT_bundle)) {
  3630. CmdArgs.push_back("-undefined");
  3631. CmdArgs.push_back("dynamic_lookup");
  3632. }
  3633. }
  3634. if (Args.hasArg(options::OPT_fopenmp))
  3635. // This is more complicated in gcc...
  3636. CmdArgs.push_back("-lgomp");
  3637. if (isObjCRuntimeLinked(Args)) {
  3638. // Avoid linking compatibility stubs on i386 mac.
  3639. if (!getDarwinToolChain().isTargetMacOS() ||
  3640. getDarwinToolChain().getArchName() != "i386") {
  3641. // If we don't have ARC or subscripting runtime support, link in the
  3642. // runtime stubs. We have to do this *before* adding any of the normal
  3643. // linker inputs so that its initializer gets run first.
  3644. ObjCRuntime runtime;
  3645. getDarwinToolChain().configureObjCRuntime(runtime);
  3646. // We use arclite library for both ARC and subscripting support.
  3647. if ((!runtime.HasARC && isObjCAutoRefCount(Args)) ||
  3648. !runtime.HasSubscripting)
  3649. getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
  3650. }
  3651. CmdArgs.push_back("-framework");
  3652. CmdArgs.push_back("Foundation");
  3653. // Link libobj.
  3654. CmdArgs.push_back("-lobjc");
  3655. }
  3656. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  3657. if (LinkingOutput) {
  3658. CmdArgs.push_back("-arch_multiple");
  3659. CmdArgs.push_back("-final_output");
  3660. CmdArgs.push_back(LinkingOutput);
  3661. }
  3662. if (Args.hasArg(options::OPT_fnested_functions))
  3663. CmdArgs.push_back("-allow_stack_execute");
  3664. if (!Args.hasArg(options::OPT_nostdlib) &&
  3665. !Args.hasArg(options::OPT_nodefaultlibs)) {
  3666. if (getToolChain().getDriver().CCCIsCXX)
  3667. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  3668. // link_ssp spec is empty.
  3669. // Let the tool chain choose which runtime library to link.
  3670. getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
  3671. }
  3672. if (!Args.hasArg(options::OPT_A) &&
  3673. !Args.hasArg(options::OPT_nostdlib) &&
  3674. !Args.hasArg(options::OPT_nostartfiles)) {
  3675. // endfile_spec is empty.
  3676. }
  3677. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  3678. Args.AddAllArgs(CmdArgs, options::OPT_F);
  3679. const char *Exec =
  3680. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  3681. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3682. }
  3683. void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
  3684. const InputInfo &Output,
  3685. const InputInfoList &Inputs,
  3686. const ArgList &Args,
  3687. const char *LinkingOutput) const {
  3688. ArgStringList CmdArgs;
  3689. CmdArgs.push_back("-create");
  3690. assert(Output.isFilename() && "Unexpected lipo output.");
  3691. CmdArgs.push_back("-output");
  3692. CmdArgs.push_back(Output.getFilename());
  3693. for (InputInfoList::const_iterator
  3694. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  3695. const InputInfo &II = *it;
  3696. assert(II.isFilename() && "Unexpected lipo input.");
  3697. CmdArgs.push_back(II.getFilename());
  3698. }
  3699. const char *Exec =
  3700. Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
  3701. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3702. }
  3703. void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
  3704. const InputInfo &Output,
  3705. const InputInfoList &Inputs,
  3706. const ArgList &Args,
  3707. const char *LinkingOutput) const {
  3708. ArgStringList CmdArgs;
  3709. CmdArgs.push_back("-o");
  3710. CmdArgs.push_back(Output.getFilename());
  3711. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  3712. const InputInfo &Input = Inputs[0];
  3713. assert(Input.isFilename() && "Unexpected dsymutil input.");
  3714. CmdArgs.push_back(Input.getFilename());
  3715. const char *Exec =
  3716. Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
  3717. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3718. }
  3719. void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
  3720. const InputInfo &Output,
  3721. const InputInfoList &Inputs,
  3722. const ArgList &Args,
  3723. const char *LinkingOutput) const {
  3724. ArgStringList CmdArgs;
  3725. CmdArgs.push_back("--verify");
  3726. CmdArgs.push_back("--debug-info");
  3727. CmdArgs.push_back("--eh-frame");
  3728. CmdArgs.push_back("--quiet");
  3729. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  3730. const InputInfo &Input = Inputs[0];
  3731. assert(Input.isFilename() && "Unexpected verify input");
  3732. // Grabbing the output of the earlier dsymutil run.
  3733. CmdArgs.push_back(Input.getFilename());
  3734. const char *Exec =
  3735. Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
  3736. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3737. }
  3738. void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  3739. const InputInfo &Output,
  3740. const InputInfoList &Inputs,
  3741. const ArgList &Args,
  3742. const char *LinkingOutput) const {
  3743. ArgStringList CmdArgs;
  3744. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  3745. options::OPT_Xassembler);
  3746. CmdArgs.push_back("-o");
  3747. CmdArgs.push_back(Output.getFilename());
  3748. for (InputInfoList::const_iterator
  3749. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  3750. const InputInfo &II = *it;
  3751. CmdArgs.push_back(II.getFilename());
  3752. }
  3753. const char *Exec =
  3754. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  3755. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3756. }
  3757. void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
  3758. const InputInfo &Output,
  3759. const InputInfoList &Inputs,
  3760. const ArgList &Args,
  3761. const char *LinkingOutput) const {
  3762. // FIXME: Find a real GCC, don't hard-code versions here
  3763. std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
  3764. const llvm::Triple &T = getToolChain().getTriple();
  3765. std::string LibPath = "/usr/lib/";
  3766. llvm::Triple::ArchType Arch = T.getArch();
  3767. switch (Arch) {
  3768. case llvm::Triple::x86:
  3769. GCCLibPath += ("i386-" + T.getVendorName() + "-" +
  3770. T.getOSName()).str() + "/4.5.2/";
  3771. break;
  3772. case llvm::Triple::x86_64:
  3773. GCCLibPath += ("i386-" + T.getVendorName() + "-" +
  3774. T.getOSName()).str();
  3775. GCCLibPath += "/4.5.2/amd64/";
  3776. LibPath += "amd64/";
  3777. break;
  3778. default:
  3779. assert(0 && "Unsupported architecture");
  3780. }
  3781. ArgStringList CmdArgs;
  3782. // Demangle C++ names in errors
  3783. CmdArgs.push_back("-C");
  3784. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  3785. (!Args.hasArg(options::OPT_shared))) {
  3786. CmdArgs.push_back("-e");
  3787. CmdArgs.push_back("_start");
  3788. }
  3789. if (Args.hasArg(options::OPT_static)) {
  3790. CmdArgs.push_back("-Bstatic");
  3791. CmdArgs.push_back("-dn");
  3792. } else {
  3793. CmdArgs.push_back("-Bdynamic");
  3794. if (Args.hasArg(options::OPT_shared)) {
  3795. CmdArgs.push_back("-shared");
  3796. } else {
  3797. CmdArgs.push_back("--dynamic-linker");
  3798. CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
  3799. }
  3800. }
  3801. if (Output.isFilename()) {
  3802. CmdArgs.push_back("-o");
  3803. CmdArgs.push_back(Output.getFilename());
  3804. } else {
  3805. assert(Output.isNothing() && "Invalid output.");
  3806. }
  3807. if (!Args.hasArg(options::OPT_nostdlib) &&
  3808. !Args.hasArg(options::OPT_nostartfiles)) {
  3809. if (!Args.hasArg(options::OPT_shared)) {
  3810. CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
  3811. CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
  3812. CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
  3813. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
  3814. } else {
  3815. CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
  3816. CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
  3817. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
  3818. }
  3819. if (getToolChain().getDriver().CCCIsCXX)
  3820. CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
  3821. }
  3822. CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
  3823. Args.AddAllArgs(CmdArgs, options::OPT_L);
  3824. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  3825. Args.AddAllArgs(CmdArgs, options::OPT_e);
  3826. Args.AddAllArgs(CmdArgs, options::OPT_r);
  3827. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  3828. if (!Args.hasArg(options::OPT_nostdlib) &&
  3829. !Args.hasArg(options::OPT_nodefaultlibs)) {
  3830. if (getToolChain().getDriver().CCCIsCXX)
  3831. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  3832. CmdArgs.push_back("-lgcc_s");
  3833. if (!Args.hasArg(options::OPT_shared)) {
  3834. CmdArgs.push_back("-lgcc");
  3835. CmdArgs.push_back("-lc");
  3836. CmdArgs.push_back("-lm");
  3837. }
  3838. }
  3839. if (!Args.hasArg(options::OPT_nostdlib) &&
  3840. !Args.hasArg(options::OPT_nostartfiles)) {
  3841. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
  3842. }
  3843. CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
  3844. addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
  3845. const char *Exec =
  3846. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  3847. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3848. }
  3849. void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  3850. const InputInfo &Output,
  3851. const InputInfoList &Inputs,
  3852. const ArgList &Args,
  3853. const char *LinkingOutput) const {
  3854. ArgStringList CmdArgs;
  3855. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  3856. options::OPT_Xassembler);
  3857. CmdArgs.push_back("-o");
  3858. CmdArgs.push_back(Output.getFilename());
  3859. for (InputInfoList::const_iterator
  3860. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  3861. const InputInfo &II = *it;
  3862. CmdArgs.push_back(II.getFilename());
  3863. }
  3864. const char *Exec =
  3865. Args.MakeArgString(getToolChain().GetProgramPath("gas"));
  3866. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3867. }
  3868. void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
  3869. const InputInfo &Output,
  3870. const InputInfoList &Inputs,
  3871. const ArgList &Args,
  3872. const char *LinkingOutput) const {
  3873. ArgStringList CmdArgs;
  3874. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  3875. (!Args.hasArg(options::OPT_shared))) {
  3876. CmdArgs.push_back("-e");
  3877. CmdArgs.push_back("_start");
  3878. }
  3879. if (Args.hasArg(options::OPT_static)) {
  3880. CmdArgs.push_back("-Bstatic");
  3881. CmdArgs.push_back("-dn");
  3882. } else {
  3883. // CmdArgs.push_back("--eh-frame-hdr");
  3884. CmdArgs.push_back("-Bdynamic");
  3885. if (Args.hasArg(options::OPT_shared)) {
  3886. CmdArgs.push_back("-shared");
  3887. } else {
  3888. CmdArgs.push_back("--dynamic-linker");
  3889. CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
  3890. }
  3891. }
  3892. if (Output.isFilename()) {
  3893. CmdArgs.push_back("-o");
  3894. CmdArgs.push_back(Output.getFilename());
  3895. } else {
  3896. assert(Output.isNothing() && "Invalid output.");
  3897. }
  3898. if (!Args.hasArg(options::OPT_nostdlib) &&
  3899. !Args.hasArg(options::OPT_nostartfiles)) {
  3900. if (!Args.hasArg(options::OPT_shared)) {
  3901. CmdArgs.push_back(Args.MakeArgString(
  3902. getToolChain().GetFilePath("crt1.o")));
  3903. CmdArgs.push_back(Args.MakeArgString(
  3904. getToolChain().GetFilePath("crti.o")));
  3905. CmdArgs.push_back(Args.MakeArgString(
  3906. getToolChain().GetFilePath("crtbegin.o")));
  3907. } else {
  3908. CmdArgs.push_back(Args.MakeArgString(
  3909. getToolChain().GetFilePath("crti.o")));
  3910. }
  3911. CmdArgs.push_back(Args.MakeArgString(
  3912. getToolChain().GetFilePath("crtn.o")));
  3913. }
  3914. CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
  3915. + getToolChain().getTripleString()
  3916. + "/4.2.4"));
  3917. Args.AddAllArgs(CmdArgs, options::OPT_L);
  3918. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  3919. Args.AddAllArgs(CmdArgs, options::OPT_e);
  3920. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  3921. if (!Args.hasArg(options::OPT_nostdlib) &&
  3922. !Args.hasArg(options::OPT_nodefaultlibs)) {
  3923. // FIXME: For some reason GCC passes -lgcc before adding
  3924. // the default system libraries. Just mimic this for now.
  3925. CmdArgs.push_back("-lgcc");
  3926. if (Args.hasArg(options::OPT_pthread))
  3927. CmdArgs.push_back("-pthread");
  3928. if (!Args.hasArg(options::OPT_shared))
  3929. CmdArgs.push_back("-lc");
  3930. CmdArgs.push_back("-lgcc");
  3931. }
  3932. if (!Args.hasArg(options::OPT_nostdlib) &&
  3933. !Args.hasArg(options::OPT_nostartfiles)) {
  3934. if (!Args.hasArg(options::OPT_shared))
  3935. CmdArgs.push_back(Args.MakeArgString(
  3936. getToolChain().GetFilePath("crtend.o")));
  3937. }
  3938. addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
  3939. const char *Exec =
  3940. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  3941. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3942. }
  3943. void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  3944. const InputInfo &Output,
  3945. const InputInfoList &Inputs,
  3946. const ArgList &Args,
  3947. const char *LinkingOutput) const {
  3948. ArgStringList CmdArgs;
  3949. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  3950. options::OPT_Xassembler);
  3951. CmdArgs.push_back("-o");
  3952. CmdArgs.push_back(Output.getFilename());
  3953. for (InputInfoList::const_iterator
  3954. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  3955. const InputInfo &II = *it;
  3956. CmdArgs.push_back(II.getFilename());
  3957. }
  3958. const char *Exec =
  3959. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  3960. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3961. }
  3962. void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
  3963. const InputInfo &Output,
  3964. const InputInfoList &Inputs,
  3965. const ArgList &Args,
  3966. const char *LinkingOutput) const {
  3967. const Driver &D = getToolChain().getDriver();
  3968. ArgStringList CmdArgs;
  3969. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  3970. (!Args.hasArg(options::OPT_shared))) {
  3971. CmdArgs.push_back("-e");
  3972. CmdArgs.push_back("__start");
  3973. }
  3974. if (Args.hasArg(options::OPT_static)) {
  3975. CmdArgs.push_back("-Bstatic");
  3976. } else {
  3977. if (Args.hasArg(options::OPT_rdynamic))
  3978. CmdArgs.push_back("-export-dynamic");
  3979. CmdArgs.push_back("--eh-frame-hdr");
  3980. CmdArgs.push_back("-Bdynamic");
  3981. if (Args.hasArg(options::OPT_shared)) {
  3982. CmdArgs.push_back("-shared");
  3983. } else {
  3984. CmdArgs.push_back("-dynamic-linker");
  3985. CmdArgs.push_back("/usr/libexec/ld.so");
  3986. }
  3987. }
  3988. if (Output.isFilename()) {
  3989. CmdArgs.push_back("-o");
  3990. CmdArgs.push_back(Output.getFilename());
  3991. } else {
  3992. assert(Output.isNothing() && "Invalid output.");
  3993. }
  3994. if (!Args.hasArg(options::OPT_nostdlib) &&
  3995. !Args.hasArg(options::OPT_nostartfiles)) {
  3996. if (!Args.hasArg(options::OPT_shared)) {
  3997. if (Args.hasArg(options::OPT_pg))
  3998. CmdArgs.push_back(Args.MakeArgString(
  3999. getToolChain().GetFilePath("gcrt0.o")));
  4000. else
  4001. CmdArgs.push_back(Args.MakeArgString(
  4002. getToolChain().GetFilePath("crt0.o")));
  4003. CmdArgs.push_back(Args.MakeArgString(
  4004. getToolChain().GetFilePath("crtbegin.o")));
  4005. } else {
  4006. CmdArgs.push_back(Args.MakeArgString(
  4007. getToolChain().GetFilePath("crtbeginS.o")));
  4008. }
  4009. }
  4010. std::string Triple = getToolChain().getTripleString();
  4011. if (Triple.substr(0, 6) == "x86_64")
  4012. Triple.replace(0, 6, "amd64");
  4013. CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
  4014. "/4.2.1"));
  4015. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4016. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4017. Args.AddAllArgs(CmdArgs, options::OPT_e);
  4018. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4019. if (!Args.hasArg(options::OPT_nostdlib) &&
  4020. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4021. if (D.CCCIsCXX) {
  4022. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  4023. if (Args.hasArg(options::OPT_pg))
  4024. CmdArgs.push_back("-lm_p");
  4025. else
  4026. CmdArgs.push_back("-lm");
  4027. }
  4028. // FIXME: For some reason GCC passes -lgcc before adding
  4029. // the default system libraries. Just mimic this for now.
  4030. CmdArgs.push_back("-lgcc");
  4031. if (Args.hasArg(options::OPT_pthread))
  4032. CmdArgs.push_back("-lpthread");
  4033. if (!Args.hasArg(options::OPT_shared)) {
  4034. if (Args.hasArg(options::OPT_pg))
  4035. CmdArgs.push_back("-lc_p");
  4036. else
  4037. CmdArgs.push_back("-lc");
  4038. }
  4039. CmdArgs.push_back("-lgcc");
  4040. }
  4041. if (!Args.hasArg(options::OPT_nostdlib) &&
  4042. !Args.hasArg(options::OPT_nostartfiles)) {
  4043. if (!Args.hasArg(options::OPT_shared))
  4044. CmdArgs.push_back(Args.MakeArgString(
  4045. getToolChain().GetFilePath("crtend.o")));
  4046. else
  4047. CmdArgs.push_back(Args.MakeArgString(
  4048. getToolChain().GetFilePath("crtendS.o")));
  4049. }
  4050. const char *Exec =
  4051. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  4052. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4053. }
  4054. void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4055. const InputInfo &Output,
  4056. const InputInfoList &Inputs,
  4057. const ArgList &Args,
  4058. const char *LinkingOutput) const {
  4059. ArgStringList CmdArgs;
  4060. // When building 32-bit code on FreeBSD/amd64, we have to explicitly
  4061. // instruct as in the base system to assemble 32-bit code.
  4062. if (getToolChain().getArchName() == "i386")
  4063. CmdArgs.push_back("--32");
  4064. if (getToolChain().getArchName() == "powerpc")
  4065. CmdArgs.push_back("-a32");
  4066. // Set byte order explicitly
  4067. if (getToolChain().getArchName() == "mips")
  4068. CmdArgs.push_back("-EB");
  4069. else if (getToolChain().getArchName() == "mipsel")
  4070. CmdArgs.push_back("-EL");
  4071. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4072. options::OPT_Xassembler);
  4073. CmdArgs.push_back("-o");
  4074. CmdArgs.push_back(Output.getFilename());
  4075. for (InputInfoList::const_iterator
  4076. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4077. const InputInfo &II = *it;
  4078. CmdArgs.push_back(II.getFilename());
  4079. }
  4080. const char *Exec =
  4081. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  4082. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4083. }
  4084. void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4085. const InputInfo &Output,
  4086. const InputInfoList &Inputs,
  4087. const ArgList &Args,
  4088. const char *LinkingOutput) const {
  4089. const Driver &D = getToolChain().getDriver();
  4090. ArgStringList CmdArgs;
  4091. if (!D.SysRoot.empty())
  4092. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  4093. if (Args.hasArg(options::OPT_static)) {
  4094. CmdArgs.push_back("-Bstatic");
  4095. } else {
  4096. if (Args.hasArg(options::OPT_rdynamic))
  4097. CmdArgs.push_back("-export-dynamic");
  4098. CmdArgs.push_back("--eh-frame-hdr");
  4099. if (Args.hasArg(options::OPT_shared)) {
  4100. CmdArgs.push_back("-Bshareable");
  4101. } else {
  4102. CmdArgs.push_back("-dynamic-linker");
  4103. CmdArgs.push_back("/libexec/ld-elf.so.1");
  4104. }
  4105. }
  4106. // When building 32-bit code on FreeBSD/amd64, we have to explicitly
  4107. // instruct ld in the base system to link 32-bit code.
  4108. if (getToolChain().getArchName() == "i386") {
  4109. CmdArgs.push_back("-m");
  4110. CmdArgs.push_back("elf_i386_fbsd");
  4111. }
  4112. if (getToolChain().getArchName() == "powerpc") {
  4113. CmdArgs.push_back("-m");
  4114. CmdArgs.push_back("elf32ppc_fbsd");
  4115. }
  4116. if (Output.isFilename()) {
  4117. CmdArgs.push_back("-o");
  4118. CmdArgs.push_back(Output.getFilename());
  4119. } else {
  4120. assert(Output.isNothing() && "Invalid output.");
  4121. }
  4122. if (!Args.hasArg(options::OPT_nostdlib) &&
  4123. !Args.hasArg(options::OPT_nostartfiles)) {
  4124. if (!Args.hasArg(options::OPT_shared)) {
  4125. if (Args.hasArg(options::OPT_pg))
  4126. CmdArgs.push_back(Args.MakeArgString(
  4127. getToolChain().GetFilePath("gcrt1.o")));
  4128. else {
  4129. const char *crt = Args.hasArg(options::OPT_pie) ? "Scrt1.o" : "crt1.o";
  4130. CmdArgs.push_back(Args.MakeArgString(
  4131. getToolChain().GetFilePath(crt)));
  4132. }
  4133. CmdArgs.push_back(Args.MakeArgString(
  4134. getToolChain().GetFilePath("crti.o")));
  4135. CmdArgs.push_back(Args.MakeArgString(
  4136. getToolChain().GetFilePath("crtbegin.o")));
  4137. } else {
  4138. CmdArgs.push_back(Args.MakeArgString(
  4139. getToolChain().GetFilePath("crti.o")));
  4140. CmdArgs.push_back(Args.MakeArgString(
  4141. getToolChain().GetFilePath("crtbeginS.o")));
  4142. }
  4143. }
  4144. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4145. const ToolChain::path_list Paths = getToolChain().getFilePaths();
  4146. for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
  4147. i != e; ++i)
  4148. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
  4149. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4150. Args.AddAllArgs(CmdArgs, options::OPT_e);
  4151. Args.AddAllArgs(CmdArgs, options::OPT_s);
  4152. Args.AddAllArgs(CmdArgs, options::OPT_t);
  4153. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  4154. Args.AddAllArgs(CmdArgs, options::OPT_r);
  4155. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4156. if (!Args.hasArg(options::OPT_nostdlib) &&
  4157. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4158. if (D.CCCIsCXX) {
  4159. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  4160. if (Args.hasArg(options::OPT_pg))
  4161. CmdArgs.push_back("-lm_p");
  4162. else
  4163. CmdArgs.push_back("-lm");
  4164. }
  4165. // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
  4166. // the default system libraries. Just mimic this for now.
  4167. if (Args.hasArg(options::OPT_pg))
  4168. CmdArgs.push_back("-lgcc_p");
  4169. else
  4170. CmdArgs.push_back("-lgcc");
  4171. if (Args.hasArg(options::OPT_static)) {
  4172. CmdArgs.push_back("-lgcc_eh");
  4173. } else if (Args.hasArg(options::OPT_pg)) {
  4174. CmdArgs.push_back("-lgcc_eh_p");
  4175. } else {
  4176. CmdArgs.push_back("--as-needed");
  4177. CmdArgs.push_back("-lgcc_s");
  4178. CmdArgs.push_back("--no-as-needed");
  4179. }
  4180. if (Args.hasArg(options::OPT_pthread)) {
  4181. if (Args.hasArg(options::OPT_pg))
  4182. CmdArgs.push_back("-lpthread_p");
  4183. else
  4184. CmdArgs.push_back("-lpthread");
  4185. }
  4186. if (Args.hasArg(options::OPT_pg)) {
  4187. if (Args.hasArg(options::OPT_shared))
  4188. CmdArgs.push_back("-lc");
  4189. else
  4190. CmdArgs.push_back("-lc_p");
  4191. CmdArgs.push_back("-lgcc_p");
  4192. } else {
  4193. CmdArgs.push_back("-lc");
  4194. CmdArgs.push_back("-lgcc");
  4195. }
  4196. if (Args.hasArg(options::OPT_static)) {
  4197. CmdArgs.push_back("-lgcc_eh");
  4198. } else if (Args.hasArg(options::OPT_pg)) {
  4199. CmdArgs.push_back("-lgcc_eh_p");
  4200. } else {
  4201. CmdArgs.push_back("--as-needed");
  4202. CmdArgs.push_back("-lgcc_s");
  4203. CmdArgs.push_back("--no-as-needed");
  4204. }
  4205. }
  4206. if (!Args.hasArg(options::OPT_nostdlib) &&
  4207. !Args.hasArg(options::OPT_nostartfiles)) {
  4208. if (!Args.hasArg(options::OPT_shared))
  4209. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  4210. "crtend.o")));
  4211. else
  4212. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  4213. "crtendS.o")));
  4214. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  4215. "crtn.o")));
  4216. }
  4217. addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
  4218. const char *Exec =
  4219. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  4220. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4221. }
  4222. void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4223. const InputInfo &Output,
  4224. const InputInfoList &Inputs,
  4225. const ArgList &Args,
  4226. const char *LinkingOutput) const {
  4227. ArgStringList CmdArgs;
  4228. // When building 32-bit code on NetBSD/amd64, we have to explicitly
  4229. // instruct as in the base system to assemble 32-bit code.
  4230. if (getToolChain().getArch() == llvm::Triple::x86)
  4231. CmdArgs.push_back("--32");
  4232. // Set byte order explicitly
  4233. if (getToolChain().getArchName() == "mips")
  4234. CmdArgs.push_back("-EB");
  4235. else if (getToolChain().getArchName() == "mipsel")
  4236. CmdArgs.push_back("-EL");
  4237. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4238. options::OPT_Xassembler);
  4239. CmdArgs.push_back("-o");
  4240. CmdArgs.push_back(Output.getFilename());
  4241. for (InputInfoList::const_iterator
  4242. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4243. const InputInfo &II = *it;
  4244. CmdArgs.push_back(II.getFilename());
  4245. }
  4246. const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
  4247. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4248. }
  4249. void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4250. const InputInfo &Output,
  4251. const InputInfoList &Inputs,
  4252. const ArgList &Args,
  4253. const char *LinkingOutput) const {
  4254. const Driver &D = getToolChain().getDriver();
  4255. ArgStringList CmdArgs;
  4256. if (!D.SysRoot.empty())
  4257. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  4258. if (Args.hasArg(options::OPT_static)) {
  4259. CmdArgs.push_back("-Bstatic");
  4260. } else {
  4261. if (Args.hasArg(options::OPT_rdynamic))
  4262. CmdArgs.push_back("-export-dynamic");
  4263. CmdArgs.push_back("--eh-frame-hdr");
  4264. if (Args.hasArg(options::OPT_shared)) {
  4265. CmdArgs.push_back("-Bshareable");
  4266. } else {
  4267. CmdArgs.push_back("-dynamic-linker");
  4268. CmdArgs.push_back("/libexec/ld.elf_so");
  4269. }
  4270. }
  4271. // When building 32-bit code on NetBSD/amd64, we have to explicitly
  4272. // instruct ld in the base system to link 32-bit code.
  4273. if (getToolChain().getArch() == llvm::Triple::x86) {
  4274. CmdArgs.push_back("-m");
  4275. CmdArgs.push_back("elf_i386");
  4276. }
  4277. if (Output.isFilename()) {
  4278. CmdArgs.push_back("-o");
  4279. CmdArgs.push_back(Output.getFilename());
  4280. } else {
  4281. assert(Output.isNothing() && "Invalid output.");
  4282. }
  4283. if (!Args.hasArg(options::OPT_nostdlib) &&
  4284. !Args.hasArg(options::OPT_nostartfiles)) {
  4285. if (!Args.hasArg(options::OPT_shared)) {
  4286. CmdArgs.push_back(Args.MakeArgString(
  4287. getToolChain().GetFilePath("crt0.o")));
  4288. CmdArgs.push_back(Args.MakeArgString(
  4289. getToolChain().GetFilePath("crti.o")));
  4290. CmdArgs.push_back(Args.MakeArgString(
  4291. getToolChain().GetFilePath("crtbegin.o")));
  4292. } else {
  4293. CmdArgs.push_back(Args.MakeArgString(
  4294. getToolChain().GetFilePath("crti.o")));
  4295. CmdArgs.push_back(Args.MakeArgString(
  4296. getToolChain().GetFilePath("crtbeginS.o")));
  4297. }
  4298. }
  4299. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4300. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4301. Args.AddAllArgs(CmdArgs, options::OPT_e);
  4302. Args.AddAllArgs(CmdArgs, options::OPT_s);
  4303. Args.AddAllArgs(CmdArgs, options::OPT_t);
  4304. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  4305. Args.AddAllArgs(CmdArgs, options::OPT_r);
  4306. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4307. if (!Args.hasArg(options::OPT_nostdlib) &&
  4308. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4309. if (D.CCCIsCXX) {
  4310. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  4311. CmdArgs.push_back("-lm");
  4312. }
  4313. // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
  4314. // the default system libraries. Just mimic this for now.
  4315. if (Args.hasArg(options::OPT_static)) {
  4316. CmdArgs.push_back("-lgcc_eh");
  4317. } else {
  4318. CmdArgs.push_back("--as-needed");
  4319. CmdArgs.push_back("-lgcc_s");
  4320. CmdArgs.push_back("--no-as-needed");
  4321. }
  4322. CmdArgs.push_back("-lgcc");
  4323. if (Args.hasArg(options::OPT_pthread))
  4324. CmdArgs.push_back("-lpthread");
  4325. CmdArgs.push_back("-lc");
  4326. CmdArgs.push_back("-lgcc");
  4327. if (Args.hasArg(options::OPT_static)) {
  4328. CmdArgs.push_back("-lgcc_eh");
  4329. } else {
  4330. CmdArgs.push_back("--as-needed");
  4331. CmdArgs.push_back("-lgcc_s");
  4332. CmdArgs.push_back("--no-as-needed");
  4333. }
  4334. }
  4335. if (!Args.hasArg(options::OPT_nostdlib) &&
  4336. !Args.hasArg(options::OPT_nostartfiles)) {
  4337. if (!Args.hasArg(options::OPT_shared))
  4338. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  4339. "crtend.o")));
  4340. else
  4341. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  4342. "crtendS.o")));
  4343. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  4344. "crtn.o")));
  4345. }
  4346. addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
  4347. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  4348. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4349. }
  4350. void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4351. const InputInfo &Output,
  4352. const InputInfoList &Inputs,
  4353. const ArgList &Args,
  4354. const char *LinkingOutput) const {
  4355. ArgStringList CmdArgs;
  4356. // Add --32/--64 to make sure we get the format we want.
  4357. // This is incomplete
  4358. if (getToolChain().getArch() == llvm::Triple::x86) {
  4359. CmdArgs.push_back("--32");
  4360. } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
  4361. CmdArgs.push_back("--64");
  4362. } else if (getToolChain().getArch() == llvm::Triple::ppc) {
  4363. CmdArgs.push_back("-a32");
  4364. CmdArgs.push_back("-mppc");
  4365. CmdArgs.push_back("-many");
  4366. } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
  4367. CmdArgs.push_back("-a64");
  4368. CmdArgs.push_back("-mppc64");
  4369. CmdArgs.push_back("-many");
  4370. } else if (getToolChain().getArch() == llvm::Triple::arm) {
  4371. StringRef MArch = getToolChain().getArchName();
  4372. if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
  4373. CmdArgs.push_back("-mfpu=neon");
  4374. StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args,
  4375. getToolChain().getTriple());
  4376. CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
  4377. Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
  4378. Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
  4379. Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
  4380. } else if (getToolChain().getArch() == llvm::Triple::mips ||
  4381. getToolChain().getArch() == llvm::Triple::mipsel ||
  4382. getToolChain().getArch() == llvm::Triple::mips64 ||
  4383. getToolChain().getArch() == llvm::Triple::mips64el) {
  4384. StringRef CPUName;
  4385. StringRef ABIName;
  4386. getMipsCPUAndABI(Args, getToolChain(), CPUName, ABIName);
  4387. CmdArgs.push_back("-march");
  4388. CmdArgs.push_back(CPUName.data());
  4389. // Convert ABI name to the GNU tools acceptable variant.
  4390. if (ABIName == "o32")
  4391. ABIName = "32";
  4392. else if (ABIName == "n64")
  4393. ABIName = "64";
  4394. CmdArgs.push_back("-mabi");
  4395. CmdArgs.push_back(ABIName.data());
  4396. if (getToolChain().getArch() == llvm::Triple::mips ||
  4397. getToolChain().getArch() == llvm::Triple::mips64)
  4398. CmdArgs.push_back("-EB");
  4399. else
  4400. CmdArgs.push_back("-EL");
  4401. }
  4402. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4403. options::OPT_Xassembler);
  4404. CmdArgs.push_back("-o");
  4405. CmdArgs.push_back(Output.getFilename());
  4406. for (InputInfoList::const_iterator
  4407. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4408. const InputInfo &II = *it;
  4409. CmdArgs.push_back(II.getFilename());
  4410. }
  4411. const char *Exec =
  4412. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  4413. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4414. }
  4415. static void AddLibgcc(llvm::Triple Triple, const Driver &D,
  4416. ArgStringList &CmdArgs, const ArgList &Args) {
  4417. bool isAndroid = Triple.getEnvironment() == llvm::Triple::ANDROIDEABI;
  4418. bool StaticLibgcc = isAndroid || Args.hasArg(options::OPT_static) ||
  4419. Args.hasArg(options::OPT_static_libgcc);
  4420. if (!D.CCCIsCXX)
  4421. CmdArgs.push_back("-lgcc");
  4422. if (StaticLibgcc) {
  4423. if (D.CCCIsCXX)
  4424. CmdArgs.push_back("-lgcc");
  4425. } else {
  4426. if (!D.CCCIsCXX)
  4427. CmdArgs.push_back("--as-needed");
  4428. CmdArgs.push_back("-lgcc_s");
  4429. if (!D.CCCIsCXX)
  4430. CmdArgs.push_back("--no-as-needed");
  4431. }
  4432. if (StaticLibgcc && !isAndroid)
  4433. CmdArgs.push_back("-lgcc_eh");
  4434. else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
  4435. CmdArgs.push_back("-lgcc");
  4436. }
  4437. void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4438. const InputInfo &Output,
  4439. const InputInfoList &Inputs,
  4440. const ArgList &Args,
  4441. const char *LinkingOutput) const {
  4442. const toolchains::Linux& ToolChain =
  4443. static_cast<const toolchains::Linux&>(getToolChain());
  4444. const Driver &D = ToolChain.getDriver();
  4445. const bool isAndroid = ToolChain.getTriple().getEnvironment() ==
  4446. llvm::Triple::ANDROIDEABI;
  4447. ArgStringList CmdArgs;
  4448. // Silence warning for "clang -g foo.o -o foo"
  4449. Args.ClaimAllArgs(options::OPT_g_Group);
  4450. // and "clang -emit-llvm foo.o -o foo"
  4451. Args.ClaimAllArgs(options::OPT_emit_llvm);
  4452. // and for "clang -g foo.o -o foo". Other warning options are already
  4453. // handled somewhere else.
  4454. Args.ClaimAllArgs(options::OPT_w);
  4455. if (!D.SysRoot.empty())
  4456. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  4457. if (Args.hasArg(options::OPT_pie))
  4458. CmdArgs.push_back("-pie");
  4459. if (Args.hasArg(options::OPT_rdynamic))
  4460. CmdArgs.push_back("-export-dynamic");
  4461. if (Args.hasArg(options::OPT_s))
  4462. CmdArgs.push_back("-s");
  4463. for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
  4464. e = ToolChain.ExtraOpts.end();
  4465. i != e; ++i)
  4466. CmdArgs.push_back(i->c_str());
  4467. if (!Args.hasArg(options::OPT_static)) {
  4468. CmdArgs.push_back("--eh-frame-hdr");
  4469. }
  4470. CmdArgs.push_back("-m");
  4471. if (ToolChain.getArch() == llvm::Triple::x86)
  4472. CmdArgs.push_back("elf_i386");
  4473. else if (ToolChain.getArch() == llvm::Triple::arm
  4474. || ToolChain.getArch() == llvm::Triple::thumb)
  4475. CmdArgs.push_back("armelf_linux_eabi");
  4476. else if (ToolChain.getArch() == llvm::Triple::ppc)
  4477. CmdArgs.push_back("elf32ppclinux");
  4478. else if (ToolChain.getArch() == llvm::Triple::ppc64)
  4479. CmdArgs.push_back("elf64ppc");
  4480. else if (ToolChain.getArch() == llvm::Triple::mips)
  4481. CmdArgs.push_back("elf32btsmip");
  4482. else if (ToolChain.getArch() == llvm::Triple::mipsel)
  4483. CmdArgs.push_back("elf32ltsmip");
  4484. else if (ToolChain.getArch() == llvm::Triple::mips64)
  4485. CmdArgs.push_back("elf64btsmip");
  4486. else if (ToolChain.getArch() == llvm::Triple::mips64el)
  4487. CmdArgs.push_back("elf64ltsmip");
  4488. else
  4489. CmdArgs.push_back("elf_x86_64");
  4490. if (Args.hasArg(options::OPT_static)) {
  4491. if (ToolChain.getArch() == llvm::Triple::arm
  4492. || ToolChain.getArch() == llvm::Triple::thumb)
  4493. CmdArgs.push_back("-Bstatic");
  4494. else
  4495. CmdArgs.push_back("-static");
  4496. } else if (Args.hasArg(options::OPT_shared)) {
  4497. CmdArgs.push_back("-shared");
  4498. if ((ToolChain.getArch() == llvm::Triple::arm
  4499. || ToolChain.getArch() == llvm::Triple::thumb) && isAndroid) {
  4500. CmdArgs.push_back("-Bsymbolic");
  4501. }
  4502. }
  4503. if (ToolChain.getArch() == llvm::Triple::arm ||
  4504. ToolChain.getArch() == llvm::Triple::thumb ||
  4505. (!Args.hasArg(options::OPT_static) &&
  4506. !Args.hasArg(options::OPT_shared))) {
  4507. CmdArgs.push_back("-dynamic-linker");
  4508. if (isAndroid)
  4509. CmdArgs.push_back("/system/bin/linker");
  4510. else if (ToolChain.getArch() == llvm::Triple::x86)
  4511. CmdArgs.push_back("/lib/ld-linux.so.2");
  4512. else if (ToolChain.getArch() == llvm::Triple::arm ||
  4513. ToolChain.getArch() == llvm::Triple::thumb)
  4514. CmdArgs.push_back("/lib/ld-linux.so.3");
  4515. else if (ToolChain.getArch() == llvm::Triple::mips ||
  4516. ToolChain.getArch() == llvm::Triple::mipsel)
  4517. CmdArgs.push_back("/lib/ld.so.1");
  4518. else if (ToolChain.getArch() == llvm::Triple::mips64 ||
  4519. ToolChain.getArch() == llvm::Triple::mips64el)
  4520. CmdArgs.push_back("/lib64/ld.so.1");
  4521. else if (ToolChain.getArch() == llvm::Triple::ppc)
  4522. CmdArgs.push_back("/lib/ld.so.1");
  4523. else if (ToolChain.getArch() == llvm::Triple::ppc64)
  4524. CmdArgs.push_back("/lib64/ld64.so.1");
  4525. else
  4526. CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
  4527. }
  4528. CmdArgs.push_back("-o");
  4529. CmdArgs.push_back(Output.getFilename());
  4530. if (!Args.hasArg(options::OPT_nostdlib) &&
  4531. !Args.hasArg(options::OPT_nostartfiles)) {
  4532. if (!isAndroid) {
  4533. const char *crt1 = NULL;
  4534. if (!Args.hasArg(options::OPT_shared)){
  4535. if (Args.hasArg(options::OPT_pie))
  4536. crt1 = "Scrt1.o";
  4537. else
  4538. crt1 = "crt1.o";
  4539. }
  4540. if (crt1)
  4541. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
  4542. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  4543. }
  4544. const char *crtbegin;
  4545. if (Args.hasArg(options::OPT_static))
  4546. crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
  4547. else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  4548. crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
  4549. else
  4550. crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
  4551. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  4552. }
  4553. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4554. const ToolChain::path_list Paths = ToolChain.getFilePaths();
  4555. for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
  4556. i != e; ++i)
  4557. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
  4558. // Tell the linker to load the plugin. This has to come before AddLinkerInputs
  4559. // as gold requires -plugin to come before any -plugin-opt that -Wl might
  4560. // forward.
  4561. if (D.IsUsingLTO(Args) || Args.hasArg(options::OPT_use_gold_plugin)) {
  4562. CmdArgs.push_back("-plugin");
  4563. std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
  4564. CmdArgs.push_back(Args.MakeArgString(Plugin));
  4565. }
  4566. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  4567. if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
  4568. bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
  4569. !Args.hasArg(options::OPT_static);
  4570. if (OnlyLibstdcxxStatic)
  4571. CmdArgs.push_back("-Bstatic");
  4572. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  4573. if (OnlyLibstdcxxStatic)
  4574. CmdArgs.push_back("-Bdynamic");
  4575. CmdArgs.push_back("-lm");
  4576. }
  4577. // Call this before we add the C run-time.
  4578. addAsanRTLinux(getToolChain(), Args, CmdArgs);
  4579. if (!Args.hasArg(options::OPT_nostdlib)) {
  4580. if (Args.hasArg(options::OPT_static))
  4581. CmdArgs.push_back("--start-group");
  4582. AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
  4583. if (Args.hasArg(options::OPT_pthread) ||
  4584. Args.hasArg(options::OPT_pthreads))
  4585. CmdArgs.push_back("-lpthread");
  4586. CmdArgs.push_back("-lc");
  4587. if (Args.hasArg(options::OPT_static))
  4588. CmdArgs.push_back("--end-group");
  4589. else
  4590. AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
  4591. if (!Args.hasArg(options::OPT_nostartfiles)) {
  4592. const char *crtend;
  4593. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  4594. crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
  4595. else
  4596. crtend = isAndroid ? "crtend_android.o" : "crtend.o";
  4597. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
  4598. if (!isAndroid)
  4599. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  4600. }
  4601. }
  4602. addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
  4603. C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
  4604. }
  4605. void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4606. const InputInfo &Output,
  4607. const InputInfoList &Inputs,
  4608. const ArgList &Args,
  4609. const char *LinkingOutput) const {
  4610. ArgStringList CmdArgs;
  4611. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4612. options::OPT_Xassembler);
  4613. CmdArgs.push_back("-o");
  4614. CmdArgs.push_back(Output.getFilename());
  4615. for (InputInfoList::const_iterator
  4616. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4617. const InputInfo &II = *it;
  4618. CmdArgs.push_back(II.getFilename());
  4619. }
  4620. const char *Exec =
  4621. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  4622. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4623. }
  4624. void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4625. const InputInfo &Output,
  4626. const InputInfoList &Inputs,
  4627. const ArgList &Args,
  4628. const char *LinkingOutput) const {
  4629. const Driver &D = getToolChain().getDriver();
  4630. ArgStringList CmdArgs;
  4631. if (Output.isFilename()) {
  4632. CmdArgs.push_back("-o");
  4633. CmdArgs.push_back(Output.getFilename());
  4634. } else {
  4635. assert(Output.isNothing() && "Invalid output.");
  4636. }
  4637. if (!Args.hasArg(options::OPT_nostdlib) &&
  4638. !Args.hasArg(options::OPT_nostartfiles)) {
  4639. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
  4640. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  4641. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  4642. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
  4643. }
  4644. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4645. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4646. Args.AddAllArgs(CmdArgs, options::OPT_e);
  4647. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4648. addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
  4649. if (!Args.hasArg(options::OPT_nostdlib) &&
  4650. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4651. if (D.CCCIsCXX) {
  4652. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  4653. CmdArgs.push_back("-lm");
  4654. }
  4655. }
  4656. if (!Args.hasArg(options::OPT_nostdlib) &&
  4657. !Args.hasArg(options::OPT_nostartfiles)) {
  4658. if (Args.hasArg(options::OPT_pthread))
  4659. CmdArgs.push_back("-lpthread");
  4660. CmdArgs.push_back("-lc");
  4661. CmdArgs.push_back("-lCompilerRT-Generic");
  4662. CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
  4663. CmdArgs.push_back(
  4664. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  4665. }
  4666. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  4667. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4668. }
  4669. /// DragonFly Tools
  4670. // For now, DragonFly Assemble does just about the same as for
  4671. // FreeBSD, but this may change soon.
  4672. void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4673. const InputInfo &Output,
  4674. const InputInfoList &Inputs,
  4675. const ArgList &Args,
  4676. const char *LinkingOutput) const {
  4677. ArgStringList CmdArgs;
  4678. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  4679. // instruct as in the base system to assemble 32-bit code.
  4680. if (getToolChain().getArchName() == "i386")
  4681. CmdArgs.push_back("--32");
  4682. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4683. options::OPT_Xassembler);
  4684. CmdArgs.push_back("-o");
  4685. CmdArgs.push_back(Output.getFilename());
  4686. for (InputInfoList::const_iterator
  4687. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4688. const InputInfo &II = *it;
  4689. CmdArgs.push_back(II.getFilename());
  4690. }
  4691. const char *Exec =
  4692. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  4693. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4694. }
  4695. void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4696. const InputInfo &Output,
  4697. const InputInfoList &Inputs,
  4698. const ArgList &Args,
  4699. const char *LinkingOutput) const {
  4700. const Driver &D = getToolChain().getDriver();
  4701. ArgStringList CmdArgs;
  4702. if (!D.SysRoot.empty())
  4703. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  4704. if (Args.hasArg(options::OPT_static)) {
  4705. CmdArgs.push_back("-Bstatic");
  4706. } else {
  4707. if (Args.hasArg(options::OPT_shared))
  4708. CmdArgs.push_back("-Bshareable");
  4709. else {
  4710. CmdArgs.push_back("-dynamic-linker");
  4711. CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
  4712. }
  4713. }
  4714. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  4715. // instruct ld in the base system to link 32-bit code.
  4716. if (getToolChain().getArchName() == "i386") {
  4717. CmdArgs.push_back("-m");
  4718. CmdArgs.push_back("elf_i386");
  4719. }
  4720. if (Output.isFilename()) {
  4721. CmdArgs.push_back("-o");
  4722. CmdArgs.push_back(Output.getFilename());
  4723. } else {
  4724. assert(Output.isNothing() && "Invalid output.");
  4725. }
  4726. if (!Args.hasArg(options::OPT_nostdlib) &&
  4727. !Args.hasArg(options::OPT_nostartfiles)) {
  4728. if (!Args.hasArg(options::OPT_shared)) {
  4729. CmdArgs.push_back(
  4730. Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
  4731. CmdArgs.push_back(
  4732. Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  4733. CmdArgs.push_back(
  4734. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  4735. } else {
  4736. CmdArgs.push_back(
  4737. Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  4738. CmdArgs.push_back(
  4739. Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
  4740. }
  4741. }
  4742. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4743. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4744. Args.AddAllArgs(CmdArgs, options::OPT_e);
  4745. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4746. if (!Args.hasArg(options::OPT_nostdlib) &&
  4747. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4748. // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
  4749. // rpaths
  4750. CmdArgs.push_back("-L/usr/lib/gcc41");
  4751. if (!Args.hasArg(options::OPT_static)) {
  4752. CmdArgs.push_back("-rpath");
  4753. CmdArgs.push_back("/usr/lib/gcc41");
  4754. CmdArgs.push_back("-rpath-link");
  4755. CmdArgs.push_back("/usr/lib/gcc41");
  4756. CmdArgs.push_back("-rpath");
  4757. CmdArgs.push_back("/usr/lib");
  4758. CmdArgs.push_back("-rpath-link");
  4759. CmdArgs.push_back("/usr/lib");
  4760. }
  4761. if (D.CCCIsCXX) {
  4762. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  4763. CmdArgs.push_back("-lm");
  4764. }
  4765. if (Args.hasArg(options::OPT_shared)) {
  4766. CmdArgs.push_back("-lgcc_pic");
  4767. } else {
  4768. CmdArgs.push_back("-lgcc");
  4769. }
  4770. if (Args.hasArg(options::OPT_pthread))
  4771. CmdArgs.push_back("-lpthread");
  4772. if (!Args.hasArg(options::OPT_nolibc)) {
  4773. CmdArgs.push_back("-lc");
  4774. }
  4775. if (Args.hasArg(options::OPT_shared)) {
  4776. CmdArgs.push_back("-lgcc_pic");
  4777. } else {
  4778. CmdArgs.push_back("-lgcc");
  4779. }
  4780. }
  4781. if (!Args.hasArg(options::OPT_nostdlib) &&
  4782. !Args.hasArg(options::OPT_nostartfiles)) {
  4783. if (!Args.hasArg(options::OPT_shared))
  4784. CmdArgs.push_back(Args.MakeArgString(
  4785. getToolChain().GetFilePath("crtend.o")));
  4786. else
  4787. CmdArgs.push_back(Args.MakeArgString(
  4788. getToolChain().GetFilePath("crtendS.o")));
  4789. CmdArgs.push_back(Args.MakeArgString(
  4790. getToolChain().GetFilePath("crtn.o")));
  4791. }
  4792. addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
  4793. const char *Exec =
  4794. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  4795. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4796. }
  4797. void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4798. const InputInfo &Output,
  4799. const InputInfoList &Inputs,
  4800. const ArgList &Args,
  4801. const char *LinkingOutput) const {
  4802. ArgStringList CmdArgs;
  4803. if (Output.isFilename()) {
  4804. CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
  4805. Output.getFilename()));
  4806. } else {
  4807. assert(Output.isNothing() && "Invalid output.");
  4808. }
  4809. if (!Args.hasArg(options::OPT_nostdlib) &&
  4810. !Args.hasArg(options::OPT_nostartfiles)) {
  4811. CmdArgs.push_back("-defaultlib:libcmt");
  4812. }
  4813. CmdArgs.push_back("-nologo");
  4814. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4815. const char *Exec =
  4816. Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
  4817. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4818. }