Tools.cpp 343 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513
  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 "InputInfo.h"
  11. #include "ToolChains.h"
  12. #include "clang/Basic/CharInfo.h"
  13. #include "clang/Basic/LangOptions.h"
  14. #include "clang/Basic/ObjCRuntime.h"
  15. #include "clang/Basic/Version.h"
  16. #include "clang/Config/config.h"
  17. #include "clang/Driver/Action.h"
  18. #include "clang/Driver/Compilation.h"
  19. #include "clang/Driver/Driver.h"
  20. #include "clang/Driver/DriverDiagnostic.h"
  21. #include "clang/Driver/Job.h"
  22. #include "clang/Driver/Options.h"
  23. #include "clang/Driver/SanitizerArgs.h"
  24. #include "clang/Driver/ToolChain.h"
  25. #include "clang/Driver/Util.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/ADT/SmallString.h"
  28. #include "llvm/ADT/StringExtras.h"
  29. #include "llvm/ADT/StringSwitch.h"
  30. #include "llvm/ADT/Twine.h"
  31. #include "llvm/Option/Arg.h"
  32. #include "llvm/Option/ArgList.h"
  33. #include "llvm/Option/Option.h"
  34. #include "llvm/Support/TargetParser.h"
  35. #include "llvm/Support/Compression.h"
  36. #include "llvm/Support/ErrorHandling.h"
  37. #include "llvm/Support/FileSystem.h"
  38. #include "llvm/Support/Host.h"
  39. #include "llvm/Support/Path.h"
  40. #include "llvm/Support/Process.h"
  41. #include "llvm/Support/Program.h"
  42. #include "llvm/Support/raw_ostream.h"
  43. #ifdef LLVM_ON_UNIX
  44. #include <unistd.h> // For getuid().
  45. #endif
  46. using namespace clang::driver;
  47. using namespace clang::driver::tools;
  48. using namespace clang;
  49. using namespace llvm::opt;
  50. static void addAssemblerKPIC(const ArgList &Args, ArgStringList &CmdArgs) {
  51. Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
  52. options::OPT_fpic, options::OPT_fno_pic,
  53. options::OPT_fPIE, options::OPT_fno_PIE,
  54. options::OPT_fpie, options::OPT_fno_pie);
  55. if (!LastPICArg)
  56. return;
  57. if (LastPICArg->getOption().matches(options::OPT_fPIC) ||
  58. LastPICArg->getOption().matches(options::OPT_fpic) ||
  59. LastPICArg->getOption().matches(options::OPT_fPIE) ||
  60. LastPICArg->getOption().matches(options::OPT_fpie)) {
  61. CmdArgs.push_back("-KPIC");
  62. }
  63. }
  64. /// CheckPreprocessingOptions - Perform some validation of preprocessing
  65. /// arguments that is shared with gcc.
  66. static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
  67. if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC)) {
  68. if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_P) &&
  69. !Args.hasArg(options::OPT__SLASH_EP) && !D.CCCIsCPP()) {
  70. D.Diag(diag::err_drv_argument_only_allowed_with)
  71. << A->getBaseArg().getAsString(Args)
  72. << (D.IsCLMode() ? "/E, /P or /EP" : "-E");
  73. }
  74. }
  75. }
  76. /// CheckCodeGenerationOptions - Perform some validation of code generation
  77. /// arguments that is shared with gcc.
  78. static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
  79. // In gcc, only ARM checks this, but it seems reasonable to check universally.
  80. if (Args.hasArg(options::OPT_static))
  81. if (const Arg *A =
  82. Args.getLastArg(options::OPT_dynamic, options::OPT_mdynamic_no_pic))
  83. D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
  84. << "-static";
  85. }
  86. // Add backslashes to escape spaces and other backslashes.
  87. // This is used for the space-separated argument list specified with
  88. // the -dwarf-debug-flags option.
  89. static void EscapeSpacesAndBackslashes(const char *Arg,
  90. SmallVectorImpl<char> &Res) {
  91. for (; *Arg; ++Arg) {
  92. switch (*Arg) {
  93. default:
  94. break;
  95. case ' ':
  96. case '\\':
  97. Res.push_back('\\');
  98. break;
  99. }
  100. Res.push_back(*Arg);
  101. }
  102. }
  103. // Quote target names for inclusion in GNU Make dependency files.
  104. // Only the characters '$', '#', ' ', '\t' are quoted.
  105. static void QuoteTarget(StringRef Target, SmallVectorImpl<char> &Res) {
  106. for (unsigned i = 0, e = Target.size(); i != e; ++i) {
  107. switch (Target[i]) {
  108. case ' ':
  109. case '\t':
  110. // Escape the preceding backslashes
  111. for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
  112. Res.push_back('\\');
  113. // Escape the space/tab
  114. Res.push_back('\\');
  115. break;
  116. case '$':
  117. Res.push_back('$');
  118. break;
  119. case '#':
  120. Res.push_back('\\');
  121. break;
  122. default:
  123. break;
  124. }
  125. Res.push_back(Target[i]);
  126. }
  127. }
  128. static void addDirectoryList(const ArgList &Args, ArgStringList &CmdArgs,
  129. const char *ArgName, const char *EnvVar) {
  130. const char *DirList = ::getenv(EnvVar);
  131. bool CombinedArg = false;
  132. if (!DirList)
  133. return; // Nothing to do.
  134. StringRef Name(ArgName);
  135. if (Name.equals("-I") || Name.equals("-L"))
  136. CombinedArg = true;
  137. StringRef Dirs(DirList);
  138. if (Dirs.empty()) // Empty string should not add '.'.
  139. return;
  140. StringRef::size_type Delim;
  141. while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) {
  142. if (Delim == 0) { // Leading colon.
  143. if (CombinedArg) {
  144. CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
  145. } else {
  146. CmdArgs.push_back(ArgName);
  147. CmdArgs.push_back(".");
  148. }
  149. } else {
  150. if (CombinedArg) {
  151. CmdArgs.push_back(
  152. Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
  153. } else {
  154. CmdArgs.push_back(ArgName);
  155. CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
  156. }
  157. }
  158. Dirs = Dirs.substr(Delim + 1);
  159. }
  160. if (Dirs.empty()) { // Trailing colon.
  161. if (CombinedArg) {
  162. CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
  163. } else {
  164. CmdArgs.push_back(ArgName);
  165. CmdArgs.push_back(".");
  166. }
  167. } else { // Add the last path.
  168. if (CombinedArg) {
  169. CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
  170. } else {
  171. CmdArgs.push_back(ArgName);
  172. CmdArgs.push_back(Args.MakeArgString(Dirs));
  173. }
  174. }
  175. }
  176. static void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
  177. const ArgList &Args, ArgStringList &CmdArgs) {
  178. const Driver &D = TC.getDriver();
  179. // Add extra linker input arguments which are not treated as inputs
  180. // (constructed via -Xarch_).
  181. Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
  182. for (const auto &II : Inputs) {
  183. if (!TC.HasNativeLLVMSupport()) {
  184. // Don't try to pass LLVM inputs unless we have native support.
  185. if (II.getType() == types::TY_LLVM_IR ||
  186. II.getType() == types::TY_LTO_IR ||
  187. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  188. D.Diag(diag::err_drv_no_linker_llvm_support) << TC.getTripleString();
  189. }
  190. // Add filenames immediately.
  191. if (II.isFilename()) {
  192. CmdArgs.push_back(II.getFilename());
  193. continue;
  194. }
  195. // Otherwise, this is a linker input argument.
  196. const Arg &A = II.getInputArg();
  197. // Handle reserved library options.
  198. if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx))
  199. TC.AddCXXStdlibLibArgs(Args, CmdArgs);
  200. else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext))
  201. TC.AddCCKextLibArgs(Args, CmdArgs);
  202. else if (A.getOption().matches(options::OPT_z)) {
  203. // Pass -z prefix for gcc linker compatibility.
  204. A.claim();
  205. A.render(Args, CmdArgs);
  206. } else {
  207. A.renderAsInput(Args, CmdArgs);
  208. }
  209. }
  210. // LIBRARY_PATH - included following the user specified library paths.
  211. // and only supported on native toolchains.
  212. if (!TC.isCrossCompiling())
  213. addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
  214. }
  215. /// \brief Determine whether Objective-C automated reference counting is
  216. /// enabled.
  217. static bool isObjCAutoRefCount(const ArgList &Args) {
  218. return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
  219. }
  220. /// \brief Determine whether we are linking the ObjC runtime.
  221. static bool isObjCRuntimeLinked(const ArgList &Args) {
  222. if (isObjCAutoRefCount(Args)) {
  223. Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
  224. return true;
  225. }
  226. return Args.hasArg(options::OPT_fobjc_link_runtime);
  227. }
  228. static bool forwardToGCC(const Option &O) {
  229. // Don't forward inputs from the original command line. They are added from
  230. // InputInfoList.
  231. return O.getKind() != Option::InputClass &&
  232. !O.hasFlag(options::DriverOption) && !O.hasFlag(options::LinkerInput);
  233. }
  234. void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
  235. const Driver &D, const ArgList &Args,
  236. ArgStringList &CmdArgs,
  237. const InputInfo &Output,
  238. const InputInfoList &Inputs) const {
  239. Arg *A;
  240. CheckPreprocessingOptions(D, Args);
  241. Args.AddLastArg(CmdArgs, options::OPT_C);
  242. Args.AddLastArg(CmdArgs, options::OPT_CC);
  243. // Handle dependency file generation.
  244. if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
  245. (A = Args.getLastArg(options::OPT_MD)) ||
  246. (A = Args.getLastArg(options::OPT_MMD))) {
  247. // Determine the output location.
  248. const char *DepFile;
  249. if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
  250. DepFile = MF->getValue();
  251. C.addFailureResultFile(DepFile, &JA);
  252. } else if (Output.getType() == types::TY_Dependencies) {
  253. DepFile = Output.getFilename();
  254. } else if (A->getOption().matches(options::OPT_M) ||
  255. A->getOption().matches(options::OPT_MM)) {
  256. DepFile = "-";
  257. } else {
  258. DepFile = getDependencyFileName(Args, Inputs);
  259. C.addFailureResultFile(DepFile, &JA);
  260. }
  261. CmdArgs.push_back("-dependency-file");
  262. CmdArgs.push_back(DepFile);
  263. // Add a default target if one wasn't specified.
  264. if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
  265. const char *DepTarget;
  266. // If user provided -o, that is the dependency target, except
  267. // when we are only generating a dependency file.
  268. Arg *OutputOpt = Args.getLastArg(options::OPT_o);
  269. if (OutputOpt && Output.getType() != types::TY_Dependencies) {
  270. DepTarget = OutputOpt->getValue();
  271. } else {
  272. // Otherwise derive from the base input.
  273. //
  274. // FIXME: This should use the computed output file location.
  275. SmallString<128> P(Inputs[0].getBaseInput());
  276. llvm::sys::path::replace_extension(P, "o");
  277. DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
  278. }
  279. CmdArgs.push_back("-MT");
  280. SmallString<128> Quoted;
  281. QuoteTarget(DepTarget, Quoted);
  282. CmdArgs.push_back(Args.MakeArgString(Quoted));
  283. }
  284. if (A->getOption().matches(options::OPT_M) ||
  285. A->getOption().matches(options::OPT_MD))
  286. CmdArgs.push_back("-sys-header-deps");
  287. if ((isa<PrecompileJobAction>(JA) &&
  288. !Args.hasArg(options::OPT_fno_module_file_deps)) ||
  289. Args.hasArg(options::OPT_fmodule_file_deps))
  290. CmdArgs.push_back("-module-file-deps");
  291. }
  292. if (Args.hasArg(options::OPT_MG)) {
  293. if (!A || A->getOption().matches(options::OPT_MD) ||
  294. A->getOption().matches(options::OPT_MMD))
  295. D.Diag(diag::err_drv_mg_requires_m_or_mm);
  296. CmdArgs.push_back("-MG");
  297. }
  298. Args.AddLastArg(CmdArgs, options::OPT_MP);
  299. Args.AddLastArg(CmdArgs, options::OPT_MV);
  300. // Convert all -MQ <target> args to -MT <quoted target>
  301. for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) {
  302. A->claim();
  303. if (A->getOption().matches(options::OPT_MQ)) {
  304. CmdArgs.push_back("-MT");
  305. SmallString<128> Quoted;
  306. QuoteTarget(A->getValue(), Quoted);
  307. CmdArgs.push_back(Args.MakeArgString(Quoted));
  308. // -MT flag - no change
  309. } else {
  310. A->render(Args, CmdArgs);
  311. }
  312. }
  313. // Add -i* options, and automatically translate to
  314. // -include-pch/-include-pth for transparent PCH support. It's
  315. // wonky, but we include looking for .gch so we can support seamless
  316. // replacement into a build system already set up to be generating
  317. // .gch files.
  318. bool RenderedImplicitInclude = false;
  319. for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
  320. if (A->getOption().matches(options::OPT_include)) {
  321. bool IsFirstImplicitInclude = !RenderedImplicitInclude;
  322. RenderedImplicitInclude = true;
  323. // Use PCH if the user requested it.
  324. bool UsePCH = D.CCCUsePCH;
  325. bool FoundPTH = false;
  326. bool FoundPCH = false;
  327. SmallString<128> P(A->getValue());
  328. // We want the files to have a name like foo.h.pch. Add a dummy extension
  329. // so that replace_extension does the right thing.
  330. P += ".dummy";
  331. if (UsePCH) {
  332. llvm::sys::path::replace_extension(P, "pch");
  333. if (llvm::sys::fs::exists(P))
  334. FoundPCH = true;
  335. }
  336. if (!FoundPCH) {
  337. llvm::sys::path::replace_extension(P, "pth");
  338. if (llvm::sys::fs::exists(P))
  339. FoundPTH = true;
  340. }
  341. if (!FoundPCH && !FoundPTH) {
  342. llvm::sys::path::replace_extension(P, "gch");
  343. if (llvm::sys::fs::exists(P)) {
  344. FoundPCH = UsePCH;
  345. FoundPTH = !UsePCH;
  346. }
  347. }
  348. if (FoundPCH || FoundPTH) {
  349. if (IsFirstImplicitInclude) {
  350. A->claim();
  351. if (UsePCH)
  352. CmdArgs.push_back("-include-pch");
  353. else
  354. CmdArgs.push_back("-include-pth");
  355. CmdArgs.push_back(Args.MakeArgString(P));
  356. continue;
  357. } else {
  358. // Ignore the PCH if not first on command line and emit warning.
  359. D.Diag(diag::warn_drv_pch_not_first_include) << P
  360. << A->getAsString(Args);
  361. }
  362. }
  363. }
  364. // Not translated, render as usual.
  365. A->claim();
  366. A->render(Args, CmdArgs);
  367. }
  368. Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
  369. Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
  370. options::OPT_index_header_map);
  371. // Add -Wp, and -Xassembler if using the preprocessor.
  372. // FIXME: There is a very unfortunate problem here, some troubled
  373. // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
  374. // really support that we would have to parse and then translate
  375. // those options. :(
  376. Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
  377. options::OPT_Xpreprocessor);
  378. // -I- is a deprecated GCC feature, reject it.
  379. if (Arg *A = Args.getLastArg(options::OPT_I_))
  380. D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
  381. // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
  382. // -isysroot to the CC1 invocation.
  383. StringRef sysroot = C.getSysRoot();
  384. if (sysroot != "") {
  385. if (!Args.hasArg(options::OPT_isysroot)) {
  386. CmdArgs.push_back("-isysroot");
  387. CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
  388. }
  389. }
  390. // Parse additional include paths from environment variables.
  391. // FIXME: We should probably sink the logic for handling these from the
  392. // frontend into the driver. It will allow deleting 4 otherwise unused flags.
  393. // CPATH - included following the user specified includes (but prior to
  394. // builtin and standard includes).
  395. addDirectoryList(Args, CmdArgs, "-I", "CPATH");
  396. // C_INCLUDE_PATH - system includes enabled when compiling C.
  397. addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
  398. // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
  399. addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
  400. // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
  401. addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
  402. // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
  403. addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
  404. // Add C++ include arguments, if needed.
  405. if (types::isCXX(Inputs[0].getType()))
  406. getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
  407. // Add system include arguments.
  408. getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
  409. }
  410. // FIXME: Move to target hook.
  411. static bool isSignedCharDefault(const llvm::Triple &Triple) {
  412. switch (Triple.getArch()) {
  413. default:
  414. return true;
  415. case llvm::Triple::aarch64:
  416. case llvm::Triple::aarch64_be:
  417. case llvm::Triple::arm:
  418. case llvm::Triple::armeb:
  419. case llvm::Triple::thumb:
  420. case llvm::Triple::thumbeb:
  421. if (Triple.isOSDarwin() || Triple.isOSWindows())
  422. return true;
  423. return false;
  424. case llvm::Triple::ppc:
  425. case llvm::Triple::ppc64:
  426. if (Triple.isOSDarwin())
  427. return true;
  428. return false;
  429. case llvm::Triple::hexagon:
  430. case llvm::Triple::ppc64le:
  431. case llvm::Triple::systemz:
  432. case llvm::Triple::xcore:
  433. return false;
  434. }
  435. }
  436. static bool isNoCommonDefault(const llvm::Triple &Triple) {
  437. switch (Triple.getArch()) {
  438. default:
  439. return false;
  440. case llvm::Triple::xcore:
  441. return true;
  442. }
  443. }
  444. // ARM tools start.
  445. // Get SubArch (vN).
  446. static int getARMSubArchVersionNumber(const llvm::Triple &Triple) {
  447. llvm::StringRef Arch = Triple.getArchName();
  448. return llvm::ARMTargetParser::parseArchVersion(Arch);
  449. }
  450. // True if M-profile.
  451. static bool isARMMProfile(const llvm::Triple &Triple) {
  452. llvm::StringRef Arch = Triple.getArchName();
  453. unsigned Profile = llvm::ARMTargetParser::parseArchProfile(Arch);
  454. return Profile == llvm::ARM::PK_M;
  455. }
  456. // Get Arch/CPU from args.
  457. static void getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch,
  458. llvm::StringRef &CPU, bool FromAs = false) {
  459. if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
  460. CPU = A->getValue();
  461. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
  462. Arch = A->getValue();
  463. if (!FromAs)
  464. return;
  465. for (const Arg *A :
  466. Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
  467. StringRef Value = A->getValue();
  468. if (Value.startswith("-mcpu="))
  469. CPU = Value.substr(6);
  470. if (Value.startswith("-march="))
  471. Arch = Value.substr(7);
  472. }
  473. }
  474. // Handle -mhwdiv=.
  475. // FIXME: Use ARMTargetParser.
  476. static void getARMHWDivFeatures(const Driver &D, const Arg *A,
  477. const ArgList &Args, StringRef HWDiv,
  478. std::vector<const char *> &Features) {
  479. if (HWDiv == "arm") {
  480. Features.push_back("+hwdiv-arm");
  481. Features.push_back("-hwdiv");
  482. } else if (HWDiv == "thumb") {
  483. Features.push_back("-hwdiv-arm");
  484. Features.push_back("+hwdiv");
  485. } else if (HWDiv == "arm,thumb" || HWDiv == "thumb,arm") {
  486. Features.push_back("+hwdiv-arm");
  487. Features.push_back("+hwdiv");
  488. } else if (HWDiv == "none") {
  489. Features.push_back("-hwdiv-arm");
  490. Features.push_back("-hwdiv");
  491. } else
  492. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  493. }
  494. // Handle -mfpu=.
  495. static void getARMFPUFeatures(const Driver &D, const Arg *A,
  496. const ArgList &Args, StringRef FPU,
  497. std::vector<const char *> &Features) {
  498. unsigned FPUID = llvm::ARMTargetParser::parseFPU(FPU);
  499. if (!llvm::ARMTargetParser::getFPUFeatures(FPUID, Features))
  500. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  501. }
  502. // Check if -march is valid by checking if it can be canonicalised and parsed.
  503. // getARMArch is used here instead of just checking the -march value in order
  504. // to handle -march=native correctly.
  505. static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args,
  506. llvm::StringRef ArchName,
  507. const llvm::Triple &Triple) {
  508. std::string MArch = arm::getARMArch(ArchName, Triple);
  509. if (llvm::ARMTargetParser::parseArch(MArch) == llvm::ARM::AK_INVALID)
  510. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  511. }
  512. // Check -mcpu=. Needs ArchName to handle -mcpu=generic.
  513. static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args,
  514. llvm::StringRef CPUName, llvm::StringRef ArchName,
  515. const llvm::Triple &Triple) {
  516. std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
  517. std::string Arch = arm::getARMArch(ArchName, Triple);
  518. if (strcmp(arm::getLLVMArchSuffixForARM(CPU, Arch), "") == 0)
  519. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  520. }
  521. // Select the float ABI as determined by -msoft-float, -mhard-float, and
  522. // -mfloat-abi=.
  523. StringRef tools::arm::getARMFloatABI(const Driver &D, const ArgList &Args,
  524. const llvm::Triple &Triple) {
  525. StringRef FloatABI;
  526. if (Arg *A =
  527. Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
  528. options::OPT_mfloat_abi_EQ)) {
  529. if (A->getOption().matches(options::OPT_msoft_float))
  530. FloatABI = "soft";
  531. else if (A->getOption().matches(options::OPT_mhard_float))
  532. FloatABI = "hard";
  533. else {
  534. FloatABI = A->getValue();
  535. if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
  536. D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
  537. FloatABI = "soft";
  538. }
  539. }
  540. }
  541. // If unspecified, choose the default based on the platform.
  542. if (FloatABI.empty()) {
  543. switch (Triple.getOS()) {
  544. case llvm::Triple::Darwin:
  545. case llvm::Triple::MacOSX:
  546. case llvm::Triple::IOS: {
  547. // Darwin defaults to "softfp" for v6 and v7.
  548. //
  549. if (getARMSubArchVersionNumber(Triple) == 6 ||
  550. getARMSubArchVersionNumber(Triple) == 7)
  551. FloatABI = "softfp";
  552. else
  553. FloatABI = "soft";
  554. break;
  555. }
  556. // FIXME: this is invalid for WindowsCE
  557. case llvm::Triple::Win32:
  558. FloatABI = "hard";
  559. break;
  560. case llvm::Triple::FreeBSD:
  561. switch (Triple.getEnvironment()) {
  562. case llvm::Triple::GNUEABIHF:
  563. FloatABI = "hard";
  564. break;
  565. default:
  566. // FreeBSD defaults to soft float
  567. FloatABI = "soft";
  568. break;
  569. }
  570. break;
  571. default:
  572. switch (Triple.getEnvironment()) {
  573. case llvm::Triple::GNUEABIHF:
  574. FloatABI = "hard";
  575. break;
  576. case llvm::Triple::GNUEABI:
  577. FloatABI = "softfp";
  578. break;
  579. case llvm::Triple::EABIHF:
  580. FloatABI = "hard";
  581. break;
  582. case llvm::Triple::EABI:
  583. // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
  584. FloatABI = "softfp";
  585. break;
  586. case llvm::Triple::Android: {
  587. if (getARMSubArchVersionNumber(Triple) == 7)
  588. FloatABI = "softfp";
  589. else
  590. FloatABI = "soft";
  591. break;
  592. }
  593. default:
  594. // Assume "soft", but warn the user we are guessing.
  595. FloatABI = "soft";
  596. if (Triple.getOS() != llvm::Triple::UnknownOS ||
  597. !Triple.isOSBinFormatMachO())
  598. D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
  599. break;
  600. }
  601. }
  602. }
  603. return FloatABI;
  604. }
  605. static void getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  606. const ArgList &Args,
  607. std::vector<const char *> &Features,
  608. bool ForAS) {
  609. bool KernelOrKext =
  610. Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
  611. StringRef FloatABI = tools::arm::getARMFloatABI(D, Args, Triple);
  612. const Arg *WaCPU = nullptr, *WaFPU = nullptr;
  613. const Arg *WaHDiv = nullptr, *WaArch = nullptr;
  614. if (!ForAS) {
  615. // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
  616. // yet (it uses the -mfloat-abi and -msoft-float options), and it is
  617. // stripped out by the ARM target. We should probably pass this a new
  618. // -target-option, which is handled by the -cc1/-cc1as invocation.
  619. //
  620. // FIXME2: For consistency, it would be ideal if we set up the target
  621. // machine state the same when using the frontend or the assembler. We don't
  622. // currently do that for the assembler, we pass the options directly to the
  623. // backend and never even instantiate the frontend TargetInfo. If we did,
  624. // and used its handleTargetFeatures hook, then we could ensure the
  625. // assembler and the frontend behave the same.
  626. // Use software floating point operations?
  627. if (FloatABI == "soft")
  628. Features.push_back("+soft-float");
  629. // Use software floating point argument passing?
  630. if (FloatABI != "hard")
  631. Features.push_back("+soft-float-abi");
  632. } else {
  633. // Here, we make sure that -Wa,-mfpu/cpu/arch/hwdiv will be passed down
  634. // to the assembler correctly.
  635. for (const Arg *A :
  636. Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
  637. StringRef Value = A->getValue();
  638. if (Value.startswith("-mfpu=")) {
  639. WaFPU = A;
  640. } else if (Value.startswith("-mcpu=")) {
  641. WaCPU = A;
  642. } else if (Value.startswith("-mhwdiv=")) {
  643. WaHDiv = A;
  644. } else if (Value.startswith("-march=")) {
  645. WaArch = A;
  646. }
  647. }
  648. }
  649. // Honor -mfpu=. ClangAs gives preference to -Wa,-mfpu=.
  650. const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ);
  651. if (WaFPU) {
  652. if (FPUArg)
  653. D.Diag(clang::diag::warn_drv_unused_argument)
  654. << FPUArg->getAsString(Args);
  655. getARMFPUFeatures(D, WaFPU, Args, StringRef(WaFPU->getValue()).substr(6),
  656. Features);
  657. } else if (FPUArg) {
  658. getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features);
  659. }
  660. // Honor -mhwdiv=. ClangAs gives preference to -Wa,-mhwdiv=.
  661. const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ);
  662. if (WaHDiv) {
  663. if (HDivArg)
  664. D.Diag(clang::diag::warn_drv_unused_argument)
  665. << HDivArg->getAsString(Args);
  666. getARMHWDivFeatures(D, WaHDiv, Args,
  667. StringRef(WaHDiv->getValue()).substr(8), Features);
  668. } else if (HDivArg)
  669. getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features);
  670. // Check -march. ClangAs gives preference to -Wa,-march=.
  671. const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ);
  672. StringRef ArchName;
  673. if (WaArch) {
  674. if (ArchArg)
  675. D.Diag(clang::diag::warn_drv_unused_argument)
  676. << ArchArg->getAsString(Args);
  677. ArchName = StringRef(WaArch->getValue()).substr(7);
  678. checkARMArchName(D, WaArch, Args, ArchName, Triple);
  679. // FIXME: Set Arch.
  680. D.Diag(clang::diag::warn_drv_unused_argument) << WaArch->getAsString(Args);
  681. } else if (ArchArg) {
  682. ArchName = ArchArg->getValue();
  683. checkARMArchName(D, ArchArg, Args, ArchName, Triple);
  684. }
  685. // Check -mcpu. ClangAs gives preference to -Wa,-mcpu=.
  686. const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
  687. StringRef CPUName;
  688. if (WaCPU) {
  689. if (CPUArg)
  690. D.Diag(clang::diag::warn_drv_unused_argument)
  691. << CPUArg->getAsString(Args);
  692. CPUName = StringRef(WaCPU->getValue()).substr(6);
  693. checkARMCPUName(D, WaCPU, Args, CPUName, ArchName, Triple);
  694. } else if (CPUArg) {
  695. CPUName = CPUArg->getValue();
  696. checkARMCPUName(D, CPUArg, Args, CPUName, ArchName, Triple);
  697. }
  698. // Setting -msoft-float effectively disables NEON because of the GCC
  699. // implementation, although the same isn't true of VFP or VFP3.
  700. if (FloatABI == "soft") {
  701. Features.push_back("-neon");
  702. // Also need to explicitly disable features which imply NEON.
  703. Features.push_back("-crypto");
  704. }
  705. // En/disable crc code generation.
  706. if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
  707. if (A->getOption().matches(options::OPT_mcrc))
  708. Features.push_back("+crc");
  709. else
  710. Features.push_back("-crc");
  711. }
  712. if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8_1a) {
  713. Features.insert(Features.begin(), "+v8.1a");
  714. }
  715. // Look for the last occurrence of -mlong-calls or -mno-long-calls. If
  716. // neither options are specified, see if we are compiling for kernel/kext and
  717. // decide whether to pass "+long-calls" based on the OS and its version.
  718. if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
  719. options::OPT_mno_long_calls)) {
  720. if (A->getOption().matches(options::OPT_mlong_calls))
  721. Features.push_back("+long-calls");
  722. } else if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6))) {
  723. Features.push_back("+long-calls");
  724. }
  725. }
  726. void Clang::AddARMTargetArgs(const ArgList &Args, ArgStringList &CmdArgs,
  727. bool KernelOrKext) const {
  728. const Driver &D = getToolChain().getDriver();
  729. // Get the effective triple, which takes into account the deployment target.
  730. std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  731. llvm::Triple Triple(TripleStr);
  732. // Select the ABI to use.
  733. //
  734. // FIXME: Support -meabi.
  735. // FIXME: Parts of this are duplicated in the backend, unify this somehow.
  736. const char *ABIName = nullptr;
  737. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
  738. ABIName = A->getValue();
  739. } else if (Triple.isOSBinFormatMachO()) {
  740. // The backend is hardwired to assume AAPCS for M-class processors, ensure
  741. // the frontend matches that.
  742. if (Triple.getEnvironment() == llvm::Triple::EABI ||
  743. Triple.getOS() == llvm::Triple::UnknownOS || isARMMProfile(Triple)) {
  744. ABIName = "aapcs";
  745. } else {
  746. ABIName = "apcs-gnu";
  747. }
  748. } else if (Triple.isOSWindows()) {
  749. // FIXME: this is invalid for WindowsCE
  750. ABIName = "aapcs";
  751. } else {
  752. // Select the default based on the platform.
  753. switch (Triple.getEnvironment()) {
  754. case llvm::Triple::Android:
  755. case llvm::Triple::GNUEABI:
  756. case llvm::Triple::GNUEABIHF:
  757. ABIName = "aapcs-linux";
  758. break;
  759. case llvm::Triple::EABIHF:
  760. case llvm::Triple::EABI:
  761. ABIName = "aapcs";
  762. break;
  763. default:
  764. if (Triple.getOS() == llvm::Triple::NetBSD)
  765. ABIName = "apcs-gnu";
  766. else
  767. ABIName = "aapcs";
  768. break;
  769. }
  770. }
  771. CmdArgs.push_back("-target-abi");
  772. CmdArgs.push_back(ABIName);
  773. // Determine floating point ABI from the options & target defaults.
  774. StringRef FloatABI = tools::arm::getARMFloatABI(D, Args, Triple);
  775. if (FloatABI == "soft") {
  776. // Floating point operations and argument passing are soft.
  777. //
  778. // FIXME: This changes CPP defines, we need -target-soft-float.
  779. CmdArgs.push_back("-msoft-float");
  780. CmdArgs.push_back("-mfloat-abi");
  781. CmdArgs.push_back("soft");
  782. } else if (FloatABI == "softfp") {
  783. // Floating point operations are hard, but argument passing is soft.
  784. CmdArgs.push_back("-mfloat-abi");
  785. CmdArgs.push_back("soft");
  786. } else {
  787. // Floating point operations and argument passing are hard.
  788. assert(FloatABI == "hard" && "Invalid float abi!");
  789. CmdArgs.push_back("-mfloat-abi");
  790. CmdArgs.push_back("hard");
  791. }
  792. // Kernel code has more strict alignment requirements.
  793. if (KernelOrKext) {
  794. CmdArgs.push_back("-backend-option");
  795. CmdArgs.push_back("-arm-strict-align");
  796. // The kext linker doesn't know how to deal with movw/movt.
  797. CmdArgs.push_back("-backend-option");
  798. CmdArgs.push_back("-arm-use-movt=0");
  799. }
  800. // -mkernel implies -mstrict-align; don't add the redundant option.
  801. if (!KernelOrKext) {
  802. if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
  803. options::OPT_munaligned_access)) {
  804. CmdArgs.push_back("-backend-option");
  805. if (A->getOption().matches(options::OPT_mno_unaligned_access))
  806. CmdArgs.push_back("-arm-strict-align");
  807. else {
  808. if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
  809. D.Diag(diag::err_target_unsupported_unaligned) << "v6m";
  810. CmdArgs.push_back("-arm-no-strict-align");
  811. }
  812. }
  813. }
  814. // Forward the -mglobal-merge option for explicit control over the pass.
  815. if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
  816. options::OPT_mno_global_merge)) {
  817. CmdArgs.push_back("-backend-option");
  818. if (A->getOption().matches(options::OPT_mno_global_merge))
  819. CmdArgs.push_back("-arm-global-merge=false");
  820. else
  821. CmdArgs.push_back("-arm-global-merge=true");
  822. }
  823. if (!Args.hasFlag(options::OPT_mimplicit_float,
  824. options::OPT_mno_implicit_float, true))
  825. CmdArgs.push_back("-no-implicit-float");
  826. // llvm does not support reserving registers in general. There is support
  827. // for reserving r9 on ARM though (defined as a platform-specific register
  828. // in ARM EABI).
  829. if (Args.hasArg(options::OPT_ffixed_r9)) {
  830. CmdArgs.push_back("-backend-option");
  831. CmdArgs.push_back("-arm-reserve-r9");
  832. }
  833. }
  834. // ARM tools end.
  835. /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
  836. /// targeting.
  837. static std::string getAArch64TargetCPU(const ArgList &Args) {
  838. Arg *A;
  839. std::string CPU;
  840. // If we have -mtune or -mcpu, use that.
  841. if ((A = Args.getLastArg(options::OPT_mtune_EQ))) {
  842. CPU = A->getValue();
  843. } else if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
  844. StringRef Mcpu = A->getValue();
  845. CPU = Mcpu.split("+").first.lower();
  846. }
  847. // Handle CPU name is 'native'.
  848. if (CPU == "native")
  849. return llvm::sys::getHostCPUName();
  850. else if (CPU.size())
  851. return CPU;
  852. // Make sure we pick "cyclone" if -arch is used.
  853. // FIXME: Should this be picked by checking the target triple instead?
  854. if (Args.getLastArg(options::OPT_arch))
  855. return "cyclone";
  856. return "generic";
  857. }
  858. void Clang::AddAArch64TargetArgs(const ArgList &Args,
  859. ArgStringList &CmdArgs) const {
  860. std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  861. llvm::Triple Triple(TripleStr);
  862. if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
  863. Args.hasArg(options::OPT_mkernel) ||
  864. Args.hasArg(options::OPT_fapple_kext))
  865. CmdArgs.push_back("-disable-red-zone");
  866. if (!Args.hasFlag(options::OPT_mimplicit_float,
  867. options::OPT_mno_implicit_float, true))
  868. CmdArgs.push_back("-no-implicit-float");
  869. const char *ABIName = nullptr;
  870. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
  871. ABIName = A->getValue();
  872. else if (Triple.isOSDarwin())
  873. ABIName = "darwinpcs";
  874. else
  875. ABIName = "aapcs";
  876. CmdArgs.push_back("-target-abi");
  877. CmdArgs.push_back(ABIName);
  878. if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
  879. options::OPT_munaligned_access)) {
  880. CmdArgs.push_back("-backend-option");
  881. if (A->getOption().matches(options::OPT_mno_unaligned_access))
  882. CmdArgs.push_back("-aarch64-strict-align");
  883. else
  884. CmdArgs.push_back("-aarch64-no-strict-align");
  885. }
  886. if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
  887. options::OPT_mno_fix_cortex_a53_835769)) {
  888. CmdArgs.push_back("-backend-option");
  889. if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
  890. CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
  891. else
  892. CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
  893. } else if (Triple.getEnvironment() == llvm::Triple::Android) {
  894. // Enabled A53 errata (835769) workaround by default on android
  895. CmdArgs.push_back("-backend-option");
  896. CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
  897. }
  898. // Forward the -mglobal-merge option for explicit control over the pass.
  899. if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
  900. options::OPT_mno_global_merge)) {
  901. CmdArgs.push_back("-backend-option");
  902. if (A->getOption().matches(options::OPT_mno_global_merge))
  903. CmdArgs.push_back("-aarch64-global-merge=false");
  904. else
  905. CmdArgs.push_back("-aarch64-global-merge=true");
  906. }
  907. if (Args.hasArg(options::OPT_ffixed_x18)) {
  908. CmdArgs.push_back("-backend-option");
  909. CmdArgs.push_back("-aarch64-reserve-x18");
  910. }
  911. }
  912. // Get CPU and ABI names. They are not independent
  913. // so we have to calculate them together.
  914. void mips::getMipsCPUAndABI(const ArgList &Args, const llvm::Triple &Triple,
  915. StringRef &CPUName, StringRef &ABIName) {
  916. const char *DefMips32CPU = "mips32r2";
  917. const char *DefMips64CPU = "mips64r2";
  918. // MIPS32r6 is the default for mips(el)?-img-linux-gnu and MIPS64r6 is the
  919. // default for mips64(el)?-img-linux-gnu.
  920. if (Triple.getVendor() == llvm::Triple::ImaginationTechnologies &&
  921. Triple.getEnvironment() == llvm::Triple::GNU) {
  922. DefMips32CPU = "mips32r6";
  923. DefMips64CPU = "mips64r6";
  924. }
  925. // MIPS3 is the default for mips64*-unknown-openbsd.
  926. if (Triple.getOS() == llvm::Triple::OpenBSD)
  927. DefMips64CPU = "mips3";
  928. if (Arg *A = Args.getLastArg(options::OPT_march_EQ, options::OPT_mcpu_EQ))
  929. CPUName = A->getValue();
  930. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
  931. ABIName = A->getValue();
  932. // Convert a GNU style Mips ABI name to the name
  933. // accepted by LLVM Mips backend.
  934. ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName)
  935. .Case("32", "o32")
  936. .Case("64", "n64")
  937. .Default(ABIName);
  938. }
  939. // Setup default CPU and ABI names.
  940. if (CPUName.empty() && ABIName.empty()) {
  941. switch (Triple.getArch()) {
  942. default:
  943. llvm_unreachable("Unexpected triple arch name");
  944. case llvm::Triple::mips:
  945. case llvm::Triple::mipsel:
  946. CPUName = DefMips32CPU;
  947. break;
  948. case llvm::Triple::mips64:
  949. case llvm::Triple::mips64el:
  950. CPUName = DefMips64CPU;
  951. break;
  952. }
  953. }
  954. if (ABIName.empty()) {
  955. // Deduce ABI name from the target triple.
  956. if (Triple.getArch() == llvm::Triple::mips ||
  957. Triple.getArch() == llvm::Triple::mipsel)
  958. ABIName = "o32";
  959. else
  960. ABIName = "n64";
  961. }
  962. if (CPUName.empty()) {
  963. // Deduce CPU name from ABI name.
  964. CPUName = llvm::StringSwitch<const char *>(ABIName)
  965. .Cases("o32", "eabi", DefMips32CPU)
  966. .Cases("n32", "n64", DefMips64CPU)
  967. .Default("");
  968. }
  969. // FIXME: Warn on inconsistent use of -march and -mabi.
  970. }
  971. // Convert ABI name to the GNU tools acceptable variant.
  972. static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
  973. return llvm::StringSwitch<llvm::StringRef>(ABI)
  974. .Case("o32", "32")
  975. .Case("n64", "64")
  976. .Default(ABI);
  977. }
  978. // Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
  979. // and -mfloat-abi=.
  980. static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
  981. StringRef FloatABI;
  982. if (Arg *A =
  983. Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
  984. options::OPT_mfloat_abi_EQ)) {
  985. if (A->getOption().matches(options::OPT_msoft_float))
  986. FloatABI = "soft";
  987. else if (A->getOption().matches(options::OPT_mhard_float))
  988. FloatABI = "hard";
  989. else {
  990. FloatABI = A->getValue();
  991. if (FloatABI != "soft" && FloatABI != "hard") {
  992. D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
  993. FloatABI = "hard";
  994. }
  995. }
  996. }
  997. // If unspecified, choose the default based on the platform.
  998. if (FloatABI.empty()) {
  999. // Assume "hard", because it's a default value used by gcc.
  1000. // When we start to recognize specific target MIPS processors,
  1001. // we will be able to select the default more correctly.
  1002. FloatABI = "hard";
  1003. }
  1004. return FloatABI;
  1005. }
  1006. static void AddTargetFeature(const ArgList &Args,
  1007. std::vector<const char *> &Features,
  1008. OptSpecifier OnOpt, OptSpecifier OffOpt,
  1009. StringRef FeatureName) {
  1010. if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
  1011. if (A->getOption().matches(OnOpt))
  1012. Features.push_back(Args.MakeArgString("+" + FeatureName));
  1013. else
  1014. Features.push_back(Args.MakeArgString("-" + FeatureName));
  1015. }
  1016. }
  1017. static void getMIPSTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  1018. const ArgList &Args,
  1019. std::vector<const char *> &Features) {
  1020. StringRef CPUName;
  1021. StringRef ABIName;
  1022. mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
  1023. ABIName = getGnuCompatibleMipsABIName(ABIName);
  1024. AddTargetFeature(Args, Features, options::OPT_mno_abicalls,
  1025. options::OPT_mabicalls, "noabicalls");
  1026. StringRef FloatABI = getMipsFloatABI(D, Args);
  1027. if (FloatABI == "soft") {
  1028. // FIXME: Note, this is a hack. We need to pass the selected float
  1029. // mode to the MipsTargetInfoBase to define appropriate macros there.
  1030. // Now it is the only method.
  1031. Features.push_back("+soft-float");
  1032. }
  1033. if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
  1034. StringRef Val = StringRef(A->getValue());
  1035. if (Val == "2008") {
  1036. if (mips::getSupportedNanEncoding(CPUName) & mips::Nan2008)
  1037. Features.push_back("+nan2008");
  1038. else {
  1039. Features.push_back("-nan2008");
  1040. D.Diag(diag::warn_target_unsupported_nan2008) << CPUName;
  1041. }
  1042. } else if (Val == "legacy") {
  1043. if (mips::getSupportedNanEncoding(CPUName) & mips::NanLegacy)
  1044. Features.push_back("-nan2008");
  1045. else {
  1046. Features.push_back("+nan2008");
  1047. D.Diag(diag::warn_target_unsupported_nanlegacy) << CPUName;
  1048. }
  1049. } else
  1050. D.Diag(diag::err_drv_unsupported_option_argument)
  1051. << A->getOption().getName() << Val;
  1052. }
  1053. AddTargetFeature(Args, Features, options::OPT_msingle_float,
  1054. options::OPT_mdouble_float, "single-float");
  1055. AddTargetFeature(Args, Features, options::OPT_mips16, options::OPT_mno_mips16,
  1056. "mips16");
  1057. AddTargetFeature(Args, Features, options::OPT_mmicromips,
  1058. options::OPT_mno_micromips, "micromips");
  1059. AddTargetFeature(Args, Features, options::OPT_mdsp, options::OPT_mno_dsp,
  1060. "dsp");
  1061. AddTargetFeature(Args, Features, options::OPT_mdspr2, options::OPT_mno_dspr2,
  1062. "dspr2");
  1063. AddTargetFeature(Args, Features, options::OPT_mmsa, options::OPT_mno_msa,
  1064. "msa");
  1065. // Add the last -mfp32/-mfpxx/-mfp64 or if none are given and the ABI is O32
  1066. // pass -mfpxx
  1067. if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
  1068. options::OPT_mfp64)) {
  1069. if (A->getOption().matches(options::OPT_mfp32))
  1070. Features.push_back(Args.MakeArgString("-fp64"));
  1071. else if (A->getOption().matches(options::OPT_mfpxx)) {
  1072. Features.push_back(Args.MakeArgString("+fpxx"));
  1073. Features.push_back(Args.MakeArgString("+nooddspreg"));
  1074. } else
  1075. Features.push_back(Args.MakeArgString("+fp64"));
  1076. } else if (mips::shouldUseFPXX(Args, Triple, CPUName, ABIName, FloatABI)) {
  1077. Features.push_back(Args.MakeArgString("+fpxx"));
  1078. Features.push_back(Args.MakeArgString("+nooddspreg"));
  1079. }
  1080. AddTargetFeature(Args, Features, options::OPT_mno_odd_spreg,
  1081. options::OPT_modd_spreg, "nooddspreg");
  1082. }
  1083. void Clang::AddMIPSTargetArgs(const ArgList &Args,
  1084. ArgStringList &CmdArgs) const {
  1085. const Driver &D = getToolChain().getDriver();
  1086. StringRef CPUName;
  1087. StringRef ABIName;
  1088. const llvm::Triple &Triple = getToolChain().getTriple();
  1089. mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
  1090. CmdArgs.push_back("-target-abi");
  1091. CmdArgs.push_back(ABIName.data());
  1092. StringRef FloatABI = getMipsFloatABI(D, Args);
  1093. if (FloatABI == "soft") {
  1094. // Floating point operations and argument passing are soft.
  1095. CmdArgs.push_back("-msoft-float");
  1096. CmdArgs.push_back("-mfloat-abi");
  1097. CmdArgs.push_back("soft");
  1098. } else {
  1099. // Floating point operations and argument passing are hard.
  1100. assert(FloatABI == "hard" && "Invalid float abi!");
  1101. CmdArgs.push_back("-mfloat-abi");
  1102. CmdArgs.push_back("hard");
  1103. }
  1104. if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
  1105. if (A->getOption().matches(options::OPT_mxgot)) {
  1106. CmdArgs.push_back("-mllvm");
  1107. CmdArgs.push_back("-mxgot");
  1108. }
  1109. }
  1110. if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
  1111. options::OPT_mno_ldc1_sdc1)) {
  1112. if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
  1113. CmdArgs.push_back("-mllvm");
  1114. CmdArgs.push_back("-mno-ldc1-sdc1");
  1115. }
  1116. }
  1117. if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
  1118. options::OPT_mno_check_zero_division)) {
  1119. if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
  1120. CmdArgs.push_back("-mllvm");
  1121. CmdArgs.push_back("-mno-check-zero-division");
  1122. }
  1123. }
  1124. if (Arg *A = Args.getLastArg(options::OPT_G)) {
  1125. StringRef v = A->getValue();
  1126. CmdArgs.push_back("-mllvm");
  1127. CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
  1128. A->claim();
  1129. }
  1130. }
  1131. /// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
  1132. static std::string getPPCTargetCPU(const ArgList &Args) {
  1133. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
  1134. StringRef CPUName = A->getValue();
  1135. if (CPUName == "native") {
  1136. std::string CPU = llvm::sys::getHostCPUName();
  1137. if (!CPU.empty() && CPU != "generic")
  1138. return CPU;
  1139. else
  1140. return "";
  1141. }
  1142. return llvm::StringSwitch<const char *>(CPUName)
  1143. .Case("common", "generic")
  1144. .Case("440", "440")
  1145. .Case("440fp", "440")
  1146. .Case("450", "450")
  1147. .Case("601", "601")
  1148. .Case("602", "602")
  1149. .Case("603", "603")
  1150. .Case("603e", "603e")
  1151. .Case("603ev", "603ev")
  1152. .Case("604", "604")
  1153. .Case("604e", "604e")
  1154. .Case("620", "620")
  1155. .Case("630", "pwr3")
  1156. .Case("G3", "g3")
  1157. .Case("7400", "7400")
  1158. .Case("G4", "g4")
  1159. .Case("7450", "7450")
  1160. .Case("G4+", "g4+")
  1161. .Case("750", "750")
  1162. .Case("970", "970")
  1163. .Case("G5", "g5")
  1164. .Case("a2", "a2")
  1165. .Case("a2q", "a2q")
  1166. .Case("e500mc", "e500mc")
  1167. .Case("e5500", "e5500")
  1168. .Case("power3", "pwr3")
  1169. .Case("power4", "pwr4")
  1170. .Case("power5", "pwr5")
  1171. .Case("power5x", "pwr5x")
  1172. .Case("power6", "pwr6")
  1173. .Case("power6x", "pwr6x")
  1174. .Case("power7", "pwr7")
  1175. .Case("power8", "pwr8")
  1176. .Case("pwr3", "pwr3")
  1177. .Case("pwr4", "pwr4")
  1178. .Case("pwr5", "pwr5")
  1179. .Case("pwr5x", "pwr5x")
  1180. .Case("pwr6", "pwr6")
  1181. .Case("pwr6x", "pwr6x")
  1182. .Case("pwr7", "pwr7")
  1183. .Case("pwr8", "pwr8")
  1184. .Case("powerpc", "ppc")
  1185. .Case("powerpc64", "ppc64")
  1186. .Case("powerpc64le", "ppc64le")
  1187. .Default("");
  1188. }
  1189. return "";
  1190. }
  1191. static void getPPCTargetFeatures(const ArgList &Args,
  1192. std::vector<const char *> &Features) {
  1193. for (const Arg *A : Args.filtered(options::OPT_m_ppc_Features_Group)) {
  1194. StringRef Name = A->getOption().getName();
  1195. A->claim();
  1196. // Skip over "-m".
  1197. assert(Name.startswith("m") && "Invalid feature name.");
  1198. Name = Name.substr(1);
  1199. bool IsNegative = Name.startswith("no-");
  1200. if (IsNegative)
  1201. Name = Name.substr(3);
  1202. // Note that gcc calls this mfcrf and LLVM calls this mfocrf so we
  1203. // pass the correct option to the backend while calling the frontend
  1204. // option the same.
  1205. // TODO: Change the LLVM backend option maybe?
  1206. if (Name == "mfcrf")
  1207. Name = "mfocrf";
  1208. Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
  1209. }
  1210. // Altivec is a bit weird, allow overriding of the Altivec feature here.
  1211. AddTargetFeature(Args, Features, options::OPT_faltivec,
  1212. options::OPT_fno_altivec, "altivec");
  1213. }
  1214. void Clang::AddPPCTargetArgs(const ArgList &Args,
  1215. ArgStringList &CmdArgs) const {
  1216. // Select the ABI to use.
  1217. const char *ABIName = nullptr;
  1218. if (getToolChain().getTriple().isOSLinux())
  1219. switch (getToolChain().getArch()) {
  1220. case llvm::Triple::ppc64: {
  1221. // When targeting a processor that supports QPX, or if QPX is
  1222. // specifically enabled, default to using the ABI that supports QPX (so
  1223. // long as it is not specifically disabled).
  1224. bool HasQPX = false;
  1225. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
  1226. HasQPX = A->getValue() == StringRef("a2q");
  1227. HasQPX = Args.hasFlag(options::OPT_mqpx, options::OPT_mno_qpx, HasQPX);
  1228. if (HasQPX) {
  1229. ABIName = "elfv1-qpx";
  1230. break;
  1231. }
  1232. ABIName = "elfv1";
  1233. break;
  1234. }
  1235. case llvm::Triple::ppc64le:
  1236. ABIName = "elfv2";
  1237. break;
  1238. default:
  1239. break;
  1240. }
  1241. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
  1242. // The ppc64 linux abis are all "altivec" abis by default. Accept and ignore
  1243. // the option if given as we don't have backend support for any targets
  1244. // that don't use the altivec abi.
  1245. if (StringRef(A->getValue()) != "altivec")
  1246. ABIName = A->getValue();
  1247. if (ABIName) {
  1248. CmdArgs.push_back("-target-abi");
  1249. CmdArgs.push_back(ABIName);
  1250. }
  1251. }
  1252. bool ppc::hasPPCAbiArg(const ArgList &Args, const char *Value) {
  1253. Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
  1254. return A && (A->getValue() == StringRef(Value));
  1255. }
  1256. /// Get the (LLVM) name of the R600 gpu we are targeting.
  1257. static std::string getR600TargetGPU(const ArgList &Args) {
  1258. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
  1259. const char *GPUName = A->getValue();
  1260. return llvm::StringSwitch<const char *>(GPUName)
  1261. .Cases("rv630", "rv635", "r600")
  1262. .Cases("rv610", "rv620", "rs780", "rs880")
  1263. .Case("rv740", "rv770")
  1264. .Case("palm", "cedar")
  1265. .Cases("sumo", "sumo2", "sumo")
  1266. .Case("hemlock", "cypress")
  1267. .Case("aruba", "cayman")
  1268. .Default(GPUName);
  1269. }
  1270. return "";
  1271. }
  1272. void Clang::AddSparcTargetArgs(const ArgList &Args,
  1273. ArgStringList &CmdArgs) const {
  1274. const Driver &D = getToolChain().getDriver();
  1275. std::string Triple = getToolChain().ComputeEffectiveClangTriple(Args);
  1276. bool SoftFloatABI = false;
  1277. if (Arg *A =
  1278. Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) {
  1279. if (A->getOption().matches(options::OPT_msoft_float))
  1280. SoftFloatABI = true;
  1281. }
  1282. // Only the hard-float ABI on Sparc is standardized, and it is the
  1283. // default. GCC also supports a nonstandard soft-float ABI mode, and
  1284. // perhaps LLVM should implement that, too. However, since llvm
  1285. // currently does not support Sparc soft-float, at all, display an
  1286. // error if it's requested.
  1287. if (SoftFloatABI) {
  1288. D.Diag(diag::err_drv_unsupported_opt_for_target) << "-msoft-float"
  1289. << Triple;
  1290. }
  1291. }
  1292. static const char *getSystemZTargetCPU(const ArgList &Args) {
  1293. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
  1294. return A->getValue();
  1295. return "z10";
  1296. }
  1297. static void getSystemZTargetFeatures(const ArgList &Args,
  1298. std::vector<const char *> &Features) {
  1299. // -m(no-)htm overrides use of the transactional-execution facility.
  1300. if (Arg *A = Args.getLastArg(options::OPT_mhtm, options::OPT_mno_htm)) {
  1301. if (A->getOption().matches(options::OPT_mhtm))
  1302. Features.push_back("+transactional-execution");
  1303. else
  1304. Features.push_back("-transactional-execution");
  1305. }
  1306. // -m(no-)vx overrides use of the vector facility.
  1307. if (Arg *A = Args.getLastArg(options::OPT_mvx, options::OPT_mno_vx)) {
  1308. if (A->getOption().matches(options::OPT_mvx))
  1309. Features.push_back("+vector");
  1310. else
  1311. Features.push_back("-vector");
  1312. }
  1313. }
  1314. static const char *getX86TargetCPU(const ArgList &Args,
  1315. const llvm::Triple &Triple) {
  1316. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  1317. if (StringRef(A->getValue()) != "native") {
  1318. if (Triple.isOSDarwin() && Triple.getArchName() == "x86_64h")
  1319. return "core-avx2";
  1320. return A->getValue();
  1321. }
  1322. // FIXME: Reject attempts to use -march=native unless the target matches
  1323. // the host.
  1324. //
  1325. // FIXME: We should also incorporate the detected target features for use
  1326. // with -native.
  1327. std::string CPU = llvm::sys::getHostCPUName();
  1328. if (!CPU.empty() && CPU != "generic")
  1329. return Args.MakeArgString(CPU);
  1330. }
  1331. if (const Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
  1332. // Mapping built by referring to X86TargetInfo::getDefaultFeatures().
  1333. StringRef Arch = A->getValue();
  1334. const char *CPU;
  1335. if (Triple.getArch() == llvm::Triple::x86) {
  1336. CPU = llvm::StringSwitch<const char *>(Arch)
  1337. .Case("IA32", "i386")
  1338. .Case("SSE", "pentium3")
  1339. .Case("SSE2", "pentium4")
  1340. .Case("AVX", "sandybridge")
  1341. .Case("AVX2", "haswell")
  1342. .Default(nullptr);
  1343. } else {
  1344. CPU = llvm::StringSwitch<const char *>(Arch)
  1345. .Case("AVX", "sandybridge")
  1346. .Case("AVX2", "haswell")
  1347. .Default(nullptr);
  1348. }
  1349. if (CPU)
  1350. return CPU;
  1351. }
  1352. // Select the default CPU if none was given (or detection failed).
  1353. if (Triple.getArch() != llvm::Triple::x86_64 &&
  1354. Triple.getArch() != llvm::Triple::x86)
  1355. return nullptr; // This routine is only handling x86 targets.
  1356. bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
  1357. // FIXME: Need target hooks.
  1358. if (Triple.isOSDarwin()) {
  1359. if (Triple.getArchName() == "x86_64h")
  1360. return "core-avx2";
  1361. return Is64Bit ? "core2" : "yonah";
  1362. }
  1363. // Set up default CPU name for PS4 compilers.
  1364. if (Triple.isPS4CPU())
  1365. return "btver2";
  1366. // On Android use targets compatible with gcc
  1367. if (Triple.getEnvironment() == llvm::Triple::Android)
  1368. return Is64Bit ? "x86-64" : "i686";
  1369. // Everything else goes to x86-64 in 64-bit mode.
  1370. if (Is64Bit)
  1371. return "x86-64";
  1372. switch (Triple.getOS()) {
  1373. case llvm::Triple::FreeBSD:
  1374. case llvm::Triple::NetBSD:
  1375. case llvm::Triple::OpenBSD:
  1376. return "i486";
  1377. case llvm::Triple::Haiku:
  1378. return "i586";
  1379. case llvm::Triple::Bitrig:
  1380. return "i686";
  1381. default:
  1382. // Fallback to p4.
  1383. return "pentium4";
  1384. }
  1385. }
  1386. static std::string getCPUName(const ArgList &Args, const llvm::Triple &T,
  1387. bool FromAs = false) {
  1388. switch (T.getArch()) {
  1389. default:
  1390. return "";
  1391. case llvm::Triple::aarch64:
  1392. case llvm::Triple::aarch64_be:
  1393. return getAArch64TargetCPU(Args);
  1394. case llvm::Triple::arm:
  1395. case llvm::Triple::armeb:
  1396. case llvm::Triple::thumb:
  1397. case llvm::Triple::thumbeb: {
  1398. StringRef MArch, MCPU;
  1399. getARMArchCPUFromArgs(Args, MArch, MCPU, FromAs);
  1400. return arm::getARMTargetCPU(MCPU, MArch, T);
  1401. }
  1402. case llvm::Triple::mips:
  1403. case llvm::Triple::mipsel:
  1404. case llvm::Triple::mips64:
  1405. case llvm::Triple::mips64el: {
  1406. StringRef CPUName;
  1407. StringRef ABIName;
  1408. mips::getMipsCPUAndABI(Args, T, CPUName, ABIName);
  1409. return CPUName;
  1410. }
  1411. case llvm::Triple::nvptx:
  1412. case llvm::Triple::nvptx64:
  1413. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
  1414. return A->getValue();
  1415. return "";
  1416. case llvm::Triple::ppc:
  1417. case llvm::Triple::ppc64:
  1418. case llvm::Triple::ppc64le: {
  1419. std::string TargetCPUName = getPPCTargetCPU(Args);
  1420. // LLVM may default to generating code for the native CPU,
  1421. // but, like gcc, we default to a more generic option for
  1422. // each architecture. (except on Darwin)
  1423. if (TargetCPUName.empty() && !T.isOSDarwin()) {
  1424. if (T.getArch() == llvm::Triple::ppc64)
  1425. TargetCPUName = "ppc64";
  1426. else if (T.getArch() == llvm::Triple::ppc64le)
  1427. TargetCPUName = "ppc64le";
  1428. else
  1429. TargetCPUName = "ppc";
  1430. }
  1431. return TargetCPUName;
  1432. }
  1433. case llvm::Triple::sparc:
  1434. case llvm::Triple::sparcel:
  1435. case llvm::Triple::sparcv9:
  1436. if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
  1437. return A->getValue();
  1438. return "";
  1439. case llvm::Triple::x86:
  1440. case llvm::Triple::x86_64:
  1441. return getX86TargetCPU(Args, T);
  1442. case llvm::Triple::hexagon:
  1443. return "hexagon" + toolchains::Hexagon_TC::GetTargetCPU(Args).str();
  1444. case llvm::Triple::systemz:
  1445. return getSystemZTargetCPU(Args);
  1446. case llvm::Triple::r600:
  1447. case llvm::Triple::amdgcn:
  1448. return getR600TargetGPU(Args);
  1449. }
  1450. }
  1451. static void AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args,
  1452. ArgStringList &CmdArgs) {
  1453. // Tell the linker to load the plugin. This has to come before AddLinkerInputs
  1454. // as gold requires -plugin to come before any -plugin-opt that -Wl might
  1455. // forward.
  1456. CmdArgs.push_back("-plugin");
  1457. std::string Plugin =
  1458. ToolChain.getDriver().Dir + "/../lib" CLANG_LIBDIR_SUFFIX "/LLVMgold.so";
  1459. CmdArgs.push_back(Args.MakeArgString(Plugin));
  1460. // Try to pass driver level flags relevant to LTO code generation down to
  1461. // the plugin.
  1462. // Handle flags for selecting CPU variants.
  1463. std::string CPU = getCPUName(Args, ToolChain.getTriple());
  1464. if (!CPU.empty())
  1465. CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU));
  1466. }
  1467. /// This is a helper function for validating the optional refinement step
  1468. /// parameter in reciprocal argument strings. Return false if there is an error
  1469. /// parsing the refinement step. Otherwise, return true and set the Position
  1470. /// of the refinement step in the input string.
  1471. static bool getRefinementStep(const StringRef &In, const Driver &D,
  1472. const Arg &A, size_t &Position) {
  1473. const char RefinementStepToken = ':';
  1474. Position = In.find(RefinementStepToken);
  1475. if (Position != StringRef::npos) {
  1476. StringRef Option = A.getOption().getName();
  1477. StringRef RefStep = In.substr(Position + 1);
  1478. // Allow exactly one numeric character for the additional refinement
  1479. // step parameter. This is reasonable for all currently-supported
  1480. // operations and architectures because we would expect that a larger value
  1481. // of refinement steps would cause the estimate "optimization" to
  1482. // under-perform the native operation. Also, if the estimate does not
  1483. // converge quickly, it probably will not ever converge, so further
  1484. // refinement steps will not produce a better answer.
  1485. if (RefStep.size() != 1) {
  1486. D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
  1487. return false;
  1488. }
  1489. char RefStepChar = RefStep[0];
  1490. if (RefStepChar < '0' || RefStepChar > '9') {
  1491. D.Diag(diag::err_drv_invalid_value) << Option << RefStep;
  1492. return false;
  1493. }
  1494. }
  1495. return true;
  1496. }
  1497. /// The -mrecip flag requires processing of many optional parameters.
  1498. static void ParseMRecip(const Driver &D, const ArgList &Args,
  1499. ArgStringList &OutStrings) {
  1500. StringRef DisabledPrefixIn = "!";
  1501. StringRef DisabledPrefixOut = "!";
  1502. StringRef EnabledPrefixOut = "";
  1503. StringRef Out = "-mrecip=";
  1504. Arg *A = Args.getLastArg(options::OPT_mrecip, options::OPT_mrecip_EQ);
  1505. if (!A)
  1506. return;
  1507. unsigned NumOptions = A->getNumValues();
  1508. if (NumOptions == 0) {
  1509. // No option is the same as "all".
  1510. OutStrings.push_back(Args.MakeArgString(Out + "all"));
  1511. return;
  1512. }
  1513. // Pass through "all", "none", or "default" with an optional refinement step.
  1514. if (NumOptions == 1) {
  1515. StringRef Val = A->getValue(0);
  1516. size_t RefStepLoc;
  1517. if (!getRefinementStep(Val, D, *A, RefStepLoc))
  1518. return;
  1519. StringRef ValBase = Val.slice(0, RefStepLoc);
  1520. if (ValBase == "all" || ValBase == "none" || ValBase == "default") {
  1521. OutStrings.push_back(Args.MakeArgString(Out + Val));
  1522. return;
  1523. }
  1524. }
  1525. // Each reciprocal type may be enabled or disabled individually.
  1526. // Check each input value for validity, concatenate them all back together,
  1527. // and pass through.
  1528. llvm::StringMap<bool> OptionStrings;
  1529. OptionStrings.insert(std::make_pair("divd", false));
  1530. OptionStrings.insert(std::make_pair("divf", false));
  1531. OptionStrings.insert(std::make_pair("vec-divd", false));
  1532. OptionStrings.insert(std::make_pair("vec-divf", false));
  1533. OptionStrings.insert(std::make_pair("sqrtd", false));
  1534. OptionStrings.insert(std::make_pair("sqrtf", false));
  1535. OptionStrings.insert(std::make_pair("vec-sqrtd", false));
  1536. OptionStrings.insert(std::make_pair("vec-sqrtf", false));
  1537. for (unsigned i = 0; i != NumOptions; ++i) {
  1538. StringRef Val = A->getValue(i);
  1539. bool IsDisabled = Val.startswith(DisabledPrefixIn);
  1540. // Ignore the disablement token for string matching.
  1541. if (IsDisabled)
  1542. Val = Val.substr(1);
  1543. size_t RefStep;
  1544. if (!getRefinementStep(Val, D, *A, RefStep))
  1545. return;
  1546. StringRef ValBase = Val.slice(0, RefStep);
  1547. llvm::StringMap<bool>::iterator OptionIter = OptionStrings.find(ValBase);
  1548. if (OptionIter == OptionStrings.end()) {
  1549. // Try again specifying float suffix.
  1550. OptionIter = OptionStrings.find(ValBase.str() + 'f');
  1551. if (OptionIter == OptionStrings.end()) {
  1552. // The input name did not match any known option string.
  1553. D.Diag(diag::err_drv_unknown_argument) << Val;
  1554. return;
  1555. }
  1556. // The option was specified without a float or double suffix.
  1557. // Make sure that the double entry was not already specified.
  1558. // The float entry will be checked below.
  1559. if (OptionStrings[ValBase.str() + 'd']) {
  1560. D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
  1561. return;
  1562. }
  1563. }
  1564. if (OptionIter->second == true) {
  1565. // Duplicate option specified.
  1566. D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Val;
  1567. return;
  1568. }
  1569. // Mark the matched option as found. Do not allow duplicate specifiers.
  1570. OptionIter->second = true;
  1571. // If the precision was not specified, also mark the double entry as found.
  1572. if (ValBase.back() != 'f' && ValBase.back() != 'd')
  1573. OptionStrings[ValBase.str() + 'd'] = true;
  1574. // Build the output string.
  1575. StringRef Prefix = IsDisabled ? DisabledPrefixOut : EnabledPrefixOut;
  1576. Out = Args.MakeArgString(Out + Prefix + Val);
  1577. if (i != NumOptions - 1)
  1578. Out = Args.MakeArgString(Out + ",");
  1579. }
  1580. OutStrings.push_back(Args.MakeArgString(Out));
  1581. }
  1582. static void getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
  1583. const ArgList &Args,
  1584. std::vector<const char *> &Features) {
  1585. // If -march=native, autodetect the feature list.
  1586. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  1587. if (StringRef(A->getValue()) == "native") {
  1588. llvm::StringMap<bool> HostFeatures;
  1589. if (llvm::sys::getHostCPUFeatures(HostFeatures))
  1590. for (auto &F : HostFeatures)
  1591. Features.push_back(
  1592. Args.MakeArgString((F.second ? "+" : "-") + F.first()));
  1593. }
  1594. }
  1595. if (Triple.getArchName() == "x86_64h") {
  1596. // x86_64h implies quite a few of the more modern subtarget features
  1597. // for Haswell class CPUs, but not all of them. Opt-out of a few.
  1598. Features.push_back("-rdrnd");
  1599. Features.push_back("-aes");
  1600. Features.push_back("-pclmul");
  1601. Features.push_back("-rtm");
  1602. Features.push_back("-hle");
  1603. Features.push_back("-fsgsbase");
  1604. }
  1605. const llvm::Triple::ArchType ArchType = Triple.getArch();
  1606. // Add features to be compatible with gcc for Android.
  1607. if (Triple.getEnvironment() == llvm::Triple::Android) {
  1608. if (ArchType == llvm::Triple::x86_64) {
  1609. Features.push_back("+sse4.2");
  1610. Features.push_back("+popcnt");
  1611. } else
  1612. Features.push_back("+ssse3");
  1613. }
  1614. // Set features according to the -arch flag on MSVC.
  1615. if (Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
  1616. StringRef Arch = A->getValue();
  1617. bool ArchUsed = false;
  1618. // First, look for flags that are shared in x86 and x86-64.
  1619. if (ArchType == llvm::Triple::x86_64 || ArchType == llvm::Triple::x86) {
  1620. if (Arch == "AVX" || Arch == "AVX2") {
  1621. ArchUsed = true;
  1622. Features.push_back(Args.MakeArgString("+" + Arch.lower()));
  1623. }
  1624. }
  1625. // Then, look for x86-specific flags.
  1626. if (ArchType == llvm::Triple::x86) {
  1627. if (Arch == "IA32") {
  1628. ArchUsed = true;
  1629. } else if (Arch == "SSE" || Arch == "SSE2") {
  1630. ArchUsed = true;
  1631. Features.push_back(Args.MakeArgString("+" + Arch.lower()));
  1632. }
  1633. }
  1634. if (!ArchUsed)
  1635. D.Diag(clang::diag::warn_drv_unused_argument) << A->getAsString(Args);
  1636. }
  1637. // Now add any that the user explicitly requested on the command line,
  1638. // which may override the defaults.
  1639. for (const Arg *A : Args.filtered(options::OPT_m_x86_Features_Group)) {
  1640. StringRef Name = A->getOption().getName();
  1641. A->claim();
  1642. // Skip over "-m".
  1643. assert(Name.startswith("m") && "Invalid feature name.");
  1644. Name = Name.substr(1);
  1645. bool IsNegative = Name.startswith("no-");
  1646. if (IsNegative)
  1647. Name = Name.substr(3);
  1648. Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
  1649. }
  1650. }
  1651. void Clang::AddX86TargetArgs(const ArgList &Args,
  1652. ArgStringList &CmdArgs) const {
  1653. if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
  1654. Args.hasArg(options::OPT_mkernel) ||
  1655. Args.hasArg(options::OPT_fapple_kext))
  1656. CmdArgs.push_back("-disable-red-zone");
  1657. // Default to avoid implicit floating-point for kernel/kext code, but allow
  1658. // that to be overridden with -mno-soft-float.
  1659. bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
  1660. Args.hasArg(options::OPT_fapple_kext));
  1661. if (Arg *A = Args.getLastArg(
  1662. options::OPT_msoft_float, options::OPT_mno_soft_float,
  1663. options::OPT_mimplicit_float, options::OPT_mno_implicit_float)) {
  1664. const Option &O = A->getOption();
  1665. NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
  1666. O.matches(options::OPT_msoft_float));
  1667. }
  1668. if (NoImplicitFloat)
  1669. CmdArgs.push_back("-no-implicit-float");
  1670. if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
  1671. StringRef Value = A->getValue();
  1672. if (Value == "intel" || Value == "att") {
  1673. CmdArgs.push_back("-mllvm");
  1674. CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
  1675. } else {
  1676. getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
  1677. << A->getOption().getName() << Value;
  1678. }
  1679. }
  1680. }
  1681. void Clang::AddHexagonTargetArgs(const ArgList &Args,
  1682. ArgStringList &CmdArgs) const {
  1683. CmdArgs.push_back("-mqdsp6-compat");
  1684. CmdArgs.push_back("-Wreturn-type");
  1685. if (const char *v = toolchains::Hexagon_TC::GetSmallDataThreshold(Args)) {
  1686. std::string SmallDataThreshold = "-hexagon-small-data-threshold=";
  1687. SmallDataThreshold += v;
  1688. CmdArgs.push_back("-mllvm");
  1689. CmdArgs.push_back(Args.MakeArgString(SmallDataThreshold));
  1690. }
  1691. if (!Args.hasArg(options::OPT_fno_short_enums))
  1692. CmdArgs.push_back("-fshort-enums");
  1693. if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
  1694. CmdArgs.push_back("-mllvm");
  1695. CmdArgs.push_back("-enable-hexagon-ieee-rnd-near");
  1696. }
  1697. CmdArgs.push_back("-mllvm");
  1698. CmdArgs.push_back("-machine-sink-split=0");
  1699. }
  1700. // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
  1701. static bool DecodeAArch64Features(const Driver &D, StringRef text,
  1702. std::vector<const char *> &Features) {
  1703. SmallVector<StringRef, 8> Split;
  1704. text.split(Split, StringRef("+"), -1, false);
  1705. for (const StringRef Feature : Split) {
  1706. const char *result = llvm::StringSwitch<const char *>(Feature)
  1707. .Case("fp", "+fp-armv8")
  1708. .Case("simd", "+neon")
  1709. .Case("crc", "+crc")
  1710. .Case("crypto", "+crypto")
  1711. .Case("nofp", "-fp-armv8")
  1712. .Case("nosimd", "-neon")
  1713. .Case("nocrc", "-crc")
  1714. .Case("nocrypto", "-crypto")
  1715. .Default(nullptr);
  1716. if (result)
  1717. Features.push_back(result);
  1718. else if (Feature == "neon" || Feature == "noneon")
  1719. D.Diag(diag::err_drv_no_neon_modifier);
  1720. else
  1721. return false;
  1722. }
  1723. return true;
  1724. }
  1725. // Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
  1726. // decode CPU and feature.
  1727. static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
  1728. std::vector<const char *> &Features) {
  1729. std::pair<StringRef, StringRef> Split = Mcpu.split("+");
  1730. CPU = Split.first;
  1731. if (CPU == "cyclone" || CPU == "cortex-a53" || CPU == "cortex-a57" ||
  1732. CPU == "cortex-a72") {
  1733. Features.push_back("+neon");
  1734. Features.push_back("+crc");
  1735. Features.push_back("+crypto");
  1736. } else if (CPU == "generic") {
  1737. Features.push_back("+neon");
  1738. } else {
  1739. return false;
  1740. }
  1741. if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
  1742. return false;
  1743. return true;
  1744. }
  1745. static bool
  1746. getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
  1747. const ArgList &Args,
  1748. std::vector<const char *> &Features) {
  1749. std::string MarchLowerCase = March.lower();
  1750. std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
  1751. if (Split.first == "armv8-a" || Split.first == "armv8a") {
  1752. // ok, no additional features.
  1753. } else if (Split.first == "armv8.1-a" || Split.first == "armv8.1a") {
  1754. Features.push_back("+v8.1a");
  1755. } else {
  1756. return false;
  1757. }
  1758. if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
  1759. return false;
  1760. return true;
  1761. }
  1762. static bool
  1763. getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
  1764. const ArgList &Args,
  1765. std::vector<const char *> &Features) {
  1766. StringRef CPU;
  1767. std::string McpuLowerCase = Mcpu.lower();
  1768. if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
  1769. return false;
  1770. return true;
  1771. }
  1772. static bool
  1773. getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
  1774. const ArgList &Args,
  1775. std::vector<const char *> &Features) {
  1776. // Handle CPU name is 'native'.
  1777. if (Mtune == "native")
  1778. Mtune = llvm::sys::getHostCPUName();
  1779. if (Mtune == "cyclone") {
  1780. Features.push_back("+zcm");
  1781. Features.push_back("+zcz");
  1782. }
  1783. return true;
  1784. }
  1785. static bool
  1786. getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
  1787. const ArgList &Args,
  1788. std::vector<const char *> &Features) {
  1789. StringRef CPU;
  1790. std::vector<const char *> DecodedFeature;
  1791. std::string McpuLowerCase = Mcpu.lower();
  1792. if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
  1793. return false;
  1794. return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
  1795. }
  1796. static void getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
  1797. std::vector<const char *> &Features) {
  1798. Arg *A;
  1799. bool success = true;
  1800. // Enable NEON by default.
  1801. Features.push_back("+neon");
  1802. if ((A = Args.getLastArg(options::OPT_march_EQ)))
  1803. success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
  1804. else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
  1805. success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
  1806. else if (Args.hasArg(options::OPT_arch))
  1807. success = getAArch64ArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args), Args,
  1808. Features);
  1809. if (success && (A = Args.getLastArg(options::OPT_mtune_EQ)))
  1810. success =
  1811. getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
  1812. else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
  1813. success =
  1814. getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
  1815. else if (Args.hasArg(options::OPT_arch))
  1816. success = getAArch64MicroArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args),
  1817. Args, Features);
  1818. if (!success)
  1819. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  1820. if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
  1821. Features.push_back("-fp-armv8");
  1822. Features.push_back("-crypto");
  1823. Features.push_back("-neon");
  1824. }
  1825. // En/disable crc
  1826. if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
  1827. if (A->getOption().matches(options::OPT_mcrc))
  1828. Features.push_back("+crc");
  1829. else
  1830. Features.push_back("-crc");
  1831. }
  1832. }
  1833. static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  1834. const ArgList &Args, ArgStringList &CmdArgs,
  1835. bool ForAS) {
  1836. std::vector<const char *> Features;
  1837. switch (Triple.getArch()) {
  1838. default:
  1839. break;
  1840. case llvm::Triple::mips:
  1841. case llvm::Triple::mipsel:
  1842. case llvm::Triple::mips64:
  1843. case llvm::Triple::mips64el:
  1844. getMIPSTargetFeatures(D, Triple, Args, Features);
  1845. break;
  1846. case llvm::Triple::arm:
  1847. case llvm::Triple::armeb:
  1848. case llvm::Triple::thumb:
  1849. case llvm::Triple::thumbeb:
  1850. getARMTargetFeatures(D, Triple, Args, Features, ForAS);
  1851. break;
  1852. case llvm::Triple::ppc:
  1853. case llvm::Triple::ppc64:
  1854. case llvm::Triple::ppc64le:
  1855. getPPCTargetFeatures(Args, Features);
  1856. break;
  1857. case llvm::Triple::systemz:
  1858. getSystemZTargetFeatures(Args, Features);
  1859. break;
  1860. case llvm::Triple::aarch64:
  1861. case llvm::Triple::aarch64_be:
  1862. getAArch64TargetFeatures(D, Args, Features);
  1863. break;
  1864. case llvm::Triple::x86:
  1865. case llvm::Triple::x86_64:
  1866. getX86TargetFeatures(D, Triple, Args, Features);
  1867. break;
  1868. }
  1869. // Find the last of each feature.
  1870. llvm::StringMap<unsigned> LastOpt;
  1871. for (unsigned I = 0, N = Features.size(); I < N; ++I) {
  1872. const char *Name = Features[I];
  1873. assert(Name[0] == '-' || Name[0] == '+');
  1874. LastOpt[Name + 1] = I;
  1875. }
  1876. for (unsigned I = 0, N = Features.size(); I < N; ++I) {
  1877. // If this feature was overridden, ignore it.
  1878. const char *Name = Features[I];
  1879. llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name + 1);
  1880. assert(LastI != LastOpt.end());
  1881. unsigned Last = LastI->second;
  1882. if (Last != I)
  1883. continue;
  1884. CmdArgs.push_back("-target-feature");
  1885. CmdArgs.push_back(Name);
  1886. }
  1887. }
  1888. static bool
  1889. shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
  1890. const llvm::Triple &Triple) {
  1891. // We use the zero-cost exception tables for Objective-C if the non-fragile
  1892. // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
  1893. // later.
  1894. if (runtime.isNonFragile())
  1895. return true;
  1896. if (!Triple.isMacOSX())
  1897. return false;
  1898. return (!Triple.isMacOSXVersionLT(10, 5) &&
  1899. (Triple.getArch() == llvm::Triple::x86_64 ||
  1900. Triple.getArch() == llvm::Triple::arm));
  1901. }
  1902. /// Adds exception related arguments to the driver command arguments. There's a
  1903. /// master flag, -fexceptions and also language specific flags to enable/disable
  1904. /// C++ and Objective-C exceptions. This makes it possible to for example
  1905. /// disable C++ exceptions but enable Objective-C exceptions.
  1906. static void addExceptionArgs(const ArgList &Args, types::ID InputType,
  1907. const ToolChain &TC, bool KernelOrKext,
  1908. const ObjCRuntime &objcRuntime,
  1909. ArgStringList &CmdArgs) {
  1910. const Driver &D = TC.getDriver();
  1911. const llvm::Triple &Triple = TC.getTriple();
  1912. if (KernelOrKext) {
  1913. // -mkernel and -fapple-kext imply no exceptions, so claim exception related
  1914. // arguments now to avoid warnings about unused arguments.
  1915. Args.ClaimAllArgs(options::OPT_fexceptions);
  1916. Args.ClaimAllArgs(options::OPT_fno_exceptions);
  1917. Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
  1918. Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
  1919. Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
  1920. Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
  1921. return;
  1922. }
  1923. // See if the user explicitly enabled exceptions.
  1924. bool EH = Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
  1925. false);
  1926. // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
  1927. // is not necessarily sensible, but follows GCC.
  1928. if (types::isObjC(InputType) &&
  1929. Args.hasFlag(options::OPT_fobjc_exceptions,
  1930. options::OPT_fno_objc_exceptions, true)) {
  1931. CmdArgs.push_back("-fobjc-exceptions");
  1932. EH |= shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
  1933. }
  1934. if (types::isCXX(InputType)) {
  1935. // Disable C++ EH by default on XCore, PS4, and MSVC.
  1936. // FIXME: Remove MSVC from this list once things work.
  1937. bool CXXExceptionsEnabled = Triple.getArch() != llvm::Triple::xcore &&
  1938. !Triple.isPS4CPU() &&
  1939. !Triple.isWindowsMSVCEnvironment();
  1940. Arg *ExceptionArg = Args.getLastArg(
  1941. options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
  1942. options::OPT_fexceptions, options::OPT_fno_exceptions);
  1943. if (ExceptionArg)
  1944. CXXExceptionsEnabled =
  1945. ExceptionArg->getOption().matches(options::OPT_fcxx_exceptions) ||
  1946. ExceptionArg->getOption().matches(options::OPT_fexceptions);
  1947. if (CXXExceptionsEnabled) {
  1948. if (Triple.isPS4CPU()) {
  1949. ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
  1950. assert(ExceptionArg &&
  1951. "On the PS4 exceptions should only be enabled if passing "
  1952. "an argument");
  1953. if (RTTIMode == ToolChain::RM_DisabledExplicitly) {
  1954. const Arg *RTTIArg = TC.getRTTIArg();
  1955. assert(RTTIArg && "RTTI disabled explicitly but no RTTIArg!");
  1956. D.Diag(diag::err_drv_argument_not_allowed_with)
  1957. << RTTIArg->getAsString(Args) << ExceptionArg->getAsString(Args);
  1958. } else if (RTTIMode == ToolChain::RM_EnabledImplicitly)
  1959. D.Diag(diag::warn_drv_enabling_rtti_with_exceptions);
  1960. } else
  1961. assert(TC.getRTTIMode() != ToolChain::RM_DisabledImplicitly);
  1962. CmdArgs.push_back("-fcxx-exceptions");
  1963. EH = true;
  1964. }
  1965. }
  1966. if (EH)
  1967. CmdArgs.push_back("-fexceptions");
  1968. }
  1969. static bool ShouldDisableAutolink(const ArgList &Args, const ToolChain &TC) {
  1970. bool Default = true;
  1971. if (TC.getTriple().isOSDarwin()) {
  1972. // The native darwin assembler doesn't support the linker_option directives,
  1973. // so we disable them if we think the .s file will be passed to it.
  1974. Default = TC.useIntegratedAs();
  1975. }
  1976. return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
  1977. Default);
  1978. }
  1979. static bool ShouldDisableDwarfDirectory(const ArgList &Args,
  1980. const ToolChain &TC) {
  1981. bool UseDwarfDirectory =
  1982. Args.hasFlag(options::OPT_fdwarf_directory_asm,
  1983. options::OPT_fno_dwarf_directory_asm, TC.useIntegratedAs());
  1984. return !UseDwarfDirectory;
  1985. }
  1986. /// \brief Check whether the given input tree contains any compilation actions.
  1987. static bool ContainsCompileAction(const Action *A) {
  1988. if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A))
  1989. return true;
  1990. for (const auto &Act : *A)
  1991. if (ContainsCompileAction(Act))
  1992. return true;
  1993. return false;
  1994. }
  1995. /// \brief Check if -relax-all should be passed to the internal assembler.
  1996. /// This is done by default when compiling non-assembler source with -O0.
  1997. static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
  1998. bool RelaxDefault = true;
  1999. if (Arg *A = Args.getLastArg(options::OPT_O_Group))
  2000. RelaxDefault = A->getOption().matches(options::OPT_O0);
  2001. if (RelaxDefault) {
  2002. RelaxDefault = false;
  2003. for (const auto &Act : C.getActions()) {
  2004. if (ContainsCompileAction(Act)) {
  2005. RelaxDefault = true;
  2006. break;
  2007. }
  2008. }
  2009. }
  2010. return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
  2011. RelaxDefault);
  2012. }
  2013. static void CollectArgsForIntegratedAssembler(Compilation &C,
  2014. const ArgList &Args,
  2015. ArgStringList &CmdArgs,
  2016. const Driver &D) {
  2017. if (UseRelaxAll(C, Args))
  2018. CmdArgs.push_back("-mrelax-all");
  2019. // When passing -I arguments to the assembler we sometimes need to
  2020. // unconditionally take the next argument. For example, when parsing
  2021. // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
  2022. // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
  2023. // arg after parsing the '-I' arg.
  2024. bool TakeNextArg = false;
  2025. // When using an integrated assembler, translate -Wa, and -Xassembler
  2026. // options.
  2027. bool CompressDebugSections = false;
  2028. for (const Arg *A :
  2029. Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
  2030. A->claim();
  2031. for (const StringRef Value : A->getValues()) {
  2032. if (TakeNextArg) {
  2033. CmdArgs.push_back(Value.data());
  2034. TakeNextArg = false;
  2035. continue;
  2036. }
  2037. if (Value == "-force_cpusubtype_ALL") {
  2038. // Do nothing, this is the default and we don't support anything else.
  2039. } else if (Value == "-L") {
  2040. CmdArgs.push_back("-msave-temp-labels");
  2041. } else if (Value == "--fatal-warnings") {
  2042. CmdArgs.push_back("-massembler-fatal-warnings");
  2043. } else if (Value == "--noexecstack") {
  2044. CmdArgs.push_back("-mnoexecstack");
  2045. } else if (Value == "-compress-debug-sections" ||
  2046. Value == "--compress-debug-sections") {
  2047. CompressDebugSections = true;
  2048. } else if (Value == "-nocompress-debug-sections" ||
  2049. Value == "--nocompress-debug-sections") {
  2050. CompressDebugSections = false;
  2051. } else if (Value.startswith("-I")) {
  2052. CmdArgs.push_back(Value.data());
  2053. // We need to consume the next argument if the current arg is a plain
  2054. // -I. The next arg will be the include directory.
  2055. if (Value == "-I")
  2056. TakeNextArg = true;
  2057. } else if (Value.startswith("-gdwarf-")) {
  2058. CmdArgs.push_back(Value.data());
  2059. } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") ||
  2060. Value.startswith("-mhwdiv") || Value.startswith("-march")) {
  2061. // Do nothing, we'll validate it later.
  2062. } else {
  2063. D.Diag(diag::err_drv_unsupported_option_argument)
  2064. << A->getOption().getName() << Value;
  2065. }
  2066. }
  2067. }
  2068. if (CompressDebugSections) {
  2069. if (llvm::zlib::isAvailable())
  2070. CmdArgs.push_back("-compress-debug-sections");
  2071. else
  2072. D.Diag(diag::warn_debug_compression_unavailable);
  2073. }
  2074. }
  2075. // Until ARM libraries are build separately, we have them all in one library
  2076. static StringRef getArchNameForCompilerRTLib(const ToolChain &TC) {
  2077. if (TC.getTriple().isWindowsMSVCEnvironment() &&
  2078. TC.getArch() == llvm::Triple::x86)
  2079. return "i386";
  2080. if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb)
  2081. return "arm";
  2082. return TC.getArchName();
  2083. }
  2084. static SmallString<128> getCompilerRTLibDir(const ToolChain &TC) {
  2085. // The runtimes are located in the OS-specific resource directory.
  2086. SmallString<128> Res(TC.getDriver().ResourceDir);
  2087. const llvm::Triple &Triple = TC.getTriple();
  2088. // TC.getOS() yield "freebsd10.0" whereas "freebsd" is expected.
  2089. StringRef OSLibName =
  2090. (Triple.getOS() == llvm::Triple::FreeBSD) ? "freebsd" : TC.getOS();
  2091. llvm::sys::path::append(Res, "lib", OSLibName);
  2092. return Res;
  2093. }
  2094. SmallString<128> tools::getCompilerRT(const ToolChain &TC, StringRef Component,
  2095. bool Shared) {
  2096. const char *Env = TC.getTriple().getEnvironment() == llvm::Triple::Android
  2097. ? "-android"
  2098. : "";
  2099. bool IsOSWindows = TC.getTriple().isOSWindows();
  2100. bool IsITANMSVCWindows = TC.getTriple().isWindowsMSVCEnvironment() ||
  2101. TC.getTriple().isWindowsItaniumEnvironment();
  2102. StringRef Arch = getArchNameForCompilerRTLib(TC);
  2103. const char *Prefix = IsITANMSVCWindows ? "" : "lib";
  2104. const char *Suffix =
  2105. Shared ? (IsOSWindows ? ".dll" : ".so") : (IsITANMSVCWindows ? ".lib" : ".a");
  2106. SmallString<128> Path = getCompilerRTLibDir(TC);
  2107. llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" +
  2108. Arch + Env + Suffix);
  2109. return Path;
  2110. }
  2111. // This adds the static libclang_rt.builtins-arch.a directly to the command line
  2112. // FIXME: Make sure we can also emit shared objects if they're requested
  2113. // and available, check for possible errors, etc.
  2114. static void addClangRT(const ToolChain &TC, const ArgList &Args,
  2115. ArgStringList &CmdArgs) {
  2116. CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, "builtins")));
  2117. if (!TC.getTriple().isOSWindows()) {
  2118. // FIXME: why do we link against gcc when we are using compiler-rt?
  2119. CmdArgs.push_back("-lgcc_s");
  2120. if (TC.getDriver().CCCIsCXX())
  2121. CmdArgs.push_back("-lgcc_eh");
  2122. }
  2123. }
  2124. static void addProfileRT(const ToolChain &TC, const ArgList &Args,
  2125. ArgStringList &CmdArgs) {
  2126. if (!(Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
  2127. false) ||
  2128. Args.hasArg(options::OPT_fprofile_generate) ||
  2129. Args.hasArg(options::OPT_fprofile_generate_EQ) ||
  2130. Args.hasArg(options::OPT_fprofile_instr_generate) ||
  2131. Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
  2132. Args.hasArg(options::OPT_fcreate_profile) ||
  2133. Args.hasArg(options::OPT_coverage)))
  2134. return;
  2135. CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, "profile")));
  2136. }
  2137. namespace {
  2138. enum OpenMPRuntimeKind {
  2139. /// An unknown OpenMP runtime. We can't generate effective OpenMP code
  2140. /// without knowing what runtime to target.
  2141. OMPRT_Unknown,
  2142. /// The LLVM OpenMP runtime. When completed and integrated, this will become
  2143. /// the default for Clang.
  2144. OMPRT_OMP,
  2145. /// The GNU OpenMP runtime. Clang doesn't support generating OpenMP code for
  2146. /// this runtime but can swallow the pragmas, and find and link against the
  2147. /// runtime library itself.
  2148. OMPRT_GOMP,
  2149. /// The legacy name for the LLVM OpenMP runtime from when it was the Intel
  2150. /// OpenMP runtime. We support this mode for users with existing dependencies
  2151. /// on this runtime library name.
  2152. OMPRT_IOMP5
  2153. };
  2154. }
  2155. /// Compute the desired OpenMP runtime from the flag provided.
  2156. static OpenMPRuntimeKind getOpenMPRuntime(const ToolChain &TC,
  2157. const ArgList &Args) {
  2158. StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME);
  2159. const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ);
  2160. if (A)
  2161. RuntimeName = A->getValue();
  2162. auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName)
  2163. .Case("libomp", OMPRT_OMP)
  2164. .Case("libgomp", OMPRT_GOMP)
  2165. .Case("libiomp5", OMPRT_IOMP5)
  2166. .Default(OMPRT_Unknown);
  2167. if (RT == OMPRT_Unknown) {
  2168. if (A)
  2169. TC.getDriver().Diag(diag::err_drv_unsupported_option_argument)
  2170. << A->getOption().getName() << A->getValue();
  2171. else
  2172. // FIXME: We could use a nicer diagnostic here.
  2173. TC.getDriver().Diag(diag::err_drv_unsupported_opt) << "-fopenmp";
  2174. }
  2175. return RT;
  2176. }
  2177. static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
  2178. ArgStringList &CmdArgs, StringRef Sanitizer,
  2179. bool IsShared) {
  2180. // Static runtimes must be forced into executable, so we wrap them in
  2181. // whole-archive.
  2182. if (!IsShared)
  2183. CmdArgs.push_back("-whole-archive");
  2184. CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, Sanitizer, IsShared)));
  2185. if (!IsShared)
  2186. CmdArgs.push_back("-no-whole-archive");
  2187. }
  2188. // Tries to use a file with the list of dynamic symbols that need to be exported
  2189. // from the runtime library. Returns true if the file was found.
  2190. static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args,
  2191. ArgStringList &CmdArgs,
  2192. StringRef Sanitizer) {
  2193. SmallString<128> SanRT = getCompilerRT(TC, Sanitizer);
  2194. if (llvm::sys::fs::exists(SanRT + ".syms")) {
  2195. CmdArgs.push_back(Args.MakeArgString("--dynamic-list=" + SanRT + ".syms"));
  2196. return true;
  2197. }
  2198. return false;
  2199. }
  2200. static void linkSanitizerRuntimeDeps(const ToolChain &TC,
  2201. ArgStringList &CmdArgs) {
  2202. // Force linking against the system libraries sanitizers depends on
  2203. // (see PR15823 why this is necessary).
  2204. CmdArgs.push_back("--no-as-needed");
  2205. CmdArgs.push_back("-lpthread");
  2206. CmdArgs.push_back("-lrt");
  2207. CmdArgs.push_back("-lm");
  2208. // There's no libdl on FreeBSD.
  2209. if (TC.getTriple().getOS() != llvm::Triple::FreeBSD)
  2210. CmdArgs.push_back("-ldl");
  2211. }
  2212. static void
  2213. collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
  2214. SmallVectorImpl<StringRef> &SharedRuntimes,
  2215. SmallVectorImpl<StringRef> &StaticRuntimes,
  2216. SmallVectorImpl<StringRef> &HelperStaticRuntimes) {
  2217. const SanitizerArgs &SanArgs = TC.getSanitizerArgs();
  2218. // Collect shared runtimes.
  2219. if (SanArgs.needsAsanRt() && SanArgs.needsSharedAsanRt()) {
  2220. SharedRuntimes.push_back("asan");
  2221. }
  2222. // Collect static runtimes.
  2223. if (Args.hasArg(options::OPT_shared) ||
  2224. (TC.getTriple().getEnvironment() == llvm::Triple::Android)) {
  2225. // Don't link static runtimes into DSOs or if compiling for Android.
  2226. return;
  2227. }
  2228. if (SanArgs.needsAsanRt()) {
  2229. if (SanArgs.needsSharedAsanRt()) {
  2230. HelperStaticRuntimes.push_back("asan-preinit");
  2231. } else {
  2232. StaticRuntimes.push_back("asan");
  2233. if (SanArgs.linkCXXRuntimes())
  2234. StaticRuntimes.push_back("asan_cxx");
  2235. }
  2236. }
  2237. if (SanArgs.needsDfsanRt())
  2238. StaticRuntimes.push_back("dfsan");
  2239. if (SanArgs.needsLsanRt())
  2240. StaticRuntimes.push_back("lsan");
  2241. if (SanArgs.needsMsanRt()) {
  2242. StaticRuntimes.push_back("msan");
  2243. if (SanArgs.linkCXXRuntimes())
  2244. StaticRuntimes.push_back("msan_cxx");
  2245. }
  2246. if (SanArgs.needsTsanRt()) {
  2247. StaticRuntimes.push_back("tsan");
  2248. if (SanArgs.linkCXXRuntimes())
  2249. StaticRuntimes.push_back("tsan_cxx");
  2250. }
  2251. if (SanArgs.needsUbsanRt()) {
  2252. StaticRuntimes.push_back("ubsan_standalone");
  2253. if (SanArgs.linkCXXRuntimes())
  2254. StaticRuntimes.push_back("ubsan_standalone_cxx");
  2255. }
  2256. if (SanArgs.needsSafeStackRt())
  2257. StaticRuntimes.push_back("safestack");
  2258. }
  2259. // Should be called before we add system libraries (C++ ABI, libstdc++/libc++,
  2260. // C runtime, etc). Returns true if sanitizer system deps need to be linked in.
  2261. static bool addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
  2262. ArgStringList &CmdArgs) {
  2263. SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes,
  2264. HelperStaticRuntimes;
  2265. collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes,
  2266. HelperStaticRuntimes);
  2267. for (auto RT : SharedRuntimes)
  2268. addSanitizerRuntime(TC, Args, CmdArgs, RT, true);
  2269. for (auto RT : HelperStaticRuntimes)
  2270. addSanitizerRuntime(TC, Args, CmdArgs, RT, false);
  2271. bool AddExportDynamic = false;
  2272. for (auto RT : StaticRuntimes) {
  2273. addSanitizerRuntime(TC, Args, CmdArgs, RT, false);
  2274. AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT);
  2275. }
  2276. // If there is a static runtime with no dynamic list, force all the symbols
  2277. // to be dynamic to be sure we export sanitizer interface functions.
  2278. if (AddExportDynamic)
  2279. CmdArgs.push_back("-export-dynamic");
  2280. return !StaticRuntimes.empty();
  2281. }
  2282. static bool areOptimizationsEnabled(const ArgList &Args) {
  2283. // Find the last -O arg and see if it is non-zero.
  2284. if (Arg *A = Args.getLastArg(options::OPT_O_Group))
  2285. return !A->getOption().matches(options::OPT_O0);
  2286. // Defaults to -O0.
  2287. return false;
  2288. }
  2289. static bool shouldUseFramePointerForTarget(const ArgList &Args,
  2290. const llvm::Triple &Triple) {
  2291. // XCore never wants frame pointers, regardless of OS.
  2292. if (Triple.getArch() == llvm::Triple::xcore) {
  2293. return false;
  2294. }
  2295. if (Triple.isOSLinux()) {
  2296. switch (Triple.getArch()) {
  2297. // Don't use a frame pointer on linux if optimizing for certain targets.
  2298. case llvm::Triple::mips64:
  2299. case llvm::Triple::mips64el:
  2300. case llvm::Triple::mips:
  2301. case llvm::Triple::mipsel:
  2302. case llvm::Triple::systemz:
  2303. case llvm::Triple::x86:
  2304. case llvm::Triple::x86_64:
  2305. return !areOptimizationsEnabled(Args);
  2306. default:
  2307. return true;
  2308. }
  2309. }
  2310. if (Triple.isOSWindows()) {
  2311. switch (Triple.getArch()) {
  2312. case llvm::Triple::x86:
  2313. return !areOptimizationsEnabled(Args);
  2314. default:
  2315. // All other supported Windows ISAs use xdata unwind information, so frame
  2316. // pointers are not generally useful.
  2317. return false;
  2318. }
  2319. }
  2320. return true;
  2321. }
  2322. static bool shouldUseFramePointer(const ArgList &Args,
  2323. const llvm::Triple &Triple) {
  2324. if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
  2325. options::OPT_fomit_frame_pointer))
  2326. return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
  2327. return shouldUseFramePointerForTarget(Args, Triple);
  2328. }
  2329. static bool shouldUseLeafFramePointer(const ArgList &Args,
  2330. const llvm::Triple &Triple) {
  2331. if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
  2332. options::OPT_momit_leaf_frame_pointer))
  2333. return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
  2334. if (Triple.isPS4CPU())
  2335. return false;
  2336. return shouldUseFramePointerForTarget(Args, Triple);
  2337. }
  2338. /// Add a CC1 option to specify the debug compilation directory.
  2339. static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
  2340. SmallString<128> cwd;
  2341. if (!llvm::sys::fs::current_path(cwd)) {
  2342. CmdArgs.push_back("-fdebug-compilation-dir");
  2343. CmdArgs.push_back(Args.MakeArgString(cwd));
  2344. }
  2345. }
  2346. static const char *SplitDebugName(const ArgList &Args, const InputInfo &Input) {
  2347. Arg *FinalOutput = Args.getLastArg(options::OPT_o);
  2348. if (FinalOutput && Args.hasArg(options::OPT_c)) {
  2349. SmallString<128> T(FinalOutput->getValue());
  2350. llvm::sys::path::replace_extension(T, "dwo");
  2351. return Args.MakeArgString(T);
  2352. } else {
  2353. // Use the compilation dir.
  2354. SmallString<128> T(
  2355. Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
  2356. SmallString<128> F(llvm::sys::path::stem(Input.getBaseInput()));
  2357. llvm::sys::path::replace_extension(F, "dwo");
  2358. T += F;
  2359. return Args.MakeArgString(F);
  2360. }
  2361. }
  2362. static void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
  2363. const JobAction &JA, const ArgList &Args,
  2364. const InputInfo &Output, const char *OutFile) {
  2365. ArgStringList ExtractArgs;
  2366. ExtractArgs.push_back("--extract-dwo");
  2367. ArgStringList StripArgs;
  2368. StripArgs.push_back("--strip-dwo");
  2369. // Grabbing the output of the earlier compile step.
  2370. StripArgs.push_back(Output.getFilename());
  2371. ExtractArgs.push_back(Output.getFilename());
  2372. ExtractArgs.push_back(OutFile);
  2373. const char *Exec = Args.MakeArgString(TC.GetProgramPath("objcopy"));
  2374. // First extract the dwo sections.
  2375. C.addCommand(llvm::make_unique<Command>(JA, T, Exec, ExtractArgs));
  2376. // Then remove them from the original .o file.
  2377. C.addCommand(llvm::make_unique<Command>(JA, T, Exec, StripArgs));
  2378. }
  2379. /// \brief Vectorize at all optimization levels greater than 1 except for -Oz.
  2380. /// For -Oz the loop vectorizer is disable, while the slp vectorizer is enabled.
  2381. static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
  2382. if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
  2383. if (A->getOption().matches(options::OPT_O4) ||
  2384. A->getOption().matches(options::OPT_Ofast))
  2385. return true;
  2386. if (A->getOption().matches(options::OPT_O0))
  2387. return false;
  2388. assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
  2389. // Vectorize -Os.
  2390. StringRef S(A->getValue());
  2391. if (S == "s")
  2392. return true;
  2393. // Don't vectorize -Oz, unless it's the slp vectorizer.
  2394. if (S == "z")
  2395. return isSlpVec;
  2396. unsigned OptLevel = 0;
  2397. if (S.getAsInteger(10, OptLevel))
  2398. return false;
  2399. return OptLevel > 1;
  2400. }
  2401. return false;
  2402. }
  2403. /// Add -x lang to \p CmdArgs for \p Input.
  2404. static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
  2405. ArgStringList &CmdArgs) {
  2406. // When using -verify-pch, we don't want to provide the type
  2407. // 'precompiled-header' if it was inferred from the file extension
  2408. if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
  2409. return;
  2410. CmdArgs.push_back("-x");
  2411. if (Args.hasArg(options::OPT_rewrite_objc))
  2412. CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
  2413. else
  2414. CmdArgs.push_back(types::getTypeName(Input.getType()));
  2415. }
  2416. static VersionTuple getMSCompatibilityVersion(unsigned Version) {
  2417. if (Version < 100)
  2418. return VersionTuple(Version);
  2419. if (Version < 10000)
  2420. return VersionTuple(Version / 100, Version % 100);
  2421. unsigned Build = 0, Factor = 1;
  2422. for (; Version > 10000; Version = Version / 10, Factor = Factor * 10)
  2423. Build = Build + (Version % 10) * Factor;
  2424. return VersionTuple(Version / 100, Version % 100, Build);
  2425. }
  2426. // Claim options we don't want to warn if they are unused. We do this for
  2427. // options that build systems might add but are unused when assembling or only
  2428. // running the preprocessor for example.
  2429. static void claimNoWarnArgs(const ArgList &Args) {
  2430. // Don't warn about unused -f(no-)?lto. This can happen when we're
  2431. // preprocessing, precompiling or assembling.
  2432. Args.ClaimAllArgs(options::OPT_flto);
  2433. Args.ClaimAllArgs(options::OPT_fno_lto);
  2434. }
  2435. static void appendUserToPath(SmallVectorImpl<char> &Result) {
  2436. #ifdef LLVM_ON_UNIX
  2437. const char *Username = getenv("LOGNAME");
  2438. #else
  2439. const char *Username = getenv("USERNAME");
  2440. #endif
  2441. if (Username) {
  2442. // Validate that LoginName can be used in a path, and get its length.
  2443. size_t Len = 0;
  2444. for (const char *P = Username; *P; ++P, ++Len) {
  2445. if (!isAlphanumeric(*P) && *P != '_') {
  2446. Username = nullptr;
  2447. break;
  2448. }
  2449. }
  2450. if (Username && Len > 0) {
  2451. Result.append(Username, Username + Len);
  2452. return;
  2453. }
  2454. }
  2455. // Fallback to user id.
  2456. #ifdef LLVM_ON_UNIX
  2457. std::string UID = llvm::utostr(getuid());
  2458. #else
  2459. // FIXME: Windows seems to have an 'SID' that might work.
  2460. std::string UID = "9999";
  2461. #endif
  2462. Result.append(UID.begin(), UID.end());
  2463. }
  2464. VersionTuple visualstudio::getMSVCVersion(const Driver *D,
  2465. const llvm::Triple &Triple,
  2466. const llvm::opt::ArgList &Args,
  2467. bool IsWindowsMSVC) {
  2468. if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
  2469. IsWindowsMSVC) ||
  2470. Args.hasArg(options::OPT_fmsc_version) ||
  2471. Args.hasArg(options::OPT_fms_compatibility_version)) {
  2472. const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
  2473. const Arg *MSCompatibilityVersion =
  2474. Args.getLastArg(options::OPT_fms_compatibility_version);
  2475. if (MSCVersion && MSCompatibilityVersion) {
  2476. if (D)
  2477. D->Diag(diag::err_drv_argument_not_allowed_with)
  2478. << MSCVersion->getAsString(Args)
  2479. << MSCompatibilityVersion->getAsString(Args);
  2480. return VersionTuple();
  2481. }
  2482. if (MSCompatibilityVersion) {
  2483. VersionTuple MSVT;
  2484. if (MSVT.tryParse(MSCompatibilityVersion->getValue()) && D)
  2485. D->Diag(diag::err_drv_invalid_value)
  2486. << MSCompatibilityVersion->getAsString(Args)
  2487. << MSCompatibilityVersion->getValue();
  2488. return MSVT;
  2489. }
  2490. if (MSCVersion) {
  2491. unsigned Version = 0;
  2492. if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version) && D)
  2493. D->Diag(diag::err_drv_invalid_value) << MSCVersion->getAsString(Args)
  2494. << MSCVersion->getValue();
  2495. return getMSCompatibilityVersion(Version);
  2496. }
  2497. unsigned Major, Minor, Micro;
  2498. Triple.getEnvironmentVersion(Major, Minor, Micro);
  2499. if (Major || Minor || Micro)
  2500. return VersionTuple(Major, Minor, Micro);
  2501. return VersionTuple(18);
  2502. }
  2503. return VersionTuple();
  2504. }
  2505. static void addPGOAndCoverageFlags(Compilation &C, const Driver &D,
  2506. const InputInfo &Output, const ArgList &Args,
  2507. ArgStringList &CmdArgs) {
  2508. auto *ProfileGenerateArg = Args.getLastArg(
  2509. options::OPT_fprofile_instr_generate,
  2510. options::OPT_fprofile_instr_generate_EQ, options::OPT_fprofile_generate,
  2511. options::OPT_fprofile_generate_EQ);
  2512. auto *ProfileUseArg = Args.getLastArg(
  2513. options::OPT_fprofile_instr_use, options::OPT_fprofile_instr_use_EQ,
  2514. options::OPT_fprofile_use, options::OPT_fprofile_use_EQ);
  2515. if (ProfileGenerateArg && ProfileUseArg)
  2516. D.Diag(diag::err_drv_argument_not_allowed_with)
  2517. << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
  2518. if (ProfileGenerateArg &&
  2519. ProfileGenerateArg->getOption().matches(
  2520. options::OPT_fprofile_instr_generate_EQ))
  2521. ProfileGenerateArg->render(Args, CmdArgs);
  2522. else if (ProfileGenerateArg &&
  2523. ProfileGenerateArg->getOption().matches(
  2524. options::OPT_fprofile_generate_EQ)) {
  2525. SmallString<128> Path(ProfileGenerateArg->getValue());
  2526. llvm::sys::path::append(Path, "default.profraw");
  2527. CmdArgs.push_back(
  2528. Args.MakeArgString(Twine("-fprofile-instr-generate=") + Path));
  2529. } else
  2530. Args.AddAllArgs(CmdArgs, options::OPT_fprofile_instr_generate);
  2531. if (ProfileUseArg &&
  2532. ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
  2533. ProfileUseArg->render(Args, CmdArgs);
  2534. else if (ProfileUseArg &&
  2535. (ProfileUseArg->getOption().matches(options::OPT_fprofile_use_EQ) ||
  2536. ProfileUseArg->getOption().matches(
  2537. options::OPT_fprofile_instr_use))) {
  2538. SmallString<128> Path(
  2539. ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
  2540. if (Path.empty() || llvm::sys::fs::is_directory(Path))
  2541. llvm::sys::path::append(Path, "default.profdata");
  2542. CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instr-use=") + Path));
  2543. }
  2544. if (Args.hasArg(options::OPT_ftest_coverage) ||
  2545. Args.hasArg(options::OPT_coverage))
  2546. CmdArgs.push_back("-femit-coverage-notes");
  2547. if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
  2548. false) ||
  2549. Args.hasArg(options::OPT_coverage))
  2550. CmdArgs.push_back("-femit-coverage-data");
  2551. if (Args.hasArg(options::OPT_fcoverage_mapping) && !ProfileGenerateArg)
  2552. D.Diag(diag::err_drv_argument_only_allowed_with)
  2553. << "-fcoverage-mapping"
  2554. << "-fprofile-instr-generate";
  2555. if (Args.hasArg(options::OPT_fcoverage_mapping))
  2556. CmdArgs.push_back("-fcoverage-mapping");
  2557. if (C.getArgs().hasArg(options::OPT_c) ||
  2558. C.getArgs().hasArg(options::OPT_S)) {
  2559. if (Output.isFilename()) {
  2560. CmdArgs.push_back("-coverage-file");
  2561. SmallString<128> CoverageFilename;
  2562. if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) {
  2563. CoverageFilename = FinalOutput->getValue();
  2564. } else {
  2565. CoverageFilename = llvm::sys::path::filename(Output.getBaseInput());
  2566. }
  2567. if (llvm::sys::path::is_relative(CoverageFilename)) {
  2568. SmallString<128> Pwd;
  2569. if (!llvm::sys::fs::current_path(Pwd)) {
  2570. llvm::sys::path::append(Pwd, CoverageFilename);
  2571. CoverageFilename.swap(Pwd);
  2572. }
  2573. }
  2574. CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
  2575. }
  2576. }
  2577. }
  2578. void Clang::ConstructJob(Compilation &C, const JobAction &JA,
  2579. const InputInfo &Output, const InputInfoList &Inputs,
  2580. const ArgList &Args, const char *LinkingOutput) const {
  2581. bool KernelOrKext =
  2582. Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
  2583. const Driver &D = getToolChain().getDriver();
  2584. ArgStringList CmdArgs;
  2585. bool IsWindowsGNU = getToolChain().getTriple().isWindowsGNUEnvironment();
  2586. bool IsWindowsCygnus =
  2587. getToolChain().getTriple().isWindowsCygwinEnvironment();
  2588. bool IsWindowsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment();
  2589. // Check number of inputs for sanity. We need at least one input.
  2590. assert(Inputs.size() >= 1 && "Must have at least one input.");
  2591. const InputInfo &Input = Inputs[0];
  2592. // CUDA compilation may have multiple inputs (source file + results of
  2593. // device-side compilations). All other jobs are expected to have exactly one
  2594. // input.
  2595. bool IsCuda = types::isCuda(Input.getType());
  2596. assert((IsCuda || Inputs.size() == 1) && "Unable to handle multiple inputs.");
  2597. // Invoke ourselves in -cc1 mode.
  2598. //
  2599. // FIXME: Implement custom jobs for internal actions.
  2600. CmdArgs.push_back("-cc1");
  2601. // Add the "effective" target triple.
  2602. CmdArgs.push_back("-triple");
  2603. std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  2604. CmdArgs.push_back(Args.MakeArgString(TripleStr));
  2605. const llvm::Triple TT(TripleStr);
  2606. if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
  2607. TT.getArch() == llvm::Triple::thumb)) {
  2608. unsigned Offset = TT.getArch() == llvm::Triple::arm ? 4 : 6;
  2609. unsigned Version;
  2610. TT.getArchName().substr(Offset).getAsInteger(10, Version);
  2611. if (Version < 7)
  2612. D.Diag(diag::err_target_unsupported_arch) << TT.getArchName()
  2613. << TripleStr;
  2614. }
  2615. // Push all default warning arguments that are specific to
  2616. // the given target. These come before user provided warning options
  2617. // are provided.
  2618. getToolChain().addClangWarningOptions(CmdArgs);
  2619. // Select the appropriate action.
  2620. RewriteKind rewriteKind = RK_None;
  2621. if (isa<AnalyzeJobAction>(JA)) {
  2622. assert(JA.getType() == types::TY_Plist && "Invalid output type.");
  2623. CmdArgs.push_back("-analyze");
  2624. } else if (isa<MigrateJobAction>(JA)) {
  2625. CmdArgs.push_back("-migrate");
  2626. } else if (isa<PreprocessJobAction>(JA)) {
  2627. if (Output.getType() == types::TY_Dependencies)
  2628. CmdArgs.push_back("-Eonly");
  2629. else {
  2630. CmdArgs.push_back("-E");
  2631. if (Args.hasArg(options::OPT_rewrite_objc) &&
  2632. !Args.hasArg(options::OPT_g_Group))
  2633. CmdArgs.push_back("-P");
  2634. }
  2635. } else if (isa<AssembleJobAction>(JA)) {
  2636. CmdArgs.push_back("-emit-obj");
  2637. CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
  2638. // Also ignore explicit -force_cpusubtype_ALL option.
  2639. (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
  2640. } else if (isa<PrecompileJobAction>(JA)) {
  2641. // Use PCH if the user requested it.
  2642. bool UsePCH = D.CCCUsePCH;
  2643. if (JA.getType() == types::TY_Nothing)
  2644. CmdArgs.push_back("-fsyntax-only");
  2645. else if (UsePCH)
  2646. CmdArgs.push_back("-emit-pch");
  2647. else
  2648. CmdArgs.push_back("-emit-pth");
  2649. } else if (isa<VerifyPCHJobAction>(JA)) {
  2650. CmdArgs.push_back("-verify-pch");
  2651. } else {
  2652. assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
  2653. "Invalid action for clang tool.");
  2654. if (JA.getType() == types::TY_LTO_IR || JA.getType() == types::TY_LTO_BC) {
  2655. CmdArgs.push_back("-flto");
  2656. }
  2657. if (JA.getType() == types::TY_Nothing) {
  2658. CmdArgs.push_back("-fsyntax-only");
  2659. } else if (JA.getType() == types::TY_LLVM_IR ||
  2660. JA.getType() == types::TY_LTO_IR) {
  2661. CmdArgs.push_back("-emit-llvm");
  2662. } else if (JA.getType() == types::TY_LLVM_BC ||
  2663. JA.getType() == types::TY_LTO_BC) {
  2664. CmdArgs.push_back("-emit-llvm-bc");
  2665. } else if (JA.getType() == types::TY_PP_Asm) {
  2666. CmdArgs.push_back("-S");
  2667. } else if (JA.getType() == types::TY_AST) {
  2668. CmdArgs.push_back("-emit-pch");
  2669. } else if (JA.getType() == types::TY_ModuleFile) {
  2670. CmdArgs.push_back("-module-file-info");
  2671. } else if (JA.getType() == types::TY_RewrittenObjC) {
  2672. CmdArgs.push_back("-rewrite-objc");
  2673. rewriteKind = RK_NonFragile;
  2674. } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
  2675. CmdArgs.push_back("-rewrite-objc");
  2676. rewriteKind = RK_Fragile;
  2677. } else {
  2678. assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
  2679. }
  2680. // Preserve use-list order by default when emitting bitcode, so that
  2681. // loading the bitcode up in 'opt' or 'llc' and running passes gives the
  2682. // same result as running passes here. For LTO, we don't need to preserve
  2683. // the use-list order, since serialization to bitcode is part of the flow.
  2684. if (JA.getType() == types::TY_LLVM_BC)
  2685. CmdArgs.push_back("-emit-llvm-uselists");
  2686. }
  2687. // We normally speed up the clang process a bit by skipping destructors at
  2688. // exit, but when we're generating diagnostics we can rely on some of the
  2689. // cleanup.
  2690. if (!C.isForDiagnostics())
  2691. CmdArgs.push_back("-disable-free");
  2692. // Disable the verification pass in -asserts builds.
  2693. #ifdef NDEBUG
  2694. CmdArgs.push_back("-disable-llvm-verifier");
  2695. #endif
  2696. // Set the main file name, so that debug info works even with
  2697. // -save-temps.
  2698. CmdArgs.push_back("-main-file-name");
  2699. CmdArgs.push_back(getBaseInputName(Args, Input));
  2700. // Some flags which affect the language (via preprocessor
  2701. // defines).
  2702. if (Args.hasArg(options::OPT_static))
  2703. CmdArgs.push_back("-static-define");
  2704. if (isa<AnalyzeJobAction>(JA)) {
  2705. // Enable region store model by default.
  2706. CmdArgs.push_back("-analyzer-store=region");
  2707. // Treat blocks as analysis entry points.
  2708. CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
  2709. CmdArgs.push_back("-analyzer-eagerly-assume");
  2710. // Add default argument set.
  2711. if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
  2712. CmdArgs.push_back("-analyzer-checker=core");
  2713. if (!IsWindowsMSVC)
  2714. CmdArgs.push_back("-analyzer-checker=unix");
  2715. if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
  2716. CmdArgs.push_back("-analyzer-checker=osx");
  2717. CmdArgs.push_back("-analyzer-checker=deadcode");
  2718. if (types::isCXX(Input.getType()))
  2719. CmdArgs.push_back("-analyzer-checker=cplusplus");
  2720. // Enable the following experimental checkers for testing.
  2721. CmdArgs.push_back(
  2722. "-analyzer-checker=security.insecureAPI.UncheckedReturn");
  2723. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
  2724. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
  2725. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
  2726. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
  2727. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
  2728. }
  2729. // Set the output format. The default is plist, for (lame) historical
  2730. // reasons.
  2731. CmdArgs.push_back("-analyzer-output");
  2732. if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
  2733. CmdArgs.push_back(A->getValue());
  2734. else
  2735. CmdArgs.push_back("plist");
  2736. // Disable the presentation of standard compiler warnings when
  2737. // using --analyze. We only want to show static analyzer diagnostics
  2738. // or frontend errors.
  2739. CmdArgs.push_back("-w");
  2740. // Add -Xanalyzer arguments when running as analyzer.
  2741. Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
  2742. }
  2743. CheckCodeGenerationOptions(D, Args);
  2744. bool PIE = getToolChain().isPIEDefault();
  2745. bool PIC = PIE || getToolChain().isPICDefault();
  2746. bool IsPICLevelTwo = PIC;
  2747. // Android-specific defaults for PIC/PIE
  2748. if (getToolChain().getTriple().getEnvironment() == llvm::Triple::Android) {
  2749. switch (getToolChain().getArch()) {
  2750. case llvm::Triple::arm:
  2751. case llvm::Triple::armeb:
  2752. case llvm::Triple::thumb:
  2753. case llvm::Triple::thumbeb:
  2754. case llvm::Triple::aarch64:
  2755. case llvm::Triple::mips:
  2756. case llvm::Triple::mipsel:
  2757. case llvm::Triple::mips64:
  2758. case llvm::Triple::mips64el:
  2759. PIC = true; // "-fpic"
  2760. break;
  2761. case llvm::Triple::x86:
  2762. case llvm::Triple::x86_64:
  2763. PIC = true; // "-fPIC"
  2764. IsPICLevelTwo = true;
  2765. break;
  2766. default:
  2767. break;
  2768. }
  2769. }
  2770. // OpenBSD-specific defaults for PIE
  2771. if (getToolChain().getTriple().getOS() == llvm::Triple::OpenBSD) {
  2772. switch (getToolChain().getArch()) {
  2773. case llvm::Triple::mips64:
  2774. case llvm::Triple::mips64el:
  2775. case llvm::Triple::sparcel:
  2776. case llvm::Triple::x86:
  2777. case llvm::Triple::x86_64:
  2778. IsPICLevelTwo = false; // "-fpie"
  2779. break;
  2780. case llvm::Triple::ppc:
  2781. case llvm::Triple::sparc:
  2782. case llvm::Triple::sparcv9:
  2783. IsPICLevelTwo = true; // "-fPIE"
  2784. break;
  2785. default:
  2786. break;
  2787. }
  2788. }
  2789. // For the PIC and PIE flag options, this logic is different from the
  2790. // legacy logic in very old versions of GCC, as that logic was just
  2791. // a bug no one had ever fixed. This logic is both more rational and
  2792. // consistent with GCC's new logic now that the bugs are fixed. The last
  2793. // argument relating to either PIC or PIE wins, and no other argument is
  2794. // used. If the last argument is any flavor of the '-fno-...' arguments,
  2795. // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
  2796. // at the same level.
  2797. Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
  2798. options::OPT_fpic, options::OPT_fno_pic,
  2799. options::OPT_fPIE, options::OPT_fno_PIE,
  2800. options::OPT_fpie, options::OPT_fno_pie);
  2801. // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
  2802. // is forced, then neither PIC nor PIE flags will have no effect.
  2803. if (!getToolChain().isPICDefaultForced()) {
  2804. if (LastPICArg) {
  2805. Option O = LastPICArg->getOption();
  2806. if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
  2807. O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
  2808. PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
  2809. PIC =
  2810. PIE || O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic);
  2811. IsPICLevelTwo =
  2812. O.matches(options::OPT_fPIE) || O.matches(options::OPT_fPIC);
  2813. } else {
  2814. PIE = PIC = false;
  2815. }
  2816. }
  2817. }
  2818. // Introduce a Darwin-specific hack. If the default is PIC but the flags
  2819. // specified while enabling PIC enabled level 1 PIC, just force it back to
  2820. // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
  2821. // informal testing).
  2822. if (PIC && getToolChain().getTriple().isOSDarwin())
  2823. IsPICLevelTwo |= getToolChain().isPICDefault();
  2824. // Note that these flags are trump-cards. Regardless of the order w.r.t. the
  2825. // PIC or PIE options above, if these show up, PIC is disabled.
  2826. llvm::Triple Triple(TripleStr);
  2827. if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6)))
  2828. PIC = PIE = false;
  2829. if (Args.hasArg(options::OPT_static))
  2830. PIC = PIE = false;
  2831. if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
  2832. // This is a very special mode. It trumps the other modes, almost no one
  2833. // uses it, and it isn't even valid on any OS but Darwin.
  2834. if (!getToolChain().getTriple().isOSDarwin())
  2835. D.Diag(diag::err_drv_unsupported_opt_for_target)
  2836. << A->getSpelling() << getToolChain().getTriple().str();
  2837. // FIXME: Warn when this flag trumps some other PIC or PIE flag.
  2838. CmdArgs.push_back("-mrelocation-model");
  2839. CmdArgs.push_back("dynamic-no-pic");
  2840. // Only a forced PIC mode can cause the actual compile to have PIC defines
  2841. // etc., no flags are sufficient. This behavior was selected to closely
  2842. // match that of llvm-gcc and Apple GCC before that.
  2843. if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
  2844. CmdArgs.push_back("-pic-level");
  2845. CmdArgs.push_back("2");
  2846. }
  2847. } else {
  2848. // Currently, LLVM only knows about PIC vs. static; the PIE differences are
  2849. // handled in Clang's IRGen by the -pie-level flag.
  2850. CmdArgs.push_back("-mrelocation-model");
  2851. CmdArgs.push_back(PIC ? "pic" : "static");
  2852. if (PIC) {
  2853. CmdArgs.push_back("-pic-level");
  2854. CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
  2855. if (PIE) {
  2856. CmdArgs.push_back("-pie-level");
  2857. CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
  2858. }
  2859. }
  2860. }
  2861. CmdArgs.push_back("-mthread-model");
  2862. if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
  2863. CmdArgs.push_back(A->getValue());
  2864. else
  2865. CmdArgs.push_back(Args.MakeArgString(getToolChain().getThreadModel()));
  2866. Args.AddLastArg(CmdArgs, options::OPT_fveclib);
  2867. if (!Args.hasFlag(options::OPT_fmerge_all_constants,
  2868. options::OPT_fno_merge_all_constants))
  2869. CmdArgs.push_back("-fno-merge-all-constants");
  2870. // LLVM Code Generator Options.
  2871. if (Args.hasArg(options::OPT_frewrite_map_file) ||
  2872. Args.hasArg(options::OPT_frewrite_map_file_EQ)) {
  2873. for (const Arg *A : Args.filtered(options::OPT_frewrite_map_file,
  2874. options::OPT_frewrite_map_file_EQ)) {
  2875. CmdArgs.push_back("-frewrite-map-file");
  2876. CmdArgs.push_back(A->getValue());
  2877. A->claim();
  2878. }
  2879. }
  2880. if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
  2881. StringRef v = A->getValue();
  2882. CmdArgs.push_back("-mllvm");
  2883. CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
  2884. A->claim();
  2885. }
  2886. if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
  2887. CmdArgs.push_back("-mregparm");
  2888. CmdArgs.push_back(A->getValue());
  2889. }
  2890. if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
  2891. options::OPT_freg_struct_return)) {
  2892. if (getToolChain().getArch() != llvm::Triple::x86) {
  2893. D.Diag(diag::err_drv_unsupported_opt_for_target)
  2894. << A->getSpelling() << getToolChain().getTriple().str();
  2895. } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
  2896. CmdArgs.push_back("-fpcc-struct-return");
  2897. } else {
  2898. assert(A->getOption().matches(options::OPT_freg_struct_return));
  2899. CmdArgs.push_back("-freg-struct-return");
  2900. }
  2901. }
  2902. if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
  2903. CmdArgs.push_back("-mrtd");
  2904. if (shouldUseFramePointer(Args, getToolChain().getTriple()))
  2905. CmdArgs.push_back("-mdisable-fp-elim");
  2906. if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
  2907. options::OPT_fno_zero_initialized_in_bss))
  2908. CmdArgs.push_back("-mno-zero-initialized-in-bss");
  2909. bool OFastEnabled = isOptimizationLevelFast(Args);
  2910. // If -Ofast is the optimization level, then -fstrict-aliasing should be
  2911. // enabled. This alias option is being used to simplify the hasFlag logic.
  2912. OptSpecifier StrictAliasingAliasOption =
  2913. OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing;
  2914. // We turn strict aliasing off by default if we're in CL mode, since MSVC
  2915. // doesn't do any TBAA.
  2916. bool TBAAOnByDefault = !getToolChain().getDriver().IsCLMode();
  2917. if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
  2918. options::OPT_fno_strict_aliasing, TBAAOnByDefault))
  2919. CmdArgs.push_back("-relaxed-aliasing");
  2920. if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
  2921. options::OPT_fno_struct_path_tbaa))
  2922. CmdArgs.push_back("-no-struct-path-tbaa");
  2923. if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
  2924. false))
  2925. CmdArgs.push_back("-fstrict-enums");
  2926. if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
  2927. options::OPT_fno_optimize_sibling_calls))
  2928. CmdArgs.push_back("-mdisable-tail-calls");
  2929. // Handle segmented stacks.
  2930. if (Args.hasArg(options::OPT_fsplit_stack))
  2931. CmdArgs.push_back("-split-stacks");
  2932. // If -Ofast is the optimization level, then -ffast-math should be enabled.
  2933. // This alias option is being used to simplify the getLastArg logic.
  2934. OptSpecifier FastMathAliasOption =
  2935. OFastEnabled ? options::OPT_Ofast : options::OPT_ffast_math;
  2936. // Handle various floating point optimization flags, mapping them to the
  2937. // appropriate LLVM code generation flags. The pattern for all of these is to
  2938. // default off the codegen optimizations, and if any flag enables them and no
  2939. // flag disables them after the flag enabling them, enable the codegen
  2940. // optimization. This is complicated by several "umbrella" flags.
  2941. if (Arg *A = Args.getLastArg(
  2942. options::OPT_ffast_math, FastMathAliasOption,
  2943. options::OPT_fno_fast_math, options::OPT_ffinite_math_only,
  2944. options::OPT_fno_finite_math_only, options::OPT_fhonor_infinities,
  2945. options::OPT_fno_honor_infinities))
  2946. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2947. A->getOption().getID() != options::OPT_fno_finite_math_only &&
  2948. A->getOption().getID() != options::OPT_fhonor_infinities)
  2949. CmdArgs.push_back("-menable-no-infs");
  2950. if (Arg *A = Args.getLastArg(
  2951. options::OPT_ffast_math, FastMathAliasOption,
  2952. options::OPT_fno_fast_math, options::OPT_ffinite_math_only,
  2953. options::OPT_fno_finite_math_only, options::OPT_fhonor_nans,
  2954. options::OPT_fno_honor_nans))
  2955. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2956. A->getOption().getID() != options::OPT_fno_finite_math_only &&
  2957. A->getOption().getID() != options::OPT_fhonor_nans)
  2958. CmdArgs.push_back("-menable-no-nans");
  2959. // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
  2960. bool MathErrno = getToolChain().IsMathErrnoDefault();
  2961. if (Arg *A =
  2962. Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2963. options::OPT_fno_fast_math, options::OPT_fmath_errno,
  2964. options::OPT_fno_math_errno)) {
  2965. // Turning on -ffast_math (with either flag) removes the need for MathErrno.
  2966. // However, turning *off* -ffast_math merely restores the toolchain default
  2967. // (which may be false).
  2968. if (A->getOption().getID() == options::OPT_fno_math_errno ||
  2969. A->getOption().getID() == options::OPT_ffast_math ||
  2970. A->getOption().getID() == options::OPT_Ofast)
  2971. MathErrno = false;
  2972. else if (A->getOption().getID() == options::OPT_fmath_errno)
  2973. MathErrno = true;
  2974. }
  2975. if (MathErrno)
  2976. CmdArgs.push_back("-fmath-errno");
  2977. // There are several flags which require disabling very specific
  2978. // optimizations. Any of these being disabled forces us to turn off the
  2979. // entire set of LLVM optimizations, so collect them through all the flag
  2980. // madness.
  2981. bool AssociativeMath = false;
  2982. if (Arg *A = Args.getLastArg(
  2983. options::OPT_ffast_math, FastMathAliasOption,
  2984. options::OPT_fno_fast_math, options::OPT_funsafe_math_optimizations,
  2985. options::OPT_fno_unsafe_math_optimizations,
  2986. options::OPT_fassociative_math, options::OPT_fno_associative_math))
  2987. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2988. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  2989. A->getOption().getID() != options::OPT_fno_associative_math)
  2990. AssociativeMath = true;
  2991. bool ReciprocalMath = false;
  2992. if (Arg *A = Args.getLastArg(
  2993. options::OPT_ffast_math, FastMathAliasOption,
  2994. options::OPT_fno_fast_math, options::OPT_funsafe_math_optimizations,
  2995. options::OPT_fno_unsafe_math_optimizations,
  2996. options::OPT_freciprocal_math, options::OPT_fno_reciprocal_math))
  2997. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2998. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  2999. A->getOption().getID() != options::OPT_fno_reciprocal_math)
  3000. ReciprocalMath = true;
  3001. bool SignedZeros = true;
  3002. if (Arg *A = Args.getLastArg(
  3003. options::OPT_ffast_math, FastMathAliasOption,
  3004. options::OPT_fno_fast_math, options::OPT_funsafe_math_optimizations,
  3005. options::OPT_fno_unsafe_math_optimizations,
  3006. options::OPT_fsigned_zeros, options::OPT_fno_signed_zeros))
  3007. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  3008. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  3009. A->getOption().getID() != options::OPT_fsigned_zeros)
  3010. SignedZeros = false;
  3011. bool TrappingMath = true;
  3012. if (Arg *A = Args.getLastArg(
  3013. options::OPT_ffast_math, FastMathAliasOption,
  3014. options::OPT_fno_fast_math, options::OPT_funsafe_math_optimizations,
  3015. options::OPT_fno_unsafe_math_optimizations,
  3016. options::OPT_ftrapping_math, options::OPT_fno_trapping_math))
  3017. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  3018. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  3019. A->getOption().getID() != options::OPT_ftrapping_math)
  3020. TrappingMath = false;
  3021. if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
  3022. !TrappingMath)
  3023. CmdArgs.push_back("-menable-unsafe-fp-math");
  3024. if (!SignedZeros)
  3025. CmdArgs.push_back("-fno-signed-zeros");
  3026. if (ReciprocalMath)
  3027. CmdArgs.push_back("-freciprocal-math");
  3028. // Validate and pass through -fp-contract option.
  3029. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  3030. options::OPT_fno_fast_math,
  3031. options::OPT_ffp_contract)) {
  3032. if (A->getOption().getID() == options::OPT_ffp_contract) {
  3033. StringRef Val = A->getValue();
  3034. if (Val == "fast" || Val == "on" || Val == "off") {
  3035. CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
  3036. } else {
  3037. D.Diag(diag::err_drv_unsupported_option_argument)
  3038. << A->getOption().getName() << Val;
  3039. }
  3040. } else if (A->getOption().matches(options::OPT_ffast_math) ||
  3041. (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) {
  3042. // If fast-math is set then set the fp-contract mode to fast.
  3043. CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
  3044. }
  3045. }
  3046. ParseMRecip(getToolChain().getDriver(), Args, CmdArgs);
  3047. // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
  3048. // and if we find them, tell the frontend to provide the appropriate
  3049. // preprocessor macros. This is distinct from enabling any optimizations as
  3050. // these options induce language changes which must survive serialization
  3051. // and deserialization, etc.
  3052. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  3053. options::OPT_fno_fast_math))
  3054. if (!A->getOption().matches(options::OPT_fno_fast_math))
  3055. CmdArgs.push_back("-ffast-math");
  3056. if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only,
  3057. options::OPT_fno_fast_math))
  3058. if (A->getOption().matches(options::OPT_ffinite_math_only))
  3059. CmdArgs.push_back("-ffinite-math-only");
  3060. // Decide whether to use verbose asm. Verbose assembly is the default on
  3061. // toolchains which have the integrated assembler on by default.
  3062. bool IsIntegratedAssemblerDefault =
  3063. getToolChain().IsIntegratedAssemblerDefault();
  3064. if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
  3065. IsIntegratedAssemblerDefault) ||
  3066. Args.hasArg(options::OPT_dA))
  3067. CmdArgs.push_back("-masm-verbose");
  3068. if (!Args.hasFlag(options::OPT_fintegrated_as, options::OPT_fno_integrated_as,
  3069. IsIntegratedAssemblerDefault))
  3070. CmdArgs.push_back("-no-integrated-as");
  3071. if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
  3072. CmdArgs.push_back("-mdebug-pass");
  3073. CmdArgs.push_back("Structure");
  3074. }
  3075. if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
  3076. CmdArgs.push_back("-mdebug-pass");
  3077. CmdArgs.push_back("Arguments");
  3078. }
  3079. // Enable -mconstructor-aliases except on darwin, where we have to
  3080. // work around a linker bug; see <rdar://problem/7651567>.
  3081. if (!getToolChain().getTriple().isOSDarwin())
  3082. CmdArgs.push_back("-mconstructor-aliases");
  3083. // Darwin's kernel doesn't support guard variables; just die if we
  3084. // try to use them.
  3085. if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
  3086. CmdArgs.push_back("-fforbid-guard-variables");
  3087. if (Args.hasArg(options::OPT_mms_bitfields)) {
  3088. CmdArgs.push_back("-mms-bitfields");
  3089. }
  3090. // This is a coarse approximation of what llvm-gcc actually does, both
  3091. // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
  3092. // complicated ways.
  3093. bool AsynchronousUnwindTables =
  3094. Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
  3095. options::OPT_fno_asynchronous_unwind_tables,
  3096. (getToolChain().IsUnwindTablesDefault() ||
  3097. getToolChain().getSanitizerArgs().needsUnwindTables()) &&
  3098. !KernelOrKext);
  3099. if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
  3100. AsynchronousUnwindTables))
  3101. CmdArgs.push_back("-munwind-tables");
  3102. getToolChain().addClangTargetOptions(Args, CmdArgs);
  3103. if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
  3104. CmdArgs.push_back("-mlimit-float-precision");
  3105. CmdArgs.push_back(A->getValue());
  3106. }
  3107. // FIXME: Handle -mtune=.
  3108. (void)Args.hasArg(options::OPT_mtune_EQ);
  3109. if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
  3110. CmdArgs.push_back("-mcode-model");
  3111. CmdArgs.push_back(A->getValue());
  3112. }
  3113. // Add the target cpu
  3114. std::string CPU = getCPUName(Args, Triple, /*FromAs*/ false);
  3115. if (!CPU.empty()) {
  3116. CmdArgs.push_back("-target-cpu");
  3117. CmdArgs.push_back(Args.MakeArgString(CPU));
  3118. }
  3119. if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
  3120. CmdArgs.push_back("-mfpmath");
  3121. CmdArgs.push_back(A->getValue());
  3122. }
  3123. // Add the target features
  3124. getTargetFeatures(D, Triple, Args, CmdArgs, false);
  3125. // Add target specific flags.
  3126. switch (getToolChain().getArch()) {
  3127. default:
  3128. break;
  3129. case llvm::Triple::arm:
  3130. case llvm::Triple::armeb:
  3131. case llvm::Triple::thumb:
  3132. case llvm::Triple::thumbeb:
  3133. AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
  3134. break;
  3135. case llvm::Triple::aarch64:
  3136. case llvm::Triple::aarch64_be:
  3137. AddAArch64TargetArgs(Args, CmdArgs);
  3138. break;
  3139. case llvm::Triple::mips:
  3140. case llvm::Triple::mipsel:
  3141. case llvm::Triple::mips64:
  3142. case llvm::Triple::mips64el:
  3143. AddMIPSTargetArgs(Args, CmdArgs);
  3144. break;
  3145. case llvm::Triple::ppc:
  3146. case llvm::Triple::ppc64:
  3147. case llvm::Triple::ppc64le:
  3148. AddPPCTargetArgs(Args, CmdArgs);
  3149. break;
  3150. case llvm::Triple::sparc:
  3151. case llvm::Triple::sparcel:
  3152. case llvm::Triple::sparcv9:
  3153. AddSparcTargetArgs(Args, CmdArgs);
  3154. break;
  3155. case llvm::Triple::x86:
  3156. case llvm::Triple::x86_64:
  3157. AddX86TargetArgs(Args, CmdArgs);
  3158. break;
  3159. case llvm::Triple::hexagon:
  3160. AddHexagonTargetArgs(Args, CmdArgs);
  3161. break;
  3162. }
  3163. // Add clang-cl arguments.
  3164. if (getToolChain().getDriver().IsCLMode())
  3165. AddClangCLArgs(Args, CmdArgs);
  3166. // Pass the linker version in use.
  3167. if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
  3168. CmdArgs.push_back("-target-linker-version");
  3169. CmdArgs.push_back(A->getValue());
  3170. }
  3171. if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
  3172. CmdArgs.push_back("-momit-leaf-frame-pointer");
  3173. // Explicitly error on some things we know we don't support and can't just
  3174. // ignore.
  3175. types::ID InputType = Input.getType();
  3176. if (!Args.hasArg(options::OPT_fallow_unsupported)) {
  3177. Arg *Unsupported;
  3178. if (types::isCXX(InputType) && getToolChain().getTriple().isOSDarwin() &&
  3179. getToolChain().getArch() == llvm::Triple::x86) {
  3180. if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
  3181. (Unsupported = Args.getLastArg(options::OPT_mkernel)))
  3182. D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
  3183. << Unsupported->getOption().getName();
  3184. }
  3185. }
  3186. Args.AddAllArgs(CmdArgs, options::OPT_v);
  3187. Args.AddLastArg(CmdArgs, options::OPT_H);
  3188. if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
  3189. CmdArgs.push_back("-header-include-file");
  3190. CmdArgs.push_back(D.CCPrintHeadersFilename ? D.CCPrintHeadersFilename
  3191. : "-");
  3192. }
  3193. Args.AddLastArg(CmdArgs, options::OPT_P);
  3194. Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
  3195. if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
  3196. CmdArgs.push_back("-diagnostic-log-file");
  3197. CmdArgs.push_back(D.CCLogDiagnosticsFilename ? D.CCLogDiagnosticsFilename
  3198. : "-");
  3199. }
  3200. // Use the last option from "-g" group. "-gline-tables-only" and "-gdwarf-x"
  3201. // are preserved, all other debug options are substituted with "-g".
  3202. Args.ClaimAllArgs(options::OPT_g_Group);
  3203. if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
  3204. if (A->getOption().matches(options::OPT_gline_tables_only) ||
  3205. A->getOption().matches(options::OPT_g1)) {
  3206. // FIXME: we should support specifying dwarf version with
  3207. // -gline-tables-only.
  3208. CmdArgs.push_back("-gline-tables-only");
  3209. // Default is dwarf-2 for Darwin, OpenBSD, FreeBSD and Solaris.
  3210. const llvm::Triple &Triple = getToolChain().getTriple();
  3211. if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::OpenBSD ||
  3212. Triple.getOS() == llvm::Triple::FreeBSD ||
  3213. Triple.getOS() == llvm::Triple::Solaris)
  3214. CmdArgs.push_back("-gdwarf-2");
  3215. } else if (A->getOption().matches(options::OPT_gdwarf_2))
  3216. CmdArgs.push_back("-gdwarf-2");
  3217. else if (A->getOption().matches(options::OPT_gdwarf_3))
  3218. CmdArgs.push_back("-gdwarf-3");
  3219. else if (A->getOption().matches(options::OPT_gdwarf_4))
  3220. CmdArgs.push_back("-gdwarf-4");
  3221. else if (!A->getOption().matches(options::OPT_g0) &&
  3222. !A->getOption().matches(options::OPT_ggdb0)) {
  3223. // Default is dwarf-2 for Darwin, OpenBSD, FreeBSD and Solaris.
  3224. const llvm::Triple &Triple = getToolChain().getTriple();
  3225. if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::OpenBSD ||
  3226. Triple.getOS() == llvm::Triple::FreeBSD ||
  3227. Triple.getOS() == llvm::Triple::Solaris)
  3228. CmdArgs.push_back("-gdwarf-2");
  3229. else
  3230. CmdArgs.push_back("-g");
  3231. }
  3232. }
  3233. // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
  3234. Args.ClaimAllArgs(options::OPT_g_flags_Group);
  3235. if (Args.hasFlag(options::OPT_gcolumn_info, options::OPT_gno_column_info,
  3236. /*Default*/ true))
  3237. CmdArgs.push_back("-dwarf-column-info");
  3238. // FIXME: Move backend command line options to the module.
  3239. // -gsplit-dwarf should turn on -g and enable the backend dwarf
  3240. // splitting and extraction.
  3241. // FIXME: Currently only works on Linux.
  3242. if (getToolChain().getTriple().isOSLinux() &&
  3243. Args.hasArg(options::OPT_gsplit_dwarf)) {
  3244. CmdArgs.push_back("-g");
  3245. CmdArgs.push_back("-backend-option");
  3246. CmdArgs.push_back("-split-dwarf=Enable");
  3247. }
  3248. // -ggnu-pubnames turns on gnu style pubnames in the backend.
  3249. if (Args.hasArg(options::OPT_ggnu_pubnames)) {
  3250. CmdArgs.push_back("-backend-option");
  3251. CmdArgs.push_back("-generate-gnu-dwarf-pub-sections");
  3252. }
  3253. // -gdwarf-aranges turns on the emission of the aranges section in the
  3254. // backend.
  3255. if (Args.hasArg(options::OPT_gdwarf_aranges)) {
  3256. CmdArgs.push_back("-backend-option");
  3257. CmdArgs.push_back("-generate-arange-section");
  3258. }
  3259. if (Args.hasFlag(options::OPT_fdebug_types_section,
  3260. options::OPT_fno_debug_types_section, false)) {
  3261. CmdArgs.push_back("-backend-option");
  3262. CmdArgs.push_back("-generate-type-units");
  3263. }
  3264. // CloudABI uses -ffunction-sections and -fdata-sections by default.
  3265. bool UseSeparateSections = Triple.getOS() == llvm::Triple::CloudABI;
  3266. if (Args.hasFlag(options::OPT_ffunction_sections,
  3267. options::OPT_fno_function_sections, UseSeparateSections)) {
  3268. CmdArgs.push_back("-ffunction-sections");
  3269. }
  3270. if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
  3271. UseSeparateSections)) {
  3272. CmdArgs.push_back("-fdata-sections");
  3273. }
  3274. if (!Args.hasFlag(options::OPT_funique_section_names,
  3275. options::OPT_fno_unique_section_names, true))
  3276. CmdArgs.push_back("-fno-unique-section-names");
  3277. Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
  3278. addPGOAndCoverageFlags(C, D, Output, Args, CmdArgs);
  3279. // Pass options for controlling the default header search paths.
  3280. if (Args.hasArg(options::OPT_nostdinc)) {
  3281. CmdArgs.push_back("-nostdsysteminc");
  3282. CmdArgs.push_back("-nobuiltininc");
  3283. } else {
  3284. if (Args.hasArg(options::OPT_nostdlibinc))
  3285. CmdArgs.push_back("-nostdsysteminc");
  3286. Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
  3287. Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
  3288. }
  3289. // Pass the path to compiler resource files.
  3290. CmdArgs.push_back("-resource-dir");
  3291. CmdArgs.push_back(D.ResourceDir.c_str());
  3292. Args.AddLastArg(CmdArgs, options::OPT_working_directory);
  3293. bool ARCMTEnabled = false;
  3294. if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
  3295. if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
  3296. options::OPT_ccc_arcmt_modify,
  3297. options::OPT_ccc_arcmt_migrate)) {
  3298. ARCMTEnabled = true;
  3299. switch (A->getOption().getID()) {
  3300. default:
  3301. llvm_unreachable("missed a case");
  3302. case options::OPT_ccc_arcmt_check:
  3303. CmdArgs.push_back("-arcmt-check");
  3304. break;
  3305. case options::OPT_ccc_arcmt_modify:
  3306. CmdArgs.push_back("-arcmt-modify");
  3307. break;
  3308. case options::OPT_ccc_arcmt_migrate:
  3309. CmdArgs.push_back("-arcmt-migrate");
  3310. CmdArgs.push_back("-mt-migrate-directory");
  3311. CmdArgs.push_back(A->getValue());
  3312. Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
  3313. Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
  3314. break;
  3315. }
  3316. }
  3317. } else {
  3318. Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
  3319. Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
  3320. Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
  3321. }
  3322. if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
  3323. if (ARCMTEnabled) {
  3324. D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
  3325. << "-ccc-arcmt-migrate";
  3326. }
  3327. CmdArgs.push_back("-mt-migrate-directory");
  3328. CmdArgs.push_back(A->getValue());
  3329. if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
  3330. options::OPT_objcmt_migrate_subscripting,
  3331. options::OPT_objcmt_migrate_property)) {
  3332. // None specified, means enable them all.
  3333. CmdArgs.push_back("-objcmt-migrate-literals");
  3334. CmdArgs.push_back("-objcmt-migrate-subscripting");
  3335. CmdArgs.push_back("-objcmt-migrate-property");
  3336. } else {
  3337. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
  3338. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
  3339. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
  3340. }
  3341. } else {
  3342. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
  3343. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
  3344. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
  3345. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
  3346. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
  3347. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
  3348. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property_dot_syntax);
  3349. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
  3350. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
  3351. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
  3352. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
  3353. Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
  3354. Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
  3355. Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
  3356. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
  3357. Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
  3358. }
  3359. // Add preprocessing options like -I, -D, etc. if we are using the
  3360. // preprocessor.
  3361. //
  3362. // FIXME: Support -fpreprocessed
  3363. if (types::getPreprocessedType(InputType) != types::TY_INVALID)
  3364. AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
  3365. // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
  3366. // that "The compiler can only warn and ignore the option if not recognized".
  3367. // When building with ccache, it will pass -D options to clang even on
  3368. // preprocessed inputs and configure concludes that -fPIC is not supported.
  3369. Args.ClaimAllArgs(options::OPT_D);
  3370. Args.AddLastArg(CmdArgs, options::OPT_hlsl_version); // HLSL Change - add the HLSL version argument
  3371. // Manually translate -O4 to -O3; let clang reject others.
  3372. if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
  3373. if (A->getOption().matches(options::OPT_O4)) {
  3374. CmdArgs.push_back("-O3");
  3375. D.Diag(diag::warn_O4_is_O3);
  3376. } else {
  3377. A->render(Args, CmdArgs);
  3378. }
  3379. }
  3380. // Warn about ignored options to clang.
  3381. for (const Arg *A :
  3382. Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
  3383. D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
  3384. }
  3385. claimNoWarnArgs(Args);
  3386. Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
  3387. Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
  3388. if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
  3389. CmdArgs.push_back("-pedantic");
  3390. Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
  3391. Args.AddLastArg(CmdArgs, options::OPT_w);
  3392. // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
  3393. // (-ansi is equivalent to -std=c89 or -std=c++98).
  3394. //
  3395. // If a std is supplied, only add -trigraphs if it follows the
  3396. // option.
  3397. bool ImplyVCPPCXXVer = false;
  3398. if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
  3399. if (Std->getOption().matches(options::OPT_ansi))
  3400. if (types::isCXX(InputType))
  3401. CmdArgs.push_back("-std=c++98");
  3402. else
  3403. CmdArgs.push_back("-std=c89");
  3404. else
  3405. Std->render(Args, CmdArgs);
  3406. // If -f(no-)trigraphs appears after the language standard flag, honor it.
  3407. if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
  3408. options::OPT_ftrigraphs,
  3409. options::OPT_fno_trigraphs))
  3410. if (A != Std)
  3411. A->render(Args, CmdArgs);
  3412. } else {
  3413. // Honor -std-default.
  3414. //
  3415. // FIXME: Clang doesn't correctly handle -std= when the input language
  3416. // doesn't match. For the time being just ignore this for C++ inputs;
  3417. // eventually we want to do all the standard defaulting here instead of
  3418. // splitting it between the driver and clang -cc1.
  3419. if (!types::isCXX(InputType))
  3420. Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, "-std=",
  3421. /*Joined=*/true);
  3422. else if (IsWindowsMSVC)
  3423. ImplyVCPPCXXVer = true;
  3424. Args.AddLastArg(CmdArgs, options::OPT_ftrigraphs,
  3425. options::OPT_fno_trigraphs);
  3426. }
  3427. // GCC's behavior for -Wwrite-strings is a bit strange:
  3428. // * In C, this "warning flag" changes the types of string literals from
  3429. // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
  3430. // for the discarded qualifier.
  3431. // * In C++, this is just a normal warning flag.
  3432. //
  3433. // Implementing this warning correctly in C is hard, so we follow GCC's
  3434. // behavior for now. FIXME: Directly diagnose uses of a string literal as
  3435. // a non-const char* in C, rather than using this crude hack.
  3436. if (!types::isCXX(InputType)) {
  3437. // FIXME: This should behave just like a warning flag, and thus should also
  3438. // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
  3439. Arg *WriteStrings =
  3440. Args.getLastArg(options::OPT_Wwrite_strings,
  3441. options::OPT_Wno_write_strings, options::OPT_w);
  3442. if (WriteStrings &&
  3443. WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
  3444. CmdArgs.push_back("-fconst-strings");
  3445. }
  3446. // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
  3447. // during C++ compilation, which it is by default. GCC keeps this define even
  3448. // in the presence of '-w', match this behavior bug-for-bug.
  3449. if (types::isCXX(InputType) &&
  3450. Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
  3451. true)) {
  3452. CmdArgs.push_back("-fdeprecated-macro");
  3453. }
  3454. // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
  3455. if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
  3456. if (Asm->getOption().matches(options::OPT_fasm))
  3457. CmdArgs.push_back("-fgnu-keywords");
  3458. else
  3459. CmdArgs.push_back("-fno-gnu-keywords");
  3460. }
  3461. if (ShouldDisableDwarfDirectory(Args, getToolChain()))
  3462. CmdArgs.push_back("-fno-dwarf-directory-asm");
  3463. if (ShouldDisableAutolink(Args, getToolChain()))
  3464. CmdArgs.push_back("-fno-autolink");
  3465. // Add in -fdebug-compilation-dir if necessary.
  3466. addDebugCompDirArg(Args, CmdArgs);
  3467. if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
  3468. options::OPT_ftemplate_depth_EQ)) {
  3469. CmdArgs.push_back("-ftemplate-depth");
  3470. CmdArgs.push_back(A->getValue());
  3471. }
  3472. if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
  3473. CmdArgs.push_back("-foperator-arrow-depth");
  3474. CmdArgs.push_back(A->getValue());
  3475. }
  3476. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
  3477. CmdArgs.push_back("-fconstexpr-depth");
  3478. CmdArgs.push_back(A->getValue());
  3479. }
  3480. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
  3481. CmdArgs.push_back("-fconstexpr-steps");
  3482. CmdArgs.push_back(A->getValue());
  3483. }
  3484. if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
  3485. CmdArgs.push_back("-fbracket-depth");
  3486. CmdArgs.push_back(A->getValue());
  3487. }
  3488. if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
  3489. options::OPT_Wlarge_by_value_copy_def)) {
  3490. if (A->getNumValues()) {
  3491. StringRef bytes = A->getValue();
  3492. CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
  3493. } else
  3494. CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
  3495. }
  3496. if (Args.hasArg(options::OPT_relocatable_pch))
  3497. CmdArgs.push_back("-relocatable-pch");
  3498. if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
  3499. CmdArgs.push_back("-fconstant-string-class");
  3500. CmdArgs.push_back(A->getValue());
  3501. }
  3502. if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
  3503. CmdArgs.push_back("-ftabstop");
  3504. CmdArgs.push_back(A->getValue());
  3505. }
  3506. CmdArgs.push_back("-ferror-limit");
  3507. if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
  3508. CmdArgs.push_back(A->getValue());
  3509. else
  3510. CmdArgs.push_back("19");
  3511. if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
  3512. CmdArgs.push_back("-fmacro-backtrace-limit");
  3513. CmdArgs.push_back(A->getValue());
  3514. }
  3515. if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
  3516. CmdArgs.push_back("-ftemplate-backtrace-limit");
  3517. CmdArgs.push_back(A->getValue());
  3518. }
  3519. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
  3520. CmdArgs.push_back("-fconstexpr-backtrace-limit");
  3521. CmdArgs.push_back(A->getValue());
  3522. }
  3523. if (Arg *A = Args.getLastArg(options::OPT_fspell_checking_limit_EQ)) {
  3524. CmdArgs.push_back("-fspell-checking-limit");
  3525. CmdArgs.push_back(A->getValue());
  3526. }
  3527. // Pass -fmessage-length=.
  3528. CmdArgs.push_back("-fmessage-length");
  3529. if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
  3530. CmdArgs.push_back(A->getValue());
  3531. } else {
  3532. // If -fmessage-length=N was not specified, determine whether this is a
  3533. // terminal and, if so, implicitly define -fmessage-length appropriately.
  3534. unsigned N = llvm::sys::Process::StandardErrColumns();
  3535. CmdArgs.push_back(Args.MakeArgString(Twine(N)));
  3536. }
  3537. // -fvisibility= and -fvisibility-ms-compat are of a piece.
  3538. if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
  3539. options::OPT_fvisibility_ms_compat)) {
  3540. if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
  3541. CmdArgs.push_back("-fvisibility");
  3542. CmdArgs.push_back(A->getValue());
  3543. } else {
  3544. assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
  3545. CmdArgs.push_back("-fvisibility");
  3546. CmdArgs.push_back("hidden");
  3547. CmdArgs.push_back("-ftype-visibility");
  3548. CmdArgs.push_back("default");
  3549. }
  3550. }
  3551. Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
  3552. Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
  3553. // -fhosted is default.
  3554. if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
  3555. KernelOrKext)
  3556. CmdArgs.push_back("-ffreestanding");
  3557. // Forward -f (flag) options which we can pass directly.
  3558. Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
  3559. Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
  3560. Args.AddLastArg(CmdArgs, options::OPT_fstandalone_debug);
  3561. Args.AddLastArg(CmdArgs, options::OPT_fno_standalone_debug);
  3562. Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
  3563. // AltiVec-like language extensions aren't relevant for assembling.
  3564. if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm) {
  3565. Args.AddLastArg(CmdArgs, options::OPT_faltivec);
  3566. Args.AddLastArg(CmdArgs, options::OPT_fzvector);
  3567. }
  3568. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
  3569. Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
  3570. // Forward flags for OpenMP
  3571. if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
  3572. options::OPT_fno_openmp, false))
  3573. switch (getOpenMPRuntime(getToolChain(), Args)) {
  3574. case OMPRT_OMP:
  3575. case OMPRT_IOMP5:
  3576. // Clang can generate useful OpenMP code for these two runtime libraries.
  3577. CmdArgs.push_back("-fopenmp");
  3578. // If no option regarding the use of TLS in OpenMP codegeneration is
  3579. // given, decide a default based on the target. Otherwise rely on the
  3580. // options and pass the right information to the frontend.
  3581. if (!Args.hasFlag(options::OPT_fopenmp_use_tls,
  3582. options::OPT_fnoopenmp_use_tls,
  3583. getToolChain().getArch() == llvm::Triple::ppc ||
  3584. getToolChain().getArch() == llvm::Triple::ppc64 ||
  3585. getToolChain().getArch() == llvm::Triple::ppc64le))
  3586. CmdArgs.push_back("-fnoopenmp-use-tls");
  3587. break;
  3588. default:
  3589. // By default, if Clang doesn't know how to generate useful OpenMP code
  3590. // for a specific runtime library, we just don't pass the '-fopenmp' flag
  3591. // down to the actual compilation.
  3592. // FIXME: It would be better to have a mode which *only* omits IR
  3593. // generation based on the OpenMP support so that we get consistent
  3594. // semantic analysis, etc.
  3595. break;
  3596. }
  3597. const SanitizerArgs &Sanitize = getToolChain().getSanitizerArgs();
  3598. Sanitize.addArgs(getToolChain(), Args, CmdArgs, InputType);
  3599. // Report an error for -faltivec on anything other than PowerPC.
  3600. if (const Arg *A = Args.getLastArg(options::OPT_faltivec)) {
  3601. const llvm::Triple::ArchType Arch = getToolChain().getArch();
  3602. if (!(Arch == llvm::Triple::ppc || Arch == llvm::Triple::ppc64 ||
  3603. Arch == llvm::Triple::ppc64le))
  3604. D.Diag(diag::err_drv_argument_only_allowed_with) << A->getAsString(Args)
  3605. << "ppc/ppc64/ppc64le";
  3606. }
  3607. // -fzvector is incompatible with -faltivec.
  3608. if (Arg *A = Args.getLastArg(options::OPT_fzvector))
  3609. if (Args.hasArg(options::OPT_faltivec))
  3610. D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
  3611. << "-faltivec";
  3612. if (getToolChain().SupportsProfiling())
  3613. Args.AddLastArg(CmdArgs, options::OPT_pg);
  3614. // -flax-vector-conversions is default.
  3615. if (!Args.hasFlag(options::OPT_flax_vector_conversions,
  3616. options::OPT_fno_lax_vector_conversions))
  3617. CmdArgs.push_back("-fno-lax-vector-conversions");
  3618. if (Args.getLastArg(options::OPT_fapple_kext))
  3619. CmdArgs.push_back("-fapple-kext");
  3620. Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
  3621. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
  3622. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
  3623. Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
  3624. Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
  3625. if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
  3626. CmdArgs.push_back("-ftrapv-handler");
  3627. CmdArgs.push_back(A->getValue());
  3628. }
  3629. Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
  3630. // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
  3631. // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
  3632. if (Arg *A = Args.getLastArg(options::OPT_fwrapv, options::OPT_fno_wrapv)) {
  3633. if (A->getOption().matches(options::OPT_fwrapv))
  3634. CmdArgs.push_back("-fwrapv");
  3635. } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
  3636. options::OPT_fno_strict_overflow)) {
  3637. if (A->getOption().matches(options::OPT_fno_strict_overflow))
  3638. CmdArgs.push_back("-fwrapv");
  3639. }
  3640. if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
  3641. options::OPT_fno_reroll_loops))
  3642. if (A->getOption().matches(options::OPT_freroll_loops))
  3643. CmdArgs.push_back("-freroll-loops");
  3644. Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
  3645. Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
  3646. options::OPT_fno_unroll_loops);
  3647. Args.AddLastArg(CmdArgs, options::OPT_pthread);
  3648. // -stack-protector=0 is default.
  3649. unsigned StackProtectorLevel = 0;
  3650. if (getToolChain().getSanitizerArgs().needsSafeStackRt()) {
  3651. Args.ClaimAllArgs(options::OPT_fno_stack_protector);
  3652. Args.ClaimAllArgs(options::OPT_fstack_protector_all);
  3653. Args.ClaimAllArgs(options::OPT_fstack_protector_strong);
  3654. Args.ClaimAllArgs(options::OPT_fstack_protector);
  3655. } else if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
  3656. options::OPT_fstack_protector_all,
  3657. options::OPT_fstack_protector_strong,
  3658. options::OPT_fstack_protector)) {
  3659. if (A->getOption().matches(options::OPT_fstack_protector)) {
  3660. StackProtectorLevel = std::max<unsigned>(
  3661. LangOptions::SSPOn,
  3662. getToolChain().GetDefaultStackProtectorLevel(KernelOrKext));
  3663. } else if (A->getOption().matches(options::OPT_fstack_protector_strong))
  3664. StackProtectorLevel = LangOptions::SSPStrong;
  3665. else if (A->getOption().matches(options::OPT_fstack_protector_all))
  3666. StackProtectorLevel = LangOptions::SSPReq;
  3667. } else {
  3668. StackProtectorLevel =
  3669. getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
  3670. }
  3671. if (StackProtectorLevel) {
  3672. CmdArgs.push_back("-stack-protector");
  3673. CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
  3674. }
  3675. // --param ssp-buffer-size=
  3676. for (const Arg *A : Args.filtered(options::OPT__param)) {
  3677. StringRef Str(A->getValue());
  3678. if (Str.startswith("ssp-buffer-size=")) {
  3679. if (StackProtectorLevel) {
  3680. CmdArgs.push_back("-stack-protector-buffer-size");
  3681. // FIXME: Verify the argument is a valid integer.
  3682. CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
  3683. }
  3684. A->claim();
  3685. }
  3686. }
  3687. // Translate -mstackrealign
  3688. if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
  3689. false)) {
  3690. CmdArgs.push_back("-backend-option");
  3691. CmdArgs.push_back("-force-align-stack");
  3692. }
  3693. if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
  3694. false)) {
  3695. CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
  3696. }
  3697. if (Args.hasArg(options::OPT_mstack_alignment)) {
  3698. StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
  3699. CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
  3700. }
  3701. if (Args.hasArg(options::OPT_mstack_probe_size)) {
  3702. StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size);
  3703. if (!Size.empty())
  3704. CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size));
  3705. else
  3706. CmdArgs.push_back("-mstack-probe-size=0");
  3707. }
  3708. if (getToolChain().getArch() == llvm::Triple::aarch64 ||
  3709. getToolChain().getArch() == llvm::Triple::aarch64_be)
  3710. CmdArgs.push_back("-fallow-half-arguments-and-returns");
  3711. if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
  3712. options::OPT_mno_restrict_it)) {
  3713. if (A->getOption().matches(options::OPT_mrestrict_it)) {
  3714. CmdArgs.push_back("-backend-option");
  3715. CmdArgs.push_back("-arm-restrict-it");
  3716. } else {
  3717. CmdArgs.push_back("-backend-option");
  3718. CmdArgs.push_back("-arm-no-restrict-it");
  3719. }
  3720. } else if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
  3721. TT.getArch() == llvm::Triple::thumb)) {
  3722. // Windows on ARM expects restricted IT blocks
  3723. CmdArgs.push_back("-backend-option");
  3724. CmdArgs.push_back("-arm-restrict-it");
  3725. }
  3726. // Forward -f options with positive and negative forms; we translate
  3727. // these by hand.
  3728. if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ)) {
  3729. StringRef fname = A->getValue();
  3730. if (!llvm::sys::fs::exists(fname))
  3731. D.Diag(diag::err_drv_no_such_file) << fname;
  3732. else
  3733. A->render(Args, CmdArgs);
  3734. }
  3735. if (Args.hasArg(options::OPT_mkernel)) {
  3736. if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
  3737. CmdArgs.push_back("-fapple-kext");
  3738. if (!Args.hasArg(options::OPT_fbuiltin))
  3739. CmdArgs.push_back("-fno-builtin");
  3740. Args.ClaimAllArgs(options::OPT_fno_builtin);
  3741. }
  3742. // -fbuiltin is default.
  3743. else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
  3744. CmdArgs.push_back("-fno-builtin");
  3745. if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
  3746. options::OPT_fno_assume_sane_operator_new))
  3747. CmdArgs.push_back("-fno-assume-sane-operator-new");
  3748. // -fblocks=0 is default.
  3749. if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
  3750. getToolChain().IsBlocksDefault()) ||
  3751. (Args.hasArg(options::OPT_fgnu_runtime) &&
  3752. Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
  3753. !Args.hasArg(options::OPT_fno_blocks))) {
  3754. CmdArgs.push_back("-fblocks");
  3755. if (!Args.hasArg(options::OPT_fgnu_runtime) &&
  3756. !getToolChain().hasBlocksRuntime())
  3757. CmdArgs.push_back("-fblocks-runtime-optional");
  3758. }
  3759. // -fmodules enables the use of precompiled modules (off by default).
  3760. // Users can pass -fno-cxx-modules to turn off modules support for
  3761. // C++/Objective-C++ programs.
  3762. bool HaveModules = false;
  3763. if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
  3764. bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
  3765. options::OPT_fno_cxx_modules, true);
  3766. if (AllowedInCXX || !types::isCXX(InputType)) {
  3767. CmdArgs.push_back("-fmodules");
  3768. HaveModules = true;
  3769. }
  3770. }
  3771. // -fmodule-maps enables implicit reading of module map files. By default,
  3772. // this is enabled if we are using precompiled modules.
  3773. if (Args.hasFlag(options::OPT_fimplicit_module_maps,
  3774. options::OPT_fno_implicit_module_maps, HaveModules)) {
  3775. CmdArgs.push_back("-fimplicit-module-maps");
  3776. }
  3777. // -fmodules-decluse checks that modules used are declared so (off by
  3778. // default).
  3779. if (Args.hasFlag(options::OPT_fmodules_decluse,
  3780. options::OPT_fno_modules_decluse, false)) {
  3781. CmdArgs.push_back("-fmodules-decluse");
  3782. }
  3783. // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
  3784. // all #included headers are part of modules.
  3785. if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
  3786. options::OPT_fno_modules_strict_decluse, false)) {
  3787. CmdArgs.push_back("-fmodules-strict-decluse");
  3788. }
  3789. // -fno-implicit-modules turns off implicitly compiling modules on demand.
  3790. if (!Args.hasFlag(options::OPT_fimplicit_modules,
  3791. options::OPT_fno_implicit_modules)) {
  3792. CmdArgs.push_back("-fno-implicit-modules");
  3793. }
  3794. // -fmodule-name specifies the module that is currently being built (or
  3795. // used for header checking by -fmodule-maps).
  3796. Args.AddLastArg(CmdArgs, options::OPT_fmodule_name);
  3797. // -fmodule-map-file can be used to specify files containing module
  3798. // definitions.
  3799. Args.AddAllArgs(CmdArgs, options::OPT_fmodule_map_file);
  3800. // -fmodule-file can be used to specify files containing precompiled modules.
  3801. Args.AddAllArgs(CmdArgs, options::OPT_fmodule_file);
  3802. // -fmodule-cache-path specifies where our implicitly-built module files
  3803. // should be written.
  3804. SmallString<128> Path;
  3805. if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path))
  3806. Path = A->getValue();
  3807. if (HaveModules) {
  3808. if (C.isForDiagnostics()) {
  3809. // When generating crash reports, we want to emit the modules along with
  3810. // the reproduction sources, so we ignore any provided module path.
  3811. Path = Output.getFilename();
  3812. llvm::sys::path::replace_extension(Path, ".cache");
  3813. llvm::sys::path::append(Path, "modules");
  3814. } else if (Path.empty()) {
  3815. // No module path was provided: use the default.
  3816. llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, Path);
  3817. llvm::sys::path::append(Path, "org.llvm.clang.");
  3818. appendUserToPath(Path);
  3819. llvm::sys::path::append(Path, "ModuleCache");
  3820. }
  3821. const char Arg[] = "-fmodules-cache-path=";
  3822. Path.insert(Path.begin(), Arg, Arg + strlen(Arg));
  3823. CmdArgs.push_back(Args.MakeArgString(Path));
  3824. }
  3825. // When building modules and generating crashdumps, we need to dump a module
  3826. // dependency VFS alongside the output.
  3827. if (HaveModules && C.isForDiagnostics()) {
  3828. SmallString<128> VFSDir(Output.getFilename());
  3829. llvm::sys::path::replace_extension(VFSDir, ".cache");
  3830. // Add the cache directory as a temp so the crash diagnostics pick it up.
  3831. C.addTempFile(Args.MakeArgString(VFSDir));
  3832. llvm::sys::path::append(VFSDir, "vfs");
  3833. CmdArgs.push_back("-module-dependency-dir");
  3834. CmdArgs.push_back(Args.MakeArgString(VFSDir));
  3835. }
  3836. if (HaveModules)
  3837. Args.AddLastArg(CmdArgs, options::OPT_fmodules_user_build_path);
  3838. // Pass through all -fmodules-ignore-macro arguments.
  3839. Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
  3840. Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
  3841. Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
  3842. Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
  3843. if (Arg *A = Args.getLastArg(options::OPT_fbuild_session_file)) {
  3844. if (Args.hasArg(options::OPT_fbuild_session_timestamp))
  3845. D.Diag(diag::err_drv_argument_not_allowed_with)
  3846. << A->getAsString(Args) << "-fbuild-session-timestamp";
  3847. llvm::sys::fs::file_status Status;
  3848. if (llvm::sys::fs::status(A->getValue(), Status))
  3849. D.Diag(diag::err_drv_no_such_file) << A->getValue();
  3850. CmdArgs.push_back(Args.MakeArgString(
  3851. "-fbuild-session-timestamp=" +
  3852. Twine((uint64_t)Status.getLastModificationTime().toEpochTime())));
  3853. }
  3854. if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
  3855. if (!Args.getLastArg(options::OPT_fbuild_session_timestamp,
  3856. options::OPT_fbuild_session_file))
  3857. D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
  3858. Args.AddLastArg(CmdArgs,
  3859. options::OPT_fmodules_validate_once_per_build_session);
  3860. }
  3861. Args.AddLastArg(CmdArgs, options::OPT_fmodules_validate_system_headers);
  3862. // -faccess-control is default.
  3863. if (Args.hasFlag(options::OPT_fno_access_control,
  3864. options::OPT_faccess_control, false))
  3865. CmdArgs.push_back("-fno-access-control");
  3866. // -felide-constructors is the default.
  3867. if (Args.hasFlag(options::OPT_fno_elide_constructors,
  3868. options::OPT_felide_constructors, false))
  3869. CmdArgs.push_back("-fno-elide-constructors");
  3870. ToolChain::RTTIMode RTTIMode = getToolChain().getRTTIMode();
  3871. if (KernelOrKext || (types::isCXX(InputType) &&
  3872. (RTTIMode == ToolChain::RM_DisabledExplicitly ||
  3873. RTTIMode == ToolChain::RM_DisabledImplicitly)))
  3874. CmdArgs.push_back("-fno-rtti");
  3875. // -fshort-enums=0 is default for all architectures except Hexagon.
  3876. if (Args.hasFlag(options::OPT_fshort_enums, options::OPT_fno_short_enums,
  3877. getToolChain().getArch() == llvm::Triple::hexagon))
  3878. CmdArgs.push_back("-fshort-enums");
  3879. // -fsigned-char is default.
  3880. if (Arg *A = Args.getLastArg(
  3881. options::OPT_fsigned_char, options::OPT_fno_signed_char,
  3882. options::OPT_funsigned_char, options::OPT_fno_unsigned_char)) {
  3883. if (A->getOption().matches(options::OPT_funsigned_char) ||
  3884. A->getOption().matches(options::OPT_fno_signed_char)) {
  3885. CmdArgs.push_back("-fno-signed-char");
  3886. }
  3887. } else if (!isSignedCharDefault(getToolChain().getTriple())) {
  3888. CmdArgs.push_back("-fno-signed-char");
  3889. }
  3890. // -fuse-cxa-atexit is default.
  3891. if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
  3892. options::OPT_fno_use_cxa_atexit,
  3893. !IsWindowsCygnus && !IsWindowsGNU &&
  3894. getToolChain().getArch() != llvm::Triple::hexagon &&
  3895. getToolChain().getArch() != llvm::Triple::xcore) ||
  3896. KernelOrKext)
  3897. CmdArgs.push_back("-fno-use-cxa-atexit");
  3898. // -fms-extensions=0 is default.
  3899. if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
  3900. IsWindowsMSVC))
  3901. CmdArgs.push_back("-fms-extensions");
  3902. // -fno-use-line-directives is default.
  3903. if (Args.hasFlag(options::OPT_fuse_line_directives,
  3904. options::OPT_fno_use_line_directives, false))
  3905. CmdArgs.push_back("-fuse-line-directives");
  3906. // -fms-compatibility=0 is default.
  3907. if (Args.hasFlag(options::OPT_fms_compatibility,
  3908. options::OPT_fno_ms_compatibility,
  3909. (IsWindowsMSVC &&
  3910. Args.hasFlag(options::OPT_fms_extensions,
  3911. options::OPT_fno_ms_extensions, true))))
  3912. CmdArgs.push_back("-fms-compatibility");
  3913. // -fms-compatibility-version=18.00 is default.
  3914. VersionTuple MSVT = visualstudio::getMSVCVersion(
  3915. &D, getToolChain().getTriple(), Args, IsWindowsMSVC);
  3916. if (!MSVT.empty())
  3917. CmdArgs.push_back(
  3918. Args.MakeArgString("-fms-compatibility-version=" + MSVT.getAsString()));
  3919. bool IsMSVC2015Compatible = MSVT.getMajor() >= 19;
  3920. if (ImplyVCPPCXXVer) {
  3921. if (IsMSVC2015Compatible)
  3922. CmdArgs.push_back("-std=c++14");
  3923. else
  3924. CmdArgs.push_back("-std=c++11");
  3925. }
  3926. // -fno-borland-extensions is default.
  3927. if (Args.hasFlag(options::OPT_fborland_extensions,
  3928. options::OPT_fno_borland_extensions, false))
  3929. CmdArgs.push_back("-fborland-extensions");
  3930. // -fthreadsafe-static is default, except for MSVC compatibility versions less
  3931. // than 19.
  3932. if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
  3933. options::OPT_fno_threadsafe_statics,
  3934. !IsWindowsMSVC || IsMSVC2015Compatible))
  3935. CmdArgs.push_back("-fno-threadsafe-statics");
  3936. // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
  3937. // needs it.
  3938. if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
  3939. options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
  3940. CmdArgs.push_back("-fdelayed-template-parsing");
  3941. // -fgnu-keywords default varies depending on language; only pass if
  3942. // specified.
  3943. if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
  3944. options::OPT_fno_gnu_keywords))
  3945. A->render(Args, CmdArgs);
  3946. if (Args.hasFlag(options::OPT_fgnu89_inline, options::OPT_fno_gnu89_inline,
  3947. false))
  3948. CmdArgs.push_back("-fgnu89-inline");
  3949. if (Args.hasArg(options::OPT_fno_inline))
  3950. CmdArgs.push_back("-fno-inline");
  3951. if (Args.hasArg(options::OPT_fno_inline_functions))
  3952. CmdArgs.push_back("-fno-inline-functions");
  3953. ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
  3954. // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
  3955. // legacy is the default. Except for deployment taget of 10.5,
  3956. // next runtime is always legacy dispatch and -fno-objc-legacy-dispatch
  3957. // gets ignored silently.
  3958. if (objcRuntime.isNonFragile()) {
  3959. if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
  3960. options::OPT_fno_objc_legacy_dispatch,
  3961. objcRuntime.isLegacyDispatchDefaultForArch(
  3962. getToolChain().getArch()))) {
  3963. if (getToolChain().UseObjCMixedDispatch())
  3964. CmdArgs.push_back("-fobjc-dispatch-method=mixed");
  3965. else
  3966. CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
  3967. }
  3968. }
  3969. // When ObjectiveC legacy runtime is in effect on MacOSX,
  3970. // turn on the option to do Array/Dictionary subscripting
  3971. // by default.
  3972. if (getToolChain().getArch() == llvm::Triple::x86 &&
  3973. getToolChain().getTriple().isMacOSX() &&
  3974. !getToolChain().getTriple().isMacOSXVersionLT(10, 7) &&
  3975. objcRuntime.getKind() == ObjCRuntime::FragileMacOSX &&
  3976. objcRuntime.isNeXTFamily())
  3977. CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
  3978. // -fencode-extended-block-signature=1 is default.
  3979. if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
  3980. CmdArgs.push_back("-fencode-extended-block-signature");
  3981. }
  3982. // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
  3983. // NOTE: This logic is duplicated in ToolChains.cpp.
  3984. bool ARC = isObjCAutoRefCount(Args);
  3985. if (ARC) {
  3986. getToolChain().CheckObjCARC();
  3987. CmdArgs.push_back("-fobjc-arc");
  3988. // FIXME: It seems like this entire block, and several around it should be
  3989. // wrapped in isObjC, but for now we just use it here as this is where it
  3990. // was being used previously.
  3991. if (types::isCXX(InputType) && types::isObjC(InputType)) {
  3992. if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
  3993. CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
  3994. else
  3995. CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
  3996. }
  3997. // Allow the user to enable full exceptions code emission.
  3998. // We define off for Objective-CC, on for Objective-C++.
  3999. if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
  4000. options::OPT_fno_objc_arc_exceptions,
  4001. /*default*/ types::isCXX(InputType)))
  4002. CmdArgs.push_back("-fobjc-arc-exceptions");
  4003. }
  4004. // -fobjc-infer-related-result-type is the default, except in the Objective-C
  4005. // rewriter.
  4006. if (rewriteKind != RK_None)
  4007. CmdArgs.push_back("-fno-objc-infer-related-result-type");
  4008. // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
  4009. // takes precedence.
  4010. const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
  4011. if (!GCArg)
  4012. GCArg = Args.getLastArg(options::OPT_fobjc_gc);
  4013. if (GCArg) {
  4014. if (ARC) {
  4015. D.Diag(diag::err_drv_objc_gc_arr) << GCArg->getAsString(Args);
  4016. } else if (getToolChain().SupportsObjCGC()) {
  4017. GCArg->render(Args, CmdArgs);
  4018. } else {
  4019. // FIXME: We should move this to a hard error.
  4020. D.Diag(diag::warn_drv_objc_gc_unsupported) << GCArg->getAsString(Args);
  4021. }
  4022. }
  4023. if (Args.hasFlag(options::OPT_fapplication_extension,
  4024. options::OPT_fno_application_extension, false))
  4025. CmdArgs.push_back("-fapplication-extension");
  4026. // Handle GCC-style exception args.
  4027. if (!C.getDriver().IsCLMode())
  4028. addExceptionArgs(Args, InputType, getToolChain(), KernelOrKext, objcRuntime,
  4029. CmdArgs);
  4030. if (getToolChain().UseSjLjExceptions())
  4031. CmdArgs.push_back("-fsjlj-exceptions");
  4032. // C++ "sane" operator new.
  4033. if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
  4034. options::OPT_fno_assume_sane_operator_new))
  4035. CmdArgs.push_back("-fno-assume-sane-operator-new");
  4036. // -fsized-deallocation is off by default, as it is an ABI-breaking change for
  4037. // most platforms.
  4038. if (Args.hasFlag(options::OPT_fsized_deallocation,
  4039. options::OPT_fno_sized_deallocation, false))
  4040. CmdArgs.push_back("-fsized-deallocation");
  4041. // -fconstant-cfstrings is default, and may be subject to argument translation
  4042. // on Darwin.
  4043. if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
  4044. options::OPT_fno_constant_cfstrings) ||
  4045. !Args.hasFlag(options::OPT_mconstant_cfstrings,
  4046. options::OPT_mno_constant_cfstrings))
  4047. CmdArgs.push_back("-fno-constant-cfstrings");
  4048. // -fshort-wchar default varies depending on platform; only
  4049. // pass if specified.
  4050. if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
  4051. options::OPT_fno_short_wchar))
  4052. A->render(Args, CmdArgs);
  4053. // -fno-pascal-strings is default, only pass non-default.
  4054. if (Args.hasFlag(options::OPT_fpascal_strings,
  4055. options::OPT_fno_pascal_strings, false))
  4056. CmdArgs.push_back("-fpascal-strings");
  4057. // Honor -fpack-struct= and -fpack-struct, if given. Note that
  4058. // -fno-pack-struct doesn't apply to -fpack-struct=.
  4059. if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
  4060. std::string PackStructStr = "-fpack-struct=";
  4061. PackStructStr += A->getValue();
  4062. CmdArgs.push_back(Args.MakeArgString(PackStructStr));
  4063. } else if (Args.hasFlag(options::OPT_fpack_struct,
  4064. options::OPT_fno_pack_struct, false)) {
  4065. CmdArgs.push_back("-fpack-struct=1");
  4066. }
  4067. // Handle -fmax-type-align=N and -fno-type-align
  4068. bool SkipMaxTypeAlign = Args.hasArg(options::OPT_fno_max_type_align);
  4069. if (Arg *A = Args.getLastArg(options::OPT_fmax_type_align_EQ)) {
  4070. if (!SkipMaxTypeAlign) {
  4071. std::string MaxTypeAlignStr = "-fmax-type-align=";
  4072. MaxTypeAlignStr += A->getValue();
  4073. CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
  4074. }
  4075. } else if (getToolChain().getTriple().isOSDarwin()) {
  4076. if (!SkipMaxTypeAlign) {
  4077. std::string MaxTypeAlignStr = "-fmax-type-align=16";
  4078. CmdArgs.push_back(Args.MakeArgString(MaxTypeAlignStr));
  4079. }
  4080. }
  4081. if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) {
  4082. if (!Args.hasArg(options::OPT_fcommon))
  4083. CmdArgs.push_back("-fno-common");
  4084. Args.ClaimAllArgs(options::OPT_fno_common);
  4085. }
  4086. // -fcommon is default, only pass non-default.
  4087. else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
  4088. CmdArgs.push_back("-fno-common");
  4089. // -fsigned-bitfields is default, and clang doesn't yet support
  4090. // -funsigned-bitfields.
  4091. if (!Args.hasFlag(options::OPT_fsigned_bitfields,
  4092. options::OPT_funsigned_bitfields))
  4093. D.Diag(diag::warn_drv_clang_unsupported)
  4094. << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
  4095. // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
  4096. if (!Args.hasFlag(options::OPT_ffor_scope, options::OPT_fno_for_scope))
  4097. D.Diag(diag::err_drv_clang_unsupported)
  4098. << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
  4099. // -finput_charset=UTF-8 is default. Reject others
  4100. if (Arg *inputCharset = Args.getLastArg(options::OPT_finput_charset_EQ)) {
  4101. StringRef value = inputCharset->getValue();
  4102. if (value != "UTF-8")
  4103. D.Diag(diag::err_drv_invalid_value) << inputCharset->getAsString(Args)
  4104. << value;
  4105. }
  4106. // -fexec_charset=UTF-8 is default. Reject others
  4107. if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) {
  4108. StringRef value = execCharset->getValue();
  4109. if (value != "UTF-8")
  4110. D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args)
  4111. << value;
  4112. }
  4113. // -fcaret-diagnostics is default.
  4114. if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
  4115. options::OPT_fno_caret_diagnostics, true))
  4116. CmdArgs.push_back("-fno-caret-diagnostics");
  4117. // -fdiagnostics-fixit-info is default, only pass non-default.
  4118. if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
  4119. options::OPT_fno_diagnostics_fixit_info))
  4120. CmdArgs.push_back("-fno-diagnostics-fixit-info");
  4121. // Enable -fdiagnostics-show-option by default.
  4122. if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
  4123. options::OPT_fno_diagnostics_show_option))
  4124. CmdArgs.push_back("-fdiagnostics-show-option");
  4125. if (const Arg *A =
  4126. Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
  4127. CmdArgs.push_back("-fdiagnostics-show-category");
  4128. CmdArgs.push_back(A->getValue());
  4129. }
  4130. if (const Arg *A = Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
  4131. CmdArgs.push_back("-fdiagnostics-format");
  4132. CmdArgs.push_back(A->getValue());
  4133. }
  4134. if (Arg *A = Args.getLastArg(
  4135. options::OPT_fdiagnostics_show_note_include_stack,
  4136. options::OPT_fno_diagnostics_show_note_include_stack)) {
  4137. if (A->getOption().matches(
  4138. options::OPT_fdiagnostics_show_note_include_stack))
  4139. CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
  4140. else
  4141. CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
  4142. }
  4143. // Color diagnostics are the default, unless the terminal doesn't support
  4144. // them.
  4145. // Support both clang's -f[no-]color-diagnostics and gcc's
  4146. // -f[no-]diagnostics-colors[=never|always|auto].
  4147. enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
  4148. for (const auto &Arg : Args) {
  4149. const Option &O = Arg->getOption();
  4150. if (!O.matches(options::OPT_fcolor_diagnostics) &&
  4151. !O.matches(options::OPT_fdiagnostics_color) &&
  4152. !O.matches(options::OPT_fno_color_diagnostics) &&
  4153. !O.matches(options::OPT_fno_diagnostics_color) &&
  4154. !O.matches(options::OPT_fdiagnostics_color_EQ))
  4155. continue;
  4156. Arg->claim();
  4157. if (O.matches(options::OPT_fcolor_diagnostics) ||
  4158. O.matches(options::OPT_fdiagnostics_color)) {
  4159. ShowColors = Colors_On;
  4160. } else if (O.matches(options::OPT_fno_color_diagnostics) ||
  4161. O.matches(options::OPT_fno_diagnostics_color)) {
  4162. ShowColors = Colors_Off;
  4163. } else {
  4164. assert(O.matches(options::OPT_fdiagnostics_color_EQ));
  4165. StringRef value(Arg->getValue());
  4166. if (value == "always")
  4167. ShowColors = Colors_On;
  4168. else if (value == "never")
  4169. ShowColors = Colors_Off;
  4170. else if (value == "auto")
  4171. ShowColors = Colors_Auto;
  4172. else
  4173. getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
  4174. << ("-fdiagnostics-color=" + value).str();
  4175. }
  4176. }
  4177. if (ShowColors == Colors_On ||
  4178. (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
  4179. CmdArgs.push_back("-fcolor-diagnostics");
  4180. #ifdef MSFT_SUPPORTS_ANSI_ESCAPE_CODES
  4181. if (Args.hasArg(options::OPT_fansi_escape_codes))
  4182. CmdArgs.push_back("-fansi-escape-codes");
  4183. #endif
  4184. if (!Args.hasFlag(options::OPT_fshow_source_location,
  4185. options::OPT_fno_show_source_location))
  4186. CmdArgs.push_back("-fno-show-source-location");
  4187. if (!Args.hasFlag(options::OPT_fshow_column, options::OPT_fno_show_column,
  4188. true))
  4189. CmdArgs.push_back("-fno-show-column");
  4190. if (!Args.hasFlag(options::OPT_fspell_checking,
  4191. options::OPT_fno_spell_checking))
  4192. CmdArgs.push_back("-fno-spell-checking");
  4193. // -fno-asm-blocks is default.
  4194. if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
  4195. false))
  4196. CmdArgs.push_back("-fasm-blocks");
  4197. // -fgnu-inline-asm is default.
  4198. if (!Args.hasFlag(options::OPT_fgnu_inline_asm,
  4199. options::OPT_fno_gnu_inline_asm, true))
  4200. CmdArgs.push_back("-fno-gnu-inline-asm");
  4201. // Enable vectorization per default according to the optimization level
  4202. // selected. For optimization levels that want vectorization we use the alias
  4203. // option to simplify the hasFlag logic.
  4204. bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
  4205. OptSpecifier VectorizeAliasOption =
  4206. EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;
  4207. if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
  4208. options::OPT_fno_vectorize, EnableVec))
  4209. CmdArgs.push_back("-vectorize-loops");
  4210. // -fslp-vectorize is enabled based on the optimization level selected.
  4211. bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
  4212. OptSpecifier SLPVectAliasOption =
  4213. EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;
  4214. if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
  4215. options::OPT_fno_slp_vectorize, EnableSLPVec))
  4216. CmdArgs.push_back("-vectorize-slp");
  4217. // -fno-slp-vectorize-aggressive is default.
  4218. if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive,
  4219. options::OPT_fno_slp_vectorize_aggressive, false))
  4220. CmdArgs.push_back("-vectorize-slp-aggressive");
  4221. if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
  4222. A->render(Args, CmdArgs);
  4223. // -fdollars-in-identifiers default varies depending on platform and
  4224. // language; only pass if specified.
  4225. if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
  4226. options::OPT_fno_dollars_in_identifiers)) {
  4227. if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
  4228. CmdArgs.push_back("-fdollars-in-identifiers");
  4229. else
  4230. CmdArgs.push_back("-fno-dollars-in-identifiers");
  4231. }
  4232. // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
  4233. // practical purposes.
  4234. if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
  4235. options::OPT_fno_unit_at_a_time)) {
  4236. if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
  4237. D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
  4238. }
  4239. if (Args.hasFlag(options::OPT_fapple_pragma_pack,
  4240. options::OPT_fno_apple_pragma_pack, false))
  4241. CmdArgs.push_back("-fapple-pragma-pack");
  4242. // le32-specific flags:
  4243. // -fno-math-builtin: clang should not convert math builtins to intrinsics
  4244. // by default.
  4245. if (getToolChain().getArch() == llvm::Triple::le32) {
  4246. CmdArgs.push_back("-fno-math-builtin");
  4247. }
  4248. // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
  4249. //
  4250. // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
  4251. #if 0
  4252. if (getToolChain().getTriple().isOSDarwin() &&
  4253. (getToolChain().getArch() == llvm::Triple::arm ||
  4254. getToolChain().getArch() == llvm::Triple::thumb)) {
  4255. if (!Args.hasArg(options::OPT_fbuiltin_strcat))
  4256. CmdArgs.push_back("-fno-builtin-strcat");
  4257. if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
  4258. CmdArgs.push_back("-fno-builtin-strcpy");
  4259. }
  4260. #endif
  4261. // Enable rewrite includes if the user's asked for it or if we're generating
  4262. // diagnostics.
  4263. // TODO: Once -module-dependency-dir works with -frewrite-includes it'd be
  4264. // nice to enable this when doing a crashdump for modules as well.
  4265. if (Args.hasFlag(options::OPT_frewrite_includes,
  4266. options::OPT_fno_rewrite_includes, false) ||
  4267. (C.isForDiagnostics() && !HaveModules))
  4268. CmdArgs.push_back("-frewrite-includes");
  4269. // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
  4270. if (Arg *A = Args.getLastArg(options::OPT_traditional,
  4271. options::OPT_traditional_cpp)) {
  4272. if (isa<PreprocessJobAction>(JA))
  4273. CmdArgs.push_back("-traditional-cpp");
  4274. else
  4275. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  4276. }
  4277. Args.AddLastArg(CmdArgs, options::OPT_dM);
  4278. Args.AddLastArg(CmdArgs, options::OPT_dD);
  4279. // Handle serialized diagnostics.
  4280. if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
  4281. CmdArgs.push_back("-serialize-diagnostic-file");
  4282. CmdArgs.push_back(Args.MakeArgString(A->getValue()));
  4283. }
  4284. if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
  4285. CmdArgs.push_back("-fretain-comments-from-system-headers");
  4286. // Forward -fcomment-block-commands to -cc1.
  4287. Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
  4288. // Forward -fparse-all-comments to -cc1.
  4289. Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
  4290. // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
  4291. // parser.
  4292. Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
  4293. bool OptDisabled = false;
  4294. for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
  4295. A->claim();
  4296. // We translate this by hand to the -cc1 argument, since nightly test uses
  4297. // it and developers have been trained to spell it with -mllvm.
  4298. if (StringRef(A->getValue(0)) == "-disable-llvm-optzns") {
  4299. CmdArgs.push_back("-disable-llvm-optzns");
  4300. OptDisabled = true;
  4301. } else
  4302. A->render(Args, CmdArgs);
  4303. }
  4304. // With -save-temps, we want to save the unoptimized bitcode output from the
  4305. // CompileJobAction, so disable optimizations if they are not already
  4306. // disabled.
  4307. if (C.getDriver().isSaveTempsEnabled() && !OptDisabled &&
  4308. isa<CompileJobAction>(JA))
  4309. CmdArgs.push_back("-disable-llvm-optzns");
  4310. if (Output.getType() == types::TY_Dependencies) {
  4311. // Handled with other dependency code.
  4312. } else if (Output.isFilename()) {
  4313. CmdArgs.push_back("-o");
  4314. CmdArgs.push_back(Output.getFilename());
  4315. } else {
  4316. assert(Output.isNothing() && "Invalid output.");
  4317. }
  4318. addDashXForInput(Args, Input, CmdArgs);
  4319. if (Input.isFilename())
  4320. CmdArgs.push_back(Input.getFilename());
  4321. else
  4322. Input.getInputArg().renderAsInput(Args, CmdArgs);
  4323. Args.AddAllArgs(CmdArgs, options::OPT_undef);
  4324. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  4325. // Optionally embed the -cc1 level arguments into the debug info, for build
  4326. // analysis.
  4327. if (getToolChain().UseDwarfDebugFlags()) {
  4328. ArgStringList OriginalArgs;
  4329. for (const auto &Arg : Args)
  4330. Arg->render(Args, OriginalArgs);
  4331. SmallString<256> Flags;
  4332. Flags += Exec;
  4333. for (const char *OriginalArg : OriginalArgs) {
  4334. SmallString<128> EscapedArg;
  4335. EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
  4336. Flags += " ";
  4337. Flags += EscapedArg;
  4338. }
  4339. CmdArgs.push_back("-dwarf-debug-flags");
  4340. CmdArgs.push_back(Args.MakeArgString(Flags));
  4341. }
  4342. // Add the split debug info name to the command lines here so we
  4343. // can propagate it to the backend.
  4344. bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
  4345. getToolChain().getTriple().isOSLinux() &&
  4346. (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA) ||
  4347. isa<BackendJobAction>(JA));
  4348. const char *SplitDwarfOut;
  4349. if (SplitDwarf) {
  4350. CmdArgs.push_back("-split-dwarf-file");
  4351. SplitDwarfOut = SplitDebugName(Args, Input);
  4352. CmdArgs.push_back(SplitDwarfOut);
  4353. }
  4354. // Host-side cuda compilation receives device-side outputs as Inputs[1...].
  4355. // Include them with -fcuda-include-gpubinary.
  4356. if (IsCuda && Inputs.size() > 1)
  4357. for (InputInfoList::const_iterator it = std::next(Inputs.begin()),
  4358. ie = Inputs.end();
  4359. it != ie; ++it) {
  4360. CmdArgs.push_back("-fcuda-include-gpubinary");
  4361. CmdArgs.push_back(it->getFilename());
  4362. }
  4363. // Finally add the compile command to the compilation.
  4364. if (Args.hasArg(options::OPT__SLASH_fallback) &&
  4365. Output.getType() == types::TY_Object &&
  4366. (InputType == types::TY_C || InputType == types::TY_CXX)) {
  4367. auto CLCommand =
  4368. getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
  4369. C.addCommand(llvm::make_unique<FallbackCommand>(JA, *this, Exec, CmdArgs,
  4370. std::move(CLCommand)));
  4371. } else {
  4372. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  4373. }
  4374. // Handle the debug info splitting at object creation time if we're
  4375. // creating an object.
  4376. // TODO: Currently only works on linux with newer objcopy.
  4377. if (SplitDwarf && !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA))
  4378. SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
  4379. if (Arg *A = Args.getLastArg(options::OPT_pg))
  4380. if (Args.hasArg(options::OPT_fomit_frame_pointer))
  4381. D.Diag(diag::err_drv_argument_not_allowed_with) << "-fomit-frame-pointer"
  4382. << A->getAsString(Args);
  4383. // Claim some arguments which clang supports automatically.
  4384. // -fpch-preprocess is used with gcc to add a special marker in the output to
  4385. // include the PCH file. Clang's PTH solution is completely transparent, so we
  4386. // do not need to deal with it at all.
  4387. Args.ClaimAllArgs(options::OPT_fpch_preprocess);
  4388. // Claim some arguments which clang doesn't support, but we don't
  4389. // care to warn the user about.
  4390. Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
  4391. Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
  4392. // Disable warnings for clang -E -emit-llvm foo.c
  4393. Args.ClaimAllArgs(options::OPT_emit_llvm);
  4394. }
  4395. /// Add options related to the Objective-C runtime/ABI.
  4396. ///
  4397. /// Returns true if the runtime is non-fragile.
  4398. ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
  4399. ArgStringList &cmdArgs,
  4400. RewriteKind rewriteKind) const {
  4401. // Look for the controlling runtime option.
  4402. Arg *runtimeArg =
  4403. args.getLastArg(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
  4404. options::OPT_fobjc_runtime_EQ);
  4405. // Just forward -fobjc-runtime= to the frontend. This supercedes
  4406. // options about fragility.
  4407. if (runtimeArg &&
  4408. runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
  4409. ObjCRuntime runtime;
  4410. StringRef value = runtimeArg->getValue();
  4411. if (runtime.tryParse(value)) {
  4412. getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
  4413. << value;
  4414. }
  4415. runtimeArg->render(args, cmdArgs);
  4416. return runtime;
  4417. }
  4418. // Otherwise, we'll need the ABI "version". Version numbers are
  4419. // slightly confusing for historical reasons:
  4420. // 1 - Traditional "fragile" ABI
  4421. // 2 - Non-fragile ABI, version 1
  4422. // 3 - Non-fragile ABI, version 2
  4423. unsigned objcABIVersion = 1;
  4424. // If -fobjc-abi-version= is present, use that to set the version.
  4425. if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
  4426. StringRef value = abiArg->getValue();
  4427. if (value == "1")
  4428. objcABIVersion = 1;
  4429. else if (value == "2")
  4430. objcABIVersion = 2;
  4431. else if (value == "3")
  4432. objcABIVersion = 3;
  4433. else
  4434. getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) << value;
  4435. } else {
  4436. // Otherwise, determine if we are using the non-fragile ABI.
  4437. bool nonFragileABIIsDefault =
  4438. (rewriteKind == RK_NonFragile ||
  4439. (rewriteKind == RK_None &&
  4440. getToolChain().IsObjCNonFragileABIDefault()));
  4441. if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
  4442. options::OPT_fno_objc_nonfragile_abi,
  4443. nonFragileABIIsDefault)) {
  4444. // Determine the non-fragile ABI version to use.
  4445. #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
  4446. unsigned nonFragileABIVersion = 1;
  4447. #else
  4448. unsigned nonFragileABIVersion = 2;
  4449. #endif
  4450. if (Arg *abiArg =
  4451. args.getLastArg(options::OPT_fobjc_nonfragile_abi_version_EQ)) {
  4452. StringRef value = abiArg->getValue();
  4453. if (value == "1")
  4454. nonFragileABIVersion = 1;
  4455. else if (value == "2")
  4456. nonFragileABIVersion = 2;
  4457. else
  4458. getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
  4459. << value;
  4460. }
  4461. objcABIVersion = 1 + nonFragileABIVersion;
  4462. } else {
  4463. objcABIVersion = 1;
  4464. }
  4465. }
  4466. // We don't actually care about the ABI version other than whether
  4467. // it's non-fragile.
  4468. bool isNonFragile = objcABIVersion != 1;
  4469. // If we have no runtime argument, ask the toolchain for its default runtime.
  4470. // However, the rewriter only really supports the Mac runtime, so assume that.
  4471. ObjCRuntime runtime;
  4472. if (!runtimeArg) {
  4473. switch (rewriteKind) {
  4474. case RK_None:
  4475. runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
  4476. break;
  4477. case RK_Fragile:
  4478. runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
  4479. break;
  4480. case RK_NonFragile:
  4481. runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
  4482. break;
  4483. }
  4484. // -fnext-runtime
  4485. } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
  4486. // On Darwin, make this use the default behavior for the toolchain.
  4487. if (getToolChain().getTriple().isOSDarwin()) {
  4488. runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
  4489. // Otherwise, build for a generic macosx port.
  4490. } else {
  4491. runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
  4492. }
  4493. // -fgnu-runtime
  4494. } else {
  4495. assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
  4496. // Legacy behaviour is to target the gnustep runtime if we are i
  4497. // non-fragile mode or the GCC runtime in fragile mode.
  4498. if (isNonFragile)
  4499. runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1, 6));
  4500. else
  4501. runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
  4502. }
  4503. cmdArgs.push_back(
  4504. args.MakeArgString("-fobjc-runtime=" + runtime.getAsString()));
  4505. return runtime;
  4506. }
  4507. static bool maybeConsumeDash(const std::string &EH, size_t &I) {
  4508. bool HaveDash = (I + 1 < EH.size() && EH[I + 1] == '-');
  4509. I += HaveDash;
  4510. return !HaveDash;
  4511. }
  4512. struct EHFlags {
  4513. EHFlags() : Synch(false), Asynch(false), NoExceptC(false) {}
  4514. bool Synch;
  4515. bool Asynch;
  4516. bool NoExceptC;
  4517. };
  4518. /// /EH controls whether to run destructor cleanups when exceptions are
  4519. /// thrown. There are three modifiers:
  4520. /// - s: Cleanup after "synchronous" exceptions, aka C++ exceptions.
  4521. /// - a: Cleanup after "asynchronous" exceptions, aka structured exceptions.
  4522. /// The 'a' modifier is unimplemented and fundamentally hard in LLVM IR.
  4523. /// - c: Assume that extern "C" functions are implicitly noexcept. This
  4524. /// modifier is an optimization, so we ignore it for now.
  4525. /// The default is /EHs-c-, meaning cleanups are disabled.
  4526. static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
  4527. EHFlags EH;
  4528. std::vector<std::string> EHArgs =
  4529. Args.getAllArgValues(options::OPT__SLASH_EH);
  4530. for (auto EHVal : EHArgs) {
  4531. for (size_t I = 0, E = EHVal.size(); I != E; ++I) {
  4532. switch (EHVal[I]) {
  4533. case 'a':
  4534. EH.Asynch = maybeConsumeDash(EHVal, I);
  4535. continue;
  4536. case 'c':
  4537. EH.NoExceptC = maybeConsumeDash(EHVal, I);
  4538. continue;
  4539. case 's':
  4540. EH.Synch = maybeConsumeDash(EHVal, I);
  4541. continue;
  4542. default:
  4543. break;
  4544. }
  4545. D.Diag(clang::diag::err_drv_invalid_value) << "/EH" << EHVal;
  4546. break;
  4547. }
  4548. }
  4549. // FIXME: Disable C++ EH completely, until it becomes more reliable. Users
  4550. // can use -Xclang to manually enable C++ EH until then.
  4551. EH = EHFlags();
  4552. return EH;
  4553. }
  4554. void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
  4555. unsigned RTOptionID = options::OPT__SLASH_MT;
  4556. if (Args.hasArg(options::OPT__SLASH_LDd))
  4557. // The /LDd option implies /MTd. The dependent lib part can be overridden,
  4558. // but defining _DEBUG is sticky.
  4559. RTOptionID = options::OPT__SLASH_MTd;
  4560. if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
  4561. RTOptionID = A->getOption().getID();
  4562. switch (RTOptionID) {
  4563. case options::OPT__SLASH_MD:
  4564. if (Args.hasArg(options::OPT__SLASH_LDd))
  4565. CmdArgs.push_back("-D_DEBUG");
  4566. CmdArgs.push_back("-D_MT");
  4567. CmdArgs.push_back("-D_DLL");
  4568. CmdArgs.push_back("--dependent-lib=msvcrt");
  4569. break;
  4570. case options::OPT__SLASH_MDd:
  4571. CmdArgs.push_back("-D_DEBUG");
  4572. CmdArgs.push_back("-D_MT");
  4573. CmdArgs.push_back("-D_DLL");
  4574. CmdArgs.push_back("--dependent-lib=msvcrtd");
  4575. break;
  4576. case options::OPT__SLASH_MT:
  4577. if (Args.hasArg(options::OPT__SLASH_LDd))
  4578. CmdArgs.push_back("-D_DEBUG");
  4579. CmdArgs.push_back("-D_MT");
  4580. CmdArgs.push_back("--dependent-lib=libcmt");
  4581. break;
  4582. case options::OPT__SLASH_MTd:
  4583. CmdArgs.push_back("-D_DEBUG");
  4584. CmdArgs.push_back("-D_MT");
  4585. CmdArgs.push_back("--dependent-lib=libcmtd");
  4586. break;
  4587. default:
  4588. llvm_unreachable("Unexpected option ID.");
  4589. }
  4590. // This provides POSIX compatibility (maps 'open' to '_open'), which most
  4591. // users want. The /Za flag to cl.exe turns this off, but it's not
  4592. // implemented in clang.
  4593. CmdArgs.push_back("--dependent-lib=oldnames");
  4594. // Both /showIncludes and /E (and /EP) write to stdout. Allowing both
  4595. // would produce interleaved output, so ignore /showIncludes in such cases.
  4596. if (!Args.hasArg(options::OPT_E) && !Args.hasArg(options::OPT__SLASH_EP))
  4597. if (Arg *A = Args.getLastArg(options::OPT_show_includes))
  4598. A->render(Args, CmdArgs);
  4599. // This controls whether or not we emit RTTI data for polymorphic types.
  4600. if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
  4601. /*default=*/false))
  4602. CmdArgs.push_back("-fno-rtti-data");
  4603. const Driver &D = getToolChain().getDriver();
  4604. EHFlags EH = parseClangCLEHFlags(D, Args);
  4605. // FIXME: Do something with NoExceptC.
  4606. if (EH.Synch || EH.Asynch) {
  4607. CmdArgs.push_back("-fcxx-exceptions");
  4608. CmdArgs.push_back("-fexceptions");
  4609. }
  4610. // /EP should expand to -E -P.
  4611. if (Args.hasArg(options::OPT__SLASH_EP)) {
  4612. CmdArgs.push_back("-E");
  4613. CmdArgs.push_back("-P");
  4614. }
  4615. unsigned VolatileOptionID;
  4616. if (getToolChain().getArch() == llvm::Triple::x86_64 ||
  4617. getToolChain().getArch() == llvm::Triple::x86)
  4618. VolatileOptionID = options::OPT__SLASH_volatile_ms;
  4619. else
  4620. VolatileOptionID = options::OPT__SLASH_volatile_iso;
  4621. if (Arg *A = Args.getLastArg(options::OPT__SLASH_volatile_Group))
  4622. VolatileOptionID = A->getOption().getID();
  4623. if (VolatileOptionID == options::OPT__SLASH_volatile_ms)
  4624. CmdArgs.push_back("-fms-volatile");
  4625. Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
  4626. Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
  4627. if (MostGeneralArg && BestCaseArg)
  4628. D.Diag(clang::diag::err_drv_argument_not_allowed_with)
  4629. << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
  4630. if (MostGeneralArg) {
  4631. Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
  4632. Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
  4633. Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
  4634. Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
  4635. Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
  4636. if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
  4637. D.Diag(clang::diag::err_drv_argument_not_allowed_with)
  4638. << FirstConflict->getAsString(Args)
  4639. << SecondConflict->getAsString(Args);
  4640. if (SingleArg)
  4641. CmdArgs.push_back("-fms-memptr-rep=single");
  4642. else if (MultipleArg)
  4643. CmdArgs.push_back("-fms-memptr-rep=multiple");
  4644. else
  4645. CmdArgs.push_back("-fms-memptr-rep=virtual");
  4646. }
  4647. if (Arg *A = Args.getLastArg(options::OPT_vtordisp_mode_EQ))
  4648. A->render(Args, CmdArgs);
  4649. if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
  4650. CmdArgs.push_back("-fdiagnostics-format");
  4651. if (Args.hasArg(options::OPT__SLASH_fallback))
  4652. CmdArgs.push_back("msvc-fallback");
  4653. else
  4654. CmdArgs.push_back("msvc");
  4655. }
  4656. }
  4657. visualstudio::Compiler *Clang::getCLFallback() const {
  4658. if (!CLFallback)
  4659. CLFallback.reset(new visualstudio::Compiler(getToolChain()));
  4660. return CLFallback.get();
  4661. }
  4662. void ClangAs::AddMIPSTargetArgs(const ArgList &Args,
  4663. ArgStringList &CmdArgs) const {
  4664. StringRef CPUName;
  4665. StringRef ABIName;
  4666. const llvm::Triple &Triple = getToolChain().getTriple();
  4667. mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
  4668. CmdArgs.push_back("-target-abi");
  4669. CmdArgs.push_back(ABIName.data());
  4670. }
  4671. void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
  4672. const InputInfo &Output, const InputInfoList &Inputs,
  4673. const ArgList &Args,
  4674. const char *LinkingOutput) const {
  4675. ArgStringList CmdArgs;
  4676. assert(Inputs.size() == 1 && "Unexpected number of inputs.");
  4677. const InputInfo &Input = Inputs[0];
  4678. // Don't warn about "clang -w -c foo.s"
  4679. Args.ClaimAllArgs(options::OPT_w);
  4680. // and "clang -emit-llvm -c foo.s"
  4681. Args.ClaimAllArgs(options::OPT_emit_llvm);
  4682. claimNoWarnArgs(Args);
  4683. // Invoke ourselves in -cc1as mode.
  4684. //
  4685. // FIXME: Implement custom jobs for internal actions.
  4686. CmdArgs.push_back("-cc1as");
  4687. // Add the "effective" target triple.
  4688. CmdArgs.push_back("-triple");
  4689. std::string TripleStr =
  4690. getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
  4691. CmdArgs.push_back(Args.MakeArgString(TripleStr));
  4692. // Set the output mode, we currently only expect to be used as a real
  4693. // assembler.
  4694. CmdArgs.push_back("-filetype");
  4695. CmdArgs.push_back("obj");
  4696. // Set the main file name, so that debug info works even with
  4697. // -save-temps or preprocessed assembly.
  4698. CmdArgs.push_back("-main-file-name");
  4699. CmdArgs.push_back(Clang::getBaseInputName(Args, Input));
  4700. // Add the target cpu
  4701. const llvm::Triple Triple(TripleStr);
  4702. std::string CPU = getCPUName(Args, Triple, /*FromAs*/ true);
  4703. if (!CPU.empty()) {
  4704. CmdArgs.push_back("-target-cpu");
  4705. CmdArgs.push_back(Args.MakeArgString(CPU));
  4706. }
  4707. // Add the target features
  4708. const Driver &D = getToolChain().getDriver();
  4709. getTargetFeatures(D, Triple, Args, CmdArgs, true);
  4710. // Ignore explicit -force_cpusubtype_ALL option.
  4711. (void)Args.hasArg(options::OPT_force__cpusubtype__ALL);
  4712. // Pass along any -I options so we get proper .include search paths.
  4713. Args.AddAllArgs(CmdArgs, options::OPT_I_Group);
  4714. // Determine the original source input.
  4715. const Action *SourceAction = &JA;
  4716. while (SourceAction->getKind() != Action::InputClass) {
  4717. assert(!SourceAction->getInputs().empty() && "unexpected root action!");
  4718. SourceAction = SourceAction->getInputs()[0];
  4719. }
  4720. // Forward -g and handle debug info related flags, assuming we are dealing
  4721. // with an actual assembly file.
  4722. if (SourceAction->getType() == types::TY_Asm ||
  4723. SourceAction->getType() == types::TY_PP_Asm) {
  4724. Args.ClaimAllArgs(options::OPT_g_Group);
  4725. if (Arg *A = Args.getLastArg(options::OPT_g_Group))
  4726. if (!A->getOption().matches(options::OPT_g0))
  4727. CmdArgs.push_back("-g");
  4728. if (Args.hasArg(options::OPT_gdwarf_2))
  4729. CmdArgs.push_back("-gdwarf-2");
  4730. if (Args.hasArg(options::OPT_gdwarf_3))
  4731. CmdArgs.push_back("-gdwarf-3");
  4732. if (Args.hasArg(options::OPT_gdwarf_4))
  4733. CmdArgs.push_back("-gdwarf-4");
  4734. // Add the -fdebug-compilation-dir flag if needed.
  4735. addDebugCompDirArg(Args, CmdArgs);
  4736. // Set the AT_producer to the clang version when using the integrated
  4737. // assembler on assembly source files.
  4738. CmdArgs.push_back("-dwarf-debug-producer");
  4739. CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
  4740. }
  4741. // Optionally embed the -cc1as level arguments into the debug info, for build
  4742. // analysis.
  4743. if (getToolChain().UseDwarfDebugFlags()) {
  4744. ArgStringList OriginalArgs;
  4745. for (const auto &Arg : Args)
  4746. Arg->render(Args, OriginalArgs);
  4747. SmallString<256> Flags;
  4748. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  4749. Flags += Exec;
  4750. for (const char *OriginalArg : OriginalArgs) {
  4751. SmallString<128> EscapedArg;
  4752. EscapeSpacesAndBackslashes(OriginalArg, EscapedArg);
  4753. Flags += " ";
  4754. Flags += EscapedArg;
  4755. }
  4756. CmdArgs.push_back("-dwarf-debug-flags");
  4757. CmdArgs.push_back(Args.MakeArgString(Flags));
  4758. }
  4759. // FIXME: Add -static support, once we have it.
  4760. // Add target specific flags.
  4761. switch (getToolChain().getArch()) {
  4762. default:
  4763. break;
  4764. case llvm::Triple::mips:
  4765. case llvm::Triple::mipsel:
  4766. case llvm::Triple::mips64:
  4767. case llvm::Triple::mips64el:
  4768. AddMIPSTargetArgs(Args, CmdArgs);
  4769. break;
  4770. }
  4771. // Consume all the warning flags. Usually this would be handled more
  4772. // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
  4773. // doesn't handle that so rather than warning about unused flags that are
  4774. // actually used, we'll lie by omission instead.
  4775. // FIXME: Stop lying and consume only the appropriate driver flags
  4776. for (const Arg *A : Args.filtered(options::OPT_W_Group))
  4777. A->claim();
  4778. CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
  4779. getToolChain().getDriver());
  4780. Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
  4781. assert(Output.isFilename() && "Unexpected lipo output.");
  4782. CmdArgs.push_back("-o");
  4783. CmdArgs.push_back(Output.getFilename());
  4784. assert(Input.isFilename() && "Invalid input.");
  4785. CmdArgs.push_back(Input.getFilename());
  4786. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  4787. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  4788. // Handle the debug info splitting at object creation time if we're
  4789. // creating an object.
  4790. // TODO: Currently only works on linux with newer objcopy.
  4791. if (Args.hasArg(options::OPT_gsplit_dwarf) &&
  4792. getToolChain().getTriple().isOSLinux())
  4793. SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
  4794. SplitDebugName(Args, Input));
  4795. }
  4796. void GnuTool::anchor() {}
  4797. void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
  4798. const InputInfo &Output,
  4799. const InputInfoList &Inputs, const ArgList &Args,
  4800. const char *LinkingOutput) const {
  4801. const Driver &D = getToolChain().getDriver();
  4802. ArgStringList CmdArgs;
  4803. for (const auto &A : Args) {
  4804. if (forwardToGCC(A->getOption())) {
  4805. // Don't forward any -g arguments to assembly steps.
  4806. if (isa<AssembleJobAction>(JA) &&
  4807. A->getOption().matches(options::OPT_g_Group))
  4808. continue;
  4809. // Don't forward any -W arguments to assembly and link steps.
  4810. if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
  4811. A->getOption().matches(options::OPT_W_Group))
  4812. continue;
  4813. // It is unfortunate that we have to claim here, as this means
  4814. // we will basically never report anything interesting for
  4815. // platforms using a generic gcc, even if we are just using gcc
  4816. // to get to the assembler.
  4817. A->claim();
  4818. A->render(Args, CmdArgs);
  4819. }
  4820. }
  4821. RenderExtraToolArgs(JA, CmdArgs);
  4822. // If using a driver driver, force the arch.
  4823. if (getToolChain().getTriple().isOSDarwin()) {
  4824. CmdArgs.push_back("-arch");
  4825. CmdArgs.push_back(
  4826. Args.MakeArgString(getToolChain().getDefaultUniversalArchName()));
  4827. }
  4828. // Try to force gcc to match the tool chain we want, if we recognize
  4829. // the arch.
  4830. //
  4831. // FIXME: The triple class should directly provide the information we want
  4832. // here.
  4833. const llvm::Triple::ArchType Arch = getToolChain().getArch();
  4834. if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
  4835. CmdArgs.push_back("-m32");
  4836. else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 ||
  4837. Arch == llvm::Triple::ppc64le)
  4838. CmdArgs.push_back("-m64");
  4839. if (Output.isFilename()) {
  4840. CmdArgs.push_back("-o");
  4841. CmdArgs.push_back(Output.getFilename());
  4842. } else {
  4843. assert(Output.isNothing() && "Unexpected output");
  4844. CmdArgs.push_back("-fsyntax-only");
  4845. }
  4846. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  4847. // Only pass -x if gcc will understand it; otherwise hope gcc
  4848. // understands the suffix correctly. The main use case this would go
  4849. // wrong in is for linker inputs if they happened to have an odd
  4850. // suffix; really the only way to get this to happen is a command
  4851. // like '-x foobar a.c' which will treat a.c like a linker input.
  4852. //
  4853. // FIXME: For the linker case specifically, can we safely convert
  4854. // inputs into '-Wl,' options?
  4855. for (const auto &II : Inputs) {
  4856. // Don't try to pass LLVM or AST inputs to a generic gcc.
  4857. if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
  4858. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  4859. D.Diag(diag::err_drv_no_linker_llvm_support)
  4860. << getToolChain().getTripleString();
  4861. else if (II.getType() == types::TY_AST)
  4862. D.Diag(diag::err_drv_no_ast_support) << getToolChain().getTripleString();
  4863. else if (II.getType() == types::TY_ModuleFile)
  4864. D.Diag(diag::err_drv_no_module_support)
  4865. << getToolChain().getTripleString();
  4866. if (types::canTypeBeUserSpecified(II.getType())) {
  4867. CmdArgs.push_back("-x");
  4868. CmdArgs.push_back(types::getTypeName(II.getType()));
  4869. }
  4870. if (II.isFilename())
  4871. CmdArgs.push_back(II.getFilename());
  4872. else {
  4873. const Arg &A = II.getInputArg();
  4874. // Reverse translate some rewritten options.
  4875. if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
  4876. CmdArgs.push_back("-lstdc++");
  4877. continue;
  4878. }
  4879. // Don't render as input, we need gcc to do the translations.
  4880. A.render(Args, CmdArgs);
  4881. }
  4882. }
  4883. const std::string customGCCName = D.getCCCGenericGCCName();
  4884. const char *GCCName;
  4885. if (!customGCCName.empty())
  4886. GCCName = customGCCName.c_str();
  4887. else if (D.CCCIsCXX()) {
  4888. GCCName = "g++";
  4889. } else
  4890. GCCName = "gcc";
  4891. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
  4892. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  4893. }
  4894. void gcc::Preprocessor::RenderExtraToolArgs(const JobAction &JA,
  4895. ArgStringList &CmdArgs) const {
  4896. CmdArgs.push_back("-E");
  4897. }
  4898. void gcc::Compiler::RenderExtraToolArgs(const JobAction &JA,
  4899. ArgStringList &CmdArgs) const {
  4900. const Driver &D = getToolChain().getDriver();
  4901. switch (JA.getType()) {
  4902. // If -flto, etc. are present then make sure not to force assembly output.
  4903. case types::TY_LLVM_IR:
  4904. case types::TY_LTO_IR:
  4905. case types::TY_LLVM_BC:
  4906. case types::TY_LTO_BC:
  4907. CmdArgs.push_back("-c");
  4908. break;
  4909. case types::TY_PP_Asm:
  4910. CmdArgs.push_back("-S");
  4911. break;
  4912. case types::TY_Nothing:
  4913. CmdArgs.push_back("-fsyntax-only");
  4914. break;
  4915. default:
  4916. D.Diag(diag::err_drv_invalid_gcc_output_type) << getTypeName(JA.getType());
  4917. }
  4918. }
  4919. void gcc::Linker::RenderExtraToolArgs(const JobAction &JA,
  4920. ArgStringList &CmdArgs) const {
  4921. // The types are (hopefully) good enough.
  4922. }
  4923. // Hexagon tools start.
  4924. void hexagon::Assembler::RenderExtraToolArgs(const JobAction &JA,
  4925. ArgStringList &CmdArgs) const {}
  4926. void hexagon::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  4927. const InputInfo &Output,
  4928. const InputInfoList &Inputs,
  4929. const ArgList &Args,
  4930. const char *LinkingOutput) const {
  4931. claimNoWarnArgs(Args);
  4932. const Driver &D = getToolChain().getDriver();
  4933. ArgStringList CmdArgs;
  4934. std::string MarchString = "-march=";
  4935. MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
  4936. CmdArgs.push_back(Args.MakeArgString(MarchString));
  4937. RenderExtraToolArgs(JA, CmdArgs);
  4938. if (Output.isFilename()) {
  4939. CmdArgs.push_back("-o");
  4940. CmdArgs.push_back(Output.getFilename());
  4941. } else {
  4942. assert(Output.isNothing() && "Unexpected output");
  4943. CmdArgs.push_back("-fsyntax-only");
  4944. }
  4945. if (const char *v = toolchains::Hexagon_TC::GetSmallDataThreshold(Args))
  4946. CmdArgs.push_back(Args.MakeArgString(std::string("-G") + v));
  4947. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  4948. // Only pass -x if gcc will understand it; otherwise hope gcc
  4949. // understands the suffix correctly. The main use case this would go
  4950. // wrong in is for linker inputs if they happened to have an odd
  4951. // suffix; really the only way to get this to happen is a command
  4952. // like '-x foobar a.c' which will treat a.c like a linker input.
  4953. //
  4954. // FIXME: For the linker case specifically, can we safely convert
  4955. // inputs into '-Wl,' options?
  4956. for (const auto &II : Inputs) {
  4957. // Don't try to pass LLVM or AST inputs to a generic gcc.
  4958. if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
  4959. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  4960. D.Diag(clang::diag::err_drv_no_linker_llvm_support)
  4961. << getToolChain().getTripleString();
  4962. else if (II.getType() == types::TY_AST)
  4963. D.Diag(clang::diag::err_drv_no_ast_support)
  4964. << getToolChain().getTripleString();
  4965. else if (II.getType() == types::TY_ModuleFile)
  4966. D.Diag(diag::err_drv_no_module_support)
  4967. << getToolChain().getTripleString();
  4968. if (II.isFilename())
  4969. CmdArgs.push_back(II.getFilename());
  4970. else
  4971. // Don't render as input, we need gcc to do the translations.
  4972. // FIXME: Pranav: What is this ?
  4973. II.getInputArg().render(Args, CmdArgs);
  4974. }
  4975. const char *GCCName = "hexagon-as";
  4976. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
  4977. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  4978. }
  4979. void hexagon::Linker::RenderExtraToolArgs(const JobAction &JA,
  4980. ArgStringList &CmdArgs) const {
  4981. // The types are (hopefully) good enough.
  4982. }
  4983. static void constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
  4984. const toolchains::Hexagon_TC &ToolChain,
  4985. const InputInfo &Output,
  4986. const InputInfoList &Inputs,
  4987. const ArgList &Args,
  4988. ArgStringList &CmdArgs,
  4989. const char *LinkingOutput) {
  4990. const Driver &D = ToolChain.getDriver();
  4991. //----------------------------------------------------------------------------
  4992. //
  4993. //----------------------------------------------------------------------------
  4994. bool hasStaticArg = Args.hasArg(options::OPT_static);
  4995. bool buildingLib = Args.hasArg(options::OPT_shared);
  4996. bool buildPIE = Args.hasArg(options::OPT_pie);
  4997. bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
  4998. bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
  4999. bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
  5000. bool useG0 = false;
  5001. bool useShared = buildingLib && !hasStaticArg;
  5002. //----------------------------------------------------------------------------
  5003. // Silence warnings for various options
  5004. //----------------------------------------------------------------------------
  5005. Args.ClaimAllArgs(options::OPT_g_Group);
  5006. Args.ClaimAllArgs(options::OPT_emit_llvm);
  5007. Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
  5008. // handled somewhere else.
  5009. Args.ClaimAllArgs(options::OPT_static_libgcc);
  5010. //----------------------------------------------------------------------------
  5011. //
  5012. //----------------------------------------------------------------------------
  5013. for (const auto &Opt : ToolChain.ExtraOpts)
  5014. CmdArgs.push_back(Opt.c_str());
  5015. std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
  5016. CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
  5017. if (buildingLib) {
  5018. CmdArgs.push_back("-shared");
  5019. CmdArgs.push_back("-call_shared"); // should be the default, but doing as
  5020. // hexagon-gcc does
  5021. }
  5022. if (hasStaticArg)
  5023. CmdArgs.push_back("-static");
  5024. if (buildPIE && !buildingLib)
  5025. CmdArgs.push_back("-pie");
  5026. if (const char *v = toolchains::Hexagon_TC::GetSmallDataThreshold(Args)) {
  5027. CmdArgs.push_back(Args.MakeArgString(std::string("-G") + v));
  5028. useG0 = toolchains::Hexagon_TC::UsesG0(v);
  5029. }
  5030. //----------------------------------------------------------------------------
  5031. //
  5032. //----------------------------------------------------------------------------
  5033. CmdArgs.push_back("-o");
  5034. CmdArgs.push_back(Output.getFilename());
  5035. const std::string MarchSuffix = "/" + MarchString;
  5036. const std::string G0Suffix = "/G0";
  5037. const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
  5038. const std::string RootDir =
  5039. toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir, Args) + "/";
  5040. const std::string StartFilesDir =
  5041. RootDir + "hexagon/lib" + (useG0 ? MarchG0Suffix : MarchSuffix);
  5042. //----------------------------------------------------------------------------
  5043. // moslib
  5044. //----------------------------------------------------------------------------
  5045. std::vector<std::string> oslibs;
  5046. bool hasStandalone = false;
  5047. for (const Arg *A : Args.filtered(options::OPT_moslib_EQ)) {
  5048. A->claim();
  5049. oslibs.emplace_back(A->getValue());
  5050. hasStandalone = hasStandalone || (oslibs.back() == "standalone");
  5051. }
  5052. if (oslibs.empty()) {
  5053. oslibs.push_back("standalone");
  5054. hasStandalone = true;
  5055. }
  5056. //----------------------------------------------------------------------------
  5057. // Start Files
  5058. //----------------------------------------------------------------------------
  5059. if (incStdLib && incStartFiles) {
  5060. if (!buildingLib) {
  5061. if (hasStandalone) {
  5062. CmdArgs.push_back(
  5063. Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
  5064. }
  5065. CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
  5066. }
  5067. std::string initObj = useShared ? "/initS.o" : "/init.o";
  5068. CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
  5069. }
  5070. //----------------------------------------------------------------------------
  5071. // Library Search Paths
  5072. //----------------------------------------------------------------------------
  5073. const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
  5074. for (const auto &LibPath : LibPaths)
  5075. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
  5076. //----------------------------------------------------------------------------
  5077. //
  5078. //----------------------------------------------------------------------------
  5079. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5080. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5081. Args.AddAllArgs(CmdArgs, options::OPT_s);
  5082. Args.AddAllArgs(CmdArgs, options::OPT_t);
  5083. Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
  5084. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  5085. //----------------------------------------------------------------------------
  5086. // Libraries
  5087. //----------------------------------------------------------------------------
  5088. if (incStdLib && incDefLibs) {
  5089. if (D.CCCIsCXX()) {
  5090. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  5091. CmdArgs.push_back("-lm");
  5092. }
  5093. CmdArgs.push_back("--start-group");
  5094. if (!buildingLib) {
  5095. for (const std::string &Lib : oslibs)
  5096. CmdArgs.push_back(Args.MakeArgString("-l" + Lib));
  5097. CmdArgs.push_back("-lc");
  5098. }
  5099. CmdArgs.push_back("-lgcc");
  5100. CmdArgs.push_back("--end-group");
  5101. }
  5102. //----------------------------------------------------------------------------
  5103. // End files
  5104. //----------------------------------------------------------------------------
  5105. if (incStdLib && incStartFiles) {
  5106. std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
  5107. CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
  5108. }
  5109. }
  5110. void hexagon::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  5111. const InputInfo &Output,
  5112. const InputInfoList &Inputs,
  5113. const ArgList &Args,
  5114. const char *LinkingOutput) const {
  5115. const toolchains::Hexagon_TC &ToolChain =
  5116. static_cast<const toolchains::Hexagon_TC &>(getToolChain());
  5117. ArgStringList CmdArgs;
  5118. constructHexagonLinkArgs(C, JA, ToolChain, Output, Inputs, Args, CmdArgs,
  5119. LinkingOutput);
  5120. std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
  5121. C.addCommand(llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Linker),
  5122. CmdArgs));
  5123. }
  5124. // Hexagon tools end.
  5125. const std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) {
  5126. std::string MArch;
  5127. if (!Arch.empty())
  5128. MArch = Arch;
  5129. else
  5130. MArch = Triple.getArchName();
  5131. MArch = StringRef(MArch).lower();
  5132. // Handle -march=native.
  5133. if (MArch == "native") {
  5134. std::string CPU = llvm::sys::getHostCPUName();
  5135. if (CPU != "generic") {
  5136. // Translate the native cpu into the architecture suffix for that CPU.
  5137. const char *Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch);
  5138. // If there is no valid architecture suffix for this CPU we don't know how
  5139. // to handle it, so return no architecture.
  5140. if (strcmp(Suffix, "") == 0)
  5141. MArch = "";
  5142. else
  5143. MArch = std::string("arm") + Suffix;
  5144. }
  5145. }
  5146. return MArch;
  5147. }
  5148. /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
  5149. const char *arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) {
  5150. std::string MArch = getARMArch(Arch, Triple);
  5151. // getARMCPUForArch defaults to the triple if MArch is empty, but empty MArch
  5152. // here means an -march=native that we can't handle, so instead return no CPU.
  5153. if (MArch.empty())
  5154. return "";
  5155. // We need to return an empty string here on invalid MArch values as the
  5156. // various places that call this function can't cope with a null result.
  5157. const char *result = Triple.getARMCPUForArch(MArch);
  5158. if (result)
  5159. return result;
  5160. else
  5161. return "";
  5162. }
  5163. /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
  5164. std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch,
  5165. const llvm::Triple &Triple) {
  5166. // FIXME: Warn on inconsistent use of -mcpu and -march.
  5167. // If we have -mcpu=, use that.
  5168. if (!CPU.empty()) {
  5169. std::string MCPU = StringRef(CPU).lower();
  5170. // Handle -mcpu=native.
  5171. if (MCPU == "native")
  5172. return llvm::sys::getHostCPUName();
  5173. else
  5174. return MCPU;
  5175. }
  5176. return getARMCPUForMArch(Arch, Triple);
  5177. }
  5178. /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
  5179. /// CPU (or Arch, if CPU is generic).
  5180. // FIXME: This is redundant with -mcpu, why does LLVM use this.
  5181. const char *arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch) {
  5182. if (CPU == "generic" &&
  5183. llvm::ARMTargetParser::parseArch(Arch) == llvm::ARM::AK_ARMV8_1A)
  5184. return "v8.1a";
  5185. unsigned ArchKind = llvm::ARMTargetParser::parseCPUArch(CPU);
  5186. if (ArchKind == llvm::ARM::AK_INVALID)
  5187. return "";
  5188. return llvm::ARMTargetParser::getSubArch(ArchKind);
  5189. }
  5190. void arm::appendEBLinkFlags(const ArgList &Args, ArgStringList &CmdArgs,
  5191. const llvm::Triple &Triple) {
  5192. if (Args.hasArg(options::OPT_r))
  5193. return;
  5194. // ARMv7 (and later) and ARMv6-M do not support BE-32, so instruct the linker
  5195. // to generate BE-8 executables.
  5196. if (getARMSubArchVersionNumber(Triple) >= 7 || isARMMProfile(Triple))
  5197. CmdArgs.push_back("--be8");
  5198. }
  5199. mips::NanEncoding mips::getSupportedNanEncoding(StringRef &CPU) {
  5200. return (NanEncoding)llvm::StringSwitch<int>(CPU)
  5201. .Case("mips1", NanLegacy)
  5202. .Case("mips2", NanLegacy)
  5203. .Case("mips3", NanLegacy)
  5204. .Case("mips4", NanLegacy)
  5205. .Case("mips5", NanLegacy)
  5206. .Case("mips32", NanLegacy)
  5207. .Case("mips32r2", NanLegacy)
  5208. .Case("mips32r3", NanLegacy | Nan2008)
  5209. .Case("mips32r5", NanLegacy | Nan2008)
  5210. .Case("mips32r6", Nan2008)
  5211. .Case("mips64", NanLegacy)
  5212. .Case("mips64r2", NanLegacy)
  5213. .Case("mips64r3", NanLegacy | Nan2008)
  5214. .Case("mips64r5", NanLegacy | Nan2008)
  5215. .Case("mips64r6", Nan2008)
  5216. .Default(NanLegacy);
  5217. }
  5218. bool mips::hasMipsAbiArg(const ArgList &Args, const char *Value) {
  5219. Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
  5220. return A && (A->getValue() == StringRef(Value));
  5221. }
  5222. bool mips::isUCLibc(const ArgList &Args) {
  5223. Arg *A = Args.getLastArg(options::OPT_m_libc_Group);
  5224. return A && A->getOption().matches(options::OPT_muclibc);
  5225. }
  5226. bool mips::isNaN2008(const ArgList &Args, const llvm::Triple &Triple) {
  5227. if (Arg *NaNArg = Args.getLastArg(options::OPT_mnan_EQ))
  5228. return llvm::StringSwitch<bool>(NaNArg->getValue())
  5229. .Case("2008", true)
  5230. .Case("legacy", false)
  5231. .Default(false);
  5232. // NaN2008 is the default for MIPS32r6/MIPS64r6.
  5233. return llvm::StringSwitch<bool>(getCPUName(Args, Triple))
  5234. .Cases("mips32r6", "mips64r6", true)
  5235. .Default(false);
  5236. return false;
  5237. }
  5238. bool mips::isFPXXDefault(const llvm::Triple &Triple, StringRef CPUName,
  5239. StringRef ABIName, StringRef FloatABI) {
  5240. if (Triple.getVendor() != llvm::Triple::ImaginationTechnologies &&
  5241. Triple.getVendor() != llvm::Triple::MipsTechnologies)
  5242. return false;
  5243. if (ABIName != "32")
  5244. return false;
  5245. // FPXX shouldn't be used if either -msoft-float or -mfloat-abi=soft is
  5246. // present.
  5247. if (FloatABI == "soft")
  5248. return false;
  5249. return llvm::StringSwitch<bool>(CPUName)
  5250. .Cases("mips2", "mips3", "mips4", "mips5", true)
  5251. .Cases("mips32", "mips32r2", "mips32r3", "mips32r5", true)
  5252. .Cases("mips64", "mips64r2", "mips64r3", "mips64r5", true)
  5253. .Default(false);
  5254. }
  5255. bool mips::shouldUseFPXX(const ArgList &Args, const llvm::Triple &Triple,
  5256. StringRef CPUName, StringRef ABIName,
  5257. StringRef FloatABI) {
  5258. bool UseFPXX = isFPXXDefault(Triple, CPUName, ABIName, FloatABI);
  5259. // FPXX shouldn't be used if -msingle-float is present.
  5260. if (Arg *A = Args.getLastArg(options::OPT_msingle_float,
  5261. options::OPT_mdouble_float))
  5262. if (A->getOption().matches(options::OPT_msingle_float))
  5263. UseFPXX = false;
  5264. return UseFPXX;
  5265. }
  5266. llvm::Triple::ArchType darwin::getArchTypeForMachOArchName(StringRef Str) {
  5267. // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
  5268. // archs which Darwin doesn't use.
  5269. // The matching this routine does is fairly pointless, since it is neither the
  5270. // complete architecture list, nor a reasonable subset. The problem is that
  5271. // historically the driver driver accepts this and also ties its -march=
  5272. // handling to the architecture name, so we need to be careful before removing
  5273. // support for it.
  5274. // This code must be kept in sync with Clang's Darwin specific argument
  5275. // translation.
  5276. return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
  5277. .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
  5278. .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
  5279. .Case("ppc64", llvm::Triple::ppc64)
  5280. .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
  5281. .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
  5282. llvm::Triple::x86)
  5283. .Cases("x86_64", "x86_64h", llvm::Triple::x86_64)
  5284. // This is derived from the driver driver.
  5285. .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
  5286. .Cases("armv7", "armv7em", "armv7k", "armv7m", llvm::Triple::arm)
  5287. .Cases("armv7s", "xscale", llvm::Triple::arm)
  5288. .Case("arm64", llvm::Triple::aarch64)
  5289. .Case("r600", llvm::Triple::r600)
  5290. .Case("amdgcn", llvm::Triple::amdgcn)
  5291. .Case("nvptx", llvm::Triple::nvptx)
  5292. .Case("nvptx64", llvm::Triple::nvptx64)
  5293. .Case("amdil", llvm::Triple::amdil)
  5294. .Case("spir", llvm::Triple::spir)
  5295. .Default(llvm::Triple::UnknownArch);
  5296. }
  5297. void darwin::setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str) {
  5298. const llvm::Triple::ArchType Arch = getArchTypeForMachOArchName(Str);
  5299. T.setArch(Arch);
  5300. if (Str == "x86_64h")
  5301. T.setArchName(Str);
  5302. else if (Str == "armv6m" || Str == "armv7m" || Str == "armv7em") {
  5303. T.setOS(llvm::Triple::UnknownOS);
  5304. T.setObjectFormat(llvm::Triple::MachO);
  5305. }
  5306. }
  5307. const char *Clang::getBaseInputName(const ArgList &Args,
  5308. const InputInfo &Input) {
  5309. return Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput()));
  5310. }
  5311. const char *Clang::getBaseInputStem(const ArgList &Args,
  5312. const InputInfoList &Inputs) {
  5313. const char *Str = getBaseInputName(Args, Inputs[0]);
  5314. if (const char *End = strrchr(Str, '.'))
  5315. return Args.MakeArgString(std::string(Str, End));
  5316. return Str;
  5317. }
  5318. const char *Clang::getDependencyFileName(const ArgList &Args,
  5319. const InputInfoList &Inputs) {
  5320. // FIXME: Think about this more.
  5321. std::string Res;
  5322. if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
  5323. std::string Str(OutputOpt->getValue());
  5324. Res = Str.substr(0, Str.rfind('.'));
  5325. } else {
  5326. Res = getBaseInputStem(Args, Inputs);
  5327. }
  5328. return Args.MakeArgString(Res + ".d");
  5329. }
  5330. void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  5331. const InputInfo &Output,
  5332. const InputInfoList &Inputs,
  5333. const ArgList &Args,
  5334. const char *LinkingOutput) const {
  5335. const ToolChain &ToolChain = getToolChain();
  5336. const Driver &D = ToolChain.getDriver();
  5337. ArgStringList CmdArgs;
  5338. // Silence warning for "clang -g foo.o -o foo"
  5339. Args.ClaimAllArgs(options::OPT_g_Group);
  5340. // and "clang -emit-llvm foo.o -o foo"
  5341. Args.ClaimAllArgs(options::OPT_emit_llvm);
  5342. // and for "clang -w foo.o -o foo". Other warning options are already
  5343. // handled somewhere else.
  5344. Args.ClaimAllArgs(options::OPT_w);
  5345. if (!D.SysRoot.empty())
  5346. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  5347. // CloudABI only supports static linkage.
  5348. CmdArgs.push_back("-Bstatic");
  5349. CmdArgs.push_back("--eh-frame-hdr");
  5350. CmdArgs.push_back("--gc-sections");
  5351. if (Output.isFilename()) {
  5352. CmdArgs.push_back("-o");
  5353. CmdArgs.push_back(Output.getFilename());
  5354. } else {
  5355. assert(Output.isNothing() && "Invalid output.");
  5356. }
  5357. if (!Args.hasArg(options::OPT_nostdlib) &&
  5358. !Args.hasArg(options::OPT_nostartfiles)) {
  5359. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
  5360. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
  5361. }
  5362. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5363. const ToolChain::path_list &Paths = ToolChain.getFilePaths();
  5364. for (const auto &Path : Paths)
  5365. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
  5366. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5367. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5368. Args.AddAllArgs(CmdArgs, options::OPT_s);
  5369. Args.AddAllArgs(CmdArgs, options::OPT_t);
  5370. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  5371. Args.AddAllArgs(CmdArgs, options::OPT_r);
  5372. if (D.IsUsingLTO(Args))
  5373. AddGoldPlugin(ToolChain, Args, CmdArgs);
  5374. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  5375. if (!Args.hasArg(options::OPT_nostdlib) &&
  5376. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5377. if (D.CCCIsCXX())
  5378. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  5379. CmdArgs.push_back("-lc");
  5380. CmdArgs.push_back("-lcompiler_rt");
  5381. }
  5382. if (!Args.hasArg(options::OPT_nostdlib) &&
  5383. !Args.hasArg(options::OPT_nostartfiles))
  5384. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
  5385. const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
  5386. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5387. }
  5388. void darwin::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  5389. const InputInfo &Output,
  5390. const InputInfoList &Inputs,
  5391. const ArgList &Args,
  5392. const char *LinkingOutput) const {
  5393. ArgStringList CmdArgs;
  5394. assert(Inputs.size() == 1 && "Unexpected number of inputs.");
  5395. const InputInfo &Input = Inputs[0];
  5396. // Determine the original source input.
  5397. const Action *SourceAction = &JA;
  5398. while (SourceAction->getKind() != Action::InputClass) {
  5399. assert(!SourceAction->getInputs().empty() && "unexpected root action!");
  5400. SourceAction = SourceAction->getInputs()[0];
  5401. }
  5402. // If -fno_integrated_as is used add -Q to the darwin assember driver to make
  5403. // sure it runs its system assembler not clang's integrated assembler.
  5404. // Applicable to darwin11+ and Xcode 4+. darwin<10 lacked integrated-as.
  5405. // FIXME: at run-time detect assembler capabilities or rely on version
  5406. // information forwarded by -target-assembler-version (future)
  5407. if (Args.hasArg(options::OPT_fno_integrated_as)) {
  5408. const llvm::Triple &T(getToolChain().getTriple());
  5409. if (!(T.isMacOSX() && T.isMacOSXVersionLT(10, 7)))
  5410. CmdArgs.push_back("-Q");
  5411. }
  5412. // Forward -g, assuming we are dealing with an actual assembly file.
  5413. if (SourceAction->getType() == types::TY_Asm ||
  5414. SourceAction->getType() == types::TY_PP_Asm) {
  5415. if (Args.hasArg(options::OPT_gstabs))
  5416. CmdArgs.push_back("--gstabs");
  5417. else if (Args.hasArg(options::OPT_g_Group))
  5418. CmdArgs.push_back("-g");
  5419. }
  5420. // Derived from asm spec.
  5421. AddMachOArch(Args, CmdArgs);
  5422. // Use -force_cpusubtype_ALL on x86 by default.
  5423. if (getToolChain().getArch() == llvm::Triple::x86 ||
  5424. getToolChain().getArch() == llvm::Triple::x86_64 ||
  5425. Args.hasArg(options::OPT_force__cpusubtype__ALL))
  5426. CmdArgs.push_back("-force_cpusubtype_ALL");
  5427. if (getToolChain().getArch() != llvm::Triple::x86_64 &&
  5428. (((Args.hasArg(options::OPT_mkernel) ||
  5429. Args.hasArg(options::OPT_fapple_kext)) &&
  5430. getMachOToolChain().isKernelStatic()) ||
  5431. Args.hasArg(options::OPT_static)))
  5432. CmdArgs.push_back("-static");
  5433. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  5434. assert(Output.isFilename() && "Unexpected lipo output.");
  5435. CmdArgs.push_back("-o");
  5436. CmdArgs.push_back(Output.getFilename());
  5437. assert(Input.isFilename() && "Invalid input.");
  5438. CmdArgs.push_back(Input.getFilename());
  5439. // asm_final spec is empty.
  5440. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  5441. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5442. }
  5443. void darwin::MachOTool::anchor() {}
  5444. void darwin::MachOTool::AddMachOArch(const ArgList &Args,
  5445. ArgStringList &CmdArgs) const {
  5446. StringRef ArchName = getMachOToolChain().getMachOArchName(Args);
  5447. // Derived from darwin_arch spec.
  5448. CmdArgs.push_back("-arch");
  5449. CmdArgs.push_back(Args.MakeArgString(ArchName));
  5450. // FIXME: Is this needed anymore?
  5451. if (ArchName == "arm")
  5452. CmdArgs.push_back("-force_cpusubtype_ALL");
  5453. }
  5454. bool darwin::Linker::NeedsTempPath(const InputInfoList &Inputs) const {
  5455. // We only need to generate a temp path for LTO if we aren't compiling object
  5456. // files. When compiling source files, we run 'dsymutil' after linking. We
  5457. // don't run 'dsymutil' when compiling object files.
  5458. for (const auto &Input : Inputs)
  5459. if (Input.getType() != types::TY_Object)
  5460. return true;
  5461. return false;
  5462. }
  5463. void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
  5464. ArgStringList &CmdArgs,
  5465. const InputInfoList &Inputs) const {
  5466. const Driver &D = getToolChain().getDriver();
  5467. const toolchains::MachO &MachOTC = getMachOToolChain();
  5468. unsigned Version[3] = {0, 0, 0};
  5469. if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
  5470. bool HadExtra;
  5471. if (!Driver::GetReleaseVersion(A->getValue(), Version[0], Version[1],
  5472. Version[2], HadExtra) ||
  5473. HadExtra)
  5474. D.Diag(diag::err_drv_invalid_version_number) << A->getAsString(Args);
  5475. }
  5476. // Newer linkers support -demangle. Pass it if supported and not disabled by
  5477. // the user.
  5478. if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
  5479. CmdArgs.push_back("-demangle");
  5480. if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137)
  5481. CmdArgs.push_back("-export_dynamic");
  5482. // If we are using App Extension restrictions, pass a flag to the linker
  5483. // telling it that the compiled code has been audited.
  5484. if (Args.hasFlag(options::OPT_fapplication_extension,
  5485. options::OPT_fno_application_extension, false))
  5486. CmdArgs.push_back("-application_extension");
  5487. // If we are using LTO, then automatically create a temporary file path for
  5488. // the linker to use, so that it's lifetime will extend past a possible
  5489. // dsymutil step.
  5490. if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
  5491. const char *TmpPath = C.getArgs().MakeArgString(
  5492. D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
  5493. C.addTempFile(TmpPath);
  5494. CmdArgs.push_back("-object_path_lto");
  5495. CmdArgs.push_back(TmpPath);
  5496. }
  5497. // Derived from the "link" spec.
  5498. Args.AddAllArgs(CmdArgs, options::OPT_static);
  5499. if (!Args.hasArg(options::OPT_static))
  5500. CmdArgs.push_back("-dynamic");
  5501. if (Args.hasArg(options::OPT_fgnu_runtime)) {
  5502. // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
  5503. // here. How do we wish to handle such things?
  5504. }
  5505. if (!Args.hasArg(options::OPT_dynamiclib)) {
  5506. AddMachOArch(Args, CmdArgs);
  5507. // FIXME: Why do this only on this path?
  5508. Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
  5509. Args.AddLastArg(CmdArgs, options::OPT_bundle);
  5510. Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
  5511. Args.AddAllArgs(CmdArgs, options::OPT_client__name);
  5512. Arg *A;
  5513. if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
  5514. (A = Args.getLastArg(options::OPT_current__version)) ||
  5515. (A = Args.getLastArg(options::OPT_install__name)))
  5516. D.Diag(diag::err_drv_argument_only_allowed_with) << A->getAsString(Args)
  5517. << "-dynamiclib";
  5518. Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
  5519. Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
  5520. Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
  5521. } else {
  5522. CmdArgs.push_back("-dylib");
  5523. Arg *A;
  5524. if ((A = Args.getLastArg(options::OPT_bundle)) ||
  5525. (A = Args.getLastArg(options::OPT_bundle__loader)) ||
  5526. (A = Args.getLastArg(options::OPT_client__name)) ||
  5527. (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
  5528. (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
  5529. (A = Args.getLastArg(options::OPT_private__bundle)))
  5530. D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
  5531. << "-dynamiclib";
  5532. Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
  5533. "-dylib_compatibility_version");
  5534. Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
  5535. "-dylib_current_version");
  5536. AddMachOArch(Args, CmdArgs);
  5537. Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
  5538. "-dylib_install_name");
  5539. }
  5540. Args.AddLastArg(CmdArgs, options::OPT_all__load);
  5541. Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
  5542. Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
  5543. if (MachOTC.isTargetIOSBased())
  5544. Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
  5545. Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
  5546. Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
  5547. Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
  5548. Args.AddLastArg(CmdArgs, options::OPT_dynamic);
  5549. Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
  5550. Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
  5551. Args.AddAllArgs(CmdArgs, options::OPT_force__load);
  5552. Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
  5553. Args.AddAllArgs(CmdArgs, options::OPT_image__base);
  5554. Args.AddAllArgs(CmdArgs, options::OPT_init);
  5555. // Add the deployment target.
  5556. MachOTC.addMinVersionArgs(Args, CmdArgs);
  5557. Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
  5558. Args.AddLastArg(CmdArgs, options::OPT_multi__module);
  5559. Args.AddLastArg(CmdArgs, options::OPT_single__module);
  5560. Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
  5561. Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
  5562. if (const Arg *A =
  5563. Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
  5564. options::OPT_fno_pie, options::OPT_fno_PIE)) {
  5565. if (A->getOption().matches(options::OPT_fpie) ||
  5566. A->getOption().matches(options::OPT_fPIE))
  5567. CmdArgs.push_back("-pie");
  5568. else
  5569. CmdArgs.push_back("-no_pie");
  5570. }
  5571. Args.AddLastArg(CmdArgs, options::OPT_prebind);
  5572. Args.AddLastArg(CmdArgs, options::OPT_noprebind);
  5573. Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
  5574. Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
  5575. Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
  5576. Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
  5577. Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
  5578. Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
  5579. Args.AddAllArgs(CmdArgs, options::OPT_segprot);
  5580. Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
  5581. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
  5582. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
  5583. Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
  5584. Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
  5585. Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
  5586. Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
  5587. // Give --sysroot= preference, over the Apple specific behavior to also use
  5588. // --isysroot as the syslibroot.
  5589. StringRef sysroot = C.getSysRoot();
  5590. if (sysroot != "") {
  5591. CmdArgs.push_back("-syslibroot");
  5592. CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
  5593. } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
  5594. CmdArgs.push_back("-syslibroot");
  5595. CmdArgs.push_back(A->getValue());
  5596. }
  5597. Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
  5598. Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
  5599. Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
  5600. Args.AddAllArgs(CmdArgs, options::OPT_undefined);
  5601. Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
  5602. Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
  5603. Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
  5604. Args.AddAllArgs(CmdArgs, options::OPT_y);
  5605. Args.AddLastArg(CmdArgs, options::OPT_w);
  5606. Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
  5607. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
  5608. Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
  5609. Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
  5610. Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
  5611. Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
  5612. Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
  5613. Args.AddLastArg(CmdArgs, options::OPT_whyload);
  5614. Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
  5615. Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
  5616. Args.AddLastArg(CmdArgs, options::OPT_dylinker);
  5617. Args.AddLastArg(CmdArgs, options::OPT_Mach);
  5618. }
  5619. void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  5620. const InputInfo &Output,
  5621. const InputInfoList &Inputs,
  5622. const ArgList &Args,
  5623. const char *LinkingOutput) const {
  5624. assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
  5625. // If the number of arguments surpasses the system limits, we will encode the
  5626. // input files in a separate file, shortening the command line. To this end,
  5627. // build a list of input file names that can be passed via a file with the
  5628. // -filelist linker option.
  5629. llvm::opt::ArgStringList InputFileList;
  5630. // The logic here is derived from gcc's behavior; most of which
  5631. // comes from specs (starting with link_command). Consult gcc for
  5632. // more information.
  5633. ArgStringList CmdArgs;
  5634. /// Hack(tm) to ignore linking errors when we are doing ARC migration.
  5635. if (Args.hasArg(options::OPT_ccc_arcmt_check,
  5636. options::OPT_ccc_arcmt_migrate)) {
  5637. for (const auto &Arg : Args)
  5638. Arg->claim();
  5639. const char *Exec =
  5640. Args.MakeArgString(getToolChain().GetProgramPath("touch"));
  5641. CmdArgs.push_back(Output.getFilename());
  5642. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5643. return;
  5644. }
  5645. // I'm not sure why this particular decomposition exists in gcc, but
  5646. // we follow suite for ease of comparison.
  5647. AddLinkArgs(C, Args, CmdArgs, Inputs);
  5648. Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
  5649. Args.AddAllArgs(CmdArgs, options::OPT_s);
  5650. Args.AddAllArgs(CmdArgs, options::OPT_t);
  5651. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  5652. Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
  5653. Args.AddLastArg(CmdArgs, options::OPT_e);
  5654. Args.AddAllArgs(CmdArgs, options::OPT_r);
  5655. // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
  5656. // members of static archive libraries which implement Objective-C classes or
  5657. // categories.
  5658. if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
  5659. CmdArgs.push_back("-ObjC");
  5660. CmdArgs.push_back("-o");
  5661. CmdArgs.push_back(Output.getFilename());
  5662. if (!Args.hasArg(options::OPT_nostdlib) &&
  5663. !Args.hasArg(options::OPT_nostartfiles))
  5664. getMachOToolChain().addStartObjectFileArgs(Args, CmdArgs);
  5665. // SafeStack requires its own runtime libraries
  5666. // These libraries should be linked first, to make sure the
  5667. // __safestack_init constructor executes before everything else
  5668. if (getToolChain().getSanitizerArgs().needsSafeStackRt()) {
  5669. getMachOToolChain().AddLinkRuntimeLib(Args, CmdArgs,
  5670. "libclang_rt.safestack_osx.a",
  5671. /*AlwaysLink=*/true);
  5672. }
  5673. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5674. if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
  5675. options::OPT_fno_openmp, false)) {
  5676. switch (getOpenMPRuntime(getToolChain(), Args)) {
  5677. case OMPRT_OMP:
  5678. CmdArgs.push_back("-lomp");
  5679. break;
  5680. case OMPRT_GOMP:
  5681. CmdArgs.push_back("-lgomp");
  5682. break;
  5683. case OMPRT_IOMP5:
  5684. CmdArgs.push_back("-liomp5");
  5685. break;
  5686. case OMPRT_Unknown:
  5687. // Already diagnosed.
  5688. break;
  5689. }
  5690. }
  5691. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  5692. // Build the input file for -filelist (list of linker input files) in case we
  5693. // need it later
  5694. for (const auto &II : Inputs) {
  5695. if (!II.isFilename()) {
  5696. // This is a linker input argument.
  5697. // We cannot mix input arguments and file names in a -filelist input, thus
  5698. // we prematurely stop our list (remaining files shall be passed as
  5699. // arguments).
  5700. if (InputFileList.size() > 0)
  5701. break;
  5702. continue;
  5703. }
  5704. InputFileList.push_back(II.getFilename());
  5705. }
  5706. if (isObjCRuntimeLinked(Args) && !Args.hasArg(options::OPT_nostdlib) &&
  5707. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5708. // We use arclite library for both ARC and subscripting support.
  5709. getMachOToolChain().AddLinkARCArgs(Args, CmdArgs);
  5710. CmdArgs.push_back("-framework");
  5711. CmdArgs.push_back("Foundation");
  5712. // Link libobj.
  5713. CmdArgs.push_back("-lobjc");
  5714. }
  5715. if (LinkingOutput) {
  5716. CmdArgs.push_back("-arch_multiple");
  5717. CmdArgs.push_back("-final_output");
  5718. CmdArgs.push_back(LinkingOutput);
  5719. }
  5720. if (Args.hasArg(options::OPT_fnested_functions))
  5721. CmdArgs.push_back("-allow_stack_execute");
  5722. // TODO: It would be nice to use addProfileRT() here, but darwin's compiler-rt
  5723. // paths are different enough from other toolchains that this needs a fair
  5724. // amount of refactoring done first.
  5725. getMachOToolChain().addProfileRTLibs(Args, CmdArgs);
  5726. if (!Args.hasArg(options::OPT_nostdlib) &&
  5727. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5728. if (getToolChain().getDriver().CCCIsCXX())
  5729. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  5730. // link_ssp spec is empty.
  5731. // Let the tool chain choose which runtime library to link.
  5732. getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
  5733. }
  5734. if (!Args.hasArg(options::OPT_nostdlib) &&
  5735. !Args.hasArg(options::OPT_nostartfiles)) {
  5736. // endfile_spec is empty.
  5737. }
  5738. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5739. Args.AddAllArgs(CmdArgs, options::OPT_F);
  5740. // -iframework should be forwarded as -F.
  5741. for (const Arg *A : Args.filtered(options::OPT_iframework))
  5742. CmdArgs.push_back(Args.MakeArgString(std::string("-F") + A->getValue()));
  5743. if (!Args.hasArg(options::OPT_nostdlib) &&
  5744. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5745. if (Arg *A = Args.getLastArg(options::OPT_fveclib)) {
  5746. if (A->getValue() == StringRef("Accelerate")) {
  5747. CmdArgs.push_back("-framework");
  5748. CmdArgs.push_back("Accelerate");
  5749. }
  5750. }
  5751. }
  5752. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  5753. std::unique_ptr<Command> Cmd =
  5754. llvm::make_unique<Command>(JA, *this, Exec, CmdArgs);
  5755. Cmd->setInputFileList(std::move(InputFileList));
  5756. C.addCommand(std::move(Cmd));
  5757. }
  5758. void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
  5759. const InputInfo &Output,
  5760. const InputInfoList &Inputs,
  5761. const ArgList &Args,
  5762. const char *LinkingOutput) const {
  5763. ArgStringList CmdArgs;
  5764. CmdArgs.push_back("-create");
  5765. assert(Output.isFilename() && "Unexpected lipo output.");
  5766. CmdArgs.push_back("-output");
  5767. CmdArgs.push_back(Output.getFilename());
  5768. for (const auto &II : Inputs) {
  5769. assert(II.isFilename() && "Unexpected lipo input.");
  5770. CmdArgs.push_back(II.getFilename());
  5771. }
  5772. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
  5773. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5774. }
  5775. void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
  5776. const InputInfo &Output,
  5777. const InputInfoList &Inputs,
  5778. const ArgList &Args,
  5779. const char *LinkingOutput) const {
  5780. ArgStringList CmdArgs;
  5781. CmdArgs.push_back("-o");
  5782. CmdArgs.push_back(Output.getFilename());
  5783. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  5784. const InputInfo &Input = Inputs[0];
  5785. assert(Input.isFilename() && "Unexpected dsymutil input.");
  5786. CmdArgs.push_back(Input.getFilename());
  5787. const char *Exec =
  5788. Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
  5789. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5790. }
  5791. void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
  5792. const InputInfo &Output,
  5793. const InputInfoList &Inputs,
  5794. const ArgList &Args,
  5795. const char *LinkingOutput) const {
  5796. ArgStringList CmdArgs;
  5797. CmdArgs.push_back("--verify");
  5798. CmdArgs.push_back("--debug-info");
  5799. CmdArgs.push_back("--eh-frame");
  5800. CmdArgs.push_back("--quiet");
  5801. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  5802. const InputInfo &Input = Inputs[0];
  5803. assert(Input.isFilename() && "Unexpected verify input");
  5804. // Grabbing the output of the earlier dsymutil run.
  5805. CmdArgs.push_back(Input.getFilename());
  5806. const char *Exec =
  5807. Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
  5808. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5809. }
  5810. void solaris::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  5811. const InputInfo &Output,
  5812. const InputInfoList &Inputs,
  5813. const ArgList &Args,
  5814. const char *LinkingOutput) const {
  5815. claimNoWarnArgs(Args);
  5816. ArgStringList CmdArgs;
  5817. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  5818. CmdArgs.push_back("-o");
  5819. CmdArgs.push_back(Output.getFilename());
  5820. for (const auto &II : Inputs)
  5821. CmdArgs.push_back(II.getFilename());
  5822. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  5823. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5824. }
  5825. void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  5826. const InputInfo &Output,
  5827. const InputInfoList &Inputs,
  5828. const ArgList &Args,
  5829. const char *LinkingOutput) const {
  5830. // FIXME: Find a real GCC, don't hard-code versions here
  5831. std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
  5832. const llvm::Triple &T = getToolChain().getTriple();
  5833. std::string LibPath = "/usr/lib/";
  5834. const llvm::Triple::ArchType Arch = T.getArch();
  5835. switch (Arch) {
  5836. case llvm::Triple::x86:
  5837. GCCLibPath +=
  5838. ("i386-" + T.getVendorName() + "-" + T.getOSName()).str() + "/4.5.2/";
  5839. break;
  5840. case llvm::Triple::x86_64:
  5841. GCCLibPath += ("i386-" + T.getVendorName() + "-" + T.getOSName()).str();
  5842. GCCLibPath += "/4.5.2/amd64/";
  5843. LibPath += "amd64/";
  5844. break;
  5845. default:
  5846. llvm_unreachable("Unsupported architecture");
  5847. }
  5848. ArgStringList CmdArgs;
  5849. // Demangle C++ names in errors
  5850. CmdArgs.push_back("-C");
  5851. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  5852. (!Args.hasArg(options::OPT_shared))) {
  5853. CmdArgs.push_back("-e");
  5854. CmdArgs.push_back("_start");
  5855. }
  5856. if (Args.hasArg(options::OPT_static)) {
  5857. CmdArgs.push_back("-Bstatic");
  5858. CmdArgs.push_back("-dn");
  5859. } else {
  5860. CmdArgs.push_back("-Bdynamic");
  5861. if (Args.hasArg(options::OPT_shared)) {
  5862. CmdArgs.push_back("-shared");
  5863. } else {
  5864. CmdArgs.push_back("--dynamic-linker");
  5865. CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
  5866. }
  5867. }
  5868. if (Output.isFilename()) {
  5869. CmdArgs.push_back("-o");
  5870. CmdArgs.push_back(Output.getFilename());
  5871. } else {
  5872. assert(Output.isNothing() && "Invalid output.");
  5873. }
  5874. if (!Args.hasArg(options::OPT_nostdlib) &&
  5875. !Args.hasArg(options::OPT_nostartfiles)) {
  5876. if (!Args.hasArg(options::OPT_shared)) {
  5877. CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
  5878. CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
  5879. CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
  5880. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
  5881. } else {
  5882. CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
  5883. CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
  5884. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
  5885. }
  5886. if (getToolChain().getDriver().CCCIsCXX())
  5887. CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
  5888. }
  5889. CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
  5890. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5891. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5892. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5893. Args.AddAllArgs(CmdArgs, options::OPT_r);
  5894. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  5895. if (!Args.hasArg(options::OPT_nostdlib) &&
  5896. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5897. if (getToolChain().getDriver().CCCIsCXX())
  5898. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  5899. CmdArgs.push_back("-lgcc_s");
  5900. if (!Args.hasArg(options::OPT_shared)) {
  5901. CmdArgs.push_back("-lgcc");
  5902. CmdArgs.push_back("-lc");
  5903. CmdArgs.push_back("-lm");
  5904. }
  5905. }
  5906. if (!Args.hasArg(options::OPT_nostdlib) &&
  5907. !Args.hasArg(options::OPT_nostartfiles)) {
  5908. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
  5909. }
  5910. CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
  5911. addProfileRT(getToolChain(), Args, CmdArgs);
  5912. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  5913. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5914. }
  5915. void openbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  5916. const InputInfo &Output,
  5917. const InputInfoList &Inputs,
  5918. const ArgList &Args,
  5919. const char *LinkingOutput) const {
  5920. claimNoWarnArgs(Args);
  5921. ArgStringList CmdArgs;
  5922. bool NeedsKPIC = false;
  5923. switch (getToolChain().getArch()) {
  5924. case llvm::Triple::x86:
  5925. // When building 32-bit code on OpenBSD/amd64, we have to explicitly
  5926. // instruct as in the base system to assemble 32-bit code.
  5927. CmdArgs.push_back("--32");
  5928. break;
  5929. case llvm::Triple::ppc:
  5930. CmdArgs.push_back("-mppc");
  5931. CmdArgs.push_back("-many");
  5932. break;
  5933. case llvm::Triple::sparc:
  5934. case llvm::Triple::sparcel:
  5935. CmdArgs.push_back("-32");
  5936. NeedsKPIC = true;
  5937. break;
  5938. case llvm::Triple::sparcv9:
  5939. CmdArgs.push_back("-64");
  5940. CmdArgs.push_back("-Av9a");
  5941. NeedsKPIC = true;
  5942. break;
  5943. case llvm::Triple::mips64:
  5944. case llvm::Triple::mips64el: {
  5945. StringRef CPUName;
  5946. StringRef ABIName;
  5947. mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  5948. CmdArgs.push_back("-mabi");
  5949. CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
  5950. if (getToolChain().getArch() == llvm::Triple::mips64)
  5951. CmdArgs.push_back("-EB");
  5952. else
  5953. CmdArgs.push_back("-EL");
  5954. NeedsKPIC = true;
  5955. break;
  5956. }
  5957. default:
  5958. break;
  5959. }
  5960. if (NeedsKPIC)
  5961. addAssemblerKPIC(Args, CmdArgs);
  5962. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  5963. CmdArgs.push_back("-o");
  5964. CmdArgs.push_back(Output.getFilename());
  5965. for (const auto &II : Inputs)
  5966. CmdArgs.push_back(II.getFilename());
  5967. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  5968. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  5969. }
  5970. void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  5971. const InputInfo &Output,
  5972. const InputInfoList &Inputs,
  5973. const ArgList &Args,
  5974. const char *LinkingOutput) const {
  5975. const Driver &D = getToolChain().getDriver();
  5976. ArgStringList CmdArgs;
  5977. // Silence warning for "clang -g foo.o -o foo"
  5978. Args.ClaimAllArgs(options::OPT_g_Group);
  5979. // and "clang -emit-llvm foo.o -o foo"
  5980. Args.ClaimAllArgs(options::OPT_emit_llvm);
  5981. // and for "clang -w foo.o -o foo". Other warning options are already
  5982. // handled somewhere else.
  5983. Args.ClaimAllArgs(options::OPT_w);
  5984. if (getToolChain().getArch() == llvm::Triple::mips64)
  5985. CmdArgs.push_back("-EB");
  5986. else if (getToolChain().getArch() == llvm::Triple::mips64el)
  5987. CmdArgs.push_back("-EL");
  5988. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  5989. (!Args.hasArg(options::OPT_shared))) {
  5990. CmdArgs.push_back("-e");
  5991. CmdArgs.push_back("__start");
  5992. }
  5993. if (Args.hasArg(options::OPT_static)) {
  5994. CmdArgs.push_back("-Bstatic");
  5995. } else {
  5996. if (Args.hasArg(options::OPT_rdynamic))
  5997. CmdArgs.push_back("-export-dynamic");
  5998. CmdArgs.push_back("--eh-frame-hdr");
  5999. CmdArgs.push_back("-Bdynamic");
  6000. if (Args.hasArg(options::OPT_shared)) {
  6001. CmdArgs.push_back("-shared");
  6002. } else {
  6003. CmdArgs.push_back("-dynamic-linker");
  6004. CmdArgs.push_back("/usr/libexec/ld.so");
  6005. }
  6006. }
  6007. if (Args.hasArg(options::OPT_nopie))
  6008. CmdArgs.push_back("-nopie");
  6009. if (Output.isFilename()) {
  6010. CmdArgs.push_back("-o");
  6011. CmdArgs.push_back(Output.getFilename());
  6012. } else {
  6013. assert(Output.isNothing() && "Invalid output.");
  6014. }
  6015. if (!Args.hasArg(options::OPT_nostdlib) &&
  6016. !Args.hasArg(options::OPT_nostartfiles)) {
  6017. if (!Args.hasArg(options::OPT_shared)) {
  6018. if (Args.hasArg(options::OPT_pg))
  6019. CmdArgs.push_back(
  6020. Args.MakeArgString(getToolChain().GetFilePath("gcrt0.o")));
  6021. else
  6022. CmdArgs.push_back(
  6023. Args.MakeArgString(getToolChain().GetFilePath("crt0.o")));
  6024. CmdArgs.push_back(
  6025. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  6026. } else {
  6027. CmdArgs.push_back(
  6028. Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
  6029. }
  6030. }
  6031. std::string Triple = getToolChain().getTripleString();
  6032. if (Triple.substr(0, 6) == "x86_64")
  6033. Triple.replace(0, 6, "amd64");
  6034. CmdArgs.push_back(
  6035. Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple + "/4.2.1"));
  6036. Args.AddAllArgs(CmdArgs, options::OPT_L);
  6037. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  6038. Args.AddAllArgs(CmdArgs, options::OPT_e);
  6039. Args.AddAllArgs(CmdArgs, options::OPT_s);
  6040. Args.AddAllArgs(CmdArgs, options::OPT_t);
  6041. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  6042. Args.AddAllArgs(CmdArgs, options::OPT_r);
  6043. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  6044. if (!Args.hasArg(options::OPT_nostdlib) &&
  6045. !Args.hasArg(options::OPT_nodefaultlibs)) {
  6046. if (D.CCCIsCXX()) {
  6047. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  6048. if (Args.hasArg(options::OPT_pg))
  6049. CmdArgs.push_back("-lm_p");
  6050. else
  6051. CmdArgs.push_back("-lm");
  6052. }
  6053. // FIXME: For some reason GCC passes -lgcc before adding
  6054. // the default system libraries. Just mimic this for now.
  6055. CmdArgs.push_back("-lgcc");
  6056. if (Args.hasArg(options::OPT_pthread)) {
  6057. if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
  6058. CmdArgs.push_back("-lpthread_p");
  6059. else
  6060. CmdArgs.push_back("-lpthread");
  6061. }
  6062. if (!Args.hasArg(options::OPT_shared)) {
  6063. if (Args.hasArg(options::OPT_pg))
  6064. CmdArgs.push_back("-lc_p");
  6065. else
  6066. CmdArgs.push_back("-lc");
  6067. }
  6068. CmdArgs.push_back("-lgcc");
  6069. }
  6070. if (!Args.hasArg(options::OPT_nostdlib) &&
  6071. !Args.hasArg(options::OPT_nostartfiles)) {
  6072. if (!Args.hasArg(options::OPT_shared))
  6073. CmdArgs.push_back(
  6074. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  6075. else
  6076. CmdArgs.push_back(
  6077. Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
  6078. }
  6079. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  6080. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6081. }
  6082. void bitrig::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  6083. const InputInfo &Output,
  6084. const InputInfoList &Inputs,
  6085. const ArgList &Args,
  6086. const char *LinkingOutput) const {
  6087. claimNoWarnArgs(Args);
  6088. ArgStringList CmdArgs;
  6089. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  6090. CmdArgs.push_back("-o");
  6091. CmdArgs.push_back(Output.getFilename());
  6092. for (const auto &II : Inputs)
  6093. CmdArgs.push_back(II.getFilename());
  6094. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  6095. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6096. }
  6097. void bitrig::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  6098. const InputInfo &Output,
  6099. const InputInfoList &Inputs,
  6100. const ArgList &Args,
  6101. const char *LinkingOutput) const {
  6102. const Driver &D = getToolChain().getDriver();
  6103. ArgStringList CmdArgs;
  6104. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  6105. (!Args.hasArg(options::OPT_shared))) {
  6106. CmdArgs.push_back("-e");
  6107. CmdArgs.push_back("__start");
  6108. }
  6109. if (Args.hasArg(options::OPT_static)) {
  6110. CmdArgs.push_back("-Bstatic");
  6111. } else {
  6112. if (Args.hasArg(options::OPT_rdynamic))
  6113. CmdArgs.push_back("-export-dynamic");
  6114. CmdArgs.push_back("--eh-frame-hdr");
  6115. CmdArgs.push_back("-Bdynamic");
  6116. if (Args.hasArg(options::OPT_shared)) {
  6117. CmdArgs.push_back("-shared");
  6118. } else {
  6119. CmdArgs.push_back("-dynamic-linker");
  6120. CmdArgs.push_back("/usr/libexec/ld.so");
  6121. }
  6122. }
  6123. if (Output.isFilename()) {
  6124. CmdArgs.push_back("-o");
  6125. CmdArgs.push_back(Output.getFilename());
  6126. } else {
  6127. assert(Output.isNothing() && "Invalid output.");
  6128. }
  6129. if (!Args.hasArg(options::OPT_nostdlib) &&
  6130. !Args.hasArg(options::OPT_nostartfiles)) {
  6131. if (!Args.hasArg(options::OPT_shared)) {
  6132. if (Args.hasArg(options::OPT_pg))
  6133. CmdArgs.push_back(
  6134. Args.MakeArgString(getToolChain().GetFilePath("gcrt0.o")));
  6135. else
  6136. CmdArgs.push_back(
  6137. Args.MakeArgString(getToolChain().GetFilePath("crt0.o")));
  6138. CmdArgs.push_back(
  6139. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  6140. } else {
  6141. CmdArgs.push_back(
  6142. Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
  6143. }
  6144. }
  6145. Args.AddAllArgs(CmdArgs, options::OPT_L);
  6146. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  6147. Args.AddAllArgs(CmdArgs, options::OPT_e);
  6148. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  6149. if (!Args.hasArg(options::OPT_nostdlib) &&
  6150. !Args.hasArg(options::OPT_nodefaultlibs)) {
  6151. if (D.CCCIsCXX()) {
  6152. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  6153. if (Args.hasArg(options::OPT_pg))
  6154. CmdArgs.push_back("-lm_p");
  6155. else
  6156. CmdArgs.push_back("-lm");
  6157. }
  6158. if (Args.hasArg(options::OPT_pthread)) {
  6159. if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
  6160. CmdArgs.push_back("-lpthread_p");
  6161. else
  6162. CmdArgs.push_back("-lpthread");
  6163. }
  6164. if (!Args.hasArg(options::OPT_shared)) {
  6165. if (Args.hasArg(options::OPT_pg))
  6166. CmdArgs.push_back("-lc_p");
  6167. else
  6168. CmdArgs.push_back("-lc");
  6169. }
  6170. StringRef MyArch;
  6171. switch (getToolChain().getArch()) {
  6172. case llvm::Triple::arm:
  6173. MyArch = "arm";
  6174. break;
  6175. case llvm::Triple::x86:
  6176. MyArch = "i386";
  6177. break;
  6178. case llvm::Triple::x86_64:
  6179. MyArch = "amd64";
  6180. break;
  6181. default:
  6182. llvm_unreachable("Unsupported architecture");
  6183. }
  6184. CmdArgs.push_back(Args.MakeArgString("-lclang_rt." + MyArch));
  6185. }
  6186. if (!Args.hasArg(options::OPT_nostdlib) &&
  6187. !Args.hasArg(options::OPT_nostartfiles)) {
  6188. if (!Args.hasArg(options::OPT_shared))
  6189. CmdArgs.push_back(
  6190. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  6191. else
  6192. CmdArgs.push_back(
  6193. Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
  6194. }
  6195. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  6196. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6197. }
  6198. void freebsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  6199. const InputInfo &Output,
  6200. const InputInfoList &Inputs,
  6201. const ArgList &Args,
  6202. const char *LinkingOutput) const {
  6203. claimNoWarnArgs(Args);
  6204. ArgStringList CmdArgs;
  6205. // When building 32-bit code on FreeBSD/amd64, we have to explicitly
  6206. // instruct as in the base system to assemble 32-bit code.
  6207. if (getToolChain().getArch() == llvm::Triple::x86)
  6208. CmdArgs.push_back("--32");
  6209. else if (getToolChain().getArch() == llvm::Triple::ppc)
  6210. CmdArgs.push_back("-a32");
  6211. else if (getToolChain().getArch() == llvm::Triple::mips ||
  6212. getToolChain().getArch() == llvm::Triple::mipsel ||
  6213. getToolChain().getArch() == llvm::Triple::mips64 ||
  6214. getToolChain().getArch() == llvm::Triple::mips64el) {
  6215. StringRef CPUName;
  6216. StringRef ABIName;
  6217. mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  6218. CmdArgs.push_back("-march");
  6219. CmdArgs.push_back(CPUName.data());
  6220. CmdArgs.push_back("-mabi");
  6221. CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
  6222. if (getToolChain().getArch() == llvm::Triple::mips ||
  6223. getToolChain().getArch() == llvm::Triple::mips64)
  6224. CmdArgs.push_back("-EB");
  6225. else
  6226. CmdArgs.push_back("-EL");
  6227. addAssemblerKPIC(Args, CmdArgs);
  6228. } else if (getToolChain().getArch() == llvm::Triple::arm ||
  6229. getToolChain().getArch() == llvm::Triple::armeb ||
  6230. getToolChain().getArch() == llvm::Triple::thumb ||
  6231. getToolChain().getArch() == llvm::Triple::thumbeb) {
  6232. const Driver &D = getToolChain().getDriver();
  6233. const llvm::Triple &Triple = getToolChain().getTriple();
  6234. StringRef FloatABI = arm::getARMFloatABI(D, Args, Triple);
  6235. if (FloatABI == "hard") {
  6236. CmdArgs.push_back("-mfpu=vfp");
  6237. } else {
  6238. CmdArgs.push_back("-mfpu=softvfp");
  6239. }
  6240. switch (getToolChain().getTriple().getEnvironment()) {
  6241. case llvm::Triple::GNUEABIHF:
  6242. case llvm::Triple::GNUEABI:
  6243. case llvm::Triple::EABI:
  6244. CmdArgs.push_back("-meabi=5");
  6245. break;
  6246. default:
  6247. CmdArgs.push_back("-matpcs");
  6248. }
  6249. } else if (getToolChain().getArch() == llvm::Triple::sparc ||
  6250. getToolChain().getArch() == llvm::Triple::sparcel ||
  6251. getToolChain().getArch() == llvm::Triple::sparcv9) {
  6252. if (getToolChain().getArch() == llvm::Triple::sparc)
  6253. CmdArgs.push_back("-Av8plusa");
  6254. else
  6255. CmdArgs.push_back("-Av9a");
  6256. addAssemblerKPIC(Args, CmdArgs);
  6257. }
  6258. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  6259. CmdArgs.push_back("-o");
  6260. CmdArgs.push_back(Output.getFilename());
  6261. for (const auto &II : Inputs)
  6262. CmdArgs.push_back(II.getFilename());
  6263. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  6264. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6265. }
  6266. void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  6267. const InputInfo &Output,
  6268. const InputInfoList &Inputs,
  6269. const ArgList &Args,
  6270. const char *LinkingOutput) const {
  6271. const toolchains::FreeBSD &ToolChain =
  6272. static_cast<const toolchains::FreeBSD &>(getToolChain());
  6273. const Driver &D = ToolChain.getDriver();
  6274. const llvm::Triple::ArchType Arch = ToolChain.getArch();
  6275. const bool IsPIE =
  6276. !Args.hasArg(options::OPT_shared) &&
  6277. (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
  6278. ArgStringList CmdArgs;
  6279. // Silence warning for "clang -g foo.o -o foo"
  6280. Args.ClaimAllArgs(options::OPT_g_Group);
  6281. // and "clang -emit-llvm foo.o -o foo"
  6282. Args.ClaimAllArgs(options::OPT_emit_llvm);
  6283. // and for "clang -w foo.o -o foo". Other warning options are already
  6284. // handled somewhere else.
  6285. Args.ClaimAllArgs(options::OPT_w);
  6286. if (!D.SysRoot.empty())
  6287. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  6288. if (IsPIE)
  6289. CmdArgs.push_back("-pie");
  6290. if (Args.hasArg(options::OPT_static)) {
  6291. CmdArgs.push_back("-Bstatic");
  6292. } else {
  6293. if (Args.hasArg(options::OPT_rdynamic))
  6294. CmdArgs.push_back("-export-dynamic");
  6295. CmdArgs.push_back("--eh-frame-hdr");
  6296. if (Args.hasArg(options::OPT_shared)) {
  6297. CmdArgs.push_back("-Bshareable");
  6298. } else {
  6299. CmdArgs.push_back("-dynamic-linker");
  6300. CmdArgs.push_back("/libexec/ld-elf.so.1");
  6301. }
  6302. if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
  6303. if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
  6304. Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
  6305. CmdArgs.push_back("--hash-style=both");
  6306. }
  6307. }
  6308. CmdArgs.push_back("--enable-new-dtags");
  6309. }
  6310. // When building 32-bit code on FreeBSD/amd64, we have to explicitly
  6311. // instruct ld in the base system to link 32-bit code.
  6312. if (Arch == llvm::Triple::x86) {
  6313. CmdArgs.push_back("-m");
  6314. CmdArgs.push_back("elf_i386_fbsd");
  6315. }
  6316. if (Arch == llvm::Triple::ppc) {
  6317. CmdArgs.push_back("-m");
  6318. CmdArgs.push_back("elf32ppc_fbsd");
  6319. }
  6320. if (Output.isFilename()) {
  6321. CmdArgs.push_back("-o");
  6322. CmdArgs.push_back(Output.getFilename());
  6323. } else {
  6324. assert(Output.isNothing() && "Invalid output.");
  6325. }
  6326. if (!Args.hasArg(options::OPT_nostdlib) &&
  6327. !Args.hasArg(options::OPT_nostartfiles)) {
  6328. const char *crt1 = nullptr;
  6329. if (!Args.hasArg(options::OPT_shared)) {
  6330. if (Args.hasArg(options::OPT_pg))
  6331. crt1 = "gcrt1.o";
  6332. else if (IsPIE)
  6333. crt1 = "Scrt1.o";
  6334. else
  6335. crt1 = "crt1.o";
  6336. }
  6337. if (crt1)
  6338. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
  6339. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  6340. const char *crtbegin = nullptr;
  6341. if (Args.hasArg(options::OPT_static))
  6342. crtbegin = "crtbeginT.o";
  6343. else if (Args.hasArg(options::OPT_shared) || IsPIE)
  6344. crtbegin = "crtbeginS.o";
  6345. else
  6346. crtbegin = "crtbegin.o";
  6347. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  6348. }
  6349. Args.AddAllArgs(CmdArgs, options::OPT_L);
  6350. const ToolChain::path_list &Paths = ToolChain.getFilePaths();
  6351. for (const auto &Path : Paths)
  6352. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
  6353. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  6354. Args.AddAllArgs(CmdArgs, options::OPT_e);
  6355. Args.AddAllArgs(CmdArgs, options::OPT_s);
  6356. Args.AddAllArgs(CmdArgs, options::OPT_t);
  6357. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  6358. Args.AddAllArgs(CmdArgs, options::OPT_r);
  6359. if (D.IsUsingLTO(Args))
  6360. AddGoldPlugin(ToolChain, Args, CmdArgs);
  6361. bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
  6362. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  6363. if (!Args.hasArg(options::OPT_nostdlib) &&
  6364. !Args.hasArg(options::OPT_nodefaultlibs)) {
  6365. if (D.CCCIsCXX()) {
  6366. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  6367. if (Args.hasArg(options::OPT_pg))
  6368. CmdArgs.push_back("-lm_p");
  6369. else
  6370. CmdArgs.push_back("-lm");
  6371. }
  6372. if (NeedsSanitizerDeps)
  6373. linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
  6374. // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
  6375. // the default system libraries. Just mimic this for now.
  6376. if (Args.hasArg(options::OPT_pg))
  6377. CmdArgs.push_back("-lgcc_p");
  6378. else
  6379. CmdArgs.push_back("-lgcc");
  6380. if (Args.hasArg(options::OPT_static)) {
  6381. CmdArgs.push_back("-lgcc_eh");
  6382. } else if (Args.hasArg(options::OPT_pg)) {
  6383. CmdArgs.push_back("-lgcc_eh_p");
  6384. } else {
  6385. CmdArgs.push_back("--as-needed");
  6386. CmdArgs.push_back("-lgcc_s");
  6387. CmdArgs.push_back("--no-as-needed");
  6388. }
  6389. if (Args.hasArg(options::OPT_pthread)) {
  6390. if (Args.hasArg(options::OPT_pg))
  6391. CmdArgs.push_back("-lpthread_p");
  6392. else
  6393. CmdArgs.push_back("-lpthread");
  6394. }
  6395. if (Args.hasArg(options::OPT_pg)) {
  6396. if (Args.hasArg(options::OPT_shared))
  6397. CmdArgs.push_back("-lc");
  6398. else
  6399. CmdArgs.push_back("-lc_p");
  6400. CmdArgs.push_back("-lgcc_p");
  6401. } else {
  6402. CmdArgs.push_back("-lc");
  6403. CmdArgs.push_back("-lgcc");
  6404. }
  6405. if (Args.hasArg(options::OPT_static)) {
  6406. CmdArgs.push_back("-lgcc_eh");
  6407. } else if (Args.hasArg(options::OPT_pg)) {
  6408. CmdArgs.push_back("-lgcc_eh_p");
  6409. } else {
  6410. CmdArgs.push_back("--as-needed");
  6411. CmdArgs.push_back("-lgcc_s");
  6412. CmdArgs.push_back("--no-as-needed");
  6413. }
  6414. }
  6415. if (!Args.hasArg(options::OPT_nostdlib) &&
  6416. !Args.hasArg(options::OPT_nostartfiles)) {
  6417. if (Args.hasArg(options::OPT_shared) || IsPIE)
  6418. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
  6419. else
  6420. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
  6421. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  6422. }
  6423. addProfileRT(ToolChain, Args, CmdArgs);
  6424. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  6425. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6426. }
  6427. void netbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  6428. const InputInfo &Output,
  6429. const InputInfoList &Inputs,
  6430. const ArgList &Args,
  6431. const char *LinkingOutput) const {
  6432. claimNoWarnArgs(Args);
  6433. ArgStringList CmdArgs;
  6434. // GNU as needs different flags for creating the correct output format
  6435. // on architectures with different ABIs or optional feature sets.
  6436. switch (getToolChain().getArch()) {
  6437. case llvm::Triple::x86:
  6438. CmdArgs.push_back("--32");
  6439. break;
  6440. case llvm::Triple::arm:
  6441. case llvm::Triple::armeb:
  6442. case llvm::Triple::thumb:
  6443. case llvm::Triple::thumbeb: {
  6444. StringRef MArch, MCPU;
  6445. getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true);
  6446. std::string Arch =
  6447. arm::getARMTargetCPU(MCPU, MArch, getToolChain().getTriple());
  6448. CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch));
  6449. break;
  6450. }
  6451. case llvm::Triple::mips:
  6452. case llvm::Triple::mipsel:
  6453. case llvm::Triple::mips64:
  6454. case llvm::Triple::mips64el: {
  6455. StringRef CPUName;
  6456. StringRef ABIName;
  6457. mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  6458. CmdArgs.push_back("-march");
  6459. CmdArgs.push_back(CPUName.data());
  6460. CmdArgs.push_back("-mabi");
  6461. CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
  6462. if (getToolChain().getArch() == llvm::Triple::mips ||
  6463. getToolChain().getArch() == llvm::Triple::mips64)
  6464. CmdArgs.push_back("-EB");
  6465. else
  6466. CmdArgs.push_back("-EL");
  6467. addAssemblerKPIC(Args, CmdArgs);
  6468. break;
  6469. }
  6470. case llvm::Triple::sparc:
  6471. case llvm::Triple::sparcel:
  6472. CmdArgs.push_back("-32");
  6473. addAssemblerKPIC(Args, CmdArgs);
  6474. break;
  6475. case llvm::Triple::sparcv9:
  6476. CmdArgs.push_back("-64");
  6477. CmdArgs.push_back("-Av9");
  6478. addAssemblerKPIC(Args, CmdArgs);
  6479. break;
  6480. default:
  6481. break;
  6482. }
  6483. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  6484. CmdArgs.push_back("-o");
  6485. CmdArgs.push_back(Output.getFilename());
  6486. for (const auto &II : Inputs)
  6487. CmdArgs.push_back(II.getFilename());
  6488. const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
  6489. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6490. }
  6491. void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  6492. const InputInfo &Output,
  6493. const InputInfoList &Inputs,
  6494. const ArgList &Args,
  6495. const char *LinkingOutput) const {
  6496. const Driver &D = getToolChain().getDriver();
  6497. ArgStringList CmdArgs;
  6498. if (!D.SysRoot.empty())
  6499. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  6500. CmdArgs.push_back("--eh-frame-hdr");
  6501. if (Args.hasArg(options::OPT_static)) {
  6502. CmdArgs.push_back("-Bstatic");
  6503. } else {
  6504. if (Args.hasArg(options::OPT_rdynamic))
  6505. CmdArgs.push_back("-export-dynamic");
  6506. if (Args.hasArg(options::OPT_shared)) {
  6507. CmdArgs.push_back("-Bshareable");
  6508. } else {
  6509. CmdArgs.push_back("-dynamic-linker");
  6510. CmdArgs.push_back("/libexec/ld.elf_so");
  6511. }
  6512. }
  6513. // Many NetBSD architectures support more than one ABI.
  6514. // Determine the correct emulation for ld.
  6515. switch (getToolChain().getArch()) {
  6516. case llvm::Triple::x86:
  6517. CmdArgs.push_back("-m");
  6518. CmdArgs.push_back("elf_i386");
  6519. break;
  6520. case llvm::Triple::arm:
  6521. case llvm::Triple::thumb:
  6522. CmdArgs.push_back("-m");
  6523. switch (getToolChain().getTriple().getEnvironment()) {
  6524. case llvm::Triple::EABI:
  6525. case llvm::Triple::GNUEABI:
  6526. CmdArgs.push_back("armelf_nbsd_eabi");
  6527. break;
  6528. case llvm::Triple::EABIHF:
  6529. case llvm::Triple::GNUEABIHF:
  6530. CmdArgs.push_back("armelf_nbsd_eabihf");
  6531. break;
  6532. default:
  6533. CmdArgs.push_back("armelf_nbsd");
  6534. break;
  6535. }
  6536. break;
  6537. case llvm::Triple::armeb:
  6538. case llvm::Triple::thumbeb:
  6539. arm::appendEBLinkFlags(
  6540. Args, CmdArgs,
  6541. llvm::Triple(getToolChain().ComputeEffectiveClangTriple(Args)));
  6542. CmdArgs.push_back("-m");
  6543. switch (getToolChain().getTriple().getEnvironment()) {
  6544. case llvm::Triple::EABI:
  6545. case llvm::Triple::GNUEABI:
  6546. CmdArgs.push_back("armelfb_nbsd_eabi");
  6547. break;
  6548. case llvm::Triple::EABIHF:
  6549. case llvm::Triple::GNUEABIHF:
  6550. CmdArgs.push_back("armelfb_nbsd_eabihf");
  6551. break;
  6552. default:
  6553. CmdArgs.push_back("armelfb_nbsd");
  6554. break;
  6555. }
  6556. break;
  6557. case llvm::Triple::mips64:
  6558. case llvm::Triple::mips64el:
  6559. if (mips::hasMipsAbiArg(Args, "32")) {
  6560. CmdArgs.push_back("-m");
  6561. if (getToolChain().getArch() == llvm::Triple::mips64)
  6562. CmdArgs.push_back("elf32btsmip");
  6563. else
  6564. CmdArgs.push_back("elf32ltsmip");
  6565. } else if (mips::hasMipsAbiArg(Args, "64")) {
  6566. CmdArgs.push_back("-m");
  6567. if (getToolChain().getArch() == llvm::Triple::mips64)
  6568. CmdArgs.push_back("elf64btsmip");
  6569. else
  6570. CmdArgs.push_back("elf64ltsmip");
  6571. }
  6572. break;
  6573. case llvm::Triple::ppc:
  6574. CmdArgs.push_back("-m");
  6575. CmdArgs.push_back("elf32ppc_nbsd");
  6576. break;
  6577. case llvm::Triple::ppc64:
  6578. case llvm::Triple::ppc64le:
  6579. CmdArgs.push_back("-m");
  6580. CmdArgs.push_back("elf64ppc");
  6581. break;
  6582. case llvm::Triple::sparc:
  6583. CmdArgs.push_back("-m");
  6584. CmdArgs.push_back("elf32_sparc");
  6585. break;
  6586. case llvm::Triple::sparcv9:
  6587. CmdArgs.push_back("-m");
  6588. CmdArgs.push_back("elf64_sparc");
  6589. break;
  6590. default:
  6591. break;
  6592. }
  6593. if (Output.isFilename()) {
  6594. CmdArgs.push_back("-o");
  6595. CmdArgs.push_back(Output.getFilename());
  6596. } else {
  6597. assert(Output.isNothing() && "Invalid output.");
  6598. }
  6599. if (!Args.hasArg(options::OPT_nostdlib) &&
  6600. !Args.hasArg(options::OPT_nostartfiles)) {
  6601. if (!Args.hasArg(options::OPT_shared)) {
  6602. CmdArgs.push_back(
  6603. Args.MakeArgString(getToolChain().GetFilePath("crt0.o")));
  6604. CmdArgs.push_back(
  6605. Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  6606. CmdArgs.push_back(
  6607. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  6608. } else {
  6609. CmdArgs.push_back(
  6610. Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  6611. CmdArgs.push_back(
  6612. Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
  6613. }
  6614. }
  6615. Args.AddAllArgs(CmdArgs, options::OPT_L);
  6616. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  6617. Args.AddAllArgs(CmdArgs, options::OPT_e);
  6618. Args.AddAllArgs(CmdArgs, options::OPT_s);
  6619. Args.AddAllArgs(CmdArgs, options::OPT_t);
  6620. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  6621. Args.AddAllArgs(CmdArgs, options::OPT_r);
  6622. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  6623. unsigned Major, Minor, Micro;
  6624. getToolChain().getTriple().getOSVersion(Major, Minor, Micro);
  6625. bool useLibgcc = true;
  6626. if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 49) || Major == 0) {
  6627. switch (getToolChain().getArch()) {
  6628. case llvm::Triple::aarch64:
  6629. case llvm::Triple::arm:
  6630. case llvm::Triple::armeb:
  6631. case llvm::Triple::thumb:
  6632. case llvm::Triple::thumbeb:
  6633. case llvm::Triple::ppc:
  6634. case llvm::Triple::ppc64:
  6635. case llvm::Triple::ppc64le:
  6636. case llvm::Triple::x86:
  6637. case llvm::Triple::x86_64:
  6638. useLibgcc = false;
  6639. break;
  6640. default:
  6641. break;
  6642. }
  6643. }
  6644. if (!Args.hasArg(options::OPT_nostdlib) &&
  6645. !Args.hasArg(options::OPT_nodefaultlibs)) {
  6646. if (D.CCCIsCXX()) {
  6647. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  6648. CmdArgs.push_back("-lm");
  6649. }
  6650. if (Args.hasArg(options::OPT_pthread))
  6651. CmdArgs.push_back("-lpthread");
  6652. CmdArgs.push_back("-lc");
  6653. if (useLibgcc) {
  6654. if (Args.hasArg(options::OPT_static)) {
  6655. // libgcc_eh depends on libc, so resolve as much as possible,
  6656. // pull in any new requirements from libc and then get the rest
  6657. // of libgcc.
  6658. CmdArgs.push_back("-lgcc_eh");
  6659. CmdArgs.push_back("-lc");
  6660. CmdArgs.push_back("-lgcc");
  6661. } else {
  6662. CmdArgs.push_back("-lgcc");
  6663. CmdArgs.push_back("--as-needed");
  6664. CmdArgs.push_back("-lgcc_s");
  6665. CmdArgs.push_back("--no-as-needed");
  6666. }
  6667. }
  6668. }
  6669. if (!Args.hasArg(options::OPT_nostdlib) &&
  6670. !Args.hasArg(options::OPT_nostartfiles)) {
  6671. if (!Args.hasArg(options::OPT_shared))
  6672. CmdArgs.push_back(
  6673. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  6674. else
  6675. CmdArgs.push_back(
  6676. Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
  6677. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
  6678. }
  6679. addProfileRT(getToolChain(), Args, CmdArgs);
  6680. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  6681. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6682. }
  6683. void gnutools::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  6684. const InputInfo &Output,
  6685. const InputInfoList &Inputs,
  6686. const ArgList &Args,
  6687. const char *LinkingOutput) const {
  6688. claimNoWarnArgs(Args);
  6689. ArgStringList CmdArgs;
  6690. bool NeedsKPIC = false;
  6691. switch (getToolChain().getArch()) {
  6692. default:
  6693. break;
  6694. // Add --32/--64 to make sure we get the format we want.
  6695. // This is incomplete
  6696. case llvm::Triple::x86:
  6697. CmdArgs.push_back("--32");
  6698. break;
  6699. case llvm::Triple::x86_64:
  6700. if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUX32)
  6701. CmdArgs.push_back("--x32");
  6702. else
  6703. CmdArgs.push_back("--64");
  6704. break;
  6705. case llvm::Triple::ppc:
  6706. CmdArgs.push_back("-a32");
  6707. CmdArgs.push_back("-mppc");
  6708. CmdArgs.push_back("-many");
  6709. break;
  6710. case llvm::Triple::ppc64:
  6711. CmdArgs.push_back("-a64");
  6712. CmdArgs.push_back("-mppc64");
  6713. CmdArgs.push_back("-many");
  6714. break;
  6715. case llvm::Triple::ppc64le:
  6716. CmdArgs.push_back("-a64");
  6717. CmdArgs.push_back("-mppc64");
  6718. CmdArgs.push_back("-many");
  6719. CmdArgs.push_back("-mlittle-endian");
  6720. break;
  6721. case llvm::Triple::sparc:
  6722. case llvm::Triple::sparcel:
  6723. CmdArgs.push_back("-32");
  6724. CmdArgs.push_back("-Av8plusa");
  6725. NeedsKPIC = true;
  6726. break;
  6727. case llvm::Triple::sparcv9:
  6728. CmdArgs.push_back("-64");
  6729. CmdArgs.push_back("-Av9a");
  6730. NeedsKPIC = true;
  6731. break;
  6732. case llvm::Triple::arm:
  6733. case llvm::Triple::armeb:
  6734. case llvm::Triple::thumb:
  6735. case llvm::Triple::thumbeb: {
  6736. const llvm::Triple &Triple = getToolChain().getTriple();
  6737. switch (Triple.getSubArch()) {
  6738. case llvm::Triple::ARMSubArch_v7:
  6739. CmdArgs.push_back("-mfpu=neon");
  6740. break;
  6741. case llvm::Triple::ARMSubArch_v8:
  6742. CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8");
  6743. break;
  6744. default:
  6745. break;
  6746. }
  6747. StringRef ARMFloatABI = tools::arm::getARMFloatABI(
  6748. getToolChain().getDriver(), Args,
  6749. llvm::Triple(getToolChain().ComputeEffectiveClangTriple(Args)));
  6750. CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
  6751. Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
  6752. // FIXME: remove krait check when GNU tools support krait cpu
  6753. // for now replace it with -march=armv7-a to avoid a lower
  6754. // march from being picked in the absence of a cpu flag.
  6755. Arg *A;
  6756. if ((A = Args.getLastArg(options::OPT_mcpu_EQ)) &&
  6757. StringRef(A->getValue()).lower() == "krait")
  6758. CmdArgs.push_back("-march=armv7-a");
  6759. else
  6760. Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
  6761. Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
  6762. break;
  6763. }
  6764. case llvm::Triple::mips:
  6765. case llvm::Triple::mipsel:
  6766. case llvm::Triple::mips64:
  6767. case llvm::Triple::mips64el: {
  6768. StringRef CPUName;
  6769. StringRef ABIName;
  6770. mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  6771. ABIName = getGnuCompatibleMipsABIName(ABIName);
  6772. CmdArgs.push_back("-march");
  6773. CmdArgs.push_back(CPUName.data());
  6774. CmdArgs.push_back("-mabi");
  6775. CmdArgs.push_back(ABIName.data());
  6776. // -mno-shared should be emitted unless -fpic, -fpie, -fPIC, -fPIE,
  6777. // or -mshared (not implemented) is in effect.
  6778. bool IsPicOrPie = false;
  6779. if (Arg *A = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
  6780. options::OPT_fpic, options::OPT_fno_pic,
  6781. options::OPT_fPIE, options::OPT_fno_PIE,
  6782. options::OPT_fpie, options::OPT_fno_pie)) {
  6783. if (A->getOption().matches(options::OPT_fPIC) ||
  6784. A->getOption().matches(options::OPT_fpic) ||
  6785. A->getOption().matches(options::OPT_fPIE) ||
  6786. A->getOption().matches(options::OPT_fpie))
  6787. IsPicOrPie = true;
  6788. }
  6789. if (!IsPicOrPie)
  6790. CmdArgs.push_back("-mno-shared");
  6791. // LLVM doesn't support -mplt yet and acts as if it is always given.
  6792. // However, -mplt has no effect with the N64 ABI.
  6793. CmdArgs.push_back(ABIName == "64" ? "-KPIC" : "-call_nonpic");
  6794. if (getToolChain().getArch() == llvm::Triple::mips ||
  6795. getToolChain().getArch() == llvm::Triple::mips64)
  6796. CmdArgs.push_back("-EB");
  6797. else
  6798. CmdArgs.push_back("-EL");
  6799. if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
  6800. if (StringRef(A->getValue()) == "2008")
  6801. CmdArgs.push_back(Args.MakeArgString("-mnan=2008"));
  6802. }
  6803. // Add the last -mfp32/-mfpxx/-mfp64 or -mfpxx if it is enabled by default.
  6804. StringRef MIPSFloatABI = getMipsFloatABI(getToolChain().getDriver(), Args);
  6805. if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
  6806. options::OPT_mfp64)) {
  6807. A->claim();
  6808. A->render(Args, CmdArgs);
  6809. } else if (mips::shouldUseFPXX(Args, getToolChain().getTriple(), CPUName,
  6810. ABIName, MIPSFloatABI))
  6811. CmdArgs.push_back("-mfpxx");
  6812. // Pass on -mmips16 or -mno-mips16. However, the assembler equivalent of
  6813. // -mno-mips16 is actually -no-mips16.
  6814. if (Arg *A =
  6815. Args.getLastArg(options::OPT_mips16, options::OPT_mno_mips16)) {
  6816. if (A->getOption().matches(options::OPT_mips16)) {
  6817. A->claim();
  6818. A->render(Args, CmdArgs);
  6819. } else {
  6820. A->claim();
  6821. CmdArgs.push_back("-no-mips16");
  6822. }
  6823. }
  6824. Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
  6825. options::OPT_mno_micromips);
  6826. Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
  6827. Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
  6828. if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) {
  6829. // Do not use AddLastArg because not all versions of MIPS assembler
  6830. // support -mmsa / -mno-msa options.
  6831. if (A->getOption().matches(options::OPT_mmsa))
  6832. CmdArgs.push_back(Args.MakeArgString("-mmsa"));
  6833. }
  6834. Args.AddLastArg(CmdArgs, options::OPT_mhard_float,
  6835. options::OPT_msoft_float);
  6836. Args.AddLastArg(CmdArgs, options::OPT_mdouble_float,
  6837. options::OPT_msingle_float);
  6838. Args.AddLastArg(CmdArgs, options::OPT_modd_spreg,
  6839. options::OPT_mno_odd_spreg);
  6840. NeedsKPIC = true;
  6841. break;
  6842. }
  6843. case llvm::Triple::systemz: {
  6844. // Always pass an -march option, since our default of z10 is later
  6845. // than the GNU assembler's default.
  6846. StringRef CPUName = getSystemZTargetCPU(Args);
  6847. CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
  6848. break;
  6849. }
  6850. }
  6851. if (NeedsKPIC)
  6852. addAssemblerKPIC(Args, CmdArgs);
  6853. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  6854. CmdArgs.push_back("-o");
  6855. CmdArgs.push_back(Output.getFilename());
  6856. for (const auto &II : Inputs)
  6857. CmdArgs.push_back(II.getFilename());
  6858. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  6859. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  6860. // Handle the debug info splitting at object creation time if we're
  6861. // creating an object.
  6862. // TODO: Currently only works on linux with newer objcopy.
  6863. if (Args.hasArg(options::OPT_gsplit_dwarf) &&
  6864. getToolChain().getTriple().isOSLinux())
  6865. SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
  6866. SplitDebugName(Args, Inputs[0]));
  6867. }
  6868. static void AddLibgcc(const llvm::Triple &Triple, const Driver &D,
  6869. ArgStringList &CmdArgs, const ArgList &Args) {
  6870. bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
  6871. bool isCygMing = Triple.isOSCygMing();
  6872. bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
  6873. Args.hasArg(options::OPT_static);
  6874. if (!D.CCCIsCXX())
  6875. CmdArgs.push_back("-lgcc");
  6876. if (StaticLibgcc || isAndroid) {
  6877. if (D.CCCIsCXX())
  6878. CmdArgs.push_back("-lgcc");
  6879. } else {
  6880. if (!D.CCCIsCXX() && !isCygMing)
  6881. CmdArgs.push_back("--as-needed");
  6882. CmdArgs.push_back("-lgcc_s");
  6883. if (!D.CCCIsCXX() && !isCygMing)
  6884. CmdArgs.push_back("--no-as-needed");
  6885. }
  6886. if (StaticLibgcc && !isAndroid)
  6887. CmdArgs.push_back("-lgcc_eh");
  6888. else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
  6889. CmdArgs.push_back("-lgcc");
  6890. // According to Android ABI, we have to link with libdl if we are
  6891. // linking with non-static libgcc.
  6892. //
  6893. // NOTE: This fixes a link error on Android MIPS as well. The non-static
  6894. // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
  6895. if (isAndroid && !StaticLibgcc)
  6896. CmdArgs.push_back("-ldl");
  6897. }
  6898. static std::string getLinuxDynamicLinker(const ArgList &Args,
  6899. const toolchains::Linux &ToolChain) {
  6900. const llvm::Triple::ArchType Arch = ToolChain.getArch();
  6901. if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android) {
  6902. if (ToolChain.getTriple().isArch64Bit())
  6903. return "/system/bin/linker64";
  6904. else
  6905. return "/system/bin/linker";
  6906. } else if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::sparc ||
  6907. Arch == llvm::Triple::sparcel)
  6908. return "/lib/ld-linux.so.2";
  6909. else if (Arch == llvm::Triple::aarch64)
  6910. return "/lib/ld-linux-aarch64.so.1";
  6911. else if (Arch == llvm::Triple::aarch64_be)
  6912. return "/lib/ld-linux-aarch64_be.so.1";
  6913. else if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
  6914. if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF ||
  6915. tools::arm::getARMFloatABI(ToolChain.getDriver(), Args, ToolChain.getTriple()) == "hard")
  6916. return "/lib/ld-linux-armhf.so.3";
  6917. else
  6918. return "/lib/ld-linux.so.3";
  6919. } else if (Arch == llvm::Triple::armeb || Arch == llvm::Triple::thumbeb) {
  6920. // TODO: check which dynamic linker name.
  6921. if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF ||
  6922. tools::arm::getARMFloatABI(ToolChain.getDriver(), Args, ToolChain.getTriple()) == "hard")
  6923. return "/lib/ld-linux-armhf.so.3";
  6924. else
  6925. return "/lib/ld-linux.so.3";
  6926. } else if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel ||
  6927. Arch == llvm::Triple::mips64 || Arch == llvm::Triple::mips64el) {
  6928. StringRef CPUName;
  6929. StringRef ABIName;
  6930. mips::getMipsCPUAndABI(Args, ToolChain.getTriple(), CPUName, ABIName);
  6931. bool IsNaN2008 = mips::isNaN2008(Args, ToolChain.getTriple());
  6932. StringRef LibDir = llvm::StringSwitch<llvm::StringRef>(ABIName)
  6933. .Case("o32", "/lib")
  6934. .Case("n32", "/lib32")
  6935. .Case("n64", "/lib64")
  6936. .Default("/lib");
  6937. StringRef LibName;
  6938. if (mips::isUCLibc(Args))
  6939. LibName = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
  6940. else
  6941. LibName = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
  6942. return (LibDir + "/" + LibName).str();
  6943. } else if (Arch == llvm::Triple::ppc)
  6944. return "/lib/ld.so.1";
  6945. else if (Arch == llvm::Triple::ppc64) {
  6946. if (ppc::hasPPCAbiArg(Args, "elfv2"))
  6947. return "/lib64/ld64.so.2";
  6948. return "/lib64/ld64.so.1";
  6949. } else if (Arch == llvm::Triple::ppc64le) {
  6950. if (ppc::hasPPCAbiArg(Args, "elfv1"))
  6951. return "/lib64/ld64.so.1";
  6952. return "/lib64/ld64.so.2";
  6953. } else if (Arch == llvm::Triple::systemz)
  6954. return "/lib64/ld64.so.1";
  6955. else if (Arch == llvm::Triple::sparcv9)
  6956. return "/lib64/ld-linux.so.2";
  6957. else if (Arch == llvm::Triple::x86_64 &&
  6958. ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUX32)
  6959. return "/libx32/ld-linux-x32.so.2";
  6960. else
  6961. return "/lib64/ld-linux-x86-64.so.2";
  6962. }
  6963. static void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
  6964. ArgStringList &CmdArgs, const ArgList &Args) {
  6965. // Make use of compiler-rt if --rtlib option is used
  6966. ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args);
  6967. switch (RLT) {
  6968. case ToolChain::RLT_CompilerRT:
  6969. switch (TC.getTriple().getOS()) {
  6970. default:
  6971. llvm_unreachable("unsupported OS");
  6972. case llvm::Triple::Win32:
  6973. case llvm::Triple::Linux:
  6974. addClangRT(TC, Args, CmdArgs);
  6975. break;
  6976. }
  6977. break;
  6978. case ToolChain::RLT_Libgcc:
  6979. AddLibgcc(TC.getTriple(), D, CmdArgs, Args);
  6980. break;
  6981. }
  6982. }
  6983. static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
  6984. switch (T.getArch()) {
  6985. case llvm::Triple::x86:
  6986. return "elf_i386";
  6987. case llvm::Triple::aarch64:
  6988. return "aarch64linux";
  6989. case llvm::Triple::aarch64_be:
  6990. return "aarch64_be_linux";
  6991. case llvm::Triple::arm:
  6992. case llvm::Triple::thumb:
  6993. return "armelf_linux_eabi";
  6994. case llvm::Triple::armeb:
  6995. case llvm::Triple::thumbeb:
  6996. return "armebelf_linux_eabi"; /* TODO: check which NAME. */
  6997. case llvm::Triple::ppc:
  6998. return "elf32ppclinux";
  6999. case llvm::Triple::ppc64:
  7000. return "elf64ppc";
  7001. case llvm::Triple::ppc64le:
  7002. return "elf64lppc";
  7003. case llvm::Triple::sparc:
  7004. case llvm::Triple::sparcel:
  7005. return "elf32_sparc";
  7006. case llvm::Triple::sparcv9:
  7007. return "elf64_sparc";
  7008. case llvm::Triple::mips:
  7009. return "elf32btsmip";
  7010. case llvm::Triple::mipsel:
  7011. return "elf32ltsmip";
  7012. case llvm::Triple::mips64:
  7013. if (mips::hasMipsAbiArg(Args, "n32"))
  7014. return "elf32btsmipn32";
  7015. return "elf64btsmip";
  7016. case llvm::Triple::mips64el:
  7017. if (mips::hasMipsAbiArg(Args, "n32"))
  7018. return "elf32ltsmipn32";
  7019. return "elf64ltsmip";
  7020. case llvm::Triple::systemz:
  7021. return "elf64_s390";
  7022. case llvm::Triple::x86_64:
  7023. if (T.getEnvironment() == llvm::Triple::GNUX32)
  7024. return "elf32_x86_64";
  7025. return "elf_x86_64";
  7026. default:
  7027. llvm_unreachable("Unexpected arch");
  7028. }
  7029. }
  7030. void gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  7031. const InputInfo &Output,
  7032. const InputInfoList &Inputs,
  7033. const ArgList &Args,
  7034. const char *LinkingOutput) const {
  7035. const toolchains::Linux &ToolChain =
  7036. static_cast<const toolchains::Linux &>(getToolChain());
  7037. const Driver &D = ToolChain.getDriver();
  7038. const llvm::Triple::ArchType Arch = ToolChain.getArch();
  7039. const bool isAndroid =
  7040. ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
  7041. const bool IsPIE =
  7042. !Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_static) &&
  7043. (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
  7044. ArgStringList CmdArgs;
  7045. // Silence warning for "clang -g foo.o -o foo"
  7046. Args.ClaimAllArgs(options::OPT_g_Group);
  7047. // and "clang -emit-llvm foo.o -o foo"
  7048. Args.ClaimAllArgs(options::OPT_emit_llvm);
  7049. // and for "clang -w foo.o -o foo". Other warning options are already
  7050. // handled somewhere else.
  7051. Args.ClaimAllArgs(options::OPT_w);
  7052. if (!D.SysRoot.empty())
  7053. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  7054. if (IsPIE)
  7055. CmdArgs.push_back("-pie");
  7056. if (Args.hasArg(options::OPT_rdynamic))
  7057. CmdArgs.push_back("-export-dynamic");
  7058. if (Args.hasArg(options::OPT_s))
  7059. CmdArgs.push_back("-s");
  7060. if (Arch == llvm::Triple::armeb || Arch == llvm::Triple::thumbeb)
  7061. arm::appendEBLinkFlags(
  7062. Args, CmdArgs,
  7063. llvm::Triple(getToolChain().ComputeEffectiveClangTriple(Args)));
  7064. for (const auto &Opt : ToolChain.ExtraOpts)
  7065. CmdArgs.push_back(Opt.c_str());
  7066. if (!Args.hasArg(options::OPT_static)) {
  7067. CmdArgs.push_back("--eh-frame-hdr");
  7068. }
  7069. CmdArgs.push_back("-m");
  7070. CmdArgs.push_back(getLDMOption(ToolChain.getTriple(), Args));
  7071. if (Args.hasArg(options::OPT_static)) {
  7072. if (Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||
  7073. Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb)
  7074. CmdArgs.push_back("-Bstatic");
  7075. else
  7076. CmdArgs.push_back("-static");
  7077. } else if (Args.hasArg(options::OPT_shared)) {
  7078. CmdArgs.push_back("-shared");
  7079. }
  7080. if (Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||
  7081. Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb ||
  7082. (!Args.hasArg(options::OPT_static) &&
  7083. !Args.hasArg(options::OPT_shared))) {
  7084. CmdArgs.push_back("-dynamic-linker");
  7085. CmdArgs.push_back(Args.MakeArgString(
  7086. D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain)));
  7087. }
  7088. CmdArgs.push_back("-o");
  7089. CmdArgs.push_back(Output.getFilename());
  7090. if (!Args.hasArg(options::OPT_nostdlib) &&
  7091. !Args.hasArg(options::OPT_nostartfiles)) {
  7092. if (!isAndroid) {
  7093. const char *crt1 = nullptr;
  7094. if (!Args.hasArg(options::OPT_shared)) {
  7095. if (Args.hasArg(options::OPT_pg))
  7096. crt1 = "gcrt1.o";
  7097. else if (IsPIE)
  7098. crt1 = "Scrt1.o";
  7099. else
  7100. crt1 = "crt1.o";
  7101. }
  7102. if (crt1)
  7103. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
  7104. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  7105. }
  7106. const char *crtbegin;
  7107. if (Args.hasArg(options::OPT_static))
  7108. crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
  7109. else if (Args.hasArg(options::OPT_shared))
  7110. crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
  7111. else if (IsPIE)
  7112. crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
  7113. else
  7114. crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
  7115. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  7116. // Add crtfastmath.o if available and fast math is enabled.
  7117. ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
  7118. }
  7119. Args.AddAllArgs(CmdArgs, options::OPT_L);
  7120. Args.AddAllArgs(CmdArgs, options::OPT_u);
  7121. const ToolChain::path_list &Paths = ToolChain.getFilePaths();
  7122. for (const auto &Path : Paths)
  7123. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
  7124. if (D.IsUsingLTO(Args))
  7125. AddGoldPlugin(ToolChain, Args, CmdArgs);
  7126. if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
  7127. CmdArgs.push_back("--no-demangle");
  7128. bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
  7129. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  7130. // The profile runtime also needs access to system libraries.
  7131. addProfileRT(getToolChain(), Args, CmdArgs);
  7132. if (D.CCCIsCXX() && !Args.hasArg(options::OPT_nostdlib) &&
  7133. !Args.hasArg(options::OPT_nodefaultlibs)) {
  7134. bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
  7135. !Args.hasArg(options::OPT_static);
  7136. if (OnlyLibstdcxxStatic)
  7137. CmdArgs.push_back("-Bstatic");
  7138. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  7139. if (OnlyLibstdcxxStatic)
  7140. CmdArgs.push_back("-Bdynamic");
  7141. CmdArgs.push_back("-lm");
  7142. }
  7143. // Silence warnings when linking C code with a C++ '-stdlib' argument.
  7144. Args.ClaimAllArgs(options::OPT_stdlib_EQ);
  7145. if (!Args.hasArg(options::OPT_nostdlib)) {
  7146. if (!Args.hasArg(options::OPT_nodefaultlibs)) {
  7147. if (Args.hasArg(options::OPT_static))
  7148. CmdArgs.push_back("--start-group");
  7149. if (NeedsSanitizerDeps)
  7150. linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
  7151. bool WantPthread = Args.hasArg(options::OPT_pthread) ||
  7152. Args.hasArg(options::OPT_pthreads);
  7153. if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
  7154. options::OPT_fno_openmp, false)) {
  7155. // OpenMP runtimes implies pthreads when using the GNU toolchain.
  7156. // FIXME: Does this really make sense for all GNU toolchains?
  7157. WantPthread = true;
  7158. // Also link the particular OpenMP runtimes.
  7159. switch (getOpenMPRuntime(ToolChain, Args)) {
  7160. case OMPRT_OMP:
  7161. CmdArgs.push_back("-lomp");
  7162. break;
  7163. case OMPRT_GOMP:
  7164. CmdArgs.push_back("-lgomp");
  7165. // FIXME: Exclude this for platforms with libgomp that don't require
  7166. // librt. Most modern Linux platforms require it, but some may not.
  7167. CmdArgs.push_back("-lrt");
  7168. break;
  7169. case OMPRT_IOMP5:
  7170. CmdArgs.push_back("-liomp5");
  7171. break;
  7172. case OMPRT_Unknown:
  7173. // Already diagnosed.
  7174. break;
  7175. }
  7176. }
  7177. AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
  7178. if (WantPthread && !isAndroid)
  7179. CmdArgs.push_back("-lpthread");
  7180. CmdArgs.push_back("-lc");
  7181. if (Args.hasArg(options::OPT_static))
  7182. CmdArgs.push_back("--end-group");
  7183. else
  7184. AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
  7185. }
  7186. if (!Args.hasArg(options::OPT_nostartfiles)) {
  7187. const char *crtend;
  7188. if (Args.hasArg(options::OPT_shared))
  7189. crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
  7190. else if (IsPIE)
  7191. crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
  7192. else
  7193. crtend = isAndroid ? "crtend_android.o" : "crtend.o";
  7194. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
  7195. if (!isAndroid)
  7196. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  7197. }
  7198. }
  7199. C.addCommand(
  7200. llvm::make_unique<Command>(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
  7201. }
  7202. // NaCl ARM assembly (inline or standalone) can be written with a set of macros
  7203. // for the various SFI requirements like register masking. The assembly tool
  7204. // inserts the file containing the macros as an input into all the assembly
  7205. // jobs.
  7206. void nacltools::AssemblerARM::ConstructJob(Compilation &C, const JobAction &JA,
  7207. const InputInfo &Output,
  7208. const InputInfoList &Inputs,
  7209. const ArgList &Args,
  7210. const char *LinkingOutput) const {
  7211. const toolchains::NaCl_TC &ToolChain =
  7212. static_cast<const toolchains::NaCl_TC &>(getToolChain());
  7213. InputInfo NaClMacros(ToolChain.GetNaClArmMacrosPath(), types::TY_PP_Asm,
  7214. "nacl-arm-macros.s");
  7215. InputInfoList NewInputs;
  7216. NewInputs.push_back(NaClMacros);
  7217. NewInputs.append(Inputs.begin(), Inputs.end());
  7218. gnutools::Assembler::ConstructJob(C, JA, Output, NewInputs, Args,
  7219. LinkingOutput);
  7220. }
  7221. // This is quite similar to gnutools::Linker::ConstructJob with changes that
  7222. // we use static by default, do not yet support sanitizers or LTO, and a few
  7223. // others. Eventually we can support more of that and hopefully migrate back
  7224. // to gnutools::Linker.
  7225. void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  7226. const InputInfo &Output,
  7227. const InputInfoList &Inputs,
  7228. const ArgList &Args,
  7229. const char *LinkingOutput) const {
  7230. const toolchains::NaCl_TC &ToolChain =
  7231. static_cast<const toolchains::NaCl_TC &>(getToolChain());
  7232. const Driver &D = ToolChain.getDriver();
  7233. const llvm::Triple::ArchType Arch = ToolChain.getArch();
  7234. const bool IsStatic =
  7235. !Args.hasArg(options::OPT_dynamic) && !Args.hasArg(options::OPT_shared);
  7236. ArgStringList CmdArgs;
  7237. // Silence warning for "clang -g foo.o -o foo"
  7238. Args.ClaimAllArgs(options::OPT_g_Group);
  7239. // and "clang -emit-llvm foo.o -o foo"
  7240. Args.ClaimAllArgs(options::OPT_emit_llvm);
  7241. // and for "clang -w foo.o -o foo". Other warning options are already
  7242. // handled somewhere else.
  7243. Args.ClaimAllArgs(options::OPT_w);
  7244. if (!D.SysRoot.empty())
  7245. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  7246. if (Args.hasArg(options::OPT_rdynamic))
  7247. CmdArgs.push_back("-export-dynamic");
  7248. if (Args.hasArg(options::OPT_s))
  7249. CmdArgs.push_back("-s");
  7250. // NaCl_TC doesn't have ExtraOpts like Linux; the only relevant flag from
  7251. // there is --build-id, which we do want.
  7252. CmdArgs.push_back("--build-id");
  7253. if (!IsStatic)
  7254. CmdArgs.push_back("--eh-frame-hdr");
  7255. CmdArgs.push_back("-m");
  7256. if (Arch == llvm::Triple::x86)
  7257. CmdArgs.push_back("elf_i386_nacl");
  7258. else if (Arch == llvm::Triple::arm)
  7259. CmdArgs.push_back("armelf_nacl");
  7260. else if (Arch == llvm::Triple::x86_64)
  7261. CmdArgs.push_back("elf_x86_64_nacl");
  7262. else if (Arch == llvm::Triple::mipsel)
  7263. CmdArgs.push_back("mipselelf_nacl");
  7264. else
  7265. D.Diag(diag::err_target_unsupported_arch) << ToolChain.getArchName()
  7266. << "Native Client";
  7267. if (IsStatic)
  7268. CmdArgs.push_back("-static");
  7269. else if (Args.hasArg(options::OPT_shared))
  7270. CmdArgs.push_back("-shared");
  7271. CmdArgs.push_back("-o");
  7272. CmdArgs.push_back(Output.getFilename());
  7273. if (!Args.hasArg(options::OPT_nostdlib) &&
  7274. !Args.hasArg(options::OPT_nostartfiles)) {
  7275. if (!Args.hasArg(options::OPT_shared))
  7276. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
  7277. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  7278. const char *crtbegin;
  7279. if (IsStatic)
  7280. crtbegin = "crtbeginT.o";
  7281. else if (Args.hasArg(options::OPT_shared))
  7282. crtbegin = "crtbeginS.o";
  7283. else
  7284. crtbegin = "crtbegin.o";
  7285. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  7286. }
  7287. Args.AddAllArgs(CmdArgs, options::OPT_L);
  7288. Args.AddAllArgs(CmdArgs, options::OPT_u);
  7289. const ToolChain::path_list &Paths = ToolChain.getFilePaths();
  7290. for (const auto &Path : Paths)
  7291. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
  7292. if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
  7293. CmdArgs.push_back("--no-demangle");
  7294. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  7295. if (D.CCCIsCXX() && !Args.hasArg(options::OPT_nostdlib) &&
  7296. !Args.hasArg(options::OPT_nodefaultlibs)) {
  7297. bool OnlyLibstdcxxStatic =
  7298. Args.hasArg(options::OPT_static_libstdcxx) && !IsStatic;
  7299. if (OnlyLibstdcxxStatic)
  7300. CmdArgs.push_back("-Bstatic");
  7301. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  7302. if (OnlyLibstdcxxStatic)
  7303. CmdArgs.push_back("-Bdynamic");
  7304. CmdArgs.push_back("-lm");
  7305. }
  7306. if (!Args.hasArg(options::OPT_nostdlib)) {
  7307. if (!Args.hasArg(options::OPT_nodefaultlibs)) {
  7308. // Always use groups, since it has no effect on dynamic libraries.
  7309. CmdArgs.push_back("--start-group");
  7310. CmdArgs.push_back("-lc");
  7311. // NaCl's libc++ currently requires libpthread, so just always include it
  7312. // in the group for C++.
  7313. if (Args.hasArg(options::OPT_pthread) ||
  7314. Args.hasArg(options::OPT_pthreads) || D.CCCIsCXX()) {
  7315. // Gold, used by Mips, handles nested groups differently than ld, and
  7316. // without '-lnacl' it prefers symbols from libpthread.a over libnacl.a,
  7317. // which is not a desired behaviour here.
  7318. // See https://sourceware.org/ml/binutils/2015-03/msg00034.html
  7319. if (getToolChain().getArch() == llvm::Triple::mipsel)
  7320. CmdArgs.push_back("-lnacl");
  7321. CmdArgs.push_back("-lpthread");
  7322. }
  7323. CmdArgs.push_back("-lgcc");
  7324. CmdArgs.push_back("--as-needed");
  7325. if (IsStatic)
  7326. CmdArgs.push_back("-lgcc_eh");
  7327. else
  7328. CmdArgs.push_back("-lgcc_s");
  7329. CmdArgs.push_back("--no-as-needed");
  7330. // Mips needs to create and use pnacl_legacy library that contains
  7331. // definitions from bitcode/pnaclmm.c and definitions for
  7332. // __nacl_tp_tls_offset() and __nacl_tp_tdb_offset().
  7333. if (getToolChain().getArch() == llvm::Triple::mipsel)
  7334. CmdArgs.push_back("-lpnacl_legacy");
  7335. CmdArgs.push_back("--end-group");
  7336. }
  7337. if (!Args.hasArg(options::OPT_nostartfiles)) {
  7338. const char *crtend;
  7339. if (Args.hasArg(options::OPT_shared))
  7340. crtend = "crtendS.o";
  7341. else
  7342. crtend = "crtend.o";
  7343. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
  7344. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  7345. }
  7346. }
  7347. C.addCommand(
  7348. llvm::make_unique<Command>(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
  7349. }
  7350. void minix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  7351. const InputInfo &Output,
  7352. const InputInfoList &Inputs,
  7353. const ArgList &Args,
  7354. const char *LinkingOutput) const {
  7355. claimNoWarnArgs(Args);
  7356. ArgStringList CmdArgs;
  7357. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  7358. CmdArgs.push_back("-o");
  7359. CmdArgs.push_back(Output.getFilename());
  7360. for (const auto &II : Inputs)
  7361. CmdArgs.push_back(II.getFilename());
  7362. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  7363. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7364. }
  7365. void minix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  7366. const InputInfo &Output,
  7367. const InputInfoList &Inputs,
  7368. const ArgList &Args,
  7369. const char *LinkingOutput) const {
  7370. const Driver &D = getToolChain().getDriver();
  7371. ArgStringList CmdArgs;
  7372. if (Output.isFilename()) {
  7373. CmdArgs.push_back("-o");
  7374. CmdArgs.push_back(Output.getFilename());
  7375. } else {
  7376. assert(Output.isNothing() && "Invalid output.");
  7377. }
  7378. if (!Args.hasArg(options::OPT_nostdlib) &&
  7379. !Args.hasArg(options::OPT_nostartfiles)) {
  7380. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
  7381. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  7382. CmdArgs.push_back(
  7383. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  7384. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
  7385. }
  7386. Args.AddAllArgs(CmdArgs, options::OPT_L);
  7387. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  7388. Args.AddAllArgs(CmdArgs, options::OPT_e);
  7389. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  7390. addProfileRT(getToolChain(), Args, CmdArgs);
  7391. if (!Args.hasArg(options::OPT_nostdlib) &&
  7392. !Args.hasArg(options::OPT_nodefaultlibs)) {
  7393. if (D.CCCIsCXX()) {
  7394. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  7395. CmdArgs.push_back("-lm");
  7396. }
  7397. }
  7398. if (!Args.hasArg(options::OPT_nostdlib) &&
  7399. !Args.hasArg(options::OPT_nostartfiles)) {
  7400. if (Args.hasArg(options::OPT_pthread))
  7401. CmdArgs.push_back("-lpthread");
  7402. CmdArgs.push_back("-lc");
  7403. CmdArgs.push_back("-lCompilerRT-Generic");
  7404. CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
  7405. CmdArgs.push_back(
  7406. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  7407. }
  7408. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  7409. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7410. }
  7411. /// DragonFly Tools
  7412. // For now, DragonFly Assemble does just about the same as for
  7413. // FreeBSD, but this may change soon.
  7414. void dragonfly::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  7415. const InputInfo &Output,
  7416. const InputInfoList &Inputs,
  7417. const ArgList &Args,
  7418. const char *LinkingOutput) const {
  7419. claimNoWarnArgs(Args);
  7420. ArgStringList CmdArgs;
  7421. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  7422. // instruct as in the base system to assemble 32-bit code.
  7423. if (getToolChain().getArch() == llvm::Triple::x86)
  7424. CmdArgs.push_back("--32");
  7425. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  7426. CmdArgs.push_back("-o");
  7427. CmdArgs.push_back(Output.getFilename());
  7428. for (const auto &II : Inputs)
  7429. CmdArgs.push_back(II.getFilename());
  7430. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  7431. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7432. }
  7433. void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  7434. const InputInfo &Output,
  7435. const InputInfoList &Inputs,
  7436. const ArgList &Args,
  7437. const char *LinkingOutput) const {
  7438. const Driver &D = getToolChain().getDriver();
  7439. ArgStringList CmdArgs;
  7440. bool UseGCC47 = llvm::sys::fs::exists("/usr/lib/gcc47");
  7441. if (!D.SysRoot.empty())
  7442. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  7443. CmdArgs.push_back("--eh-frame-hdr");
  7444. if (Args.hasArg(options::OPT_static)) {
  7445. CmdArgs.push_back("-Bstatic");
  7446. } else {
  7447. if (Args.hasArg(options::OPT_rdynamic))
  7448. CmdArgs.push_back("-export-dynamic");
  7449. if (Args.hasArg(options::OPT_shared))
  7450. CmdArgs.push_back("-Bshareable");
  7451. else {
  7452. CmdArgs.push_back("-dynamic-linker");
  7453. CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
  7454. }
  7455. CmdArgs.push_back("--hash-style=both");
  7456. }
  7457. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  7458. // instruct ld in the base system to link 32-bit code.
  7459. if (getToolChain().getArch() == llvm::Triple::x86) {
  7460. CmdArgs.push_back("-m");
  7461. CmdArgs.push_back("elf_i386");
  7462. }
  7463. if (Output.isFilename()) {
  7464. CmdArgs.push_back("-o");
  7465. CmdArgs.push_back(Output.getFilename());
  7466. } else {
  7467. assert(Output.isNothing() && "Invalid output.");
  7468. }
  7469. if (!Args.hasArg(options::OPT_nostdlib) &&
  7470. !Args.hasArg(options::OPT_nostartfiles)) {
  7471. if (!Args.hasArg(options::OPT_shared)) {
  7472. if (Args.hasArg(options::OPT_pg))
  7473. CmdArgs.push_back(
  7474. Args.MakeArgString(getToolChain().GetFilePath("gcrt1.o")));
  7475. else {
  7476. if (Args.hasArg(options::OPT_pie))
  7477. CmdArgs.push_back(
  7478. Args.MakeArgString(getToolChain().GetFilePath("Scrt1.o")));
  7479. else
  7480. CmdArgs.push_back(
  7481. Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
  7482. }
  7483. }
  7484. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  7485. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  7486. CmdArgs.push_back(
  7487. Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
  7488. else
  7489. CmdArgs.push_back(
  7490. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  7491. }
  7492. Args.AddAllArgs(CmdArgs, options::OPT_L);
  7493. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  7494. Args.AddAllArgs(CmdArgs, options::OPT_e);
  7495. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  7496. if (!Args.hasArg(options::OPT_nostdlib) &&
  7497. !Args.hasArg(options::OPT_nodefaultlibs)) {
  7498. // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
  7499. // rpaths
  7500. if (UseGCC47)
  7501. CmdArgs.push_back("-L/usr/lib/gcc47");
  7502. else
  7503. CmdArgs.push_back("-L/usr/lib/gcc44");
  7504. if (!Args.hasArg(options::OPT_static)) {
  7505. if (UseGCC47) {
  7506. CmdArgs.push_back("-rpath");
  7507. CmdArgs.push_back("/usr/lib/gcc47");
  7508. } else {
  7509. CmdArgs.push_back("-rpath");
  7510. CmdArgs.push_back("/usr/lib/gcc44");
  7511. }
  7512. }
  7513. if (D.CCCIsCXX()) {
  7514. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  7515. CmdArgs.push_back("-lm");
  7516. }
  7517. if (Args.hasArg(options::OPT_pthread))
  7518. CmdArgs.push_back("-lpthread");
  7519. if (!Args.hasArg(options::OPT_nolibc)) {
  7520. CmdArgs.push_back("-lc");
  7521. }
  7522. if (UseGCC47) {
  7523. if (Args.hasArg(options::OPT_static) ||
  7524. Args.hasArg(options::OPT_static_libgcc)) {
  7525. CmdArgs.push_back("-lgcc");
  7526. CmdArgs.push_back("-lgcc_eh");
  7527. } else {
  7528. if (Args.hasArg(options::OPT_shared_libgcc)) {
  7529. CmdArgs.push_back("-lgcc_pic");
  7530. if (!Args.hasArg(options::OPT_shared))
  7531. CmdArgs.push_back("-lgcc");
  7532. } else {
  7533. CmdArgs.push_back("-lgcc");
  7534. CmdArgs.push_back("--as-needed");
  7535. CmdArgs.push_back("-lgcc_pic");
  7536. CmdArgs.push_back("--no-as-needed");
  7537. }
  7538. }
  7539. } else {
  7540. if (Args.hasArg(options::OPT_shared)) {
  7541. CmdArgs.push_back("-lgcc_pic");
  7542. } else {
  7543. CmdArgs.push_back("-lgcc");
  7544. }
  7545. }
  7546. }
  7547. if (!Args.hasArg(options::OPT_nostdlib) &&
  7548. !Args.hasArg(options::OPT_nostartfiles)) {
  7549. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  7550. CmdArgs.push_back(
  7551. Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
  7552. else
  7553. CmdArgs.push_back(
  7554. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  7555. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
  7556. }
  7557. addProfileRT(getToolChain(), Args, CmdArgs);
  7558. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  7559. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7560. }
  7561. // Try to find Exe from a Visual Studio distribution. This first tries to find
  7562. // an installed copy of Visual Studio and, failing that, looks in the PATH,
  7563. // making sure that whatever executable that's found is not a same-named exe
  7564. // from clang itself to prevent clang from falling back to itself.
  7565. static std::string FindVisualStudioExecutable(const ToolChain &TC,
  7566. const char *Exe,
  7567. const char *ClangProgramPath) {
  7568. const auto &MSVC = static_cast<const toolchains::MSVCToolChain &>(TC);
  7569. std::string visualStudioBinDir;
  7570. if (MSVC.getVisualStudioBinariesFolder(ClangProgramPath,
  7571. visualStudioBinDir)) {
  7572. SmallString<128> FilePath(visualStudioBinDir);
  7573. llvm::sys::path::append(FilePath, Exe);
  7574. if (llvm::sys::fs::can_execute(FilePath.c_str()))
  7575. return FilePath.str();
  7576. }
  7577. return Exe;
  7578. }
  7579. void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  7580. const InputInfo &Output,
  7581. const InputInfoList &Inputs,
  7582. const ArgList &Args,
  7583. const char *LinkingOutput) const {
  7584. ArgStringList CmdArgs;
  7585. const ToolChain &TC = getToolChain();
  7586. assert((Output.isFilename() || Output.isNothing()) && "invalid output");
  7587. if (Output.isFilename())
  7588. CmdArgs.push_back(
  7589. Args.MakeArgString(std::string("-out:") + Output.getFilename()));
  7590. if (!Args.hasArg(options::OPT_nostdlib) &&
  7591. !Args.hasArg(options::OPT_nostartfiles) && !C.getDriver().IsCLMode())
  7592. CmdArgs.push_back("-defaultlib:libcmt");
  7593. if (!llvm::sys::Process::GetEnv("LIB")) {
  7594. // If the VC environment hasn't been configured (perhaps because the user
  7595. // did not run vcvarsall), try to build a consistent link environment. If
  7596. // the environment variable is set however, assume the user knows what
  7597. // they're doing.
  7598. std::string VisualStudioDir;
  7599. const auto &MSVC = static_cast<const toolchains::MSVCToolChain &>(TC);
  7600. if (MSVC.getVisualStudioInstallDir(VisualStudioDir)) {
  7601. SmallString<128> LibDir(VisualStudioDir);
  7602. llvm::sys::path::append(LibDir, "VC", "lib");
  7603. switch (MSVC.getArch()) {
  7604. case llvm::Triple::x86:
  7605. // x86 just puts the libraries directly in lib
  7606. break;
  7607. case llvm::Triple::x86_64:
  7608. llvm::sys::path::append(LibDir, "amd64");
  7609. break;
  7610. case llvm::Triple::arm:
  7611. llvm::sys::path::append(LibDir, "arm");
  7612. break;
  7613. default:
  7614. break;
  7615. }
  7616. CmdArgs.push_back(
  7617. Args.MakeArgString(std::string("-libpath:") + LibDir.c_str()));
  7618. }
  7619. std::string WindowsSdkLibPath;
  7620. if (MSVC.getWindowsSDKLibraryPath(WindowsSdkLibPath))
  7621. CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
  7622. WindowsSdkLibPath.c_str()));
  7623. }
  7624. CmdArgs.push_back("-nologo");
  7625. if (Args.hasArg(options::OPT_g_Group))
  7626. CmdArgs.push_back("-debug");
  7627. bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd,
  7628. options::OPT_shared);
  7629. if (DLL) {
  7630. CmdArgs.push_back(Args.MakeArgString("-dll"));
  7631. SmallString<128> ImplibName(Output.getFilename());
  7632. llvm::sys::path::replace_extension(ImplibName, "lib");
  7633. CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") + ImplibName));
  7634. }
  7635. if (TC.getSanitizerArgs().needsAsanRt()) {
  7636. CmdArgs.push_back(Args.MakeArgString("-debug"));
  7637. CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
  7638. if (Args.hasArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd)) {
  7639. static const char *CompilerRTComponents[] = {
  7640. "asan_dynamic", "asan_dynamic_runtime_thunk",
  7641. };
  7642. for (const auto &Component : CompilerRTComponents)
  7643. CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, Component)));
  7644. // Make sure the dynamic runtime thunk is not optimized out at link time
  7645. // to ensure proper SEH handling.
  7646. CmdArgs.push_back(Args.MakeArgString("-include:___asan_seh_interceptor"));
  7647. } else if (DLL) {
  7648. CmdArgs.push_back(
  7649. Args.MakeArgString(getCompilerRT(TC, "asan_dll_thunk")));
  7650. } else {
  7651. static const char *CompilerRTComponents[] = {
  7652. "asan", "asan_cxx",
  7653. };
  7654. for (const auto &Component : CompilerRTComponents)
  7655. CmdArgs.push_back(Args.MakeArgString(getCompilerRT(TC, Component)));
  7656. }
  7657. }
  7658. Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
  7659. // Add filenames, libraries, and other linker inputs.
  7660. for (const auto &Input : Inputs) {
  7661. if (Input.isFilename()) {
  7662. CmdArgs.push_back(Input.getFilename());
  7663. continue;
  7664. }
  7665. const Arg &A = Input.getInputArg();
  7666. // Render -l options differently for the MSVC linker.
  7667. if (A.getOption().matches(options::OPT_l)) {
  7668. StringRef Lib = A.getValue();
  7669. const char *LinkLibArg;
  7670. if (Lib.endswith(".lib"))
  7671. LinkLibArg = Args.MakeArgString(Lib);
  7672. else
  7673. LinkLibArg = Args.MakeArgString(Lib + ".lib");
  7674. CmdArgs.push_back(LinkLibArg);
  7675. continue;
  7676. }
  7677. // Otherwise, this is some other kind of linker input option like -Wl, -z,
  7678. // or -L. Render it, even if MSVC doesn't understand it.
  7679. A.renderAsInput(Args, CmdArgs);
  7680. }
  7681. // We need to special case some linker paths. In the case of lld, we need to
  7682. // translate 'lld' into 'lld-link', and in the case of the regular msvc
  7683. // linker, we need to use a special search algorithm.
  7684. llvm::SmallString<128> linkPath;
  7685. StringRef Linker = Args.getLastArgValue(options::OPT_fuse_ld_EQ, "link");
  7686. if (Linker.equals_lower("lld"))
  7687. Linker = "lld-link";
  7688. if (Linker.equals_lower("link")) {
  7689. // If we're using the MSVC linker, it's not sufficient to just use link
  7690. // from the program PATH, because other environments like GnuWin32 install
  7691. // their own link.exe which may come first.
  7692. linkPath = FindVisualStudioExecutable(TC, "link.exe",
  7693. C.getDriver().getClangProgramPath());
  7694. } else {
  7695. linkPath = Linker;
  7696. llvm::sys::path::replace_extension(linkPath, "exe");
  7697. linkPath = TC.GetProgramPath(linkPath.c_str());
  7698. }
  7699. const char *Exec = Args.MakeArgString(linkPath);
  7700. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7701. }
  7702. void visualstudio::Compiler::ConstructJob(Compilation &C, const JobAction &JA,
  7703. const InputInfo &Output,
  7704. const InputInfoList &Inputs,
  7705. const ArgList &Args,
  7706. const char *LinkingOutput) const {
  7707. C.addCommand(GetCommand(C, JA, Output, Inputs, Args, LinkingOutput));
  7708. }
  7709. std::unique_ptr<Command> visualstudio::Compiler::GetCommand(
  7710. Compilation &C, const JobAction &JA, const InputInfo &Output,
  7711. const InputInfoList &Inputs, const ArgList &Args,
  7712. const char *LinkingOutput) const {
  7713. ArgStringList CmdArgs;
  7714. CmdArgs.push_back("/nologo");
  7715. CmdArgs.push_back("/c"); // Compile only.
  7716. CmdArgs.push_back("/W0"); // No warnings.
  7717. // The goal is to be able to invoke this tool correctly based on
  7718. // any flag accepted by clang-cl.
  7719. // These are spelled the same way in clang and cl.exe,.
  7720. Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
  7721. Args.AddAllArgs(CmdArgs, options::OPT_I);
  7722. // Optimization level.
  7723. if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) {
  7724. if (A->getOption().getID() == options::OPT_O0) {
  7725. CmdArgs.push_back("/Od");
  7726. } else {
  7727. StringRef OptLevel = A->getValue();
  7728. if (OptLevel == "1" || OptLevel == "2" || OptLevel == "s")
  7729. A->render(Args, CmdArgs);
  7730. else if (OptLevel == "3")
  7731. CmdArgs.push_back("/Ox");
  7732. }
  7733. }
  7734. // Flags for which clang-cl has an alias.
  7735. // FIXME: How can we ensure this stays in sync with relevant clang-cl options?
  7736. if (Args.hasFlag(options::OPT__SLASH_GR_, options::OPT__SLASH_GR,
  7737. /*default=*/false))
  7738. CmdArgs.push_back("/GR-");
  7739. if (Arg *A = Args.getLastArg(options::OPT_ffunction_sections,
  7740. options::OPT_fno_function_sections))
  7741. CmdArgs.push_back(A->getOption().getID() == options::OPT_ffunction_sections
  7742. ? "/Gy"
  7743. : "/Gy-");
  7744. if (Arg *A = Args.getLastArg(options::OPT_fdata_sections,
  7745. options::OPT_fno_data_sections))
  7746. CmdArgs.push_back(
  7747. A->getOption().getID() == options::OPT_fdata_sections ? "/Gw" : "/Gw-");
  7748. if (Args.hasArg(options::OPT_fsyntax_only))
  7749. CmdArgs.push_back("/Zs");
  7750. if (Args.hasArg(options::OPT_g_Flag, options::OPT_gline_tables_only))
  7751. CmdArgs.push_back("/Z7");
  7752. std::vector<std::string> Includes =
  7753. Args.getAllArgValues(options::OPT_include);
  7754. for (const auto &Include : Includes)
  7755. CmdArgs.push_back(Args.MakeArgString(std::string("/FI") + Include));
  7756. // Flags that can simply be passed through.
  7757. Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LD);
  7758. Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LDd);
  7759. Args.AddAllArgs(CmdArgs, options::OPT__SLASH_EH);
  7760. // The order of these flags is relevant, so pick the last one.
  7761. if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd,
  7762. options::OPT__SLASH_MT, options::OPT__SLASH_MTd))
  7763. A->render(Args, CmdArgs);
  7764. // Input filename.
  7765. assert(Inputs.size() == 1);
  7766. const InputInfo &II = Inputs[0];
  7767. assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX);
  7768. CmdArgs.push_back(II.getType() == types::TY_C ? "/Tc" : "/Tp");
  7769. if (II.isFilename())
  7770. CmdArgs.push_back(II.getFilename());
  7771. else
  7772. II.getInputArg().renderAsInput(Args, CmdArgs);
  7773. // Output filename.
  7774. assert(Output.getType() == types::TY_Object);
  7775. const char *Fo =
  7776. Args.MakeArgString(std::string("/Fo") + Output.getFilename());
  7777. CmdArgs.push_back(Fo);
  7778. const Driver &D = getToolChain().getDriver();
  7779. std::string Exec = FindVisualStudioExecutable(getToolChain(), "cl.exe",
  7780. D.getClangProgramPath());
  7781. return llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec),
  7782. CmdArgs);
  7783. }
  7784. /// MinGW Tools
  7785. void MinGW::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  7786. const InputInfo &Output,
  7787. const InputInfoList &Inputs,
  7788. const ArgList &Args,
  7789. const char *LinkingOutput) const {
  7790. claimNoWarnArgs(Args);
  7791. ArgStringList CmdArgs;
  7792. if (getToolChain().getArch() == llvm::Triple::x86) {
  7793. CmdArgs.push_back("--32");
  7794. } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
  7795. CmdArgs.push_back("--64");
  7796. }
  7797. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  7798. CmdArgs.push_back("-o");
  7799. CmdArgs.push_back(Output.getFilename());
  7800. for (const auto &II : Inputs)
  7801. CmdArgs.push_back(II.getFilename());
  7802. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  7803. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7804. if (Args.hasArg(options::OPT_gsplit_dwarf))
  7805. SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
  7806. SplitDebugName(Args, Inputs[0]));
  7807. }
  7808. void MinGW::Linker::AddLibGCC(const ArgList &Args,
  7809. ArgStringList &CmdArgs) const {
  7810. if (Args.hasArg(options::OPT_mthreads))
  7811. CmdArgs.push_back("-lmingwthrd");
  7812. CmdArgs.push_back("-lmingw32");
  7813. // Add libgcc or compiler-rt.
  7814. AddRunTimeLibs(getToolChain(), getToolChain().getDriver(), CmdArgs, Args);
  7815. CmdArgs.push_back("-lmoldname");
  7816. CmdArgs.push_back("-lmingwex");
  7817. CmdArgs.push_back("-lmsvcrt");
  7818. }
  7819. void MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  7820. const InputInfo &Output,
  7821. const InputInfoList &Inputs,
  7822. const ArgList &Args,
  7823. const char *LinkingOutput) const {
  7824. const ToolChain &TC = getToolChain();
  7825. const Driver &D = TC.getDriver();
  7826. // const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
  7827. ArgStringList CmdArgs;
  7828. // Silence warning for "clang -g foo.o -o foo"
  7829. Args.ClaimAllArgs(options::OPT_g_Group);
  7830. // and "clang -emit-llvm foo.o -o foo"
  7831. Args.ClaimAllArgs(options::OPT_emit_llvm);
  7832. // and for "clang -w foo.o -o foo". Other warning options are already
  7833. // handled somewhere else.
  7834. Args.ClaimAllArgs(options::OPT_w);
  7835. StringRef LinkerName = Args.getLastArgValue(options::OPT_fuse_ld_EQ, "ld");
  7836. if (LinkerName.equals_lower("lld")) {
  7837. CmdArgs.push_back("-flavor");
  7838. CmdArgs.push_back("gnu");
  7839. }
  7840. if (!D.SysRoot.empty())
  7841. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  7842. if (Args.hasArg(options::OPT_s))
  7843. CmdArgs.push_back("-s");
  7844. CmdArgs.push_back("-m");
  7845. if (TC.getArch() == llvm::Triple::x86)
  7846. CmdArgs.push_back("i386pe");
  7847. if (TC.getArch() == llvm::Triple::x86_64)
  7848. CmdArgs.push_back("i386pep");
  7849. if (TC.getArch() == llvm::Triple::arm)
  7850. CmdArgs.push_back("thumb2pe");
  7851. if (Args.hasArg(options::OPT_mwindows)) {
  7852. CmdArgs.push_back("--subsystem");
  7853. CmdArgs.push_back("windows");
  7854. } else if (Args.hasArg(options::OPT_mconsole)) {
  7855. CmdArgs.push_back("--subsystem");
  7856. CmdArgs.push_back("console");
  7857. }
  7858. if (Args.hasArg(options::OPT_static))
  7859. CmdArgs.push_back("-Bstatic");
  7860. else {
  7861. if (Args.hasArg(options::OPT_mdll))
  7862. CmdArgs.push_back("--dll");
  7863. else if (Args.hasArg(options::OPT_shared))
  7864. CmdArgs.push_back("--shared");
  7865. CmdArgs.push_back("-Bdynamic");
  7866. if (Args.hasArg(options::OPT_mdll) || Args.hasArg(options::OPT_shared)) {
  7867. CmdArgs.push_back("-e");
  7868. if (TC.getArch() == llvm::Triple::x86)
  7869. CmdArgs.push_back("_DllMainCRTStartup@12");
  7870. else
  7871. CmdArgs.push_back("DllMainCRTStartup");
  7872. CmdArgs.push_back("--enable-auto-image-base");
  7873. }
  7874. }
  7875. CmdArgs.push_back("-o");
  7876. CmdArgs.push_back(Output.getFilename());
  7877. Args.AddAllArgs(CmdArgs, options::OPT_e);
  7878. // FIXME: add -N, -n flags
  7879. Args.AddLastArg(CmdArgs, options::OPT_r);
  7880. Args.AddLastArg(CmdArgs, options::OPT_s);
  7881. Args.AddLastArg(CmdArgs, options::OPT_t);
  7882. Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
  7883. Args.AddLastArg(CmdArgs, options::OPT_Z_Flag);
  7884. if (!Args.hasArg(options::OPT_nostdlib) &&
  7885. !Args.hasArg(options::OPT_nostartfiles)) {
  7886. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_mdll)) {
  7887. CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("dllcrt2.o")));
  7888. } else {
  7889. if (Args.hasArg(options::OPT_municode))
  7890. CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2u.o")));
  7891. else
  7892. CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2.o")));
  7893. }
  7894. if (Args.hasArg(options::OPT_pg))
  7895. CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("gcrt2.o")));
  7896. CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtbegin.o")));
  7897. }
  7898. Args.AddAllArgs(CmdArgs, options::OPT_L);
  7899. const ToolChain::path_list Paths = TC.getFilePaths();
  7900. for (const auto &Path : Paths)
  7901. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
  7902. AddLinkerInputs(TC, Inputs, Args, CmdArgs);
  7903. // TODO: Add ASan stuff here
  7904. // TODO: Add profile stuff here
  7905. if (D.CCCIsCXX() && !Args.hasArg(options::OPT_nostdlib) &&
  7906. !Args.hasArg(options::OPT_nodefaultlibs)) {
  7907. bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
  7908. !Args.hasArg(options::OPT_static);
  7909. if (OnlyLibstdcxxStatic)
  7910. CmdArgs.push_back("-Bstatic");
  7911. TC.AddCXXStdlibLibArgs(Args, CmdArgs);
  7912. if (OnlyLibstdcxxStatic)
  7913. CmdArgs.push_back("-Bdynamic");
  7914. }
  7915. if (!Args.hasArg(options::OPT_nostdlib)) {
  7916. if (!Args.hasArg(options::OPT_nodefaultlibs)) {
  7917. if (Args.hasArg(options::OPT_static))
  7918. CmdArgs.push_back("--start-group");
  7919. if (Args.hasArg(options::OPT_fstack_protector) ||
  7920. Args.hasArg(options::OPT_fstack_protector_strong) ||
  7921. Args.hasArg(options::OPT_fstack_protector_all)) {
  7922. CmdArgs.push_back("-lssp_nonshared");
  7923. CmdArgs.push_back("-lssp");
  7924. }
  7925. if (Args.hasArg(options::OPT_fopenmp))
  7926. CmdArgs.push_back("-lgomp");
  7927. AddLibGCC(Args, CmdArgs);
  7928. if (Args.hasArg(options::OPT_pg))
  7929. CmdArgs.push_back("-lgmon");
  7930. if (Args.hasArg(options::OPT_pthread))
  7931. CmdArgs.push_back("-lpthread");
  7932. // add system libraries
  7933. if (Args.hasArg(options::OPT_mwindows)) {
  7934. CmdArgs.push_back("-lgdi32");
  7935. CmdArgs.push_back("-lcomdlg32");
  7936. }
  7937. CmdArgs.push_back("-ladvapi32");
  7938. CmdArgs.push_back("-lshell32");
  7939. CmdArgs.push_back("-luser32");
  7940. CmdArgs.push_back("-lkernel32");
  7941. if (Args.hasArg(options::OPT_static))
  7942. CmdArgs.push_back("--end-group");
  7943. else if (!LinkerName.equals_lower("lld"))
  7944. AddLibGCC(Args, CmdArgs);
  7945. }
  7946. if (!Args.hasArg(options::OPT_nostartfiles)) {
  7947. // Add crtfastmath.o if available and fast math is enabled.
  7948. TC.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
  7949. CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtend.o")));
  7950. }
  7951. }
  7952. const char *Exec = Args.MakeArgString(TC.GetProgramPath(LinkerName.data()));
  7953. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7954. }
  7955. /// XCore Tools
  7956. // We pass assemble and link construction to the xcc tool.
  7957. void XCore::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  7958. const InputInfo &Output,
  7959. const InputInfoList &Inputs,
  7960. const ArgList &Args,
  7961. const char *LinkingOutput) const {
  7962. claimNoWarnArgs(Args);
  7963. ArgStringList CmdArgs;
  7964. CmdArgs.push_back("-o");
  7965. CmdArgs.push_back(Output.getFilename());
  7966. CmdArgs.push_back("-c");
  7967. if (Args.hasArg(options::OPT_v))
  7968. CmdArgs.push_back("-v");
  7969. if (Arg *A = Args.getLastArg(options::OPT_g_Group))
  7970. if (!A->getOption().matches(options::OPT_g0))
  7971. CmdArgs.push_back("-g");
  7972. if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
  7973. false))
  7974. CmdArgs.push_back("-fverbose-asm");
  7975. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  7976. for (const auto &II : Inputs)
  7977. CmdArgs.push_back(II.getFilename());
  7978. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
  7979. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  7980. }
  7981. void XCore::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  7982. const InputInfo &Output,
  7983. const InputInfoList &Inputs,
  7984. const ArgList &Args,
  7985. const char *LinkingOutput) const {
  7986. ArgStringList CmdArgs;
  7987. if (Output.isFilename()) {
  7988. CmdArgs.push_back("-o");
  7989. CmdArgs.push_back(Output.getFilename());
  7990. } else {
  7991. assert(Output.isNothing() && "Invalid output.");
  7992. }
  7993. if (Args.hasArg(options::OPT_v))
  7994. CmdArgs.push_back("-v");
  7995. // Pass -fexceptions through to the linker if it was present.
  7996. if (Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
  7997. false))
  7998. CmdArgs.push_back("-fexceptions");
  7999. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  8000. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
  8001. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  8002. }
  8003. void CrossWindows::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  8004. const InputInfo &Output,
  8005. const InputInfoList &Inputs,
  8006. const ArgList &Args,
  8007. const char *LinkingOutput) const {
  8008. claimNoWarnArgs(Args);
  8009. const auto &TC =
  8010. static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
  8011. ArgStringList CmdArgs;
  8012. const char *Exec;
  8013. switch (TC.getArch()) {
  8014. default:
  8015. llvm_unreachable("unsupported architecture");
  8016. case llvm::Triple::arm:
  8017. case llvm::Triple::thumb:
  8018. break;
  8019. case llvm::Triple::x86:
  8020. CmdArgs.push_back("--32");
  8021. break;
  8022. case llvm::Triple::x86_64:
  8023. CmdArgs.push_back("--64");
  8024. break;
  8025. }
  8026. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  8027. CmdArgs.push_back("-o");
  8028. CmdArgs.push_back(Output.getFilename());
  8029. for (const auto &Input : Inputs)
  8030. CmdArgs.push_back(Input.getFilename());
  8031. const std::string Assembler = TC.GetProgramPath("as");
  8032. Exec = Args.MakeArgString(Assembler);
  8033. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  8034. }
  8035. void CrossWindows::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  8036. const InputInfo &Output,
  8037. const InputInfoList &Inputs,
  8038. const ArgList &Args,
  8039. const char *LinkingOutput) const {
  8040. const auto &TC =
  8041. static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
  8042. const llvm::Triple &T = TC.getTriple();
  8043. const Driver &D = TC.getDriver();
  8044. SmallString<128> EntryPoint;
  8045. ArgStringList CmdArgs;
  8046. const char *Exec;
  8047. // Silence warning for "clang -g foo.o -o foo"
  8048. Args.ClaimAllArgs(options::OPT_g_Group);
  8049. // and "clang -emit-llvm foo.o -o foo"
  8050. Args.ClaimAllArgs(options::OPT_emit_llvm);
  8051. // and for "clang -w foo.o -o foo"
  8052. Args.ClaimAllArgs(options::OPT_w);
  8053. // Other warning options are already handled somewhere else.
  8054. if (!D.SysRoot.empty())
  8055. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  8056. if (Args.hasArg(options::OPT_pie))
  8057. CmdArgs.push_back("-pie");
  8058. if (Args.hasArg(options::OPT_rdynamic))
  8059. CmdArgs.push_back("-export-dynamic");
  8060. if (Args.hasArg(options::OPT_s))
  8061. CmdArgs.push_back("--strip-all");
  8062. CmdArgs.push_back("-m");
  8063. switch (TC.getArch()) {
  8064. default:
  8065. llvm_unreachable("unsupported architecture");
  8066. case llvm::Triple::arm:
  8067. case llvm::Triple::thumb:
  8068. // FIXME: this is incorrect for WinCE
  8069. CmdArgs.push_back("thumb2pe");
  8070. break;
  8071. case llvm::Triple::x86:
  8072. CmdArgs.push_back("i386pe");
  8073. EntryPoint.append("_");
  8074. break;
  8075. case llvm::Triple::x86_64:
  8076. CmdArgs.push_back("i386pep");
  8077. break;
  8078. }
  8079. if (Args.hasArg(options::OPT_shared)) {
  8080. switch (T.getArch()) {
  8081. default:
  8082. llvm_unreachable("unsupported architecture");
  8083. case llvm::Triple::arm:
  8084. case llvm::Triple::thumb:
  8085. case llvm::Triple::x86_64:
  8086. EntryPoint.append("_DllMainCRTStartup");
  8087. break;
  8088. case llvm::Triple::x86:
  8089. EntryPoint.append("_DllMainCRTStartup@12");
  8090. break;
  8091. }
  8092. CmdArgs.push_back("-shared");
  8093. CmdArgs.push_back("-Bdynamic");
  8094. CmdArgs.push_back("--enable-auto-image-base");
  8095. CmdArgs.push_back("--entry");
  8096. CmdArgs.push_back(Args.MakeArgString(EntryPoint));
  8097. } else {
  8098. EntryPoint.append("mainCRTStartup");
  8099. CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
  8100. : "-Bdynamic");
  8101. if (!Args.hasArg(options::OPT_nostdlib) &&
  8102. !Args.hasArg(options::OPT_nostartfiles)) {
  8103. CmdArgs.push_back("--entry");
  8104. CmdArgs.push_back(Args.MakeArgString(EntryPoint));
  8105. }
  8106. // FIXME: handle subsystem
  8107. }
  8108. // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
  8109. CmdArgs.push_back("--allow-multiple-definition");
  8110. CmdArgs.push_back("-o");
  8111. CmdArgs.push_back(Output.getFilename());
  8112. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_rdynamic)) {
  8113. SmallString<261> ImpLib(Output.getFilename());
  8114. llvm::sys::path::replace_extension(ImpLib, ".lib");
  8115. CmdArgs.push_back("--out-implib");
  8116. CmdArgs.push_back(Args.MakeArgString(ImpLib));
  8117. }
  8118. if (!Args.hasArg(options::OPT_nostdlib) &&
  8119. !Args.hasArg(options::OPT_nostartfiles)) {
  8120. const std::string CRTPath(D.SysRoot + "/usr/lib/");
  8121. const char *CRTBegin;
  8122. CRTBegin =
  8123. Args.hasArg(options::OPT_shared) ? "crtbeginS.obj" : "crtbegin.obj";
  8124. CmdArgs.push_back(Args.MakeArgString(CRTPath + CRTBegin));
  8125. }
  8126. Args.AddAllArgs(CmdArgs, options::OPT_L);
  8127. const auto &Paths = TC.getFilePaths();
  8128. for (const auto &Path : Paths)
  8129. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
  8130. AddLinkerInputs(TC, Inputs, Args, CmdArgs);
  8131. if (D.CCCIsCXX() && !Args.hasArg(options::OPT_nostdlib) &&
  8132. !Args.hasArg(options::OPT_nodefaultlibs)) {
  8133. bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
  8134. !Args.hasArg(options::OPT_static);
  8135. if (StaticCXX)
  8136. CmdArgs.push_back("-Bstatic");
  8137. TC.AddCXXStdlibLibArgs(Args, CmdArgs);
  8138. if (StaticCXX)
  8139. CmdArgs.push_back("-Bdynamic");
  8140. }
  8141. if (!Args.hasArg(options::OPT_nostdlib)) {
  8142. if (!Args.hasArg(options::OPT_nodefaultlibs)) {
  8143. // TODO handle /MT[d] /MD[d]
  8144. CmdArgs.push_back("-lmsvcrt");
  8145. AddRunTimeLibs(TC, D, CmdArgs, Args);
  8146. }
  8147. }
  8148. const std::string Linker = TC.GetProgramPath("ld");
  8149. Exec = Args.MakeArgString(Linker);
  8150. C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs));
  8151. }
  8152. void tools::SHAVE::Compiler::ConstructJob(Compilation &C, const JobAction &JA,
  8153. const InputInfo &Output,
  8154. const InputInfoList &Inputs,
  8155. const ArgList &Args,
  8156. const char *LinkingOutput) const {
  8157. ArgStringList CmdArgs;
  8158. assert(Inputs.size() == 1);
  8159. const InputInfo &II = Inputs[0];
  8160. assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX);
  8161. assert(Output.getType() == types::TY_PP_Asm); // Require preprocessed asm.
  8162. // Append all -I, -iquote, -isystem paths.
  8163. Args.AddAllArgs(CmdArgs, options::OPT_clang_i_Group);
  8164. // These are spelled the same way in clang and moviCompile.
  8165. Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
  8166. CmdArgs.push_back("-DMYRIAD2");
  8167. CmdArgs.push_back("-mcpu=myriad2");
  8168. CmdArgs.push_back("-S");
  8169. // Any -O option passes through without translation. What about -Ofast ?
  8170. if (Arg *A = Args.getLastArg(options::OPT_O_Group))
  8171. A->render(Args, CmdArgs);
  8172. if (Args.hasFlag(options::OPT_ffunction_sections,
  8173. options::OPT_fno_function_sections)) {
  8174. CmdArgs.push_back("-ffunction-sections");
  8175. }
  8176. if (Args.hasArg(options::OPT_fno_inline_functions))
  8177. CmdArgs.push_back("-fno-inline-functions");
  8178. CmdArgs.push_back("-fno-exceptions"); // Always do this even if unspecified.
  8179. CmdArgs.push_back(II.getFilename());
  8180. CmdArgs.push_back("-o");
  8181. CmdArgs.push_back(Output.getFilename());
  8182. std::string Exec =
  8183. Args.MakeArgString(getToolChain().GetProgramPath("moviCompile"));
  8184. C.addCommand(
  8185. llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec), CmdArgs));
  8186. }
  8187. void tools::SHAVE::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  8188. const InputInfo &Output,
  8189. const InputInfoList &Inputs,
  8190. const ArgList &Args,
  8191. const char *LinkingOutput) const {
  8192. ArgStringList CmdArgs;
  8193. assert(Inputs.size() == 1);
  8194. const InputInfo &II = Inputs[0];
  8195. assert(II.getType() == types::TY_PP_Asm); // Require preprocessed asm input.
  8196. assert(Output.getType() == types::TY_Object);
  8197. CmdArgs.push_back("-no6thSlotCompression");
  8198. CmdArgs.push_back("-cv:myriad2"); // Chip Version ?
  8199. CmdArgs.push_back("-noSPrefixing");
  8200. CmdArgs.push_back("-a"); // Mystery option.
  8201. for (auto Arg : Args.filtered(options::OPT_I)) {
  8202. Arg->claim();
  8203. CmdArgs.push_back(
  8204. Args.MakeArgString(std::string("-i:") + Arg->getValue(0)));
  8205. }
  8206. CmdArgs.push_back("-elf"); // Output format.
  8207. CmdArgs.push_back(II.getFilename());
  8208. CmdArgs.push_back(
  8209. Args.MakeArgString(std::string("-o:") + Output.getFilename()));
  8210. std::string Exec =
  8211. Args.MakeArgString(getToolChain().GetProgramPath("moviAsm"));
  8212. C.addCommand(
  8213. llvm::make_unique<Command>(JA, *this, Args.MakeArgString(Exec), CmdArgs));
  8214. }