Tools.cpp 234 KB

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