Tools.cpp 217 KB

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