Tools.cpp 278 KB

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