Tools.cpp 217 KB

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