Tools.cpp 275 KB

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