Tools.cpp 319 KB

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