Tools.cpp 256 KB

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