Tools.cpp 338 KB

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