Tools.cpp 280 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763
  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/LangOptions.h"
  13. #include "clang/Basic/ObjCRuntime.h"
  14. #include "clang/Basic/Version.h"
  15. #include "clang/Driver/Action.h"
  16. #include "clang/Driver/Compilation.h"
  17. #include "clang/Driver/Driver.h"
  18. #include "clang/Driver/DriverDiagnostic.h"
  19. #include "clang/Driver/Job.h"
  20. #include "clang/Driver/Options.h"
  21. #include "clang/Driver/SanitizerArgs.h"
  22. #include "clang/Driver/ToolChain.h"
  23. #include "clang/Driver/Util.h"
  24. #include "llvm/ADT/SmallString.h"
  25. #include "llvm/ADT/StringExtras.h"
  26. #include "llvm/ADT/StringSwitch.h"
  27. #include "llvm/ADT/Twine.h"
  28. #include "llvm/Option/Arg.h"
  29. #include "llvm/Option/ArgList.h"
  30. #include "llvm/Option/Option.h"
  31. #include "llvm/Support/Compression.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Support/FileSystem.h"
  34. #include "llvm/Support/Format.h"
  35. #include "llvm/Support/Host.h"
  36. #include "llvm/Support/Path.h"
  37. #include "llvm/Support/Process.h"
  38. #include "llvm/Support/Program.h"
  39. #include "llvm/Support/raw_ostream.h"
  40. #include <sys/stat.h>
  41. using namespace clang::driver;
  42. using namespace clang::driver::tools;
  43. using namespace clang;
  44. using namespace llvm::opt;
  45. static void addAssemblerKPIC(const ArgList &Args, ArgStringList &CmdArgs) {
  46. Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
  47. options::OPT_fpic, options::OPT_fno_pic,
  48. options::OPT_fPIE, options::OPT_fno_PIE,
  49. options::OPT_fpie, options::OPT_fno_pie);
  50. if (!LastPICArg)
  51. return;
  52. if (LastPICArg->getOption().matches(options::OPT_fPIC) ||
  53. LastPICArg->getOption().matches(options::OPT_fpic) ||
  54. LastPICArg->getOption().matches(options::OPT_fPIE) ||
  55. LastPICArg->getOption().matches(options::OPT_fpie)) {
  56. CmdArgs.push_back("-KPIC");
  57. }
  58. }
  59. /// CheckPreprocessingOptions - Perform some validation of preprocessing
  60. /// arguments that is shared with gcc.
  61. static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
  62. if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
  63. if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP())
  64. D.Diag(diag::err_drv_argument_only_allowed_with)
  65. << A->getAsString(Args) << "-E";
  66. }
  67. /// CheckCodeGenerationOptions - Perform some validation of code generation
  68. /// arguments that is shared with gcc.
  69. static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
  70. // In gcc, only ARM checks this, but it seems reasonable to check universally.
  71. if (Args.hasArg(options::OPT_static))
  72. if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
  73. options::OPT_mdynamic_no_pic))
  74. D.Diag(diag::err_drv_argument_not_allowed_with)
  75. << A->getAsString(Args) << "-static";
  76. }
  77. // Quote target names for inclusion in GNU Make dependency files.
  78. // Only the characters '$', '#', ' ', '\t' are quoted.
  79. static void QuoteTarget(StringRef Target,
  80. SmallVectorImpl<char> &Res) {
  81. for (unsigned i = 0, e = Target.size(); i != e; ++i) {
  82. switch (Target[i]) {
  83. case ' ':
  84. case '\t':
  85. // Escape the preceding backslashes
  86. for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
  87. Res.push_back('\\');
  88. // Escape the space/tab
  89. Res.push_back('\\');
  90. break;
  91. case '$':
  92. Res.push_back('$');
  93. break;
  94. case '#':
  95. Res.push_back('\\');
  96. break;
  97. default:
  98. break;
  99. }
  100. Res.push_back(Target[i]);
  101. }
  102. }
  103. static void addDirectoryList(const ArgList &Args,
  104. ArgStringList &CmdArgs,
  105. const char *ArgName,
  106. const char *EnvVar) {
  107. const char *DirList = ::getenv(EnvVar);
  108. bool CombinedArg = false;
  109. if (!DirList)
  110. return; // Nothing to do.
  111. StringRef Name(ArgName);
  112. if (Name.equals("-I") || Name.equals("-L"))
  113. CombinedArg = true;
  114. StringRef Dirs(DirList);
  115. if (Dirs.empty()) // Empty string should not add '.'.
  116. return;
  117. StringRef::size_type Delim;
  118. while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) {
  119. if (Delim == 0) { // Leading colon.
  120. if (CombinedArg) {
  121. CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
  122. } else {
  123. CmdArgs.push_back(ArgName);
  124. CmdArgs.push_back(".");
  125. }
  126. } else {
  127. if (CombinedArg) {
  128. CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
  129. } else {
  130. CmdArgs.push_back(ArgName);
  131. CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
  132. }
  133. }
  134. Dirs = Dirs.substr(Delim + 1);
  135. }
  136. if (Dirs.empty()) { // Trailing colon.
  137. if (CombinedArg) {
  138. CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
  139. } else {
  140. CmdArgs.push_back(ArgName);
  141. CmdArgs.push_back(".");
  142. }
  143. } else { // Add the last path.
  144. if (CombinedArg) {
  145. CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
  146. } else {
  147. CmdArgs.push_back(ArgName);
  148. CmdArgs.push_back(Args.MakeArgString(Dirs));
  149. }
  150. }
  151. }
  152. static void AddLinkerInputs(const ToolChain &TC,
  153. const InputInfoList &Inputs, const ArgList &Args,
  154. ArgStringList &CmdArgs) {
  155. const Driver &D = TC.getDriver();
  156. // Add extra linker input arguments which are not treated as inputs
  157. // (constructed via -Xarch_).
  158. Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
  159. for (InputInfoList::const_iterator
  160. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  161. const InputInfo &II = *it;
  162. if (!TC.HasNativeLLVMSupport()) {
  163. // Don't try to pass LLVM inputs unless we have native support.
  164. if (II.getType() == types::TY_LLVM_IR ||
  165. II.getType() == types::TY_LTO_IR ||
  166. II.getType() == types::TY_LLVM_BC ||
  167. II.getType() == types::TY_LTO_BC)
  168. D.Diag(diag::err_drv_no_linker_llvm_support)
  169. << TC.getTripleString();
  170. }
  171. // Add filenames immediately.
  172. if (II.isFilename()) {
  173. CmdArgs.push_back(II.getFilename());
  174. continue;
  175. }
  176. // Otherwise, this is a linker input argument.
  177. const Arg &A = II.getInputArg();
  178. // Handle reserved library options.
  179. if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
  180. TC.AddCXXStdlibLibArgs(Args, CmdArgs);
  181. } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
  182. TC.AddCCKextLibArgs(Args, CmdArgs);
  183. } else
  184. A.renderAsInput(Args, CmdArgs);
  185. }
  186. // LIBRARY_PATH - included following the user specified library paths.
  187. // and only supported on native toolchains.
  188. if (!TC.isCrossCompiling())
  189. addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
  190. }
  191. /// \brief Determine whether Objective-C automated reference counting is
  192. /// enabled.
  193. static bool isObjCAutoRefCount(const ArgList &Args) {
  194. return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
  195. }
  196. /// \brief Determine whether we are linking the ObjC runtime.
  197. static bool isObjCRuntimeLinked(const ArgList &Args) {
  198. if (isObjCAutoRefCount(Args)) {
  199. Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
  200. return true;
  201. }
  202. return Args.hasArg(options::OPT_fobjc_link_runtime);
  203. }
  204. static bool forwardToGCC(const Option &O) {
  205. // Don't forward inputs from the original command line. They are added from
  206. // InputInfoList.
  207. return O.getKind() != Option::InputClass &&
  208. !O.hasFlag(options::DriverOption) &&
  209. !O.hasFlag(options::LinkerInput);
  210. }
  211. void Clang::AddPreprocessingOptions(Compilation &C,
  212. const JobAction &JA,
  213. const Driver &D,
  214. const ArgList &Args,
  215. ArgStringList &CmdArgs,
  216. const InputInfo &Output,
  217. const InputInfoList &Inputs) const {
  218. Arg *A;
  219. CheckPreprocessingOptions(D, Args);
  220. Args.AddLastArg(CmdArgs, options::OPT_C);
  221. Args.AddLastArg(CmdArgs, options::OPT_CC);
  222. // Handle dependency file generation.
  223. if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
  224. (A = Args.getLastArg(options::OPT_MD)) ||
  225. (A = Args.getLastArg(options::OPT_MMD))) {
  226. // Determine the output location.
  227. const char *DepFile;
  228. if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
  229. DepFile = MF->getValue();
  230. C.addFailureResultFile(DepFile, &JA);
  231. } else if (Output.getType() == types::TY_Dependencies) {
  232. DepFile = Output.getFilename();
  233. } else if (A->getOption().matches(options::OPT_M) ||
  234. A->getOption().matches(options::OPT_MM)) {
  235. DepFile = "-";
  236. } else {
  237. DepFile = getDependencyFileName(Args, Inputs);
  238. C.addFailureResultFile(DepFile, &JA);
  239. }
  240. CmdArgs.push_back("-dependency-file");
  241. CmdArgs.push_back(DepFile);
  242. // Add a default target if one wasn't specified.
  243. if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
  244. const char *DepTarget;
  245. // If user provided -o, that is the dependency target, except
  246. // when we are only generating a dependency file.
  247. Arg *OutputOpt = Args.getLastArg(options::OPT_o);
  248. if (OutputOpt && Output.getType() != types::TY_Dependencies) {
  249. DepTarget = OutputOpt->getValue();
  250. } else {
  251. // Otherwise derive from the base input.
  252. //
  253. // FIXME: This should use the computed output file location.
  254. SmallString<128> P(Inputs[0].getBaseInput());
  255. llvm::sys::path::replace_extension(P, "o");
  256. DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
  257. }
  258. CmdArgs.push_back("-MT");
  259. SmallString<128> Quoted;
  260. QuoteTarget(DepTarget, Quoted);
  261. CmdArgs.push_back(Args.MakeArgString(Quoted));
  262. }
  263. if (A->getOption().matches(options::OPT_M) ||
  264. A->getOption().matches(options::OPT_MD))
  265. CmdArgs.push_back("-sys-header-deps");
  266. if (isa<PrecompileJobAction>(JA))
  267. CmdArgs.push_back("-module-file-deps");
  268. }
  269. if (Args.hasArg(options::OPT_MG)) {
  270. if (!A || A->getOption().matches(options::OPT_MD) ||
  271. A->getOption().matches(options::OPT_MMD))
  272. D.Diag(diag::err_drv_mg_requires_m_or_mm);
  273. CmdArgs.push_back("-MG");
  274. }
  275. Args.AddLastArg(CmdArgs, options::OPT_MP);
  276. // Convert all -MQ <target> args to -MT <quoted target>
  277. for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
  278. options::OPT_MQ),
  279. ie = Args.filtered_end(); it != ie; ++it) {
  280. const Arg *A = *it;
  281. A->claim();
  282. if (A->getOption().matches(options::OPT_MQ)) {
  283. CmdArgs.push_back("-MT");
  284. SmallString<128> Quoted;
  285. QuoteTarget(A->getValue(), Quoted);
  286. CmdArgs.push_back(Args.MakeArgString(Quoted));
  287. // -MT flag - no change
  288. } else {
  289. A->render(Args, CmdArgs);
  290. }
  291. }
  292. // Add -i* options, and automatically translate to
  293. // -include-pch/-include-pth for transparent PCH support. It's
  294. // wonky, but we include looking for .gch so we can support seamless
  295. // replacement into a build system already set up to be generating
  296. // .gch files.
  297. bool RenderedImplicitInclude = false;
  298. for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
  299. ie = Args.filtered_end(); it != ie; ++it) {
  300. const Arg *A = it;
  301. if (A->getOption().matches(options::OPT_include)) {
  302. bool IsFirstImplicitInclude = !RenderedImplicitInclude;
  303. RenderedImplicitInclude = true;
  304. // Use PCH if the user requested it.
  305. bool UsePCH = D.CCCUsePCH;
  306. bool FoundPTH = false;
  307. bool FoundPCH = false;
  308. SmallString<128> P(A->getValue());
  309. // We want the files to have a name like foo.h.pch. Add a dummy extension
  310. // so that replace_extension does the right thing.
  311. P += ".dummy";
  312. if (UsePCH) {
  313. llvm::sys::path::replace_extension(P, "pch");
  314. if (llvm::sys::fs::exists(P.str()))
  315. FoundPCH = true;
  316. }
  317. if (!FoundPCH) {
  318. llvm::sys::path::replace_extension(P, "pth");
  319. if (llvm::sys::fs::exists(P.str()))
  320. FoundPTH = true;
  321. }
  322. if (!FoundPCH && !FoundPTH) {
  323. llvm::sys::path::replace_extension(P, "gch");
  324. if (llvm::sys::fs::exists(P.str())) {
  325. FoundPCH = UsePCH;
  326. FoundPTH = !UsePCH;
  327. }
  328. }
  329. if (FoundPCH || FoundPTH) {
  330. if (IsFirstImplicitInclude) {
  331. A->claim();
  332. if (UsePCH)
  333. CmdArgs.push_back("-include-pch");
  334. else
  335. CmdArgs.push_back("-include-pth");
  336. CmdArgs.push_back(Args.MakeArgString(P.str()));
  337. continue;
  338. } else {
  339. // Ignore the PCH if not first on command line and emit warning.
  340. D.Diag(diag::warn_drv_pch_not_first_include)
  341. << P.str() << A->getAsString(Args);
  342. }
  343. }
  344. }
  345. // Not translated, render as usual.
  346. A->claim();
  347. A->render(Args, CmdArgs);
  348. }
  349. Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
  350. Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
  351. options::OPT_index_header_map);
  352. // Add -Wp, and -Xassembler if using the preprocessor.
  353. // FIXME: There is a very unfortunate problem here, some troubled
  354. // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
  355. // really support that we would have to parse and then translate
  356. // those options. :(
  357. Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
  358. options::OPT_Xpreprocessor);
  359. // -I- is a deprecated GCC feature, reject it.
  360. if (Arg *A = Args.getLastArg(options::OPT_I_))
  361. D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
  362. // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
  363. // -isysroot to the CC1 invocation.
  364. StringRef sysroot = C.getSysRoot();
  365. if (sysroot != "") {
  366. if (!Args.hasArg(options::OPT_isysroot)) {
  367. CmdArgs.push_back("-isysroot");
  368. CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
  369. }
  370. }
  371. // Parse additional include paths from environment variables.
  372. // FIXME: We should probably sink the logic for handling these from the
  373. // frontend into the driver. It will allow deleting 4 otherwise unused flags.
  374. // CPATH - included following the user specified includes (but prior to
  375. // builtin and standard includes).
  376. addDirectoryList(Args, CmdArgs, "-I", "CPATH");
  377. // C_INCLUDE_PATH - system includes enabled when compiling C.
  378. addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
  379. // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
  380. addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
  381. // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
  382. addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
  383. // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
  384. addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
  385. // Add C++ include arguments, if needed.
  386. if (types::isCXX(Inputs[0].getType()))
  387. getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
  388. // Add system include arguments.
  389. getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
  390. }
  391. // FIXME: Move to target hook.
  392. static bool isSignedCharDefault(const llvm::Triple &Triple) {
  393. switch (Triple.getArch()) {
  394. default:
  395. return true;
  396. case llvm::Triple::aarch64:
  397. case llvm::Triple::aarch64_be:
  398. case llvm::Triple::arm64:
  399. case llvm::Triple::arm64_be:
  400. case llvm::Triple::arm:
  401. case llvm::Triple::armeb:
  402. if (Triple.isOSDarwin() || Triple.isOSWindows())
  403. return true;
  404. return false;
  405. case llvm::Triple::ppc:
  406. case llvm::Triple::ppc64:
  407. if (Triple.isOSDarwin())
  408. return true;
  409. return false;
  410. case llvm::Triple::ppc64le:
  411. case llvm::Triple::systemz:
  412. case llvm::Triple::xcore:
  413. return false;
  414. }
  415. }
  416. static bool isNoCommonDefault(const llvm::Triple &Triple) {
  417. switch (Triple.getArch()) {
  418. default:
  419. return false;
  420. case llvm::Triple::xcore:
  421. return true;
  422. }
  423. }
  424. // Handle -mfpu=.
  425. //
  426. // FIXME: Centralize feature selection, defaulting shouldn't be also in the
  427. // frontend target.
  428. static void getAArch64FPUFeatures(const Driver &D, const Arg *A,
  429. const ArgList &Args,
  430. std::vector<const char *> &Features) {
  431. StringRef FPU = A->getValue();
  432. if (FPU == "fp-armv8") {
  433. Features.push_back("+fp-armv8");
  434. } else if (FPU == "neon-fp-armv8") {
  435. Features.push_back("+fp-armv8");
  436. Features.push_back("+neon");
  437. } else if (FPU == "crypto-neon-fp-armv8") {
  438. Features.push_back("+fp-armv8");
  439. Features.push_back("+neon");
  440. Features.push_back("+crypto");
  441. } else if (FPU == "neon") {
  442. Features.push_back("+neon");
  443. } else
  444. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  445. }
  446. // Handle -mhwdiv=.
  447. static void getARMHWDivFeatures(const Driver &D, const Arg *A,
  448. const ArgList &Args,
  449. std::vector<const char *> &Features) {
  450. StringRef HWDiv = A->getValue();
  451. if (HWDiv == "arm") {
  452. Features.push_back("+hwdiv-arm");
  453. Features.push_back("-hwdiv");
  454. } else if (HWDiv == "thumb") {
  455. Features.push_back("-hwdiv-arm");
  456. Features.push_back("+hwdiv");
  457. } else if (HWDiv == "arm,thumb" || HWDiv == "thumb,arm") {
  458. Features.push_back("+hwdiv-arm");
  459. Features.push_back("+hwdiv");
  460. } else if (HWDiv == "none") {
  461. Features.push_back("-hwdiv-arm");
  462. Features.push_back("-hwdiv");
  463. } else
  464. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  465. }
  466. // Handle -mfpu=.
  467. //
  468. // FIXME: Centralize feature selection, defaulting shouldn't be also in the
  469. // frontend target.
  470. static void getARMFPUFeatures(const Driver &D, const Arg *A,
  471. const ArgList &Args,
  472. std::vector<const char *> &Features) {
  473. StringRef FPU = A->getValue();
  474. // Set the target features based on the FPU.
  475. if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
  476. // Disable any default FPU support.
  477. Features.push_back("-vfp2");
  478. Features.push_back("-vfp3");
  479. Features.push_back("-neon");
  480. } else if (FPU == "vfp") {
  481. Features.push_back("+vfp2");
  482. Features.push_back("-neon");
  483. } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
  484. Features.push_back("+vfp3");
  485. Features.push_back("+d16");
  486. Features.push_back("-neon");
  487. } else if (FPU == "vfp3" || FPU == "vfpv3") {
  488. Features.push_back("+vfp3");
  489. Features.push_back("-neon");
  490. } else if (FPU == "vfp4-d16" || FPU == "vfpv4-d16") {
  491. Features.push_back("+vfp4");
  492. Features.push_back("+d16");
  493. Features.push_back("-neon");
  494. } else if (FPU == "vfp4" || FPU == "vfpv4") {
  495. Features.push_back("+vfp4");
  496. Features.push_back("-neon");
  497. } else if (FPU == "fp4-sp-d16" || FPU == "fpv4-sp-d16") {
  498. Features.push_back("+vfp4");
  499. Features.push_back("+d16");
  500. Features.push_back("+fp-only-sp");
  501. Features.push_back("-neon");
  502. } else if (FPU == "fp-armv8") {
  503. Features.push_back("+fp-armv8");
  504. Features.push_back("-neon");
  505. Features.push_back("-crypto");
  506. } else if (FPU == "neon-fp-armv8") {
  507. Features.push_back("+fp-armv8");
  508. Features.push_back("+neon");
  509. Features.push_back("-crypto");
  510. } else if (FPU == "crypto-neon-fp-armv8") {
  511. Features.push_back("+fp-armv8");
  512. Features.push_back("+neon");
  513. Features.push_back("+crypto");
  514. } else if (FPU == "neon") {
  515. Features.push_back("+neon");
  516. } else if (FPU == "none") {
  517. Features.push_back("-vfp2");
  518. Features.push_back("-vfp3");
  519. Features.push_back("-vfp4");
  520. Features.push_back("-fp-armv8");
  521. Features.push_back("-crypto");
  522. Features.push_back("-neon");
  523. } else
  524. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  525. }
  526. // Select the float ABI as determined by -msoft-float, -mhard-float, and
  527. // -mfloat-abi=.
  528. StringRef tools::arm::getARMFloatABI(const Driver &D, const ArgList &Args,
  529. const llvm::Triple &Triple) {
  530. StringRef FloatABI;
  531. if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
  532. options::OPT_mhard_float,
  533. options::OPT_mfloat_abi_EQ)) {
  534. if (A->getOption().matches(options::OPT_msoft_float))
  535. FloatABI = "soft";
  536. else if (A->getOption().matches(options::OPT_mhard_float))
  537. FloatABI = "hard";
  538. else {
  539. FloatABI = A->getValue();
  540. if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
  541. D.Diag(diag::err_drv_invalid_mfloat_abi)
  542. << A->getAsString(Args);
  543. FloatABI = "soft";
  544. }
  545. }
  546. }
  547. // If unspecified, choose the default based on the platform.
  548. if (FloatABI.empty()) {
  549. switch (Triple.getOS()) {
  550. case llvm::Triple::Darwin:
  551. case llvm::Triple::MacOSX:
  552. case llvm::Triple::IOS: {
  553. // Darwin defaults to "softfp" for v6 and v7.
  554. //
  555. // FIXME: Factor out an ARM class so we can cache the arch somewhere.
  556. std::string ArchName =
  557. arm::getLLVMArchSuffixForARM(arm::getARMTargetCPU(Args, Triple));
  558. if (StringRef(ArchName).startswith("v6") ||
  559. StringRef(ArchName).startswith("v7"))
  560. FloatABI = "softfp";
  561. else
  562. FloatABI = "soft";
  563. break;
  564. }
  565. // FIXME: this is invalid for WindowsCE
  566. case llvm::Triple::Win32:
  567. FloatABI = "hard";
  568. break;
  569. case llvm::Triple::FreeBSD:
  570. switch(Triple.getEnvironment()) {
  571. case llvm::Triple::GNUEABIHF:
  572. FloatABI = "hard";
  573. break;
  574. default:
  575. // FreeBSD defaults to soft float
  576. FloatABI = "soft";
  577. break;
  578. }
  579. break;
  580. default:
  581. switch(Triple.getEnvironment()) {
  582. case llvm::Triple::GNUEABIHF:
  583. FloatABI = "hard";
  584. break;
  585. case llvm::Triple::GNUEABI:
  586. FloatABI = "softfp";
  587. break;
  588. case llvm::Triple::EABIHF:
  589. FloatABI = "hard";
  590. break;
  591. case llvm::Triple::EABI:
  592. // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
  593. FloatABI = "softfp";
  594. break;
  595. case llvm::Triple::Android: {
  596. std::string ArchName =
  597. arm::getLLVMArchSuffixForARM(arm::getARMTargetCPU(Args, Triple));
  598. if (StringRef(ArchName).startswith("v7"))
  599. FloatABI = "softfp";
  600. else
  601. FloatABI = "soft";
  602. break;
  603. }
  604. default:
  605. // Assume "soft", but warn the user we are guessing.
  606. FloatABI = "soft";
  607. if (Triple.getOS() != llvm::Triple::UnknownOS ||
  608. !Triple.isOSBinFormatMachO())
  609. D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
  610. break;
  611. }
  612. }
  613. }
  614. return FloatABI;
  615. }
  616. static void getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  617. const ArgList &Args,
  618. std::vector<const char *> &Features,
  619. bool ForAS) {
  620. StringRef FloatABI = tools::arm::getARMFloatABI(D, Args, Triple);
  621. if (!ForAS) {
  622. // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
  623. // yet (it uses the -mfloat-abi and -msoft-float options), and it is
  624. // stripped out by the ARM target. We should probably pass this a new
  625. // -target-option, which is handled by the -cc1/-cc1as invocation.
  626. //
  627. // FIXME2: For consistency, it would be ideal if we set up the target
  628. // machine state the same when using the frontend or the assembler. We don't
  629. // currently do that for the assembler, we pass the options directly to the
  630. // backend and never even instantiate the frontend TargetInfo. If we did,
  631. // and used its handleTargetFeatures hook, then we could ensure the
  632. // assembler and the frontend behave the same.
  633. // Use software floating point operations?
  634. if (FloatABI == "soft")
  635. Features.push_back("+soft-float");
  636. // Use software floating point argument passing?
  637. if (FloatABI != "hard")
  638. Features.push_back("+soft-float-abi");
  639. }
  640. // Honor -mfpu=.
  641. if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
  642. getARMFPUFeatures(D, A, Args, Features);
  643. if (const Arg *A = Args.getLastArg(options::OPT_mhwdiv_EQ))
  644. getARMHWDivFeatures(D, A, Args, Features);
  645. // Setting -msoft-float effectively disables NEON because of the GCC
  646. // implementation, although the same isn't true of VFP or VFP3.
  647. if (FloatABI == "soft") {
  648. Features.push_back("-neon");
  649. // Also need to explicitly disable features which imply NEON.
  650. Features.push_back("-crypto");
  651. }
  652. // En/disable crc
  653. if (Arg *A = Args.getLastArg(options::OPT_mcrc,
  654. options::OPT_mnocrc)) {
  655. if (A->getOption().matches(options::OPT_mcrc))
  656. Features.push_back("+crc");
  657. else
  658. Features.push_back("-crc");
  659. }
  660. }
  661. void Clang::AddARMTargetArgs(const ArgList &Args,
  662. ArgStringList &CmdArgs,
  663. bool KernelOrKext) const {
  664. const Driver &D = getToolChain().getDriver();
  665. // Get the effective triple, which takes into account the deployment target.
  666. std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  667. llvm::Triple Triple(TripleStr);
  668. std::string CPUName = arm::getARMTargetCPU(Args, Triple);
  669. // Select the ABI to use.
  670. //
  671. // FIXME: Support -meabi.
  672. const char *ABIName = nullptr;
  673. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
  674. ABIName = A->getValue();
  675. } else if (Triple.isOSBinFormatMachO()) {
  676. // The backend is hardwired to assume AAPCS for M-class processors, ensure
  677. // the frontend matches that.
  678. if (Triple.getEnvironment() == llvm::Triple::EABI ||
  679. (Triple.getOS() == llvm::Triple::UnknownOS &&
  680. Triple.getObjectFormat() == llvm::Triple::MachO) ||
  681. StringRef(CPUName).startswith("cortex-m")) {
  682. ABIName = "aapcs";
  683. } else {
  684. ABIName = "apcs-gnu";
  685. }
  686. } else if (Triple.isOSWindows()) {
  687. // FIXME: this is invalid for WindowsCE
  688. ABIName = "aapcs";
  689. } else {
  690. // Select the default based on the platform.
  691. switch(Triple.getEnvironment()) {
  692. case llvm::Triple::Android:
  693. case llvm::Triple::GNUEABI:
  694. case llvm::Triple::GNUEABIHF:
  695. ABIName = "aapcs-linux";
  696. break;
  697. case llvm::Triple::EABIHF:
  698. case llvm::Triple::EABI:
  699. ABIName = "aapcs";
  700. break;
  701. default:
  702. ABIName = "apcs-gnu";
  703. }
  704. }
  705. CmdArgs.push_back("-target-abi");
  706. CmdArgs.push_back(ABIName);
  707. // Determine floating point ABI from the options & target defaults.
  708. StringRef FloatABI = tools::arm::getARMFloatABI(D, Args, Triple);
  709. if (FloatABI == "soft") {
  710. // Floating point operations and argument passing are soft.
  711. //
  712. // FIXME: This changes CPP defines, we need -target-soft-float.
  713. CmdArgs.push_back("-msoft-float");
  714. CmdArgs.push_back("-mfloat-abi");
  715. CmdArgs.push_back("soft");
  716. } else if (FloatABI == "softfp") {
  717. // Floating point operations are hard, but argument passing is soft.
  718. CmdArgs.push_back("-mfloat-abi");
  719. CmdArgs.push_back("soft");
  720. } else {
  721. // Floating point operations and argument passing are hard.
  722. assert(FloatABI == "hard" && "Invalid float abi!");
  723. CmdArgs.push_back("-mfloat-abi");
  724. CmdArgs.push_back("hard");
  725. }
  726. // Kernel code has more strict alignment requirements.
  727. if (KernelOrKext) {
  728. if (!Triple.isiOS() || Triple.isOSVersionLT(6)) {
  729. CmdArgs.push_back("-backend-option");
  730. CmdArgs.push_back("-arm-long-calls");
  731. }
  732. CmdArgs.push_back("-backend-option");
  733. CmdArgs.push_back("-arm-strict-align");
  734. // The kext linker doesn't know how to deal with movw/movt.
  735. CmdArgs.push_back("-backend-option");
  736. CmdArgs.push_back("-arm-use-movt=0");
  737. }
  738. // Setting -mno-global-merge disables the codegen global merge pass. Setting
  739. // -mglobal-merge has no effect as the pass is enabled by default.
  740. if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
  741. options::OPT_mno_global_merge)) {
  742. if (A->getOption().matches(options::OPT_mno_global_merge))
  743. CmdArgs.push_back("-mno-global-merge");
  744. }
  745. if (!Args.hasFlag(options::OPT_mimplicit_float,
  746. options::OPT_mno_implicit_float,
  747. true))
  748. CmdArgs.push_back("-no-implicit-float");
  749. // llvm does not support reserving registers in general. There is support
  750. // for reserving r9 on ARM though (defined as a platform-specific register
  751. // in ARM EABI).
  752. if (Args.hasArg(options::OPT_ffixed_r9)) {
  753. CmdArgs.push_back("-backend-option");
  754. CmdArgs.push_back("-arm-reserve-r9");
  755. }
  756. }
  757. /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
  758. /// targeting.
  759. static std::string getAArch64TargetCPU(const ArgList &Args) {
  760. // If we have -mcpu=, use that.
  761. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
  762. StringRef MCPU = A->getValue();
  763. // Handle -mcpu=native.
  764. if (MCPU == "native")
  765. return llvm::sys::getHostCPUName();
  766. else
  767. return MCPU;
  768. }
  769. // At some point, we may need to check -march here, but for now we only
  770. // one arm64 architecture.
  771. // Make sure we pick "cyclone" if -arch is used.
  772. // FIXME: Should this be picked by checking the target triple instead?
  773. if (Args.getLastArg(options::OPT_arch))
  774. return "cyclone";
  775. return "generic";
  776. }
  777. void Clang::AddAArch64TargetArgs(const ArgList &Args,
  778. ArgStringList &CmdArgs) const {
  779. std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  780. llvm::Triple Triple(TripleStr);
  781. if (!Args.hasFlag(options::OPT_mred_zone, options::OPT_mno_red_zone, true) ||
  782. Args.hasArg(options::OPT_mkernel) ||
  783. Args.hasArg(options::OPT_fapple_kext))
  784. CmdArgs.push_back("-disable-red-zone");
  785. if (!Args.hasFlag(options::OPT_mimplicit_float,
  786. options::OPT_mno_implicit_float, true))
  787. CmdArgs.push_back("-no-implicit-float");
  788. const char *ABIName = nullptr;
  789. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
  790. ABIName = A->getValue();
  791. else if (Triple.isOSDarwin())
  792. ABIName = "darwinpcs";
  793. else
  794. ABIName = "aapcs";
  795. CmdArgs.push_back("-target-abi");
  796. CmdArgs.push_back(ABIName);
  797. CmdArgs.push_back("-target-cpu");
  798. CmdArgs.push_back(Args.MakeArgString(getAArch64TargetCPU(Args)));
  799. if (Args.hasArg(options::OPT_mstrict_align)) {
  800. CmdArgs.push_back("-backend-option");
  801. CmdArgs.push_back("-aarch64-strict-align");
  802. }
  803. // Setting -mno-global-merge disables the codegen global merge pass. Setting
  804. // -mglobal-merge has no effect as the pass is enabled by default.
  805. if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
  806. options::OPT_mno_global_merge)) {
  807. if (A->getOption().matches(options::OPT_mno_global_merge))
  808. CmdArgs.push_back("-mno-global-merge");
  809. }
  810. }
  811. // Get CPU and ABI names. They are not independent
  812. // so we have to calculate them together.
  813. static void getMipsCPUAndABI(const ArgList &Args,
  814. const llvm::Triple &Triple,
  815. StringRef &CPUName,
  816. StringRef &ABIName) {
  817. const char *DefMips32CPU = "mips32r2";
  818. const char *DefMips64CPU = "mips64r2";
  819. if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
  820. options::OPT_mcpu_EQ))
  821. CPUName = A->getValue();
  822. if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
  823. ABIName = A->getValue();
  824. // Convert a GNU style Mips ABI name to the name
  825. // accepted by LLVM Mips backend.
  826. ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName)
  827. .Case("32", "o32")
  828. .Case("64", "n64")
  829. .Default(ABIName);
  830. }
  831. // Setup default CPU and ABI names.
  832. if (CPUName.empty() && ABIName.empty()) {
  833. switch (Triple.getArch()) {
  834. default:
  835. llvm_unreachable("Unexpected triple arch name");
  836. case llvm::Triple::mips:
  837. case llvm::Triple::mipsel:
  838. CPUName = DefMips32CPU;
  839. break;
  840. case llvm::Triple::mips64:
  841. case llvm::Triple::mips64el:
  842. CPUName = DefMips64CPU;
  843. break;
  844. }
  845. }
  846. if (!ABIName.empty()) {
  847. // Deduce CPU name from ABI name.
  848. CPUName = llvm::StringSwitch<const char *>(ABIName)
  849. .Cases("32", "o32", "eabi", DefMips32CPU)
  850. .Cases("n32", "n64", "64", DefMips64CPU)
  851. .Default("");
  852. }
  853. else if (!CPUName.empty()) {
  854. // Deduce ABI name from CPU name.
  855. ABIName = llvm::StringSwitch<const char *>(CPUName)
  856. .Cases("mips32", "mips32r2", "o32")
  857. .Cases("mips64", "mips64r2", "n64")
  858. .Default("");
  859. }
  860. // FIXME: Warn on inconsistent cpu and abi usage.
  861. }
  862. // Convert ABI name to the GNU tools acceptable variant.
  863. static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
  864. return llvm::StringSwitch<llvm::StringRef>(ABI)
  865. .Case("o32", "32")
  866. .Case("n64", "64")
  867. .Default(ABI);
  868. }
  869. // Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
  870. // and -mfloat-abi=.
  871. static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
  872. StringRef FloatABI;
  873. if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
  874. options::OPT_mhard_float,
  875. options::OPT_mfloat_abi_EQ)) {
  876. if (A->getOption().matches(options::OPT_msoft_float))
  877. FloatABI = "soft";
  878. else if (A->getOption().matches(options::OPT_mhard_float))
  879. FloatABI = "hard";
  880. else {
  881. FloatABI = A->getValue();
  882. if (FloatABI != "soft" && FloatABI != "hard") {
  883. D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
  884. FloatABI = "hard";
  885. }
  886. }
  887. }
  888. // If unspecified, choose the default based on the platform.
  889. if (FloatABI.empty()) {
  890. // Assume "hard", because it's a default value used by gcc.
  891. // When we start to recognize specific target MIPS processors,
  892. // we will be able to select the default more correctly.
  893. FloatABI = "hard";
  894. }
  895. return FloatABI;
  896. }
  897. static void AddTargetFeature(const ArgList &Args,
  898. std::vector<const char *> &Features,
  899. OptSpecifier OnOpt, OptSpecifier OffOpt,
  900. StringRef FeatureName) {
  901. if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
  902. if (A->getOption().matches(OnOpt))
  903. Features.push_back(Args.MakeArgString("+" + FeatureName));
  904. else
  905. Features.push_back(Args.MakeArgString("-" + FeatureName));
  906. }
  907. }
  908. static void getMIPSTargetFeatures(const Driver &D, const ArgList &Args,
  909. std::vector<const char *> &Features) {
  910. StringRef FloatABI = getMipsFloatABI(D, Args);
  911. if (FloatABI == "soft") {
  912. // FIXME: Note, this is a hack. We need to pass the selected float
  913. // mode to the MipsTargetInfoBase to define appropriate macros there.
  914. // Now it is the only method.
  915. Features.push_back("+soft-float");
  916. }
  917. if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
  918. StringRef Val = StringRef(A->getValue());
  919. if (Val == "2008")
  920. Features.push_back("+nan2008");
  921. else if (Val == "legacy")
  922. Features.push_back("-nan2008");
  923. else
  924. D.Diag(diag::err_drv_unsupported_option_argument)
  925. << A->getOption().getName() << Val;
  926. }
  927. AddTargetFeature(Args, Features, options::OPT_msingle_float,
  928. options::OPT_mdouble_float, "single-float");
  929. AddTargetFeature(Args, Features, options::OPT_mips16, options::OPT_mno_mips16,
  930. "mips16");
  931. AddTargetFeature(Args, Features, options::OPT_mmicromips,
  932. options::OPT_mno_micromips, "micromips");
  933. AddTargetFeature(Args, Features, options::OPT_mdsp, options::OPT_mno_dsp,
  934. "dsp");
  935. AddTargetFeature(Args, Features, options::OPT_mdspr2, options::OPT_mno_dspr2,
  936. "dspr2");
  937. AddTargetFeature(Args, Features, options::OPT_mmsa, options::OPT_mno_msa,
  938. "msa");
  939. AddTargetFeature(Args, Features, options::OPT_mfp64, options::OPT_mfp32,
  940. "fp64");
  941. }
  942. void Clang::AddMIPSTargetArgs(const ArgList &Args,
  943. ArgStringList &CmdArgs) const {
  944. const Driver &D = getToolChain().getDriver();
  945. StringRef CPUName;
  946. StringRef ABIName;
  947. const llvm::Triple &Triple = getToolChain().getTriple();
  948. getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
  949. CmdArgs.push_back("-target-abi");
  950. CmdArgs.push_back(ABIName.data());
  951. StringRef FloatABI = getMipsFloatABI(D, Args);
  952. if (FloatABI == "soft") {
  953. // Floating point operations and argument passing are soft.
  954. CmdArgs.push_back("-msoft-float");
  955. CmdArgs.push_back("-mfloat-abi");
  956. CmdArgs.push_back("soft");
  957. }
  958. else {
  959. // Floating point operations and argument passing are hard.
  960. assert(FloatABI == "hard" && "Invalid float abi!");
  961. CmdArgs.push_back("-mfloat-abi");
  962. CmdArgs.push_back("hard");
  963. }
  964. if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
  965. if (A->getOption().matches(options::OPT_mxgot)) {
  966. CmdArgs.push_back("-mllvm");
  967. CmdArgs.push_back("-mxgot");
  968. }
  969. }
  970. if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
  971. options::OPT_mno_ldc1_sdc1)) {
  972. if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
  973. CmdArgs.push_back("-mllvm");
  974. CmdArgs.push_back("-mno-ldc1-sdc1");
  975. }
  976. }
  977. if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
  978. options::OPT_mno_check_zero_division)) {
  979. if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
  980. CmdArgs.push_back("-mllvm");
  981. CmdArgs.push_back("-mno-check-zero-division");
  982. }
  983. }
  984. if (Arg *A = Args.getLastArg(options::OPT_G)) {
  985. StringRef v = A->getValue();
  986. CmdArgs.push_back("-mllvm");
  987. CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
  988. A->claim();
  989. }
  990. }
  991. /// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
  992. static std::string getPPCTargetCPU(const ArgList &Args) {
  993. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
  994. StringRef CPUName = A->getValue();
  995. if (CPUName == "native") {
  996. std::string CPU = llvm::sys::getHostCPUName();
  997. if (!CPU.empty() && CPU != "generic")
  998. return CPU;
  999. else
  1000. return "";
  1001. }
  1002. return llvm::StringSwitch<const char *>(CPUName)
  1003. .Case("common", "generic")
  1004. .Case("440", "440")
  1005. .Case("440fp", "440")
  1006. .Case("450", "450")
  1007. .Case("601", "601")
  1008. .Case("602", "602")
  1009. .Case("603", "603")
  1010. .Case("603e", "603e")
  1011. .Case("603ev", "603ev")
  1012. .Case("604", "604")
  1013. .Case("604e", "604e")
  1014. .Case("620", "620")
  1015. .Case("630", "pwr3")
  1016. .Case("G3", "g3")
  1017. .Case("7400", "7400")
  1018. .Case("G4", "g4")
  1019. .Case("7450", "7450")
  1020. .Case("G4+", "g4+")
  1021. .Case("750", "750")
  1022. .Case("970", "970")
  1023. .Case("G5", "g5")
  1024. .Case("a2", "a2")
  1025. .Case("a2q", "a2q")
  1026. .Case("e500mc", "e500mc")
  1027. .Case("e5500", "e5500")
  1028. .Case("power3", "pwr3")
  1029. .Case("power4", "pwr4")
  1030. .Case("power5", "pwr5")
  1031. .Case("power5x", "pwr5x")
  1032. .Case("power6", "pwr6")
  1033. .Case("power6x", "pwr6x")
  1034. .Case("power7", "pwr7")
  1035. .Case("pwr3", "pwr3")
  1036. .Case("pwr4", "pwr4")
  1037. .Case("pwr5", "pwr5")
  1038. .Case("pwr5x", "pwr5x")
  1039. .Case("pwr6", "pwr6")
  1040. .Case("pwr6x", "pwr6x")
  1041. .Case("pwr7", "pwr7")
  1042. .Case("powerpc", "ppc")
  1043. .Case("powerpc64", "ppc64")
  1044. .Case("powerpc64le", "ppc64le")
  1045. .Default("");
  1046. }
  1047. return "";
  1048. }
  1049. static void getPPCTargetFeatures(const ArgList &Args,
  1050. std::vector<const char *> &Features) {
  1051. for (arg_iterator it = Args.filtered_begin(options::OPT_m_ppc_Features_Group),
  1052. ie = Args.filtered_end();
  1053. it != ie; ++it) {
  1054. StringRef Name = (*it)->getOption().getName();
  1055. (*it)->claim();
  1056. // Skip over "-m".
  1057. assert(Name.startswith("m") && "Invalid feature name.");
  1058. Name = Name.substr(1);
  1059. bool IsNegative = Name.startswith("no-");
  1060. if (IsNegative)
  1061. Name = Name.substr(3);
  1062. // Note that gcc calls this mfcrf and LLVM calls this mfocrf so we
  1063. // pass the correct option to the backend while calling the frontend
  1064. // option the same.
  1065. // TODO: Change the LLVM backend option maybe?
  1066. if (Name == "mfcrf")
  1067. Name = "mfocrf";
  1068. Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
  1069. }
  1070. // Altivec is a bit weird, allow overriding of the Altivec feature here.
  1071. AddTargetFeature(Args, Features, options::OPT_faltivec,
  1072. options::OPT_fno_altivec, "altivec");
  1073. }
  1074. /// Get the (LLVM) name of the R600 gpu we are targeting.
  1075. static std::string getR600TargetGPU(const ArgList &Args) {
  1076. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
  1077. const char *GPUName = A->getValue();
  1078. return llvm::StringSwitch<const char *>(GPUName)
  1079. .Cases("rv630", "rv635", "r600")
  1080. .Cases("rv610", "rv620", "rs780", "rs880")
  1081. .Case("rv740", "rv770")
  1082. .Case("palm", "cedar")
  1083. .Cases("sumo", "sumo2", "sumo")
  1084. .Case("hemlock", "cypress")
  1085. .Case("aruba", "cayman")
  1086. .Default(GPUName);
  1087. }
  1088. return "";
  1089. }
  1090. static void getSparcTargetFeatures(const ArgList &Args,
  1091. std::vector<const char *> Features) {
  1092. bool SoftFloatABI = true;
  1093. if (Arg *A =
  1094. Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) {
  1095. if (A->getOption().matches(options::OPT_mhard_float))
  1096. SoftFloatABI = false;
  1097. }
  1098. if (SoftFloatABI)
  1099. Features.push_back("+soft-float");
  1100. }
  1101. void Clang::AddSparcTargetArgs(const ArgList &Args,
  1102. ArgStringList &CmdArgs) const {
  1103. const Driver &D = getToolChain().getDriver();
  1104. // Select the float ABI as determined by -msoft-float, -mhard-float, and
  1105. StringRef FloatABI;
  1106. if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
  1107. options::OPT_mhard_float)) {
  1108. if (A->getOption().matches(options::OPT_msoft_float))
  1109. FloatABI = "soft";
  1110. else if (A->getOption().matches(options::OPT_mhard_float))
  1111. FloatABI = "hard";
  1112. }
  1113. // If unspecified, choose the default based on the platform.
  1114. if (FloatABI.empty()) {
  1115. // Assume "soft", but warn the user we are guessing.
  1116. FloatABI = "soft";
  1117. D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
  1118. }
  1119. if (FloatABI == "soft") {
  1120. // Floating point operations and argument passing are soft.
  1121. //
  1122. // FIXME: This changes CPP defines, we need -target-soft-float.
  1123. CmdArgs.push_back("-msoft-float");
  1124. } else {
  1125. assert(FloatABI == "hard" && "Invalid float abi!");
  1126. CmdArgs.push_back("-mhard-float");
  1127. }
  1128. }
  1129. static const char *getSystemZTargetCPU(const ArgList &Args) {
  1130. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
  1131. return A->getValue();
  1132. return "z10";
  1133. }
  1134. static const char *getX86TargetCPU(const ArgList &Args,
  1135. const llvm::Triple &Triple) {
  1136. if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  1137. if (StringRef(A->getValue()) != "native") {
  1138. if (Triple.isOSDarwin() && Triple.getArchName() == "x86_64h")
  1139. return "core-avx2";
  1140. return A->getValue();
  1141. }
  1142. // FIXME: Reject attempts to use -march=native unless the target matches
  1143. // the host.
  1144. //
  1145. // FIXME: We should also incorporate the detected target features for use
  1146. // with -native.
  1147. std::string CPU = llvm::sys::getHostCPUName();
  1148. if (!CPU.empty() && CPU != "generic")
  1149. return Args.MakeArgString(CPU);
  1150. }
  1151. // Select the default CPU if none was given (or detection failed).
  1152. if (Triple.getArch() != llvm::Triple::x86_64 &&
  1153. Triple.getArch() != llvm::Triple::x86)
  1154. return nullptr; // This routine is only handling x86 targets.
  1155. bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
  1156. // FIXME: Need target hooks.
  1157. if (Triple.isOSDarwin()) {
  1158. if (Triple.getArchName() == "x86_64h")
  1159. return "core-avx2";
  1160. return Is64Bit ? "core2" : "yonah";
  1161. }
  1162. // On Android use targets compatible with gcc
  1163. if (Triple.getEnvironment() == llvm::Triple::Android)
  1164. return Is64Bit ? "x86-64" : "i686";
  1165. // Everything else goes to x86-64 in 64-bit mode.
  1166. if (Is64Bit)
  1167. return "x86-64";
  1168. switch (Triple.getOS()) {
  1169. case llvm::Triple::FreeBSD:
  1170. case llvm::Triple::NetBSD:
  1171. case llvm::Triple::OpenBSD:
  1172. return "i486";
  1173. case llvm::Triple::Haiku:
  1174. return "i586";
  1175. case llvm::Triple::Bitrig:
  1176. return "i686";
  1177. default:
  1178. // Fallback to p4.
  1179. return "pentium4";
  1180. }
  1181. }
  1182. static std::string getCPUName(const ArgList &Args, const llvm::Triple &T) {
  1183. switch(T.getArch()) {
  1184. default:
  1185. return "";
  1186. case llvm::Triple::aarch64:
  1187. case llvm::Triple::aarch64_be:
  1188. case llvm::Triple::arm64:
  1189. case llvm::Triple::arm64_be:
  1190. return getAArch64TargetCPU(Args);
  1191. case llvm::Triple::arm:
  1192. case llvm::Triple::armeb:
  1193. case llvm::Triple::thumb:
  1194. case llvm::Triple::thumbeb:
  1195. return arm::getARMTargetCPU(Args, T);
  1196. case llvm::Triple::mips:
  1197. case llvm::Triple::mipsel:
  1198. case llvm::Triple::mips64:
  1199. case llvm::Triple::mips64el: {
  1200. StringRef CPUName;
  1201. StringRef ABIName;
  1202. getMipsCPUAndABI(Args, T, CPUName, ABIName);
  1203. return CPUName;
  1204. }
  1205. case llvm::Triple::ppc:
  1206. case llvm::Triple::ppc64:
  1207. case llvm::Triple::ppc64le: {
  1208. std::string TargetCPUName = getPPCTargetCPU(Args);
  1209. // LLVM may default to generating code for the native CPU,
  1210. // but, like gcc, we default to a more generic option for
  1211. // each architecture. (except on Darwin)
  1212. if (TargetCPUName.empty() && !T.isOSDarwin()) {
  1213. if (T.getArch() == llvm::Triple::ppc64)
  1214. TargetCPUName = "ppc64";
  1215. else if (T.getArch() == llvm::Triple::ppc64le)
  1216. TargetCPUName = "ppc64le";
  1217. else
  1218. TargetCPUName = "ppc";
  1219. }
  1220. return TargetCPUName;
  1221. }
  1222. case llvm::Triple::sparc:
  1223. case llvm::Triple::sparcv9:
  1224. if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
  1225. return A->getValue();
  1226. return "";
  1227. case llvm::Triple::x86:
  1228. case llvm::Triple::x86_64:
  1229. return getX86TargetCPU(Args, T);
  1230. case llvm::Triple::hexagon:
  1231. return "hexagon" + toolchains::Hexagon_TC::GetTargetCPU(Args).str();
  1232. case llvm::Triple::systemz:
  1233. return getSystemZTargetCPU(Args);
  1234. case llvm::Triple::r600:
  1235. return getR600TargetGPU(Args);
  1236. }
  1237. }
  1238. static void AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args,
  1239. ArgStringList &CmdArgs) {
  1240. // Tell the linker to load the plugin. This has to come before AddLinkerInputs
  1241. // as gold requires -plugin to come before any -plugin-opt that -Wl might
  1242. // forward.
  1243. CmdArgs.push_back("-plugin");
  1244. std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
  1245. CmdArgs.push_back(Args.MakeArgString(Plugin));
  1246. // Try to pass driver level flags relevant to LTO code generation down to
  1247. // the plugin.
  1248. // Handle flags for selecting CPU variants.
  1249. std::string CPU = getCPUName(Args, ToolChain.getTriple());
  1250. if (!CPU.empty())
  1251. CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU));
  1252. }
  1253. static void getX86TargetFeatures(const llvm::Triple &Triple,
  1254. const ArgList &Args,
  1255. std::vector<const char *> &Features) {
  1256. if (Triple.getArchName() == "x86_64h") {
  1257. // x86_64h implies quite a few of the more modern subtarget features
  1258. // for Haswell class CPUs, but not all of them. Opt-out of a few.
  1259. Features.push_back("-rdrnd");
  1260. Features.push_back("-aes");
  1261. Features.push_back("-pclmul");
  1262. Features.push_back("-rtm");
  1263. Features.push_back("-hle");
  1264. Features.push_back("-fsgsbase");
  1265. }
  1266. if (Triple.getEnvironment() == llvm::Triple::Android) {
  1267. // Add sse3 feature to comply with gcc on Android
  1268. Features.push_back("+sse3");
  1269. }
  1270. // Now add any that the user explicitly requested on the command line,
  1271. // which may override the defaults.
  1272. for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
  1273. ie = Args.filtered_end();
  1274. it != ie; ++it) {
  1275. StringRef Name = (*it)->getOption().getName();
  1276. (*it)->claim();
  1277. // Skip over "-m".
  1278. assert(Name.startswith("m") && "Invalid feature name.");
  1279. Name = Name.substr(1);
  1280. bool IsNegative = Name.startswith("no-");
  1281. if (IsNegative)
  1282. Name = Name.substr(3);
  1283. Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
  1284. }
  1285. }
  1286. void Clang::AddX86TargetArgs(const ArgList &Args,
  1287. ArgStringList &CmdArgs) const {
  1288. if (!Args.hasFlag(options::OPT_mred_zone,
  1289. options::OPT_mno_red_zone,
  1290. true) ||
  1291. Args.hasArg(options::OPT_mkernel) ||
  1292. Args.hasArg(options::OPT_fapple_kext))
  1293. CmdArgs.push_back("-disable-red-zone");
  1294. // Default to avoid implicit floating-point for kernel/kext code, but allow
  1295. // that to be overridden with -mno-soft-float.
  1296. bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
  1297. Args.hasArg(options::OPT_fapple_kext));
  1298. if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
  1299. options::OPT_mno_soft_float,
  1300. options::OPT_mimplicit_float,
  1301. options::OPT_mno_implicit_float)) {
  1302. const Option &O = A->getOption();
  1303. NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
  1304. O.matches(options::OPT_msoft_float));
  1305. }
  1306. if (NoImplicitFloat)
  1307. CmdArgs.push_back("-no-implicit-float");
  1308. if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
  1309. StringRef Value = A->getValue();
  1310. if (Value == "intel" || Value == "att") {
  1311. CmdArgs.push_back("-mllvm");
  1312. CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
  1313. } else {
  1314. getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
  1315. << A->getOption().getName() << Value;
  1316. }
  1317. }
  1318. }
  1319. static inline bool HasPICArg(const ArgList &Args) {
  1320. return Args.hasArg(options::OPT_fPIC)
  1321. || Args.hasArg(options::OPT_fpic);
  1322. }
  1323. static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
  1324. return Args.getLastArg(options::OPT_G,
  1325. options::OPT_G_EQ,
  1326. options::OPT_msmall_data_threshold_EQ);
  1327. }
  1328. static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
  1329. std::string value;
  1330. if (HasPICArg(Args))
  1331. value = "0";
  1332. else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
  1333. value = A->getValue();
  1334. A->claim();
  1335. }
  1336. return value;
  1337. }
  1338. void Clang::AddHexagonTargetArgs(const ArgList &Args,
  1339. ArgStringList &CmdArgs) const {
  1340. CmdArgs.push_back("-fno-signed-char");
  1341. CmdArgs.push_back("-mqdsp6-compat");
  1342. CmdArgs.push_back("-Wreturn-type");
  1343. std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
  1344. if (!SmallDataThreshold.empty()) {
  1345. CmdArgs.push_back ("-mllvm");
  1346. CmdArgs.push_back(Args.MakeArgString(
  1347. "-hexagon-small-data-threshold=" + SmallDataThreshold));
  1348. }
  1349. if (!Args.hasArg(options::OPT_fno_short_enums))
  1350. CmdArgs.push_back("-fshort-enums");
  1351. if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
  1352. CmdArgs.push_back ("-mllvm");
  1353. CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
  1354. }
  1355. CmdArgs.push_back ("-mllvm");
  1356. CmdArgs.push_back ("-machine-sink-split=0");
  1357. }
  1358. static void getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
  1359. std::vector<const char *> &Features) {
  1360. // Honor -mfpu=.
  1361. if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
  1362. getAArch64FPUFeatures(D, A, Args, Features);
  1363. else
  1364. Features.push_back("+neon");
  1365. if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
  1366. Features.push_back("-fp-armv8");
  1367. Features.push_back("-crypto");
  1368. Features.push_back("-neon");
  1369. }
  1370. // En/disable crc
  1371. if (Arg *A = Args.getLastArg(options::OPT_mcrc,
  1372. options::OPT_mnocrc)) {
  1373. if (A->getOption().matches(options::OPT_mcrc))
  1374. Features.push_back("+crc");
  1375. else
  1376. Features.push_back("-crc");
  1377. }
  1378. }
  1379. static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  1380. const ArgList &Args, ArgStringList &CmdArgs,
  1381. bool ForAS) {
  1382. std::vector<const char *> Features;
  1383. switch (Triple.getArch()) {
  1384. default:
  1385. break;
  1386. case llvm::Triple::mips:
  1387. case llvm::Triple::mipsel:
  1388. case llvm::Triple::mips64:
  1389. case llvm::Triple::mips64el:
  1390. getMIPSTargetFeatures(D, Args, Features);
  1391. break;
  1392. case llvm::Triple::arm:
  1393. case llvm::Triple::armeb:
  1394. case llvm::Triple::thumb:
  1395. case llvm::Triple::thumbeb:
  1396. getARMTargetFeatures(D, Triple, Args, Features, ForAS);
  1397. break;
  1398. case llvm::Triple::ppc:
  1399. case llvm::Triple::ppc64:
  1400. case llvm::Triple::ppc64le:
  1401. getPPCTargetFeatures(Args, Features);
  1402. break;
  1403. case llvm::Triple::sparc:
  1404. getSparcTargetFeatures(Args, Features);
  1405. break;
  1406. case llvm::Triple::aarch64:
  1407. case llvm::Triple::aarch64_be:
  1408. case llvm::Triple::arm64:
  1409. case llvm::Triple::arm64_be:
  1410. getAArch64TargetFeatures(D, Args, Features);
  1411. break;
  1412. case llvm::Triple::x86:
  1413. case llvm::Triple::x86_64:
  1414. getX86TargetFeatures(Triple, Args, Features);
  1415. break;
  1416. }
  1417. // Find the last of each feature.
  1418. llvm::StringMap<unsigned> LastOpt;
  1419. for (unsigned I = 0, N = Features.size(); I < N; ++I) {
  1420. const char *Name = Features[I];
  1421. assert(Name[0] == '-' || Name[0] == '+');
  1422. LastOpt[Name + 1] = I;
  1423. }
  1424. for (unsigned I = 0, N = Features.size(); I < N; ++I) {
  1425. // If this feature was overridden, ignore it.
  1426. const char *Name = Features[I];
  1427. llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name + 1);
  1428. assert(LastI != LastOpt.end());
  1429. unsigned Last = LastI->second;
  1430. if (Last != I)
  1431. continue;
  1432. CmdArgs.push_back("-target-feature");
  1433. CmdArgs.push_back(Name);
  1434. }
  1435. }
  1436. static bool
  1437. shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
  1438. const llvm::Triple &Triple) {
  1439. // We use the zero-cost exception tables for Objective-C if the non-fragile
  1440. // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
  1441. // later.
  1442. if (runtime.isNonFragile())
  1443. return true;
  1444. if (!Triple.isMacOSX())
  1445. return false;
  1446. return (!Triple.isMacOSXVersionLT(10,5) &&
  1447. (Triple.getArch() == llvm::Triple::x86_64 ||
  1448. Triple.getArch() == llvm::Triple::arm));
  1449. }
  1450. namespace {
  1451. struct ExceptionSettings {
  1452. bool ExceptionsEnabled;
  1453. bool ShouldUseExceptionTables;
  1454. ExceptionSettings() : ExceptionsEnabled(false),
  1455. ShouldUseExceptionTables(false) {}
  1456. };
  1457. } // end anonymous namespace.
  1458. // exceptionSettings() exists to share the logic between -cc1 and linker
  1459. // invocations.
  1460. static ExceptionSettings exceptionSettings(const ArgList &Args,
  1461. const llvm::Triple &Triple) {
  1462. ExceptionSettings ES;
  1463. // Are exceptions enabled by default?
  1464. ES.ExceptionsEnabled = (Triple.getArch() != llvm::Triple::xcore);
  1465. // This keeps track of whether exceptions were explicitly turned on or off.
  1466. bool DidHaveExplicitExceptionFlag = false;
  1467. if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
  1468. options::OPT_fno_exceptions)) {
  1469. if (A->getOption().matches(options::OPT_fexceptions))
  1470. ES.ExceptionsEnabled = true;
  1471. else
  1472. ES.ExceptionsEnabled = false;
  1473. DidHaveExplicitExceptionFlag = true;
  1474. }
  1475. // Exception tables and cleanups can be enabled with -fexceptions even if the
  1476. // language itself doesn't support exceptions.
  1477. if (ES.ExceptionsEnabled && DidHaveExplicitExceptionFlag)
  1478. ES.ShouldUseExceptionTables = true;
  1479. return ES;
  1480. }
  1481. /// addExceptionArgs - Adds exception related arguments to the driver command
  1482. /// arguments. There's a master flag, -fexceptions and also language specific
  1483. /// flags to enable/disable C++ and Objective-C exceptions.
  1484. /// This makes it possible to for example disable C++ exceptions but enable
  1485. /// Objective-C exceptions.
  1486. static void addExceptionArgs(const ArgList &Args, types::ID InputType,
  1487. const llvm::Triple &Triple,
  1488. bool KernelOrKext,
  1489. const ObjCRuntime &objcRuntime,
  1490. ArgStringList &CmdArgs) {
  1491. if (KernelOrKext) {
  1492. // -mkernel and -fapple-kext imply no exceptions, so claim exception related
  1493. // arguments now to avoid warnings about unused arguments.
  1494. Args.ClaimAllArgs(options::OPT_fexceptions);
  1495. Args.ClaimAllArgs(options::OPT_fno_exceptions);
  1496. Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
  1497. Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
  1498. Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
  1499. Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
  1500. return;
  1501. }
  1502. // Gather the exception settings from the command line arguments.
  1503. ExceptionSettings ES = exceptionSettings(Args, Triple);
  1504. // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
  1505. // is not necessarily sensible, but follows GCC.
  1506. if (types::isObjC(InputType) &&
  1507. Args.hasFlag(options::OPT_fobjc_exceptions,
  1508. options::OPT_fno_objc_exceptions,
  1509. true)) {
  1510. CmdArgs.push_back("-fobjc-exceptions");
  1511. ES.ShouldUseExceptionTables |=
  1512. shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
  1513. }
  1514. if (types::isCXX(InputType)) {
  1515. bool CXXExceptionsEnabled = ES.ExceptionsEnabled;
  1516. if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
  1517. options::OPT_fno_cxx_exceptions,
  1518. options::OPT_fexceptions,
  1519. options::OPT_fno_exceptions)) {
  1520. if (A->getOption().matches(options::OPT_fcxx_exceptions))
  1521. CXXExceptionsEnabled = true;
  1522. else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
  1523. CXXExceptionsEnabled = false;
  1524. }
  1525. if (CXXExceptionsEnabled) {
  1526. CmdArgs.push_back("-fcxx-exceptions");
  1527. ES.ShouldUseExceptionTables = true;
  1528. }
  1529. }
  1530. if (ES.ShouldUseExceptionTables)
  1531. CmdArgs.push_back("-fexceptions");
  1532. }
  1533. static bool ShouldDisableAutolink(const ArgList &Args,
  1534. const ToolChain &TC) {
  1535. bool Default = true;
  1536. if (TC.getTriple().isOSDarwin()) {
  1537. // The native darwin assembler doesn't support the linker_option directives,
  1538. // so we disable them if we think the .s file will be passed to it.
  1539. Default = TC.useIntegratedAs();
  1540. }
  1541. return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
  1542. Default);
  1543. }
  1544. static bool ShouldDisableDwarfDirectory(const ArgList &Args,
  1545. const ToolChain &TC) {
  1546. bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
  1547. options::OPT_fno_dwarf_directory_asm,
  1548. TC.useIntegratedAs());
  1549. return !UseDwarfDirectory;
  1550. }
  1551. /// \brief Check whether the given input tree contains any compilation actions.
  1552. static bool ContainsCompileAction(const Action *A) {
  1553. if (isa<CompileJobAction>(A))
  1554. return true;
  1555. for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
  1556. if (ContainsCompileAction(*it))
  1557. return true;
  1558. return false;
  1559. }
  1560. /// \brief Check if -relax-all should be passed to the internal assembler.
  1561. /// This is done by default when compiling non-assembler source with -O0.
  1562. static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
  1563. bool RelaxDefault = true;
  1564. if (Arg *A = Args.getLastArg(options::OPT_O_Group))
  1565. RelaxDefault = A->getOption().matches(options::OPT_O0);
  1566. if (RelaxDefault) {
  1567. RelaxDefault = false;
  1568. for (ActionList::const_iterator it = C.getActions().begin(),
  1569. ie = C.getActions().end(); it != ie; ++it) {
  1570. if (ContainsCompileAction(*it)) {
  1571. RelaxDefault = true;
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
  1577. RelaxDefault);
  1578. }
  1579. static void CollectArgsForIntegratedAssembler(Compilation &C,
  1580. const ArgList &Args,
  1581. ArgStringList &CmdArgs,
  1582. const Driver &D) {
  1583. if (UseRelaxAll(C, Args))
  1584. CmdArgs.push_back("-mrelax-all");
  1585. // When passing -I arguments to the assembler we sometimes need to
  1586. // unconditionally take the next argument. For example, when parsing
  1587. // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
  1588. // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
  1589. // arg after parsing the '-I' arg.
  1590. bool TakeNextArg = false;
  1591. // When using an integrated assembler, translate -Wa, and -Xassembler
  1592. // options.
  1593. bool CompressDebugSections = false;
  1594. for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
  1595. options::OPT_Xassembler),
  1596. ie = Args.filtered_end(); it != ie; ++it) {
  1597. const Arg *A = *it;
  1598. A->claim();
  1599. for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
  1600. StringRef Value = A->getValue(i);
  1601. if (TakeNextArg) {
  1602. CmdArgs.push_back(Value.data());
  1603. TakeNextArg = false;
  1604. continue;
  1605. }
  1606. if (Value == "-force_cpusubtype_ALL") {
  1607. // Do nothing, this is the default and we don't support anything else.
  1608. } else if (Value == "-L") {
  1609. CmdArgs.push_back("-msave-temp-labels");
  1610. } else if (Value == "--fatal-warnings") {
  1611. CmdArgs.push_back("-mllvm");
  1612. CmdArgs.push_back("-fatal-assembler-warnings");
  1613. } else if (Value == "--noexecstack") {
  1614. CmdArgs.push_back("-mnoexecstack");
  1615. } else if (Value == "-compress-debug-sections" ||
  1616. Value == "--compress-debug-sections") {
  1617. CompressDebugSections = true;
  1618. } else if (Value == "-nocompress-debug-sections" ||
  1619. Value == "--nocompress-debug-sections") {
  1620. CompressDebugSections = false;
  1621. } else if (Value.startswith("-I")) {
  1622. CmdArgs.push_back(Value.data());
  1623. // We need to consume the next argument if the current arg is a plain
  1624. // -I. The next arg will be the include directory.
  1625. if (Value == "-I")
  1626. TakeNextArg = true;
  1627. } else if (Value.startswith("-gdwarf-")) {
  1628. CmdArgs.push_back(Value.data());
  1629. } else {
  1630. D.Diag(diag::err_drv_unsupported_option_argument)
  1631. << A->getOption().getName() << Value;
  1632. }
  1633. }
  1634. }
  1635. if (CompressDebugSections) {
  1636. if (llvm::zlib::isAvailable())
  1637. CmdArgs.push_back("-compress-debug-sections");
  1638. else
  1639. D.Diag(diag::warn_debug_compression_unavailable);
  1640. }
  1641. }
  1642. // Until ARM libraries are build separately, we have them all in one library
  1643. static StringRef getArchNameForCompilerRTLib(const ToolChain &TC) {
  1644. if (TC.getArch() == llvm::Triple::arm ||
  1645. TC.getArch() == llvm::Triple::armeb)
  1646. return "arm";
  1647. else
  1648. return TC.getArchName();
  1649. }
  1650. static SmallString<128> getCompilerRTLibDir(const ToolChain &TC) {
  1651. // The runtimes are located in the OS-specific resource directory.
  1652. SmallString<128> Res(TC.getDriver().ResourceDir);
  1653. const llvm::Triple &Triple = TC.getTriple();
  1654. // TC.getOS() yield "freebsd10.0" whereas "freebsd" is expected.
  1655. StringRef OSLibName = (Triple.getOS() == llvm::Triple::FreeBSD) ?
  1656. "freebsd" : TC.getOS();
  1657. llvm::sys::path::append(Res, "lib", OSLibName);
  1658. return Res;
  1659. }
  1660. // This adds the static libclang_rt.builtins-arch.a directly to the command line
  1661. // FIXME: Make sure we can also emit shared objects if they're requested
  1662. // and available, check for possible errors, etc.
  1663. static void addClangRTLinux(
  1664. const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
  1665. SmallString<128> LibClangRT = getCompilerRTLibDir(TC);
  1666. llvm::sys::path::append(LibClangRT, Twine("libclang_rt.builtins-") +
  1667. getArchNameForCompilerRTLib(TC) +
  1668. ".a");
  1669. CmdArgs.push_back(Args.MakeArgString(LibClangRT));
  1670. CmdArgs.push_back("-lgcc_s");
  1671. if (TC.getDriver().CCCIsCXX())
  1672. CmdArgs.push_back("-lgcc_eh");
  1673. }
  1674. static void addProfileRT(
  1675. const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
  1676. if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
  1677. Args.hasArg(options::OPT_fprofile_generate) ||
  1678. Args.hasArg(options::OPT_fprofile_instr_generate) ||
  1679. Args.hasArg(options::OPT_fcreate_profile) ||
  1680. Args.hasArg(options::OPT_coverage)))
  1681. return;
  1682. // -fprofile-instr-generate requires position-independent code to build with
  1683. // shared objects. Link against the right archive.
  1684. const char *Lib = "libclang_rt.profile-";
  1685. if (Args.hasArg(options::OPT_fprofile_instr_generate) &&
  1686. Args.hasArg(options::OPT_shared))
  1687. Lib = "libclang_rt.profile-pic-";
  1688. SmallString<128> LibProfile = getCompilerRTLibDir(TC);
  1689. llvm::sys::path::append(LibProfile,
  1690. Twine(Lib) + getArchNameForCompilerRTLib(TC) + ".a");
  1691. CmdArgs.push_back(Args.MakeArgString(LibProfile));
  1692. }
  1693. static SmallString<128> getSanitizerRTLibName(const ToolChain &TC,
  1694. const StringRef Sanitizer,
  1695. bool Shared) {
  1696. // Sanitizer runtime has name "libclang_rt.<Sanitizer>-<ArchName>.{a,so}"
  1697. // (or "libclang_rt.<Sanitizer>-<ArchName>-android.so for Android)
  1698. const char *EnvSuffix =
  1699. TC.getTriple().getEnvironment() == llvm::Triple::Android ? "-android" : "";
  1700. SmallString<128> LibSanitizer = getCompilerRTLibDir(TC);
  1701. llvm::sys::path::append(LibSanitizer,
  1702. Twine("libclang_rt.") + Sanitizer + "-" +
  1703. getArchNameForCompilerRTLib(TC) + EnvSuffix +
  1704. (Shared ? ".so" : ".a"));
  1705. return LibSanitizer;
  1706. }
  1707. static void addSanitizerRTLinkFlags(const ToolChain &TC, const ArgList &Args,
  1708. ArgStringList &CmdArgs,
  1709. const StringRef Sanitizer,
  1710. bool BeforeLibStdCXX,
  1711. bool ExportSymbols = true,
  1712. bool LinkDeps = true) {
  1713. SmallString<128> LibSanitizer =
  1714. getSanitizerRTLibName(TC, Sanitizer, /*Shared*/ false);
  1715. // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a,
  1716. // etc.) so that the linker picks custom versions of the global 'operator
  1717. // new' and 'operator delete' symbols. We take the extreme (but simple)
  1718. // strategy of inserting it at the front of the link command. It also
  1719. // needs to be forced to end up in the executable, so wrap it in
  1720. // whole-archive.
  1721. SmallVector<const char *, 3> LibSanitizerArgs;
  1722. LibSanitizerArgs.push_back("-whole-archive");
  1723. LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer));
  1724. LibSanitizerArgs.push_back("-no-whole-archive");
  1725. CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(),
  1726. LibSanitizerArgs.begin(), LibSanitizerArgs.end());
  1727. if (LinkDeps) {
  1728. // Link sanitizer dependencies explicitly
  1729. CmdArgs.push_back("-lpthread");
  1730. CmdArgs.push_back("-lrt");
  1731. CmdArgs.push_back("-lm");
  1732. // There's no libdl on FreeBSD.
  1733. if (TC.getTriple().getOS() != llvm::Triple::FreeBSD)
  1734. CmdArgs.push_back("-ldl");
  1735. }
  1736. // If possible, use a dynamic symbols file to export the symbols from the
  1737. // runtime library. If we can't do so, use -export-dynamic instead to export
  1738. // all symbols from the binary.
  1739. if (ExportSymbols) {
  1740. if (llvm::sys::fs::exists(LibSanitizer + ".syms"))
  1741. CmdArgs.push_back(
  1742. Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms"));
  1743. else
  1744. CmdArgs.push_back("-export-dynamic");
  1745. }
  1746. }
  1747. /// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
  1748. /// This needs to be called before we add the C run-time (malloc, etc).
  1749. static void addAsanRT(const ToolChain &TC, const ArgList &Args,
  1750. ArgStringList &CmdArgs, bool Shared, bool IsCXX) {
  1751. if (Shared) {
  1752. // Link dynamic runtime if necessary.
  1753. SmallString<128> LibSanitizer =
  1754. getSanitizerRTLibName(TC, "asan", Shared);
  1755. CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibSanitizer));
  1756. }
  1757. // Do not link static runtime to DSOs or if compiling for Android.
  1758. if (Args.hasArg(options::OPT_shared) ||
  1759. (TC.getTriple().getEnvironment() == llvm::Triple::Android))
  1760. return;
  1761. if (Shared) {
  1762. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "asan-preinit",
  1763. /*BeforeLibStdCXX*/ true, /*ExportSymbols*/ false,
  1764. /*LinkDeps*/ false);
  1765. } else {
  1766. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "asan", true);
  1767. if (IsCXX)
  1768. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "asan_cxx", true);
  1769. }
  1770. }
  1771. /// If ThreadSanitizer is enabled, add appropriate linker flags (Linux).
  1772. /// This needs to be called before we add the C run-time (malloc, etc).
  1773. static void addTsanRT(const ToolChain &TC, const ArgList &Args,
  1774. ArgStringList &CmdArgs) {
  1775. if (!Args.hasArg(options::OPT_shared))
  1776. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "tsan", true);
  1777. }
  1778. /// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
  1779. /// This needs to be called before we add the C run-time (malloc, etc).
  1780. static void addMsanRT(const ToolChain &TC, const ArgList &Args,
  1781. ArgStringList &CmdArgs) {
  1782. if (!Args.hasArg(options::OPT_shared))
  1783. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "msan", true);
  1784. }
  1785. /// If LeakSanitizer is enabled, add appropriate linker flags (Linux).
  1786. /// This needs to be called before we add the C run-time (malloc, etc).
  1787. static void addLsanRT(const ToolChain &TC, const ArgList &Args,
  1788. ArgStringList &CmdArgs) {
  1789. if (!Args.hasArg(options::OPT_shared))
  1790. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "lsan", true);
  1791. }
  1792. /// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
  1793. /// (Linux).
  1794. static void addUbsanRT(const ToolChain &TC, const ArgList &Args,
  1795. ArgStringList &CmdArgs, bool IsCXX,
  1796. bool HasOtherSanitizerRt) {
  1797. // Export symbols if we're not building a shared library. This allows two
  1798. // models: either every DSO containing ubsan-sanitized code contains the
  1799. // ubsan runtime, or the main executable does (or both).
  1800. const bool ExportSymbols = !Args.hasArg(options::OPT_shared);
  1801. // Need a copy of sanitizer_common. This could come from another sanitizer
  1802. // runtime; if we're not including one, include our own copy.
  1803. if (!HasOtherSanitizerRt)
  1804. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "san", true, false);
  1805. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "ubsan", false, ExportSymbols);
  1806. // Only include the bits of the runtime which need a C++ ABI library if
  1807. // we're linking in C++ mode.
  1808. if (IsCXX)
  1809. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "ubsan_cxx", false,
  1810. ExportSymbols);
  1811. }
  1812. static void addDfsanRT(const ToolChain &TC, const ArgList &Args,
  1813. ArgStringList &CmdArgs) {
  1814. if (!Args.hasArg(options::OPT_shared))
  1815. addSanitizerRTLinkFlags(TC, Args, CmdArgs, "dfsan", true);
  1816. }
  1817. // Should be called before we add C++ ABI library.
  1818. static void addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
  1819. ArgStringList &CmdArgs) {
  1820. const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
  1821. const Driver &D = TC.getDriver();
  1822. if (Sanitize.needsUbsanRt())
  1823. addUbsanRT(TC, Args, CmdArgs, D.CCCIsCXX(),
  1824. Sanitize.needsAsanRt() || Sanitize.needsTsanRt() ||
  1825. Sanitize.needsMsanRt() || Sanitize.needsLsanRt());
  1826. if (Sanitize.needsAsanRt())
  1827. addAsanRT(TC, Args, CmdArgs, Sanitize.needsSharedAsanRt(), D.CCCIsCXX());
  1828. if (Sanitize.needsTsanRt())
  1829. addTsanRT(TC, Args, CmdArgs);
  1830. if (Sanitize.needsMsanRt())
  1831. addMsanRT(TC, Args, CmdArgs);
  1832. if (Sanitize.needsLsanRt())
  1833. addLsanRT(TC, Args, CmdArgs);
  1834. if (Sanitize.needsDfsanRt())
  1835. addDfsanRT(TC, Args, CmdArgs);
  1836. }
  1837. static bool shouldUseFramePointerForTarget(const ArgList &Args,
  1838. const llvm::Triple &Triple) {
  1839. switch (Triple.getArch()) {
  1840. // Don't use a frame pointer on linux if optimizing for certain targets.
  1841. case llvm::Triple::mips64:
  1842. case llvm::Triple::mips64el:
  1843. case llvm::Triple::mips:
  1844. case llvm::Triple::mipsel:
  1845. case llvm::Triple::systemz:
  1846. case llvm::Triple::x86:
  1847. case llvm::Triple::x86_64:
  1848. if (Triple.isOSLinux())
  1849. if (Arg *A = Args.getLastArg(options::OPT_O_Group))
  1850. if (!A->getOption().matches(options::OPT_O0))
  1851. return false;
  1852. return true;
  1853. case llvm::Triple::xcore:
  1854. return false;
  1855. default:
  1856. return true;
  1857. }
  1858. }
  1859. static bool shouldUseFramePointer(const ArgList &Args,
  1860. const llvm::Triple &Triple) {
  1861. if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
  1862. options::OPT_fomit_frame_pointer))
  1863. return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
  1864. return shouldUseFramePointerForTarget(Args, Triple);
  1865. }
  1866. static bool shouldUseLeafFramePointer(const ArgList &Args,
  1867. const llvm::Triple &Triple) {
  1868. if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
  1869. options::OPT_momit_leaf_frame_pointer))
  1870. return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
  1871. return shouldUseFramePointerForTarget(Args, Triple);
  1872. }
  1873. /// Add a CC1 option to specify the debug compilation directory.
  1874. static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
  1875. SmallString<128> cwd;
  1876. if (!llvm::sys::fs::current_path(cwd)) {
  1877. CmdArgs.push_back("-fdebug-compilation-dir");
  1878. CmdArgs.push_back(Args.MakeArgString(cwd));
  1879. }
  1880. }
  1881. static const char *SplitDebugName(const ArgList &Args,
  1882. const InputInfoList &Inputs) {
  1883. Arg *FinalOutput = Args.getLastArg(options::OPT_o);
  1884. if (FinalOutput && Args.hasArg(options::OPT_c)) {
  1885. SmallString<128> T(FinalOutput->getValue());
  1886. llvm::sys::path::replace_extension(T, "dwo");
  1887. return Args.MakeArgString(T);
  1888. } else {
  1889. // Use the compilation dir.
  1890. SmallString<128> T(
  1891. Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
  1892. SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
  1893. llvm::sys::path::replace_extension(F, "dwo");
  1894. T += F;
  1895. return Args.MakeArgString(F);
  1896. }
  1897. }
  1898. static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
  1899. const Tool &T, const JobAction &JA,
  1900. const ArgList &Args, const InputInfo &Output,
  1901. const char *OutFile) {
  1902. ArgStringList ExtractArgs;
  1903. ExtractArgs.push_back("--extract-dwo");
  1904. ArgStringList StripArgs;
  1905. StripArgs.push_back("--strip-dwo");
  1906. // Grabbing the output of the earlier compile step.
  1907. StripArgs.push_back(Output.getFilename());
  1908. ExtractArgs.push_back(Output.getFilename());
  1909. ExtractArgs.push_back(OutFile);
  1910. const char *Exec =
  1911. Args.MakeArgString(TC.GetProgramPath("objcopy"));
  1912. // First extract the dwo sections.
  1913. C.addCommand(new Command(JA, T, Exec, ExtractArgs));
  1914. // Then remove them from the original .o file.
  1915. C.addCommand(new Command(JA, T, Exec, StripArgs));
  1916. }
  1917. /// \brief Vectorize at all optimization levels greater than 1 except for -Oz.
  1918. /// For -Oz the loop vectorizer is disable, while the slp vectorizer is enabled.
  1919. static bool shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
  1920. if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
  1921. if (A->getOption().matches(options::OPT_O4) ||
  1922. A->getOption().matches(options::OPT_Ofast))
  1923. return true;
  1924. if (A->getOption().matches(options::OPT_O0))
  1925. return false;
  1926. assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
  1927. // Vectorize -Os.
  1928. StringRef S(A->getValue());
  1929. if (S == "s")
  1930. return true;
  1931. // Don't vectorize -Oz, unless it's the slp vectorizer.
  1932. if (S == "z")
  1933. return isSlpVec;
  1934. unsigned OptLevel = 0;
  1935. if (S.getAsInteger(10, OptLevel))
  1936. return false;
  1937. return OptLevel > 1;
  1938. }
  1939. return false;
  1940. }
  1941. /// Add -x lang to \p CmdArgs for \p Input.
  1942. static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
  1943. ArgStringList &CmdArgs) {
  1944. // When using -verify-pch, we don't want to provide the type
  1945. // 'precompiled-header' if it was inferred from the file extension
  1946. if (Args.hasArg(options::OPT_verify_pch) && Input.getType() == types::TY_PCH)
  1947. return;
  1948. CmdArgs.push_back("-x");
  1949. if (Args.hasArg(options::OPT_rewrite_objc))
  1950. CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
  1951. else
  1952. CmdArgs.push_back(types::getTypeName(Input.getType()));
  1953. }
  1954. void Clang::ConstructJob(Compilation &C, const JobAction &JA,
  1955. const InputInfo &Output,
  1956. const InputInfoList &Inputs,
  1957. const ArgList &Args,
  1958. const char *LinkingOutput) const {
  1959. bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
  1960. options::OPT_fapple_kext);
  1961. const Driver &D = getToolChain().getDriver();
  1962. ArgStringList CmdArgs;
  1963. bool IsWindowsGNU = getToolChain().getTriple().isWindowsGNUEnvironment();
  1964. bool IsWindowsCygnus =
  1965. getToolChain().getTriple().isWindowsCygwinEnvironment();
  1966. bool IsWindowsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment();
  1967. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  1968. // Invoke ourselves in -cc1 mode.
  1969. //
  1970. // FIXME: Implement custom jobs for internal actions.
  1971. CmdArgs.push_back("-cc1");
  1972. // Add the "effective" target triple.
  1973. CmdArgs.push_back("-triple");
  1974. std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  1975. CmdArgs.push_back(Args.MakeArgString(TripleStr));
  1976. const llvm::Triple TT(TripleStr);
  1977. if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
  1978. TT.getArch() == llvm::Triple::thumb)) {
  1979. unsigned Offset = TT.getArch() == llvm::Triple::arm ? 4 : 6;
  1980. unsigned Version;
  1981. TT.getArchName().substr(Offset).getAsInteger(10, Version);
  1982. if (Version < 7)
  1983. D.Diag(diag::err_target_unsupported_arch) << TT.getArchName()
  1984. << TripleStr;
  1985. }
  1986. // Push all default warning arguments that are specific to
  1987. // the given target. These come before user provided warning options
  1988. // are provided.
  1989. getToolChain().addClangWarningOptions(CmdArgs);
  1990. // Select the appropriate action.
  1991. RewriteKind rewriteKind = RK_None;
  1992. if (isa<AnalyzeJobAction>(JA)) {
  1993. assert(JA.getType() == types::TY_Plist && "Invalid output type.");
  1994. CmdArgs.push_back("-analyze");
  1995. } else if (isa<MigrateJobAction>(JA)) {
  1996. CmdArgs.push_back("-migrate");
  1997. } else if (isa<PreprocessJobAction>(JA)) {
  1998. if (Output.getType() == types::TY_Dependencies)
  1999. CmdArgs.push_back("-Eonly");
  2000. else {
  2001. CmdArgs.push_back("-E");
  2002. if (Args.hasArg(options::OPT_rewrite_objc) &&
  2003. !Args.hasArg(options::OPT_g_Group))
  2004. CmdArgs.push_back("-P");
  2005. }
  2006. } else if (isa<AssembleJobAction>(JA)) {
  2007. CmdArgs.push_back("-emit-obj");
  2008. CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
  2009. // Also ignore explicit -force_cpusubtype_ALL option.
  2010. (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
  2011. } else if (isa<PrecompileJobAction>(JA)) {
  2012. // Use PCH if the user requested it.
  2013. bool UsePCH = D.CCCUsePCH;
  2014. if (JA.getType() == types::TY_Nothing)
  2015. CmdArgs.push_back("-fsyntax-only");
  2016. else if (UsePCH)
  2017. CmdArgs.push_back("-emit-pch");
  2018. else
  2019. CmdArgs.push_back("-emit-pth");
  2020. } else if (isa<VerifyPCHJobAction>(JA)) {
  2021. CmdArgs.push_back("-verify-pch");
  2022. } else {
  2023. assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
  2024. if (JA.getType() == types::TY_Nothing) {
  2025. CmdArgs.push_back("-fsyntax-only");
  2026. } else if (JA.getType() == types::TY_LLVM_IR ||
  2027. JA.getType() == types::TY_LTO_IR) {
  2028. CmdArgs.push_back("-emit-llvm");
  2029. } else if (JA.getType() == types::TY_LLVM_BC ||
  2030. JA.getType() == types::TY_LTO_BC) {
  2031. CmdArgs.push_back("-emit-llvm-bc");
  2032. } else if (JA.getType() == types::TY_PP_Asm) {
  2033. CmdArgs.push_back("-S");
  2034. } else if (JA.getType() == types::TY_AST) {
  2035. CmdArgs.push_back("-emit-pch");
  2036. } else if (JA.getType() == types::TY_ModuleFile) {
  2037. CmdArgs.push_back("-module-file-info");
  2038. } else if (JA.getType() == types::TY_RewrittenObjC) {
  2039. CmdArgs.push_back("-rewrite-objc");
  2040. rewriteKind = RK_NonFragile;
  2041. } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
  2042. CmdArgs.push_back("-rewrite-objc");
  2043. rewriteKind = RK_Fragile;
  2044. } else {
  2045. assert(JA.getType() == types::TY_PP_Asm &&
  2046. "Unexpected output type!");
  2047. }
  2048. }
  2049. // The make clang go fast button.
  2050. CmdArgs.push_back("-disable-free");
  2051. // Disable the verification pass in -asserts builds.
  2052. #ifdef NDEBUG
  2053. CmdArgs.push_back("-disable-llvm-verifier");
  2054. #endif
  2055. // Set the main file name, so that debug info works even with
  2056. // -save-temps.
  2057. CmdArgs.push_back("-main-file-name");
  2058. CmdArgs.push_back(getBaseInputName(Args, Inputs));
  2059. // Some flags which affect the language (via preprocessor
  2060. // defines).
  2061. if (Args.hasArg(options::OPT_static))
  2062. CmdArgs.push_back("-static-define");
  2063. if (isa<AnalyzeJobAction>(JA)) {
  2064. // Enable region store model by default.
  2065. CmdArgs.push_back("-analyzer-store=region");
  2066. // Treat blocks as analysis entry points.
  2067. CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
  2068. CmdArgs.push_back("-analyzer-eagerly-assume");
  2069. // Add default argument set.
  2070. if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
  2071. CmdArgs.push_back("-analyzer-checker=core");
  2072. if (!IsWindowsMSVC)
  2073. CmdArgs.push_back("-analyzer-checker=unix");
  2074. if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
  2075. CmdArgs.push_back("-analyzer-checker=osx");
  2076. CmdArgs.push_back("-analyzer-checker=deadcode");
  2077. if (types::isCXX(Inputs[0].getType()))
  2078. CmdArgs.push_back("-analyzer-checker=cplusplus");
  2079. // Enable the following experimental checkers for testing.
  2080. CmdArgs.push_back(
  2081. "-analyzer-checker=security.insecureAPI.UncheckedReturn");
  2082. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
  2083. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
  2084. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
  2085. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
  2086. CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
  2087. }
  2088. // Set the output format. The default is plist, for (lame) historical
  2089. // reasons.
  2090. CmdArgs.push_back("-analyzer-output");
  2091. if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
  2092. CmdArgs.push_back(A->getValue());
  2093. else
  2094. CmdArgs.push_back("plist");
  2095. // Disable the presentation of standard compiler warnings when
  2096. // using --analyze. We only want to show static analyzer diagnostics
  2097. // or frontend errors.
  2098. CmdArgs.push_back("-w");
  2099. // Add -Xanalyzer arguments when running as analyzer.
  2100. Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
  2101. }
  2102. CheckCodeGenerationOptions(D, Args);
  2103. bool PIE = getToolChain().isPIEDefault();
  2104. bool PIC = PIE || getToolChain().isPICDefault();
  2105. bool IsPICLevelTwo = PIC;
  2106. // Android-specific defaults for PIC/PIE
  2107. if (getToolChain().getTriple().getEnvironment() == llvm::Triple::Android) {
  2108. switch (getToolChain().getTriple().getArch()) {
  2109. case llvm::Triple::arm:
  2110. case llvm::Triple::armeb:
  2111. case llvm::Triple::thumb:
  2112. case llvm::Triple::thumbeb:
  2113. case llvm::Triple::aarch64:
  2114. case llvm::Triple::arm64:
  2115. case llvm::Triple::mips:
  2116. case llvm::Triple::mipsel:
  2117. case llvm::Triple::mips64:
  2118. case llvm::Triple::mips64el:
  2119. PIC = true; // "-fpic"
  2120. break;
  2121. case llvm::Triple::x86:
  2122. case llvm::Triple::x86_64:
  2123. PIC = true; // "-fPIC"
  2124. IsPICLevelTwo = true;
  2125. break;
  2126. default:
  2127. break;
  2128. }
  2129. }
  2130. // For the PIC and PIE flag options, this logic is different from the
  2131. // legacy logic in very old versions of GCC, as that logic was just
  2132. // a bug no one had ever fixed. This logic is both more rational and
  2133. // consistent with GCC's new logic now that the bugs are fixed. The last
  2134. // argument relating to either PIC or PIE wins, and no other argument is
  2135. // used. If the last argument is any flavor of the '-fno-...' arguments,
  2136. // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
  2137. // at the same level.
  2138. Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
  2139. options::OPT_fpic, options::OPT_fno_pic,
  2140. options::OPT_fPIE, options::OPT_fno_PIE,
  2141. options::OPT_fpie, options::OPT_fno_pie);
  2142. // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
  2143. // is forced, then neither PIC nor PIE flags will have no effect.
  2144. if (!getToolChain().isPICDefaultForced()) {
  2145. if (LastPICArg) {
  2146. Option O = LastPICArg->getOption();
  2147. if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
  2148. O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
  2149. PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
  2150. PIC = PIE || O.matches(options::OPT_fPIC) ||
  2151. O.matches(options::OPT_fpic);
  2152. IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
  2153. O.matches(options::OPT_fPIC);
  2154. } else {
  2155. PIE = PIC = false;
  2156. }
  2157. }
  2158. }
  2159. // Introduce a Darwin-specific hack. If the default is PIC but the flags
  2160. // specified while enabling PIC enabled level 1 PIC, just force it back to
  2161. // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
  2162. // informal testing).
  2163. if (PIC && getToolChain().getTriple().isOSDarwin())
  2164. IsPICLevelTwo |= getToolChain().isPICDefault();
  2165. // Note that these flags are trump-cards. Regardless of the order w.r.t. the
  2166. // PIC or PIE options above, if these show up, PIC is disabled.
  2167. llvm::Triple Triple(TripleStr);
  2168. if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6) ||
  2169. Triple.getArch() == llvm::Triple::arm64 ||
  2170. Triple.getArch() == llvm::Triple::aarch64))
  2171. PIC = PIE = false;
  2172. if (Args.hasArg(options::OPT_static))
  2173. PIC = PIE = false;
  2174. if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
  2175. // This is a very special mode. It trumps the other modes, almost no one
  2176. // uses it, and it isn't even valid on any OS but Darwin.
  2177. if (!getToolChain().getTriple().isOSDarwin())
  2178. D.Diag(diag::err_drv_unsupported_opt_for_target)
  2179. << A->getSpelling() << getToolChain().getTriple().str();
  2180. // FIXME: Warn when this flag trumps some other PIC or PIE flag.
  2181. CmdArgs.push_back("-mrelocation-model");
  2182. CmdArgs.push_back("dynamic-no-pic");
  2183. // Only a forced PIC mode can cause the actual compile to have PIC defines
  2184. // etc., no flags are sufficient. This behavior was selected to closely
  2185. // match that of llvm-gcc and Apple GCC before that.
  2186. if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
  2187. CmdArgs.push_back("-pic-level");
  2188. CmdArgs.push_back("2");
  2189. }
  2190. } else {
  2191. // Currently, LLVM only knows about PIC vs. static; the PIE differences are
  2192. // handled in Clang's IRGen by the -pie-level flag.
  2193. CmdArgs.push_back("-mrelocation-model");
  2194. CmdArgs.push_back(PIC ? "pic" : "static");
  2195. if (PIC) {
  2196. CmdArgs.push_back("-pic-level");
  2197. CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
  2198. if (PIE) {
  2199. CmdArgs.push_back("-pie-level");
  2200. CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
  2201. }
  2202. }
  2203. }
  2204. if (!Args.hasFlag(options::OPT_fmerge_all_constants,
  2205. options::OPT_fno_merge_all_constants))
  2206. CmdArgs.push_back("-fno-merge-all-constants");
  2207. // LLVM Code Generator Options.
  2208. if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
  2209. StringRef v = A->getValue();
  2210. CmdArgs.push_back("-mllvm");
  2211. CmdArgs.push_back(Args.MakeArgString("-warn-stack-size=" + v));
  2212. A->claim();
  2213. }
  2214. if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
  2215. CmdArgs.push_back("-mregparm");
  2216. CmdArgs.push_back(A->getValue());
  2217. }
  2218. if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
  2219. options::OPT_freg_struct_return)) {
  2220. if (getToolChain().getArch() != llvm::Triple::x86) {
  2221. D.Diag(diag::err_drv_unsupported_opt_for_target)
  2222. << A->getSpelling() << getToolChain().getTriple().str();
  2223. } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
  2224. CmdArgs.push_back("-fpcc-struct-return");
  2225. } else {
  2226. assert(A->getOption().matches(options::OPT_freg_struct_return));
  2227. CmdArgs.push_back("-freg-struct-return");
  2228. }
  2229. }
  2230. if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
  2231. CmdArgs.push_back("-mrtd");
  2232. if (shouldUseFramePointer(Args, getToolChain().getTriple()))
  2233. CmdArgs.push_back("-mdisable-fp-elim");
  2234. if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
  2235. options::OPT_fno_zero_initialized_in_bss))
  2236. CmdArgs.push_back("-mno-zero-initialized-in-bss");
  2237. bool OFastEnabled = isOptimizationLevelFast(Args);
  2238. // If -Ofast is the optimization level, then -fstrict-aliasing should be
  2239. // enabled. This alias option is being used to simplify the hasFlag logic.
  2240. OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast :
  2241. options::OPT_fstrict_aliasing;
  2242. // We turn strict aliasing off by default if we're in CL mode, since MSVC
  2243. // doesn't do any TBAA.
  2244. bool TBAAOnByDefault = !getToolChain().getDriver().IsCLMode();
  2245. if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
  2246. options::OPT_fno_strict_aliasing, TBAAOnByDefault))
  2247. CmdArgs.push_back("-relaxed-aliasing");
  2248. if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
  2249. options::OPT_fno_struct_path_tbaa))
  2250. CmdArgs.push_back("-no-struct-path-tbaa");
  2251. if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
  2252. false))
  2253. CmdArgs.push_back("-fstrict-enums");
  2254. if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
  2255. options::OPT_fno_optimize_sibling_calls))
  2256. CmdArgs.push_back("-mdisable-tail-calls");
  2257. // Handle segmented stacks.
  2258. if (Args.hasArg(options::OPT_fsplit_stack))
  2259. CmdArgs.push_back("-split-stacks");
  2260. // If -Ofast is the optimization level, then -ffast-math should be enabled.
  2261. // This alias option is being used to simplify the getLastArg logic.
  2262. OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast :
  2263. options::OPT_ffast_math;
  2264. // Handle various floating point optimization flags, mapping them to the
  2265. // appropriate LLVM code generation flags. The pattern for all of these is to
  2266. // default off the codegen optimizations, and if any flag enables them and no
  2267. // flag disables them after the flag enabling them, enable the codegen
  2268. // optimization. This is complicated by several "umbrella" flags.
  2269. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2270. options::OPT_fno_fast_math,
  2271. options::OPT_ffinite_math_only,
  2272. options::OPT_fno_finite_math_only,
  2273. options::OPT_fhonor_infinities,
  2274. options::OPT_fno_honor_infinities))
  2275. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2276. A->getOption().getID() != options::OPT_fno_finite_math_only &&
  2277. A->getOption().getID() != options::OPT_fhonor_infinities)
  2278. CmdArgs.push_back("-menable-no-infs");
  2279. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2280. options::OPT_fno_fast_math,
  2281. options::OPT_ffinite_math_only,
  2282. options::OPT_fno_finite_math_only,
  2283. options::OPT_fhonor_nans,
  2284. options::OPT_fno_honor_nans))
  2285. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2286. A->getOption().getID() != options::OPT_fno_finite_math_only &&
  2287. A->getOption().getID() != options::OPT_fhonor_nans)
  2288. CmdArgs.push_back("-menable-no-nans");
  2289. // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
  2290. bool MathErrno = getToolChain().IsMathErrnoDefault();
  2291. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2292. options::OPT_fno_fast_math,
  2293. options::OPT_fmath_errno,
  2294. options::OPT_fno_math_errno)) {
  2295. // Turning on -ffast_math (with either flag) removes the need for MathErrno.
  2296. // However, turning *off* -ffast_math merely restores the toolchain default
  2297. // (which may be false).
  2298. if (A->getOption().getID() == options::OPT_fno_math_errno ||
  2299. A->getOption().getID() == options::OPT_ffast_math ||
  2300. A->getOption().getID() == options::OPT_Ofast)
  2301. MathErrno = false;
  2302. else if (A->getOption().getID() == options::OPT_fmath_errno)
  2303. MathErrno = true;
  2304. }
  2305. if (MathErrno)
  2306. CmdArgs.push_back("-fmath-errno");
  2307. // There are several flags which require disabling very specific
  2308. // optimizations. Any of these being disabled forces us to turn off the
  2309. // entire set of LLVM optimizations, so collect them through all the flag
  2310. // madness.
  2311. bool AssociativeMath = false;
  2312. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2313. options::OPT_fno_fast_math,
  2314. options::OPT_funsafe_math_optimizations,
  2315. options::OPT_fno_unsafe_math_optimizations,
  2316. options::OPT_fassociative_math,
  2317. options::OPT_fno_associative_math))
  2318. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2319. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  2320. A->getOption().getID() != options::OPT_fno_associative_math)
  2321. AssociativeMath = true;
  2322. bool ReciprocalMath = false;
  2323. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2324. options::OPT_fno_fast_math,
  2325. options::OPT_funsafe_math_optimizations,
  2326. options::OPT_fno_unsafe_math_optimizations,
  2327. options::OPT_freciprocal_math,
  2328. options::OPT_fno_reciprocal_math))
  2329. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2330. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  2331. A->getOption().getID() != options::OPT_fno_reciprocal_math)
  2332. ReciprocalMath = true;
  2333. bool SignedZeros = true;
  2334. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2335. options::OPT_fno_fast_math,
  2336. options::OPT_funsafe_math_optimizations,
  2337. options::OPT_fno_unsafe_math_optimizations,
  2338. options::OPT_fsigned_zeros,
  2339. options::OPT_fno_signed_zeros))
  2340. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2341. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  2342. A->getOption().getID() != options::OPT_fsigned_zeros)
  2343. SignedZeros = false;
  2344. bool TrappingMath = true;
  2345. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2346. options::OPT_fno_fast_math,
  2347. options::OPT_funsafe_math_optimizations,
  2348. options::OPT_fno_unsafe_math_optimizations,
  2349. options::OPT_ftrapping_math,
  2350. options::OPT_fno_trapping_math))
  2351. if (A->getOption().getID() != options::OPT_fno_fast_math &&
  2352. A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
  2353. A->getOption().getID() != options::OPT_ftrapping_math)
  2354. TrappingMath = false;
  2355. if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
  2356. !TrappingMath)
  2357. CmdArgs.push_back("-menable-unsafe-fp-math");
  2358. // Validate and pass through -fp-contract option.
  2359. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2360. options::OPT_fno_fast_math,
  2361. options::OPT_ffp_contract)) {
  2362. if (A->getOption().getID() == options::OPT_ffp_contract) {
  2363. StringRef Val = A->getValue();
  2364. if (Val == "fast" || Val == "on" || Val == "off") {
  2365. CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
  2366. } else {
  2367. D.Diag(diag::err_drv_unsupported_option_argument)
  2368. << A->getOption().getName() << Val;
  2369. }
  2370. } else if (A->getOption().matches(options::OPT_ffast_math) ||
  2371. (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) {
  2372. // If fast-math is set then set the fp-contract mode to fast.
  2373. CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
  2374. }
  2375. }
  2376. // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
  2377. // and if we find them, tell the frontend to provide the appropriate
  2378. // preprocessor macros. This is distinct from enabling any optimizations as
  2379. // these options induce language changes which must survive serialization
  2380. // and deserialization, etc.
  2381. if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
  2382. options::OPT_fno_fast_math))
  2383. if (!A->getOption().matches(options::OPT_fno_fast_math))
  2384. CmdArgs.push_back("-ffast-math");
  2385. if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only,
  2386. options::OPT_fno_fast_math))
  2387. if (A->getOption().matches(options::OPT_ffinite_math_only))
  2388. CmdArgs.push_back("-ffinite-math-only");
  2389. // Decide whether to use verbose asm. Verbose assembly is the default on
  2390. // toolchains which have the integrated assembler on by default.
  2391. bool IsIntegratedAssemblerDefault =
  2392. getToolChain().IsIntegratedAssemblerDefault();
  2393. if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
  2394. IsIntegratedAssemblerDefault) ||
  2395. Args.hasArg(options::OPT_dA))
  2396. CmdArgs.push_back("-masm-verbose");
  2397. if (!Args.hasFlag(options::OPT_fintegrated_as, options::OPT_fno_integrated_as,
  2398. IsIntegratedAssemblerDefault))
  2399. CmdArgs.push_back("-no-integrated-as");
  2400. if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
  2401. CmdArgs.push_back("-mdebug-pass");
  2402. CmdArgs.push_back("Structure");
  2403. }
  2404. if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
  2405. CmdArgs.push_back("-mdebug-pass");
  2406. CmdArgs.push_back("Arguments");
  2407. }
  2408. // Enable -mconstructor-aliases except on darwin, where we have to
  2409. // work around a linker bug; see <rdar://problem/7651567>.
  2410. if (!getToolChain().getTriple().isOSDarwin())
  2411. CmdArgs.push_back("-mconstructor-aliases");
  2412. // Darwin's kernel doesn't support guard variables; just die if we
  2413. // try to use them.
  2414. if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
  2415. CmdArgs.push_back("-fforbid-guard-variables");
  2416. if (Args.hasArg(options::OPT_mms_bitfields)) {
  2417. CmdArgs.push_back("-mms-bitfields");
  2418. }
  2419. // This is a coarse approximation of what llvm-gcc actually does, both
  2420. // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
  2421. // complicated ways.
  2422. bool AsynchronousUnwindTables =
  2423. Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
  2424. options::OPT_fno_asynchronous_unwind_tables,
  2425. (getToolChain().IsUnwindTablesDefault() ||
  2426. getToolChain().getSanitizerArgs().needsUnwindTables()) &&
  2427. !KernelOrKext);
  2428. if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
  2429. AsynchronousUnwindTables))
  2430. CmdArgs.push_back("-munwind-tables");
  2431. getToolChain().addClangTargetOptions(Args, CmdArgs);
  2432. if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
  2433. CmdArgs.push_back("-mlimit-float-precision");
  2434. CmdArgs.push_back(A->getValue());
  2435. }
  2436. // FIXME: Handle -mtune=.
  2437. (void) Args.hasArg(options::OPT_mtune_EQ);
  2438. if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
  2439. CmdArgs.push_back("-mcode-model");
  2440. CmdArgs.push_back(A->getValue());
  2441. }
  2442. // Add the target cpu
  2443. std::string ETripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
  2444. llvm::Triple ETriple(ETripleStr);
  2445. std::string CPU = getCPUName(Args, ETriple);
  2446. if (!CPU.empty()) {
  2447. CmdArgs.push_back("-target-cpu");
  2448. CmdArgs.push_back(Args.MakeArgString(CPU));
  2449. }
  2450. if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
  2451. CmdArgs.push_back("-mfpmath");
  2452. CmdArgs.push_back(A->getValue());
  2453. }
  2454. // Add the target features
  2455. getTargetFeatures(D, ETriple, Args, CmdArgs, false);
  2456. // Add target specific flags.
  2457. switch(getToolChain().getArch()) {
  2458. default:
  2459. break;
  2460. case llvm::Triple::arm:
  2461. case llvm::Triple::armeb:
  2462. case llvm::Triple::thumb:
  2463. case llvm::Triple::thumbeb:
  2464. AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
  2465. break;
  2466. case llvm::Triple::aarch64:
  2467. case llvm::Triple::aarch64_be:
  2468. case llvm::Triple::arm64:
  2469. case llvm::Triple::arm64_be:
  2470. AddAArch64TargetArgs(Args, CmdArgs);
  2471. break;
  2472. case llvm::Triple::mips:
  2473. case llvm::Triple::mipsel:
  2474. case llvm::Triple::mips64:
  2475. case llvm::Triple::mips64el:
  2476. AddMIPSTargetArgs(Args, CmdArgs);
  2477. break;
  2478. case llvm::Triple::sparc:
  2479. AddSparcTargetArgs(Args, CmdArgs);
  2480. break;
  2481. case llvm::Triple::x86:
  2482. case llvm::Triple::x86_64:
  2483. AddX86TargetArgs(Args, CmdArgs);
  2484. break;
  2485. case llvm::Triple::hexagon:
  2486. AddHexagonTargetArgs(Args, CmdArgs);
  2487. break;
  2488. }
  2489. // Add clang-cl arguments.
  2490. if (getToolChain().getDriver().IsCLMode())
  2491. AddClangCLArgs(Args, CmdArgs);
  2492. // Pass the linker version in use.
  2493. if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
  2494. CmdArgs.push_back("-target-linker-version");
  2495. CmdArgs.push_back(A->getValue());
  2496. }
  2497. if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
  2498. CmdArgs.push_back("-momit-leaf-frame-pointer");
  2499. // Explicitly error on some things we know we don't support and can't just
  2500. // ignore.
  2501. types::ID InputType = Inputs[0].getType();
  2502. if (!Args.hasArg(options::OPT_fallow_unsupported)) {
  2503. Arg *Unsupported;
  2504. if (types::isCXX(InputType) &&
  2505. getToolChain().getTriple().isOSDarwin() &&
  2506. getToolChain().getArch() == llvm::Triple::x86) {
  2507. if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
  2508. (Unsupported = Args.getLastArg(options::OPT_mkernel)))
  2509. D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
  2510. << Unsupported->getOption().getName();
  2511. }
  2512. }
  2513. Args.AddAllArgs(CmdArgs, options::OPT_v);
  2514. Args.AddLastArg(CmdArgs, options::OPT_H);
  2515. if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
  2516. CmdArgs.push_back("-header-include-file");
  2517. CmdArgs.push_back(D.CCPrintHeadersFilename ?
  2518. D.CCPrintHeadersFilename : "-");
  2519. }
  2520. Args.AddLastArg(CmdArgs, options::OPT_P);
  2521. Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
  2522. if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
  2523. CmdArgs.push_back("-diagnostic-log-file");
  2524. CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
  2525. D.CCLogDiagnosticsFilename : "-");
  2526. }
  2527. // Use the last option from "-g" group. "-gline-tables-only" and "-gdwarf-x"
  2528. // are preserved, all other debug options are substituted with "-g".
  2529. Args.ClaimAllArgs(options::OPT_g_Group);
  2530. if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
  2531. if (A->getOption().matches(options::OPT_gline_tables_only)) {
  2532. // FIXME: we should support specifying dwarf version with
  2533. // -gline-tables-only.
  2534. CmdArgs.push_back("-gline-tables-only");
  2535. // Default is dwarf-2 for darwin.
  2536. if (getToolChain().getTriple().isOSDarwin())
  2537. CmdArgs.push_back("-gdwarf-2");
  2538. } else if (A->getOption().matches(options::OPT_gdwarf_2))
  2539. CmdArgs.push_back("-gdwarf-2");
  2540. else if (A->getOption().matches(options::OPT_gdwarf_3))
  2541. CmdArgs.push_back("-gdwarf-3");
  2542. else if (A->getOption().matches(options::OPT_gdwarf_4))
  2543. CmdArgs.push_back("-gdwarf-4");
  2544. else if (!A->getOption().matches(options::OPT_g0) &&
  2545. !A->getOption().matches(options::OPT_ggdb0)) {
  2546. // Default is dwarf-2 for darwin.
  2547. if (getToolChain().getTriple().isOSDarwin())
  2548. CmdArgs.push_back("-gdwarf-2");
  2549. else
  2550. CmdArgs.push_back("-g");
  2551. }
  2552. }
  2553. // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
  2554. Args.ClaimAllArgs(options::OPT_g_flags_Group);
  2555. if (Args.hasArg(options::OPT_gcolumn_info))
  2556. CmdArgs.push_back("-dwarf-column-info");
  2557. // FIXME: Move backend command line options to the module.
  2558. // -gsplit-dwarf should turn on -g and enable the backend dwarf
  2559. // splitting and extraction.
  2560. // FIXME: Currently only works on Linux.
  2561. if (getToolChain().getTriple().isOSLinux() &&
  2562. Args.hasArg(options::OPT_gsplit_dwarf)) {
  2563. CmdArgs.push_back("-g");
  2564. CmdArgs.push_back("-backend-option");
  2565. CmdArgs.push_back("-split-dwarf=Enable");
  2566. }
  2567. // -ggnu-pubnames turns on gnu style pubnames in the backend.
  2568. if (Args.hasArg(options::OPT_ggnu_pubnames)) {
  2569. CmdArgs.push_back("-backend-option");
  2570. CmdArgs.push_back("-generate-gnu-dwarf-pub-sections");
  2571. }
  2572. // -gdwarf-aranges turns on the emission of the aranges section in the
  2573. // backend.
  2574. if (Args.hasArg(options::OPT_gdwarf_aranges)) {
  2575. CmdArgs.push_back("-backend-option");
  2576. CmdArgs.push_back("-generate-arange-section");
  2577. }
  2578. if (Args.hasFlag(options::OPT_fdebug_types_section,
  2579. options::OPT_fno_debug_types_section, false)) {
  2580. CmdArgs.push_back("-backend-option");
  2581. CmdArgs.push_back("-generate-type-units");
  2582. }
  2583. if (Args.hasFlag(options::OPT_ffunction_sections,
  2584. options::OPT_fno_function_sections, false)) {
  2585. CmdArgs.push_back("-ffunction-sections");
  2586. }
  2587. if (Args.hasFlag(options::OPT_fdata_sections,
  2588. options::OPT_fno_data_sections, false)) {
  2589. CmdArgs.push_back("-fdata-sections");
  2590. }
  2591. Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
  2592. if (Args.hasArg(options::OPT_fprofile_instr_generate) &&
  2593. (Args.hasArg(options::OPT_fprofile_instr_use) ||
  2594. Args.hasArg(options::OPT_fprofile_instr_use_EQ)))
  2595. D.Diag(diag::err_drv_argument_not_allowed_with)
  2596. << "-fprofile-instr-generate" << "-fprofile-instr-use";
  2597. Args.AddAllArgs(CmdArgs, options::OPT_fprofile_instr_generate);
  2598. if (Arg *A = Args.getLastArg(options::OPT_fprofile_instr_use_EQ))
  2599. A->render(Args, CmdArgs);
  2600. else if (Args.hasArg(options::OPT_fprofile_instr_use))
  2601. CmdArgs.push_back("-fprofile-instr-use=pgo-data");
  2602. if (Args.hasArg(options::OPT_ftest_coverage) ||
  2603. Args.hasArg(options::OPT_coverage))
  2604. CmdArgs.push_back("-femit-coverage-notes");
  2605. if (Args.hasArg(options::OPT_fprofile_arcs) ||
  2606. Args.hasArg(options::OPT_coverage))
  2607. CmdArgs.push_back("-femit-coverage-data");
  2608. if (C.getArgs().hasArg(options::OPT_c) ||
  2609. C.getArgs().hasArg(options::OPT_S)) {
  2610. if (Output.isFilename()) {
  2611. CmdArgs.push_back("-coverage-file");
  2612. SmallString<128> CoverageFilename(Output.getFilename());
  2613. if (llvm::sys::path::is_relative(CoverageFilename.str())) {
  2614. SmallString<128> Pwd;
  2615. if (!llvm::sys::fs::current_path(Pwd)) {
  2616. llvm::sys::path::append(Pwd, CoverageFilename.str());
  2617. CoverageFilename.swap(Pwd);
  2618. }
  2619. }
  2620. CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
  2621. }
  2622. }
  2623. // Pass options for controlling the default header search paths.
  2624. if (Args.hasArg(options::OPT_nostdinc)) {
  2625. CmdArgs.push_back("-nostdsysteminc");
  2626. CmdArgs.push_back("-nobuiltininc");
  2627. } else {
  2628. if (Args.hasArg(options::OPT_nostdlibinc))
  2629. CmdArgs.push_back("-nostdsysteminc");
  2630. Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
  2631. Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
  2632. }
  2633. // Pass the path to compiler resource files.
  2634. CmdArgs.push_back("-resource-dir");
  2635. CmdArgs.push_back(D.ResourceDir.c_str());
  2636. Args.AddLastArg(CmdArgs, options::OPT_working_directory);
  2637. bool ARCMTEnabled = false;
  2638. if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
  2639. if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
  2640. options::OPT_ccc_arcmt_modify,
  2641. options::OPT_ccc_arcmt_migrate)) {
  2642. ARCMTEnabled = true;
  2643. switch (A->getOption().getID()) {
  2644. default:
  2645. llvm_unreachable("missed a case");
  2646. case options::OPT_ccc_arcmt_check:
  2647. CmdArgs.push_back("-arcmt-check");
  2648. break;
  2649. case options::OPT_ccc_arcmt_modify:
  2650. CmdArgs.push_back("-arcmt-modify");
  2651. break;
  2652. case options::OPT_ccc_arcmt_migrate:
  2653. CmdArgs.push_back("-arcmt-migrate");
  2654. CmdArgs.push_back("-mt-migrate-directory");
  2655. CmdArgs.push_back(A->getValue());
  2656. Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
  2657. Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
  2658. break;
  2659. }
  2660. }
  2661. } else {
  2662. Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
  2663. Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
  2664. Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
  2665. }
  2666. if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
  2667. if (ARCMTEnabled) {
  2668. D.Diag(diag::err_drv_argument_not_allowed_with)
  2669. << A->getAsString(Args) << "-ccc-arcmt-migrate";
  2670. }
  2671. CmdArgs.push_back("-mt-migrate-directory");
  2672. CmdArgs.push_back(A->getValue());
  2673. if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
  2674. options::OPT_objcmt_migrate_subscripting,
  2675. options::OPT_objcmt_migrate_property)) {
  2676. // None specified, means enable them all.
  2677. CmdArgs.push_back("-objcmt-migrate-literals");
  2678. CmdArgs.push_back("-objcmt-migrate-subscripting");
  2679. CmdArgs.push_back("-objcmt-migrate-property");
  2680. } else {
  2681. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
  2682. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
  2683. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
  2684. }
  2685. } else {
  2686. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
  2687. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
  2688. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
  2689. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
  2690. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
  2691. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
  2692. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
  2693. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
  2694. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
  2695. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
  2696. Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
  2697. Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
  2698. Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
  2699. Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_designated_init);
  2700. Args.AddLastArg(CmdArgs, options::OPT_objcmt_whitelist_dir_path);
  2701. }
  2702. // Add preprocessing options like -I, -D, etc. if we are using the
  2703. // preprocessor.
  2704. //
  2705. // FIXME: Support -fpreprocessed
  2706. if (types::getPreprocessedType(InputType) != types::TY_INVALID)
  2707. AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
  2708. // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
  2709. // that "The compiler can only warn and ignore the option if not recognized".
  2710. // When building with ccache, it will pass -D options to clang even on
  2711. // preprocessed inputs and configure concludes that -fPIC is not supported.
  2712. Args.ClaimAllArgs(options::OPT_D);
  2713. // Manually translate -O4 to -O3; let clang reject others.
  2714. if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
  2715. if (A->getOption().matches(options::OPT_O4)) {
  2716. CmdArgs.push_back("-O3");
  2717. D.Diag(diag::warn_O4_is_O3);
  2718. } else {
  2719. A->render(Args, CmdArgs);
  2720. }
  2721. }
  2722. // Don't warn about unused -flto. This can happen when we're preprocessing or
  2723. // precompiling.
  2724. Args.ClaimAllArgs(options::OPT_flto);
  2725. Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
  2726. if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
  2727. CmdArgs.push_back("-pedantic");
  2728. Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
  2729. Args.AddLastArg(CmdArgs, options::OPT_w);
  2730. // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
  2731. // (-ansi is equivalent to -std=c89 or -std=c++98).
  2732. //
  2733. // If a std is supplied, only add -trigraphs if it follows the
  2734. // option.
  2735. if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
  2736. if (Std->getOption().matches(options::OPT_ansi))
  2737. if (types::isCXX(InputType))
  2738. CmdArgs.push_back("-std=c++98");
  2739. else
  2740. CmdArgs.push_back("-std=c89");
  2741. else
  2742. Std->render(Args, CmdArgs);
  2743. if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
  2744. options::OPT_trigraphs))
  2745. if (A != Std)
  2746. A->render(Args, CmdArgs);
  2747. } else {
  2748. // Honor -std-default.
  2749. //
  2750. // FIXME: Clang doesn't correctly handle -std= when the input language
  2751. // doesn't match. For the time being just ignore this for C++ inputs;
  2752. // eventually we want to do all the standard defaulting here instead of
  2753. // splitting it between the driver and clang -cc1.
  2754. if (!types::isCXX(InputType))
  2755. Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
  2756. "-std=", /*Joined=*/true);
  2757. else if (IsWindowsMSVC)
  2758. CmdArgs.push_back("-std=c++11");
  2759. Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
  2760. }
  2761. // GCC's behavior for -Wwrite-strings is a bit strange:
  2762. // * In C, this "warning flag" changes the types of string literals from
  2763. // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
  2764. // for the discarded qualifier.
  2765. // * In C++, this is just a normal warning flag.
  2766. //
  2767. // Implementing this warning correctly in C is hard, so we follow GCC's
  2768. // behavior for now. FIXME: Directly diagnose uses of a string literal as
  2769. // a non-const char* in C, rather than using this crude hack.
  2770. if (!types::isCXX(InputType)) {
  2771. // FIXME: This should behave just like a warning flag, and thus should also
  2772. // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on.
  2773. Arg *WriteStrings =
  2774. Args.getLastArg(options::OPT_Wwrite_strings,
  2775. options::OPT_Wno_write_strings, options::OPT_w);
  2776. if (WriteStrings &&
  2777. WriteStrings->getOption().matches(options::OPT_Wwrite_strings))
  2778. CmdArgs.push_back("-fconst-strings");
  2779. }
  2780. // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
  2781. // during C++ compilation, which it is by default. GCC keeps this define even
  2782. // in the presence of '-w', match this behavior bug-for-bug.
  2783. if (types::isCXX(InputType) &&
  2784. Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
  2785. true)) {
  2786. CmdArgs.push_back("-fdeprecated-macro");
  2787. }
  2788. // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
  2789. if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
  2790. if (Asm->getOption().matches(options::OPT_fasm))
  2791. CmdArgs.push_back("-fgnu-keywords");
  2792. else
  2793. CmdArgs.push_back("-fno-gnu-keywords");
  2794. }
  2795. if (ShouldDisableDwarfDirectory(Args, getToolChain()))
  2796. CmdArgs.push_back("-fno-dwarf-directory-asm");
  2797. if (ShouldDisableAutolink(Args, getToolChain()))
  2798. CmdArgs.push_back("-fno-autolink");
  2799. // Add in -fdebug-compilation-dir if necessary.
  2800. addDebugCompDirArg(Args, CmdArgs);
  2801. if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
  2802. options::OPT_ftemplate_depth_EQ)) {
  2803. CmdArgs.push_back("-ftemplate-depth");
  2804. CmdArgs.push_back(A->getValue());
  2805. }
  2806. if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
  2807. CmdArgs.push_back("-foperator-arrow-depth");
  2808. CmdArgs.push_back(A->getValue());
  2809. }
  2810. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
  2811. CmdArgs.push_back("-fconstexpr-depth");
  2812. CmdArgs.push_back(A->getValue());
  2813. }
  2814. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
  2815. CmdArgs.push_back("-fconstexpr-steps");
  2816. CmdArgs.push_back(A->getValue());
  2817. }
  2818. if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
  2819. CmdArgs.push_back("-fbracket-depth");
  2820. CmdArgs.push_back(A->getValue());
  2821. }
  2822. if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
  2823. options::OPT_Wlarge_by_value_copy_def)) {
  2824. if (A->getNumValues()) {
  2825. StringRef bytes = A->getValue();
  2826. CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
  2827. } else
  2828. CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
  2829. }
  2830. if (Args.hasArg(options::OPT_relocatable_pch))
  2831. CmdArgs.push_back("-relocatable-pch");
  2832. if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
  2833. CmdArgs.push_back("-fconstant-string-class");
  2834. CmdArgs.push_back(A->getValue());
  2835. }
  2836. if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
  2837. CmdArgs.push_back("-ftabstop");
  2838. CmdArgs.push_back(A->getValue());
  2839. }
  2840. CmdArgs.push_back("-ferror-limit");
  2841. if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
  2842. CmdArgs.push_back(A->getValue());
  2843. else
  2844. CmdArgs.push_back("19");
  2845. if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
  2846. CmdArgs.push_back("-fmacro-backtrace-limit");
  2847. CmdArgs.push_back(A->getValue());
  2848. }
  2849. if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
  2850. CmdArgs.push_back("-ftemplate-backtrace-limit");
  2851. CmdArgs.push_back(A->getValue());
  2852. }
  2853. if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
  2854. CmdArgs.push_back("-fconstexpr-backtrace-limit");
  2855. CmdArgs.push_back(A->getValue());
  2856. }
  2857. // Pass -fmessage-length=.
  2858. CmdArgs.push_back("-fmessage-length");
  2859. if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
  2860. CmdArgs.push_back(A->getValue());
  2861. } else {
  2862. // If -fmessage-length=N was not specified, determine whether this is a
  2863. // terminal and, if so, implicitly define -fmessage-length appropriately.
  2864. unsigned N = llvm::sys::Process::StandardErrColumns();
  2865. CmdArgs.push_back(Args.MakeArgString(Twine(N)));
  2866. }
  2867. // -fvisibility= and -fvisibility-ms-compat are of a piece.
  2868. if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
  2869. options::OPT_fvisibility_ms_compat)) {
  2870. if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
  2871. CmdArgs.push_back("-fvisibility");
  2872. CmdArgs.push_back(A->getValue());
  2873. } else {
  2874. assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
  2875. CmdArgs.push_back("-fvisibility");
  2876. CmdArgs.push_back("hidden");
  2877. CmdArgs.push_back("-ftype-visibility");
  2878. CmdArgs.push_back("default");
  2879. }
  2880. }
  2881. Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
  2882. Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
  2883. // -fhosted is default.
  2884. if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
  2885. KernelOrKext)
  2886. CmdArgs.push_back("-ffreestanding");
  2887. // Forward -f (flag) options which we can pass directly.
  2888. Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
  2889. Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
  2890. Args.AddLastArg(CmdArgs, options::OPT_fstandalone_debug);
  2891. Args.AddLastArg(CmdArgs, options::OPT_fno_standalone_debug);
  2892. Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
  2893. // AltiVec language extensions aren't relevant for assembling.
  2894. if (!isa<PreprocessJobAction>(JA) ||
  2895. Output.getType() != types::TY_PP_Asm)
  2896. Args.AddLastArg(CmdArgs, options::OPT_faltivec);
  2897. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
  2898. Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
  2899. const SanitizerArgs &Sanitize = getToolChain().getSanitizerArgs();
  2900. Sanitize.addArgs(Args, CmdArgs);
  2901. if (!Args.hasFlag(options::OPT_fsanitize_recover,
  2902. options::OPT_fno_sanitize_recover,
  2903. true))
  2904. CmdArgs.push_back("-fno-sanitize-recover");
  2905. if (Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
  2906. options::OPT_fno_sanitize_undefined_trap_on_error, false))
  2907. CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
  2908. // Report an error for -faltivec on anything other than PowerPC.
  2909. if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
  2910. if (!(getToolChain().getArch() == llvm::Triple::ppc ||
  2911. getToolChain().getArch() == llvm::Triple::ppc64 ||
  2912. getToolChain().getArch() == llvm::Triple::ppc64le))
  2913. D.Diag(diag::err_drv_argument_only_allowed_with)
  2914. << A->getAsString(Args) << "ppc/ppc64/ppc64le";
  2915. if (getToolChain().SupportsProfiling())
  2916. Args.AddLastArg(CmdArgs, options::OPT_pg);
  2917. // -flax-vector-conversions is default.
  2918. if (!Args.hasFlag(options::OPT_flax_vector_conversions,
  2919. options::OPT_fno_lax_vector_conversions))
  2920. CmdArgs.push_back("-fno-lax-vector-conversions");
  2921. if (Args.getLastArg(options::OPT_fapple_kext))
  2922. CmdArgs.push_back("-fapple-kext");
  2923. if (Args.hasFlag(options::OPT_frewrite_includes,
  2924. options::OPT_fno_rewrite_includes, false))
  2925. CmdArgs.push_back("-frewrite-includes");
  2926. Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
  2927. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
  2928. Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
  2929. Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
  2930. Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
  2931. if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
  2932. CmdArgs.push_back("-ftrapv-handler");
  2933. CmdArgs.push_back(A->getValue());
  2934. }
  2935. Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
  2936. // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
  2937. // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
  2938. if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
  2939. options::OPT_fno_wrapv)) {
  2940. if (A->getOption().matches(options::OPT_fwrapv))
  2941. CmdArgs.push_back("-fwrapv");
  2942. } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
  2943. options::OPT_fno_strict_overflow)) {
  2944. if (A->getOption().matches(options::OPT_fno_strict_overflow))
  2945. CmdArgs.push_back("-fwrapv");
  2946. }
  2947. if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
  2948. options::OPT_fno_reroll_loops))
  2949. if (A->getOption().matches(options::OPT_freroll_loops))
  2950. CmdArgs.push_back("-freroll-loops");
  2951. Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
  2952. Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
  2953. options::OPT_fno_unroll_loops);
  2954. Args.AddLastArg(CmdArgs, options::OPT_pthread);
  2955. // -stack-protector=0 is default.
  2956. unsigned StackProtectorLevel = 0;
  2957. if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
  2958. options::OPT_fstack_protector_all,
  2959. options::OPT_fstack_protector_strong,
  2960. options::OPT_fstack_protector)) {
  2961. if (A->getOption().matches(options::OPT_fstack_protector)) {
  2962. StackProtectorLevel = std::max<unsigned>(LangOptions::SSPOn,
  2963. getToolChain().GetDefaultStackProtectorLevel(KernelOrKext));
  2964. } else if (A->getOption().matches(options::OPT_fstack_protector_strong))
  2965. StackProtectorLevel = LangOptions::SSPStrong;
  2966. else if (A->getOption().matches(options::OPT_fstack_protector_all))
  2967. StackProtectorLevel = LangOptions::SSPReq;
  2968. } else {
  2969. StackProtectorLevel =
  2970. getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
  2971. }
  2972. if (StackProtectorLevel) {
  2973. CmdArgs.push_back("-stack-protector");
  2974. CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
  2975. }
  2976. // --param ssp-buffer-size=
  2977. for (arg_iterator it = Args.filtered_begin(options::OPT__param),
  2978. ie = Args.filtered_end(); it != ie; ++it) {
  2979. StringRef Str((*it)->getValue());
  2980. if (Str.startswith("ssp-buffer-size=")) {
  2981. if (StackProtectorLevel) {
  2982. CmdArgs.push_back("-stack-protector-buffer-size");
  2983. // FIXME: Verify the argument is a valid integer.
  2984. CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
  2985. }
  2986. (*it)->claim();
  2987. }
  2988. }
  2989. // Translate -mstackrealign
  2990. if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
  2991. false)) {
  2992. CmdArgs.push_back("-backend-option");
  2993. CmdArgs.push_back("-force-align-stack");
  2994. }
  2995. if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
  2996. false)) {
  2997. CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
  2998. }
  2999. if (Args.hasArg(options::OPT_mstack_alignment)) {
  3000. StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
  3001. CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
  3002. }
  3003. // -mkernel implies -mstrict-align; don't add the redundant option.
  3004. if (!KernelOrKext) {
  3005. if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
  3006. options::OPT_munaligned_access)) {
  3007. if (A->getOption().matches(options::OPT_mno_unaligned_access)) {
  3008. CmdArgs.push_back("-backend-option");
  3009. if (getToolChain().getTriple().getArch() == llvm::Triple::aarch64 ||
  3010. getToolChain().getTriple().getArch() == llvm::Triple::aarch64_be ||
  3011. getToolChain().getTriple().getArch() == llvm::Triple::arm64 ||
  3012. getToolChain().getTriple().getArch() == llvm::Triple::arm64_be)
  3013. CmdArgs.push_back("-aarch64-strict-align");
  3014. else
  3015. CmdArgs.push_back("-arm-strict-align");
  3016. } else {
  3017. CmdArgs.push_back("-backend-option");
  3018. if (getToolChain().getTriple().getArch() == llvm::Triple::aarch64 ||
  3019. getToolChain().getTriple().getArch() == llvm::Triple::aarch64_be ||
  3020. getToolChain().getTriple().getArch() == llvm::Triple::arm64 ||
  3021. getToolChain().getTriple().getArch() == llvm::Triple::arm64_be)
  3022. CmdArgs.push_back("-aarch64-no-strict-align");
  3023. else
  3024. CmdArgs.push_back("-arm-no-strict-align");
  3025. }
  3026. }
  3027. }
  3028. if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
  3029. options::OPT_mno_restrict_it)) {
  3030. if (A->getOption().matches(options::OPT_mrestrict_it)) {
  3031. CmdArgs.push_back("-backend-option");
  3032. CmdArgs.push_back("-arm-restrict-it");
  3033. } else {
  3034. CmdArgs.push_back("-backend-option");
  3035. CmdArgs.push_back("-arm-no-restrict-it");
  3036. }
  3037. } else if (TT.isOSWindows() && (TT.getArch() == llvm::Triple::arm ||
  3038. TT.getArch() == llvm::Triple::thumb)) {
  3039. // Windows on ARM expects restricted IT blocks
  3040. CmdArgs.push_back("-backend-option");
  3041. CmdArgs.push_back("-arm-restrict-it");
  3042. }
  3043. // Forward -f options with positive and negative forms; we translate
  3044. // these by hand.
  3045. if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ)) {
  3046. StringRef fname = A->getValue();
  3047. if (!llvm::sys::fs::exists(fname))
  3048. D.Diag(diag::err_drv_no_such_file) << fname;
  3049. else
  3050. A->render(Args, CmdArgs);
  3051. }
  3052. if (Arg *A = Args.getLastArg(options::OPT_Rpass_EQ))
  3053. A->render(Args, CmdArgs);
  3054. if (Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ))
  3055. A->render(Args, CmdArgs);
  3056. if (Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ))
  3057. A->render(Args, CmdArgs);
  3058. if (Args.hasArg(options::OPT_mkernel)) {
  3059. if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
  3060. CmdArgs.push_back("-fapple-kext");
  3061. if (!Args.hasArg(options::OPT_fbuiltin))
  3062. CmdArgs.push_back("-fno-builtin");
  3063. Args.ClaimAllArgs(options::OPT_fno_builtin);
  3064. }
  3065. // -fbuiltin is default.
  3066. else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
  3067. CmdArgs.push_back("-fno-builtin");
  3068. if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
  3069. options::OPT_fno_assume_sane_operator_new))
  3070. CmdArgs.push_back("-fno-assume-sane-operator-new");
  3071. // -fblocks=0 is default.
  3072. if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
  3073. getToolChain().IsBlocksDefault()) ||
  3074. (Args.hasArg(options::OPT_fgnu_runtime) &&
  3075. Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
  3076. !Args.hasArg(options::OPT_fno_blocks))) {
  3077. CmdArgs.push_back("-fblocks");
  3078. if (!Args.hasArg(options::OPT_fgnu_runtime) &&
  3079. !getToolChain().hasBlocksRuntime())
  3080. CmdArgs.push_back("-fblocks-runtime-optional");
  3081. }
  3082. // -fmodules enables modules (off by default). However, for C++/Objective-C++,
  3083. // users must also pass -fcxx-modules. The latter flag will disappear once the
  3084. // modules implementation is solid for C++/Objective-C++ programs as well.
  3085. bool HaveModules = false;
  3086. if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
  3087. bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
  3088. options::OPT_fno_cxx_modules,
  3089. false);
  3090. if (AllowedInCXX || !types::isCXX(InputType)) {
  3091. CmdArgs.push_back("-fmodules");
  3092. HaveModules = true;
  3093. }
  3094. }
  3095. // -fmodule-maps enables module map processing (off by default) for header
  3096. // checking. It is implied by -fmodules.
  3097. if (Args.hasFlag(options::OPT_fmodule_maps, options::OPT_fno_module_maps,
  3098. false)) {
  3099. CmdArgs.push_back("-fmodule-maps");
  3100. }
  3101. // -fmodules-decluse checks that modules used are declared so (off by
  3102. // default).
  3103. if (Args.hasFlag(options::OPT_fmodules_decluse,
  3104. options::OPT_fno_modules_decluse,
  3105. false)) {
  3106. CmdArgs.push_back("-fmodules-decluse");
  3107. }
  3108. // -fmodules-strict-decluse is like -fmodule-decluse, but also checks that
  3109. // all #included headers are part of modules.
  3110. if (Args.hasFlag(options::OPT_fmodules_strict_decluse,
  3111. options::OPT_fno_modules_strict_decluse,
  3112. false)) {
  3113. CmdArgs.push_back("-fmodules-strict-decluse");
  3114. }
  3115. // -fmodule-name specifies the module that is currently being built (or
  3116. // used for header checking by -fmodule-maps).
  3117. if (Arg *A = Args.getLastArg(options::OPT_fmodule_name)) {
  3118. A->claim();
  3119. A->render(Args, CmdArgs);
  3120. }
  3121. // -fmodule-map-file can be used to specify a file containing module
  3122. // definitions.
  3123. if (Arg *A = Args.getLastArg(options::OPT_fmodule_map_file)) {
  3124. A->claim();
  3125. A->render(Args, CmdArgs);
  3126. }
  3127. // If a module path was provided, pass it along. Otherwise, use a temporary
  3128. // directory.
  3129. if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) {
  3130. A->claim();
  3131. if (HaveModules) {
  3132. A->render(Args, CmdArgs);
  3133. }
  3134. } else if (HaveModules) {
  3135. SmallString<128> DefaultModuleCache;
  3136. llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
  3137. DefaultModuleCache);
  3138. llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
  3139. llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
  3140. const char Arg[] = "-fmodules-cache-path=";
  3141. DefaultModuleCache.insert(DefaultModuleCache.begin(),
  3142. Arg, Arg + strlen(Arg));
  3143. CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
  3144. }
  3145. if (Arg *A = Args.getLastArg(options::OPT_fmodules_user_build_path)) {
  3146. A->claim();
  3147. if (HaveModules) {
  3148. A->render(Args, CmdArgs);
  3149. }
  3150. }
  3151. // Pass through all -fmodules-ignore-macro arguments.
  3152. Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
  3153. Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
  3154. Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
  3155. Args.AddLastArg(CmdArgs, options::OPT_fbuild_session_timestamp);
  3156. if (Args.getLastArg(options::OPT_fmodules_validate_once_per_build_session)) {
  3157. if (!Args.getLastArg(options::OPT_fbuild_session_timestamp))
  3158. D.Diag(diag::err_drv_modules_validate_once_requires_timestamp);
  3159. Args.AddLastArg(CmdArgs,
  3160. options::OPT_fmodules_validate_once_per_build_session);
  3161. }
  3162. Args.AddLastArg(CmdArgs, options::OPT_fmodules_validate_system_headers);
  3163. // -faccess-control is default.
  3164. if (Args.hasFlag(options::OPT_fno_access_control,
  3165. options::OPT_faccess_control,
  3166. false))
  3167. CmdArgs.push_back("-fno-access-control");
  3168. // -felide-constructors is the default.
  3169. if (Args.hasFlag(options::OPT_fno_elide_constructors,
  3170. options::OPT_felide_constructors,
  3171. false))
  3172. CmdArgs.push_back("-fno-elide-constructors");
  3173. // -frtti is default.
  3174. if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
  3175. KernelOrKext) {
  3176. CmdArgs.push_back("-fno-rtti");
  3177. // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
  3178. if (Sanitize.sanitizesVptr()) {
  3179. std::string NoRttiArg =
  3180. Args.getLastArg(options::OPT_mkernel,
  3181. options::OPT_fapple_kext,
  3182. options::OPT_fno_rtti)->getAsString(Args);
  3183. D.Diag(diag::err_drv_argument_not_allowed_with)
  3184. << "-fsanitize=vptr" << NoRttiArg;
  3185. }
  3186. }
  3187. // -fshort-enums=0 is default for all architectures except Hexagon.
  3188. if (Args.hasFlag(options::OPT_fshort_enums,
  3189. options::OPT_fno_short_enums,
  3190. getToolChain().getArch() ==
  3191. llvm::Triple::hexagon))
  3192. CmdArgs.push_back("-fshort-enums");
  3193. // -fsigned-char is default.
  3194. if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
  3195. isSignedCharDefault(getToolChain().getTriple())))
  3196. CmdArgs.push_back("-fno-signed-char");
  3197. // -fthreadsafe-static is default.
  3198. if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
  3199. options::OPT_fno_threadsafe_statics))
  3200. CmdArgs.push_back("-fno-threadsafe-statics");
  3201. // -fuse-cxa-atexit is default.
  3202. if (!Args.hasFlag(options::OPT_fuse_cxa_atexit,
  3203. options::OPT_fno_use_cxa_atexit,
  3204. !IsWindowsCygnus && !IsWindowsGNU &&
  3205. getToolChain().getArch() != llvm::Triple::hexagon &&
  3206. getToolChain().getArch() != llvm::Triple::xcore) ||
  3207. KernelOrKext)
  3208. CmdArgs.push_back("-fno-use-cxa-atexit");
  3209. // -fms-extensions=0 is default.
  3210. if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
  3211. IsWindowsMSVC))
  3212. CmdArgs.push_back("-fms-extensions");
  3213. // -fms-compatibility=0 is default.
  3214. if (Args.hasFlag(options::OPT_fms_compatibility,
  3215. options::OPT_fno_ms_compatibility,
  3216. (IsWindowsMSVC && Args.hasFlag(options::OPT_fms_extensions,
  3217. options::OPT_fno_ms_extensions,
  3218. true))))
  3219. CmdArgs.push_back("-fms-compatibility");
  3220. // -fmsc-version=1700 is default.
  3221. if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
  3222. IsWindowsMSVC) || Args.hasArg(options::OPT_fmsc_version)) {
  3223. StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
  3224. if (msc_ver.empty())
  3225. CmdArgs.push_back("-fmsc-version=1700");
  3226. else
  3227. CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
  3228. }
  3229. // -fno-borland-extensions is default.
  3230. if (Args.hasFlag(options::OPT_fborland_extensions,
  3231. options::OPT_fno_borland_extensions, false))
  3232. CmdArgs.push_back("-fborland-extensions");
  3233. // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
  3234. // needs it.
  3235. if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
  3236. options::OPT_fno_delayed_template_parsing, IsWindowsMSVC))
  3237. CmdArgs.push_back("-fdelayed-template-parsing");
  3238. // -fgnu-keywords default varies depending on language; only pass if
  3239. // specified.
  3240. if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
  3241. options::OPT_fno_gnu_keywords))
  3242. A->render(Args, CmdArgs);
  3243. if (Args.hasFlag(options::OPT_fgnu89_inline,
  3244. options::OPT_fno_gnu89_inline,
  3245. false))
  3246. CmdArgs.push_back("-fgnu89-inline");
  3247. if (Args.hasArg(options::OPT_fno_inline))
  3248. CmdArgs.push_back("-fno-inline");
  3249. if (Args.hasArg(options::OPT_fno_inline_functions))
  3250. CmdArgs.push_back("-fno-inline-functions");
  3251. ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
  3252. // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
  3253. // legacy is the default. Except for deployment taget of 10.5,
  3254. // next runtime is always legacy dispatch and -fno-objc-legacy-dispatch
  3255. // gets ignored silently.
  3256. if (objcRuntime.isNonFragile()) {
  3257. if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
  3258. options::OPT_fno_objc_legacy_dispatch,
  3259. objcRuntime.isLegacyDispatchDefaultForArch(
  3260. getToolChain().getArch()))) {
  3261. if (getToolChain().UseObjCMixedDispatch())
  3262. CmdArgs.push_back("-fobjc-dispatch-method=mixed");
  3263. else
  3264. CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
  3265. }
  3266. }
  3267. // When ObjectiveC legacy runtime is in effect on MacOSX,
  3268. // turn on the option to do Array/Dictionary subscripting
  3269. // by default.
  3270. if (getToolChain().getTriple().getArch() == llvm::Triple::x86 &&
  3271. getToolChain().getTriple().isMacOSX() &&
  3272. !getToolChain().getTriple().isMacOSXVersionLT(10, 7) &&
  3273. objcRuntime.getKind() == ObjCRuntime::FragileMacOSX &&
  3274. objcRuntime.isNeXTFamily())
  3275. CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
  3276. // -fencode-extended-block-signature=1 is default.
  3277. if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
  3278. CmdArgs.push_back("-fencode-extended-block-signature");
  3279. }
  3280. // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
  3281. // NOTE: This logic is duplicated in ToolChains.cpp.
  3282. bool ARC = isObjCAutoRefCount(Args);
  3283. if (ARC) {
  3284. getToolChain().CheckObjCARC();
  3285. CmdArgs.push_back("-fobjc-arc");
  3286. // FIXME: It seems like this entire block, and several around it should be
  3287. // wrapped in isObjC, but for now we just use it here as this is where it
  3288. // was being used previously.
  3289. if (types::isCXX(InputType) && types::isObjC(InputType)) {
  3290. if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
  3291. CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
  3292. else
  3293. CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
  3294. }
  3295. // Allow the user to enable full exceptions code emission.
  3296. // We define off for Objective-CC, on for Objective-C++.
  3297. if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
  3298. options::OPT_fno_objc_arc_exceptions,
  3299. /*default*/ types::isCXX(InputType)))
  3300. CmdArgs.push_back("-fobjc-arc-exceptions");
  3301. }
  3302. // -fobjc-infer-related-result-type is the default, except in the Objective-C
  3303. // rewriter.
  3304. if (rewriteKind != RK_None)
  3305. CmdArgs.push_back("-fno-objc-infer-related-result-type");
  3306. // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
  3307. // takes precedence.
  3308. const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
  3309. if (!GCArg)
  3310. GCArg = Args.getLastArg(options::OPT_fobjc_gc);
  3311. if (GCArg) {
  3312. if (ARC) {
  3313. D.Diag(diag::err_drv_objc_gc_arr)
  3314. << GCArg->getAsString(Args);
  3315. } else if (getToolChain().SupportsObjCGC()) {
  3316. GCArg->render(Args, CmdArgs);
  3317. } else {
  3318. // FIXME: We should move this to a hard error.
  3319. D.Diag(diag::warn_drv_objc_gc_unsupported)
  3320. << GCArg->getAsString(Args);
  3321. }
  3322. }
  3323. // Add exception args.
  3324. addExceptionArgs(Args, InputType, getToolChain().getTriple(),
  3325. KernelOrKext, objcRuntime, CmdArgs);
  3326. if (getToolChain().UseSjLjExceptions())
  3327. CmdArgs.push_back("-fsjlj-exceptions");
  3328. // C++ "sane" operator new.
  3329. if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
  3330. options::OPT_fno_assume_sane_operator_new))
  3331. CmdArgs.push_back("-fno-assume-sane-operator-new");
  3332. // -fconstant-cfstrings is default, and may be subject to argument translation
  3333. // on Darwin.
  3334. if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
  3335. options::OPT_fno_constant_cfstrings) ||
  3336. !Args.hasFlag(options::OPT_mconstant_cfstrings,
  3337. options::OPT_mno_constant_cfstrings))
  3338. CmdArgs.push_back("-fno-constant-cfstrings");
  3339. // -fshort-wchar default varies depending on platform; only
  3340. // pass if specified.
  3341. if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar,
  3342. options::OPT_fno_short_wchar))
  3343. A->render(Args, CmdArgs);
  3344. // -fno-pascal-strings is default, only pass non-default.
  3345. if (Args.hasFlag(options::OPT_fpascal_strings,
  3346. options::OPT_fno_pascal_strings,
  3347. false))
  3348. CmdArgs.push_back("-fpascal-strings");
  3349. // Honor -fpack-struct= and -fpack-struct, if given. Note that
  3350. // -fno-pack-struct doesn't apply to -fpack-struct=.
  3351. if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
  3352. std::string PackStructStr = "-fpack-struct=";
  3353. PackStructStr += A->getValue();
  3354. CmdArgs.push_back(Args.MakeArgString(PackStructStr));
  3355. } else if (Args.hasFlag(options::OPT_fpack_struct,
  3356. options::OPT_fno_pack_struct, false)) {
  3357. CmdArgs.push_back("-fpack-struct=1");
  3358. }
  3359. if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) {
  3360. if (!Args.hasArg(options::OPT_fcommon))
  3361. CmdArgs.push_back("-fno-common");
  3362. Args.ClaimAllArgs(options::OPT_fno_common);
  3363. }
  3364. // -fcommon is default, only pass non-default.
  3365. else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
  3366. CmdArgs.push_back("-fno-common");
  3367. // -fsigned-bitfields is default, and clang doesn't yet support
  3368. // -funsigned-bitfields.
  3369. if (!Args.hasFlag(options::OPT_fsigned_bitfields,
  3370. options::OPT_funsigned_bitfields))
  3371. D.Diag(diag::warn_drv_clang_unsupported)
  3372. << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
  3373. // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
  3374. if (!Args.hasFlag(options::OPT_ffor_scope,
  3375. options::OPT_fno_for_scope))
  3376. D.Diag(diag::err_drv_clang_unsupported)
  3377. << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
  3378. // -fcaret-diagnostics is default.
  3379. if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
  3380. options::OPT_fno_caret_diagnostics, true))
  3381. CmdArgs.push_back("-fno-caret-diagnostics");
  3382. // -fdiagnostics-fixit-info is default, only pass non-default.
  3383. if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
  3384. options::OPT_fno_diagnostics_fixit_info))
  3385. CmdArgs.push_back("-fno-diagnostics-fixit-info");
  3386. // Enable -fdiagnostics-show-option by default.
  3387. if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
  3388. options::OPT_fno_diagnostics_show_option))
  3389. CmdArgs.push_back("-fdiagnostics-show-option");
  3390. if (const Arg *A =
  3391. Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
  3392. CmdArgs.push_back("-fdiagnostics-show-category");
  3393. CmdArgs.push_back(A->getValue());
  3394. }
  3395. if (const Arg *A =
  3396. Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
  3397. CmdArgs.push_back("-fdiagnostics-format");
  3398. CmdArgs.push_back(A->getValue());
  3399. }
  3400. if (Arg *A = Args.getLastArg(
  3401. options::OPT_fdiagnostics_show_note_include_stack,
  3402. options::OPT_fno_diagnostics_show_note_include_stack)) {
  3403. if (A->getOption().matches(
  3404. options::OPT_fdiagnostics_show_note_include_stack))
  3405. CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
  3406. else
  3407. CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
  3408. }
  3409. // Color diagnostics are the default, unless the terminal doesn't support
  3410. // them.
  3411. // Support both clang's -f[no-]color-diagnostics and gcc's
  3412. // -f[no-]diagnostics-colors[=never|always|auto].
  3413. enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
  3414. for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
  3415. it != ie; ++it) {
  3416. const Option &O = (*it)->getOption();
  3417. if (!O.matches(options::OPT_fcolor_diagnostics) &&
  3418. !O.matches(options::OPT_fdiagnostics_color) &&
  3419. !O.matches(options::OPT_fno_color_diagnostics) &&
  3420. !O.matches(options::OPT_fno_diagnostics_color) &&
  3421. !O.matches(options::OPT_fdiagnostics_color_EQ))
  3422. continue;
  3423. (*it)->claim();
  3424. if (O.matches(options::OPT_fcolor_diagnostics) ||
  3425. O.matches(options::OPT_fdiagnostics_color)) {
  3426. ShowColors = Colors_On;
  3427. } else if (O.matches(options::OPT_fno_color_diagnostics) ||
  3428. O.matches(options::OPT_fno_diagnostics_color)) {
  3429. ShowColors = Colors_Off;
  3430. } else {
  3431. assert(O.matches(options::OPT_fdiagnostics_color_EQ));
  3432. StringRef value((*it)->getValue());
  3433. if (value == "always")
  3434. ShowColors = Colors_On;
  3435. else if (value == "never")
  3436. ShowColors = Colors_Off;
  3437. else if (value == "auto")
  3438. ShowColors = Colors_Auto;
  3439. else
  3440. getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
  3441. << ("-fdiagnostics-color=" + value).str();
  3442. }
  3443. }
  3444. if (ShowColors == Colors_On ||
  3445. (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
  3446. CmdArgs.push_back("-fcolor-diagnostics");
  3447. if (Args.hasArg(options::OPT_fansi_escape_codes))
  3448. CmdArgs.push_back("-fansi-escape-codes");
  3449. if (!Args.hasFlag(options::OPT_fshow_source_location,
  3450. options::OPT_fno_show_source_location))
  3451. CmdArgs.push_back("-fno-show-source-location");
  3452. if (!Args.hasFlag(options::OPT_fshow_column,
  3453. options::OPT_fno_show_column,
  3454. true))
  3455. CmdArgs.push_back("-fno-show-column");
  3456. if (!Args.hasFlag(options::OPT_fspell_checking,
  3457. options::OPT_fno_spell_checking))
  3458. CmdArgs.push_back("-fno-spell-checking");
  3459. // -fno-asm-blocks is default.
  3460. if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
  3461. false))
  3462. CmdArgs.push_back("-fasm-blocks");
  3463. // Enable vectorization per default according to the optimization level
  3464. // selected. For optimization levels that want vectorization we use the alias
  3465. // option to simplify the hasFlag logic.
  3466. bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);
  3467. OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group :
  3468. options::OPT_fvectorize;
  3469. if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
  3470. options::OPT_fno_vectorize, EnableVec))
  3471. CmdArgs.push_back("-vectorize-loops");
  3472. // -fslp-vectorize is enabled based on the optimization level selected.
  3473. bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);
  3474. OptSpecifier SLPVectAliasOption = EnableSLPVec ? options::OPT_O_Group :
  3475. options::OPT_fslp_vectorize;
  3476. if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,
  3477. options::OPT_fno_slp_vectorize, EnableSLPVec))
  3478. CmdArgs.push_back("-vectorize-slp");
  3479. // -fno-slp-vectorize-aggressive is default.
  3480. if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive,
  3481. options::OPT_fno_slp_vectorize_aggressive, false))
  3482. CmdArgs.push_back("-vectorize-slp-aggressive");
  3483. if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
  3484. A->render(Args, CmdArgs);
  3485. // -fdollars-in-identifiers default varies depending on platform and
  3486. // language; only pass if specified.
  3487. if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
  3488. options::OPT_fno_dollars_in_identifiers)) {
  3489. if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
  3490. CmdArgs.push_back("-fdollars-in-identifiers");
  3491. else
  3492. CmdArgs.push_back("-fno-dollars-in-identifiers");
  3493. }
  3494. // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
  3495. // practical purposes.
  3496. if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
  3497. options::OPT_fno_unit_at_a_time)) {
  3498. if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
  3499. D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
  3500. }
  3501. if (Args.hasFlag(options::OPT_fapple_pragma_pack,
  3502. options::OPT_fno_apple_pragma_pack, false))
  3503. CmdArgs.push_back("-fapple-pragma-pack");
  3504. // le32-specific flags:
  3505. // -fno-math-builtin: clang should not convert math builtins to intrinsics
  3506. // by default.
  3507. if (getToolChain().getArch() == llvm::Triple::le32) {
  3508. CmdArgs.push_back("-fno-math-builtin");
  3509. }
  3510. // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
  3511. //
  3512. // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
  3513. #if 0
  3514. if (getToolChain().getTriple().isOSDarwin() &&
  3515. (getToolChain().getArch() == llvm::Triple::arm ||
  3516. getToolChain().getArch() == llvm::Triple::thumb)) {
  3517. if (!Args.hasArg(options::OPT_fbuiltin_strcat))
  3518. CmdArgs.push_back("-fno-builtin-strcat");
  3519. if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
  3520. CmdArgs.push_back("-fno-builtin-strcpy");
  3521. }
  3522. #endif
  3523. // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
  3524. if (Arg *A = Args.getLastArg(options::OPT_traditional,
  3525. options::OPT_traditional_cpp)) {
  3526. if (isa<PreprocessJobAction>(JA))
  3527. CmdArgs.push_back("-traditional-cpp");
  3528. else
  3529. D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
  3530. }
  3531. Args.AddLastArg(CmdArgs, options::OPT_dM);
  3532. Args.AddLastArg(CmdArgs, options::OPT_dD);
  3533. // Handle serialized diagnostics.
  3534. if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
  3535. CmdArgs.push_back("-serialize-diagnostic-file");
  3536. CmdArgs.push_back(Args.MakeArgString(A->getValue()));
  3537. }
  3538. if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
  3539. CmdArgs.push_back("-fretain-comments-from-system-headers");
  3540. // Forward -fcomment-block-commands to -cc1.
  3541. Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
  3542. // Forward -fparse-all-comments to -cc1.
  3543. Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
  3544. // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
  3545. // parser.
  3546. Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
  3547. for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
  3548. ie = Args.filtered_end(); it != ie; ++it) {
  3549. (*it)->claim();
  3550. // We translate this by hand to the -cc1 argument, since nightly test uses
  3551. // it and developers have been trained to spell it with -mllvm.
  3552. if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
  3553. CmdArgs.push_back("-disable-llvm-optzns");
  3554. else
  3555. (*it)->render(Args, CmdArgs);
  3556. }
  3557. if (Output.getType() == types::TY_Dependencies) {
  3558. // Handled with other dependency code.
  3559. } else if (Output.isFilename()) {
  3560. CmdArgs.push_back("-o");
  3561. CmdArgs.push_back(Output.getFilename());
  3562. } else {
  3563. assert(Output.isNothing() && "Invalid output.");
  3564. }
  3565. for (InputInfoList::const_iterator
  3566. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  3567. const InputInfo &II = *it;
  3568. addDashXForInput(Args, II, CmdArgs);
  3569. if (II.isFilename())
  3570. CmdArgs.push_back(II.getFilename());
  3571. else
  3572. II.getInputArg().renderAsInput(Args, CmdArgs);
  3573. }
  3574. Args.AddAllArgs(CmdArgs, options::OPT_undef);
  3575. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  3576. // Optionally embed the -cc1 level arguments into the debug info, for build
  3577. // analysis.
  3578. if (getToolChain().UseDwarfDebugFlags()) {
  3579. ArgStringList OriginalArgs;
  3580. for (ArgList::const_iterator it = Args.begin(),
  3581. ie = Args.end(); it != ie; ++it)
  3582. (*it)->render(Args, OriginalArgs);
  3583. SmallString<256> Flags;
  3584. Flags += Exec;
  3585. for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
  3586. Flags += " ";
  3587. Flags += OriginalArgs[i];
  3588. }
  3589. CmdArgs.push_back("-dwarf-debug-flags");
  3590. CmdArgs.push_back(Args.MakeArgString(Flags.str()));
  3591. }
  3592. // Add the split debug info name to the command lines here so we
  3593. // can propagate it to the backend.
  3594. bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
  3595. getToolChain().getTriple().isOSLinux() &&
  3596. (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA));
  3597. const char *SplitDwarfOut;
  3598. if (SplitDwarf) {
  3599. CmdArgs.push_back("-split-dwarf-file");
  3600. SplitDwarfOut = SplitDebugName(Args, Inputs);
  3601. CmdArgs.push_back(SplitDwarfOut);
  3602. }
  3603. // Finally add the compile command to the compilation.
  3604. if (Args.hasArg(options::OPT__SLASH_fallback) &&
  3605. Output.getType() == types::TY_Object &&
  3606. (InputType == types::TY_C || InputType == types::TY_CXX)) {
  3607. tools::visualstudio::Compile CL(getToolChain());
  3608. Command *CLCommand = CL.GetCommand(C, JA, Output, Inputs, Args,
  3609. LinkingOutput);
  3610. // RTTI support in clang-cl is a work in progress. Fall back to MSVC early
  3611. // if we are using 'clang-cl /fallback /GR'.
  3612. // FIXME: Remove this when RTTI is finished.
  3613. if (Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti, false)) {
  3614. D.Diag(diag::warn_drv_rtti_fallback) << CLCommand->getExecutable();
  3615. C.addCommand(CLCommand);
  3616. } else {
  3617. C.addCommand(new FallbackCommand(JA, *this, Exec, CmdArgs, CLCommand));
  3618. }
  3619. } else {
  3620. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3621. }
  3622. // Handle the debug info splitting at object creation time if we're
  3623. // creating an object.
  3624. // TODO: Currently only works on linux with newer objcopy.
  3625. if (SplitDwarf && !isa<CompileJobAction>(JA))
  3626. SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
  3627. if (Arg *A = Args.getLastArg(options::OPT_pg))
  3628. if (Args.hasArg(options::OPT_fomit_frame_pointer))
  3629. D.Diag(diag::err_drv_argument_not_allowed_with)
  3630. << "-fomit-frame-pointer" << A->getAsString(Args);
  3631. // Claim some arguments which clang supports automatically.
  3632. // -fpch-preprocess is used with gcc to add a special marker in the output to
  3633. // include the PCH file. Clang's PTH solution is completely transparent, so we
  3634. // do not need to deal with it at all.
  3635. Args.ClaimAllArgs(options::OPT_fpch_preprocess);
  3636. // Claim some arguments which clang doesn't support, but we don't
  3637. // care to warn the user about.
  3638. Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
  3639. Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
  3640. // Disable warnings for clang -E -emit-llvm foo.c
  3641. Args.ClaimAllArgs(options::OPT_emit_llvm);
  3642. }
  3643. /// Add options related to the Objective-C runtime/ABI.
  3644. ///
  3645. /// Returns true if the runtime is non-fragile.
  3646. ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
  3647. ArgStringList &cmdArgs,
  3648. RewriteKind rewriteKind) const {
  3649. // Look for the controlling runtime option.
  3650. Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
  3651. options::OPT_fgnu_runtime,
  3652. options::OPT_fobjc_runtime_EQ);
  3653. // Just forward -fobjc-runtime= to the frontend. This supercedes
  3654. // options about fragility.
  3655. if (runtimeArg &&
  3656. runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
  3657. ObjCRuntime runtime;
  3658. StringRef value = runtimeArg->getValue();
  3659. if (runtime.tryParse(value)) {
  3660. getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
  3661. << value;
  3662. }
  3663. runtimeArg->render(args, cmdArgs);
  3664. return runtime;
  3665. }
  3666. // Otherwise, we'll need the ABI "version". Version numbers are
  3667. // slightly confusing for historical reasons:
  3668. // 1 - Traditional "fragile" ABI
  3669. // 2 - Non-fragile ABI, version 1
  3670. // 3 - Non-fragile ABI, version 2
  3671. unsigned objcABIVersion = 1;
  3672. // If -fobjc-abi-version= is present, use that to set the version.
  3673. if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
  3674. StringRef value = abiArg->getValue();
  3675. if (value == "1")
  3676. objcABIVersion = 1;
  3677. else if (value == "2")
  3678. objcABIVersion = 2;
  3679. else if (value == "3")
  3680. objcABIVersion = 3;
  3681. else
  3682. getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
  3683. << value;
  3684. } else {
  3685. // Otherwise, determine if we are using the non-fragile ABI.
  3686. bool nonFragileABIIsDefault =
  3687. (rewriteKind == RK_NonFragile ||
  3688. (rewriteKind == RK_None &&
  3689. getToolChain().IsObjCNonFragileABIDefault()));
  3690. if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
  3691. options::OPT_fno_objc_nonfragile_abi,
  3692. nonFragileABIIsDefault)) {
  3693. // Determine the non-fragile ABI version to use.
  3694. #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
  3695. unsigned nonFragileABIVersion = 1;
  3696. #else
  3697. unsigned nonFragileABIVersion = 2;
  3698. #endif
  3699. if (Arg *abiArg = args.getLastArg(
  3700. options::OPT_fobjc_nonfragile_abi_version_EQ)) {
  3701. StringRef value = abiArg->getValue();
  3702. if (value == "1")
  3703. nonFragileABIVersion = 1;
  3704. else if (value == "2")
  3705. nonFragileABIVersion = 2;
  3706. else
  3707. getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
  3708. << value;
  3709. }
  3710. objcABIVersion = 1 + nonFragileABIVersion;
  3711. } else {
  3712. objcABIVersion = 1;
  3713. }
  3714. }
  3715. // We don't actually care about the ABI version other than whether
  3716. // it's non-fragile.
  3717. bool isNonFragile = objcABIVersion != 1;
  3718. // If we have no runtime argument, ask the toolchain for its default runtime.
  3719. // However, the rewriter only really supports the Mac runtime, so assume that.
  3720. ObjCRuntime runtime;
  3721. if (!runtimeArg) {
  3722. switch (rewriteKind) {
  3723. case RK_None:
  3724. runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
  3725. break;
  3726. case RK_Fragile:
  3727. runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
  3728. break;
  3729. case RK_NonFragile:
  3730. runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
  3731. break;
  3732. }
  3733. // -fnext-runtime
  3734. } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
  3735. // On Darwin, make this use the default behavior for the toolchain.
  3736. if (getToolChain().getTriple().isOSDarwin()) {
  3737. runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
  3738. // Otherwise, build for a generic macosx port.
  3739. } else {
  3740. runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
  3741. }
  3742. // -fgnu-runtime
  3743. } else {
  3744. assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
  3745. // Legacy behaviour is to target the gnustep runtime if we are i
  3746. // non-fragile mode or the GCC runtime in fragile mode.
  3747. if (isNonFragile)
  3748. runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
  3749. else
  3750. runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
  3751. }
  3752. cmdArgs.push_back(args.MakeArgString(
  3753. "-fobjc-runtime=" + runtime.getAsString()));
  3754. return runtime;
  3755. }
  3756. void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
  3757. unsigned RTOptionID = options::OPT__SLASH_MT;
  3758. if (Args.hasArg(options::OPT__SLASH_LDd))
  3759. // The /LDd option implies /MTd. The dependent lib part can be overridden,
  3760. // but defining _DEBUG is sticky.
  3761. RTOptionID = options::OPT__SLASH_MTd;
  3762. if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
  3763. RTOptionID = A->getOption().getID();
  3764. switch(RTOptionID) {
  3765. case options::OPT__SLASH_MD:
  3766. if (Args.hasArg(options::OPT__SLASH_LDd))
  3767. CmdArgs.push_back("-D_DEBUG");
  3768. CmdArgs.push_back("-D_MT");
  3769. CmdArgs.push_back("-D_DLL");
  3770. CmdArgs.push_back("--dependent-lib=msvcrt");
  3771. break;
  3772. case options::OPT__SLASH_MDd:
  3773. CmdArgs.push_back("-D_DEBUG");
  3774. CmdArgs.push_back("-D_MT");
  3775. CmdArgs.push_back("-D_DLL");
  3776. CmdArgs.push_back("--dependent-lib=msvcrtd");
  3777. break;
  3778. case options::OPT__SLASH_MT:
  3779. if (Args.hasArg(options::OPT__SLASH_LDd))
  3780. CmdArgs.push_back("-D_DEBUG");
  3781. CmdArgs.push_back("-D_MT");
  3782. CmdArgs.push_back("--dependent-lib=libcmt");
  3783. break;
  3784. case options::OPT__SLASH_MTd:
  3785. CmdArgs.push_back("-D_DEBUG");
  3786. CmdArgs.push_back("-D_MT");
  3787. CmdArgs.push_back("--dependent-lib=libcmtd");
  3788. break;
  3789. default:
  3790. llvm_unreachable("Unexpected option ID.");
  3791. }
  3792. // This provides POSIX compatibility (maps 'open' to '_open'), which most
  3793. // users want. The /Za flag to cl.exe turns this off, but it's not
  3794. // implemented in clang.
  3795. CmdArgs.push_back("--dependent-lib=oldnames");
  3796. if (Arg *A = Args.getLastArg(options::OPT_show_includes))
  3797. A->render(Args, CmdArgs);
  3798. // RTTI is currently not supported, so disable it by default.
  3799. if (!Args.hasArg(options::OPT_frtti, options::OPT_fno_rtti))
  3800. CmdArgs.push_back("-fno-rtti");
  3801. const Driver &D = getToolChain().getDriver();
  3802. Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
  3803. Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
  3804. if (MostGeneralArg && BestCaseArg)
  3805. D.Diag(clang::diag::err_drv_argument_not_allowed_with)
  3806. << MostGeneralArg->getAsString(Args) << BestCaseArg->getAsString(Args);
  3807. if (MostGeneralArg) {
  3808. Arg *SingleArg = Args.getLastArg(options::OPT__SLASH_vms);
  3809. Arg *MultipleArg = Args.getLastArg(options::OPT__SLASH_vmm);
  3810. Arg *VirtualArg = Args.getLastArg(options::OPT__SLASH_vmv);
  3811. Arg *FirstConflict = SingleArg ? SingleArg : MultipleArg;
  3812. Arg *SecondConflict = VirtualArg ? VirtualArg : MultipleArg;
  3813. if (FirstConflict && SecondConflict && FirstConflict != SecondConflict)
  3814. D.Diag(clang::diag::err_drv_argument_not_allowed_with)
  3815. << FirstConflict->getAsString(Args)
  3816. << SecondConflict->getAsString(Args);
  3817. if (SingleArg)
  3818. CmdArgs.push_back("-fms-memptr-rep=single");
  3819. else if (MultipleArg)
  3820. CmdArgs.push_back("-fms-memptr-rep=multiple");
  3821. else
  3822. CmdArgs.push_back("-fms-memptr-rep=virtual");
  3823. }
  3824. if (Arg *A = Args.getLastArg(options::OPT_vtordisp_mode_EQ))
  3825. A->render(Args, CmdArgs);
  3826. if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
  3827. CmdArgs.push_back("-fdiagnostics-format");
  3828. if (Args.hasArg(options::OPT__SLASH_fallback))
  3829. CmdArgs.push_back("msvc-fallback");
  3830. else
  3831. CmdArgs.push_back("msvc");
  3832. }
  3833. }
  3834. void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
  3835. const InputInfo &Output,
  3836. const InputInfoList &Inputs,
  3837. const ArgList &Args,
  3838. const char *LinkingOutput) const {
  3839. ArgStringList CmdArgs;
  3840. assert(Inputs.size() == 1 && "Unexpected number of inputs.");
  3841. const InputInfo &Input = Inputs[0];
  3842. // Don't warn about "clang -w -c foo.s"
  3843. Args.ClaimAllArgs(options::OPT_w);
  3844. // and "clang -emit-llvm -c foo.s"
  3845. Args.ClaimAllArgs(options::OPT_emit_llvm);
  3846. // Invoke ourselves in -cc1as mode.
  3847. //
  3848. // FIXME: Implement custom jobs for internal actions.
  3849. CmdArgs.push_back("-cc1as");
  3850. // Add the "effective" target triple.
  3851. CmdArgs.push_back("-triple");
  3852. std::string TripleStr =
  3853. getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
  3854. CmdArgs.push_back(Args.MakeArgString(TripleStr));
  3855. // Set the output mode, we currently only expect to be used as a real
  3856. // assembler.
  3857. CmdArgs.push_back("-filetype");
  3858. CmdArgs.push_back("obj");
  3859. // Set the main file name, so that debug info works even with
  3860. // -save-temps or preprocessed assembly.
  3861. CmdArgs.push_back("-main-file-name");
  3862. CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
  3863. // Add the target cpu
  3864. const llvm::Triple &Triple = getToolChain().getTriple();
  3865. std::string CPU = getCPUName(Args, Triple);
  3866. if (!CPU.empty()) {
  3867. CmdArgs.push_back("-target-cpu");
  3868. CmdArgs.push_back(Args.MakeArgString(CPU));
  3869. }
  3870. // Add the target features
  3871. const Driver &D = getToolChain().getDriver();
  3872. getTargetFeatures(D, Triple, Args, CmdArgs, true);
  3873. // Ignore explicit -force_cpusubtype_ALL option.
  3874. (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
  3875. // Determine the original source input.
  3876. const Action *SourceAction = &JA;
  3877. while (SourceAction->getKind() != Action::InputClass) {
  3878. assert(!SourceAction->getInputs().empty() && "unexpected root action!");
  3879. SourceAction = SourceAction->getInputs()[0];
  3880. }
  3881. // Forward -g and handle debug info related flags, assuming we are dealing
  3882. // with an actual assembly file.
  3883. if (SourceAction->getType() == types::TY_Asm ||
  3884. SourceAction->getType() == types::TY_PP_Asm) {
  3885. Args.ClaimAllArgs(options::OPT_g_Group);
  3886. if (Arg *A = Args.getLastArg(options::OPT_g_Group))
  3887. if (!A->getOption().matches(options::OPT_g0))
  3888. CmdArgs.push_back("-g");
  3889. if (Args.hasArg(options::OPT_gdwarf_2))
  3890. CmdArgs.push_back("-gdwarf-2");
  3891. if (Args.hasArg(options::OPT_gdwarf_3))
  3892. CmdArgs.push_back("-gdwarf-3");
  3893. if (Args.hasArg(options::OPT_gdwarf_4))
  3894. CmdArgs.push_back("-gdwarf-4");
  3895. // Add the -fdebug-compilation-dir flag if needed.
  3896. addDebugCompDirArg(Args, CmdArgs);
  3897. // Set the AT_producer to the clang version when using the integrated
  3898. // assembler on assembly source files.
  3899. CmdArgs.push_back("-dwarf-debug-producer");
  3900. CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
  3901. }
  3902. // Optionally embed the -cc1as level arguments into the debug info, for build
  3903. // analysis.
  3904. if (getToolChain().UseDwarfDebugFlags()) {
  3905. ArgStringList OriginalArgs;
  3906. for (ArgList::const_iterator it = Args.begin(),
  3907. ie = Args.end(); it != ie; ++it)
  3908. (*it)->render(Args, OriginalArgs);
  3909. SmallString<256> Flags;
  3910. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  3911. Flags += Exec;
  3912. for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
  3913. Flags += " ";
  3914. Flags += OriginalArgs[i];
  3915. }
  3916. CmdArgs.push_back("-dwarf-debug-flags");
  3917. CmdArgs.push_back(Args.MakeArgString(Flags.str()));
  3918. }
  3919. // FIXME: Add -static support, once we have it.
  3920. // Consume all the warning flags. Usually this would be handled more
  3921. // gracefully by -cc1 (warning about unknown warning flags, etc) but -cc1as
  3922. // doesn't handle that so rather than warning about unused flags that are
  3923. // actually used, we'll lie by omission instead.
  3924. // FIXME: Stop lying and consume only the appropriate driver flags
  3925. for (arg_iterator it = Args.filtered_begin(options::OPT_W_Group),
  3926. ie = Args.filtered_end();
  3927. it != ie; ++it)
  3928. (*it)->claim();
  3929. CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
  3930. getToolChain().getDriver());
  3931. Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
  3932. assert(Output.isFilename() && "Unexpected lipo output.");
  3933. CmdArgs.push_back("-o");
  3934. CmdArgs.push_back(Output.getFilename());
  3935. assert(Input.isFilename() && "Invalid input.");
  3936. CmdArgs.push_back(Input.getFilename());
  3937. const char *Exec = getToolChain().getDriver().getClangProgramPath();
  3938. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  3939. // Handle the debug info splitting at object creation time if we're
  3940. // creating an object.
  3941. // TODO: Currently only works on linux with newer objcopy.
  3942. if (Args.hasArg(options::OPT_gsplit_dwarf) &&
  3943. getToolChain().getTriple().isOSLinux())
  3944. SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
  3945. SplitDebugName(Args, Inputs));
  3946. }
  3947. void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
  3948. const InputInfo &Output,
  3949. const InputInfoList &Inputs,
  3950. const ArgList &Args,
  3951. const char *LinkingOutput) const {
  3952. const Driver &D = getToolChain().getDriver();
  3953. ArgStringList CmdArgs;
  3954. for (ArgList::const_iterator
  3955. it = Args.begin(), ie = Args.end(); it != ie; ++it) {
  3956. Arg *A = *it;
  3957. if (forwardToGCC(A->getOption())) {
  3958. // Don't forward any -g arguments to assembly steps.
  3959. if (isa<AssembleJobAction>(JA) &&
  3960. A->getOption().matches(options::OPT_g_Group))
  3961. continue;
  3962. // Don't forward any -W arguments to assembly and link steps.
  3963. if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
  3964. A->getOption().matches(options::OPT_W_Group))
  3965. continue;
  3966. // It is unfortunate that we have to claim here, as this means
  3967. // we will basically never report anything interesting for
  3968. // platforms using a generic gcc, even if we are just using gcc
  3969. // to get to the assembler.
  3970. A->claim();
  3971. A->render(Args, CmdArgs);
  3972. }
  3973. }
  3974. RenderExtraToolArgs(JA, CmdArgs);
  3975. // If using a driver driver, force the arch.
  3976. llvm::Triple::ArchType Arch = getToolChain().getArch();
  3977. if (getToolChain().getTriple().isOSDarwin()) {
  3978. CmdArgs.push_back("-arch");
  3979. // FIXME: Remove these special cases.
  3980. if (Arch == llvm::Triple::ppc)
  3981. CmdArgs.push_back("ppc");
  3982. else if (Arch == llvm::Triple::ppc64)
  3983. CmdArgs.push_back("ppc64");
  3984. else if (Arch == llvm::Triple::ppc64le)
  3985. CmdArgs.push_back("ppc64le");
  3986. else
  3987. CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
  3988. }
  3989. // Try to force gcc to match the tool chain we want, if we recognize
  3990. // the arch.
  3991. //
  3992. // FIXME: The triple class should directly provide the information we want
  3993. // here.
  3994. if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
  3995. CmdArgs.push_back("-m32");
  3996. else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 ||
  3997. Arch == llvm::Triple::ppc64le)
  3998. CmdArgs.push_back("-m64");
  3999. if (Output.isFilename()) {
  4000. CmdArgs.push_back("-o");
  4001. CmdArgs.push_back(Output.getFilename());
  4002. } else {
  4003. assert(Output.isNothing() && "Unexpected output");
  4004. CmdArgs.push_back("-fsyntax-only");
  4005. }
  4006. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4007. options::OPT_Xassembler);
  4008. // Only pass -x if gcc will understand it; otherwise hope gcc
  4009. // understands the suffix correctly. The main use case this would go
  4010. // wrong in is for linker inputs if they happened to have an odd
  4011. // suffix; really the only way to get this to happen is a command
  4012. // like '-x foobar a.c' which will treat a.c like a linker input.
  4013. //
  4014. // FIXME: For the linker case specifically, can we safely convert
  4015. // inputs into '-Wl,' options?
  4016. for (InputInfoList::const_iterator
  4017. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4018. const InputInfo &II = *it;
  4019. // Don't try to pass LLVM or AST inputs to a generic gcc.
  4020. if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
  4021. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  4022. D.Diag(diag::err_drv_no_linker_llvm_support)
  4023. << getToolChain().getTripleString();
  4024. else if (II.getType() == types::TY_AST)
  4025. D.Diag(diag::err_drv_no_ast_support)
  4026. << getToolChain().getTripleString();
  4027. else if (II.getType() == types::TY_ModuleFile)
  4028. D.Diag(diag::err_drv_no_module_support)
  4029. << getToolChain().getTripleString();
  4030. if (types::canTypeBeUserSpecified(II.getType())) {
  4031. CmdArgs.push_back("-x");
  4032. CmdArgs.push_back(types::getTypeName(II.getType()));
  4033. }
  4034. if (II.isFilename())
  4035. CmdArgs.push_back(II.getFilename());
  4036. else {
  4037. const Arg &A = II.getInputArg();
  4038. // Reverse translate some rewritten options.
  4039. if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
  4040. CmdArgs.push_back("-lstdc++");
  4041. continue;
  4042. }
  4043. // Don't render as input, we need gcc to do the translations.
  4044. A.render(Args, CmdArgs);
  4045. }
  4046. }
  4047. const std::string customGCCName = D.getCCCGenericGCCName();
  4048. const char *GCCName;
  4049. if (!customGCCName.empty())
  4050. GCCName = customGCCName.c_str();
  4051. else if (D.CCCIsCXX()) {
  4052. GCCName = "g++";
  4053. } else
  4054. GCCName = "gcc";
  4055. const char *Exec =
  4056. Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
  4057. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4058. }
  4059. void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
  4060. ArgStringList &CmdArgs) const {
  4061. CmdArgs.push_back("-E");
  4062. }
  4063. void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
  4064. ArgStringList &CmdArgs) const {
  4065. const Driver &D = getToolChain().getDriver();
  4066. // If -flto, etc. are present then make sure not to force assembly output.
  4067. if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
  4068. JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
  4069. CmdArgs.push_back("-c");
  4070. else {
  4071. if (JA.getType() != types::TY_PP_Asm)
  4072. D.Diag(diag::err_drv_invalid_gcc_output_type)
  4073. << getTypeName(JA.getType());
  4074. CmdArgs.push_back("-S");
  4075. }
  4076. }
  4077. void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
  4078. ArgStringList &CmdArgs) const {
  4079. // The types are (hopefully) good enough.
  4080. }
  4081. // Hexagon tools start.
  4082. void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
  4083. ArgStringList &CmdArgs) const {
  4084. }
  4085. void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4086. const InputInfo &Output,
  4087. const InputInfoList &Inputs,
  4088. const ArgList &Args,
  4089. const char *LinkingOutput) const {
  4090. const Driver &D = getToolChain().getDriver();
  4091. ArgStringList CmdArgs;
  4092. std::string MarchString = "-march=";
  4093. MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
  4094. CmdArgs.push_back(Args.MakeArgString(MarchString));
  4095. RenderExtraToolArgs(JA, CmdArgs);
  4096. if (Output.isFilename()) {
  4097. CmdArgs.push_back("-o");
  4098. CmdArgs.push_back(Output.getFilename());
  4099. } else {
  4100. assert(Output.isNothing() && "Unexpected output");
  4101. CmdArgs.push_back("-fsyntax-only");
  4102. }
  4103. std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
  4104. if (!SmallDataThreshold.empty())
  4105. CmdArgs.push_back(
  4106. Args.MakeArgString(std::string("-G") + SmallDataThreshold));
  4107. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4108. options::OPT_Xassembler);
  4109. // Only pass -x if gcc will understand it; otherwise hope gcc
  4110. // understands the suffix correctly. The main use case this would go
  4111. // wrong in is for linker inputs if they happened to have an odd
  4112. // suffix; really the only way to get this to happen is a command
  4113. // like '-x foobar a.c' which will treat a.c like a linker input.
  4114. //
  4115. // FIXME: For the linker case specifically, can we safely convert
  4116. // inputs into '-Wl,' options?
  4117. for (InputInfoList::const_iterator
  4118. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4119. const InputInfo &II = *it;
  4120. // Don't try to pass LLVM or AST inputs to a generic gcc.
  4121. if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
  4122. II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
  4123. D.Diag(clang::diag::err_drv_no_linker_llvm_support)
  4124. << getToolChain().getTripleString();
  4125. else if (II.getType() == types::TY_AST)
  4126. D.Diag(clang::diag::err_drv_no_ast_support)
  4127. << getToolChain().getTripleString();
  4128. else if (II.getType() == types::TY_ModuleFile)
  4129. D.Diag(diag::err_drv_no_module_support)
  4130. << getToolChain().getTripleString();
  4131. if (II.isFilename())
  4132. CmdArgs.push_back(II.getFilename());
  4133. else
  4134. // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
  4135. II.getInputArg().render(Args, CmdArgs);
  4136. }
  4137. const char *GCCName = "hexagon-as";
  4138. const char *Exec =
  4139. Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
  4140. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4141. }
  4142. void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
  4143. ArgStringList &CmdArgs) const {
  4144. // The types are (hopefully) good enough.
  4145. }
  4146. void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4147. const InputInfo &Output,
  4148. const InputInfoList &Inputs,
  4149. const ArgList &Args,
  4150. const char *LinkingOutput) const {
  4151. const toolchains::Hexagon_TC& ToolChain =
  4152. static_cast<const toolchains::Hexagon_TC&>(getToolChain());
  4153. const Driver &D = ToolChain.getDriver();
  4154. ArgStringList CmdArgs;
  4155. //----------------------------------------------------------------------------
  4156. //
  4157. //----------------------------------------------------------------------------
  4158. bool hasStaticArg = Args.hasArg(options::OPT_static);
  4159. bool buildingLib = Args.hasArg(options::OPT_shared);
  4160. bool buildPIE = Args.hasArg(options::OPT_pie);
  4161. bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
  4162. bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
  4163. bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
  4164. bool useShared = buildingLib && !hasStaticArg;
  4165. //----------------------------------------------------------------------------
  4166. // Silence warnings for various options
  4167. //----------------------------------------------------------------------------
  4168. Args.ClaimAllArgs(options::OPT_g_Group);
  4169. Args.ClaimAllArgs(options::OPT_emit_llvm);
  4170. Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
  4171. // handled somewhere else.
  4172. Args.ClaimAllArgs(options::OPT_static_libgcc);
  4173. //----------------------------------------------------------------------------
  4174. //
  4175. //----------------------------------------------------------------------------
  4176. for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
  4177. e = ToolChain.ExtraOpts.end();
  4178. i != e; ++i)
  4179. CmdArgs.push_back(i->c_str());
  4180. std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
  4181. CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
  4182. if (buildingLib) {
  4183. CmdArgs.push_back("-shared");
  4184. CmdArgs.push_back("-call_shared"); // should be the default, but doing as
  4185. // hexagon-gcc does
  4186. }
  4187. if (hasStaticArg)
  4188. CmdArgs.push_back("-static");
  4189. if (buildPIE && !buildingLib)
  4190. CmdArgs.push_back("-pie");
  4191. std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
  4192. if (!SmallDataThreshold.empty()) {
  4193. CmdArgs.push_back(
  4194. Args.MakeArgString(std::string("-G") + SmallDataThreshold));
  4195. }
  4196. //----------------------------------------------------------------------------
  4197. //
  4198. //----------------------------------------------------------------------------
  4199. CmdArgs.push_back("-o");
  4200. CmdArgs.push_back(Output.getFilename());
  4201. const std::string MarchSuffix = "/" + MarchString;
  4202. const std::string G0Suffix = "/G0";
  4203. const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
  4204. const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir)
  4205. + "/";
  4206. const std::string StartFilesDir = RootDir
  4207. + "hexagon/lib"
  4208. + (buildingLib
  4209. ? MarchG0Suffix : MarchSuffix);
  4210. //----------------------------------------------------------------------------
  4211. // moslib
  4212. //----------------------------------------------------------------------------
  4213. std::vector<std::string> oslibs;
  4214. bool hasStandalone= false;
  4215. for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
  4216. ie = Args.filtered_end(); it != ie; ++it) {
  4217. (*it)->claim();
  4218. oslibs.push_back((*it)->getValue());
  4219. hasStandalone = hasStandalone || (oslibs.back() == "standalone");
  4220. }
  4221. if (oslibs.empty()) {
  4222. oslibs.push_back("standalone");
  4223. hasStandalone = true;
  4224. }
  4225. //----------------------------------------------------------------------------
  4226. // Start Files
  4227. //----------------------------------------------------------------------------
  4228. if (incStdLib && incStartFiles) {
  4229. if (!buildingLib) {
  4230. if (hasStandalone) {
  4231. CmdArgs.push_back(
  4232. Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
  4233. }
  4234. CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
  4235. }
  4236. std::string initObj = useShared ? "/initS.o" : "/init.o";
  4237. CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
  4238. }
  4239. //----------------------------------------------------------------------------
  4240. // Library Search Paths
  4241. //----------------------------------------------------------------------------
  4242. const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
  4243. for (ToolChain::path_list::const_iterator
  4244. i = LibPaths.begin(),
  4245. e = LibPaths.end();
  4246. i != e;
  4247. ++i)
  4248. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
  4249. //----------------------------------------------------------------------------
  4250. //
  4251. //----------------------------------------------------------------------------
  4252. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4253. Args.AddAllArgs(CmdArgs, options::OPT_e);
  4254. Args.AddAllArgs(CmdArgs, options::OPT_s);
  4255. Args.AddAllArgs(CmdArgs, options::OPT_t);
  4256. Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
  4257. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  4258. //----------------------------------------------------------------------------
  4259. // Libraries
  4260. //----------------------------------------------------------------------------
  4261. if (incStdLib && incDefLibs) {
  4262. if (D.CCCIsCXX()) {
  4263. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  4264. CmdArgs.push_back("-lm");
  4265. }
  4266. CmdArgs.push_back("--start-group");
  4267. if (!buildingLib) {
  4268. for(std::vector<std::string>::iterator i = oslibs.begin(),
  4269. e = oslibs.end(); i != e; ++i)
  4270. CmdArgs.push_back(Args.MakeArgString("-l" + *i));
  4271. CmdArgs.push_back("-lc");
  4272. }
  4273. CmdArgs.push_back("-lgcc");
  4274. CmdArgs.push_back("--end-group");
  4275. }
  4276. //----------------------------------------------------------------------------
  4277. // End files
  4278. //----------------------------------------------------------------------------
  4279. if (incStdLib && incStartFiles) {
  4280. std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
  4281. CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
  4282. }
  4283. std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
  4284. C.addCommand(new Command(JA, *this, Args.MakeArgString(Linker), CmdArgs));
  4285. }
  4286. // Hexagon tools end.
  4287. /// getARMCPUForMArch - Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting
  4288. //
  4289. // FIXME: tblgen this.
  4290. const char *arm::getARMCPUForMArch(const ArgList &Args,
  4291. const llvm::Triple &Triple) {
  4292. StringRef MArch;
  4293. if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
  4294. // Otherwise, if we have -march= choose the base CPU for that arch.
  4295. MArch = A->getValue();
  4296. } else {
  4297. // Otherwise, use the Arch from the triple.
  4298. MArch = Triple.getArchName();
  4299. }
  4300. // Handle -march=native.
  4301. if (MArch == "native") {
  4302. std::string CPU = llvm::sys::getHostCPUName();
  4303. if (CPU != "generic") {
  4304. // Translate the native cpu into the architecture. The switch below will
  4305. // then chose the minimum cpu for that arch.
  4306. MArch = std::string("arm") + arm::getLLVMArchSuffixForARM(CPU);
  4307. }
  4308. }
  4309. switch (Triple.getOS()) {
  4310. case llvm::Triple::NetBSD:
  4311. if (MArch == "armv6")
  4312. return "arm1176jzf-s";
  4313. break;
  4314. case llvm::Triple::Win32:
  4315. // FIXME: this is invalid for WindowsCE
  4316. return "cortex-a9";
  4317. default:
  4318. break;
  4319. }
  4320. const char *result = nullptr;
  4321. size_t offset = StringRef::npos;
  4322. if (MArch.startswith("arm"))
  4323. offset = 3;
  4324. if (MArch.startswith("thumb"))
  4325. offset = 5;
  4326. if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
  4327. offset += 2;
  4328. if (offset != StringRef::npos)
  4329. result = llvm::StringSwitch<const char *>(MArch.substr(offset))
  4330. .Cases("v2", "v2a", "arm2")
  4331. .Case("v3", "arm6")
  4332. .Case("v3m", "arm7m")
  4333. .Case("v4", "strongarm")
  4334. .Case("v4t", "arm7tdmi")
  4335. .Cases("v5", "v5t", "arm10tdmi")
  4336. .Cases("v5e", "v5te", "arm1022e")
  4337. .Case("v5tej", "arm926ej-s")
  4338. .Cases("v6", "v6k", "arm1136jf-s")
  4339. .Case("v6j", "arm1136j-s")
  4340. .Cases("v6z", "v6zk", "arm1176jzf-s")
  4341. .Case("v6t2", "arm1156t2-s")
  4342. .Cases("v6m", "v6-m", "cortex-m0")
  4343. .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
  4344. .Cases("v7s", "v7-s", "swift")
  4345. .Cases("v7r", "v7-r", "cortex-r4")
  4346. .Cases("v7m", "v7-m", "cortex-m3")
  4347. .Cases("v7em", "v7e-m", "cortex-m4")
  4348. .Cases("v8", "v8a", "v8-a", "cortex-a53")
  4349. .Default(nullptr);
  4350. else
  4351. result = llvm::StringSwitch<const char *>(MArch)
  4352. .Case("ep9312", "ep9312")
  4353. .Case("iwmmxt", "iwmmxt")
  4354. .Case("xscale", "xscale")
  4355. .Default(nullptr);
  4356. if (result)
  4357. return result;
  4358. // If all else failed, return the most base CPU with thumb interworking
  4359. // supported by LLVM.
  4360. // FIXME: Should warn once that we're falling back.
  4361. switch (Triple.getOS()) {
  4362. case llvm::Triple::NetBSD:
  4363. switch (Triple.getEnvironment()) {
  4364. case llvm::Triple::GNUEABIHF:
  4365. case llvm::Triple::GNUEABI:
  4366. case llvm::Triple::EABIHF:
  4367. case llvm::Triple::EABI:
  4368. return "arm926ej-s";
  4369. default:
  4370. return "strongarm";
  4371. }
  4372. default:
  4373. switch (Triple.getEnvironment()) {
  4374. case llvm::Triple::EABIHF:
  4375. case llvm::Triple::GNUEABIHF:
  4376. return "arm1176jzf-s";
  4377. default:
  4378. return "arm7tdmi";
  4379. }
  4380. }
  4381. }
  4382. /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
  4383. StringRef arm::getARMTargetCPU(const ArgList &Args,
  4384. const llvm::Triple &Triple) {
  4385. // FIXME: Warn on inconsistent use of -mcpu and -march.
  4386. // If we have -mcpu=, use that.
  4387. if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
  4388. StringRef MCPU = A->getValue();
  4389. // Handle -mcpu=native.
  4390. if (MCPU == "native")
  4391. return llvm::sys::getHostCPUName();
  4392. else
  4393. return MCPU;
  4394. }
  4395. return getARMCPUForMArch(Args, Triple);
  4396. }
  4397. /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
  4398. /// CPU.
  4399. //
  4400. // FIXME: This is redundant with -mcpu, why does LLVM use this.
  4401. // FIXME: tblgen this, or kill it!
  4402. const char *arm::getLLVMArchSuffixForARM(StringRef CPU) {
  4403. return llvm::StringSwitch<const char *>(CPU)
  4404. .Case("strongarm", "v4")
  4405. .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
  4406. .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
  4407. .Cases("arm920", "arm920t", "arm922t", "v4t")
  4408. .Cases("arm940t", "ep9312","v4t")
  4409. .Cases("arm10tdmi", "arm1020t", "v5")
  4410. .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
  4411. .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
  4412. .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
  4413. .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
  4414. .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
  4415. .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
  4416. .Cases("cortex-a5", "cortex-a7", "cortex-a8", "cortex-a9-mp", "v7")
  4417. .Cases("cortex-a9", "cortex-a12", "cortex-a15", "krait", "v7")
  4418. .Cases("cortex-r4", "cortex-r5", "v7r")
  4419. .Case("cortex-m0", "v6m")
  4420. .Case("cortex-m3", "v7m")
  4421. .Case("cortex-m4", "v7em")
  4422. .Case("swift", "v7s")
  4423. .Case("cyclone", "v8")
  4424. .Cases("cortex-a53", "cortex-a57", "v8")
  4425. .Default("");
  4426. }
  4427. bool mips::hasMipsAbiArg(const ArgList &Args, const char *Value) {
  4428. Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
  4429. return A && (A->getValue() == StringRef(Value));
  4430. }
  4431. llvm::Triple::ArchType darwin::getArchTypeForMachOArchName(StringRef Str) {
  4432. // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
  4433. // archs which Darwin doesn't use.
  4434. // The matching this routine does is fairly pointless, since it is neither the
  4435. // complete architecture list, nor a reasonable subset. The problem is that
  4436. // historically the driver driver accepts this and also ties its -march=
  4437. // handling to the architecture name, so we need to be careful before removing
  4438. // support for it.
  4439. // This code must be kept in sync with Clang's Darwin specific argument
  4440. // translation.
  4441. return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
  4442. .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
  4443. .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
  4444. .Case("ppc64", llvm::Triple::ppc64)
  4445. .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
  4446. .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
  4447. llvm::Triple::x86)
  4448. .Cases("x86_64", "x86_64h", llvm::Triple::x86_64)
  4449. // This is derived from the driver driver.
  4450. .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
  4451. .Cases("armv7", "armv7em", "armv7k", "armv7m", llvm::Triple::arm)
  4452. .Cases("armv7s", "xscale", llvm::Triple::arm)
  4453. .Case("arm64", llvm::Triple::arm64)
  4454. .Case("r600", llvm::Triple::r600)
  4455. .Case("nvptx", llvm::Triple::nvptx)
  4456. .Case("nvptx64", llvm::Triple::nvptx64)
  4457. .Case("amdil", llvm::Triple::amdil)
  4458. .Case("spir", llvm::Triple::spir)
  4459. .Default(llvm::Triple::UnknownArch);
  4460. }
  4461. void darwin::setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str) {
  4462. llvm::Triple::ArchType Arch = getArchTypeForMachOArchName(Str);
  4463. T.setArch(Arch);
  4464. if (Str == "x86_64h")
  4465. T.setArchName(Str);
  4466. else if (Str == "armv6m" || Str == "armv7m" || Str == "armv7em") {
  4467. T.setOS(llvm::Triple::UnknownOS);
  4468. T.setObjectFormat(llvm::Triple::MachO);
  4469. }
  4470. }
  4471. const char *Clang::getBaseInputName(const ArgList &Args,
  4472. const InputInfoList &Inputs) {
  4473. return Args.MakeArgString(
  4474. llvm::sys::path::filename(Inputs[0].getBaseInput()));
  4475. }
  4476. const char *Clang::getBaseInputStem(const ArgList &Args,
  4477. const InputInfoList &Inputs) {
  4478. const char *Str = getBaseInputName(Args, Inputs);
  4479. if (const char *End = strrchr(Str, '.'))
  4480. return Args.MakeArgString(std::string(Str, End));
  4481. return Str;
  4482. }
  4483. const char *Clang::getDependencyFileName(const ArgList &Args,
  4484. const InputInfoList &Inputs) {
  4485. // FIXME: Think about this more.
  4486. std::string Res;
  4487. if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
  4488. std::string Str(OutputOpt->getValue());
  4489. Res = Str.substr(0, Str.rfind('.'));
  4490. } else {
  4491. Res = getBaseInputStem(Args, Inputs);
  4492. }
  4493. return Args.MakeArgString(Res + ".d");
  4494. }
  4495. void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4496. const InputInfo &Output,
  4497. const InputInfoList &Inputs,
  4498. const ArgList &Args,
  4499. const char *LinkingOutput) const {
  4500. ArgStringList CmdArgs;
  4501. assert(Inputs.size() == 1 && "Unexpected number of inputs.");
  4502. const InputInfo &Input = Inputs[0];
  4503. // Determine the original source input.
  4504. const Action *SourceAction = &JA;
  4505. while (SourceAction->getKind() != Action::InputClass) {
  4506. assert(!SourceAction->getInputs().empty() && "unexpected root action!");
  4507. SourceAction = SourceAction->getInputs()[0];
  4508. }
  4509. // If -fno_integrated_as is used add -Q to the darwin assember driver to make
  4510. // sure it runs its system assembler not clang's integrated assembler.
  4511. // Applicable to darwin11+ and Xcode 4+. darwin<10 lacked integrated-as.
  4512. // FIXME: at run-time detect assembler capabilities or rely on version
  4513. // information forwarded by -target-assembler-version (future)
  4514. if (Args.hasArg(options::OPT_fno_integrated_as)) {
  4515. const llvm::Triple &T(getToolChain().getTriple());
  4516. if (!(T.isMacOSX() && T.isMacOSXVersionLT(10, 7)))
  4517. CmdArgs.push_back("-Q");
  4518. }
  4519. // Forward -g, assuming we are dealing with an actual assembly file.
  4520. if (SourceAction->getType() == types::TY_Asm ||
  4521. SourceAction->getType() == types::TY_PP_Asm) {
  4522. if (Args.hasArg(options::OPT_gstabs))
  4523. CmdArgs.push_back("--gstabs");
  4524. else if (Args.hasArg(options::OPT_g_Group))
  4525. CmdArgs.push_back("-g");
  4526. }
  4527. // Derived from asm spec.
  4528. AddMachOArch(Args, CmdArgs);
  4529. // Use -force_cpusubtype_ALL on x86 by default.
  4530. if (getToolChain().getArch() == llvm::Triple::x86 ||
  4531. getToolChain().getArch() == llvm::Triple::x86_64 ||
  4532. Args.hasArg(options::OPT_force__cpusubtype__ALL))
  4533. CmdArgs.push_back("-force_cpusubtype_ALL");
  4534. if (getToolChain().getArch() != llvm::Triple::x86_64 &&
  4535. (((Args.hasArg(options::OPT_mkernel) ||
  4536. Args.hasArg(options::OPT_fapple_kext)) &&
  4537. getMachOToolChain().isKernelStatic()) ||
  4538. Args.hasArg(options::OPT_static)))
  4539. CmdArgs.push_back("-static");
  4540. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4541. options::OPT_Xassembler);
  4542. assert(Output.isFilename() && "Unexpected lipo output.");
  4543. CmdArgs.push_back("-o");
  4544. CmdArgs.push_back(Output.getFilename());
  4545. assert(Input.isFilename() && "Invalid input.");
  4546. CmdArgs.push_back(Input.getFilename());
  4547. // asm_final spec is empty.
  4548. const char *Exec =
  4549. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  4550. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4551. }
  4552. void darwin::MachOTool::anchor() {}
  4553. void darwin::MachOTool::AddMachOArch(const ArgList &Args,
  4554. ArgStringList &CmdArgs) const {
  4555. StringRef ArchName = getMachOToolChain().getMachOArchName(Args);
  4556. // Derived from darwin_arch spec.
  4557. CmdArgs.push_back("-arch");
  4558. CmdArgs.push_back(Args.MakeArgString(ArchName));
  4559. // FIXME: Is this needed anymore?
  4560. if (ArchName == "arm")
  4561. CmdArgs.push_back("-force_cpusubtype_ALL");
  4562. }
  4563. bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
  4564. // We only need to generate a temp path for LTO if we aren't compiling object
  4565. // files. When compiling source files, we run 'dsymutil' after linking. We
  4566. // don't run 'dsymutil' when compiling object files.
  4567. for (InputInfoList::const_iterator
  4568. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it)
  4569. if (it->getType() != types::TY_Object)
  4570. return true;
  4571. return false;
  4572. }
  4573. void darwin::Link::AddLinkArgs(Compilation &C,
  4574. const ArgList &Args,
  4575. ArgStringList &CmdArgs,
  4576. const InputInfoList &Inputs) const {
  4577. const Driver &D = getToolChain().getDriver();
  4578. const toolchains::MachO &MachOTC = getMachOToolChain();
  4579. unsigned Version[3] = { 0, 0, 0 };
  4580. if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
  4581. bool HadExtra;
  4582. if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
  4583. Version[1], Version[2], HadExtra) ||
  4584. HadExtra)
  4585. D.Diag(diag::err_drv_invalid_version_number)
  4586. << A->getAsString(Args);
  4587. }
  4588. // Newer linkers support -demangle. Pass it if supported and not disabled by
  4589. // the user.
  4590. if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
  4591. CmdArgs.push_back("-demangle");
  4592. if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137)
  4593. CmdArgs.push_back("-export_dynamic");
  4594. // If we are using LTO, then automatically create a temporary file path for
  4595. // the linker to use, so that it's lifetime will extend past a possible
  4596. // dsymutil step.
  4597. if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
  4598. const char *TmpPath = C.getArgs().MakeArgString(
  4599. D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
  4600. C.addTempFile(TmpPath);
  4601. CmdArgs.push_back("-object_path_lto");
  4602. CmdArgs.push_back(TmpPath);
  4603. }
  4604. // Derived from the "link" spec.
  4605. Args.AddAllArgs(CmdArgs, options::OPT_static);
  4606. if (!Args.hasArg(options::OPT_static))
  4607. CmdArgs.push_back("-dynamic");
  4608. if (Args.hasArg(options::OPT_fgnu_runtime)) {
  4609. // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
  4610. // here. How do we wish to handle such things?
  4611. }
  4612. if (!Args.hasArg(options::OPT_dynamiclib)) {
  4613. AddMachOArch(Args, CmdArgs);
  4614. // FIXME: Why do this only on this path?
  4615. Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
  4616. Args.AddLastArg(CmdArgs, options::OPT_bundle);
  4617. Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
  4618. Args.AddAllArgs(CmdArgs, options::OPT_client__name);
  4619. Arg *A;
  4620. if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
  4621. (A = Args.getLastArg(options::OPT_current__version)) ||
  4622. (A = Args.getLastArg(options::OPT_install__name)))
  4623. D.Diag(diag::err_drv_argument_only_allowed_with)
  4624. << A->getAsString(Args) << "-dynamiclib";
  4625. Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
  4626. Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
  4627. Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
  4628. } else {
  4629. CmdArgs.push_back("-dylib");
  4630. Arg *A;
  4631. if ((A = Args.getLastArg(options::OPT_bundle)) ||
  4632. (A = Args.getLastArg(options::OPT_bundle__loader)) ||
  4633. (A = Args.getLastArg(options::OPT_client__name)) ||
  4634. (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
  4635. (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
  4636. (A = Args.getLastArg(options::OPT_private__bundle)))
  4637. D.Diag(diag::err_drv_argument_not_allowed_with)
  4638. << A->getAsString(Args) << "-dynamiclib";
  4639. Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
  4640. "-dylib_compatibility_version");
  4641. Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
  4642. "-dylib_current_version");
  4643. AddMachOArch(Args, CmdArgs);
  4644. Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
  4645. "-dylib_install_name");
  4646. }
  4647. Args.AddLastArg(CmdArgs, options::OPT_all__load);
  4648. Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
  4649. Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
  4650. if (MachOTC.isTargetIOSBased())
  4651. Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
  4652. Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
  4653. Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
  4654. Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
  4655. Args.AddLastArg(CmdArgs, options::OPT_dynamic);
  4656. Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
  4657. Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
  4658. Args.AddAllArgs(CmdArgs, options::OPT_force__load);
  4659. Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
  4660. Args.AddAllArgs(CmdArgs, options::OPT_image__base);
  4661. Args.AddAllArgs(CmdArgs, options::OPT_init);
  4662. // Add the deployment target.
  4663. MachOTC.addMinVersionArgs(Args, CmdArgs);
  4664. Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
  4665. Args.AddLastArg(CmdArgs, options::OPT_multi__module);
  4666. Args.AddLastArg(CmdArgs, options::OPT_single__module);
  4667. Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
  4668. Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
  4669. if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
  4670. options::OPT_fno_pie,
  4671. options::OPT_fno_PIE)) {
  4672. if (A->getOption().matches(options::OPT_fpie) ||
  4673. A->getOption().matches(options::OPT_fPIE))
  4674. CmdArgs.push_back("-pie");
  4675. else
  4676. CmdArgs.push_back("-no_pie");
  4677. }
  4678. Args.AddLastArg(CmdArgs, options::OPT_prebind);
  4679. Args.AddLastArg(CmdArgs, options::OPT_noprebind);
  4680. Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
  4681. Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
  4682. Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
  4683. Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
  4684. Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
  4685. Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
  4686. Args.AddAllArgs(CmdArgs, options::OPT_segprot);
  4687. Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
  4688. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
  4689. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
  4690. Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
  4691. Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
  4692. Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
  4693. Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
  4694. // Give --sysroot= preference, over the Apple specific behavior to also use
  4695. // --isysroot as the syslibroot.
  4696. StringRef sysroot = C.getSysRoot();
  4697. if (sysroot != "") {
  4698. CmdArgs.push_back("-syslibroot");
  4699. CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
  4700. } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
  4701. CmdArgs.push_back("-syslibroot");
  4702. CmdArgs.push_back(A->getValue());
  4703. }
  4704. Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
  4705. Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
  4706. Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
  4707. Args.AddAllArgs(CmdArgs, options::OPT_undefined);
  4708. Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
  4709. Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
  4710. Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
  4711. Args.AddAllArgs(CmdArgs, options::OPT_y);
  4712. Args.AddLastArg(CmdArgs, options::OPT_w);
  4713. Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
  4714. Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
  4715. Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
  4716. Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
  4717. Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
  4718. Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
  4719. Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
  4720. Args.AddLastArg(CmdArgs, options::OPT_whyload);
  4721. Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
  4722. Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
  4723. Args.AddLastArg(CmdArgs, options::OPT_dylinker);
  4724. Args.AddLastArg(CmdArgs, options::OPT_Mach);
  4725. }
  4726. enum LibOpenMP {
  4727. LibUnknown,
  4728. LibGOMP,
  4729. LibIOMP5
  4730. };
  4731. void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4732. const InputInfo &Output,
  4733. const InputInfoList &Inputs,
  4734. const ArgList &Args,
  4735. const char *LinkingOutput) const {
  4736. assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
  4737. // The logic here is derived from gcc's behavior; most of which
  4738. // comes from specs (starting with link_command). Consult gcc for
  4739. // more information.
  4740. ArgStringList CmdArgs;
  4741. /// Hack(tm) to ignore linking errors when we are doing ARC migration.
  4742. if (Args.hasArg(options::OPT_ccc_arcmt_check,
  4743. options::OPT_ccc_arcmt_migrate)) {
  4744. for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
  4745. (*I)->claim();
  4746. const char *Exec =
  4747. Args.MakeArgString(getToolChain().GetProgramPath("touch"));
  4748. CmdArgs.push_back(Output.getFilename());
  4749. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4750. return;
  4751. }
  4752. // I'm not sure why this particular decomposition exists in gcc, but
  4753. // we follow suite for ease of comparison.
  4754. AddLinkArgs(C, Args, CmdArgs, Inputs);
  4755. Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
  4756. Args.AddAllArgs(CmdArgs, options::OPT_s);
  4757. Args.AddAllArgs(CmdArgs, options::OPT_t);
  4758. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  4759. Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
  4760. Args.AddLastArg(CmdArgs, options::OPT_e);
  4761. Args.AddAllArgs(CmdArgs, options::OPT_r);
  4762. // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
  4763. // members of static archive libraries which implement Objective-C classes or
  4764. // categories.
  4765. if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
  4766. CmdArgs.push_back("-ObjC");
  4767. CmdArgs.push_back("-o");
  4768. CmdArgs.push_back(Output.getFilename());
  4769. if (!Args.hasArg(options::OPT_nostdlib) &&
  4770. !Args.hasArg(options::OPT_nostartfiles))
  4771. getMachOToolChain().addStartObjectFileArgs(Args, CmdArgs);
  4772. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4773. LibOpenMP UsedOpenMPLib = LibUnknown;
  4774. if (Args.hasArg(options::OPT_fopenmp)) {
  4775. UsedOpenMPLib = LibGOMP;
  4776. } else if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
  4777. UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(A->getValue())
  4778. .Case("libgomp", LibGOMP)
  4779. .Case("libiomp5", LibIOMP5)
  4780. .Default(LibUnknown);
  4781. if (UsedOpenMPLib == LibUnknown)
  4782. getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
  4783. << A->getOption().getName() << A->getValue();
  4784. }
  4785. switch (UsedOpenMPLib) {
  4786. case LibGOMP:
  4787. CmdArgs.push_back("-lgomp");
  4788. break;
  4789. case LibIOMP5:
  4790. CmdArgs.push_back("-liomp5");
  4791. break;
  4792. case LibUnknown:
  4793. break;
  4794. }
  4795. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4796. if (isObjCRuntimeLinked(Args) &&
  4797. !Args.hasArg(options::OPT_nostdlib) &&
  4798. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4799. // We use arclite library for both ARC and subscripting support.
  4800. getMachOToolChain().AddLinkARCArgs(Args, CmdArgs);
  4801. CmdArgs.push_back("-framework");
  4802. CmdArgs.push_back("Foundation");
  4803. // Link libobj.
  4804. CmdArgs.push_back("-lobjc");
  4805. }
  4806. if (LinkingOutput) {
  4807. CmdArgs.push_back("-arch_multiple");
  4808. CmdArgs.push_back("-final_output");
  4809. CmdArgs.push_back(LinkingOutput);
  4810. }
  4811. if (Args.hasArg(options::OPT_fnested_functions))
  4812. CmdArgs.push_back("-allow_stack_execute");
  4813. if (!Args.hasArg(options::OPT_nostdlib) &&
  4814. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4815. if (getToolChain().getDriver().CCCIsCXX())
  4816. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  4817. // link_ssp spec is empty.
  4818. // Let the tool chain choose which runtime library to link.
  4819. getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
  4820. }
  4821. if (!Args.hasArg(options::OPT_nostdlib) &&
  4822. !Args.hasArg(options::OPT_nostartfiles)) {
  4823. // endfile_spec is empty.
  4824. }
  4825. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4826. Args.AddAllArgs(CmdArgs, options::OPT_F);
  4827. const char *Exec =
  4828. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  4829. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4830. }
  4831. void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
  4832. const InputInfo &Output,
  4833. const InputInfoList &Inputs,
  4834. const ArgList &Args,
  4835. const char *LinkingOutput) const {
  4836. ArgStringList CmdArgs;
  4837. CmdArgs.push_back("-create");
  4838. assert(Output.isFilename() && "Unexpected lipo output.");
  4839. CmdArgs.push_back("-output");
  4840. CmdArgs.push_back(Output.getFilename());
  4841. for (InputInfoList::const_iterator
  4842. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4843. const InputInfo &II = *it;
  4844. assert(II.isFilename() && "Unexpected lipo input.");
  4845. CmdArgs.push_back(II.getFilename());
  4846. }
  4847. const char *Exec =
  4848. Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
  4849. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4850. }
  4851. void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
  4852. const InputInfo &Output,
  4853. const InputInfoList &Inputs,
  4854. const ArgList &Args,
  4855. const char *LinkingOutput) const {
  4856. ArgStringList CmdArgs;
  4857. CmdArgs.push_back("-o");
  4858. CmdArgs.push_back(Output.getFilename());
  4859. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  4860. const InputInfo &Input = Inputs[0];
  4861. assert(Input.isFilename() && "Unexpected dsymutil input.");
  4862. CmdArgs.push_back(Input.getFilename());
  4863. const char *Exec =
  4864. Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
  4865. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4866. }
  4867. void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
  4868. const InputInfo &Output,
  4869. const InputInfoList &Inputs,
  4870. const ArgList &Args,
  4871. const char *LinkingOutput) const {
  4872. ArgStringList CmdArgs;
  4873. CmdArgs.push_back("--verify");
  4874. CmdArgs.push_back("--debug-info");
  4875. CmdArgs.push_back("--eh-frame");
  4876. CmdArgs.push_back("--quiet");
  4877. assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
  4878. const InputInfo &Input = Inputs[0];
  4879. assert(Input.isFilename() && "Unexpected verify input");
  4880. // Grabbing the output of the earlier dsymutil run.
  4881. CmdArgs.push_back(Input.getFilename());
  4882. const char *Exec =
  4883. Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
  4884. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4885. }
  4886. void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4887. const InputInfo &Output,
  4888. const InputInfoList &Inputs,
  4889. const ArgList &Args,
  4890. const char *LinkingOutput) const {
  4891. ArgStringList CmdArgs;
  4892. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  4893. options::OPT_Xassembler);
  4894. CmdArgs.push_back("-o");
  4895. CmdArgs.push_back(Output.getFilename());
  4896. for (InputInfoList::const_iterator
  4897. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  4898. const InputInfo &II = *it;
  4899. CmdArgs.push_back(II.getFilename());
  4900. }
  4901. const char *Exec =
  4902. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  4903. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4904. }
  4905. void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
  4906. const InputInfo &Output,
  4907. const InputInfoList &Inputs,
  4908. const ArgList &Args,
  4909. const char *LinkingOutput) const {
  4910. // FIXME: Find a real GCC, don't hard-code versions here
  4911. std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
  4912. const llvm::Triple &T = getToolChain().getTriple();
  4913. std::string LibPath = "/usr/lib/";
  4914. llvm::Triple::ArchType Arch = T.getArch();
  4915. switch (Arch) {
  4916. case llvm::Triple::x86:
  4917. GCCLibPath +=
  4918. ("i386-" + T.getVendorName() + "-" + T.getOSName()).str() + "/4.5.2/";
  4919. break;
  4920. case llvm::Triple::x86_64:
  4921. GCCLibPath += ("i386-" + T.getVendorName() + "-" + T.getOSName()).str();
  4922. GCCLibPath += "/4.5.2/amd64/";
  4923. LibPath += "amd64/";
  4924. break;
  4925. default:
  4926. llvm_unreachable("Unsupported architecture");
  4927. }
  4928. ArgStringList CmdArgs;
  4929. // Demangle C++ names in errors
  4930. CmdArgs.push_back("-C");
  4931. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  4932. (!Args.hasArg(options::OPT_shared))) {
  4933. CmdArgs.push_back("-e");
  4934. CmdArgs.push_back("_start");
  4935. }
  4936. if (Args.hasArg(options::OPT_static)) {
  4937. CmdArgs.push_back("-Bstatic");
  4938. CmdArgs.push_back("-dn");
  4939. } else {
  4940. CmdArgs.push_back("-Bdynamic");
  4941. if (Args.hasArg(options::OPT_shared)) {
  4942. CmdArgs.push_back("-shared");
  4943. } else {
  4944. CmdArgs.push_back("--dynamic-linker");
  4945. CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
  4946. }
  4947. }
  4948. if (Output.isFilename()) {
  4949. CmdArgs.push_back("-o");
  4950. CmdArgs.push_back(Output.getFilename());
  4951. } else {
  4952. assert(Output.isNothing() && "Invalid output.");
  4953. }
  4954. if (!Args.hasArg(options::OPT_nostdlib) &&
  4955. !Args.hasArg(options::OPT_nostartfiles)) {
  4956. if (!Args.hasArg(options::OPT_shared)) {
  4957. CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
  4958. CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
  4959. CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
  4960. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
  4961. } else {
  4962. CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
  4963. CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
  4964. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
  4965. }
  4966. if (getToolChain().getDriver().CCCIsCXX())
  4967. CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
  4968. }
  4969. CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
  4970. Args.AddAllArgs(CmdArgs, options::OPT_L);
  4971. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  4972. Args.AddAllArgs(CmdArgs, options::OPT_e);
  4973. Args.AddAllArgs(CmdArgs, options::OPT_r);
  4974. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  4975. if (!Args.hasArg(options::OPT_nostdlib) &&
  4976. !Args.hasArg(options::OPT_nodefaultlibs)) {
  4977. if (getToolChain().getDriver().CCCIsCXX())
  4978. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  4979. CmdArgs.push_back("-lgcc_s");
  4980. if (!Args.hasArg(options::OPT_shared)) {
  4981. CmdArgs.push_back("-lgcc");
  4982. CmdArgs.push_back("-lc");
  4983. CmdArgs.push_back("-lm");
  4984. }
  4985. }
  4986. if (!Args.hasArg(options::OPT_nostdlib) &&
  4987. !Args.hasArg(options::OPT_nostartfiles)) {
  4988. CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
  4989. }
  4990. CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
  4991. addProfileRT(getToolChain(), Args, CmdArgs);
  4992. const char *Exec =
  4993. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  4994. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  4995. }
  4996. void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  4997. const InputInfo &Output,
  4998. const InputInfoList &Inputs,
  4999. const ArgList &Args,
  5000. const char *LinkingOutput) const {
  5001. ArgStringList CmdArgs;
  5002. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  5003. options::OPT_Xassembler);
  5004. CmdArgs.push_back("-o");
  5005. CmdArgs.push_back(Output.getFilename());
  5006. for (InputInfoList::const_iterator
  5007. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  5008. const InputInfo &II = *it;
  5009. CmdArgs.push_back(II.getFilename());
  5010. }
  5011. const char *Exec =
  5012. Args.MakeArgString(getToolChain().GetProgramPath("gas"));
  5013. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5014. }
  5015. void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
  5016. const InputInfo &Output,
  5017. const InputInfoList &Inputs,
  5018. const ArgList &Args,
  5019. const char *LinkingOutput) const {
  5020. ArgStringList CmdArgs;
  5021. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  5022. (!Args.hasArg(options::OPT_shared))) {
  5023. CmdArgs.push_back("-e");
  5024. CmdArgs.push_back("_start");
  5025. }
  5026. if (Args.hasArg(options::OPT_static)) {
  5027. CmdArgs.push_back("-Bstatic");
  5028. CmdArgs.push_back("-dn");
  5029. } else {
  5030. // CmdArgs.push_back("--eh-frame-hdr");
  5031. CmdArgs.push_back("-Bdynamic");
  5032. if (Args.hasArg(options::OPT_shared)) {
  5033. CmdArgs.push_back("-shared");
  5034. } else {
  5035. CmdArgs.push_back("--dynamic-linker");
  5036. CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
  5037. }
  5038. }
  5039. if (Output.isFilename()) {
  5040. CmdArgs.push_back("-o");
  5041. CmdArgs.push_back(Output.getFilename());
  5042. } else {
  5043. assert(Output.isNothing() && "Invalid output.");
  5044. }
  5045. if (!Args.hasArg(options::OPT_nostdlib) &&
  5046. !Args.hasArg(options::OPT_nostartfiles)) {
  5047. if (!Args.hasArg(options::OPT_shared)) {
  5048. CmdArgs.push_back(Args.MakeArgString(
  5049. getToolChain().GetFilePath("crt1.o")));
  5050. CmdArgs.push_back(Args.MakeArgString(
  5051. getToolChain().GetFilePath("crti.o")));
  5052. CmdArgs.push_back(Args.MakeArgString(
  5053. getToolChain().GetFilePath("crtbegin.o")));
  5054. } else {
  5055. CmdArgs.push_back(Args.MakeArgString(
  5056. getToolChain().GetFilePath("crti.o")));
  5057. }
  5058. CmdArgs.push_back(Args.MakeArgString(
  5059. getToolChain().GetFilePath("crtn.o")));
  5060. }
  5061. CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
  5062. + getToolChain().getTripleString()
  5063. + "/4.2.4"));
  5064. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5065. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5066. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5067. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  5068. if (!Args.hasArg(options::OPT_nostdlib) &&
  5069. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5070. // FIXME: For some reason GCC passes -lgcc before adding
  5071. // the default system libraries. Just mimic this for now.
  5072. CmdArgs.push_back("-lgcc");
  5073. if (Args.hasArg(options::OPT_pthread))
  5074. CmdArgs.push_back("-pthread");
  5075. if (!Args.hasArg(options::OPT_shared))
  5076. CmdArgs.push_back("-lc");
  5077. CmdArgs.push_back("-lgcc");
  5078. }
  5079. if (!Args.hasArg(options::OPT_nostdlib) &&
  5080. !Args.hasArg(options::OPT_nostartfiles)) {
  5081. if (!Args.hasArg(options::OPT_shared))
  5082. CmdArgs.push_back(Args.MakeArgString(
  5083. getToolChain().GetFilePath("crtend.o")));
  5084. }
  5085. addProfileRT(getToolChain(), Args, CmdArgs);
  5086. const char *Exec =
  5087. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  5088. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5089. }
  5090. void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  5091. const InputInfo &Output,
  5092. const InputInfoList &Inputs,
  5093. const ArgList &Args,
  5094. const char *LinkingOutput) const {
  5095. ArgStringList CmdArgs;
  5096. bool NeedsKPIC = false;
  5097. switch (getToolChain().getArch()) {
  5098. case llvm::Triple::x86:
  5099. // When building 32-bit code on OpenBSD/amd64, we have to explicitly
  5100. // instruct as in the base system to assemble 32-bit code.
  5101. CmdArgs.push_back("--32");
  5102. break;
  5103. case llvm::Triple::ppc:
  5104. CmdArgs.push_back("-mppc");
  5105. CmdArgs.push_back("-many");
  5106. break;
  5107. case llvm::Triple::sparc:
  5108. CmdArgs.push_back("-32");
  5109. NeedsKPIC = true;
  5110. break;
  5111. case llvm::Triple::sparcv9:
  5112. CmdArgs.push_back("-64");
  5113. CmdArgs.push_back("-Av9a");
  5114. NeedsKPIC = true;
  5115. break;
  5116. case llvm::Triple::mips64:
  5117. case llvm::Triple::mips64el: {
  5118. StringRef CPUName;
  5119. StringRef ABIName;
  5120. getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  5121. CmdArgs.push_back("-mabi");
  5122. CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
  5123. if (getToolChain().getArch() == llvm::Triple::mips64)
  5124. CmdArgs.push_back("-EB");
  5125. else
  5126. CmdArgs.push_back("-EL");
  5127. NeedsKPIC = true;
  5128. break;
  5129. }
  5130. default:
  5131. break;
  5132. }
  5133. if (NeedsKPIC)
  5134. addAssemblerKPIC(Args, CmdArgs);
  5135. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  5136. options::OPT_Xassembler);
  5137. CmdArgs.push_back("-o");
  5138. CmdArgs.push_back(Output.getFilename());
  5139. for (InputInfoList::const_iterator
  5140. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  5141. const InputInfo &II = *it;
  5142. CmdArgs.push_back(II.getFilename());
  5143. }
  5144. const char *Exec =
  5145. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  5146. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5147. }
  5148. void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
  5149. const InputInfo &Output,
  5150. const InputInfoList &Inputs,
  5151. const ArgList &Args,
  5152. const char *LinkingOutput) const {
  5153. const Driver &D = getToolChain().getDriver();
  5154. ArgStringList CmdArgs;
  5155. // Silence warning for "clang -g foo.o -o foo"
  5156. Args.ClaimAllArgs(options::OPT_g_Group);
  5157. // and "clang -emit-llvm foo.o -o foo"
  5158. Args.ClaimAllArgs(options::OPT_emit_llvm);
  5159. // and for "clang -w foo.o -o foo". Other warning options are already
  5160. // handled somewhere else.
  5161. Args.ClaimAllArgs(options::OPT_w);
  5162. if (getToolChain().getArch() == llvm::Triple::mips64)
  5163. CmdArgs.push_back("-EB");
  5164. else if (getToolChain().getArch() == llvm::Triple::mips64el)
  5165. CmdArgs.push_back("-EL");
  5166. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  5167. (!Args.hasArg(options::OPT_shared))) {
  5168. CmdArgs.push_back("-e");
  5169. CmdArgs.push_back("__start");
  5170. }
  5171. if (Args.hasArg(options::OPT_static)) {
  5172. CmdArgs.push_back("-Bstatic");
  5173. } else {
  5174. if (Args.hasArg(options::OPT_rdynamic))
  5175. CmdArgs.push_back("-export-dynamic");
  5176. CmdArgs.push_back("--eh-frame-hdr");
  5177. CmdArgs.push_back("-Bdynamic");
  5178. if (Args.hasArg(options::OPT_shared)) {
  5179. CmdArgs.push_back("-shared");
  5180. } else {
  5181. CmdArgs.push_back("-dynamic-linker");
  5182. CmdArgs.push_back("/usr/libexec/ld.so");
  5183. }
  5184. }
  5185. if (Args.hasArg(options::OPT_nopie))
  5186. CmdArgs.push_back("-nopie");
  5187. if (Output.isFilename()) {
  5188. CmdArgs.push_back("-o");
  5189. CmdArgs.push_back(Output.getFilename());
  5190. } else {
  5191. assert(Output.isNothing() && "Invalid output.");
  5192. }
  5193. if (!Args.hasArg(options::OPT_nostdlib) &&
  5194. !Args.hasArg(options::OPT_nostartfiles)) {
  5195. if (!Args.hasArg(options::OPT_shared)) {
  5196. if (Args.hasArg(options::OPT_pg))
  5197. CmdArgs.push_back(Args.MakeArgString(
  5198. getToolChain().GetFilePath("gcrt0.o")));
  5199. else
  5200. CmdArgs.push_back(Args.MakeArgString(
  5201. getToolChain().GetFilePath("crt0.o")));
  5202. CmdArgs.push_back(Args.MakeArgString(
  5203. getToolChain().GetFilePath("crtbegin.o")));
  5204. } else {
  5205. CmdArgs.push_back(Args.MakeArgString(
  5206. getToolChain().GetFilePath("crtbeginS.o")));
  5207. }
  5208. }
  5209. std::string Triple = getToolChain().getTripleString();
  5210. if (Triple.substr(0, 6) == "x86_64")
  5211. Triple.replace(0, 6, "amd64");
  5212. CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
  5213. "/4.2.1"));
  5214. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5215. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5216. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5217. Args.AddAllArgs(CmdArgs, options::OPT_s);
  5218. Args.AddAllArgs(CmdArgs, options::OPT_t);
  5219. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  5220. Args.AddAllArgs(CmdArgs, options::OPT_r);
  5221. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  5222. if (!Args.hasArg(options::OPT_nostdlib) &&
  5223. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5224. if (D.CCCIsCXX()) {
  5225. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  5226. if (Args.hasArg(options::OPT_pg))
  5227. CmdArgs.push_back("-lm_p");
  5228. else
  5229. CmdArgs.push_back("-lm");
  5230. }
  5231. // FIXME: For some reason GCC passes -lgcc before adding
  5232. // the default system libraries. Just mimic this for now.
  5233. CmdArgs.push_back("-lgcc");
  5234. if (Args.hasArg(options::OPT_pthread)) {
  5235. if (!Args.hasArg(options::OPT_shared) &&
  5236. Args.hasArg(options::OPT_pg))
  5237. CmdArgs.push_back("-lpthread_p");
  5238. else
  5239. CmdArgs.push_back("-lpthread");
  5240. }
  5241. if (!Args.hasArg(options::OPT_shared)) {
  5242. if (Args.hasArg(options::OPT_pg))
  5243. CmdArgs.push_back("-lc_p");
  5244. else
  5245. CmdArgs.push_back("-lc");
  5246. }
  5247. CmdArgs.push_back("-lgcc");
  5248. }
  5249. if (!Args.hasArg(options::OPT_nostdlib) &&
  5250. !Args.hasArg(options::OPT_nostartfiles)) {
  5251. if (!Args.hasArg(options::OPT_shared))
  5252. CmdArgs.push_back(Args.MakeArgString(
  5253. getToolChain().GetFilePath("crtend.o")));
  5254. else
  5255. CmdArgs.push_back(Args.MakeArgString(
  5256. getToolChain().GetFilePath("crtendS.o")));
  5257. }
  5258. const char *Exec =
  5259. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  5260. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5261. }
  5262. void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  5263. const InputInfo &Output,
  5264. const InputInfoList &Inputs,
  5265. const ArgList &Args,
  5266. const char *LinkingOutput) const {
  5267. ArgStringList CmdArgs;
  5268. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  5269. options::OPT_Xassembler);
  5270. CmdArgs.push_back("-o");
  5271. CmdArgs.push_back(Output.getFilename());
  5272. for (InputInfoList::const_iterator
  5273. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  5274. const InputInfo &II = *it;
  5275. CmdArgs.push_back(II.getFilename());
  5276. }
  5277. const char *Exec =
  5278. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  5279. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5280. }
  5281. void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
  5282. const InputInfo &Output,
  5283. const InputInfoList &Inputs,
  5284. const ArgList &Args,
  5285. const char *LinkingOutput) const {
  5286. const Driver &D = getToolChain().getDriver();
  5287. ArgStringList CmdArgs;
  5288. if ((!Args.hasArg(options::OPT_nostdlib)) &&
  5289. (!Args.hasArg(options::OPT_shared))) {
  5290. CmdArgs.push_back("-e");
  5291. CmdArgs.push_back("__start");
  5292. }
  5293. if (Args.hasArg(options::OPT_static)) {
  5294. CmdArgs.push_back("-Bstatic");
  5295. } else {
  5296. if (Args.hasArg(options::OPT_rdynamic))
  5297. CmdArgs.push_back("-export-dynamic");
  5298. CmdArgs.push_back("--eh-frame-hdr");
  5299. CmdArgs.push_back("-Bdynamic");
  5300. if (Args.hasArg(options::OPT_shared)) {
  5301. CmdArgs.push_back("-shared");
  5302. } else {
  5303. CmdArgs.push_back("-dynamic-linker");
  5304. CmdArgs.push_back("/usr/libexec/ld.so");
  5305. }
  5306. }
  5307. if (Output.isFilename()) {
  5308. CmdArgs.push_back("-o");
  5309. CmdArgs.push_back(Output.getFilename());
  5310. } else {
  5311. assert(Output.isNothing() && "Invalid output.");
  5312. }
  5313. if (!Args.hasArg(options::OPT_nostdlib) &&
  5314. !Args.hasArg(options::OPT_nostartfiles)) {
  5315. if (!Args.hasArg(options::OPT_shared)) {
  5316. if (Args.hasArg(options::OPT_pg))
  5317. CmdArgs.push_back(Args.MakeArgString(
  5318. getToolChain().GetFilePath("gcrt0.o")));
  5319. else
  5320. CmdArgs.push_back(Args.MakeArgString(
  5321. getToolChain().GetFilePath("crt0.o")));
  5322. CmdArgs.push_back(Args.MakeArgString(
  5323. getToolChain().GetFilePath("crtbegin.o")));
  5324. } else {
  5325. CmdArgs.push_back(Args.MakeArgString(
  5326. getToolChain().GetFilePath("crtbeginS.o")));
  5327. }
  5328. }
  5329. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5330. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5331. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5332. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  5333. if (!Args.hasArg(options::OPT_nostdlib) &&
  5334. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5335. if (D.CCCIsCXX()) {
  5336. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  5337. if (Args.hasArg(options::OPT_pg))
  5338. CmdArgs.push_back("-lm_p");
  5339. else
  5340. CmdArgs.push_back("-lm");
  5341. }
  5342. if (Args.hasArg(options::OPT_pthread)) {
  5343. if (!Args.hasArg(options::OPT_shared) &&
  5344. Args.hasArg(options::OPT_pg))
  5345. CmdArgs.push_back("-lpthread_p");
  5346. else
  5347. CmdArgs.push_back("-lpthread");
  5348. }
  5349. if (!Args.hasArg(options::OPT_shared)) {
  5350. if (Args.hasArg(options::OPT_pg))
  5351. CmdArgs.push_back("-lc_p");
  5352. else
  5353. CmdArgs.push_back("-lc");
  5354. }
  5355. StringRef MyArch;
  5356. switch (getToolChain().getTriple().getArch()) {
  5357. case llvm::Triple::arm:
  5358. MyArch = "arm";
  5359. break;
  5360. case llvm::Triple::x86:
  5361. MyArch = "i386";
  5362. break;
  5363. case llvm::Triple::x86_64:
  5364. MyArch = "amd64";
  5365. break;
  5366. default:
  5367. llvm_unreachable("Unsupported architecture");
  5368. }
  5369. CmdArgs.push_back(Args.MakeArgString("-lclang_rt." + MyArch));
  5370. }
  5371. if (!Args.hasArg(options::OPT_nostdlib) &&
  5372. !Args.hasArg(options::OPT_nostartfiles)) {
  5373. if (!Args.hasArg(options::OPT_shared))
  5374. CmdArgs.push_back(Args.MakeArgString(
  5375. getToolChain().GetFilePath("crtend.o")));
  5376. else
  5377. CmdArgs.push_back(Args.MakeArgString(
  5378. getToolChain().GetFilePath("crtendS.o")));
  5379. }
  5380. const char *Exec =
  5381. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  5382. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5383. }
  5384. void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  5385. const InputInfo &Output,
  5386. const InputInfoList &Inputs,
  5387. const ArgList &Args,
  5388. const char *LinkingOutput) const {
  5389. ArgStringList CmdArgs;
  5390. // When building 32-bit code on FreeBSD/amd64, we have to explicitly
  5391. // instruct as in the base system to assemble 32-bit code.
  5392. if (getToolChain().getArch() == llvm::Triple::x86)
  5393. CmdArgs.push_back("--32");
  5394. else if (getToolChain().getArch() == llvm::Triple::ppc)
  5395. CmdArgs.push_back("-a32");
  5396. else if (getToolChain().getArch() == llvm::Triple::mips ||
  5397. getToolChain().getArch() == llvm::Triple::mipsel ||
  5398. getToolChain().getArch() == llvm::Triple::mips64 ||
  5399. getToolChain().getArch() == llvm::Triple::mips64el) {
  5400. StringRef CPUName;
  5401. StringRef ABIName;
  5402. getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  5403. CmdArgs.push_back("-march");
  5404. CmdArgs.push_back(CPUName.data());
  5405. CmdArgs.push_back("-mabi");
  5406. CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
  5407. if (getToolChain().getArch() == llvm::Triple::mips ||
  5408. getToolChain().getArch() == llvm::Triple::mips64)
  5409. CmdArgs.push_back("-EB");
  5410. else
  5411. CmdArgs.push_back("-EL");
  5412. addAssemblerKPIC(Args, CmdArgs);
  5413. } else if (getToolChain().getArch() == llvm::Triple::arm ||
  5414. getToolChain().getArch() == llvm::Triple::armeb ||
  5415. getToolChain().getArch() == llvm::Triple::thumb ||
  5416. getToolChain().getArch() == llvm::Triple::thumbeb) {
  5417. const Driver &D = getToolChain().getDriver();
  5418. const llvm::Triple &Triple = getToolChain().getTriple();
  5419. StringRef FloatABI = arm::getARMFloatABI(D, Args, Triple);
  5420. if (FloatABI == "hard") {
  5421. CmdArgs.push_back("-mfpu=vfp");
  5422. } else {
  5423. CmdArgs.push_back("-mfpu=softvfp");
  5424. }
  5425. switch(getToolChain().getTriple().getEnvironment()) {
  5426. case llvm::Triple::GNUEABIHF:
  5427. case llvm::Triple::GNUEABI:
  5428. case llvm::Triple::EABI:
  5429. CmdArgs.push_back("-meabi=5");
  5430. break;
  5431. default:
  5432. CmdArgs.push_back("-matpcs");
  5433. }
  5434. } else if (getToolChain().getArch() == llvm::Triple::sparc ||
  5435. getToolChain().getArch() == llvm::Triple::sparcv9) {
  5436. if (getToolChain().getArch() == llvm::Triple::sparc)
  5437. CmdArgs.push_back("-Av8plusa");
  5438. else
  5439. CmdArgs.push_back("-Av9a");
  5440. addAssemblerKPIC(Args, CmdArgs);
  5441. }
  5442. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  5443. options::OPT_Xassembler);
  5444. CmdArgs.push_back("-o");
  5445. CmdArgs.push_back(Output.getFilename());
  5446. for (InputInfoList::const_iterator
  5447. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  5448. const InputInfo &II = *it;
  5449. CmdArgs.push_back(II.getFilename());
  5450. }
  5451. const char *Exec =
  5452. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  5453. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5454. }
  5455. void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
  5456. const InputInfo &Output,
  5457. const InputInfoList &Inputs,
  5458. const ArgList &Args,
  5459. const char *LinkingOutput) const {
  5460. const toolchains::FreeBSD& ToolChain =
  5461. static_cast<const toolchains::FreeBSD&>(getToolChain());
  5462. const Driver &D = ToolChain.getDriver();
  5463. const bool IsPIE =
  5464. !Args.hasArg(options::OPT_shared) &&
  5465. (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
  5466. ArgStringList CmdArgs;
  5467. // Silence warning for "clang -g foo.o -o foo"
  5468. Args.ClaimAllArgs(options::OPT_g_Group);
  5469. // and "clang -emit-llvm foo.o -o foo"
  5470. Args.ClaimAllArgs(options::OPT_emit_llvm);
  5471. // and for "clang -w foo.o -o foo". Other warning options are already
  5472. // handled somewhere else.
  5473. Args.ClaimAllArgs(options::OPT_w);
  5474. if (!D.SysRoot.empty())
  5475. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  5476. if (IsPIE)
  5477. CmdArgs.push_back("-pie");
  5478. if (Args.hasArg(options::OPT_static)) {
  5479. CmdArgs.push_back("-Bstatic");
  5480. } else {
  5481. if (Args.hasArg(options::OPT_rdynamic))
  5482. CmdArgs.push_back("-export-dynamic");
  5483. CmdArgs.push_back("--eh-frame-hdr");
  5484. if (Args.hasArg(options::OPT_shared)) {
  5485. CmdArgs.push_back("-Bshareable");
  5486. } else {
  5487. CmdArgs.push_back("-dynamic-linker");
  5488. CmdArgs.push_back("/libexec/ld-elf.so.1");
  5489. }
  5490. if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
  5491. llvm::Triple::ArchType Arch = ToolChain.getArch();
  5492. if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
  5493. Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
  5494. CmdArgs.push_back("--hash-style=both");
  5495. }
  5496. }
  5497. CmdArgs.push_back("--enable-new-dtags");
  5498. }
  5499. // When building 32-bit code on FreeBSD/amd64, we have to explicitly
  5500. // instruct ld in the base system to link 32-bit code.
  5501. if (ToolChain.getArch() == llvm::Triple::x86) {
  5502. CmdArgs.push_back("-m");
  5503. CmdArgs.push_back("elf_i386_fbsd");
  5504. }
  5505. if (ToolChain.getArch() == llvm::Triple::ppc) {
  5506. CmdArgs.push_back("-m");
  5507. CmdArgs.push_back("elf32ppc_fbsd");
  5508. }
  5509. if (Output.isFilename()) {
  5510. CmdArgs.push_back("-o");
  5511. CmdArgs.push_back(Output.getFilename());
  5512. } else {
  5513. assert(Output.isNothing() && "Invalid output.");
  5514. }
  5515. if (!Args.hasArg(options::OPT_nostdlib) &&
  5516. !Args.hasArg(options::OPT_nostartfiles)) {
  5517. const char *crt1 = nullptr;
  5518. if (!Args.hasArg(options::OPT_shared)) {
  5519. if (Args.hasArg(options::OPT_pg))
  5520. crt1 = "gcrt1.o";
  5521. else if (IsPIE)
  5522. crt1 = "Scrt1.o";
  5523. else
  5524. crt1 = "crt1.o";
  5525. }
  5526. if (crt1)
  5527. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
  5528. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  5529. const char *crtbegin = nullptr;
  5530. if (Args.hasArg(options::OPT_static))
  5531. crtbegin = "crtbeginT.o";
  5532. else if (Args.hasArg(options::OPT_shared) || IsPIE)
  5533. crtbegin = "crtbeginS.o";
  5534. else
  5535. crtbegin = "crtbegin.o";
  5536. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  5537. }
  5538. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5539. const ToolChain::path_list Paths = ToolChain.getFilePaths();
  5540. for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
  5541. i != e; ++i)
  5542. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
  5543. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5544. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5545. Args.AddAllArgs(CmdArgs, options::OPT_s);
  5546. Args.AddAllArgs(CmdArgs, options::OPT_t);
  5547. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  5548. Args.AddAllArgs(CmdArgs, options::OPT_r);
  5549. if (D.IsUsingLTO(Args))
  5550. AddGoldPlugin(ToolChain, Args, CmdArgs);
  5551. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  5552. if (!Args.hasArg(options::OPT_nostdlib) &&
  5553. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5554. if (D.CCCIsCXX()) {
  5555. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  5556. if (Args.hasArg(options::OPT_pg))
  5557. CmdArgs.push_back("-lm_p");
  5558. else
  5559. CmdArgs.push_back("-lm");
  5560. }
  5561. // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
  5562. // the default system libraries. Just mimic this for now.
  5563. if (Args.hasArg(options::OPT_pg))
  5564. CmdArgs.push_back("-lgcc_p");
  5565. else
  5566. CmdArgs.push_back("-lgcc");
  5567. if (Args.hasArg(options::OPT_static)) {
  5568. CmdArgs.push_back("-lgcc_eh");
  5569. } else if (Args.hasArg(options::OPT_pg)) {
  5570. CmdArgs.push_back("-lgcc_eh_p");
  5571. } else {
  5572. CmdArgs.push_back("--as-needed");
  5573. CmdArgs.push_back("-lgcc_s");
  5574. CmdArgs.push_back("--no-as-needed");
  5575. }
  5576. if (Args.hasArg(options::OPT_pthread)) {
  5577. if (Args.hasArg(options::OPT_pg))
  5578. CmdArgs.push_back("-lpthread_p");
  5579. else
  5580. CmdArgs.push_back("-lpthread");
  5581. }
  5582. if (Args.hasArg(options::OPT_pg)) {
  5583. if (Args.hasArg(options::OPT_shared))
  5584. CmdArgs.push_back("-lc");
  5585. else
  5586. CmdArgs.push_back("-lc_p");
  5587. CmdArgs.push_back("-lgcc_p");
  5588. } else {
  5589. CmdArgs.push_back("-lc");
  5590. CmdArgs.push_back("-lgcc");
  5591. }
  5592. if (Args.hasArg(options::OPT_static)) {
  5593. CmdArgs.push_back("-lgcc_eh");
  5594. } else if (Args.hasArg(options::OPT_pg)) {
  5595. CmdArgs.push_back("-lgcc_eh_p");
  5596. } else {
  5597. CmdArgs.push_back("--as-needed");
  5598. CmdArgs.push_back("-lgcc_s");
  5599. CmdArgs.push_back("--no-as-needed");
  5600. }
  5601. }
  5602. if (!Args.hasArg(options::OPT_nostdlib) &&
  5603. !Args.hasArg(options::OPT_nostartfiles)) {
  5604. if (Args.hasArg(options::OPT_shared) || IsPIE)
  5605. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
  5606. else
  5607. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
  5608. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  5609. }
  5610. addSanitizerRuntimes(getToolChain(), Args, CmdArgs);
  5611. addProfileRT(ToolChain, Args, CmdArgs);
  5612. const char *Exec =
  5613. Args.MakeArgString(ToolChain.GetProgramPath("ld"));
  5614. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5615. }
  5616. void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  5617. const InputInfo &Output,
  5618. const InputInfoList &Inputs,
  5619. const ArgList &Args,
  5620. const char *LinkingOutput) const {
  5621. ArgStringList CmdArgs;
  5622. // GNU as needs different flags for creating the correct output format
  5623. // on architectures with different ABIs or optional feature sets.
  5624. switch (getToolChain().getArch()) {
  5625. case llvm::Triple::x86:
  5626. CmdArgs.push_back("--32");
  5627. break;
  5628. case llvm::Triple::arm:
  5629. case llvm::Triple::armeb:
  5630. case llvm::Triple::thumb:
  5631. case llvm::Triple::thumbeb: {
  5632. std::string MArch(arm::getARMTargetCPU(Args, getToolChain().getTriple()));
  5633. CmdArgs.push_back(Args.MakeArgString("-mcpu=" + MArch));
  5634. break;
  5635. }
  5636. case llvm::Triple::mips:
  5637. case llvm::Triple::mipsel:
  5638. case llvm::Triple::mips64:
  5639. case llvm::Triple::mips64el: {
  5640. StringRef CPUName;
  5641. StringRef ABIName;
  5642. getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  5643. CmdArgs.push_back("-march");
  5644. CmdArgs.push_back(CPUName.data());
  5645. CmdArgs.push_back("-mabi");
  5646. CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
  5647. if (getToolChain().getArch() == llvm::Triple::mips ||
  5648. getToolChain().getArch() == llvm::Triple::mips64)
  5649. CmdArgs.push_back("-EB");
  5650. else
  5651. CmdArgs.push_back("-EL");
  5652. addAssemblerKPIC(Args, CmdArgs);
  5653. break;
  5654. }
  5655. case llvm::Triple::sparc:
  5656. CmdArgs.push_back("-32");
  5657. addAssemblerKPIC(Args, CmdArgs);
  5658. break;
  5659. case llvm::Triple::sparcv9:
  5660. CmdArgs.push_back("-64");
  5661. CmdArgs.push_back("-Av9");
  5662. addAssemblerKPIC(Args, CmdArgs);
  5663. break;
  5664. default:
  5665. break;
  5666. }
  5667. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  5668. options::OPT_Xassembler);
  5669. CmdArgs.push_back("-o");
  5670. CmdArgs.push_back(Output.getFilename());
  5671. for (InputInfoList::const_iterator
  5672. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  5673. const InputInfo &II = *it;
  5674. CmdArgs.push_back(II.getFilename());
  5675. }
  5676. const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
  5677. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5678. }
  5679. void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
  5680. const InputInfo &Output,
  5681. const InputInfoList &Inputs,
  5682. const ArgList &Args,
  5683. const char *LinkingOutput) const {
  5684. const Driver &D = getToolChain().getDriver();
  5685. ArgStringList CmdArgs;
  5686. if (!D.SysRoot.empty())
  5687. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  5688. CmdArgs.push_back("--eh-frame-hdr");
  5689. if (Args.hasArg(options::OPT_static)) {
  5690. CmdArgs.push_back("-Bstatic");
  5691. } else {
  5692. if (Args.hasArg(options::OPT_rdynamic))
  5693. CmdArgs.push_back("-export-dynamic");
  5694. if (Args.hasArg(options::OPT_shared)) {
  5695. CmdArgs.push_back("-Bshareable");
  5696. } else {
  5697. CmdArgs.push_back("-dynamic-linker");
  5698. CmdArgs.push_back("/libexec/ld.elf_so");
  5699. }
  5700. }
  5701. // Many NetBSD architectures support more than one ABI.
  5702. // Determine the correct emulation for ld.
  5703. switch (getToolChain().getArch()) {
  5704. case llvm::Triple::x86:
  5705. CmdArgs.push_back("-m");
  5706. CmdArgs.push_back("elf_i386");
  5707. break;
  5708. case llvm::Triple::arm:
  5709. case llvm::Triple::armeb:
  5710. case llvm::Triple::thumb:
  5711. case llvm::Triple::thumbeb:
  5712. CmdArgs.push_back("-m");
  5713. switch (getToolChain().getTriple().getEnvironment()) {
  5714. case llvm::Triple::EABI:
  5715. case llvm::Triple::GNUEABI:
  5716. CmdArgs.push_back("armelf_nbsd_eabi");
  5717. break;
  5718. case llvm::Triple::EABIHF:
  5719. case llvm::Triple::GNUEABIHF:
  5720. CmdArgs.push_back("armelf_nbsd_eabihf");
  5721. break;
  5722. default:
  5723. CmdArgs.push_back("armelf_nbsd");
  5724. break;
  5725. }
  5726. break;
  5727. case llvm::Triple::mips64:
  5728. case llvm::Triple::mips64el:
  5729. if (mips::hasMipsAbiArg(Args, "32")) {
  5730. CmdArgs.push_back("-m");
  5731. if (getToolChain().getArch() == llvm::Triple::mips64)
  5732. CmdArgs.push_back("elf32btsmip");
  5733. else
  5734. CmdArgs.push_back("elf32ltsmip");
  5735. } else if (mips::hasMipsAbiArg(Args, "64")) {
  5736. CmdArgs.push_back("-m");
  5737. if (getToolChain().getArch() == llvm::Triple::mips64)
  5738. CmdArgs.push_back("elf64btsmip");
  5739. else
  5740. CmdArgs.push_back("elf64ltsmip");
  5741. }
  5742. break;
  5743. case llvm::Triple::sparc:
  5744. CmdArgs.push_back("-m");
  5745. CmdArgs.push_back("elf32_sparc");
  5746. break;
  5747. case llvm::Triple::sparcv9:
  5748. CmdArgs.push_back("-m");
  5749. CmdArgs.push_back("elf64_sparc");
  5750. break;
  5751. default:
  5752. break;
  5753. }
  5754. if (Output.isFilename()) {
  5755. CmdArgs.push_back("-o");
  5756. CmdArgs.push_back(Output.getFilename());
  5757. } else {
  5758. assert(Output.isNothing() && "Invalid output.");
  5759. }
  5760. if (!Args.hasArg(options::OPT_nostdlib) &&
  5761. !Args.hasArg(options::OPT_nostartfiles)) {
  5762. if (!Args.hasArg(options::OPT_shared)) {
  5763. CmdArgs.push_back(Args.MakeArgString(
  5764. getToolChain().GetFilePath("crt0.o")));
  5765. CmdArgs.push_back(Args.MakeArgString(
  5766. getToolChain().GetFilePath("crti.o")));
  5767. CmdArgs.push_back(Args.MakeArgString(
  5768. getToolChain().GetFilePath("crtbegin.o")));
  5769. } else {
  5770. CmdArgs.push_back(Args.MakeArgString(
  5771. getToolChain().GetFilePath("crti.o")));
  5772. CmdArgs.push_back(Args.MakeArgString(
  5773. getToolChain().GetFilePath("crtbeginS.o")));
  5774. }
  5775. }
  5776. Args.AddAllArgs(CmdArgs, options::OPT_L);
  5777. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  5778. Args.AddAllArgs(CmdArgs, options::OPT_e);
  5779. Args.AddAllArgs(CmdArgs, options::OPT_s);
  5780. Args.AddAllArgs(CmdArgs, options::OPT_t);
  5781. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  5782. Args.AddAllArgs(CmdArgs, options::OPT_r);
  5783. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  5784. unsigned Major, Minor, Micro;
  5785. getToolChain().getTriple().getOSVersion(Major, Minor, Micro);
  5786. bool useLibgcc = true;
  5787. if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 40) || Major == 0) {
  5788. switch(getToolChain().getArch()) {
  5789. case llvm::Triple::arm:
  5790. case llvm::Triple::armeb:
  5791. case llvm::Triple::thumb:
  5792. case llvm::Triple::thumbeb:
  5793. case llvm::Triple::x86:
  5794. case llvm::Triple::x86_64:
  5795. useLibgcc = false;
  5796. break;
  5797. default:
  5798. break;
  5799. }
  5800. }
  5801. if (!Args.hasArg(options::OPT_nostdlib) &&
  5802. !Args.hasArg(options::OPT_nodefaultlibs)) {
  5803. if (D.CCCIsCXX()) {
  5804. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  5805. CmdArgs.push_back("-lm");
  5806. }
  5807. if (Args.hasArg(options::OPT_pthread))
  5808. CmdArgs.push_back("-lpthread");
  5809. CmdArgs.push_back("-lc");
  5810. if (useLibgcc) {
  5811. if (Args.hasArg(options::OPT_static)) {
  5812. // libgcc_eh depends on libc, so resolve as much as possible,
  5813. // pull in any new requirements from libc and then get the rest
  5814. // of libgcc.
  5815. CmdArgs.push_back("-lgcc_eh");
  5816. CmdArgs.push_back("-lc");
  5817. CmdArgs.push_back("-lgcc");
  5818. } else {
  5819. CmdArgs.push_back("-lgcc");
  5820. CmdArgs.push_back("--as-needed");
  5821. CmdArgs.push_back("-lgcc_s");
  5822. CmdArgs.push_back("--no-as-needed");
  5823. }
  5824. }
  5825. }
  5826. if (!Args.hasArg(options::OPT_nostdlib) &&
  5827. !Args.hasArg(options::OPT_nostartfiles)) {
  5828. if (!Args.hasArg(options::OPT_shared))
  5829. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  5830. "crtend.o")));
  5831. else
  5832. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  5833. "crtendS.o")));
  5834. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
  5835. "crtn.o")));
  5836. }
  5837. addProfileRT(getToolChain(), Args, CmdArgs);
  5838. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  5839. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5840. }
  5841. void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  5842. const InputInfo &Output,
  5843. const InputInfoList &Inputs,
  5844. const ArgList &Args,
  5845. const char *LinkingOutput) const {
  5846. ArgStringList CmdArgs;
  5847. bool NeedsKPIC = false;
  5848. // Add --32/--64 to make sure we get the format we want.
  5849. // This is incomplete
  5850. if (getToolChain().getArch() == llvm::Triple::x86) {
  5851. CmdArgs.push_back("--32");
  5852. } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
  5853. CmdArgs.push_back("--64");
  5854. } else if (getToolChain().getArch() == llvm::Triple::ppc) {
  5855. CmdArgs.push_back("-a32");
  5856. CmdArgs.push_back("-mppc");
  5857. CmdArgs.push_back("-many");
  5858. } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
  5859. CmdArgs.push_back("-a64");
  5860. CmdArgs.push_back("-mppc64");
  5861. CmdArgs.push_back("-many");
  5862. } else if (getToolChain().getArch() == llvm::Triple::ppc64le) {
  5863. CmdArgs.push_back("-a64");
  5864. CmdArgs.push_back("-mppc64");
  5865. CmdArgs.push_back("-many");
  5866. CmdArgs.push_back("-mlittle-endian");
  5867. } else if (getToolChain().getArch() == llvm::Triple::sparc) {
  5868. CmdArgs.push_back("-32");
  5869. CmdArgs.push_back("-Av8plusa");
  5870. NeedsKPIC = true;
  5871. } else if (getToolChain().getArch() == llvm::Triple::sparcv9) {
  5872. CmdArgs.push_back("-64");
  5873. CmdArgs.push_back("-Av9a");
  5874. NeedsKPIC = true;
  5875. } else if (getToolChain().getArch() == llvm::Triple::arm ||
  5876. getToolChain().getArch() == llvm::Triple::armeb) {
  5877. StringRef MArch = getToolChain().getArchName();
  5878. if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
  5879. CmdArgs.push_back("-mfpu=neon");
  5880. if (MArch == "armv8" || MArch == "armv8a" || MArch == "armv8-a" ||
  5881. MArch == "armebv8" || MArch == "armebv8a" || MArch == "armebv8-a")
  5882. CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8");
  5883. StringRef ARMFloatABI = tools::arm::getARMFloatABI(
  5884. getToolChain().getDriver(), Args, getToolChain().getTriple());
  5885. CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
  5886. Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
  5887. // FIXME: remove krait check when GNU tools support krait cpu
  5888. // for now replace it with -march=armv7-a to avoid a lower
  5889. // march from being picked in the absence of a cpu flag.
  5890. Arg *A;
  5891. if ((A = Args.getLastArg(options::OPT_mcpu_EQ)) &&
  5892. StringRef(A->getValue()) == "krait")
  5893. CmdArgs.push_back("-march=armv7-a");
  5894. else
  5895. Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
  5896. Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
  5897. } else if (getToolChain().getArch() == llvm::Triple::mips ||
  5898. getToolChain().getArch() == llvm::Triple::mipsel ||
  5899. getToolChain().getArch() == llvm::Triple::mips64 ||
  5900. getToolChain().getArch() == llvm::Triple::mips64el) {
  5901. StringRef CPUName;
  5902. StringRef ABIName;
  5903. getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  5904. CmdArgs.push_back("-march");
  5905. CmdArgs.push_back(CPUName.data());
  5906. CmdArgs.push_back("-mabi");
  5907. CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
  5908. if (getToolChain().getArch() == llvm::Triple::mips ||
  5909. getToolChain().getArch() == llvm::Triple::mips64)
  5910. CmdArgs.push_back("-EB");
  5911. else
  5912. CmdArgs.push_back("-EL");
  5913. if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
  5914. if (StringRef(A->getValue()) == "2008")
  5915. CmdArgs.push_back(Args.MakeArgString("-mnan=2008"));
  5916. }
  5917. Args.AddLastArg(CmdArgs, options::OPT_mfp32, options::OPT_mfp64);
  5918. Args.AddLastArg(CmdArgs, options::OPT_mips16, options::OPT_mno_mips16);
  5919. Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
  5920. options::OPT_mno_micromips);
  5921. Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
  5922. Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
  5923. if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) {
  5924. // Do not use AddLastArg because not all versions of MIPS assembler
  5925. // support -mmsa / -mno-msa options.
  5926. if (A->getOption().matches(options::OPT_mmsa))
  5927. CmdArgs.push_back(Args.MakeArgString("-mmsa"));
  5928. }
  5929. NeedsKPIC = true;
  5930. } else if (getToolChain().getArch() == llvm::Triple::systemz) {
  5931. // Always pass an -march option, since our default of z10 is later
  5932. // than the GNU assembler's default.
  5933. StringRef CPUName = getSystemZTargetCPU(Args);
  5934. CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
  5935. }
  5936. if (NeedsKPIC)
  5937. addAssemblerKPIC(Args, CmdArgs);
  5938. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  5939. options::OPT_Xassembler);
  5940. CmdArgs.push_back("-o");
  5941. CmdArgs.push_back(Output.getFilename());
  5942. for (InputInfoList::const_iterator
  5943. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  5944. const InputInfo &II = *it;
  5945. CmdArgs.push_back(II.getFilename());
  5946. }
  5947. const char *Exec =
  5948. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  5949. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  5950. // Handle the debug info splitting at object creation time if we're
  5951. // creating an object.
  5952. // TODO: Currently only works on linux with newer objcopy.
  5953. if (Args.hasArg(options::OPT_gsplit_dwarf) &&
  5954. getToolChain().getTriple().isOSLinux())
  5955. SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
  5956. SplitDebugName(Args, Inputs));
  5957. }
  5958. static void AddLibgcc(const llvm::Triple &Triple, const Driver &D,
  5959. ArgStringList &CmdArgs, const ArgList &Args) {
  5960. bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
  5961. bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
  5962. Args.hasArg(options::OPT_static);
  5963. if (!D.CCCIsCXX())
  5964. CmdArgs.push_back("-lgcc");
  5965. if (StaticLibgcc || isAndroid) {
  5966. if (D.CCCIsCXX())
  5967. CmdArgs.push_back("-lgcc");
  5968. } else {
  5969. if (!D.CCCIsCXX())
  5970. CmdArgs.push_back("--as-needed");
  5971. CmdArgs.push_back("-lgcc_s");
  5972. if (!D.CCCIsCXX())
  5973. CmdArgs.push_back("--no-as-needed");
  5974. }
  5975. if (StaticLibgcc && !isAndroid)
  5976. CmdArgs.push_back("-lgcc_eh");
  5977. else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
  5978. CmdArgs.push_back("-lgcc");
  5979. // According to Android ABI, we have to link with libdl if we are
  5980. // linking with non-static libgcc.
  5981. //
  5982. // NOTE: This fixes a link error on Android MIPS as well. The non-static
  5983. // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
  5984. if (isAndroid && !StaticLibgcc)
  5985. CmdArgs.push_back("-ldl");
  5986. }
  5987. static StringRef getLinuxDynamicLinker(const ArgList &Args,
  5988. const toolchains::Linux &ToolChain) {
  5989. if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android) {
  5990. if (ToolChain.getTriple().isArch64Bit())
  5991. return "/system/bin/linker64";
  5992. else
  5993. return "/system/bin/linker";
  5994. } else if (ToolChain.getArch() == llvm::Triple::x86 ||
  5995. ToolChain.getArch() == llvm::Triple::sparc)
  5996. return "/lib/ld-linux.so.2";
  5997. else if (ToolChain.getArch() == llvm::Triple::aarch64 ||
  5998. ToolChain.getArch() == llvm::Triple::arm64)
  5999. return "/lib/ld-linux-aarch64.so.1";
  6000. else if (ToolChain.getArch() == llvm::Triple::aarch64_be ||
  6001. ToolChain.getArch() == llvm::Triple::arm64_be)
  6002. return "/lib/ld-linux-aarch64_be.so.1";
  6003. else if (ToolChain.getArch() == llvm::Triple::arm ||
  6004. ToolChain.getArch() == llvm::Triple::thumb) {
  6005. if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
  6006. return "/lib/ld-linux-armhf.so.3";
  6007. else
  6008. return "/lib/ld-linux.so.3";
  6009. } else if (ToolChain.getArch() == llvm::Triple::armeb ||
  6010. ToolChain.getArch() == llvm::Triple::thumbeb) {
  6011. if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
  6012. return "/lib/ld-linux-armhf.so.3"; /* TODO: check which dynamic linker name. */
  6013. else
  6014. return "/lib/ld-linux.so.3"; /* TODO: check which dynamic linker name. */
  6015. } else if (ToolChain.getArch() == llvm::Triple::mips ||
  6016. ToolChain.getArch() == llvm::Triple::mipsel)
  6017. return "/lib/ld.so.1";
  6018. else if (ToolChain.getArch() == llvm::Triple::mips64 ||
  6019. ToolChain.getArch() == llvm::Triple::mips64el) {
  6020. if (mips::hasMipsAbiArg(Args, "n32"))
  6021. return "/lib32/ld.so.1";
  6022. else
  6023. return "/lib64/ld.so.1";
  6024. } else if (ToolChain.getArch() == llvm::Triple::ppc)
  6025. return "/lib/ld.so.1";
  6026. else if (ToolChain.getArch() == llvm::Triple::ppc64 ||
  6027. ToolChain.getArch() == llvm::Triple::ppc64le ||
  6028. ToolChain.getArch() == llvm::Triple::systemz)
  6029. return "/lib64/ld64.so.1";
  6030. else if (ToolChain.getArch() == llvm::Triple::sparcv9)
  6031. return "/lib64/ld-linux.so.2";
  6032. else
  6033. return "/lib64/ld-linux-x86-64.so.2";
  6034. }
  6035. static void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
  6036. ArgStringList &CmdArgs, const ArgList &Args) {
  6037. // Make use of compiler-rt if --rtlib option is used
  6038. ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args);
  6039. switch(RLT) {
  6040. case ToolChain::RLT_CompilerRT:
  6041. addClangRTLinux(TC, Args, CmdArgs);
  6042. break;
  6043. case ToolChain::RLT_Libgcc:
  6044. AddLibgcc(TC.getTriple(), D, CmdArgs, Args);
  6045. break;
  6046. }
  6047. }
  6048. void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
  6049. const InputInfo &Output,
  6050. const InputInfoList &Inputs,
  6051. const ArgList &Args,
  6052. const char *LinkingOutput) const {
  6053. const toolchains::Linux& ToolChain =
  6054. static_cast<const toolchains::Linux&>(getToolChain());
  6055. const Driver &D = ToolChain.getDriver();
  6056. const bool isAndroid =
  6057. ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
  6058. const bool IsPIE =
  6059. !Args.hasArg(options::OPT_shared) &&
  6060. !Args.hasArg(options::OPT_static) &&
  6061. (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault() ||
  6062. // On Android every code is PIC so every executable is PIE
  6063. // Cannot use isPIEDefault here since otherwise
  6064. // PIE only logic will be enabled during compilation
  6065. isAndroid);
  6066. ArgStringList CmdArgs;
  6067. // Silence warning for "clang -g foo.o -o foo"
  6068. Args.ClaimAllArgs(options::OPT_g_Group);
  6069. // and "clang -emit-llvm foo.o -o foo"
  6070. Args.ClaimAllArgs(options::OPT_emit_llvm);
  6071. // and for "clang -w foo.o -o foo". Other warning options are already
  6072. // handled somewhere else.
  6073. Args.ClaimAllArgs(options::OPT_w);
  6074. if (!D.SysRoot.empty())
  6075. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  6076. if (IsPIE)
  6077. CmdArgs.push_back("-pie");
  6078. if (Args.hasArg(options::OPT_rdynamic))
  6079. CmdArgs.push_back("-export-dynamic");
  6080. if (Args.hasArg(options::OPT_s))
  6081. CmdArgs.push_back("-s");
  6082. for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
  6083. e = ToolChain.ExtraOpts.end();
  6084. i != e; ++i)
  6085. CmdArgs.push_back(i->c_str());
  6086. if (!Args.hasArg(options::OPT_static)) {
  6087. CmdArgs.push_back("--eh-frame-hdr");
  6088. }
  6089. CmdArgs.push_back("-m");
  6090. if (ToolChain.getArch() == llvm::Triple::x86)
  6091. CmdArgs.push_back("elf_i386");
  6092. else if (ToolChain.getArch() == llvm::Triple::aarch64 ||
  6093. ToolChain.getArch() == llvm::Triple::arm64)
  6094. CmdArgs.push_back("aarch64linux");
  6095. else if (ToolChain.getArch() == llvm::Triple::aarch64_be ||
  6096. ToolChain.getArch() == llvm::Triple::arm64_be)
  6097. CmdArgs.push_back("aarch64_be_linux");
  6098. else if (ToolChain.getArch() == llvm::Triple::arm
  6099. || ToolChain.getArch() == llvm::Triple::thumb)
  6100. CmdArgs.push_back("armelf_linux_eabi");
  6101. else if (ToolChain.getArch() == llvm::Triple::armeb
  6102. || ToolChain.getArch() == llvm::Triple::thumbeb)
  6103. CmdArgs.push_back("armebelf_linux_eabi"); /* TODO: check which NAME. */
  6104. else if (ToolChain.getArch() == llvm::Triple::ppc)
  6105. CmdArgs.push_back("elf32ppclinux");
  6106. else if (ToolChain.getArch() == llvm::Triple::ppc64)
  6107. CmdArgs.push_back("elf64ppc");
  6108. else if (ToolChain.getArch() == llvm::Triple::ppc64le)
  6109. CmdArgs.push_back("elf64lppc");
  6110. else if (ToolChain.getArch() == llvm::Triple::sparc)
  6111. CmdArgs.push_back("elf32_sparc");
  6112. else if (ToolChain.getArch() == llvm::Triple::sparcv9)
  6113. CmdArgs.push_back("elf64_sparc");
  6114. else if (ToolChain.getArch() == llvm::Triple::mips)
  6115. CmdArgs.push_back("elf32btsmip");
  6116. else if (ToolChain.getArch() == llvm::Triple::mipsel)
  6117. CmdArgs.push_back("elf32ltsmip");
  6118. else if (ToolChain.getArch() == llvm::Triple::mips64) {
  6119. if (mips::hasMipsAbiArg(Args, "n32"))
  6120. CmdArgs.push_back("elf32btsmipn32");
  6121. else
  6122. CmdArgs.push_back("elf64btsmip");
  6123. }
  6124. else if (ToolChain.getArch() == llvm::Triple::mips64el) {
  6125. if (mips::hasMipsAbiArg(Args, "n32"))
  6126. CmdArgs.push_back("elf32ltsmipn32");
  6127. else
  6128. CmdArgs.push_back("elf64ltsmip");
  6129. }
  6130. else if (ToolChain.getArch() == llvm::Triple::systemz)
  6131. CmdArgs.push_back("elf64_s390");
  6132. else
  6133. CmdArgs.push_back("elf_x86_64");
  6134. if (Args.hasArg(options::OPT_static)) {
  6135. if (ToolChain.getArch() == llvm::Triple::arm ||
  6136. ToolChain.getArch() == llvm::Triple::armeb ||
  6137. ToolChain.getArch() == llvm::Triple::thumb ||
  6138. ToolChain.getArch() == llvm::Triple::thumbeb)
  6139. CmdArgs.push_back("-Bstatic");
  6140. else
  6141. CmdArgs.push_back("-static");
  6142. } else if (Args.hasArg(options::OPT_shared)) {
  6143. CmdArgs.push_back("-shared");
  6144. if (isAndroid) {
  6145. CmdArgs.push_back("-Bsymbolic");
  6146. }
  6147. }
  6148. if (ToolChain.getArch() == llvm::Triple::arm ||
  6149. ToolChain.getArch() == llvm::Triple::armeb ||
  6150. ToolChain.getArch() == llvm::Triple::thumb ||
  6151. ToolChain.getArch() == llvm::Triple::thumbeb ||
  6152. (!Args.hasArg(options::OPT_static) &&
  6153. !Args.hasArg(options::OPT_shared))) {
  6154. CmdArgs.push_back("-dynamic-linker");
  6155. CmdArgs.push_back(Args.MakeArgString(
  6156. D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain)));
  6157. }
  6158. CmdArgs.push_back("-o");
  6159. CmdArgs.push_back(Output.getFilename());
  6160. if (!Args.hasArg(options::OPT_nostdlib) &&
  6161. !Args.hasArg(options::OPT_nostartfiles)) {
  6162. if (!isAndroid) {
  6163. const char *crt1 = nullptr;
  6164. if (!Args.hasArg(options::OPT_shared)){
  6165. if (Args.hasArg(options::OPT_pg))
  6166. crt1 = "gcrt1.o";
  6167. else if (IsPIE)
  6168. crt1 = "Scrt1.o";
  6169. else
  6170. crt1 = "crt1.o";
  6171. }
  6172. if (crt1)
  6173. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
  6174. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  6175. }
  6176. const char *crtbegin;
  6177. if (Args.hasArg(options::OPT_static))
  6178. crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
  6179. else if (Args.hasArg(options::OPT_shared))
  6180. crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
  6181. else if (IsPIE)
  6182. crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
  6183. else
  6184. crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
  6185. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  6186. // Add crtfastmath.o if available and fast math is enabled.
  6187. ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
  6188. }
  6189. Args.AddAllArgs(CmdArgs, options::OPT_L);
  6190. const ToolChain::path_list Paths = ToolChain.getFilePaths();
  6191. for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
  6192. i != e; ++i)
  6193. CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
  6194. if (D.IsUsingLTO(Args))
  6195. AddGoldPlugin(ToolChain, Args, CmdArgs);
  6196. if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
  6197. CmdArgs.push_back("--no-demangle");
  6198. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
  6199. addSanitizerRuntimes(getToolChain(), Args, CmdArgs);
  6200. // The profile runtime also needs access to system libraries.
  6201. addProfileRT(getToolChain(), Args, CmdArgs);
  6202. if (D.CCCIsCXX() &&
  6203. !Args.hasArg(options::OPT_nostdlib) &&
  6204. !Args.hasArg(options::OPT_nodefaultlibs)) {
  6205. bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
  6206. !Args.hasArg(options::OPT_static);
  6207. if (OnlyLibstdcxxStatic)
  6208. CmdArgs.push_back("-Bstatic");
  6209. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  6210. if (OnlyLibstdcxxStatic)
  6211. CmdArgs.push_back("-Bdynamic");
  6212. CmdArgs.push_back("-lm");
  6213. }
  6214. if (!Args.hasArg(options::OPT_nostdlib)) {
  6215. if (!Args.hasArg(options::OPT_nodefaultlibs)) {
  6216. if (Args.hasArg(options::OPT_static))
  6217. CmdArgs.push_back("--start-group");
  6218. LibOpenMP UsedOpenMPLib = LibUnknown;
  6219. if (Args.hasArg(options::OPT_fopenmp)) {
  6220. UsedOpenMPLib = LibGOMP;
  6221. } else if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
  6222. UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(A->getValue())
  6223. .Case("libgomp", LibGOMP)
  6224. .Case("libiomp5", LibIOMP5)
  6225. .Default(LibUnknown);
  6226. if (UsedOpenMPLib == LibUnknown)
  6227. D.Diag(diag::err_drv_unsupported_option_argument)
  6228. << A->getOption().getName() << A->getValue();
  6229. }
  6230. switch (UsedOpenMPLib) {
  6231. case LibGOMP:
  6232. CmdArgs.push_back("-lgomp");
  6233. // FIXME: Exclude this for platforms with libgomp that don't require
  6234. // librt. Most modern Linux platforms require it, but some may not.
  6235. CmdArgs.push_back("-lrt");
  6236. break;
  6237. case LibIOMP5:
  6238. CmdArgs.push_back("-liomp5");
  6239. break;
  6240. case LibUnknown:
  6241. break;
  6242. }
  6243. AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
  6244. if ((Args.hasArg(options::OPT_pthread) ||
  6245. Args.hasArg(options::OPT_pthreads) || UsedOpenMPLib != LibUnknown) &&
  6246. !isAndroid)
  6247. CmdArgs.push_back("-lpthread");
  6248. CmdArgs.push_back("-lc");
  6249. if (Args.hasArg(options::OPT_static))
  6250. CmdArgs.push_back("--end-group");
  6251. else
  6252. AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
  6253. }
  6254. if (!Args.hasArg(options::OPT_nostartfiles)) {
  6255. const char *crtend;
  6256. if (Args.hasArg(options::OPT_shared))
  6257. crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
  6258. else if (IsPIE)
  6259. crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
  6260. else
  6261. crtend = isAndroid ? "crtend_android.o" : "crtend.o";
  6262. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
  6263. if (!isAndroid)
  6264. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  6265. }
  6266. }
  6267. C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
  6268. }
  6269. void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  6270. const InputInfo &Output,
  6271. const InputInfoList &Inputs,
  6272. const ArgList &Args,
  6273. const char *LinkingOutput) const {
  6274. ArgStringList CmdArgs;
  6275. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  6276. options::OPT_Xassembler);
  6277. CmdArgs.push_back("-o");
  6278. CmdArgs.push_back(Output.getFilename());
  6279. for (InputInfoList::const_iterator
  6280. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  6281. const InputInfo &II = *it;
  6282. CmdArgs.push_back(II.getFilename());
  6283. }
  6284. const char *Exec =
  6285. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  6286. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  6287. }
  6288. void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
  6289. const InputInfo &Output,
  6290. const InputInfoList &Inputs,
  6291. const ArgList &Args,
  6292. const char *LinkingOutput) const {
  6293. const Driver &D = getToolChain().getDriver();
  6294. ArgStringList CmdArgs;
  6295. if (Output.isFilename()) {
  6296. CmdArgs.push_back("-o");
  6297. CmdArgs.push_back(Output.getFilename());
  6298. } else {
  6299. assert(Output.isNothing() && "Invalid output.");
  6300. }
  6301. if (!Args.hasArg(options::OPT_nostdlib) &&
  6302. !Args.hasArg(options::OPT_nostartfiles)) {
  6303. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
  6304. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  6305. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  6306. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
  6307. }
  6308. Args.AddAllArgs(CmdArgs, options::OPT_L);
  6309. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  6310. Args.AddAllArgs(CmdArgs, options::OPT_e);
  6311. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  6312. addProfileRT(getToolChain(), Args, CmdArgs);
  6313. if (!Args.hasArg(options::OPT_nostdlib) &&
  6314. !Args.hasArg(options::OPT_nodefaultlibs)) {
  6315. if (D.CCCIsCXX()) {
  6316. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  6317. CmdArgs.push_back("-lm");
  6318. }
  6319. }
  6320. if (!Args.hasArg(options::OPT_nostdlib) &&
  6321. !Args.hasArg(options::OPT_nostartfiles)) {
  6322. if (Args.hasArg(options::OPT_pthread))
  6323. CmdArgs.push_back("-lpthread");
  6324. CmdArgs.push_back("-lc");
  6325. CmdArgs.push_back("-lCompilerRT-Generic");
  6326. CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
  6327. CmdArgs.push_back(
  6328. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  6329. }
  6330. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  6331. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  6332. }
  6333. /// DragonFly Tools
  6334. // For now, DragonFly Assemble does just about the same as for
  6335. // FreeBSD, but this may change soon.
  6336. void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  6337. const InputInfo &Output,
  6338. const InputInfoList &Inputs,
  6339. const ArgList &Args,
  6340. const char *LinkingOutput) const {
  6341. ArgStringList CmdArgs;
  6342. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  6343. // instruct as in the base system to assemble 32-bit code.
  6344. if (getToolChain().getArch() == llvm::Triple::x86)
  6345. CmdArgs.push_back("--32");
  6346. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  6347. options::OPT_Xassembler);
  6348. CmdArgs.push_back("-o");
  6349. CmdArgs.push_back(Output.getFilename());
  6350. for (InputInfoList::const_iterator
  6351. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  6352. const InputInfo &II = *it;
  6353. CmdArgs.push_back(II.getFilename());
  6354. }
  6355. const char *Exec =
  6356. Args.MakeArgString(getToolChain().GetProgramPath("as"));
  6357. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  6358. }
  6359. void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
  6360. const InputInfo &Output,
  6361. const InputInfoList &Inputs,
  6362. const ArgList &Args,
  6363. const char *LinkingOutput) const {
  6364. bool UseGCC47 = false;
  6365. const Driver &D = getToolChain().getDriver();
  6366. ArgStringList CmdArgs;
  6367. if (llvm::sys::fs::exists("/usr/lib/gcc47", UseGCC47))
  6368. UseGCC47 = false;
  6369. if (!D.SysRoot.empty())
  6370. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  6371. CmdArgs.push_back("--eh-frame-hdr");
  6372. if (Args.hasArg(options::OPT_static)) {
  6373. CmdArgs.push_back("-Bstatic");
  6374. } else {
  6375. if (Args.hasArg(options::OPT_rdynamic))
  6376. CmdArgs.push_back("-export-dynamic");
  6377. if (Args.hasArg(options::OPT_shared))
  6378. CmdArgs.push_back("-Bshareable");
  6379. else {
  6380. CmdArgs.push_back("-dynamic-linker");
  6381. CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
  6382. }
  6383. CmdArgs.push_back("--hash-style=both");
  6384. }
  6385. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  6386. // instruct ld in the base system to link 32-bit code.
  6387. if (getToolChain().getArch() == llvm::Triple::x86) {
  6388. CmdArgs.push_back("-m");
  6389. CmdArgs.push_back("elf_i386");
  6390. }
  6391. if (Output.isFilename()) {
  6392. CmdArgs.push_back("-o");
  6393. CmdArgs.push_back(Output.getFilename());
  6394. } else {
  6395. assert(Output.isNothing() && "Invalid output.");
  6396. }
  6397. if (!Args.hasArg(options::OPT_nostdlib) &&
  6398. !Args.hasArg(options::OPT_nostartfiles)) {
  6399. if (!Args.hasArg(options::OPT_shared)) {
  6400. if (Args.hasArg(options::OPT_pg))
  6401. CmdArgs.push_back(Args.MakeArgString(
  6402. getToolChain().GetFilePath("gcrt1.o")));
  6403. else {
  6404. if (Args.hasArg(options::OPT_pie))
  6405. CmdArgs.push_back(Args.MakeArgString(
  6406. getToolChain().GetFilePath("Scrt1.o")));
  6407. else
  6408. CmdArgs.push_back(Args.MakeArgString(
  6409. getToolChain().GetFilePath("crt1.o")));
  6410. }
  6411. }
  6412. CmdArgs.push_back(Args.MakeArgString(
  6413. getToolChain().GetFilePath("crti.o")));
  6414. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  6415. CmdArgs.push_back(Args.MakeArgString(
  6416. getToolChain().GetFilePath("crtbeginS.o")));
  6417. else
  6418. CmdArgs.push_back(Args.MakeArgString(
  6419. getToolChain().GetFilePath("crtbegin.o")));
  6420. }
  6421. Args.AddAllArgs(CmdArgs, options::OPT_L);
  6422. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  6423. Args.AddAllArgs(CmdArgs, options::OPT_e);
  6424. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  6425. if (!Args.hasArg(options::OPT_nostdlib) &&
  6426. !Args.hasArg(options::OPT_nodefaultlibs)) {
  6427. // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
  6428. // rpaths
  6429. if (UseGCC47)
  6430. CmdArgs.push_back("-L/usr/lib/gcc47");
  6431. else
  6432. CmdArgs.push_back("-L/usr/lib/gcc44");
  6433. if (!Args.hasArg(options::OPT_static)) {
  6434. if (UseGCC47) {
  6435. CmdArgs.push_back("-rpath");
  6436. CmdArgs.push_back("/usr/lib/gcc47");
  6437. } else {
  6438. CmdArgs.push_back("-rpath");
  6439. CmdArgs.push_back("/usr/lib/gcc44");
  6440. }
  6441. }
  6442. if (D.CCCIsCXX()) {
  6443. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  6444. CmdArgs.push_back("-lm");
  6445. }
  6446. if (Args.hasArg(options::OPT_pthread))
  6447. CmdArgs.push_back("-lpthread");
  6448. if (!Args.hasArg(options::OPT_nolibc)) {
  6449. CmdArgs.push_back("-lc");
  6450. }
  6451. if (UseGCC47) {
  6452. if (Args.hasArg(options::OPT_static) ||
  6453. Args.hasArg(options::OPT_static_libgcc)) {
  6454. CmdArgs.push_back("-lgcc");
  6455. CmdArgs.push_back("-lgcc_eh");
  6456. } else {
  6457. if (Args.hasArg(options::OPT_shared_libgcc)) {
  6458. CmdArgs.push_back("-lgcc_pic");
  6459. if (!Args.hasArg(options::OPT_shared))
  6460. CmdArgs.push_back("-lgcc");
  6461. } else {
  6462. CmdArgs.push_back("-lgcc");
  6463. CmdArgs.push_back("--as-needed");
  6464. CmdArgs.push_back("-lgcc_pic");
  6465. CmdArgs.push_back("--no-as-needed");
  6466. }
  6467. }
  6468. } else {
  6469. if (Args.hasArg(options::OPT_shared)) {
  6470. CmdArgs.push_back("-lgcc_pic");
  6471. } else {
  6472. CmdArgs.push_back("-lgcc");
  6473. }
  6474. }
  6475. }
  6476. if (!Args.hasArg(options::OPT_nostdlib) &&
  6477. !Args.hasArg(options::OPT_nostartfiles)) {
  6478. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  6479. CmdArgs.push_back(Args.MakeArgString(
  6480. getToolChain().GetFilePath("crtendS.o")));
  6481. else
  6482. CmdArgs.push_back(Args.MakeArgString(
  6483. getToolChain().GetFilePath("crtend.o")));
  6484. CmdArgs.push_back(Args.MakeArgString(
  6485. getToolChain().GetFilePath("crtn.o")));
  6486. }
  6487. addProfileRT(getToolChain(), Args, CmdArgs);
  6488. const char *Exec =
  6489. Args.MakeArgString(getToolChain().GetProgramPath("ld"));
  6490. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  6491. }
  6492. static void addSanitizerRTWindows(const ToolChain &TC, const ArgList &Args,
  6493. ArgStringList &CmdArgs,
  6494. const StringRef RTName) {
  6495. SmallString<128> LibSanitizer(getCompilerRTLibDir(TC));
  6496. llvm::sys::path::append(LibSanitizer,
  6497. Twine("clang_rt.") + RTName + ".lib");
  6498. CmdArgs.push_back(Args.MakeArgString(LibSanitizer));
  6499. }
  6500. void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
  6501. const InputInfo &Output,
  6502. const InputInfoList &Inputs,
  6503. const ArgList &Args,
  6504. const char *LinkingOutput) const {
  6505. ArgStringList CmdArgs;
  6506. if (Output.isFilename()) {
  6507. CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
  6508. Output.getFilename()));
  6509. } else {
  6510. assert(Output.isNothing() && "Invalid output.");
  6511. }
  6512. if (!Args.hasArg(options::OPT_nostdlib) &&
  6513. !Args.hasArg(options::OPT_nostartfiles) &&
  6514. !C.getDriver().IsCLMode()) {
  6515. CmdArgs.push_back("-defaultlib:libcmt");
  6516. }
  6517. CmdArgs.push_back("-nologo");
  6518. if (Args.hasArg(options::OPT_g_Group)) {
  6519. CmdArgs.push_back("-debug");
  6520. }
  6521. bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd);
  6522. if (DLL) {
  6523. CmdArgs.push_back(Args.MakeArgString("-dll"));
  6524. SmallString<128> ImplibName(Output.getFilename());
  6525. llvm::sys::path::replace_extension(ImplibName, "lib");
  6526. CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") +
  6527. ImplibName.str()));
  6528. }
  6529. if (getToolChain().getSanitizerArgs().needsAsanRt()) {
  6530. CmdArgs.push_back(Args.MakeArgString("-debug"));
  6531. CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
  6532. // FIXME: Handle 64-bit.
  6533. if (DLL) {
  6534. addSanitizerRTWindows(getToolChain(), Args, CmdArgs,
  6535. "asan_dll_thunk-i386");
  6536. } else {
  6537. addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan-i386");
  6538. addSanitizerRTWindows(getToolChain(), Args, CmdArgs, "asan_cxx-i386");
  6539. }
  6540. }
  6541. Args.AddAllArgValues(CmdArgs, options::OPT_l);
  6542. Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
  6543. // Add filenames immediately.
  6544. for (InputInfoList::const_iterator
  6545. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  6546. if (it->isFilename())
  6547. CmdArgs.push_back(it->getFilename());
  6548. else
  6549. it->getInputArg().renderAsInput(Args, CmdArgs);
  6550. }
  6551. const char *Exec =
  6552. Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
  6553. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  6554. }
  6555. void visualstudio::Compile::ConstructJob(Compilation &C, const JobAction &JA,
  6556. const InputInfo &Output,
  6557. const InputInfoList &Inputs,
  6558. const ArgList &Args,
  6559. const char *LinkingOutput) const {
  6560. C.addCommand(GetCommand(C, JA, Output, Inputs, Args, LinkingOutput));
  6561. }
  6562. // Try to find FallbackName on PATH that is not identical to ClangProgramPath.
  6563. // If one cannot be found, return FallbackName.
  6564. // We do this special search to prevent clang-cl from falling back onto itself
  6565. // if it's available as cl.exe on the path.
  6566. static std::string FindFallback(const char *FallbackName,
  6567. const char *ClangProgramPath) {
  6568. llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH");
  6569. if (!OptPath.hasValue())
  6570. return FallbackName;
  6571. #ifdef LLVM_ON_WIN32
  6572. const StringRef PathSeparators = ";";
  6573. #else
  6574. const StringRef PathSeparators = ":";
  6575. #endif
  6576. SmallVector<StringRef, 8> PathSegments;
  6577. llvm::SplitString(OptPath.getValue(), PathSegments, PathSeparators);
  6578. for (size_t i = 0, e = PathSegments.size(); i != e; ++i) {
  6579. const StringRef &PathSegment = PathSegments[i];
  6580. if (PathSegment.empty())
  6581. continue;
  6582. SmallString<128> FilePath(PathSegment);
  6583. llvm::sys::path::append(FilePath, FallbackName);
  6584. if (llvm::sys::fs::can_execute(Twine(FilePath)) &&
  6585. !llvm::sys::fs::equivalent(Twine(FilePath), ClangProgramPath))
  6586. return FilePath.str();
  6587. }
  6588. return FallbackName;
  6589. }
  6590. Command *visualstudio::Compile::GetCommand(Compilation &C, const JobAction &JA,
  6591. const InputInfo &Output,
  6592. const InputInfoList &Inputs,
  6593. const ArgList &Args,
  6594. const char *LinkingOutput) const {
  6595. ArgStringList CmdArgs;
  6596. CmdArgs.push_back("/nologo");
  6597. CmdArgs.push_back("/c"); // Compile only.
  6598. CmdArgs.push_back("/W0"); // No warnings.
  6599. // The goal is to be able to invoke this tool correctly based on
  6600. // any flag accepted by clang-cl.
  6601. // These are spelled the same way in clang and cl.exe,.
  6602. Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
  6603. Args.AddAllArgs(CmdArgs, options::OPT_I);
  6604. // Optimization level.
  6605. if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) {
  6606. if (A->getOption().getID() == options::OPT_O0) {
  6607. CmdArgs.push_back("/Od");
  6608. } else {
  6609. StringRef OptLevel = A->getValue();
  6610. if (OptLevel == "1" || OptLevel == "2" || OptLevel == "s")
  6611. A->render(Args, CmdArgs);
  6612. else if (OptLevel == "3")
  6613. CmdArgs.push_back("/Ox");
  6614. }
  6615. }
  6616. // Flags for which clang-cl have an alias.
  6617. // FIXME: How can we ensure this stays in sync with relevant clang-cl options?
  6618. if (Arg *A = Args.getLastArg(options::OPT_frtti, options::OPT_fno_rtti))
  6619. CmdArgs.push_back(A->getOption().getID() == options::OPT_frtti ? "/GR"
  6620. : "/GR-");
  6621. if (Arg *A = Args.getLastArg(options::OPT_ffunction_sections,
  6622. options::OPT_fno_function_sections))
  6623. CmdArgs.push_back(A->getOption().getID() == options::OPT_ffunction_sections
  6624. ? "/Gy"
  6625. : "/Gy-");
  6626. if (Arg *A = Args.getLastArg(options::OPT_fdata_sections,
  6627. options::OPT_fno_data_sections))
  6628. CmdArgs.push_back(
  6629. A->getOption().getID() == options::OPT_fdata_sections ? "/Gw" : "/Gw-");
  6630. if (Args.hasArg(options::OPT_fsyntax_only))
  6631. CmdArgs.push_back("/Zs");
  6632. if (Args.hasArg(options::OPT_g_Flag, options::OPT_gline_tables_only))
  6633. CmdArgs.push_back("/Z7");
  6634. std::vector<std::string> Includes = Args.getAllArgValues(options::OPT_include);
  6635. for (size_t I = 0, E = Includes.size(); I != E; ++I)
  6636. CmdArgs.push_back(Args.MakeArgString(std::string("/FI") + Includes[I]));
  6637. // Flags that can simply be passed through.
  6638. Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LD);
  6639. Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LDd);
  6640. // The order of these flags is relevant, so pick the last one.
  6641. if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd,
  6642. options::OPT__SLASH_MT, options::OPT__SLASH_MTd))
  6643. A->render(Args, CmdArgs);
  6644. // Input filename.
  6645. assert(Inputs.size() == 1);
  6646. const InputInfo &II = Inputs[0];
  6647. assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX);
  6648. CmdArgs.push_back(II.getType() == types::TY_C ? "/Tc" : "/Tp");
  6649. if (II.isFilename())
  6650. CmdArgs.push_back(II.getFilename());
  6651. else
  6652. II.getInputArg().renderAsInput(Args, CmdArgs);
  6653. // Output filename.
  6654. assert(Output.getType() == types::TY_Object);
  6655. const char *Fo = Args.MakeArgString(std::string("/Fo") +
  6656. Output.getFilename());
  6657. CmdArgs.push_back(Fo);
  6658. const Driver &D = getToolChain().getDriver();
  6659. std::string Exec = FindFallback("cl.exe", D.getClangProgramPath());
  6660. return new Command(JA, *this, Args.MakeArgString(Exec), CmdArgs);
  6661. }
  6662. /// XCore Tools
  6663. // We pass assemble and link construction to the xcc tool.
  6664. void XCore::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
  6665. const InputInfo &Output,
  6666. const InputInfoList &Inputs,
  6667. const ArgList &Args,
  6668. const char *LinkingOutput) const {
  6669. ArgStringList CmdArgs;
  6670. CmdArgs.push_back("-o");
  6671. CmdArgs.push_back(Output.getFilename());
  6672. CmdArgs.push_back("-c");
  6673. if (Args.hasArg(options::OPT_v))
  6674. CmdArgs.push_back("-v");
  6675. if (Arg *A = Args.getLastArg(options::OPT_g_Group))
  6676. if (!A->getOption().matches(options::OPT_g0))
  6677. CmdArgs.push_back("-g");
  6678. if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
  6679. false))
  6680. CmdArgs.push_back("-fverbose-asm");
  6681. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
  6682. options::OPT_Xassembler);
  6683. for (InputInfoList::const_iterator
  6684. it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
  6685. const InputInfo &II = *it;
  6686. CmdArgs.push_back(II.getFilename());
  6687. }
  6688. const char *Exec =
  6689. Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
  6690. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  6691. }
  6692. void XCore::Link::ConstructJob(Compilation &C, const JobAction &JA,
  6693. const InputInfo &Output,
  6694. const InputInfoList &Inputs,
  6695. const ArgList &Args,
  6696. const char *LinkingOutput) const {
  6697. ArgStringList CmdArgs;
  6698. if (Output.isFilename()) {
  6699. CmdArgs.push_back("-o");
  6700. CmdArgs.push_back(Output.getFilename());
  6701. } else {
  6702. assert(Output.isNothing() && "Invalid output.");
  6703. }
  6704. if (Args.hasArg(options::OPT_v))
  6705. CmdArgs.push_back("-v");
  6706. ExceptionSettings EH = exceptionSettings(Args, getToolChain().getTriple());
  6707. if (EH.ShouldUseExceptionTables)
  6708. CmdArgs.push_back("-fexceptions");
  6709. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
  6710. const char *Exec =
  6711. Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
  6712. C.addCommand(new Command(JA, *this, Exec, CmdArgs));
  6713. }