Tools.cpp 292 KB

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