cmGeneratorTarget.cxx 211 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGeneratorTarget.h"
  4. #include "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <iterator>
  9. #include <memory> // IWYU pragma: keep
  10. #include <queue>
  11. #include <sstream>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unordered_set>
  16. #include <utility>
  17. #include "cmAlgorithms.h"
  18. #include "cmComputeLinkInformation.h"
  19. #include "cmCustomCommand.h"
  20. #include "cmCustomCommandGenerator.h"
  21. #include "cmCustomCommandLines.h"
  22. #include "cmGeneratorExpression.h"
  23. #include "cmGeneratorExpressionDAGChecker.h"
  24. #include "cmGlobalGenerator.h"
  25. #include "cmLocalGenerator.h"
  26. #include "cmMakefile.h"
  27. #include "cmMessageType.h"
  28. #include "cmPropertyMap.h"
  29. #include "cmRange.h"
  30. #include "cmSourceFile.h"
  31. #include "cmSourceFileLocation.h"
  32. #include "cmState.h"
  33. #include "cmSystemTools.h"
  34. #include "cmTarget.h"
  35. #include "cmTargetLinkLibraryType.h"
  36. #include "cmTargetPropertyComputer.h"
  37. #include "cmake.h"
  38. class cmMessenger;
  39. template <>
  40. const char* cmTargetPropertyComputer::GetSources<cmGeneratorTarget>(
  41. cmGeneratorTarget const* tgt, cmMessenger* /* messenger */,
  42. cmListFileBacktrace const& /* context */)
  43. {
  44. return tgt->GetSourcesProperty();
  45. }
  46. template <>
  47. const char*
  48. cmTargetPropertyComputer::ComputeLocationForBuild<cmGeneratorTarget>(
  49. cmGeneratorTarget const* tgt)
  50. {
  51. return tgt->GetLocation("");
  52. }
  53. template <>
  54. const char* cmTargetPropertyComputer::ComputeLocation<cmGeneratorTarget>(
  55. cmGeneratorTarget const* tgt, const std::string& config)
  56. {
  57. return tgt->GetLocation(config);
  58. }
  59. class cmGeneratorTarget::TargetPropertyEntry
  60. {
  61. protected:
  62. static cmLinkImplItem NoLinkImplItem;
  63. public:
  64. TargetPropertyEntry(cmLinkImplItem const& item)
  65. : LinkImplItem(item)
  66. {
  67. }
  68. virtual ~TargetPropertyEntry() = default;
  69. virtual const std::string& Evaluate(
  70. cmLocalGenerator* lg, const std::string& config, bool quiet = false,
  71. cmGeneratorTarget const* headTarget = nullptr,
  72. cmGeneratorTarget const* currentTarget = nullptr,
  73. cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
  74. std::string const& language = std::string()) const = 0;
  75. virtual const std::string& Evaluate(
  76. cmLocalGenerator* lg, const std::string& config, bool quiet,
  77. cmGeneratorTarget const* headTarget,
  78. cmGeneratorExpressionDAGChecker* dagChecker,
  79. std::string const& language = std::string()) const = 0;
  80. virtual cmListFileBacktrace GetBacktrace() const = 0;
  81. virtual std::string const& GetInput() const = 0;
  82. virtual bool GetHadContextSensitiveCondition() const { return false; }
  83. cmLinkImplItem const& LinkImplItem;
  84. private:
  85. cmListFileBacktrace Backtrace;
  86. };
  87. cmLinkImplItem cmGeneratorTarget::TargetPropertyEntry::NoLinkImplItem;
  88. class TargetPropertyEntryGenex : public cmGeneratorTarget::TargetPropertyEntry
  89. {
  90. public:
  91. TargetPropertyEntryGenex(std::unique_ptr<cmCompiledGeneratorExpression> cge,
  92. cmLinkImplItem const& item = NoLinkImplItem)
  93. : cmGeneratorTarget::TargetPropertyEntry(item)
  94. , ge(std::move(cge))
  95. {
  96. }
  97. const std::string& Evaluate(
  98. cmLocalGenerator* lg, const std::string& config, bool quiet = false,
  99. cmGeneratorTarget const* headTarget = nullptr,
  100. cmGeneratorTarget const* currentTarget = nullptr,
  101. cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
  102. std::string const& language = std::string()) const override
  103. {
  104. return this->ge->Evaluate(lg, config, quiet, headTarget, currentTarget,
  105. dagChecker, language);
  106. }
  107. const std::string& Evaluate(
  108. cmLocalGenerator* lg, const std::string& config, bool quiet,
  109. cmGeneratorTarget const* headTarget,
  110. cmGeneratorExpressionDAGChecker* dagChecker,
  111. std::string const& language = std::string()) const override
  112. {
  113. return this->ge->Evaluate(lg, config, quiet, headTarget, dagChecker,
  114. language);
  115. }
  116. cmListFileBacktrace GetBacktrace() const override
  117. {
  118. return this->ge->GetBacktrace();
  119. }
  120. std::string const& GetInput() const override { return this->ge->GetInput(); }
  121. bool GetHadContextSensitiveCondition() const override
  122. {
  123. return this->ge->GetHadContextSensitiveCondition();
  124. }
  125. private:
  126. const std::unique_ptr<cmCompiledGeneratorExpression> ge;
  127. };
  128. class TargetPropertyEntryString : public cmGeneratorTarget::TargetPropertyEntry
  129. {
  130. public:
  131. TargetPropertyEntryString(std::string propertyValue,
  132. cmListFileBacktrace backtrace,
  133. cmLinkImplItem const& item = NoLinkImplItem)
  134. : cmGeneratorTarget::TargetPropertyEntry(item)
  135. , PropertyValue(std::move(propertyValue))
  136. , Backtrace(std::move(backtrace))
  137. {
  138. }
  139. const std::string& Evaluate(cmLocalGenerator*, const std::string&, bool,
  140. cmGeneratorTarget const*,
  141. cmGeneratorTarget const*,
  142. cmGeneratorExpressionDAGChecker*,
  143. std::string const&) const override
  144. {
  145. return this->PropertyValue;
  146. }
  147. const std::string& Evaluate(cmLocalGenerator*, const std::string&, bool,
  148. cmGeneratorTarget const*,
  149. cmGeneratorExpressionDAGChecker*,
  150. std::string const&) const override
  151. {
  152. return this->PropertyValue;
  153. }
  154. cmListFileBacktrace GetBacktrace() const override { return this->Backtrace; }
  155. std::string const& GetInput() const override { return this->PropertyValue; }
  156. private:
  157. std::string PropertyValue;
  158. cmListFileBacktrace Backtrace;
  159. };
  160. cmGeneratorTarget::TargetPropertyEntry* CreateTargetPropertyEntry(
  161. const std::string& propertyValue,
  162. cmListFileBacktrace backtrace = cmListFileBacktrace(),
  163. bool evaluateForBuildsystem = false)
  164. {
  165. if (cmGeneratorExpression::Find(propertyValue) != std::string::npos) {
  166. cmGeneratorExpression ge(std::move(backtrace));
  167. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  168. ge.Parse(propertyValue);
  169. cge->SetEvaluateForBuildsystem(evaluateForBuildsystem);
  170. return new TargetPropertyEntryGenex(std::move(cge));
  171. }
  172. return new TargetPropertyEntryString(propertyValue, backtrace);
  173. }
  174. void CreatePropertyGeneratorExpressions(
  175. cmStringRange entries, cmBacktraceRange backtraces,
  176. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& items,
  177. bool evaluateForBuildsystem = false)
  178. {
  179. std::vector<cmListFileBacktrace>::const_iterator btIt = backtraces.begin();
  180. for (std::vector<std::string>::const_iterator it = entries.begin();
  181. it != entries.end(); ++it, ++btIt) {
  182. items.push_back(
  183. CreateTargetPropertyEntry(*it, *btIt, evaluateForBuildsystem));
  184. }
  185. }
  186. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t, cmLocalGenerator* lg)
  187. : Target(t)
  188. , FortranModuleDirectoryCreated(false)
  189. , SourceFileFlagsConstructed(false)
  190. , PolicyWarnedCMP0022(false)
  191. , PolicyReportedCMP0069(false)
  192. , DebugIncludesDone(false)
  193. , DebugCompileOptionsDone(false)
  194. , DebugCompileFeaturesDone(false)
  195. , DebugCompileDefinitionsDone(false)
  196. , DebugLinkOptionsDone(false)
  197. , DebugLinkDirectoriesDone(false)
  198. , DebugSourcesDone(false)
  199. , LinkImplementationLanguageIsContextDependent(true)
  200. , UtilityItemsDone(false)
  201. {
  202. this->Makefile = this->Target->GetMakefile();
  203. this->LocalGenerator = lg;
  204. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  205. this->GlobalGenerator->ComputeTargetObjectDirectory(this);
  206. CreatePropertyGeneratorExpressions(t->GetIncludeDirectoriesEntries(),
  207. t->GetIncludeDirectoriesBacktraces(),
  208. this->IncludeDirectoriesEntries);
  209. CreatePropertyGeneratorExpressions(t->GetCompileOptionsEntries(),
  210. t->GetCompileOptionsBacktraces(),
  211. this->CompileOptionsEntries);
  212. CreatePropertyGeneratorExpressions(t->GetCompileFeaturesEntries(),
  213. t->GetCompileFeaturesBacktraces(),
  214. this->CompileFeaturesEntries);
  215. CreatePropertyGeneratorExpressions(t->GetCompileDefinitionsEntries(),
  216. t->GetCompileDefinitionsBacktraces(),
  217. this->CompileDefinitionsEntries);
  218. CreatePropertyGeneratorExpressions(t->GetLinkOptionsEntries(),
  219. t->GetLinkOptionsBacktraces(),
  220. this->LinkOptionsEntries);
  221. CreatePropertyGeneratorExpressions(t->GetLinkDirectoriesEntries(),
  222. t->GetLinkDirectoriesBacktraces(),
  223. this->LinkDirectoriesEntries);
  224. CreatePropertyGeneratorExpressions(t->GetSourceEntries(),
  225. t->GetSourceBacktraces(),
  226. this->SourceEntries, true);
  227. this->DLLPlatform =
  228. !this->Makefile->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX").empty();
  229. this->PolicyMap = t->GetPolicyMap();
  230. }
  231. cmGeneratorTarget::~cmGeneratorTarget()
  232. {
  233. cmDeleteAll(this->IncludeDirectoriesEntries);
  234. cmDeleteAll(this->CompileOptionsEntries);
  235. cmDeleteAll(this->CompileFeaturesEntries);
  236. cmDeleteAll(this->CompileDefinitionsEntries);
  237. cmDeleteAll(this->LinkOptionsEntries);
  238. cmDeleteAll(this->LinkDirectoriesEntries);
  239. cmDeleteAll(this->SourceEntries);
  240. cmDeleteAll(this->LinkInformation);
  241. }
  242. const char* cmGeneratorTarget::GetSourcesProperty() const
  243. {
  244. std::vector<std::string> values;
  245. for (TargetPropertyEntry* se : this->SourceEntries) {
  246. values.push_back(se->GetInput());
  247. }
  248. static std::string value;
  249. value.clear();
  250. value = cmJoin(values, ";");
  251. return value.c_str();
  252. }
  253. cmGlobalGenerator* cmGeneratorTarget::GetGlobalGenerator() const
  254. {
  255. return this->GetLocalGenerator()->GetGlobalGenerator();
  256. }
  257. cmLocalGenerator* cmGeneratorTarget::GetLocalGenerator() const
  258. {
  259. return this->LocalGenerator;
  260. }
  261. cmStateEnums::TargetType cmGeneratorTarget::GetType() const
  262. {
  263. return this->Target->GetType();
  264. }
  265. const std::string& cmGeneratorTarget::GetName() const
  266. {
  267. return this->Target->GetName();
  268. }
  269. std::string cmGeneratorTarget::GetExportName() const
  270. {
  271. const char* exportName = this->GetProperty("EXPORT_NAME");
  272. if (exportName && *exportName) {
  273. if (!cmGeneratorExpression::IsValidTargetName(exportName)) {
  274. std::ostringstream e;
  275. e << "EXPORT_NAME property \"" << exportName << "\" for \""
  276. << this->GetName() << "\": is not valid.";
  277. cmSystemTools::Error(e.str());
  278. return "";
  279. }
  280. return exportName;
  281. }
  282. return this->GetName();
  283. }
  284. const char* cmGeneratorTarget::GetProperty(const std::string& prop) const
  285. {
  286. if (!cmTargetPropertyComputer::PassesWhitelist(
  287. this->GetType(), prop, this->Makefile->GetMessenger(),
  288. this->GetBacktrace())) {
  289. return nullptr;
  290. }
  291. if (const char* result = cmTargetPropertyComputer::GetProperty(
  292. this, prop, this->Makefile->GetMessenger(), this->GetBacktrace())) {
  293. return result;
  294. }
  295. if (cmSystemTools::GetFatalErrorOccured()) {
  296. return nullptr;
  297. }
  298. return this->Target->GetProperty(prop);
  299. }
  300. const char* cmGeneratorTarget::GetSafeProperty(const std::string& prop) const
  301. {
  302. const char* ret = this->GetProperty(prop);
  303. if (!ret) {
  304. return "";
  305. }
  306. return ret;
  307. }
  308. const char* cmGeneratorTarget::GetOutputTargetType(
  309. cmStateEnums::ArtifactType artifact) const
  310. {
  311. switch (this->GetType()) {
  312. case cmStateEnums::SHARED_LIBRARY:
  313. if (this->IsDLLPlatform()) {
  314. switch (artifact) {
  315. case cmStateEnums::RuntimeBinaryArtifact:
  316. // A DLL shared library is treated as a runtime target.
  317. return "RUNTIME";
  318. case cmStateEnums::ImportLibraryArtifact:
  319. // A DLL import library is treated as an archive target.
  320. return "ARCHIVE";
  321. }
  322. } else {
  323. // For non-DLL platforms shared libraries are treated as
  324. // library targets.
  325. return "LIBRARY";
  326. }
  327. break;
  328. case cmStateEnums::STATIC_LIBRARY:
  329. // Static libraries are always treated as archive targets.
  330. return "ARCHIVE";
  331. case cmStateEnums::MODULE_LIBRARY:
  332. switch (artifact) {
  333. case cmStateEnums::RuntimeBinaryArtifact:
  334. // Module libraries are always treated as library targets.
  335. return "LIBRARY";
  336. case cmStateEnums::ImportLibraryArtifact:
  337. // Module import libraries are treated as archive targets.
  338. return "ARCHIVE";
  339. }
  340. break;
  341. case cmStateEnums::OBJECT_LIBRARY:
  342. // Object libraries are always treated as object targets.
  343. return "OBJECT";
  344. case cmStateEnums::EXECUTABLE:
  345. switch (artifact) {
  346. case cmStateEnums::RuntimeBinaryArtifact:
  347. // Executables are always treated as runtime targets.
  348. return "RUNTIME";
  349. case cmStateEnums::ImportLibraryArtifact:
  350. // Executable import libraries are treated as archive targets.
  351. return "ARCHIVE";
  352. }
  353. break;
  354. default:
  355. break;
  356. }
  357. return "";
  358. }
  359. std::string cmGeneratorTarget::GetOutputName(
  360. const std::string& config, cmStateEnums::ArtifactType artifact) const
  361. {
  362. // Lookup/compute/cache the output name for this configuration.
  363. OutputNameKey key(config, artifact);
  364. cmGeneratorTarget::OutputNameMapType::iterator i =
  365. this->OutputNameMap.find(key);
  366. if (i == this->OutputNameMap.end()) {
  367. // Add empty name in map to detect potential recursion.
  368. OutputNameMapType::value_type entry(key, "");
  369. i = this->OutputNameMap.insert(entry).first;
  370. // Compute output name.
  371. std::vector<std::string> props;
  372. std::string type = this->GetOutputTargetType(artifact);
  373. std::string configUpper = cmSystemTools::UpperCase(config);
  374. if (!type.empty() && !configUpper.empty()) {
  375. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME_<CONFIG>
  376. props.push_back(type + "_OUTPUT_NAME_" + configUpper);
  377. }
  378. if (!type.empty()) {
  379. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME
  380. props.push_back(type + "_OUTPUT_NAME");
  381. }
  382. if (!configUpper.empty()) {
  383. // OUTPUT_NAME_<CONFIG>
  384. props.push_back("OUTPUT_NAME_" + configUpper);
  385. // <CONFIG>_OUTPUT_NAME
  386. props.push_back(configUpper + "_OUTPUT_NAME");
  387. }
  388. // OUTPUT_NAME
  389. props.emplace_back("OUTPUT_NAME");
  390. std::string outName;
  391. for (std::string const& p : props) {
  392. if (const char* outNameProp = this->GetProperty(p)) {
  393. outName = outNameProp;
  394. break;
  395. }
  396. }
  397. if (outName.empty()) {
  398. outName = this->GetName();
  399. }
  400. // Now evaluate genex and update the previously-prepared map entry.
  401. cmGeneratorExpression ge;
  402. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outName);
  403. i->second = cge->Evaluate(this->LocalGenerator, config);
  404. } else if (i->second.empty()) {
  405. // An empty map entry indicates we have been called recursively
  406. // from the above block.
  407. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  408. MessageType::FATAL_ERROR,
  409. "Target '" + this->GetName() + "' OUTPUT_NAME depends on itself.",
  410. this->GetBacktrace());
  411. }
  412. return i->second;
  413. }
  414. void cmGeneratorTarget::ClearSourcesCache()
  415. {
  416. this->KindedSourcesMap.clear();
  417. this->LinkImplementationLanguageIsContextDependent = true;
  418. this->Objects.clear();
  419. }
  420. void cmGeneratorTarget::AddSourceCommon(const std::string& src, bool before)
  421. {
  422. this->SourceEntries.insert(
  423. before ? this->SourceEntries.begin() : this->SourceEntries.end(),
  424. CreateTargetPropertyEntry(src, this->Makefile->GetBacktrace(), true));
  425. this->ClearSourcesCache();
  426. }
  427. void cmGeneratorTarget::AddSource(const std::string& src, bool before)
  428. {
  429. this->Target->AddSource(src, before);
  430. this->AddSourceCommon(src, before);
  431. }
  432. void cmGeneratorTarget::AddTracedSources(std::vector<std::string> const& srcs)
  433. {
  434. this->Target->AddTracedSources(srcs);
  435. if (!srcs.empty()) {
  436. this->AddSourceCommon(cmJoin(srcs, ";"));
  437. }
  438. }
  439. void cmGeneratorTarget::AddIncludeDirectory(const std::string& src,
  440. bool before)
  441. {
  442. this->Target->InsertInclude(src, this->Makefile->GetBacktrace(), before);
  443. this->IncludeDirectoriesEntries.insert(
  444. before ? this->IncludeDirectoriesEntries.begin()
  445. : this->IncludeDirectoriesEntries.end(),
  446. CreateTargetPropertyEntry(src, this->Makefile->GetBacktrace(), true));
  447. }
  448. std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
  449. cmSourceFile const* sf) const
  450. {
  451. SourceEntriesType::const_iterator i = this->SourceDepends.find(sf);
  452. if (i != this->SourceDepends.end()) {
  453. return &i->second.Depends;
  454. }
  455. return nullptr;
  456. }
  457. static void handleSystemIncludesDep(
  458. cmLocalGenerator* lg, cmGeneratorTarget const* depTgt,
  459. const std::string& config, cmGeneratorTarget const* headTarget,
  460. cmGeneratorExpressionDAGChecker* dagChecker,
  461. std::vector<std::string>& result, bool excludeImported,
  462. std::string const& language)
  463. {
  464. if (const char* dirs =
  465. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")) {
  466. cmGeneratorExpression ge;
  467. cmSystemTools::ExpandListArgument(
  468. ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
  469. dagChecker, language),
  470. result);
  471. }
  472. if (!depTgt->IsImported() || excludeImported) {
  473. return;
  474. }
  475. if (const char* dirs =
  476. depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
  477. cmGeneratorExpression ge;
  478. cmSystemTools::ExpandListArgument(
  479. ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
  480. dagChecker, language),
  481. result);
  482. }
  483. }
  484. /* clang-format off */
  485. #define IMPLEMENT_VISIT(KIND) \
  486. do { \
  487. KindedSources const& kinded = this->GetKindedSources(config); \
  488. for (SourceAndKind const& s : kinded.Sources) { \
  489. if (s.Kind == KIND) { \
  490. data.push_back(s.Source.Value); \
  491. } \
  492. } \
  493. } while (false)
  494. /* clang-format on */
  495. void cmGeneratorTarget::GetObjectSources(
  496. std::vector<cmSourceFile const*>& data, const std::string& config) const
  497. {
  498. IMPLEMENT_VISIT(SourceKindObjectSource);
  499. if (!this->Objects.empty()) {
  500. return;
  501. }
  502. for (cmSourceFile const* it : data) {
  503. this->Objects[it];
  504. }
  505. this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
  506. }
  507. void cmGeneratorTarget::ComputeObjectMapping()
  508. {
  509. if (!this->Objects.empty()) {
  510. return;
  511. }
  512. std::vector<std::string> configs;
  513. this->Makefile->GetConfigurations(configs);
  514. if (configs.empty()) {
  515. configs.emplace_back();
  516. }
  517. for (std::string const& c : configs) {
  518. std::vector<cmSourceFile const*> sourceFiles;
  519. this->GetObjectSources(sourceFiles, c);
  520. }
  521. }
  522. const char* cmGeneratorTarget::GetFeature(const std::string& feature,
  523. const std::string& config) const
  524. {
  525. if (!config.empty()) {
  526. std::string featureConfig = feature;
  527. featureConfig += "_";
  528. featureConfig += cmSystemTools::UpperCase(config);
  529. if (const char* value = this->GetProperty(featureConfig)) {
  530. return value;
  531. }
  532. }
  533. if (const char* value = this->GetProperty(feature)) {
  534. return value;
  535. }
  536. return this->LocalGenerator->GetFeature(feature, config);
  537. }
  538. const char* cmGeneratorTarget::GetLinkPIEProperty(
  539. const std::string& config) const
  540. {
  541. static std::string PICValue;
  542. PICValue = this->GetLinkInterfaceDependentStringAsBoolProperty(
  543. "POSITION_INDEPENDENT_CODE", config);
  544. if (PICValue == "(unset)") {
  545. // POSITION_INDEPENDENT_CODE is not set
  546. return nullptr;
  547. }
  548. auto status = this->GetPolicyStatusCMP0083();
  549. return (status != cmPolicies::WARN && status != cmPolicies::OLD)
  550. ? PICValue.c_str()
  551. : nullptr;
  552. }
  553. bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
  554. std::string const& config) const
  555. {
  556. const char* feature = "INTERPROCEDURAL_OPTIMIZATION";
  557. const bool result = cmSystemTools::IsOn(this->GetFeature(feature, config));
  558. if (!result) {
  559. // 'INTERPROCEDURAL_OPTIMIZATION' is off, no need to check policies
  560. return false;
  561. }
  562. if (lang != "C" && lang != "CXX" && lang != "Fortran") {
  563. // We do not define IPO behavior for other languages.
  564. return false;
  565. }
  566. cmPolicies::PolicyStatus cmp0069 = this->GetPolicyStatusCMP0069();
  567. if (cmp0069 == cmPolicies::OLD || cmp0069 == cmPolicies::WARN) {
  568. if (this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_LEGACY_BEHAVIOR")) {
  569. return true;
  570. }
  571. if (this->PolicyReportedCMP0069) {
  572. // problem is already reported, no need to issue a message
  573. return false;
  574. }
  575. const bool in_try_compile =
  576. this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile();
  577. if (cmp0069 == cmPolicies::WARN && !in_try_compile) {
  578. std::ostringstream w;
  579. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0069) << "\n";
  580. w << "INTERPROCEDURAL_OPTIMIZATION property will be ignored for target "
  581. << "'" << this->GetName() << "'.";
  582. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  583. MessageType::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  584. this->PolicyReportedCMP0069 = true;
  585. }
  586. return false;
  587. }
  588. // Note: check consistency with messages from CheckIPOSupported
  589. const char* message = nullptr;
  590. if (!this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_SUPPORTED_BY_CMAKE")) {
  591. message = "CMake doesn't support IPO for current compiler";
  592. } else if (!this->Makefile->IsOn("_CMAKE_" + lang +
  593. "_IPO_MAY_BE_SUPPORTED_BY_COMPILER")) {
  594. message = "Compiler doesn't support IPO";
  595. } else if (!this->GlobalGenerator->IsIPOSupported()) {
  596. message = "CMake doesn't support IPO for current generator";
  597. }
  598. if (!message) {
  599. // No error/warning messages
  600. return true;
  601. }
  602. if (this->PolicyReportedCMP0069) {
  603. // problem is already reported, no need to issue a message
  604. return false;
  605. }
  606. this->PolicyReportedCMP0069 = true;
  607. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  608. MessageType::FATAL_ERROR, message, this->GetBacktrace());
  609. return false;
  610. }
  611. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  612. {
  613. this->ComputeObjectMapping();
  614. return this->Objects[file];
  615. }
  616. const char* cmGeneratorTarget::GetCustomObjectExtension() const
  617. {
  618. static std::string extension;
  619. const bool has_ptx_extension =
  620. this->GetPropertyAsBool("CUDA_PTX_COMPILATION");
  621. if (has_ptx_extension) {
  622. extension = ".ptx";
  623. return extension.c_str();
  624. }
  625. return nullptr;
  626. }
  627. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  628. {
  629. this->ExplicitObjectName.insert(sf);
  630. }
  631. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  632. {
  633. const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
  634. std::set<cmSourceFile const*>::const_iterator it =
  635. this->ExplicitObjectName.find(file);
  636. return it != this->ExplicitObjectName.end();
  637. }
  638. void cmGeneratorTarget::GetModuleDefinitionSources(
  639. std::vector<cmSourceFile const*>& data, const std::string& config) const
  640. {
  641. IMPLEMENT_VISIT(SourceKindModuleDefinition);
  642. }
  643. void cmGeneratorTarget::GetHeaderSources(
  644. std::vector<cmSourceFile const*>& data, const std::string& config) const
  645. {
  646. IMPLEMENT_VISIT(SourceKindHeader);
  647. }
  648. void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile const*>& data,
  649. const std::string& config) const
  650. {
  651. IMPLEMENT_VISIT(SourceKindExtra);
  652. }
  653. void cmGeneratorTarget::GetCustomCommands(
  654. std::vector<cmSourceFile const*>& data, const std::string& config) const
  655. {
  656. IMPLEMENT_VISIT(SourceKindCustomCommand);
  657. }
  658. void cmGeneratorTarget::GetExternalObjects(
  659. std::vector<cmSourceFile const*>& data, const std::string& config) const
  660. {
  661. IMPLEMENT_VISIT(SourceKindExternalObject);
  662. }
  663. void cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& headers,
  664. const std::string& config) const
  665. {
  666. KindedSources const& kinded = this->GetKindedSources(config);
  667. headers = kinded.ExpectedResxHeaders;
  668. }
  669. void cmGeneratorTarget::GetResxSources(std::vector<cmSourceFile const*>& data,
  670. const std::string& config) const
  671. {
  672. IMPLEMENT_VISIT(SourceKindResx);
  673. }
  674. void cmGeneratorTarget::GetAppManifest(std::vector<cmSourceFile const*>& data,
  675. const std::string& config) const
  676. {
  677. IMPLEMENT_VISIT(SourceKindAppManifest);
  678. }
  679. void cmGeneratorTarget::GetManifests(std::vector<cmSourceFile const*>& data,
  680. const std::string& config) const
  681. {
  682. IMPLEMENT_VISIT(SourceKindManifest);
  683. }
  684. void cmGeneratorTarget::GetCertificates(std::vector<cmSourceFile const*>& data,
  685. const std::string& config) const
  686. {
  687. IMPLEMENT_VISIT(SourceKindCertificate);
  688. }
  689. void cmGeneratorTarget::GetExpectedXamlHeaders(std::set<std::string>& headers,
  690. const std::string& config) const
  691. {
  692. KindedSources const& kinded = this->GetKindedSources(config);
  693. headers = kinded.ExpectedXamlHeaders;
  694. }
  695. void cmGeneratorTarget::GetExpectedXamlSources(std::set<std::string>& srcs,
  696. const std::string& config) const
  697. {
  698. KindedSources const& kinded = this->GetKindedSources(config);
  699. srcs = kinded.ExpectedXamlSources;
  700. }
  701. std::set<cmLinkItem> const& cmGeneratorTarget::GetUtilityItems() const
  702. {
  703. if (!this->UtilityItemsDone) {
  704. this->UtilityItemsDone = true;
  705. std::set<BT<std::string>> const& utilities = this->GetUtilities();
  706. for (BT<std::string> const& i : utilities) {
  707. if (cmGeneratorTarget* gt =
  708. this->LocalGenerator->FindGeneratorTargetToUse(i.Value)) {
  709. this->UtilityItems.insert(cmLinkItem(gt, i.Backtrace));
  710. } else {
  711. this->UtilityItems.insert(cmLinkItem(i.Value, i.Backtrace));
  712. }
  713. }
  714. }
  715. return this->UtilityItems;
  716. }
  717. void cmGeneratorTarget::GetXamlSources(std::vector<cmSourceFile const*>& data,
  718. const std::string& config) const
  719. {
  720. IMPLEMENT_VISIT(SourceKindXaml);
  721. }
  722. const char* cmGeneratorTarget::GetLocation(const std::string& config) const
  723. {
  724. static std::string location;
  725. if (this->IsImported()) {
  726. location = this->Target->ImportedGetFullPath(
  727. config, cmStateEnums::RuntimeBinaryArtifact);
  728. } else {
  729. location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  730. }
  731. return location.c_str();
  732. }
  733. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreBuildCommands()
  734. const
  735. {
  736. return this->Target->GetPreBuildCommands();
  737. }
  738. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreLinkCommands()
  739. const
  740. {
  741. return this->Target->GetPreLinkCommands();
  742. }
  743. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPostBuildCommands()
  744. const
  745. {
  746. return this->Target->GetPostBuildCommands();
  747. }
  748. bool cmGeneratorTarget::IsImported() const
  749. {
  750. return this->Target->IsImported();
  751. }
  752. bool cmGeneratorTarget::IsImportedGloballyVisible() const
  753. {
  754. return this->Target->IsImportedGloballyVisible();
  755. }
  756. const char* cmGeneratorTarget::GetLocationForBuild() const
  757. {
  758. static std::string location;
  759. if (this->IsImported()) {
  760. location = this->Target->ImportedGetFullPath(
  761. "", cmStateEnums::RuntimeBinaryArtifact);
  762. return location.c_str();
  763. }
  764. // Now handle the deprecated build-time configuration location.
  765. location = this->GetDirectory();
  766. const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  767. if (cfgid && strcmp(cfgid, ".") != 0) {
  768. location += "/";
  769. location += cfgid;
  770. }
  771. if (this->IsAppBundleOnApple()) {
  772. std::string macdir = this->BuildBundleDirectory("", "", FullLevel);
  773. if (!macdir.empty()) {
  774. location += "/";
  775. location += macdir;
  776. }
  777. }
  778. location += "/";
  779. location += this->GetFullName("", cmStateEnums::RuntimeBinaryArtifact);
  780. return location.c_str();
  781. }
  782. bool cmGeneratorTarget::IsSystemIncludeDirectory(
  783. const std::string& dir, const std::string& config,
  784. const std::string& language) const
  785. {
  786. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  787. std::string config_upper;
  788. if (!config.empty()) {
  789. config_upper = cmSystemTools::UpperCase(config);
  790. }
  791. typedef std::map<std::string, std::vector<std::string>> IncludeCacheType;
  792. IncludeCacheType::const_iterator iter =
  793. this->SystemIncludesCache.find(config_upper);
  794. if (iter == this->SystemIncludesCache.end()) {
  795. cmGeneratorExpressionDAGChecker dagChecker(
  796. this, "SYSTEM_INCLUDE_DIRECTORIES", nullptr, nullptr);
  797. bool excludeImported = this->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  798. std::vector<std::string> result;
  799. for (std::string const& it : this->Target->GetSystemIncludeDirectories()) {
  800. cmGeneratorExpression ge;
  801. cmSystemTools::ExpandListArgument(
  802. ge.Parse(it)->Evaluate(this->LocalGenerator, config, false, this,
  803. &dagChecker, language),
  804. result);
  805. }
  806. std::vector<cmGeneratorTarget const*> const& deps =
  807. this->GetLinkImplementationClosure(config);
  808. for (cmGeneratorTarget const* dep : deps) {
  809. handleSystemIncludesDep(this->LocalGenerator, dep, config, this,
  810. &dagChecker, result, excludeImported, language);
  811. }
  812. std::for_each(result.begin(), result.end(),
  813. cmSystemTools::ConvertToUnixSlashes);
  814. std::sort(result.begin(), result.end());
  815. result.erase(std::unique(result.begin(), result.end()), result.end());
  816. IncludeCacheType::value_type entry(config_upper, result);
  817. iter = this->SystemIncludesCache.insert(entry).first;
  818. }
  819. return std::binary_search(iter->second.begin(), iter->second.end(), dir);
  820. }
  821. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  822. {
  823. return this->Target->GetPropertyAsBool(prop);
  824. }
  825. static void AddInterfaceEntries(
  826. cmGeneratorTarget const* thisTarget, std::string const& config,
  827. std::string const& prop,
  828. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries)
  829. {
  830. if (cmLinkImplementationLibraries const* impl =
  831. thisTarget->GetLinkImplementationLibraries(config)) {
  832. for (cmLinkImplItem const& lib : impl->Libraries) {
  833. if (lib.Target) {
  834. std::string uniqueName =
  835. thisTarget->GetGlobalGenerator()->IndexGeneratorTargetUniquely(
  836. lib.Target);
  837. std::string genex =
  838. "$<TARGET_PROPERTY:" + std::move(uniqueName) + "," + prop + ">";
  839. cmGeneratorExpression ge(lib.Backtrace);
  840. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  841. cge->SetEvaluateForBuildsystem(true);
  842. entries.push_back(new TargetPropertyEntryGenex(std::move(cge), lib));
  843. }
  844. }
  845. }
  846. }
  847. static void AddObjectEntries(
  848. cmGeneratorTarget const* thisTarget, std::string const& config,
  849. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries)
  850. {
  851. if (cmLinkImplementationLibraries const* impl =
  852. thisTarget->GetLinkImplementationLibraries(config)) {
  853. for (cmLinkImplItem const& lib : impl->Libraries) {
  854. if (lib.Target &&
  855. lib.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  856. std::string uniqueName =
  857. thisTarget->GetGlobalGenerator()->IndexGeneratorTargetUniquely(
  858. lib.Target);
  859. std::string genex = "$<TARGET_OBJECTS:" + std::move(uniqueName) + ">";
  860. cmGeneratorExpression ge(lib.Backtrace);
  861. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  862. cge->SetEvaluateForBuildsystem(true);
  863. entries.push_back(new TargetPropertyEntryGenex(std::move(cge), lib));
  864. }
  865. }
  866. }
  867. }
  868. static bool processSources(
  869. cmGeneratorTarget const* tgt,
  870. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  871. std::vector<BT<std::string>>& srcs,
  872. std::unordered_set<std::string>& uniqueSrcs,
  873. cmGeneratorExpressionDAGChecker* dagChecker, std::string const& config,
  874. bool debugSources)
  875. {
  876. cmMakefile* mf = tgt->Target->GetMakefile();
  877. bool contextDependent = false;
  878. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  879. cmLinkImplItem const& item = entry->LinkImplItem;
  880. std::string const& targetName = item.AsStr();
  881. std::vector<std::string> entrySources;
  882. cmSystemTools::ExpandListArgument(entry->Evaluate(tgt->GetLocalGenerator(),
  883. config, false, tgt, tgt,
  884. dagChecker),
  885. entrySources);
  886. if (entry->GetHadContextSensitiveCondition()) {
  887. contextDependent = true;
  888. }
  889. for (std::string& src : entrySources) {
  890. cmSourceFile* sf = mf->GetOrCreateSource(src);
  891. std::string e;
  892. std::string fullPath = sf->GetFullPath(&e);
  893. if (fullPath.empty()) {
  894. if (!e.empty()) {
  895. cmake* cm = tgt->GetLocalGenerator()->GetCMakeInstance();
  896. cm->IssueMessage(MessageType::FATAL_ERROR, e, tgt->GetBacktrace());
  897. }
  898. return contextDependent;
  899. }
  900. if (!targetName.empty() && !cmSystemTools::FileIsFullPath(src)) {
  901. std::ostringstream err;
  902. if (!targetName.empty()) {
  903. err << "Target \"" << targetName
  904. << "\" contains relative path in its INTERFACE_SOURCES:\n \""
  905. << src << "\"";
  906. } else {
  907. err << "Found relative path while evaluating sources of \""
  908. << tgt->GetName() << "\":\n \"" << src << "\"\n";
  909. }
  910. tgt->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  911. err.str());
  912. return contextDependent;
  913. }
  914. src = fullPath;
  915. }
  916. std::string usedSources;
  917. for (std::string const& src : entrySources) {
  918. if (uniqueSrcs.insert(src).second) {
  919. srcs.emplace_back(src, entry->GetBacktrace());
  920. if (debugSources) {
  921. usedSources += " * " + src + "\n";
  922. }
  923. }
  924. }
  925. if (!usedSources.empty()) {
  926. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  927. MessageType::LOG,
  928. std::string("Used sources for target ") + tgt->GetName() + ":\n" +
  929. usedSources,
  930. entry->GetBacktrace());
  931. }
  932. }
  933. return contextDependent;
  934. }
  935. std::vector<BT<std::string>> cmGeneratorTarget::GetSourceFilePaths(
  936. std::string const& config) const
  937. {
  938. std::vector<BT<std::string>> files;
  939. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  940. if (!this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  941. // At configure-time, this method can be called as part of getting the
  942. // LOCATION property or to export() a file to be include()d. However
  943. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  944. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  945. // behavior of CMP0024 and CMP0026 only.
  946. cmStringRange sourceEntries = this->Target->GetSourceEntries();
  947. for (std::string const& entry : sourceEntries) {
  948. std::vector<std::string> items;
  949. cmSystemTools::ExpandListArgument(entry, items);
  950. for (std::string const& item : items) {
  951. if (cmHasLiteralPrefix(item, "$<TARGET_OBJECTS:") &&
  952. item.back() == '>') {
  953. continue;
  954. }
  955. files.emplace_back(item);
  956. }
  957. }
  958. return files;
  959. }
  960. std::vector<std::string> debugProperties;
  961. const char* debugProp =
  962. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  963. if (debugProp) {
  964. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  965. }
  966. bool debugSources = !this->DebugSourcesDone &&
  967. std::find(debugProperties.begin(), debugProperties.end(), "SOURCES") !=
  968. debugProperties.end();
  969. if (this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  970. this->DebugSourcesDone = true;
  971. }
  972. cmGeneratorExpressionDAGChecker dagChecker(this, "SOURCES", nullptr,
  973. nullptr);
  974. std::unordered_set<std::string> uniqueSrcs;
  975. bool contextDependentDirectSources =
  976. processSources(this, this->SourceEntries, files, uniqueSrcs, &dagChecker,
  977. config, debugSources);
  978. // Collect INTERFACE_SOURCES of all direct link-dependencies.
  979. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  980. linkInterfaceSourcesEntries;
  981. AddInterfaceEntries(this, config, "INTERFACE_SOURCES",
  982. linkInterfaceSourcesEntries);
  983. std::vector<std::string>::size_type numFilesBefore = files.size();
  984. bool contextDependentInterfaceSources =
  985. processSources(this, linkInterfaceSourcesEntries, files, uniqueSrcs,
  986. &dagChecker, config, debugSources);
  987. // Collect TARGET_OBJECTS of direct object link-dependencies.
  988. std::vector<cmGeneratorTarget::TargetPropertyEntry*> linkObjectsEntries;
  989. AddObjectEntries(this, config, linkObjectsEntries);
  990. std::vector<std::string>::size_type numFilesBefore2 = files.size();
  991. bool contextDependentObjects =
  992. processSources(this, linkObjectsEntries, files, uniqueSrcs, &dagChecker,
  993. config, debugSources);
  994. if (!contextDependentDirectSources &&
  995. !(contextDependentInterfaceSources && numFilesBefore < files.size()) &&
  996. !(contextDependentObjects && numFilesBefore2 < files.size())) {
  997. this->LinkImplementationLanguageIsContextDependent = false;
  998. }
  999. cmDeleteAll(linkInterfaceSourcesEntries);
  1000. cmDeleteAll(linkObjectsEntries);
  1001. return files;
  1002. }
  1003. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*>& files,
  1004. const std::string& config) const
  1005. {
  1006. std::vector<BT<cmSourceFile*>> tmp = this->GetSourceFiles(config);
  1007. files.reserve(tmp.size());
  1008. for (BT<cmSourceFile*>& v : tmp) {
  1009. files.push_back(v.Value);
  1010. }
  1011. }
  1012. std::vector<BT<cmSourceFile*>> cmGeneratorTarget::GetSourceFiles(
  1013. std::string const& config) const
  1014. {
  1015. std::vector<BT<cmSourceFile*>> files;
  1016. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  1017. // Since we are still configuring not all sources may exist yet,
  1018. // so we need to avoid full source classification because that
  1019. // requires the absolute paths to all sources to be determined.
  1020. // Since this is only for compatibility with old policies that
  1021. // projects should not depend on anymore, just compute the files
  1022. // without memoizing them.
  1023. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1024. std::set<cmSourceFile*> emitted;
  1025. for (BT<std::string> const& s : srcs) {
  1026. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1027. if (emitted.insert(sf).second) {
  1028. files.emplace_back(sf, s.Backtrace);
  1029. }
  1030. }
  1031. return files;
  1032. }
  1033. KindedSources const& kinded = this->GetKindedSources(config);
  1034. files.reserve(kinded.Sources.size());
  1035. for (SourceAndKind const& si : kinded.Sources) {
  1036. files.push_back(si.Source);
  1037. }
  1038. return files;
  1039. }
  1040. void cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1041. std::vector<cmSourceFile*>& files, const std::string& config) const
  1042. {
  1043. std::vector<BT<cmSourceFile*>> tmp =
  1044. this->GetSourceFilesWithoutObjectLibraries(config);
  1045. files.reserve(tmp.size());
  1046. for (BT<cmSourceFile*>& v : tmp) {
  1047. files.push_back(v.Value);
  1048. }
  1049. }
  1050. std::vector<BT<cmSourceFile*>>
  1051. cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1052. std::string const& config) const
  1053. {
  1054. std::vector<BT<cmSourceFile*>> files;
  1055. KindedSources const& kinded = this->GetKindedSources(config);
  1056. files.reserve(kinded.Sources.size());
  1057. for (SourceAndKind const& si : kinded.Sources) {
  1058. if (si.Source.Value->GetObjectLibrary().empty()) {
  1059. files.push_back(si.Source);
  1060. }
  1061. }
  1062. return files;
  1063. }
  1064. cmGeneratorTarget::KindedSources const& cmGeneratorTarget::GetKindedSources(
  1065. std::string const& config) const
  1066. {
  1067. // If we already processed one configuration and found no dependenc
  1068. // on configuration then always use the one result.
  1069. if (!this->LinkImplementationLanguageIsContextDependent) {
  1070. return this->KindedSourcesMap.begin()->second;
  1071. }
  1072. // Lookup any existing link implementation for this configuration.
  1073. std::string const key = cmSystemTools::UpperCase(config);
  1074. KindedSourcesMapType::iterator it = this->KindedSourcesMap.find(key);
  1075. if (it != this->KindedSourcesMap.end()) {
  1076. if (!it->second.Initialized) {
  1077. std::ostringstream e;
  1078. e << "The SOURCES of \"" << this->GetName()
  1079. << "\" use a generator expression that depends on the "
  1080. "SOURCES themselves.";
  1081. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1082. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1083. static KindedSources empty;
  1084. return empty;
  1085. }
  1086. return it->second;
  1087. }
  1088. // Add an entry to the map for this configuration.
  1089. KindedSources& files = this->KindedSourcesMap[key];
  1090. this->ComputeKindedSources(files, config);
  1091. files.Initialized = true;
  1092. return files;
  1093. }
  1094. void cmGeneratorTarget::ComputeKindedSources(KindedSources& files,
  1095. std::string const& config) const
  1096. {
  1097. // Get the source file paths by string.
  1098. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1099. cmsys::RegularExpression header_regex(CM_HEADER_REGEX);
  1100. std::vector<cmSourceFile*> badObjLib;
  1101. std::set<cmSourceFile*> emitted;
  1102. for (BT<std::string> const& s : srcs) {
  1103. // Create each source at most once.
  1104. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1105. if (!emitted.insert(sf).second) {
  1106. continue;
  1107. }
  1108. // Compute the kind (classification) of this source file.
  1109. SourceKind kind;
  1110. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  1111. if (sf->GetCustomCommand()) {
  1112. kind = SourceKindCustomCommand;
  1113. } else if (this->Target->GetType() == cmStateEnums::UTILITY) {
  1114. kind = SourceKindExtra;
  1115. } else if (sf->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  1116. kind = SourceKindHeader;
  1117. } else if (sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1118. kind = SourceKindExternalObject;
  1119. } else if (!sf->GetLanguage().empty()) {
  1120. kind = SourceKindObjectSource;
  1121. } else if (ext == "def") {
  1122. kind = SourceKindModuleDefinition;
  1123. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1124. badObjLib.push_back(sf);
  1125. }
  1126. } else if (ext == "idl") {
  1127. kind = SourceKindIDL;
  1128. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1129. badObjLib.push_back(sf);
  1130. }
  1131. } else if (ext == "resx") {
  1132. kind = SourceKindResx;
  1133. // Build and save the name of the corresponding .h file
  1134. // This relationship will be used later when building the project files.
  1135. // Both names would have been auto generated from Visual Studio
  1136. // where the user supplied the file name and Visual Studio
  1137. // appended the suffix.
  1138. std::string resx = sf->GetFullPath();
  1139. std::string hFileName = resx.substr(0, resx.find_last_of('.')) + ".h";
  1140. files.ExpectedResxHeaders.insert(hFileName);
  1141. } else if (ext == "appxmanifest") {
  1142. kind = SourceKindAppManifest;
  1143. } else if (ext == "manifest") {
  1144. kind = SourceKindManifest;
  1145. } else if (ext == "pfx") {
  1146. kind = SourceKindCertificate;
  1147. } else if (ext == "xaml") {
  1148. kind = SourceKindXaml;
  1149. // Build and save the name of the corresponding .h and .cpp file
  1150. // This relationship will be used later when building the project files.
  1151. // Both names would have been auto generated from Visual Studio
  1152. // where the user supplied the file name and Visual Studio
  1153. // appended the suffix.
  1154. std::string xaml = sf->GetFullPath();
  1155. std::string hFileName = xaml + ".h";
  1156. std::string cppFileName = xaml + ".cpp";
  1157. files.ExpectedXamlHeaders.insert(hFileName);
  1158. files.ExpectedXamlSources.insert(cppFileName);
  1159. } else if (header_regex.find(sf->GetFullPath())) {
  1160. kind = SourceKindHeader;
  1161. } else {
  1162. kind = SourceKindExtra;
  1163. }
  1164. // Save this classified source file in the result vector.
  1165. files.Sources.push_back({ BT<cmSourceFile*>(sf, s.Backtrace), kind });
  1166. }
  1167. if (!badObjLib.empty()) {
  1168. std::ostringstream e;
  1169. e << "OBJECT library \"" << this->GetName() << "\" contains:\n";
  1170. for (cmSourceFile* i : badObjLib) {
  1171. e << " " << i->GetLocation().GetName() << "\n";
  1172. }
  1173. e << "but may contain only sources that compile, header files, and "
  1174. "other files that would not affect linking of a normal library.";
  1175. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1176. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1177. }
  1178. }
  1179. std::vector<cmGeneratorTarget::AllConfigSource> const&
  1180. cmGeneratorTarget::GetAllConfigSources() const
  1181. {
  1182. if (this->AllConfigSources.empty()) {
  1183. this->ComputeAllConfigSources();
  1184. }
  1185. return this->AllConfigSources;
  1186. }
  1187. void cmGeneratorTarget::ComputeAllConfigSources() const
  1188. {
  1189. std::vector<std::string> configs;
  1190. this->Makefile->GetConfigurations(configs);
  1191. std::map<cmSourceFile const*, size_t> index;
  1192. for (size_t ci = 0; ci < configs.size(); ++ci) {
  1193. KindedSources const& sources = this->GetKindedSources(configs[ci]);
  1194. for (SourceAndKind const& src : sources.Sources) {
  1195. std::map<cmSourceFile const*, size_t>::iterator mi =
  1196. index.find(src.Source.Value);
  1197. if (mi == index.end()) {
  1198. AllConfigSource acs;
  1199. acs.Source = src.Source.Value;
  1200. acs.Kind = src.Kind;
  1201. this->AllConfigSources.push_back(std::move(acs));
  1202. std::map<cmSourceFile const*, size_t>::value_type entry(
  1203. src.Source.Value, this->AllConfigSources.size() - 1);
  1204. mi = index.insert(entry).first;
  1205. }
  1206. this->AllConfigSources[mi->second].Configs.push_back(ci);
  1207. }
  1208. }
  1209. }
  1210. std::string cmGeneratorTarget::GetCompilePDBName(
  1211. const std::string& config) const
  1212. {
  1213. std::string prefix;
  1214. std::string base;
  1215. std::string suffix;
  1216. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  1217. prefix, base, suffix);
  1218. // Check for a per-configuration output directory target property.
  1219. std::string configUpper = cmSystemTools::UpperCase(config);
  1220. std::string configProp = "COMPILE_PDB_NAME_";
  1221. configProp += configUpper;
  1222. const char* config_name = this->GetProperty(configProp);
  1223. if (config_name && *config_name) {
  1224. return prefix + config_name + ".pdb";
  1225. }
  1226. const char* name = this->GetProperty("COMPILE_PDB_NAME");
  1227. if (name && *name) {
  1228. return prefix + name + ".pdb";
  1229. }
  1230. return "";
  1231. }
  1232. std::string cmGeneratorTarget::GetCompilePDBPath(
  1233. const std::string& config) const
  1234. {
  1235. std::string dir = this->GetCompilePDBDirectory(config);
  1236. std::string name = this->GetCompilePDBName(config);
  1237. if (dir.empty() && !name.empty() && this->HaveWellDefinedOutputFiles()) {
  1238. dir = this->GetPDBDirectory(config);
  1239. }
  1240. if (!dir.empty()) {
  1241. dir += "/";
  1242. }
  1243. return dir + name;
  1244. }
  1245. bool cmGeneratorTarget::HasSOName(const std::string& config) const
  1246. {
  1247. // soname is supported only for shared libraries and modules,
  1248. // and then only when the platform supports an soname flag.
  1249. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY) &&
  1250. !this->GetPropertyAsBool("NO_SONAME") &&
  1251. this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
  1252. }
  1253. bool cmGeneratorTarget::NeedRelinkBeforeInstall(
  1254. const std::string& config) const
  1255. {
  1256. // Only executables and shared libraries can have an rpath and may
  1257. // need relinking.
  1258. if (this->GetType() != cmStateEnums::EXECUTABLE &&
  1259. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1260. this->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1261. return false;
  1262. }
  1263. // If there is no install location this target will not be installed
  1264. // and therefore does not need relinking.
  1265. if (!this->Target->GetHaveInstallRule()) {
  1266. return false;
  1267. }
  1268. // If skipping all rpaths completely then no relinking is needed.
  1269. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1270. return false;
  1271. }
  1272. // If building with the install-tree rpath no relinking is needed.
  1273. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1274. return false;
  1275. }
  1276. // If chrpath is going to be used no relinking is needed.
  1277. if (this->IsChrpathUsed(config)) {
  1278. return false;
  1279. }
  1280. // Check for rpath support on this platform.
  1281. std::string ll = this->GetLinkerLanguage(config);
  1282. if (!ll.empty()) {
  1283. std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1284. flagVar += ll;
  1285. flagVar += "_FLAG";
  1286. if (!this->Makefile->IsSet(flagVar)) {
  1287. // There is no rpath support on this platform so nothing needs
  1288. // relinking.
  1289. return false;
  1290. }
  1291. } else {
  1292. // No linker language is known. This error will be reported by
  1293. // other code.
  1294. return false;
  1295. }
  1296. // If either a build or install tree rpath is set then the rpath
  1297. // will likely change between the build tree and install tree and
  1298. // this target must be relinked.
  1299. bool have_rpath =
  1300. this->HaveBuildTreeRPATH(config) || this->HaveInstallTreeRPATH();
  1301. bool is_ninja =
  1302. this->LocalGenerator->GetGlobalGenerator()->GetName() == "Ninja";
  1303. if (have_rpath && is_ninja) {
  1304. std::ostringstream w;
  1305. /* clang-format off */
  1306. w <<
  1307. "The install of the " << this->GetName() << " target requires "
  1308. "changing an RPATH from the build tree, but this is not supported "
  1309. "with the Ninja generator unless on an ELF-based platform. The "
  1310. "CMAKE_BUILD_WITH_INSTALL_RPATH variable may be set to avoid this "
  1311. "relinking step."
  1312. ;
  1313. /* clang-format on */
  1314. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1315. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  1316. }
  1317. return have_rpath;
  1318. }
  1319. bool cmGeneratorTarget::IsChrpathUsed(const std::string& config) const
  1320. {
  1321. // Only certain target types have an rpath.
  1322. if (!(this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1323. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1324. this->GetType() == cmStateEnums::EXECUTABLE)) {
  1325. return false;
  1326. }
  1327. // If the target will not be installed we do not need to change its
  1328. // rpath.
  1329. if (!this->Target->GetHaveInstallRule()) {
  1330. return false;
  1331. }
  1332. // Skip chrpath if skipping rpath altogether.
  1333. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1334. return false;
  1335. }
  1336. // Skip chrpath if it does not need to be changed at install time.
  1337. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1338. return false;
  1339. }
  1340. // Allow the user to disable builtin chrpath explicitly.
  1341. if (this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH")) {
  1342. return false;
  1343. }
  1344. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1345. return true;
  1346. }
  1347. #if defined(CMAKE_USE_ELF_PARSER)
  1348. // Enable if the rpath flag uses a separator and the target uses ELF
  1349. // binaries.
  1350. std::string ll = this->GetLinkerLanguage(config);
  1351. if (!ll.empty()) {
  1352. std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1353. sepVar += ll;
  1354. sepVar += "_FLAG_SEP";
  1355. const char* sep = this->Makefile->GetDefinition(sepVar);
  1356. if (sep && *sep) {
  1357. // TODO: Add ELF check to ABI detection and get rid of
  1358. // CMAKE_EXECUTABLE_FORMAT.
  1359. if (const char* fmt =
  1360. this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT")) {
  1361. return strcmp(fmt, "ELF") == 0;
  1362. }
  1363. }
  1364. }
  1365. #endif
  1366. static_cast<void>(config);
  1367. return false;
  1368. }
  1369. bool cmGeneratorTarget::IsImportedSharedLibWithoutSOName(
  1370. const std::string& config) const
  1371. {
  1372. if (this->IsImported() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1373. if (cmGeneratorTarget::ImportInfo const* info =
  1374. this->GetImportInfo(config)) {
  1375. return info->NoSOName;
  1376. }
  1377. }
  1378. return false;
  1379. }
  1380. bool cmGeneratorTarget::HasMacOSXRpathInstallNameDir(
  1381. const std::string& config) const
  1382. {
  1383. bool install_name_is_rpath = false;
  1384. bool macosx_rpath = false;
  1385. if (!this->IsImported()) {
  1386. if (this->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1387. return false;
  1388. }
  1389. const char* install_name = this->GetProperty("INSTALL_NAME_DIR");
  1390. bool use_install_name = this->MacOSXUseInstallNameDir();
  1391. if (install_name && use_install_name &&
  1392. std::string(install_name) == "@rpath") {
  1393. install_name_is_rpath = true;
  1394. } else if (install_name && use_install_name) {
  1395. return false;
  1396. }
  1397. if (!install_name_is_rpath) {
  1398. macosx_rpath = this->MacOSXRpathInstallNameDirDefault();
  1399. }
  1400. } else {
  1401. // Lookup the imported soname.
  1402. if (cmGeneratorTarget::ImportInfo const* info =
  1403. this->GetImportInfo(config)) {
  1404. if (!info->NoSOName && !info->SOName.empty()) {
  1405. if (info->SOName.find("@rpath/") == 0) {
  1406. install_name_is_rpath = true;
  1407. }
  1408. } else {
  1409. std::string install_name;
  1410. cmSystemTools::GuessLibraryInstallName(info->Location, install_name);
  1411. if (install_name.find("@rpath") != std::string::npos) {
  1412. install_name_is_rpath = true;
  1413. }
  1414. }
  1415. }
  1416. }
  1417. if (!install_name_is_rpath && !macosx_rpath) {
  1418. return false;
  1419. }
  1420. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1421. std::ostringstream w;
  1422. w << "Attempting to use ";
  1423. if (macosx_rpath) {
  1424. w << "MACOSX_RPATH";
  1425. } else {
  1426. w << "@rpath";
  1427. }
  1428. w << " without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being set.";
  1429. w << " This could be because you are using a Mac OS X version";
  1430. w << " less than 10.5 or because CMake's platform configuration is";
  1431. w << " corrupt.";
  1432. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1433. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  1434. }
  1435. return true;
  1436. }
  1437. bool cmGeneratorTarget::MacOSXRpathInstallNameDirDefault() const
  1438. {
  1439. // we can't do rpaths when unsupported
  1440. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1441. return false;
  1442. }
  1443. const char* macosx_rpath_str = this->GetProperty("MACOSX_RPATH");
  1444. if (macosx_rpath_str) {
  1445. return this->GetPropertyAsBool("MACOSX_RPATH");
  1446. }
  1447. cmPolicies::PolicyStatus cmp0042 = this->GetPolicyStatusCMP0042();
  1448. if (cmp0042 == cmPolicies::WARN) {
  1449. this->LocalGenerator->GetGlobalGenerator()->AddCMP0042WarnTarget(
  1450. this->GetName());
  1451. }
  1452. return cmp0042 == cmPolicies::NEW;
  1453. }
  1454. bool cmGeneratorTarget::MacOSXUseInstallNameDir() const
  1455. {
  1456. const char* build_with_install_name =
  1457. this->GetProperty("BUILD_WITH_INSTALL_NAME_DIR");
  1458. if (build_with_install_name) {
  1459. return cmSystemTools::IsOn(build_with_install_name);
  1460. }
  1461. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1462. if (cmp0068 == cmPolicies::NEW) {
  1463. return false;
  1464. }
  1465. bool use_install_name = this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1466. if (use_install_name && cmp0068 == cmPolicies::WARN) {
  1467. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1468. this->GetName());
  1469. }
  1470. return use_install_name;
  1471. }
  1472. bool cmGeneratorTarget::CanGenerateInstallNameDir(
  1473. InstallNameType name_type) const
  1474. {
  1475. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1476. if (cmp0068 == cmPolicies::NEW) {
  1477. return true;
  1478. }
  1479. bool skip = this->Makefile->IsOn("CMAKE_SKIP_RPATH");
  1480. if (name_type == INSTALL_NAME_FOR_INSTALL) {
  1481. skip |= this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  1482. } else {
  1483. skip |= this->GetPropertyAsBool("SKIP_BUILD_RPATH");
  1484. }
  1485. if (skip && cmp0068 == cmPolicies::WARN) {
  1486. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1487. this->GetName());
  1488. }
  1489. return !skip;
  1490. }
  1491. std::string cmGeneratorTarget::GetSOName(const std::string& config) const
  1492. {
  1493. if (this->IsImported()) {
  1494. // Lookup the imported soname.
  1495. if (cmGeneratorTarget::ImportInfo const* info =
  1496. this->GetImportInfo(config)) {
  1497. if (info->NoSOName) {
  1498. // The imported library has no builtin soname so the name
  1499. // searched at runtime will be just the filename.
  1500. return cmSystemTools::GetFilenameName(info->Location);
  1501. }
  1502. // Use the soname given if any.
  1503. if (info->SOName.find("@rpath/") == 0) {
  1504. return info->SOName.substr(6);
  1505. }
  1506. return info->SOName;
  1507. }
  1508. return "";
  1509. }
  1510. // Compute the soname that will be built.
  1511. return this->GetLibraryNames(config).SharedObject;
  1512. }
  1513. static bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  1514. {
  1515. return level == cmGeneratorTarget::FullLevel;
  1516. }
  1517. static bool shouldAddContentLevel(
  1518. cmGeneratorTarget::BundleDirectoryLevel level)
  1519. {
  1520. return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level);
  1521. }
  1522. std::string cmGeneratorTarget::GetAppBundleDirectory(
  1523. const std::string& config, BundleDirectoryLevel level) const
  1524. {
  1525. std::string fpath =
  1526. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  1527. fpath += ".";
  1528. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1529. if (!ext) {
  1530. ext = "app";
  1531. }
  1532. fpath += ext;
  1533. if (shouldAddContentLevel(level) &&
  1534. !this->Makefile->PlatformIsAppleEmbedded()) {
  1535. fpath += "/Contents";
  1536. if (shouldAddFullLevel(level)) {
  1537. fpath += "/MacOS";
  1538. }
  1539. }
  1540. return fpath;
  1541. }
  1542. bool cmGeneratorTarget::IsBundleOnApple() const
  1543. {
  1544. return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
  1545. this->IsCFBundleOnApple();
  1546. }
  1547. std::string cmGeneratorTarget::GetCFBundleDirectory(
  1548. const std::string& config, BundleDirectoryLevel level) const
  1549. {
  1550. std::string fpath;
  1551. fpath += this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  1552. fpath += ".";
  1553. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1554. if (!ext) {
  1555. if (this->IsXCTestOnApple()) {
  1556. ext = "xctest";
  1557. } else {
  1558. ext = "bundle";
  1559. }
  1560. }
  1561. fpath += ext;
  1562. if (shouldAddContentLevel(level) &&
  1563. !this->Makefile->PlatformIsAppleEmbedded()) {
  1564. fpath += "/Contents";
  1565. if (shouldAddFullLevel(level)) {
  1566. fpath += "/MacOS";
  1567. }
  1568. }
  1569. return fpath;
  1570. }
  1571. std::string cmGeneratorTarget::GetFrameworkDirectory(
  1572. const std::string& config, BundleDirectoryLevel level) const
  1573. {
  1574. std::string fpath;
  1575. fpath += this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  1576. fpath += ".";
  1577. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1578. if (!ext) {
  1579. ext = "framework";
  1580. }
  1581. fpath += ext;
  1582. if (shouldAddFullLevel(level) &&
  1583. !this->Makefile->PlatformIsAppleEmbedded()) {
  1584. fpath += "/Versions/";
  1585. fpath += this->GetFrameworkVersion();
  1586. }
  1587. return fpath;
  1588. }
  1589. std::string cmGeneratorTarget::GetFullName(
  1590. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1591. {
  1592. if (this->IsImported()) {
  1593. return this->GetFullNameImported(config, artifact);
  1594. }
  1595. return this->GetFullNameInternal(config, artifact);
  1596. }
  1597. std::string cmGeneratorTarget::GetInstallNameDirForBuildTree(
  1598. const std::string& config) const
  1599. {
  1600. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1601. // If building directly for installation then the build tree install_name
  1602. // is the same as the install tree.
  1603. if (this->MacOSXUseInstallNameDir()) {
  1604. return this->GetInstallNameDirForInstallTree();
  1605. }
  1606. // Use the build tree directory for the target.
  1607. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_BUILD)) {
  1608. std::string dir;
  1609. if (this->MacOSXRpathInstallNameDirDefault()) {
  1610. dir = "@rpath";
  1611. } else {
  1612. dir = this->GetDirectory(config);
  1613. }
  1614. dir += "/";
  1615. return dir;
  1616. }
  1617. }
  1618. return "";
  1619. }
  1620. std::string cmGeneratorTarget::GetInstallNameDirForInstallTree() const
  1621. {
  1622. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1623. std::string dir;
  1624. const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  1625. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_INSTALL)) {
  1626. if (install_name_dir && *install_name_dir) {
  1627. dir = install_name_dir;
  1628. dir += "/";
  1629. }
  1630. }
  1631. if (!install_name_dir) {
  1632. if (this->MacOSXRpathInstallNameDirDefault()) {
  1633. dir = "@rpath/";
  1634. }
  1635. }
  1636. return dir;
  1637. }
  1638. return "";
  1639. }
  1640. cmListFileBacktrace cmGeneratorTarget::GetBacktrace() const
  1641. {
  1642. return this->Target->GetBacktrace();
  1643. }
  1644. const std::set<BT<std::string>>& cmGeneratorTarget::GetUtilities() const
  1645. {
  1646. return this->Target->GetUtilities();
  1647. }
  1648. bool cmGeneratorTarget::HaveWellDefinedOutputFiles() const
  1649. {
  1650. return this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  1651. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1652. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1653. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  1654. this->GetType() == cmStateEnums::EXECUTABLE;
  1655. }
  1656. const std::string* cmGeneratorTarget::GetExportMacro() const
  1657. {
  1658. // Define the symbol for targets that export symbols.
  1659. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1660. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1661. this->IsExecutableWithExports()) {
  1662. if (const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL")) {
  1663. this->ExportMacro = custom_export_name;
  1664. } else {
  1665. std::string in = this->GetName();
  1666. in += "_EXPORTS";
  1667. this->ExportMacro = cmSystemTools::MakeCidentifier(in);
  1668. }
  1669. return &this->ExportMacro;
  1670. }
  1671. return nullptr;
  1672. }
  1673. class cmTargetCollectLinkLanguages
  1674. {
  1675. public:
  1676. cmTargetCollectLinkLanguages(cmGeneratorTarget const* target,
  1677. std::string config,
  1678. std::unordered_set<std::string>& languages,
  1679. cmGeneratorTarget const* head)
  1680. : Config(std::move(config))
  1681. , Languages(languages)
  1682. , HeadTarget(head)
  1683. , Target(target)
  1684. {
  1685. this->Visited.insert(target);
  1686. }
  1687. void Visit(cmLinkItem const& item)
  1688. {
  1689. if (!item.Target) {
  1690. if (item.AsStr().find("::") != std::string::npos) {
  1691. bool noMessage = false;
  1692. MessageType messageType = MessageType::FATAL_ERROR;
  1693. std::ostringstream e;
  1694. switch (this->Target->GetLocalGenerator()->GetPolicyStatus(
  1695. cmPolicies::CMP0028)) {
  1696. case cmPolicies::WARN: {
  1697. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0028) << "\n";
  1698. messageType = MessageType::AUTHOR_WARNING;
  1699. } break;
  1700. case cmPolicies::OLD:
  1701. noMessage = true;
  1702. case cmPolicies::REQUIRED_IF_USED:
  1703. case cmPolicies::REQUIRED_ALWAYS:
  1704. case cmPolicies::NEW:
  1705. // Issue the fatal message.
  1706. break;
  1707. }
  1708. if (!noMessage) {
  1709. e << "Target \"" << this->Target->GetName()
  1710. << "\" links to target \"" << item.AsStr()
  1711. << "\" but the target was not found. Perhaps a find_package() "
  1712. "call is missing for an IMPORTED target, or an ALIAS target is "
  1713. "missing?";
  1714. this->Target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1715. messageType, e.str(), this->Target->GetBacktrace());
  1716. }
  1717. }
  1718. return;
  1719. }
  1720. if (!this->Visited.insert(item.Target).second) {
  1721. return;
  1722. }
  1723. cmLinkInterface const* iface =
  1724. item.Target->GetLinkInterface(this->Config, this->HeadTarget);
  1725. if (!iface) {
  1726. return;
  1727. }
  1728. for (std::string const& language : iface->Languages) {
  1729. this->Languages.insert(language);
  1730. }
  1731. for (cmLinkItem const& lib : iface->Libraries) {
  1732. this->Visit(lib);
  1733. }
  1734. }
  1735. private:
  1736. std::string Config;
  1737. std::unordered_set<std::string>& Languages;
  1738. cmGeneratorTarget const* HeadTarget;
  1739. const cmGeneratorTarget* Target;
  1740. std::set<cmGeneratorTarget const*> Visited;
  1741. };
  1742. cmGeneratorTarget::LinkClosure const* cmGeneratorTarget::GetLinkClosure(
  1743. const std::string& config) const
  1744. {
  1745. std::string key(cmSystemTools::UpperCase(config));
  1746. LinkClosureMapType::iterator i = this->LinkClosureMap.find(key);
  1747. if (i == this->LinkClosureMap.end()) {
  1748. LinkClosure lc;
  1749. this->ComputeLinkClosure(config, lc);
  1750. LinkClosureMapType::value_type entry(key, lc);
  1751. i = this->LinkClosureMap.insert(entry).first;
  1752. }
  1753. return &i->second;
  1754. }
  1755. class cmTargetSelectLinker
  1756. {
  1757. int Preference;
  1758. cmGeneratorTarget const* Target;
  1759. cmGlobalGenerator* GG;
  1760. std::set<std::string> Preferred;
  1761. public:
  1762. cmTargetSelectLinker(cmGeneratorTarget const* target)
  1763. : Preference(0)
  1764. , Target(target)
  1765. {
  1766. this->GG = this->Target->GetLocalGenerator()->GetGlobalGenerator();
  1767. }
  1768. void Consider(const char* lang)
  1769. {
  1770. int preference = this->GG->GetLinkerPreference(lang);
  1771. if (preference > this->Preference) {
  1772. this->Preference = preference;
  1773. this->Preferred.clear();
  1774. }
  1775. if (preference == this->Preference) {
  1776. this->Preferred.insert(lang);
  1777. }
  1778. }
  1779. std::string Choose()
  1780. {
  1781. if (this->Preferred.empty()) {
  1782. return "";
  1783. }
  1784. if (this->Preferred.size() > 1) {
  1785. std::ostringstream e;
  1786. e << "Target " << this->Target->GetName()
  1787. << " contains multiple languages with the highest linker preference"
  1788. << " (" << this->Preference << "):\n";
  1789. for (std::string const& li : this->Preferred) {
  1790. e << " " << li << "\n";
  1791. }
  1792. e << "Set the LINKER_LANGUAGE property for this target.";
  1793. cmake* cm = this->Target->GetLocalGenerator()->GetCMakeInstance();
  1794. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  1795. this->Target->GetBacktrace());
  1796. }
  1797. return *this->Preferred.begin();
  1798. }
  1799. };
  1800. void cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  1801. LinkClosure& lc) const
  1802. {
  1803. // Get languages built in this target.
  1804. std::unordered_set<std::string> languages;
  1805. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  1806. assert(impl);
  1807. for (std::string const& li : impl->Languages) {
  1808. languages.insert(li);
  1809. }
  1810. // Add interface languages from linked targets.
  1811. cmTargetCollectLinkLanguages cll(this, config, languages, this);
  1812. for (cmLinkImplItem const& lib : impl->Libraries) {
  1813. cll.Visit(lib);
  1814. }
  1815. // Store the transitive closure of languages.
  1816. for (std::string const& lang : languages) {
  1817. lc.Languages.push_back(lang);
  1818. }
  1819. // Choose the language whose linker should be used.
  1820. if (this->GetProperty("HAS_CXX")) {
  1821. lc.LinkerLanguage = "CXX";
  1822. } else if (const char* linkerLang = this->GetProperty("LINKER_LANGUAGE")) {
  1823. lc.LinkerLanguage = linkerLang;
  1824. } else {
  1825. // Find the language with the highest preference value.
  1826. cmTargetSelectLinker tsl(this);
  1827. // First select from the languages compiled directly in this target.
  1828. for (std::string const& l : impl->Languages) {
  1829. tsl.Consider(l.c_str());
  1830. }
  1831. // Now consider languages that propagate from linked targets.
  1832. for (std::string const& lang : languages) {
  1833. std::string propagates =
  1834. "CMAKE_" + lang + "_LINKER_PREFERENCE_PROPAGATES";
  1835. if (this->Makefile->IsOn(propagates)) {
  1836. tsl.Consider(lang.c_str());
  1837. }
  1838. }
  1839. lc.LinkerLanguage = tsl.Choose();
  1840. }
  1841. }
  1842. void cmGeneratorTarget::GetFullNameComponents(
  1843. std::string& prefix, std::string& base, std::string& suffix,
  1844. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1845. {
  1846. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  1847. }
  1848. std::string cmGeneratorTarget::BuildBundleDirectory(
  1849. const std::string& base, const std::string& config,
  1850. BundleDirectoryLevel level) const
  1851. {
  1852. std::string fpath = base;
  1853. if (this->IsAppBundleOnApple()) {
  1854. fpath += this->GetAppBundleDirectory(config, level);
  1855. }
  1856. if (this->IsFrameworkOnApple()) {
  1857. fpath += this->GetFrameworkDirectory(config, level);
  1858. }
  1859. if (this->IsCFBundleOnApple()) {
  1860. fpath += this->GetCFBundleDirectory(config, level);
  1861. }
  1862. return fpath;
  1863. }
  1864. std::string cmGeneratorTarget::GetMacContentDirectory(
  1865. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1866. {
  1867. // Start with the output directory for the target.
  1868. std::string fpath = this->GetDirectory(config, artifact);
  1869. fpath += "/";
  1870. BundleDirectoryLevel level = ContentLevel;
  1871. if (this->IsFrameworkOnApple()) {
  1872. // additional files with a framework go into the version specific
  1873. // directory
  1874. level = FullLevel;
  1875. }
  1876. fpath = this->BuildBundleDirectory(fpath, config, level);
  1877. return fpath;
  1878. }
  1879. std::string cmGeneratorTarget::GetEffectiveFolderName() const
  1880. {
  1881. std::string effectiveFolder;
  1882. if (!this->GlobalGenerator->UseFolderProperty()) {
  1883. return effectiveFolder;
  1884. }
  1885. const char* targetFolder = this->GetProperty("FOLDER");
  1886. if (targetFolder) {
  1887. effectiveFolder += targetFolder;
  1888. }
  1889. return effectiveFolder;
  1890. }
  1891. cmGeneratorTarget::CompileInfo const* cmGeneratorTarget::GetCompileInfo(
  1892. const std::string& config) const
  1893. {
  1894. // There is no compile information for imported targets.
  1895. if (this->IsImported()) {
  1896. return nullptr;
  1897. }
  1898. if (this->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  1899. std::string msg = "cmTarget::GetCompileInfo called for ";
  1900. msg += this->GetName();
  1901. msg += " which has type ";
  1902. msg += cmState::GetTargetTypeName(this->GetType());
  1903. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  1904. return nullptr;
  1905. }
  1906. // Lookup/compute/cache the compile information for this configuration.
  1907. std::string config_upper;
  1908. if (!config.empty()) {
  1909. config_upper = cmSystemTools::UpperCase(config);
  1910. }
  1911. CompileInfoMapType::const_iterator i =
  1912. this->CompileInfoMap.find(config_upper);
  1913. if (i == this->CompileInfoMap.end()) {
  1914. CompileInfo info;
  1915. this->ComputePDBOutputDir("COMPILE_PDB", config, info.CompilePdbDir);
  1916. CompileInfoMapType::value_type entry(config_upper, info);
  1917. i = this->CompileInfoMap.insert(entry).first;
  1918. }
  1919. return &i->second;
  1920. }
  1921. cmGeneratorTarget::ModuleDefinitionInfo const*
  1922. cmGeneratorTarget::GetModuleDefinitionInfo(std::string const& config) const
  1923. {
  1924. // A module definition file only makes sense on certain target types.
  1925. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1926. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1927. !this->IsExecutableWithExports()) {
  1928. return nullptr;
  1929. }
  1930. // Lookup/compute/cache the compile information for this configuration.
  1931. std::string config_upper;
  1932. if (!config.empty()) {
  1933. config_upper = cmSystemTools::UpperCase(config);
  1934. }
  1935. ModuleDefinitionInfoMapType::const_iterator i =
  1936. this->ModuleDefinitionInfoMap.find(config_upper);
  1937. if (i == this->ModuleDefinitionInfoMap.end()) {
  1938. ModuleDefinitionInfo info;
  1939. this->ComputeModuleDefinitionInfo(config, info);
  1940. ModuleDefinitionInfoMapType::value_type entry(config_upper, info);
  1941. i = this->ModuleDefinitionInfoMap.insert(entry).first;
  1942. }
  1943. return &i->second;
  1944. }
  1945. void cmGeneratorTarget::ComputeModuleDefinitionInfo(
  1946. std::string const& config, ModuleDefinitionInfo& info) const
  1947. {
  1948. this->GetModuleDefinitionSources(info.Sources, config);
  1949. info.WindowsExportAllSymbols =
  1950. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
  1951. this->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS");
  1952. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  1953. info.DefFileGenerated =
  1954. info.WindowsExportAllSymbols || info.Sources.size() > 1;
  1955. #else
  1956. // Our __create_def helper is only available on Windows.
  1957. info.DefFileGenerated = false;
  1958. #endif
  1959. if (info.DefFileGenerated) {
  1960. info.DefFile = this->ObjectDirectory /* has slash */ + "exports.def";
  1961. } else if (!info.Sources.empty()) {
  1962. info.DefFile = info.Sources.front()->GetFullPath();
  1963. }
  1964. }
  1965. bool cmGeneratorTarget::IsDLLPlatform() const
  1966. {
  1967. return this->DLLPlatform;
  1968. }
  1969. void cmGeneratorTarget::GetAutoUicOptions(std::vector<std::string>& result,
  1970. const std::string& config) const
  1971. {
  1972. const char* prop =
  1973. this->GetLinkInterfaceDependentStringProperty("AUTOUIC_OPTIONS", config);
  1974. if (!prop) {
  1975. return;
  1976. }
  1977. cmGeneratorExpression ge;
  1978. cmGeneratorExpressionDAGChecker dagChecker(this, "AUTOUIC_OPTIONS", nullptr,
  1979. nullptr);
  1980. cmSystemTools::ExpandListArgument(
  1981. ge.Parse(prop)->Evaluate(this->LocalGenerator, config, false, this,
  1982. &dagChecker),
  1983. result);
  1984. }
  1985. void processILibs(const std::string& config,
  1986. cmGeneratorTarget const* headTarget, cmLinkItem const& item,
  1987. cmGlobalGenerator* gg,
  1988. std::vector<cmGeneratorTarget const*>& tgts,
  1989. std::set<cmGeneratorTarget const*>& emitted)
  1990. {
  1991. if (item.Target && emitted.insert(item.Target).second) {
  1992. tgts.push_back(item.Target);
  1993. if (cmLinkInterfaceLibraries const* iface =
  1994. item.Target->GetLinkInterfaceLibraries(config, headTarget, true)) {
  1995. for (cmLinkItem const& lib : iface->Libraries) {
  1996. processILibs(config, headTarget, lib, gg, tgts, emitted);
  1997. }
  1998. }
  1999. }
  2000. }
  2001. const std::vector<const cmGeneratorTarget*>&
  2002. cmGeneratorTarget::GetLinkImplementationClosure(
  2003. const std::string& config) const
  2004. {
  2005. LinkImplClosure& tgts = this->LinkImplClosureMap[config];
  2006. if (!tgts.Done) {
  2007. tgts.Done = true;
  2008. std::set<cmGeneratorTarget const*> emitted;
  2009. cmLinkImplementationLibraries const* impl =
  2010. this->GetLinkImplementationLibraries(config);
  2011. for (cmLinkImplItem const& lib : impl->Libraries) {
  2012. processILibs(config, this, lib,
  2013. this->LocalGenerator->GetGlobalGenerator(), tgts, emitted);
  2014. }
  2015. }
  2016. return tgts;
  2017. }
  2018. class cmTargetTraceDependencies
  2019. {
  2020. public:
  2021. cmTargetTraceDependencies(cmGeneratorTarget* target);
  2022. void Trace();
  2023. private:
  2024. cmGeneratorTarget* GeneratorTarget;
  2025. cmMakefile* Makefile;
  2026. cmLocalGenerator* LocalGenerator;
  2027. cmGlobalGenerator const* GlobalGenerator;
  2028. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  2029. SourceEntry* CurrentEntry;
  2030. std::queue<cmSourceFile*> SourceQueue;
  2031. std::set<cmSourceFile*> SourcesQueued;
  2032. typedef std::map<std::string, cmSourceFile*> NameMapType;
  2033. NameMapType NameMap;
  2034. std::vector<std::string> NewSources;
  2035. void QueueSource(cmSourceFile* sf);
  2036. void FollowName(std::string const& name);
  2037. void FollowNames(std::vector<std::string> const& names);
  2038. bool IsUtility(std::string const& dep);
  2039. void CheckCustomCommand(cmCustomCommand const& cc);
  2040. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  2041. void FollowCommandDepends(cmCustomCommand const& cc,
  2042. const std::string& config,
  2043. std::set<std::string>& emitted);
  2044. };
  2045. cmTargetTraceDependencies::cmTargetTraceDependencies(cmGeneratorTarget* target)
  2046. : GeneratorTarget(target)
  2047. {
  2048. // Convenience.
  2049. this->Makefile = target->Target->GetMakefile();
  2050. this->LocalGenerator = target->GetLocalGenerator();
  2051. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  2052. this->CurrentEntry = nullptr;
  2053. // Queue all the source files already specified for the target.
  2054. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2055. std::vector<std::string> configs;
  2056. this->Makefile->GetConfigurations(configs);
  2057. if (configs.empty()) {
  2058. configs.emplace_back();
  2059. }
  2060. std::set<cmSourceFile*> emitted;
  2061. for (std::string const& c : configs) {
  2062. std::vector<cmSourceFile*> sources;
  2063. this->GeneratorTarget->GetSourceFiles(sources, c);
  2064. for (cmSourceFile* sf : sources) {
  2065. const std::set<cmGeneratorTarget const*> tgts =
  2066. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  2067. if (tgts.find(this->GeneratorTarget) != tgts.end()) {
  2068. std::ostringstream e;
  2069. e << "Evaluation output file\n \"" << sf->GetFullPath()
  2070. << "\"\ndepends on the sources of a target it is used in. This "
  2071. "is a dependency loop and is not allowed.";
  2072. this->GeneratorTarget->LocalGenerator->IssueMessage(
  2073. MessageType::FATAL_ERROR, e.str());
  2074. return;
  2075. }
  2076. if (emitted.insert(sf).second &&
  2077. this->SourcesQueued.insert(sf).second) {
  2078. this->SourceQueue.push(sf);
  2079. }
  2080. }
  2081. }
  2082. }
  2083. // Queue pre-build, pre-link, and post-build rule dependencies.
  2084. this->CheckCustomCommands(this->GeneratorTarget->GetPreBuildCommands());
  2085. this->CheckCustomCommands(this->GeneratorTarget->GetPreLinkCommands());
  2086. this->CheckCustomCommands(this->GeneratorTarget->GetPostBuildCommands());
  2087. }
  2088. void cmTargetTraceDependencies::Trace()
  2089. {
  2090. // Process one dependency at a time until the queue is empty.
  2091. while (!this->SourceQueue.empty()) {
  2092. // Get the next source from the queue.
  2093. cmSourceFile* sf = this->SourceQueue.front();
  2094. this->SourceQueue.pop();
  2095. this->CurrentEntry = &this->GeneratorTarget->SourceDepends[sf];
  2096. // Queue dependencies added explicitly by the user.
  2097. if (const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS")) {
  2098. std::vector<std::string> objDeps;
  2099. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  2100. for (std::string& objDep : objDeps) {
  2101. if (cmSystemTools::FileIsFullPath(objDep)) {
  2102. objDep = cmSystemTools::CollapseFullPath(objDep);
  2103. }
  2104. }
  2105. this->FollowNames(objDeps);
  2106. }
  2107. // Queue the source needed to generate this file, if any.
  2108. this->FollowName(sf->GetFullPath());
  2109. // Queue dependencies added programmatically by commands.
  2110. this->FollowNames(sf->GetDepends());
  2111. // Queue custom command dependencies.
  2112. if (cmCustomCommand const* cc = sf->GetCustomCommand()) {
  2113. this->CheckCustomCommand(*cc);
  2114. }
  2115. }
  2116. this->CurrentEntry = nullptr;
  2117. this->GeneratorTarget->AddTracedSources(this->NewSources);
  2118. }
  2119. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  2120. {
  2121. if (this->SourcesQueued.insert(sf).second) {
  2122. this->SourceQueue.push(sf);
  2123. // Make sure this file is in the target at the end.
  2124. this->NewSources.push_back(sf->GetFullPath());
  2125. }
  2126. }
  2127. void cmTargetTraceDependencies::FollowName(std::string const& name)
  2128. {
  2129. NameMapType::iterator i = this->NameMap.find(name);
  2130. if (i == this->NameMap.end()) {
  2131. // Check if we know how to generate this file.
  2132. cmSourceFile* sf = this->Makefile->GetSourceFileWithOutput(name);
  2133. NameMapType::value_type entry(name, sf);
  2134. i = this->NameMap.insert(entry).first;
  2135. }
  2136. if (cmSourceFile* sf = i->second) {
  2137. // Record the dependency we just followed.
  2138. if (this->CurrentEntry) {
  2139. this->CurrentEntry->Depends.push_back(sf);
  2140. }
  2141. this->QueueSource(sf);
  2142. }
  2143. }
  2144. void cmTargetTraceDependencies::FollowNames(
  2145. std::vector<std::string> const& names)
  2146. {
  2147. for (std::string const& name : names) {
  2148. this->FollowName(name);
  2149. }
  2150. }
  2151. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  2152. {
  2153. // Dependencies on targets (utilities) are supposed to be named by
  2154. // just the target name. However for compatibility we support
  2155. // naming the output file generated by the target (assuming there is
  2156. // no output-name property which old code would not have set). In
  2157. // that case the target name will be the file basename of the
  2158. // dependency.
  2159. std::string util = cmSystemTools::GetFilenameName(dep);
  2160. if (cmSystemTools::GetFilenameLastExtension(util) == ".exe") {
  2161. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  2162. }
  2163. // Check for a target with this name.
  2164. if (cmGeneratorTarget* t =
  2165. this->GeneratorTarget->GetLocalGenerator()->FindGeneratorTargetToUse(
  2166. util)) {
  2167. // If we find the target and the dep was given as a full path,
  2168. // then make sure it was not a full path to something else, and
  2169. // the fact that the name matched a target was just a coincidence.
  2170. if (cmSystemTools::FileIsFullPath(dep)) {
  2171. if (t->GetType() >= cmStateEnums::EXECUTABLE &&
  2172. t->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  2173. // This is really only for compatibility so we do not need to
  2174. // worry about configuration names and output names.
  2175. std::string tLocation = t->GetLocationForBuild();
  2176. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  2177. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  2178. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  2179. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  2180. if (depLocation == tLocation) {
  2181. this->GeneratorTarget->Target->AddUtility(util);
  2182. return true;
  2183. }
  2184. }
  2185. } else {
  2186. // The original name of the dependency was not a full path. It
  2187. // must name a target, so add the target-level dependency.
  2188. this->GeneratorTarget->Target->AddUtility(util);
  2189. return true;
  2190. }
  2191. }
  2192. // The dependency does not name a target built in this project.
  2193. return false;
  2194. }
  2195. void cmTargetTraceDependencies::CheckCustomCommand(cmCustomCommand const& cc)
  2196. {
  2197. // Transform command names that reference targets built in this
  2198. // project to corresponding target-level dependencies.
  2199. cmGeneratorExpression ge(cc.GetBacktrace());
  2200. // Add target-level dependencies referenced by generator expressions.
  2201. std::set<cmGeneratorTarget*> targets;
  2202. for (cmCustomCommandLine const& cCmdLine : cc.GetCommandLines()) {
  2203. std::string const& command = cCmdLine.front();
  2204. // Check for a target with this name.
  2205. if (cmGeneratorTarget* t =
  2206. this->LocalGenerator->FindGeneratorTargetToUse(command)) {
  2207. if (t->GetType() == cmStateEnums::EXECUTABLE) {
  2208. // The command refers to an executable target built in
  2209. // this project. Add the target-level dependency to make
  2210. // sure the executable is up to date before this custom
  2211. // command possibly runs.
  2212. this->GeneratorTarget->Target->AddUtility(command);
  2213. }
  2214. }
  2215. // Check for target references in generator expressions.
  2216. for (std::string const& cl : cCmdLine) {
  2217. const std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(cl);
  2218. cge->Evaluate(this->GeneratorTarget->GetLocalGenerator(), "", true);
  2219. std::set<cmGeneratorTarget*> geTargets = cge->GetTargets();
  2220. targets.insert(geTargets.begin(), geTargets.end());
  2221. }
  2222. }
  2223. for (cmGeneratorTarget* target : targets) {
  2224. this->GeneratorTarget->Target->AddUtility(target->GetName());
  2225. }
  2226. // Queue the custom command dependencies.
  2227. std::vector<std::string> configs;
  2228. std::set<std::string> emitted;
  2229. this->Makefile->GetConfigurations(configs);
  2230. if (configs.empty()) {
  2231. configs.emplace_back();
  2232. }
  2233. for (std::string const& conf : configs) {
  2234. this->FollowCommandDepends(cc, conf, emitted);
  2235. }
  2236. }
  2237. void cmTargetTraceDependencies::FollowCommandDepends(
  2238. cmCustomCommand const& cc, const std::string& config,
  2239. std::set<std::string>& emitted)
  2240. {
  2241. cmCustomCommandGenerator ccg(cc, config,
  2242. this->GeneratorTarget->LocalGenerator);
  2243. const std::vector<std::string>& depends = ccg.GetDepends();
  2244. for (std::string const& dep : depends) {
  2245. if (emitted.insert(dep).second) {
  2246. if (!this->IsUtility(dep)) {
  2247. // The dependency does not name a target and may be a file we
  2248. // know how to generate. Queue it.
  2249. this->FollowName(dep);
  2250. }
  2251. }
  2252. }
  2253. }
  2254. void cmTargetTraceDependencies::CheckCustomCommands(
  2255. const std::vector<cmCustomCommand>& commands)
  2256. {
  2257. for (cmCustomCommand const& command : commands) {
  2258. this->CheckCustomCommand(command);
  2259. }
  2260. }
  2261. void cmGeneratorTarget::TraceDependencies()
  2262. {
  2263. // CMake-generated targets have no dependencies to trace. Normally tracing
  2264. // would find nothing anyway, but when building CMake itself the "install"
  2265. // target command ends up referencing the "cmake" target but we do not
  2266. // really want the dependency because "install" depend on "all" anyway.
  2267. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2268. return;
  2269. }
  2270. // Use a helper object to trace the dependencies.
  2271. cmTargetTraceDependencies tracer(this);
  2272. tracer.Trace();
  2273. }
  2274. std::string cmGeneratorTarget::GetCompilePDBDirectory(
  2275. const std::string& config) const
  2276. {
  2277. if (CompileInfo const* info = this->GetCompileInfo(config)) {
  2278. return info->CompilePdbDir;
  2279. }
  2280. return "";
  2281. }
  2282. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  2283. std::vector<std::string>& archVec) const
  2284. {
  2285. const char* archs = nullptr;
  2286. if (!config.empty()) {
  2287. std::string defVarName = "OSX_ARCHITECTURES_";
  2288. defVarName += cmSystemTools::UpperCase(config);
  2289. archs = this->GetProperty(defVarName);
  2290. }
  2291. if (!archs) {
  2292. archs = this->GetProperty("OSX_ARCHITECTURES");
  2293. }
  2294. if (archs) {
  2295. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  2296. }
  2297. }
  2298. //----------------------------------------------------------------------------
  2299. std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
  2300. std::string const& var, std::string const& lang,
  2301. std::string const& config) const
  2302. {
  2303. if (this->IsIPOEnabled(lang, config)) {
  2304. std::string varIPO = var + "_IPO";
  2305. if (this->Makefile->IsDefinitionSet(varIPO)) {
  2306. return varIPO;
  2307. }
  2308. }
  2309. return var;
  2310. }
  2311. //----------------------------------------------------------------------------
  2312. std::string cmGeneratorTarget::GetCreateRuleVariable(
  2313. std::string const& lang, std::string const& config) const
  2314. {
  2315. switch (this->GetType()) {
  2316. case cmStateEnums::STATIC_LIBRARY: {
  2317. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  2318. return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
  2319. }
  2320. case cmStateEnums::SHARED_LIBRARY:
  2321. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  2322. case cmStateEnums::MODULE_LIBRARY:
  2323. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  2324. case cmStateEnums::EXECUTABLE:
  2325. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  2326. default:
  2327. break;
  2328. }
  2329. return "";
  2330. }
  2331. static void processIncludeDirectories(
  2332. cmGeneratorTarget const* tgt,
  2333. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2334. std::vector<BT<std::string>>& includes,
  2335. std::unordered_set<std::string>& uniqueIncludes,
  2336. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2337. bool debugIncludes, const std::string& language)
  2338. {
  2339. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  2340. cmLinkImplItem const& item = entry->LinkImplItem;
  2341. std::string const& targetName = item.AsStr();
  2342. bool const fromImported = item.Target && item.Target->IsImported();
  2343. bool const checkCMP0027 = item.FromGenex;
  2344. std::vector<std::string> entryIncludes;
  2345. cmSystemTools::ExpandListArgument(entry->Evaluate(tgt->GetLocalGenerator(),
  2346. config, false, tgt,
  2347. dagChecker, language),
  2348. entryIncludes);
  2349. std::string usedIncludes;
  2350. for (std::string& entryInclude : entryIncludes) {
  2351. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  2352. std::ostringstream e;
  2353. MessageType messageType = MessageType::FATAL_ERROR;
  2354. if (checkCMP0027) {
  2355. switch (tgt->GetPolicyStatusCMP0027()) {
  2356. case cmPolicies::WARN:
  2357. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0027) << "\n";
  2358. CM_FALLTHROUGH;
  2359. case cmPolicies::OLD:
  2360. messageType = MessageType::AUTHOR_WARNING;
  2361. break;
  2362. case cmPolicies::REQUIRED_ALWAYS:
  2363. case cmPolicies::REQUIRED_IF_USED:
  2364. case cmPolicies::NEW:
  2365. break;
  2366. }
  2367. }
  2368. /* clang-format off */
  2369. e << "Imported target \"" << targetName << "\" includes "
  2370. "non-existent path\n \"" << entryInclude << "\"\nin its "
  2371. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  2372. "* The path was deleted, renamed, or moved to another "
  2373. "location.\n"
  2374. "* An install or uninstall procedure did not complete "
  2375. "successfully.\n"
  2376. "* The installation package was faulty and references files it "
  2377. "does not provide.\n";
  2378. /* clang-format on */
  2379. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2380. return;
  2381. }
  2382. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  2383. std::ostringstream e;
  2384. bool noMessage = false;
  2385. MessageType messageType = MessageType::FATAL_ERROR;
  2386. if (!targetName.empty()) {
  2387. /* clang-format off */
  2388. e << "Target \"" << targetName << "\" contains relative "
  2389. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  2390. " \"" << entryInclude << "\"";
  2391. /* clang-format on */
  2392. } else {
  2393. switch (tgt->GetPolicyStatusCMP0021()) {
  2394. case cmPolicies::WARN: {
  2395. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  2396. messageType = MessageType::AUTHOR_WARNING;
  2397. } break;
  2398. case cmPolicies::OLD:
  2399. noMessage = true;
  2400. case cmPolicies::REQUIRED_IF_USED:
  2401. case cmPolicies::REQUIRED_ALWAYS:
  2402. case cmPolicies::NEW:
  2403. // Issue the fatal message.
  2404. break;
  2405. }
  2406. e << "Found relative path while evaluating include directories of "
  2407. "\""
  2408. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  2409. }
  2410. if (!noMessage) {
  2411. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2412. if (messageType == MessageType::FATAL_ERROR) {
  2413. return;
  2414. }
  2415. }
  2416. }
  2417. if (!cmSystemTools::IsOff(entryInclude)) {
  2418. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  2419. }
  2420. std::string inc = entryInclude;
  2421. if (uniqueIncludes.insert(inc).second) {
  2422. includes.emplace_back(inc, entry->GetBacktrace());
  2423. if (debugIncludes) {
  2424. usedIncludes += " * " + inc + "\n";
  2425. }
  2426. }
  2427. }
  2428. if (!usedIncludes.empty()) {
  2429. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2430. MessageType::LOG,
  2431. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  2432. usedIncludes,
  2433. entry->GetBacktrace());
  2434. }
  2435. }
  2436. }
  2437. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  2438. const std::string& config, const std::string& lang) const
  2439. {
  2440. std::vector<BT<std::string>> includes;
  2441. std::unordered_set<std::string> uniqueIncludes;
  2442. cmGeneratorExpressionDAGChecker dagChecker(this, "INCLUDE_DIRECTORIES",
  2443. nullptr, nullptr);
  2444. std::vector<std::string> debugProperties;
  2445. const char* debugProp =
  2446. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2447. if (debugProp) {
  2448. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2449. }
  2450. bool debugIncludes = !this->DebugIncludesDone &&
  2451. std::find(debugProperties.begin(), debugProperties.end(),
  2452. "INCLUDE_DIRECTORIES") != debugProperties.end();
  2453. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2454. this->DebugIncludesDone = true;
  2455. }
  2456. processIncludeDirectories(this, this->IncludeDirectoriesEntries, includes,
  2457. uniqueIncludes, &dagChecker, config, debugIncludes,
  2458. lang);
  2459. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2460. linkInterfaceIncludeDirectoriesEntries;
  2461. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES",
  2462. linkInterfaceIncludeDirectoriesEntries);
  2463. if (this->Makefile->IsOn("APPLE")) {
  2464. cmLinkImplementationLibraries const* impl =
  2465. this->GetLinkImplementationLibraries(config);
  2466. for (cmLinkImplItem const& lib : impl->Libraries) {
  2467. std::string libDir = cmSystemTools::CollapseFullPath(lib.AsStr());
  2468. static cmsys::RegularExpression frameworkCheck(
  2469. "(.*\\.framework)(/Versions/[^/]+)?/[^/]+$");
  2470. if (!frameworkCheck.find(libDir)) {
  2471. continue;
  2472. }
  2473. libDir = frameworkCheck.match(1);
  2474. linkInterfaceIncludeDirectoriesEntries.push_back(
  2475. CreateTargetPropertyEntry(libDir));
  2476. }
  2477. }
  2478. processIncludeDirectories(this, linkInterfaceIncludeDirectoriesEntries,
  2479. includes, uniqueIncludes, &dagChecker, config,
  2480. debugIncludes, lang);
  2481. cmDeleteAll(linkInterfaceIncludeDirectoriesEntries);
  2482. return includes;
  2483. }
  2484. enum class OptionsParse
  2485. {
  2486. None,
  2487. Shell
  2488. };
  2489. static void processOptionsInternal(
  2490. cmGeneratorTarget const* tgt,
  2491. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2492. std::vector<BT<std::string>>& options,
  2493. std::unordered_set<std::string>& uniqueOptions,
  2494. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2495. bool debugOptions, const char* logName, std::string const& language,
  2496. OptionsParse parse)
  2497. {
  2498. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  2499. std::vector<std::string> entryOptions;
  2500. cmSystemTools::ExpandListArgument(entry->Evaluate(tgt->GetLocalGenerator(),
  2501. config, false, tgt,
  2502. dagChecker, language),
  2503. entryOptions);
  2504. std::string usedOptions;
  2505. for (std::string const& opt : entryOptions) {
  2506. if (uniqueOptions.insert(opt).second) {
  2507. if (parse == OptionsParse::Shell &&
  2508. cmHasLiteralPrefix(opt, "SHELL:")) {
  2509. std::vector<std::string> tmp;
  2510. cmSystemTools::ParseUnixCommandLine(opt.c_str() + 6, tmp);
  2511. for (std::string& o : tmp) {
  2512. options.emplace_back(std::move(o), entry->GetBacktrace());
  2513. }
  2514. } else {
  2515. options.emplace_back(opt, entry->GetBacktrace());
  2516. }
  2517. if (debugOptions) {
  2518. usedOptions += " * " + opt + "\n";
  2519. }
  2520. }
  2521. }
  2522. if (!usedOptions.empty()) {
  2523. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2524. MessageType::LOG,
  2525. std::string("Used ") + logName + std::string(" for target ") +
  2526. tgt->GetName() + ":\n" + usedOptions,
  2527. entry->GetBacktrace());
  2528. }
  2529. }
  2530. }
  2531. static void processCompileOptions(
  2532. cmGeneratorTarget const* tgt,
  2533. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2534. std::vector<BT<std::string>>& options,
  2535. std::unordered_set<std::string>& uniqueOptions,
  2536. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2537. bool debugOptions, std::string const& language)
  2538. {
  2539. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2540. config, debugOptions, "compile options", language,
  2541. OptionsParse::Shell);
  2542. }
  2543. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  2544. const std::string& config,
  2545. const std::string& language) const
  2546. {
  2547. std::vector<BT<std::string>> tmp = this->GetCompileOptions(config, language);
  2548. result.reserve(tmp.size());
  2549. for (BT<std::string>& v : tmp) {
  2550. result.emplace_back(std::move(v.Value));
  2551. }
  2552. }
  2553. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileOptions(
  2554. std::string const& config, std::string const& language) const
  2555. {
  2556. std::vector<BT<std::string>> result;
  2557. std::unordered_set<std::string> uniqueOptions;
  2558. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_OPTIONS", nullptr,
  2559. nullptr);
  2560. std::vector<std::string> debugProperties;
  2561. const char* debugProp =
  2562. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2563. if (debugProp) {
  2564. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2565. }
  2566. bool debugOptions = !this->DebugCompileOptionsDone &&
  2567. std::find(debugProperties.begin(), debugProperties.end(),
  2568. "COMPILE_OPTIONS") != debugProperties.end();
  2569. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2570. this->DebugCompileOptionsDone = true;
  2571. }
  2572. processCompileOptions(this, this->CompileOptionsEntries, result,
  2573. uniqueOptions, &dagChecker, config, debugOptions,
  2574. language);
  2575. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2576. linkInterfaceCompileOptionsEntries;
  2577. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS",
  2578. linkInterfaceCompileOptionsEntries);
  2579. processCompileOptions(this, linkInterfaceCompileOptionsEntries, result,
  2580. uniqueOptions, &dagChecker, config, debugOptions,
  2581. language);
  2582. cmDeleteAll(linkInterfaceCompileOptionsEntries);
  2583. return result;
  2584. }
  2585. static void processCompileFeatures(
  2586. cmGeneratorTarget const* tgt,
  2587. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2588. std::vector<BT<std::string>>& options,
  2589. std::unordered_set<std::string>& uniqueOptions,
  2590. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2591. bool debugOptions)
  2592. {
  2593. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2594. config, debugOptions, "compile features",
  2595. std::string(), OptionsParse::None);
  2596. }
  2597. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  2598. const std::string& config) const
  2599. {
  2600. std::vector<BT<std::string>> tmp = this->GetCompileFeatures(config);
  2601. result.reserve(tmp.size());
  2602. for (BT<std::string>& v : tmp) {
  2603. result.emplace_back(std::move(v.Value));
  2604. }
  2605. }
  2606. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileFeatures(
  2607. std::string const& config) const
  2608. {
  2609. std::vector<BT<std::string>> result;
  2610. std::unordered_set<std::string> uniqueFeatures;
  2611. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_FEATURES", nullptr,
  2612. nullptr);
  2613. std::vector<std::string> debugProperties;
  2614. const char* debugProp =
  2615. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2616. if (debugProp) {
  2617. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2618. }
  2619. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  2620. std::find(debugProperties.begin(), debugProperties.end(),
  2621. "COMPILE_FEATURES") != debugProperties.end();
  2622. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2623. this->DebugCompileFeaturesDone = true;
  2624. }
  2625. processCompileFeatures(this, this->CompileFeaturesEntries, result,
  2626. uniqueFeatures, &dagChecker, config, debugFeatures);
  2627. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2628. linkInterfaceCompileFeaturesEntries;
  2629. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  2630. linkInterfaceCompileFeaturesEntries);
  2631. processCompileFeatures(this, linkInterfaceCompileFeaturesEntries, result,
  2632. uniqueFeatures, &dagChecker, config, debugFeatures);
  2633. cmDeleteAll(linkInterfaceCompileFeaturesEntries);
  2634. return result;
  2635. }
  2636. static void processCompileDefinitions(
  2637. cmGeneratorTarget const* tgt,
  2638. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2639. std::vector<BT<std::string>>& options,
  2640. std::unordered_set<std::string>& uniqueOptions,
  2641. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2642. bool debugOptions, std::string const& language)
  2643. {
  2644. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2645. config, debugOptions, "compile definitions", language,
  2646. OptionsParse::None);
  2647. }
  2648. void cmGeneratorTarget::GetCompileDefinitions(
  2649. std::vector<std::string>& result, const std::string& config,
  2650. const std::string& language) const
  2651. {
  2652. std::vector<BT<std::string>> tmp =
  2653. this->GetCompileDefinitions(config, language);
  2654. result.reserve(tmp.size());
  2655. for (BT<std::string>& v : tmp) {
  2656. result.emplace_back(std::move(v.Value));
  2657. }
  2658. }
  2659. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileDefinitions(
  2660. std::string const& config, std::string const& language) const
  2661. {
  2662. std::vector<BT<std::string>> list;
  2663. std::unordered_set<std::string> uniqueOptions;
  2664. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_DEFINITIONS",
  2665. nullptr, nullptr);
  2666. std::vector<std::string> debugProperties;
  2667. const char* debugProp =
  2668. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2669. if (debugProp) {
  2670. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2671. }
  2672. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  2673. std::find(debugProperties.begin(), debugProperties.end(),
  2674. "COMPILE_DEFINITIONS") != debugProperties.end();
  2675. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2676. this->DebugCompileDefinitionsDone = true;
  2677. }
  2678. processCompileDefinitions(this, this->CompileDefinitionsEntries, list,
  2679. uniqueOptions, &dagChecker, config, debugDefines,
  2680. language);
  2681. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2682. linkInterfaceCompileDefinitionsEntries;
  2683. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS",
  2684. linkInterfaceCompileDefinitionsEntries);
  2685. if (!config.empty()) {
  2686. std::string configPropName =
  2687. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  2688. const char* configProp = this->GetProperty(configPropName);
  2689. if (configProp) {
  2690. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  2691. case cmPolicies::WARN: {
  2692. std::ostringstream e;
  2693. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0043);
  2694. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  2695. e.str());
  2696. CM_FALLTHROUGH;
  2697. }
  2698. case cmPolicies::OLD: {
  2699. linkInterfaceCompileDefinitionsEntries.push_back(
  2700. CreateTargetPropertyEntry(configProp));
  2701. } break;
  2702. case cmPolicies::NEW:
  2703. case cmPolicies::REQUIRED_ALWAYS:
  2704. case cmPolicies::REQUIRED_IF_USED:
  2705. break;
  2706. }
  2707. }
  2708. }
  2709. processCompileDefinitions(this, linkInterfaceCompileDefinitionsEntries, list,
  2710. uniqueOptions, &dagChecker, config, debugDefines,
  2711. language);
  2712. cmDeleteAll(linkInterfaceCompileDefinitionsEntries);
  2713. return list;
  2714. }
  2715. namespace {
  2716. void processLinkOptions(
  2717. cmGeneratorTarget const* tgt,
  2718. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2719. std::vector<BT<std::string>>& options,
  2720. std::unordered_set<std::string>& uniqueOptions,
  2721. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2722. bool debugOptions, std::string const& language)
  2723. {
  2724. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2725. config, debugOptions, "link options", language,
  2726. OptionsParse::Shell);
  2727. }
  2728. }
  2729. void cmGeneratorTarget::GetLinkOptions(std::vector<std::string>& result,
  2730. const std::string& config,
  2731. const std::string& language) const
  2732. {
  2733. std::vector<BT<std::string>> tmp = this->GetLinkOptions(config, language);
  2734. result.reserve(tmp.size());
  2735. for (BT<std::string>& v : tmp) {
  2736. result.emplace_back(std::move(v.Value));
  2737. }
  2738. }
  2739. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkOptions(
  2740. std::string const& config, std::string const& language) const
  2741. {
  2742. std::vector<BT<std::string>> result;
  2743. std::unordered_set<std::string> uniqueOptions;
  2744. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_OPTIONS", nullptr,
  2745. nullptr);
  2746. std::vector<std::string> debugProperties;
  2747. const char* debugProp =
  2748. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2749. if (debugProp) {
  2750. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2751. }
  2752. bool debugOptions = !this->DebugLinkOptionsDone &&
  2753. std::find(debugProperties.begin(), debugProperties.end(),
  2754. "LINK_OPTIONS") != debugProperties.end();
  2755. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2756. this->DebugLinkOptionsDone = true;
  2757. }
  2758. processLinkOptions(this, this->LinkOptionsEntries, result, uniqueOptions,
  2759. &dagChecker, config, debugOptions, language);
  2760. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2761. linkInterfaceLinkOptionsEntries;
  2762. AddInterfaceEntries(this, config, "INTERFACE_LINK_OPTIONS",
  2763. linkInterfaceLinkOptionsEntries);
  2764. processLinkOptions(this, linkInterfaceLinkOptionsEntries, result,
  2765. uniqueOptions, &dagChecker, config, debugOptions,
  2766. language);
  2767. cmDeleteAll(linkInterfaceLinkOptionsEntries);
  2768. // Last step: replace "LINKER:" prefixed elements by
  2769. // actual linker wrapper
  2770. const std::string wrapper(this->Makefile->GetSafeDefinition(
  2771. "CMAKE_" + language + "_LINKER_WRAPPER_FLAG"));
  2772. std::vector<std::string> wrapperFlag;
  2773. cmSystemTools::ExpandListArgument(wrapper, wrapperFlag);
  2774. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  2775. "CMAKE_" + language + "_LINKER_WRAPPER_FLAG_SEP"));
  2776. bool concatFlagAndArgs = true;
  2777. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  2778. concatFlagAndArgs = false;
  2779. wrapperFlag.pop_back();
  2780. }
  2781. const std::string LINKER{ "LINKER:" };
  2782. const std::string SHELL{ "SHELL:" };
  2783. const std::string LINKER_SHELL = LINKER + SHELL;
  2784. std::vector<BT<std::string>>::iterator entry;
  2785. while ((entry = std::find_if(result.begin(), result.end(),
  2786. [&LINKER](BT<std::string> const& item) -> bool {
  2787. return item.Value.compare(0, LINKER.length(),
  2788. LINKER) == 0;
  2789. })) != result.end()) {
  2790. std::string value = std::move(entry->Value);
  2791. cmListFileBacktrace bt = std::move(entry->Backtrace);
  2792. entry = result.erase(entry);
  2793. std::vector<std::string> linkerOptions;
  2794. if (value.compare(0, LINKER_SHELL.length(), LINKER_SHELL) == 0) {
  2795. cmSystemTools::ParseUnixCommandLine(
  2796. value.c_str() + LINKER_SHELL.length(), linkerOptions);
  2797. } else {
  2798. linkerOptions =
  2799. cmSystemTools::tokenize(value.substr(LINKER.length()), ",");
  2800. }
  2801. if (linkerOptions.empty() ||
  2802. (linkerOptions.size() == 1 && linkerOptions.front().empty())) {
  2803. continue;
  2804. }
  2805. // for now, raise an error if prefix SHELL: is part of arguments
  2806. if (std::find_if(linkerOptions.begin(), linkerOptions.end(),
  2807. [&SHELL](const std::string& item) -> bool {
  2808. return item.find(SHELL) != std::string::npos;
  2809. }) != linkerOptions.end()) {
  2810. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  2811. MessageType::FATAL_ERROR,
  2812. "'SHELL:' prefix is not supported as part of 'LINKER:' arguments.",
  2813. this->GetBacktrace());
  2814. return result;
  2815. }
  2816. std::vector<BT<std::string>> options;
  2817. if (wrapperFlag.empty()) {
  2818. // nothing specified, insert elements as is
  2819. options.reserve(linkerOptions.size());
  2820. for (std::string& o : linkerOptions) {
  2821. options.emplace_back(std::move(o), bt);
  2822. }
  2823. } else {
  2824. if (!wrapperSep.empty()) {
  2825. if (concatFlagAndArgs) {
  2826. // insert flag elements except last one
  2827. for (auto i = wrapperFlag.begin(); i != wrapperFlag.end() - 1; ++i) {
  2828. options.emplace_back(*i, bt);
  2829. }
  2830. // concatenate last flag element and all LINKER list values
  2831. // in one option
  2832. options.emplace_back(
  2833. wrapperFlag.back() + cmJoin(linkerOptions, wrapperSep), bt);
  2834. } else {
  2835. for (std::string const& i : wrapperFlag) {
  2836. options.emplace_back(i, bt);
  2837. }
  2838. // concatenate all LINKER list values in one option
  2839. options.emplace_back(cmJoin(linkerOptions, wrapperSep), bt);
  2840. }
  2841. } else {
  2842. // prefix each element of LINKER list with wrapper
  2843. if (concatFlagAndArgs) {
  2844. std::transform(linkerOptions.begin(), linkerOptions.end(),
  2845. linkerOptions.begin(),
  2846. [&wrapperFlag](std::string const& o) -> std::string {
  2847. return wrapperFlag.back() + o;
  2848. });
  2849. }
  2850. for (std::string& o : linkerOptions) {
  2851. for (auto i = wrapperFlag.begin(),
  2852. e = concatFlagAndArgs ? wrapperFlag.end() - 1
  2853. : wrapperFlag.end();
  2854. i != e; ++i) {
  2855. options.emplace_back(*i, bt);
  2856. }
  2857. options.emplace_back(std::move(o), bt);
  2858. }
  2859. }
  2860. }
  2861. result.insert(entry, options.begin(), options.end());
  2862. }
  2863. return result;
  2864. }
  2865. namespace {
  2866. void processStaticLibraryLinkOptions(
  2867. cmGeneratorTarget const* tgt,
  2868. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2869. std::vector<BT<std::string>>& options,
  2870. std::unordered_set<std::string>& uniqueOptions,
  2871. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2872. std::string const& language)
  2873. {
  2874. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2875. config, false, "static library link options",
  2876. language, OptionsParse::Shell);
  2877. }
  2878. }
  2879. void cmGeneratorTarget::GetStaticLibraryLinkOptions(
  2880. std::vector<std::string>& result, const std::string& config,
  2881. const std::string& language) const
  2882. {
  2883. std::vector<BT<std::string>> tmp =
  2884. this->GetStaticLibraryLinkOptions(config, language);
  2885. result.reserve(tmp.size());
  2886. for (BT<std::string>& v : tmp) {
  2887. result.emplace_back(std::move(v.Value));
  2888. }
  2889. }
  2890. std::vector<BT<std::string>> cmGeneratorTarget::GetStaticLibraryLinkOptions(
  2891. std::string const& config, std::string const& language) const
  2892. {
  2893. std::vector<BT<std::string>> result;
  2894. std::vector<cmGeneratorTarget::TargetPropertyEntry*> entries;
  2895. std::unordered_set<std::string> uniqueOptions;
  2896. cmGeneratorExpressionDAGChecker dagChecker(this, "STATIC_LIBRARY_OPTIONS",
  2897. nullptr, nullptr);
  2898. if (const char* linkOptions = this->GetProperty("STATIC_LIBRARY_OPTIONS")) {
  2899. std::vector<std::string> options;
  2900. cmSystemTools::ExpandListArgument(linkOptions, options);
  2901. for (const auto& option : options) {
  2902. entries.push_back(CreateTargetPropertyEntry(option));
  2903. }
  2904. }
  2905. processStaticLibraryLinkOptions(this, entries, result, uniqueOptions,
  2906. &dagChecker, config, language);
  2907. cmDeleteAll(entries);
  2908. return result;
  2909. }
  2910. namespace {
  2911. void processLinkDirectories(
  2912. cmGeneratorTarget const* tgt,
  2913. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2914. std::vector<BT<std::string>>& directories,
  2915. std::unordered_set<std::string>& uniqueDirectories,
  2916. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2917. bool debugDirectories, std::string const& language)
  2918. {
  2919. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  2920. cmLinkImplItem const& item = entry->LinkImplItem;
  2921. std::string const& targetName = item.AsStr();
  2922. std::vector<std::string> entryDirectories;
  2923. cmSystemTools::ExpandListArgument(entry->Evaluate(tgt->GetLocalGenerator(),
  2924. config, false, tgt,
  2925. dagChecker, language),
  2926. entryDirectories);
  2927. std::string usedDirectories;
  2928. for (std::string& entryDirectory : entryDirectories) {
  2929. if (!cmSystemTools::FileIsFullPath(entryDirectory)) {
  2930. std::ostringstream e;
  2931. bool noMessage = false;
  2932. MessageType messageType = MessageType::FATAL_ERROR;
  2933. if (!targetName.empty()) {
  2934. /* clang-format off */
  2935. e << "Target \"" << targetName << "\" contains relative "
  2936. "path in its INTERFACE_LINK_DIRECTORIES:\n"
  2937. " \"" << entryDirectory << "\"";
  2938. /* clang-format on */
  2939. } else {
  2940. switch (tgt->GetPolicyStatusCMP0081()) {
  2941. case cmPolicies::WARN: {
  2942. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0081) << "\n";
  2943. messageType = MessageType::AUTHOR_WARNING;
  2944. } break;
  2945. case cmPolicies::OLD:
  2946. noMessage = true;
  2947. break;
  2948. case cmPolicies::REQUIRED_IF_USED:
  2949. case cmPolicies::REQUIRED_ALWAYS:
  2950. case cmPolicies::NEW:
  2951. // Issue the fatal message.
  2952. break;
  2953. }
  2954. e << "Found relative path while evaluating link directories of "
  2955. "\""
  2956. << tgt->GetName() << "\":\n \"" << entryDirectory << "\"\n";
  2957. }
  2958. if (!noMessage) {
  2959. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2960. if (messageType == MessageType::FATAL_ERROR) {
  2961. return;
  2962. }
  2963. }
  2964. }
  2965. // Sanitize the path the same way the link_directories command does
  2966. // in case projects set the LINK_DIRECTORIES property directly.
  2967. cmSystemTools::ConvertToUnixSlashes(entryDirectory);
  2968. if (uniqueDirectories.insert(entryDirectory).second) {
  2969. directories.emplace_back(entryDirectory);
  2970. if (debugDirectories) {
  2971. usedDirectories += " * " + entryDirectory + "\n";
  2972. }
  2973. }
  2974. }
  2975. if (!usedDirectories.empty()) {
  2976. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2977. MessageType::LOG,
  2978. std::string("Used link directories for target ") + tgt->GetName() +
  2979. ":\n" + usedDirectories,
  2980. entry->GetBacktrace());
  2981. }
  2982. }
  2983. }
  2984. }
  2985. void cmGeneratorTarget::GetLinkDirectories(std::vector<std::string>& result,
  2986. const std::string& config,
  2987. const std::string& language) const
  2988. {
  2989. std::vector<BT<std::string>> tmp =
  2990. this->GetLinkDirectories(config, language);
  2991. result.reserve(tmp.size());
  2992. for (BT<std::string>& v : tmp) {
  2993. result.emplace_back(std::move(v.Value));
  2994. }
  2995. }
  2996. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDirectories(
  2997. std::string const& config, std::string const& language) const
  2998. {
  2999. std::vector<BT<std::string>> result;
  3000. std::unordered_set<std::string> uniqueDirectories;
  3001. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DIRECTORIES", nullptr,
  3002. nullptr);
  3003. std::vector<std::string> debugProperties;
  3004. const char* debugProp =
  3005. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  3006. if (debugProp) {
  3007. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  3008. }
  3009. bool debugDirectories = !this->DebugLinkDirectoriesDone &&
  3010. std::find(debugProperties.begin(), debugProperties.end(),
  3011. "LINK_DIRECTORIES") != debugProperties.end();
  3012. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3013. this->DebugLinkDirectoriesDone = true;
  3014. }
  3015. processLinkDirectories(this, this->LinkDirectoriesEntries, result,
  3016. uniqueDirectories, &dagChecker, config,
  3017. debugDirectories, language);
  3018. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  3019. linkInterfaceLinkDirectoriesEntries;
  3020. AddInterfaceEntries(this, config, "INTERFACE_LINK_DIRECTORIES",
  3021. linkInterfaceLinkDirectoriesEntries);
  3022. processLinkDirectories(this, linkInterfaceLinkDirectoriesEntries, result,
  3023. uniqueDirectories, &dagChecker, config,
  3024. debugDirectories, language);
  3025. cmDeleteAll(linkInterfaceLinkDirectoriesEntries);
  3026. return result;
  3027. }
  3028. namespace {
  3029. void processLinkDepends(
  3030. cmGeneratorTarget const* tgt,
  3031. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  3032. std::vector<BT<std::string>>& options,
  3033. std::unordered_set<std::string>& uniqueOptions,
  3034. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  3035. std::string const& language)
  3036. {
  3037. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  3038. config, false, "link depends", language,
  3039. OptionsParse::None);
  3040. }
  3041. }
  3042. void cmGeneratorTarget::GetLinkDepends(std::vector<std::string>& result,
  3043. const std::string& config,
  3044. const std::string& language) const
  3045. {
  3046. std::vector<BT<std::string>> tmp = this->GetLinkDepends(config, language);
  3047. result.reserve(tmp.size());
  3048. for (BT<std::string>& v : tmp) {
  3049. result.emplace_back(std::move(v.Value));
  3050. }
  3051. }
  3052. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDepends(
  3053. std::string const& config, std::string const& language) const
  3054. {
  3055. std::vector<BT<std::string>> result;
  3056. std::vector<cmGeneratorTarget::TargetPropertyEntry*> linkDependsEntries;
  3057. std::unordered_set<std::string> uniqueOptions;
  3058. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DEPENDS", nullptr,
  3059. nullptr);
  3060. if (const char* linkDepends = this->GetProperty("LINK_DEPENDS")) {
  3061. std::vector<std::string> depends;
  3062. cmSystemTools::ExpandListArgument(linkDepends, depends);
  3063. for (const auto& depend : depends) {
  3064. linkDependsEntries.push_back(CreateTargetPropertyEntry(depend));
  3065. }
  3066. }
  3067. AddInterfaceEntries(this, config, "INTERFACE_LINK_DEPENDS",
  3068. linkDependsEntries);
  3069. processLinkDepends(this, linkDependsEntries, result, uniqueOptions,
  3070. &dagChecker, config, language);
  3071. cmDeleteAll(linkDependsEntries);
  3072. return result;
  3073. }
  3074. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  3075. {
  3076. if (this->IsImported()) {
  3077. return;
  3078. }
  3079. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  3080. // Get the names.
  3081. cmGeneratorTarget::Names targetNames;
  3082. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  3083. targetNames = this->GetExecutableNames(config);
  3084. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3085. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3086. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3087. targetNames = this->GetLibraryNames(config);
  3088. } else {
  3089. return;
  3090. }
  3091. // Get the directory.
  3092. std::string dir =
  3093. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  3094. // Add each name.
  3095. std::string f;
  3096. if (!targetNames.Output.empty()) {
  3097. f = dir;
  3098. f += "/";
  3099. f += targetNames.Output;
  3100. gg->AddToManifest(f);
  3101. }
  3102. if (!targetNames.SharedObject.empty()) {
  3103. f = dir;
  3104. f += "/";
  3105. f += targetNames.SharedObject;
  3106. gg->AddToManifest(f);
  3107. }
  3108. if (!targetNames.Real.empty()) {
  3109. f = dir;
  3110. f += "/";
  3111. f += targetNames.Real;
  3112. gg->AddToManifest(f);
  3113. }
  3114. if (!targetNames.PDB.empty()) {
  3115. f = dir;
  3116. f += "/";
  3117. f += targetNames.PDB;
  3118. gg->AddToManifest(f);
  3119. }
  3120. if (!targetNames.ImportLibrary.empty()) {
  3121. f = this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  3122. f += "/";
  3123. f += targetNames.ImportLibrary;
  3124. gg->AddToManifest(f);
  3125. }
  3126. }
  3127. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  3128. {
  3129. std::vector<BT<std::string>> features = this->GetCompileFeatures(config);
  3130. for (BT<std::string> const& f : features) {
  3131. if (!this->Makefile->AddRequiredTargetFeature(this->Target, f.Value)) {
  3132. return false;
  3133. }
  3134. }
  3135. return true;
  3136. }
  3137. std::string cmGeneratorTarget::GetImportedLibName(
  3138. std::string const& config) const
  3139. {
  3140. if (cmGeneratorTarget::ImportInfo const* info =
  3141. this->GetImportInfo(config)) {
  3142. return info->LibName;
  3143. }
  3144. return std::string();
  3145. }
  3146. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  3147. cmStateEnums::ArtifactType artifact,
  3148. bool realname) const
  3149. {
  3150. if (this->IsImported()) {
  3151. return this->Target->ImportedGetFullPath(config, artifact);
  3152. }
  3153. return this->NormalGetFullPath(config, artifact, realname);
  3154. }
  3155. std::string cmGeneratorTarget::NormalGetFullPath(
  3156. const std::string& config, cmStateEnums::ArtifactType artifact,
  3157. bool realname) const
  3158. {
  3159. std::string fpath = this->GetDirectory(config, artifact);
  3160. fpath += "/";
  3161. if (this->IsAppBundleOnApple()) {
  3162. fpath = this->BuildBundleDirectory(fpath, config, FullLevel);
  3163. fpath += "/";
  3164. }
  3165. // Add the full name of the target.
  3166. switch (artifact) {
  3167. case cmStateEnums::RuntimeBinaryArtifact:
  3168. if (realname) {
  3169. fpath += this->NormalGetRealName(config);
  3170. } else {
  3171. fpath +=
  3172. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  3173. }
  3174. break;
  3175. case cmStateEnums::ImportLibraryArtifact:
  3176. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  3177. break;
  3178. }
  3179. return fpath;
  3180. }
  3181. std::string cmGeneratorTarget::NormalGetRealName(
  3182. const std::string& config) const
  3183. {
  3184. // This should not be called for imported targets.
  3185. // TODO: Split cmTarget into a class hierarchy to get compile-time
  3186. // enforcement of the limited imported target API.
  3187. if (this->IsImported()) {
  3188. std::string msg = "NormalGetRealName called on imported target: ";
  3189. msg += this->GetName();
  3190. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  3191. }
  3192. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  3193. // Compute the real name that will be built.
  3194. return this->GetExecutableNames(config).Real;
  3195. }
  3196. // Compute the real name that will be built.
  3197. return this->GetLibraryNames(config).Real;
  3198. }
  3199. cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
  3200. const std::string& config) const
  3201. {
  3202. cmGeneratorTarget::Names targetNames;
  3203. // This should not be called for imported targets.
  3204. // TODO: Split cmTarget into a class hierarchy to get compile-time
  3205. // enforcement of the limited imported target API.
  3206. if (this->IsImported()) {
  3207. std::string msg = "GetLibraryNames called on imported target: ";
  3208. msg += this->GetName();
  3209. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  3210. }
  3211. // Check for library version properties.
  3212. const char* version = this->GetProperty("VERSION");
  3213. const char* soversion = this->GetProperty("SOVERSION");
  3214. if (!this->HasSOName(config) ||
  3215. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  3216. this->IsFrameworkOnApple()) {
  3217. // Versioning is supported only for shared libraries and modules,
  3218. // and then only when the platform supports an soname flag.
  3219. version = nullptr;
  3220. soversion = nullptr;
  3221. }
  3222. if (version && !soversion) {
  3223. // The soversion must be set if the library version is set. Use
  3224. // the library version as the soversion.
  3225. soversion = version;
  3226. }
  3227. if (!version && soversion) {
  3228. // Use the soversion as the library version.
  3229. version = soversion;
  3230. }
  3231. // Get the components of the library name.
  3232. std::string prefix;
  3233. std::string suffix;
  3234. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  3235. prefix, targetNames.Base, suffix);
  3236. // The library name.
  3237. targetNames.Output = prefix + targetNames.Base + suffix;
  3238. if (this->IsFrameworkOnApple()) {
  3239. targetNames.Real = prefix;
  3240. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  3241. targetNames.Real += "Versions/";
  3242. targetNames.Real += this->GetFrameworkVersion();
  3243. targetNames.Real += "/";
  3244. }
  3245. targetNames.Real += targetNames.Base;
  3246. targetNames.SharedObject = targetNames.Real;
  3247. } else {
  3248. // The library's soname.
  3249. this->ComputeVersionedName(targetNames.SharedObject, prefix,
  3250. targetNames.Base, suffix, targetNames.Output,
  3251. soversion);
  3252. // The library's real name on disk.
  3253. this->ComputeVersionedName(targetNames.Real, prefix, targetNames.Base,
  3254. suffix, targetNames.Output, version);
  3255. }
  3256. // The import library name.
  3257. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3258. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3259. targetNames.ImportLibrary =
  3260. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  3261. }
  3262. // The program database file name.
  3263. targetNames.PDB = this->GetPDBName(config);
  3264. return targetNames;
  3265. }
  3266. cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
  3267. const std::string& config) const
  3268. {
  3269. cmGeneratorTarget::Names targetNames;
  3270. // This should not be called for imported targets.
  3271. // TODO: Split cmTarget into a class hierarchy to get compile-time
  3272. // enforcement of the limited imported target API.
  3273. if (this->IsImported()) {
  3274. std::string msg = "GetExecutableNames called on imported target: ";
  3275. msg += this->GetName();
  3276. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  3277. }
  3278. // This versioning is supported only for executables and then only
  3279. // when the platform supports symbolic links.
  3280. #if defined(_WIN32) && !defined(__CYGWIN__)
  3281. const char* version = 0;
  3282. #else
  3283. // Check for executable version properties.
  3284. const char* version = this->GetProperty("VERSION");
  3285. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  3286. this->Makefile->IsOn("XCODE")) {
  3287. version = nullptr;
  3288. }
  3289. #endif
  3290. // Get the components of the executable name.
  3291. std::string prefix;
  3292. std::string suffix;
  3293. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  3294. prefix, targetNames.Base, suffix);
  3295. // The executable name.
  3296. targetNames.Output = prefix + targetNames.Base + suffix;
  3297. // The executable's real name on disk.
  3298. #if defined(__CYGWIN__)
  3299. targetNames.Real = prefix + targetNames.Base;
  3300. #else
  3301. targetNames.Real = targetNames.Output;
  3302. #endif
  3303. if (version) {
  3304. targetNames.Real += "-";
  3305. targetNames.Real += version;
  3306. }
  3307. #if defined(__CYGWIN__)
  3308. targetNames.Real += suffix;
  3309. #endif
  3310. // The import library name.
  3311. targetNames.ImportLibrary =
  3312. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  3313. // The program database file name.
  3314. targetNames.PDB = this->GetPDBName(config);
  3315. return targetNames;
  3316. }
  3317. std::string cmGeneratorTarget::GetFullNameInternal(
  3318. const std::string& config, cmStateEnums::ArtifactType artifact) const
  3319. {
  3320. std::string prefix;
  3321. std::string base;
  3322. std::string suffix;
  3323. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  3324. return prefix + base + suffix;
  3325. }
  3326. std::string cmGeneratorTarget::ImportedGetLocation(
  3327. const std::string& config) const
  3328. {
  3329. assert(this->IsImported());
  3330. return this->Target->ImportedGetFullPath(
  3331. config, cmStateEnums::RuntimeBinaryArtifact);
  3332. }
  3333. std::string cmGeneratorTarget::GetFullNameImported(
  3334. const std::string& config, cmStateEnums::ArtifactType artifact) const
  3335. {
  3336. return cmSystemTools::GetFilenameName(
  3337. this->Target->ImportedGetFullPath(config, artifact));
  3338. }
  3339. void cmGeneratorTarget::GetFullNameInternal(
  3340. const std::string& config, cmStateEnums::ArtifactType artifact,
  3341. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  3342. {
  3343. // Use just the target name for non-main target types.
  3344. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  3345. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3346. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  3347. this->GetType() != cmStateEnums::EXECUTABLE) {
  3348. outPrefix.clear();
  3349. outBase = this->GetName();
  3350. outSuffix.clear();
  3351. return;
  3352. }
  3353. const bool isImportedLibraryArtifact =
  3354. (artifact == cmStateEnums::ImportLibraryArtifact);
  3355. // Return an empty name for the import library if this platform
  3356. // does not support import libraries.
  3357. if (isImportedLibraryArtifact &&
  3358. !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  3359. outPrefix.clear();
  3360. outBase.clear();
  3361. outSuffix.clear();
  3362. return;
  3363. }
  3364. // The implib option is only allowed for shared libraries, module
  3365. // libraries, and executables.
  3366. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3367. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  3368. this->GetType() != cmStateEnums::EXECUTABLE) {
  3369. artifact = cmStateEnums::RuntimeBinaryArtifact;
  3370. }
  3371. // Compute the full name for main target types.
  3372. const char* targetPrefix =
  3373. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
  3374. : this->GetProperty("PREFIX"));
  3375. const char* targetSuffix =
  3376. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
  3377. : this->GetProperty("SUFFIX"));
  3378. const char* configPostfix = nullptr;
  3379. if (!config.empty()) {
  3380. std::string configProp = cmSystemTools::UpperCase(config);
  3381. configProp += "_POSTFIX";
  3382. configPostfix = this->GetProperty(configProp);
  3383. // Mac application bundles and frameworks have no postfix.
  3384. if (configPostfix &&
  3385. (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
  3386. configPostfix = nullptr;
  3387. }
  3388. }
  3389. const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
  3390. const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
  3391. // Check for language-specific default prefix and suffix.
  3392. std::string ll = this->GetLinkerLanguage(config);
  3393. if (!ll.empty()) {
  3394. if (!targetSuffix && suffixVar && *suffixVar) {
  3395. std::string langSuff = suffixVar + std::string("_") + ll;
  3396. targetSuffix = this->Makefile->GetDefinition(langSuff);
  3397. }
  3398. if (!targetPrefix && prefixVar && *prefixVar) {
  3399. std::string langPrefix = prefixVar + std::string("_") + ll;
  3400. targetPrefix = this->Makefile->GetDefinition(langPrefix);
  3401. }
  3402. }
  3403. // if there is no prefix on the target use the cmake definition
  3404. if (!targetPrefix && prefixVar) {
  3405. targetPrefix = this->Makefile->GetSafeDefinition(prefixVar).c_str();
  3406. }
  3407. // if there is no suffix on the target use the cmake definition
  3408. if (!targetSuffix && suffixVar) {
  3409. targetSuffix = this->Makefile->GetSafeDefinition(suffixVar).c_str();
  3410. }
  3411. // frameworks have directory prefix but no suffix
  3412. std::string fw_prefix;
  3413. if (this->IsFrameworkOnApple()) {
  3414. fw_prefix = this->GetFrameworkDirectory(config, ContentLevel);
  3415. fw_prefix += "/";
  3416. targetPrefix = fw_prefix.c_str();
  3417. targetSuffix = nullptr;
  3418. }
  3419. if (this->IsCFBundleOnApple()) {
  3420. fw_prefix = this->GetCFBundleDirectory(config, FullLevel);
  3421. fw_prefix += "/";
  3422. targetPrefix = fw_prefix.c_str();
  3423. targetSuffix = nullptr;
  3424. }
  3425. // Begin the final name with the prefix.
  3426. outPrefix = targetPrefix ? targetPrefix : "";
  3427. // Append the target name or property-specified name.
  3428. outBase += this->GetOutputName(config, artifact);
  3429. // Append the per-configuration postfix.
  3430. outBase += configPostfix ? configPostfix : "";
  3431. // Name shared libraries with their version number on some platforms.
  3432. if (const char* soversion = this->GetProperty("SOVERSION")) {
  3433. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  3434. !isImportedLibraryArtifact &&
  3435. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  3436. outBase += "-";
  3437. outBase += soversion;
  3438. }
  3439. }
  3440. // Append the suffix.
  3441. outSuffix = targetSuffix ? targetSuffix : "";
  3442. }
  3443. std::string cmGeneratorTarget::GetLinkerLanguage(
  3444. const std::string& config) const
  3445. {
  3446. return this->GetLinkClosure(config)->LinkerLanguage;
  3447. }
  3448. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  3449. {
  3450. std::string prefix;
  3451. std::string base;
  3452. std::string suffix;
  3453. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  3454. prefix, base, suffix);
  3455. std::vector<std::string> props;
  3456. std::string configUpper = cmSystemTools::UpperCase(config);
  3457. if (!configUpper.empty()) {
  3458. // PDB_NAME_<CONFIG>
  3459. props.push_back("PDB_NAME_" + configUpper);
  3460. }
  3461. // PDB_NAME
  3462. props.emplace_back("PDB_NAME");
  3463. for (std::string const& p : props) {
  3464. if (const char* outName = this->GetProperty(p)) {
  3465. base = outName;
  3466. break;
  3467. }
  3468. }
  3469. return prefix + base + ".pdb";
  3470. }
  3471. std::string cmGeneratorTarget::GetObjectDirectory(
  3472. std::string const& config) const
  3473. {
  3474. std::string obj_dir =
  3475. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  3476. #if defined(__APPLE__)
  3477. // find and replace $(PROJECT_NAME) xcode placeholder
  3478. const std::string projectName = this->LocalGenerator->GetProjectName();
  3479. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  3480. // Replace Xcode's placeholder for the object file directory since
  3481. // installation and export scripts need to know the real directory.
  3482. // Xcode has build-time settings (e.g. for sanitizers) that affect this,
  3483. // but we use the default here. Users that want to enable sanitizers
  3484. // will do so at the cost of object library installation and export.
  3485. cmSystemTools::ReplaceString(obj_dir, "$(OBJECT_FILE_DIR_normal:base)",
  3486. "Objects-normal");
  3487. #endif
  3488. return obj_dir;
  3489. }
  3490. void cmGeneratorTarget::GetTargetObjectNames(
  3491. std::string const& config, std::vector<std::string>& objects) const
  3492. {
  3493. std::vector<cmSourceFile const*> objectSources;
  3494. this->GetObjectSources(objectSources, config);
  3495. std::map<cmSourceFile const*, std::string> mapping;
  3496. for (cmSourceFile const* sf : objectSources) {
  3497. mapping[sf];
  3498. }
  3499. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  3500. for (cmSourceFile const* src : objectSources) {
  3501. // Find the object file name corresponding to this source file.
  3502. std::map<cmSourceFile const*, std::string>::const_iterator map_it =
  3503. mapping.find(src);
  3504. // It must exist because we populated the mapping just above.
  3505. assert(!map_it->second.empty());
  3506. objects.push_back(map_it->second);
  3507. }
  3508. }
  3509. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  3510. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  3511. {
  3512. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  3513. if (nameResult == 0) {
  3514. return strcmp(
  3515. t1->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str(),
  3516. t2->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str()) < 0;
  3517. }
  3518. return nameResult < 0;
  3519. }
  3520. struct cmGeneratorTarget::SourceFileFlags
  3521. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  3522. {
  3523. struct SourceFileFlags flags;
  3524. this->ConstructSourceFileFlags();
  3525. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  3526. this->SourceFlagsMap.find(sf);
  3527. if (si != this->SourceFlagsMap.end()) {
  3528. flags = si->second;
  3529. } else {
  3530. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  3531. // were not listed in one of the other lists.
  3532. if (const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  3533. flags.MacFolder = location;
  3534. const bool stripResources =
  3535. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  3536. if (strcmp(location, "Resources") == 0) {
  3537. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3538. if (stripResources) {
  3539. flags.MacFolder = "";
  3540. }
  3541. } else if (cmSystemTools::StringStartsWith(location, "Resources/")) {
  3542. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  3543. if (stripResources) {
  3544. flags.MacFolder += strlen("Resources/");
  3545. }
  3546. } else {
  3547. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  3548. }
  3549. }
  3550. }
  3551. return flags;
  3552. }
  3553. void cmGeneratorTarget::ConstructSourceFileFlags() const
  3554. {
  3555. if (this->SourceFileFlagsConstructed) {
  3556. return;
  3557. }
  3558. this->SourceFileFlagsConstructed = true;
  3559. // Process public headers to mark the source files.
  3560. if (const char* files = this->GetProperty("PUBLIC_HEADER")) {
  3561. std::vector<std::string> relFiles;
  3562. cmSystemTools::ExpandListArgument(files, relFiles);
  3563. for (std::string const& relFile : relFiles) {
  3564. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3565. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3566. flags.MacFolder = "Headers";
  3567. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  3568. }
  3569. }
  3570. }
  3571. // Process private headers after public headers so that they take
  3572. // precedence if a file is listed in both.
  3573. if (const char* files = this->GetProperty("PRIVATE_HEADER")) {
  3574. std::vector<std::string> relFiles;
  3575. cmSystemTools::ExpandListArgument(files, relFiles);
  3576. for (std::string const& relFile : relFiles) {
  3577. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3578. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3579. flags.MacFolder = "PrivateHeaders";
  3580. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  3581. }
  3582. }
  3583. }
  3584. // Mark sources listed as resources.
  3585. if (const char* files = this->GetProperty("RESOURCE")) {
  3586. std::vector<std::string> relFiles;
  3587. cmSystemTools::ExpandListArgument(files, relFiles);
  3588. for (std::string const& relFile : relFiles) {
  3589. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3590. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3591. flags.MacFolder = "";
  3592. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  3593. flags.MacFolder = "Resources";
  3594. }
  3595. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3596. }
  3597. }
  3598. }
  3599. }
  3600. const cmGeneratorTarget::CompatibleInterfacesBase&
  3601. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  3602. {
  3603. cmGeneratorTarget::CompatibleInterfaces& compat =
  3604. this->CompatibleInterfacesMap[config];
  3605. if (!compat.Done) {
  3606. compat.Done = true;
  3607. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  3608. compat.PropsString.insert("AUTOUIC_OPTIONS");
  3609. std::vector<cmGeneratorTarget const*> const& deps =
  3610. this->GetLinkImplementationClosure(config);
  3611. for (cmGeneratorTarget const* li : deps) {
  3612. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  3613. if (const char* prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  3614. std::vector<std::string> props; \
  3615. cmSystemTools::ExpandListArgument(prop, props); \
  3616. compat.Props##x.insert(props.begin(), props.end()); \
  3617. }
  3618. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  3619. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  3620. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  3621. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  3622. #undef CM_READ_COMPATIBLE_INTERFACE
  3623. }
  3624. }
  3625. return compat;
  3626. }
  3627. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  3628. const std::string& p, const std::string& config) const
  3629. {
  3630. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3631. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3632. return false;
  3633. }
  3634. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  3635. }
  3636. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  3637. const std::string& p, const std::string& config) const
  3638. {
  3639. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3640. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3641. return false;
  3642. }
  3643. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  3644. }
  3645. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  3646. const std::string& p, const std::string& config) const
  3647. {
  3648. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3649. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3650. return false;
  3651. }
  3652. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  3653. }
  3654. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  3655. const std::string& p, const std::string& config) const
  3656. {
  3657. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3658. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3659. return false;
  3660. }
  3661. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  3662. }
  3663. enum CompatibleType
  3664. {
  3665. BoolType,
  3666. StringType,
  3667. NumberMinType,
  3668. NumberMaxType
  3669. };
  3670. template <typename PropertyType>
  3671. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3672. const std::string& prop,
  3673. const std::string& config,
  3674. CompatibleType, PropertyType*);
  3675. template <>
  3676. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3677. const std::string& prop,
  3678. const std::string& config,
  3679. CompatibleType /*unused*/,
  3680. bool* /*unused*/)
  3681. {
  3682. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  3683. }
  3684. template <>
  3685. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3686. const std::string& prop,
  3687. const std::string& config,
  3688. CompatibleType t,
  3689. const char** /*unused*/)
  3690. {
  3691. switch (t) {
  3692. case BoolType:
  3693. assert(false &&
  3694. "String compatibility check function called for boolean");
  3695. return nullptr;
  3696. case StringType:
  3697. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  3698. case NumberMinType:
  3699. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  3700. case NumberMaxType:
  3701. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  3702. }
  3703. assert(false && "Unreachable!");
  3704. return nullptr;
  3705. }
  3706. template <typename PropertyType>
  3707. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  3708. cmGeneratorTarget const* dependee,
  3709. const std::string& propName,
  3710. std::set<std::string>& emitted,
  3711. const std::string& config, CompatibleType t,
  3712. PropertyType* /*unused*/)
  3713. {
  3714. const char* prop = dependee->GetProperty(propName);
  3715. if (!prop) {
  3716. return;
  3717. }
  3718. std::vector<std::string> props;
  3719. cmSystemTools::ExpandListArgument(prop, props);
  3720. std::string pdir = cmSystemTools::GetCMakeRoot();
  3721. pdir += "/Help/prop_tgt/";
  3722. for (std::string const& p : props) {
  3723. std::string pname = cmSystemTools::HelpFileName(p);
  3724. std::string pfile = pdir + pname + ".rst";
  3725. if (cmSystemTools::FileExists(pfile, true)) {
  3726. std::ostringstream e;
  3727. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  3728. << "\" listed in its " << propName
  3729. << " property. "
  3730. "This is not allowed. Only user-defined properties may appear "
  3731. "listed in the "
  3732. << propName << " property.";
  3733. depender->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  3734. e.str());
  3735. return;
  3736. }
  3737. if (emitted.insert(p).second) {
  3738. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  3739. nullptr);
  3740. if (cmSystemTools::GetErrorOccuredFlag()) {
  3741. return;
  3742. }
  3743. }
  3744. }
  3745. }
  3746. static std::string intersect(const std::set<std::string>& s1,
  3747. const std::set<std::string>& s2)
  3748. {
  3749. std::set<std::string> intersect;
  3750. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  3751. std::inserter(intersect, intersect.begin()));
  3752. if (!intersect.empty()) {
  3753. return *intersect.begin();
  3754. }
  3755. return "";
  3756. }
  3757. static std::string intersect(const std::set<std::string>& s1,
  3758. const std::set<std::string>& s2,
  3759. const std::set<std::string>& s3)
  3760. {
  3761. std::string result;
  3762. result = intersect(s1, s2);
  3763. if (!result.empty()) {
  3764. return result;
  3765. }
  3766. result = intersect(s1, s3);
  3767. if (!result.empty()) {
  3768. return result;
  3769. }
  3770. return intersect(s2, s3);
  3771. }
  3772. static std::string intersect(const std::set<std::string>& s1,
  3773. const std::set<std::string>& s2,
  3774. const std::set<std::string>& s3,
  3775. const std::set<std::string>& s4)
  3776. {
  3777. std::string result;
  3778. result = intersect(s1, s2);
  3779. if (!result.empty()) {
  3780. return result;
  3781. }
  3782. result = intersect(s1, s3);
  3783. if (!result.empty()) {
  3784. return result;
  3785. }
  3786. result = intersect(s1, s4);
  3787. if (!result.empty()) {
  3788. return result;
  3789. }
  3790. return intersect(s2, s3, s4);
  3791. }
  3792. void cmGeneratorTarget::CheckPropertyCompatibility(
  3793. cmComputeLinkInformation* info, const std::string& config) const
  3794. {
  3795. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  3796. std::set<std::string> emittedBools;
  3797. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  3798. std::set<std::string> emittedStrings;
  3799. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  3800. std::set<std::string> emittedMinNumbers;
  3801. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  3802. std::set<std::string> emittedMaxNumbers;
  3803. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  3804. for (auto const& dep : deps) {
  3805. if (!dep.Target) {
  3806. continue;
  3807. }
  3808. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  3809. config, BoolType, nullptr);
  3810. if (cmSystemTools::GetErrorOccuredFlag()) {
  3811. return;
  3812. }
  3813. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  3814. emittedStrings, config, StringType,
  3815. nullptr);
  3816. if (cmSystemTools::GetErrorOccuredFlag()) {
  3817. return;
  3818. }
  3819. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  3820. emittedMinNumbers, config,
  3821. NumberMinType, nullptr);
  3822. if (cmSystemTools::GetErrorOccuredFlag()) {
  3823. return;
  3824. }
  3825. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  3826. emittedMaxNumbers, config,
  3827. NumberMaxType, nullptr);
  3828. if (cmSystemTools::GetErrorOccuredFlag()) {
  3829. return;
  3830. }
  3831. }
  3832. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  3833. emittedMaxNumbers);
  3834. if (!prop.empty()) {
  3835. // Use a sorted std::vector to keep the error message sorted.
  3836. std::vector<std::string> props;
  3837. std::set<std::string>::const_iterator i = emittedBools.find(prop);
  3838. if (i != emittedBools.end()) {
  3839. props.push_back(strBool);
  3840. }
  3841. i = emittedStrings.find(prop);
  3842. if (i != emittedStrings.end()) {
  3843. props.push_back(strString);
  3844. }
  3845. i = emittedMinNumbers.find(prop);
  3846. if (i != emittedMinNumbers.end()) {
  3847. props.push_back(strNumMin);
  3848. }
  3849. i = emittedMaxNumbers.find(prop);
  3850. if (i != emittedMaxNumbers.end()) {
  3851. props.push_back(strNumMax);
  3852. }
  3853. std::sort(props.begin(), props.end());
  3854. std::string propsString = cmJoin(cmMakeRange(props).retreat(1), ", ");
  3855. propsString += " and the " + props.back();
  3856. std::ostringstream e;
  3857. e << "Property \"" << prop << "\" appears in both the " << propsString
  3858. << " property in the dependencies of target \"" << this->GetName()
  3859. << "\". This is not allowed. A property may only require compatibility "
  3860. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  3861. "or a "
  3862. "string interpretation, but not a mixture.";
  3863. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  3864. }
  3865. }
  3866. template <typename PropertyType>
  3867. std::string valueAsString(PropertyType);
  3868. template <>
  3869. std::string valueAsString<bool>(bool value)
  3870. {
  3871. return value ? "TRUE" : "FALSE";
  3872. }
  3873. template <>
  3874. std::string valueAsString<const char*>(const char* value)
  3875. {
  3876. return value ? value : "(unset)";
  3877. }
  3878. template <>
  3879. std::string valueAsString<std::string>(std::string value)
  3880. {
  3881. return value;
  3882. }
  3883. template <>
  3884. std::string valueAsString<std::nullptr_t>(std::nullptr_t /*unused*/)
  3885. {
  3886. return "(unset)";
  3887. }
  3888. std::string compatibilityType(CompatibleType t)
  3889. {
  3890. switch (t) {
  3891. case BoolType:
  3892. return "Boolean compatibility";
  3893. case StringType:
  3894. return "String compatibility";
  3895. case NumberMaxType:
  3896. return "Numeric maximum compatibility";
  3897. case NumberMinType:
  3898. return "Numeric minimum compatibility";
  3899. }
  3900. assert(false && "Unreachable!");
  3901. return "";
  3902. }
  3903. std::string compatibilityAgree(CompatibleType t, bool dominant)
  3904. {
  3905. switch (t) {
  3906. case BoolType:
  3907. case StringType:
  3908. return dominant ? "(Disagree)\n" : "(Agree)\n";
  3909. case NumberMaxType:
  3910. case NumberMinType:
  3911. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  3912. }
  3913. assert(false && "Unreachable!");
  3914. return "";
  3915. }
  3916. template <typename PropertyType>
  3917. PropertyType getTypedProperty(
  3918. cmGeneratorTarget const* tgt, const std::string& prop,
  3919. cmGeneratorExpressionInterpreter* genexInterpreter = nullptr);
  3920. template <>
  3921. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  3922. const std::string& prop,
  3923. cmGeneratorExpressionInterpreter* genexInterpreter)
  3924. {
  3925. if (genexInterpreter == nullptr) {
  3926. return tgt->GetPropertyAsBool(prop);
  3927. }
  3928. const char* value = tgt->GetProperty(prop);
  3929. return cmSystemTools::IsOn(genexInterpreter->Evaluate(value, prop));
  3930. }
  3931. template <>
  3932. const char* getTypedProperty<const char*>(
  3933. cmGeneratorTarget const* tgt, const std::string& prop,
  3934. cmGeneratorExpressionInterpreter* genexInterpreter)
  3935. {
  3936. const char* value = tgt->GetProperty(prop);
  3937. if (genexInterpreter == nullptr) {
  3938. return value;
  3939. }
  3940. return genexInterpreter->Evaluate(value, prop).c_str();
  3941. }
  3942. template <>
  3943. std::string getTypedProperty<std::string>(
  3944. cmGeneratorTarget const* tgt, const std::string& prop,
  3945. cmGeneratorExpressionInterpreter* genexInterpreter)
  3946. {
  3947. const char* value = tgt->GetProperty(prop);
  3948. if (genexInterpreter == nullptr) {
  3949. return valueAsString(value);
  3950. }
  3951. return genexInterpreter->Evaluate(value, prop);
  3952. }
  3953. template <typename PropertyType>
  3954. PropertyType impliedValue(PropertyType);
  3955. template <>
  3956. bool impliedValue<bool>(bool /*unused*/)
  3957. {
  3958. return false;
  3959. }
  3960. template <>
  3961. const char* impliedValue<const char*>(const char* /*unused*/)
  3962. {
  3963. return "";
  3964. }
  3965. template <>
  3966. std::string impliedValue<std::string>(std::string /*unused*/) // NOLINT(*)
  3967. {
  3968. return std::string();
  3969. }
  3970. template <typename PropertyType>
  3971. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  3972. PropertyType rhs,
  3973. CompatibleType t);
  3974. template <>
  3975. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  3976. CompatibleType /*unused*/)
  3977. {
  3978. return std::make_pair(lhs == rhs, lhs);
  3979. }
  3980. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  3981. const char* rhs)
  3982. {
  3983. const bool b = strcmp(lhs, rhs) == 0;
  3984. return std::make_pair(b, b ? lhs : nullptr);
  3985. }
  3986. std::pair<bool, std::string> consistentStringProperty(const std::string& lhs,
  3987. const std::string& rhs)
  3988. {
  3989. const bool b = lhs == rhs;
  3990. return std::make_pair(b, b ? lhs : valueAsString(nullptr));
  3991. }
  3992. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  3993. const char* rhs,
  3994. CompatibleType t)
  3995. {
  3996. char* pEnd;
  3997. const char* const null_ptr = nullptr;
  3998. long lnum = strtol(lhs, &pEnd, 0);
  3999. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  4000. return std::pair<bool, const char*>(false, null_ptr);
  4001. }
  4002. long rnum = strtol(rhs, &pEnd, 0);
  4003. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  4004. return std::pair<bool, const char*>(false, null_ptr);
  4005. }
  4006. if (t == NumberMaxType) {
  4007. return std::make_pair(true, std::max(lnum, rnum) == lnum ? lhs : rhs);
  4008. }
  4009. return std::make_pair(true, std::min(lnum, rnum) == lnum ? lhs : rhs);
  4010. }
  4011. template <>
  4012. std::pair<bool, const char*> consistentProperty(const char* lhs,
  4013. const char* rhs,
  4014. CompatibleType t)
  4015. {
  4016. if (!lhs && !rhs) {
  4017. return std::make_pair(true, lhs);
  4018. }
  4019. if (!lhs) {
  4020. return std::make_pair(true, rhs);
  4021. }
  4022. if (!rhs) {
  4023. return std::make_pair(true, lhs);
  4024. }
  4025. const char* const null_ptr = nullptr;
  4026. switch (t) {
  4027. case BoolType: {
  4028. bool same = cmSystemTools::IsOn(lhs) == cmSystemTools::IsOn(rhs);
  4029. return std::make_pair(same, same ? lhs : nullptr);
  4030. }
  4031. case StringType:
  4032. return consistentStringProperty(lhs, rhs);
  4033. case NumberMinType:
  4034. case NumberMaxType:
  4035. return consistentNumberProperty(lhs, rhs, t);
  4036. }
  4037. assert(false && "Unreachable!");
  4038. return std::pair<bool, const char*>(false, null_ptr);
  4039. }
  4040. std::pair<bool, std::string> consistentProperty(const std::string& lhs,
  4041. const std::string& rhs,
  4042. CompatibleType t)
  4043. {
  4044. const std::string null_ptr = valueAsString(nullptr);
  4045. if (lhs == null_ptr && rhs == null_ptr) {
  4046. return std::make_pair(true, lhs);
  4047. }
  4048. if (lhs == null_ptr) {
  4049. return std::make_pair(true, rhs);
  4050. }
  4051. if (rhs == null_ptr) {
  4052. return std::make_pair(true, lhs);
  4053. }
  4054. switch (t) {
  4055. case BoolType: {
  4056. bool same = cmSystemTools::IsOn(lhs) == cmSystemTools::IsOn(rhs);
  4057. return std::make_pair(same, same ? lhs : null_ptr);
  4058. }
  4059. case StringType:
  4060. return consistentStringProperty(lhs, rhs);
  4061. case NumberMinType:
  4062. case NumberMaxType: {
  4063. auto value = consistentNumberProperty(lhs.c_str(), rhs.c_str(), t);
  4064. return std::make_pair(
  4065. value.first, value.first ? std::string(value.second) : null_ptr);
  4066. }
  4067. }
  4068. assert(false && "Unreachable!");
  4069. return std::pair<bool, std::string>(false, null_ptr);
  4070. }
  4071. template <typename PropertyType>
  4072. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  4073. const std::string& p,
  4074. const std::string& config,
  4075. const char* defaultValue,
  4076. CompatibleType t,
  4077. PropertyType* /*unused*/)
  4078. {
  4079. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  4080. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  4081. const bool explicitlySet =
  4082. std::find(headPropKeys.begin(), headPropKeys.end(), p) !=
  4083. headPropKeys.end();
  4084. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  4085. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  4086. std::vector<cmGeneratorTarget const*> const& deps =
  4087. tgt->GetLinkImplementationClosure(config);
  4088. if (deps.empty()) {
  4089. return propContent;
  4090. }
  4091. bool propInitialized = explicitlySet;
  4092. std::string report = " * Target \"";
  4093. report += tgt->GetName();
  4094. if (explicitlySet) {
  4095. report += "\" has property content \"";
  4096. report += valueAsString<PropertyType>(propContent);
  4097. report += "\"\n";
  4098. } else if (impliedByUse) {
  4099. report += "\" property is implied by use.\n";
  4100. } else {
  4101. report += "\" property not set.\n";
  4102. }
  4103. std::string interfaceProperty = "INTERFACE_" + p;
  4104. std::unique_ptr<cmGeneratorExpressionInterpreter> genexInterpreter(
  4105. p == "POSITION_INDEPENDENT_CODE" ? new cmGeneratorExpressionInterpreter(
  4106. tgt->GetLocalGenerator(), config, tgt)
  4107. : nullptr);
  4108. for (cmGeneratorTarget const* theTarget : deps) {
  4109. // An error should be reported if one dependency
  4110. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  4111. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  4112. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  4113. // with a dependency.
  4114. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  4115. const bool ifaceIsSet = std::find(propKeys.begin(), propKeys.end(),
  4116. interfaceProperty) != propKeys.end();
  4117. PropertyType ifacePropContent = getTypedProperty<PropertyType>(
  4118. theTarget, interfaceProperty, genexInterpreter.get());
  4119. std::string reportEntry;
  4120. if (ifaceIsSet) {
  4121. reportEntry += " * Target \"";
  4122. reportEntry += theTarget->GetName();
  4123. reportEntry += "\" property value \"";
  4124. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  4125. reportEntry += "\" ";
  4126. }
  4127. if (explicitlySet) {
  4128. if (ifaceIsSet) {
  4129. std::pair<bool, PropertyType> consistent =
  4130. consistentProperty(propContent, ifacePropContent, t);
  4131. report += reportEntry;
  4132. report += compatibilityAgree(t, propContent != consistent.second);
  4133. if (!consistent.first) {
  4134. std::ostringstream e;
  4135. e << "Property " << p << " on target \"" << tgt->GetName()
  4136. << "\" does\nnot match the "
  4137. "INTERFACE_"
  4138. << p
  4139. << " property requirement\nof "
  4140. "dependency \""
  4141. << theTarget->GetName() << "\".\n";
  4142. cmSystemTools::Error(e.str());
  4143. break;
  4144. }
  4145. propContent = consistent.second;
  4146. continue;
  4147. }
  4148. // Explicitly set on target and not set in iface. Can't disagree.
  4149. continue;
  4150. }
  4151. if (impliedByUse) {
  4152. propContent = impliedValue<PropertyType>(propContent);
  4153. if (ifaceIsSet) {
  4154. std::pair<bool, PropertyType> consistent =
  4155. consistentProperty(propContent, ifacePropContent, t);
  4156. report += reportEntry;
  4157. report += compatibilityAgree(t, propContent != consistent.second);
  4158. if (!consistent.first) {
  4159. std::ostringstream e;
  4160. e << "Property " << p << " on target \"" << tgt->GetName()
  4161. << "\" is\nimplied to be " << defaultValue
  4162. << " because it was used to determine the link libraries\n"
  4163. "already. The INTERFACE_"
  4164. << p << " property on\ndependency \"" << theTarget->GetName()
  4165. << "\" is in conflict.\n";
  4166. cmSystemTools::Error(e.str());
  4167. break;
  4168. }
  4169. propContent = consistent.second;
  4170. continue;
  4171. }
  4172. // Implicitly set on target and not set in iface. Can't disagree.
  4173. continue;
  4174. }
  4175. if (ifaceIsSet) {
  4176. if (propInitialized) {
  4177. std::pair<bool, PropertyType> consistent =
  4178. consistentProperty(propContent, ifacePropContent, t);
  4179. report += reportEntry;
  4180. report += compatibilityAgree(t, propContent != consistent.second);
  4181. if (!consistent.first) {
  4182. std::ostringstream e;
  4183. e << "The INTERFACE_" << p << " property of \""
  4184. << theTarget->GetName() << "\" does\nnot agree with the value of "
  4185. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  4186. cmSystemTools::Error(e.str());
  4187. break;
  4188. }
  4189. propContent = consistent.second;
  4190. continue;
  4191. }
  4192. report += reportEntry + "(Interface set)\n";
  4193. propContent = ifacePropContent;
  4194. propInitialized = true;
  4195. } else {
  4196. // Not set. Nothing to agree on.
  4197. continue;
  4198. }
  4199. }
  4200. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  4201. report, compatibilityType(t));
  4202. return propContent;
  4203. }
  4204. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  4205. const std::string& p, const std::string& config) const
  4206. {
  4207. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  4208. BoolType, nullptr);
  4209. }
  4210. std::string cmGeneratorTarget::GetLinkInterfaceDependentStringAsBoolProperty(
  4211. const std::string& p, const std::string& config) const
  4212. {
  4213. return checkInterfacePropertyCompatibility<std::string>(
  4214. this, p, config, "FALSE", BoolType, nullptr);
  4215. }
  4216. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  4217. const std::string& p, const std::string& config) const
  4218. {
  4219. return checkInterfacePropertyCompatibility<const char*>(
  4220. this, p, config, "empty", StringType, nullptr);
  4221. }
  4222. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  4223. const std::string& p, const std::string& config) const
  4224. {
  4225. return checkInterfacePropertyCompatibility<const char*>(
  4226. this, p, config, "empty", NumberMinType, nullptr);
  4227. }
  4228. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  4229. const std::string& p, const std::string& config) const
  4230. {
  4231. return checkInterfacePropertyCompatibility<const char*>(
  4232. this, p, config, "empty", NumberMaxType, nullptr);
  4233. }
  4234. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  4235. const std::string& config) const
  4236. {
  4237. // Lookup any existing information for this configuration.
  4238. std::string key(cmSystemTools::UpperCase(config));
  4239. cmTargetLinkInformationMap::iterator i = this->LinkInformation.find(key);
  4240. if (i == this->LinkInformation.end()) {
  4241. // Compute information for this configuration.
  4242. cmComputeLinkInformation* info =
  4243. new cmComputeLinkInformation(this, config);
  4244. if (!info || !info->Compute()) {
  4245. delete info;
  4246. info = nullptr;
  4247. }
  4248. // Store the information for this configuration.
  4249. cmTargetLinkInformationMap::value_type entry(key, info);
  4250. i = this->LinkInformation.insert(entry).first;
  4251. if (info) {
  4252. this->CheckPropertyCompatibility(info, config);
  4253. }
  4254. }
  4255. return i->second;
  4256. }
  4257. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  4258. {
  4259. int patch;
  4260. this->GetTargetVersion(false, major, minor, patch);
  4261. }
  4262. void cmGeneratorTarget::GetTargetVersion(bool soversion, int& major,
  4263. int& minor, int& patch) const
  4264. {
  4265. // Set the default values.
  4266. major = 0;
  4267. minor = 0;
  4268. patch = 0;
  4269. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  4270. // Look for a VERSION or SOVERSION property.
  4271. const char* prop = soversion ? "SOVERSION" : "VERSION";
  4272. if (const char* version = this->GetProperty(prop)) {
  4273. // Try to parse the version number and store the results that were
  4274. // successfully parsed.
  4275. int parsed_major;
  4276. int parsed_minor;
  4277. int parsed_patch;
  4278. switch (sscanf(version, "%d.%d.%d", &parsed_major, &parsed_minor,
  4279. &parsed_patch)) {
  4280. case 3:
  4281. patch = parsed_patch;
  4282. CM_FALLTHROUGH;
  4283. case 2:
  4284. minor = parsed_minor;
  4285. CM_FALLTHROUGH;
  4286. case 1:
  4287. major = parsed_major;
  4288. CM_FALLTHROUGH;
  4289. default:
  4290. break;
  4291. }
  4292. }
  4293. }
  4294. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  4295. std::string const& working_dir) const
  4296. {
  4297. if (!this->FortranModuleDirectoryCreated) {
  4298. this->FortranModuleDirectory =
  4299. this->CreateFortranModuleDirectory(working_dir);
  4300. this->FortranModuleDirectoryCreated = true;
  4301. }
  4302. return this->FortranModuleDirectory;
  4303. }
  4304. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  4305. std::string const& working_dir) const
  4306. {
  4307. std::string mod_dir;
  4308. std::string target_mod_dir;
  4309. if (const char* prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  4310. target_mod_dir = prop;
  4311. } else {
  4312. std::string const& default_mod_dir =
  4313. this->LocalGenerator->GetCurrentBinaryDirectory();
  4314. if (default_mod_dir != working_dir) {
  4315. target_mod_dir = default_mod_dir;
  4316. }
  4317. }
  4318. const char* moddir_flag =
  4319. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  4320. if (!target_mod_dir.empty() && moddir_flag) {
  4321. // Compute the full path to the module directory.
  4322. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  4323. // Already a full path.
  4324. mod_dir = target_mod_dir;
  4325. } else {
  4326. // Interpret relative to the current output directory.
  4327. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  4328. mod_dir += "/";
  4329. mod_dir += target_mod_dir;
  4330. }
  4331. // Make sure the module output directory exists.
  4332. cmSystemTools::MakeDirectory(mod_dir);
  4333. }
  4334. return mod_dir;
  4335. }
  4336. std::string cmGeneratorTarget::GetFrameworkVersion() const
  4337. {
  4338. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  4339. if (const char* fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  4340. return fversion;
  4341. }
  4342. if (const char* tversion = this->GetProperty("VERSION")) {
  4343. return tversion;
  4344. }
  4345. return "A";
  4346. }
  4347. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  4348. std::string const& prefix,
  4349. std::string const& base,
  4350. std::string const& suffix,
  4351. std::string const& name,
  4352. const char* version) const
  4353. {
  4354. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  4355. if (version) {
  4356. vName += ".";
  4357. vName += version;
  4358. }
  4359. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  4360. }
  4361. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  4362. {
  4363. cmPropertyMap const& propsObject = this->Target->GetProperties();
  4364. std::vector<std::string> props;
  4365. props.reserve(propsObject.size());
  4366. for (auto const& it : propsObject) {
  4367. props.push_back(it.first);
  4368. }
  4369. return props;
  4370. }
  4371. void cmGeneratorTarget::ReportPropertyOrigin(
  4372. const std::string& p, const std::string& result, const std::string& report,
  4373. const std::string& compatibilityType) const
  4374. {
  4375. std::vector<std::string> debugProperties;
  4376. const char* debugProp = this->Target->GetMakefile()->GetDefinition(
  4377. "CMAKE_DEBUG_TARGET_PROPERTIES");
  4378. if (debugProp) {
  4379. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  4380. }
  4381. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  4382. std::find(debugProperties.begin(), debugProperties.end(), p) !=
  4383. debugProperties.end();
  4384. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4385. this->DebugCompatiblePropertiesDone[p] = true;
  4386. }
  4387. if (!debugOrigin) {
  4388. return;
  4389. }
  4390. std::string areport = compatibilityType;
  4391. areport += std::string(" of property \"") + p + "\" for target \"";
  4392. areport += std::string(this->GetName());
  4393. areport += "\" (result: \"";
  4394. areport += result;
  4395. areport += "\"):\n" + report;
  4396. this->LocalGenerator->GetCMakeInstance()->IssueMessage(MessageType::LOG,
  4397. areport);
  4398. }
  4399. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  4400. cmListFileBacktrace const& bt,
  4401. std::vector<cmLinkItem>& items) const
  4402. {
  4403. for (std::string const& n : names) {
  4404. std::string name = this->CheckCMP0004(n);
  4405. if (name == this->GetName() || name.empty()) {
  4406. continue;
  4407. }
  4408. items.push_back(this->ResolveLinkItem(name, bt));
  4409. }
  4410. }
  4411. void cmGeneratorTarget::ExpandLinkItems(
  4412. std::string const& prop, std::string const& value, std::string const& config,
  4413. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  4414. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition) const
  4415. {
  4416. cmGeneratorExpression ge;
  4417. cmGeneratorExpressionDAGChecker dagChecker(this, prop, nullptr, nullptr);
  4418. // The $<LINK_ONLY> expression may be in a link interface to specify private
  4419. // link dependencies that are otherwise excluded from usage requirements.
  4420. if (usage_requirements_only) {
  4421. dagChecker.SetTransitivePropertiesOnly();
  4422. }
  4423. std::vector<std::string> libs;
  4424. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  4425. cmSystemTools::ExpandListArgument(cge->Evaluate(this->LocalGenerator, config,
  4426. false, headTarget, this,
  4427. &dagChecker),
  4428. libs);
  4429. this->LookupLinkItems(libs, cge->GetBacktrace(), items);
  4430. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  4431. }
  4432. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  4433. const std::string& config, cmGeneratorTarget const* head) const
  4434. {
  4435. // Imported targets have their own link interface.
  4436. if (this->IsImported()) {
  4437. return this->GetImportLinkInterface(config, head, false);
  4438. }
  4439. // Link interfaces are not supported for executables that do not
  4440. // export symbols.
  4441. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  4442. !this->IsExecutableWithExports()) {
  4443. return nullptr;
  4444. }
  4445. // Lookup any existing link interface for this configuration.
  4446. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  4447. // If the link interface does not depend on the head target
  4448. // then return the one we computed first.
  4449. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4450. return &hm.begin()->second;
  4451. }
  4452. cmOptionalLinkInterface& iface = hm[head];
  4453. if (!iface.LibrariesDone) {
  4454. iface.LibrariesDone = true;
  4455. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  4456. }
  4457. if (!iface.AllDone) {
  4458. iface.AllDone = true;
  4459. if (iface.Exists) {
  4460. this->ComputeLinkInterface(config, iface, head);
  4461. }
  4462. }
  4463. return iface.Exists ? &iface : nullptr;
  4464. }
  4465. void cmGeneratorTarget::ComputeLinkInterface(
  4466. const std::string& config, cmOptionalLinkInterface& iface,
  4467. cmGeneratorTarget const* headTarget) const
  4468. {
  4469. if (iface.ExplicitLibraries) {
  4470. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4471. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4472. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4473. // Shared libraries may have runtime implementation dependencies
  4474. // on other shared libraries that are not in the interface.
  4475. std::set<cmLinkItem> emitted;
  4476. for (cmLinkItem const& lib : iface.Libraries) {
  4477. emitted.insert(lib);
  4478. }
  4479. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4480. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  4481. for (cmLinkImplItem const& lib : impl->Libraries) {
  4482. if (emitted.insert(lib).second) {
  4483. if (lib.Target) {
  4484. // This is a runtime dependency on another shared library.
  4485. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4486. iface.SharedDeps.push_back(lib);
  4487. }
  4488. } else {
  4489. // TODO: Recognize shared library file names. Perhaps this
  4490. // should be moved to cmComputeLinkInformation, but that creates
  4491. // a chicken-and-egg problem since this list is needed for its
  4492. // construction.
  4493. }
  4494. }
  4495. }
  4496. }
  4497. }
  4498. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4499. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  4500. // The link implementation is the default link interface.
  4501. cmLinkImplementationLibraries const* impl =
  4502. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4503. iface.ImplementationIsInterface = true;
  4504. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  4505. }
  4506. if (this->LinkLanguagePropagatesToDependents()) {
  4507. // Targets using this archive need its language runtime libraries.
  4508. if (cmLinkImplementation const* impl =
  4509. this->GetLinkImplementation(config)) {
  4510. iface.Languages = impl->Languages;
  4511. }
  4512. }
  4513. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4514. // Construct the property name suffix for this configuration.
  4515. std::string suffix = "_";
  4516. if (!config.empty()) {
  4517. suffix += cmSystemTools::UpperCase(config);
  4518. } else {
  4519. suffix += "NOCONFIG";
  4520. }
  4521. // How many repetitions are needed if this library has cyclic
  4522. // dependencies?
  4523. std::string propName = "LINK_INTERFACE_MULTIPLICITY";
  4524. propName += suffix;
  4525. if (const char* config_reps = this->GetProperty(propName)) {
  4526. sscanf(config_reps, "%u", &iface.Multiplicity);
  4527. } else if (const char* reps =
  4528. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  4529. sscanf(reps, "%u", &iface.Multiplicity);
  4530. }
  4531. }
  4532. }
  4533. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  4534. const std::string& config, cmGeneratorTarget const* head,
  4535. bool usage_requirements_only) const
  4536. {
  4537. // Imported targets have their own link interface.
  4538. if (this->IsImported()) {
  4539. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  4540. }
  4541. // Link interfaces are not supported for executables that do not
  4542. // export symbols.
  4543. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  4544. !this->IsExecutableWithExports()) {
  4545. return nullptr;
  4546. }
  4547. // Lookup any existing link interface for this configuration.
  4548. std::string CONFIG = cmSystemTools::UpperCase(config);
  4549. cmHeadToLinkInterfaceMap& hm =
  4550. (usage_requirements_only
  4551. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4552. : this->GetHeadToLinkInterfaceMap(config));
  4553. // If the link interface does not depend on the head target
  4554. // then return the one we computed first.
  4555. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4556. return &hm.begin()->second;
  4557. }
  4558. cmOptionalLinkInterface& iface = hm[head];
  4559. if (!iface.LibrariesDone) {
  4560. iface.LibrariesDone = true;
  4561. this->ComputeLinkInterfaceLibraries(config, iface, head,
  4562. usage_requirements_only);
  4563. }
  4564. return iface.Exists ? &iface : nullptr;
  4565. }
  4566. std::string cmGeneratorTarget::GetDirectory(
  4567. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4568. {
  4569. if (this->IsImported()) {
  4570. // Return the directory from which the target is imported.
  4571. return cmSystemTools::GetFilenamePath(
  4572. this->Target->ImportedGetFullPath(config, artifact));
  4573. }
  4574. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  4575. // Return the directory in which the target will be built.
  4576. switch (artifact) {
  4577. case cmStateEnums::RuntimeBinaryArtifact:
  4578. return info->OutDir;
  4579. case cmStateEnums::ImportLibraryArtifact:
  4580. return info->ImpDir;
  4581. }
  4582. }
  4583. return "";
  4584. }
  4585. bool cmGeneratorTarget::UsesDefaultOutputDir(
  4586. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4587. {
  4588. std::string dir;
  4589. return this->ComputeOutputDir(config, artifact, dir);
  4590. }
  4591. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  4592. const std::string& config) const
  4593. {
  4594. // There is no output information for imported targets.
  4595. if (this->IsImported()) {
  4596. return nullptr;
  4597. }
  4598. // Only libraries and executables have well-defined output files.
  4599. if (!this->HaveWellDefinedOutputFiles()) {
  4600. std::string msg = "cmGeneratorTarget::GetOutputInfo called for ";
  4601. msg += this->GetName();
  4602. msg += " which has type ";
  4603. msg += cmState::GetTargetTypeName(this->GetType());
  4604. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4605. return nullptr;
  4606. }
  4607. // Lookup/compute/cache the output information for this configuration.
  4608. std::string config_upper;
  4609. if (!config.empty()) {
  4610. config_upper = cmSystemTools::UpperCase(config);
  4611. }
  4612. OutputInfoMapType::iterator i = this->OutputInfoMap.find(config_upper);
  4613. if (i == this->OutputInfoMap.end()) {
  4614. // Add empty info in map to detect potential recursion.
  4615. OutputInfo info;
  4616. OutputInfoMapType::value_type entry(config_upper, info);
  4617. i = this->OutputInfoMap.insert(entry).first;
  4618. // Compute output directories.
  4619. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  4620. info.OutDir);
  4621. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  4622. info.ImpDir);
  4623. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  4624. info.PdbDir = info.OutDir;
  4625. }
  4626. // Now update the previously-prepared map entry.
  4627. i->second = info;
  4628. } else if (i->second.empty()) {
  4629. // An empty map entry indicates we have been called recursively
  4630. // from the above block.
  4631. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4632. MessageType::FATAL_ERROR,
  4633. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  4634. this->GetBacktrace());
  4635. return nullptr;
  4636. }
  4637. return &i->second;
  4638. }
  4639. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  4640. cmStateEnums::ArtifactType artifact,
  4641. std::string& out) const
  4642. {
  4643. bool usesDefaultOutputDir = false;
  4644. std::string conf = config;
  4645. // Look for a target property defining the target output directory
  4646. // based on the target type.
  4647. std::string targetTypeName = this->GetOutputTargetType(artifact);
  4648. const char* propertyName = nullptr;
  4649. std::string propertyNameStr = targetTypeName;
  4650. if (!propertyNameStr.empty()) {
  4651. propertyNameStr += "_OUTPUT_DIRECTORY";
  4652. propertyName = propertyNameStr.c_str();
  4653. }
  4654. // Check for a per-configuration output directory target property.
  4655. std::string configUpper = cmSystemTools::UpperCase(conf);
  4656. const char* configProp = nullptr;
  4657. std::string configPropStr = targetTypeName;
  4658. if (!configPropStr.empty()) {
  4659. configPropStr += "_OUTPUT_DIRECTORY_";
  4660. configPropStr += configUpper;
  4661. configProp = configPropStr.c_str();
  4662. }
  4663. // Select an output directory.
  4664. if (const char* config_outdir = this->GetProperty(configProp)) {
  4665. // Use the user-specified per-configuration output directory.
  4666. cmGeneratorExpression ge;
  4667. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4668. ge.Parse(config_outdir);
  4669. out = cge->Evaluate(this->LocalGenerator, config);
  4670. // Skip per-configuration subdirectory.
  4671. conf.clear();
  4672. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4673. // Use the user-specified output directory.
  4674. cmGeneratorExpression ge;
  4675. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4676. out = cge->Evaluate(this->LocalGenerator, config);
  4677. // Skip per-configuration subdirectory if the value contained a
  4678. // generator expression.
  4679. if (out != outdir) {
  4680. conf.clear();
  4681. }
  4682. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4683. // Lookup the output path for executables.
  4684. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  4685. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4686. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4687. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4688. // Lookup the output path for libraries.
  4689. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  4690. }
  4691. if (out.empty()) {
  4692. // Default to the current output directory.
  4693. usesDefaultOutputDir = true;
  4694. out = ".";
  4695. }
  4696. // Convert the output path to a full path in case it is
  4697. // specified as a relative path. Treat a relative path as
  4698. // relative to the current output directory for this makefile.
  4699. out = (cmSystemTools::CollapseFullPath(
  4700. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4701. // The generator may add the configuration's subdirectory.
  4702. if (!conf.empty()) {
  4703. bool useEPN =
  4704. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  4705. std::string suffix =
  4706. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  4707. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4708. "/", conf, suffix, out);
  4709. }
  4710. return usesDefaultOutputDir;
  4711. }
  4712. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  4713. const std::string& config,
  4714. std::string& out) const
  4715. {
  4716. // Look for a target property defining the target output directory
  4717. // based on the target type.
  4718. const char* propertyName = nullptr;
  4719. std::string propertyNameStr = kind;
  4720. if (!propertyNameStr.empty()) {
  4721. propertyNameStr += "_OUTPUT_DIRECTORY";
  4722. propertyName = propertyNameStr.c_str();
  4723. }
  4724. std::string conf = config;
  4725. // Check for a per-configuration output directory target property.
  4726. std::string configUpper = cmSystemTools::UpperCase(conf);
  4727. const char* configProp = nullptr;
  4728. std::string configPropStr = kind;
  4729. if (!configPropStr.empty()) {
  4730. configPropStr += "_OUTPUT_DIRECTORY_";
  4731. configPropStr += configUpper;
  4732. configProp = configPropStr.c_str();
  4733. }
  4734. // Select an output directory.
  4735. if (const char* config_outdir = this->GetProperty(configProp)) {
  4736. // Use the user-specified per-configuration output directory.
  4737. cmGeneratorExpression ge;
  4738. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4739. ge.Parse(config_outdir);
  4740. out = cge->Evaluate(this->LocalGenerator, config);
  4741. // Skip per-configuration subdirectory.
  4742. conf.clear();
  4743. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4744. // Use the user-specified output directory.
  4745. cmGeneratorExpression ge;
  4746. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4747. out = cge->Evaluate(this->LocalGenerator, config);
  4748. // Skip per-configuration subdirectory if the value contained a
  4749. // generator expression.
  4750. if (out != outdir) {
  4751. conf.clear();
  4752. }
  4753. }
  4754. if (out.empty()) {
  4755. return false;
  4756. }
  4757. // Convert the output path to a full path in case it is
  4758. // specified as a relative path. Treat a relative path as
  4759. // relative to the current output directory for this makefile.
  4760. out = (cmSystemTools::CollapseFullPath(
  4761. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4762. // The generator may add the configuration's subdirectory.
  4763. if (!conf.empty()) {
  4764. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4765. "/", conf, "", out);
  4766. }
  4767. return true;
  4768. }
  4769. bool cmGeneratorTarget::HaveInstallTreeRPATH() const
  4770. {
  4771. const char* install_rpath = this->GetProperty("INSTALL_RPATH");
  4772. return (install_rpath && *install_rpath) &&
  4773. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  4774. }
  4775. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  4776. const std::string& config, cmOptionalLinkInterface& iface,
  4777. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  4778. {
  4779. // Construct the property name suffix for this configuration.
  4780. std::string suffix = "_";
  4781. if (!config.empty()) {
  4782. suffix += cmSystemTools::UpperCase(config);
  4783. } else {
  4784. suffix += "NOCONFIG";
  4785. }
  4786. // An explicit list of interface libraries may be set for shared
  4787. // libraries and executables that export symbols.
  4788. const char* explicitLibraries = nullptr;
  4789. std::string linkIfaceProp;
  4790. if (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  4791. this->GetPolicyStatusCMP0022() != cmPolicies::WARN) {
  4792. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  4793. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  4794. explicitLibraries = this->GetProperty(linkIfaceProp);
  4795. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4796. this->IsExecutableWithExports()) {
  4797. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  4798. // shared lib or executable.
  4799. // Lookup the per-configuration property.
  4800. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4801. linkIfaceProp += suffix;
  4802. explicitLibraries = this->GetProperty(linkIfaceProp);
  4803. // If not set, try the generic property.
  4804. if (!explicitLibraries) {
  4805. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4806. explicitLibraries = this->GetProperty(linkIfaceProp);
  4807. }
  4808. }
  4809. if (explicitLibraries &&
  4810. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4811. !this->PolicyWarnedCMP0022) {
  4812. // Compare the explicitly set old link interface properties to the
  4813. // preferred new link interface property one and warn if different.
  4814. const char* newExplicitLibraries =
  4815. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  4816. if (newExplicitLibraries &&
  4817. strcmp(newExplicitLibraries, explicitLibraries) != 0) {
  4818. std::ostringstream w;
  4819. /* clang-format off */
  4820. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4821. "Target \"" << this->GetName() << "\" has an "
  4822. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  4823. linkIfaceProp << " properties."
  4824. "\n"
  4825. "INTERFACE_LINK_LIBRARIES:\n"
  4826. " " << newExplicitLibraries << "\n" <<
  4827. linkIfaceProp << ":\n"
  4828. " " << explicitLibraries << "\n";
  4829. /* clang-format on */
  4830. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  4831. this->PolicyWarnedCMP0022 = true;
  4832. }
  4833. }
  4834. // There is no implicit link interface for executables or modules
  4835. // so if none was explicitly set then there is no link interface.
  4836. if (!explicitLibraries &&
  4837. (this->GetType() == cmStateEnums::EXECUTABLE ||
  4838. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  4839. return;
  4840. }
  4841. iface.Exists = true;
  4842. iface.ExplicitLibraries = explicitLibraries;
  4843. if (explicitLibraries) {
  4844. // The interface libraries have been explicitly set.
  4845. this->ExpandLinkItems(linkIfaceProp, explicitLibraries, config, headTarget,
  4846. usage_requirements_only, iface.Libraries,
  4847. iface.HadHeadSensitiveCondition);
  4848. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4849. this->GetPolicyStatusCMP0022() == cmPolicies::OLD)
  4850. // If CMP0022 is NEW then the plain tll signature sets the
  4851. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  4852. // cleared the property explicitly and we should not fall back
  4853. // to the link implementation.
  4854. {
  4855. // The link implementation is the default link interface.
  4856. cmLinkImplementationLibraries const* impl =
  4857. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4858. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  4859. impl->Libraries.end());
  4860. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4861. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  4862. // Compare the link implementation fallback link interface to the
  4863. // preferred new link interface property and warn if different.
  4864. std::vector<cmLinkItem> ifaceLibs;
  4865. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  4866. if (const char* newExplicitLibraries = this->GetProperty(newProp)) {
  4867. bool hadHeadSensitiveConditionDummy = false;
  4868. this->ExpandLinkItems(newProp, newExplicitLibraries, config,
  4869. headTarget, usage_requirements_only, ifaceLibs,
  4870. hadHeadSensitiveConditionDummy);
  4871. }
  4872. if (ifaceLibs != iface.Libraries) {
  4873. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  4874. std::string newLibraries = cmJoin(ifaceLibs, ";");
  4875. if (oldLibraries.empty()) {
  4876. oldLibraries = "(empty)";
  4877. }
  4878. if (newLibraries.empty()) {
  4879. newLibraries = "(empty)";
  4880. }
  4881. std::ostringstream w;
  4882. /* clang-format off */
  4883. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4884. "Target \"" << this->GetName() << "\" has an "
  4885. "INTERFACE_LINK_LIBRARIES property. "
  4886. "This should be preferred as the source of the link interface "
  4887. "for this library but because CMP0022 is not set CMake is "
  4888. "ignoring the property and using the link implementation "
  4889. "as the link interface instead."
  4890. "\n"
  4891. "INTERFACE_LINK_LIBRARIES:\n"
  4892. " " << newLibraries << "\n"
  4893. "Link implementation:\n"
  4894. " " << oldLibraries << "\n";
  4895. /* clang-format on */
  4896. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  4897. w.str());
  4898. this->PolicyWarnedCMP0022 = true;
  4899. }
  4900. }
  4901. }
  4902. }
  4903. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  4904. const std::string& config, cmGeneratorTarget const* headTarget,
  4905. bool usage_requirements_only) const
  4906. {
  4907. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  4908. if (!info) {
  4909. return nullptr;
  4910. }
  4911. std::string CONFIG = cmSystemTools::UpperCase(config);
  4912. cmHeadToLinkInterfaceMap& hm =
  4913. (usage_requirements_only
  4914. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4915. : this->GetHeadToLinkInterfaceMap(config));
  4916. // If the link interface does not depend on the head target
  4917. // then return the one we computed first.
  4918. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4919. return &hm.begin()->second;
  4920. }
  4921. cmOptionalLinkInterface& iface = hm[headTarget];
  4922. if (!iface.AllDone) {
  4923. iface.AllDone = true;
  4924. iface.Multiplicity = info->Multiplicity;
  4925. cmSystemTools::ExpandListArgument(info->Languages, iface.Languages);
  4926. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  4927. headTarget, usage_requirements_only, iface.Libraries,
  4928. iface.HadHeadSensitiveCondition);
  4929. std::vector<std::string> deps;
  4930. cmSystemTools::ExpandListArgument(info->SharedDeps, deps);
  4931. this->LookupLinkItems(deps, cmListFileBacktrace(), iface.SharedDeps);
  4932. }
  4933. return &iface;
  4934. }
  4935. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  4936. const std::string& config) const
  4937. {
  4938. // There is no imported information for non-imported targets.
  4939. if (!this->IsImported()) {
  4940. return nullptr;
  4941. }
  4942. // Lookup/compute/cache the import information for this
  4943. // configuration.
  4944. std::string config_upper;
  4945. if (!config.empty()) {
  4946. config_upper = cmSystemTools::UpperCase(config);
  4947. } else {
  4948. config_upper = "NOCONFIG";
  4949. }
  4950. ImportInfoMapType::const_iterator i = this->ImportInfoMap.find(config_upper);
  4951. if (i == this->ImportInfoMap.end()) {
  4952. ImportInfo info;
  4953. this->ComputeImportInfo(config_upper, info);
  4954. ImportInfoMapType::value_type entry(config_upper, info);
  4955. i = this->ImportInfoMap.insert(entry).first;
  4956. }
  4957. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4958. return &i->second;
  4959. }
  4960. // If the location is empty then the target is not available for
  4961. // this configuration.
  4962. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  4963. return nullptr;
  4964. }
  4965. // Return the import information.
  4966. return &i->second;
  4967. }
  4968. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  4969. ImportInfo& info) const
  4970. {
  4971. // This method finds information about an imported target from its
  4972. // properties. The "IMPORTED_" namespace is reserved for properties
  4973. // defined by the project exporting the target.
  4974. // Initialize members.
  4975. info.NoSOName = false;
  4976. const char* loc = nullptr;
  4977. const char* imp = nullptr;
  4978. std::string suffix;
  4979. if (!this->Target->GetMappedConfig(desired_config, &loc, &imp, suffix)) {
  4980. return;
  4981. }
  4982. // Get the link interface.
  4983. {
  4984. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  4985. const char* propertyLibs = this->GetProperty(linkProp);
  4986. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4987. if (!propertyLibs) {
  4988. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4989. linkProp += suffix;
  4990. propertyLibs = this->GetProperty(linkProp);
  4991. }
  4992. if (!propertyLibs) {
  4993. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4994. propertyLibs = this->GetProperty(linkProp);
  4995. }
  4996. }
  4997. if (propertyLibs) {
  4998. info.LibrariesProp = linkProp;
  4999. info.Libraries = propertyLibs;
  5000. }
  5001. }
  5002. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5003. if (loc) {
  5004. info.LibName = loc;
  5005. }
  5006. return;
  5007. }
  5008. // A provided configuration has been chosen. Load the
  5009. // configuration's properties.
  5010. // Get the location.
  5011. if (loc) {
  5012. info.Location = loc;
  5013. } else {
  5014. std::string impProp = "IMPORTED_LOCATION";
  5015. impProp += suffix;
  5016. if (const char* config_location = this->GetProperty(impProp)) {
  5017. info.Location = config_location;
  5018. } else if (const char* location = this->GetProperty("IMPORTED_LOCATION")) {
  5019. info.Location = location;
  5020. }
  5021. }
  5022. // Get the soname.
  5023. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5024. std::string soProp = "IMPORTED_SONAME";
  5025. soProp += suffix;
  5026. if (const char* config_soname = this->GetProperty(soProp)) {
  5027. info.SOName = config_soname;
  5028. } else if (const char* soname = this->GetProperty("IMPORTED_SONAME")) {
  5029. info.SOName = soname;
  5030. }
  5031. }
  5032. // Get the "no-soname" mark.
  5033. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5034. std::string soProp = "IMPORTED_NO_SONAME";
  5035. soProp += suffix;
  5036. if (const char* config_no_soname = this->GetProperty(soProp)) {
  5037. info.NoSOName = cmSystemTools::IsOn(config_no_soname);
  5038. } else if (const char* no_soname =
  5039. this->GetProperty("IMPORTED_NO_SONAME")) {
  5040. info.NoSOName = cmSystemTools::IsOn(no_soname);
  5041. }
  5042. }
  5043. // Get the import library.
  5044. if (imp) {
  5045. info.ImportLibrary = imp;
  5046. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5047. this->IsExecutableWithExports()) {
  5048. std::string impProp = "IMPORTED_IMPLIB";
  5049. impProp += suffix;
  5050. if (const char* config_implib = this->GetProperty(impProp)) {
  5051. info.ImportLibrary = config_implib;
  5052. } else if (const char* implib = this->GetProperty("IMPORTED_IMPLIB")) {
  5053. info.ImportLibrary = implib;
  5054. }
  5055. }
  5056. // Get the link dependencies.
  5057. {
  5058. std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
  5059. linkProp += suffix;
  5060. if (const char* config_libs = this->GetProperty(linkProp)) {
  5061. info.SharedDeps = config_libs;
  5062. } else if (const char* libs =
  5063. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  5064. info.SharedDeps = libs;
  5065. }
  5066. }
  5067. // Get the link languages.
  5068. if (this->LinkLanguagePropagatesToDependents()) {
  5069. std::string linkProp = "IMPORTED_LINK_INTERFACE_LANGUAGES";
  5070. linkProp += suffix;
  5071. if (const char* config_libs = this->GetProperty(linkProp)) {
  5072. info.Languages = config_libs;
  5073. } else if (const char* libs =
  5074. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  5075. info.Languages = libs;
  5076. }
  5077. }
  5078. // Get information if target is managed assembly.
  5079. {
  5080. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  5081. if (auto pc = this->GetProperty(linkProp + suffix)) {
  5082. info.Managed = this->CheckManagedType(pc);
  5083. } else if (auto p = this->GetProperty(linkProp)) {
  5084. info.Managed = this->CheckManagedType(p);
  5085. }
  5086. }
  5087. // Get the cyclic repetition count.
  5088. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5089. std::string linkProp = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  5090. linkProp += suffix;
  5091. if (const char* config_reps = this->GetProperty(linkProp)) {
  5092. sscanf(config_reps, "%u", &info.Multiplicity);
  5093. } else if (const char* reps =
  5094. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  5095. sscanf(reps, "%u", &info.Multiplicity);
  5096. }
  5097. }
  5098. }
  5099. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  5100. const std::string& config) const
  5101. {
  5102. std::string CONFIG = cmSystemTools::UpperCase(config);
  5103. return this->LinkInterfaceMap[CONFIG];
  5104. }
  5105. cmHeadToLinkInterfaceMap&
  5106. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  5107. const std::string& config) const
  5108. {
  5109. std::string CONFIG = cmSystemTools::UpperCase(config);
  5110. return this->LinkInterfaceUsageRequirementsOnlyMap[CONFIG];
  5111. }
  5112. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  5113. const std::string& config) const
  5114. {
  5115. // There is no link implementation for imported targets.
  5116. if (this->IsImported()) {
  5117. return nullptr;
  5118. }
  5119. std::string CONFIG = cmSystemTools::UpperCase(config);
  5120. cmOptionalLinkImplementation& impl = this->LinkImplMap[CONFIG][this];
  5121. if (!impl.LibrariesDone) {
  5122. impl.LibrariesDone = true;
  5123. this->ComputeLinkImplementationLibraries(config, impl, this);
  5124. }
  5125. if (!impl.LanguagesDone) {
  5126. impl.LanguagesDone = true;
  5127. this->ComputeLinkImplementationLanguages(config, impl);
  5128. }
  5129. return &impl;
  5130. }
  5131. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  5132. std::vector<cmSourceFile*>& files) const
  5133. {
  5134. std::vector<std::string> configs;
  5135. this->Makefile->GetConfigurations(configs);
  5136. if (configs.empty()) {
  5137. configs.emplace_back();
  5138. }
  5139. std::vector<std::string>::const_iterator it = configs.begin();
  5140. const std::string& firstConfig = *it;
  5141. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  5142. for (; it != configs.end(); ++it) {
  5143. std::vector<cmSourceFile*> configFiles;
  5144. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  5145. if (configFiles != files) {
  5146. std::string firstConfigFiles;
  5147. const char* sep = "";
  5148. for (cmSourceFile* f : files) {
  5149. firstConfigFiles += sep;
  5150. firstConfigFiles += f->GetFullPath();
  5151. sep = "\n ";
  5152. }
  5153. std::string thisConfigFiles;
  5154. sep = "";
  5155. for (cmSourceFile* f : configFiles) {
  5156. thisConfigFiles += sep;
  5157. thisConfigFiles += f->GetFullPath();
  5158. sep = "\n ";
  5159. }
  5160. std::ostringstream e;
  5161. /* clang-format off */
  5162. e << "Target \"" << this->GetName()
  5163. << "\" has source files which vary by "
  5164. "configuration. This is not supported by the \""
  5165. << this->GlobalGenerator->GetName()
  5166. << "\" generator.\n"
  5167. "Config \"" << firstConfig << "\":\n"
  5168. " " << firstConfigFiles << "\n"
  5169. "Config \"" << *it << "\":\n"
  5170. " " << thisConfigFiles << "\n";
  5171. /* clang-format on */
  5172. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  5173. return false;
  5174. }
  5175. }
  5176. return true;
  5177. }
  5178. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  5179. std::vector<cmGeneratorTarget*>& objlibs) const
  5180. {
  5181. // At configure-time, this method can be called as part of getting the
  5182. // LOCATION property or to export() a file to be include()d. However
  5183. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  5184. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  5185. // behavior of CMP0024 and CMP0026 only.
  5186. cmStringRange rng = this->Target->GetSourceEntries();
  5187. for (std::string const& entry : rng) {
  5188. std::vector<std::string> files;
  5189. cmSystemTools::ExpandListArgument(entry, files);
  5190. for (std::string const& li : files) {
  5191. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") && li.back() == '>') {
  5192. std::string objLibName = li.substr(17, li.size() - 18);
  5193. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  5194. continue;
  5195. }
  5196. cmGeneratorTarget* objLib =
  5197. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  5198. if (objLib) {
  5199. objlibs.push_back(objLib);
  5200. }
  5201. }
  5202. }
  5203. }
  5204. }
  5205. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  5206. {
  5207. // Strip whitespace off the library names because we used to do this
  5208. // in case variables were expanded at generate time. We no longer
  5209. // do the expansion but users link to libraries like " ${VAR} ".
  5210. std::string lib = item;
  5211. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  5212. if (pos != std::string::npos) {
  5213. lib = lib.substr(pos);
  5214. }
  5215. pos = lib.find_last_not_of(" \t\r\n");
  5216. if (pos != std::string::npos) {
  5217. lib = lib.substr(0, pos + 1);
  5218. }
  5219. if (lib != item) {
  5220. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  5221. switch (this->GetPolicyStatusCMP0004()) {
  5222. case cmPolicies::WARN: {
  5223. std::ostringstream w;
  5224. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  5225. << "Target \"" << this->GetName() << "\" links to item \"" << item
  5226. << "\" which has leading or trailing whitespace.";
  5227. cm->IssueMessage(MessageType::AUTHOR_WARNING, w.str(),
  5228. this->GetBacktrace());
  5229. }
  5230. case cmPolicies::OLD:
  5231. break;
  5232. case cmPolicies::NEW: {
  5233. std::ostringstream e;
  5234. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  5235. << "\" which has leading or trailing whitespace. "
  5236. << "This is now an error according to policy CMP0004.";
  5237. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  5238. this->GetBacktrace());
  5239. } break;
  5240. case cmPolicies::REQUIRED_IF_USED:
  5241. case cmPolicies::REQUIRED_ALWAYS: {
  5242. std::ostringstream e;
  5243. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  5244. << "Target \"" << this->GetName() << "\" links to item \"" << item
  5245. << "\" which has leading or trailing whitespace.";
  5246. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  5247. this->GetBacktrace());
  5248. } break;
  5249. }
  5250. }
  5251. return lib;
  5252. }
  5253. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  5254. const std::string& config) const
  5255. {
  5256. std::vector<cmSourceFile*> sourceFiles;
  5257. this->GetSourceFiles(sourceFiles, config);
  5258. for (cmSourceFile* src : sourceFiles) {
  5259. const std::string& lang = src->GetLanguage();
  5260. if (!lang.empty()) {
  5261. languages.insert(lang);
  5262. }
  5263. }
  5264. std::vector<cmGeneratorTarget*> objectLibraries;
  5265. std::vector<cmSourceFile const*> externalObjects;
  5266. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  5267. std::vector<cmGeneratorTarget*> objectTargets;
  5268. this->GetObjectLibrariesCMP0026(objectTargets);
  5269. objectLibraries.reserve(objectTargets.size());
  5270. for (cmGeneratorTarget* gt : objectTargets) {
  5271. objectLibraries.push_back(gt);
  5272. }
  5273. } else {
  5274. this->GetExternalObjects(externalObjects, config);
  5275. for (cmSourceFile const* extObj : externalObjects) {
  5276. std::string objLib = extObj->GetObjectLibrary();
  5277. if (cmGeneratorTarget* tgt =
  5278. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  5279. auto const objLibIt =
  5280. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  5281. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  5282. if (objectLibraries.cend() == objLibIt) {
  5283. objectLibraries.push_back(tgt);
  5284. }
  5285. }
  5286. }
  5287. }
  5288. for (cmGeneratorTarget* objLib : objectLibraries) {
  5289. objLib->GetLanguages(languages, config);
  5290. }
  5291. }
  5292. bool cmGeneratorTarget::IsCSharpOnly() const
  5293. {
  5294. // Only certain target types may compile CSharp.
  5295. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  5296. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  5297. this->GetType() != cmStateEnums::EXECUTABLE) {
  5298. return false;
  5299. }
  5300. std::set<std::string> languages;
  5301. this->GetLanguages(languages, "");
  5302. // Consider an explicit linker language property, but *not* the
  5303. // computed linker language that may depend on linked targets.
  5304. const char* linkLang = this->GetProperty("LINKER_LANGUAGE");
  5305. if (linkLang && *linkLang) {
  5306. languages.insert(linkLang);
  5307. }
  5308. return languages.size() == 1 && languages.count("CSharp") > 0;
  5309. }
  5310. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  5311. const std::string& config, cmOptionalLinkImplementation& impl) const
  5312. {
  5313. // This target needs runtime libraries for its source languages.
  5314. std::set<std::string> languages;
  5315. // Get languages used in our source files.
  5316. this->GetLanguages(languages, config);
  5317. // Copy the set of languages to the link implementation.
  5318. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  5319. languages.end());
  5320. }
  5321. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  5322. {
  5323. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  5324. return false;
  5325. }
  5326. if (this->GetProperty("BUILD_RPATH")) {
  5327. return true;
  5328. }
  5329. if (cmLinkImplementationLibraries const* impl =
  5330. this->GetLinkImplementationLibraries(config)) {
  5331. return !impl->Libraries.empty();
  5332. }
  5333. return false;
  5334. }
  5335. cmLinkImplementationLibraries const*
  5336. cmGeneratorTarget::GetLinkImplementationLibraries(
  5337. const std::string& config) const
  5338. {
  5339. return this->GetLinkImplementationLibrariesInternal(config, this);
  5340. }
  5341. cmLinkImplementationLibraries const*
  5342. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  5343. const std::string& config, cmGeneratorTarget const* head) const
  5344. {
  5345. // There is no link implementation for imported targets.
  5346. if (this->IsImported()) {
  5347. return nullptr;
  5348. }
  5349. // Populate the link implementation libraries for this configuration.
  5350. std::string CONFIG = cmSystemTools::UpperCase(config);
  5351. HeadToLinkImplementationMap& hm = this->LinkImplMap[CONFIG];
  5352. // If the link implementation does not depend on the head target
  5353. // then return the one we computed first.
  5354. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5355. return &hm.begin()->second;
  5356. }
  5357. cmOptionalLinkImplementation& impl = hm[head];
  5358. if (!impl.LibrariesDone) {
  5359. impl.LibrariesDone = true;
  5360. this->ComputeLinkImplementationLibraries(config, impl, head);
  5361. }
  5362. return &impl;
  5363. }
  5364. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  5365. const std::string& p) const
  5366. {
  5367. return this->LinkImplicitNullProperties.find(p) !=
  5368. this->LinkImplicitNullProperties.end();
  5369. }
  5370. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  5371. const std::string& config, cmOptionalLinkImplementation& impl,
  5372. cmGeneratorTarget const* head) const
  5373. {
  5374. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  5375. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  5376. cmBacktraceRange::const_iterator btIt = btRange.begin();
  5377. // Collect libraries directly linked in this configuration.
  5378. for (cmStringRange::const_iterator le = entryRange.begin(),
  5379. end = entryRange.end();
  5380. le != end; ++le, ++btIt) {
  5381. std::vector<std::string> llibs;
  5382. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_LIBRARIES", nullptr,
  5383. nullptr);
  5384. cmGeneratorExpression ge(*btIt);
  5385. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  5386. std::string const& evaluated =
  5387. cge->Evaluate(this->LocalGenerator, config, false, head, &dagChecker);
  5388. cmSystemTools::ExpandListArgument(evaluated, llibs);
  5389. if (cge->GetHadHeadSensitiveCondition()) {
  5390. impl.HadHeadSensitiveCondition = true;
  5391. }
  5392. for (std::string const& lib : llibs) {
  5393. // Skip entries that resolve to the target itself or are empty.
  5394. std::string name = this->CheckCMP0004(lib);
  5395. if (name == this->GetName() || name.empty()) {
  5396. if (name == this->GetName()) {
  5397. bool noMessage = false;
  5398. MessageType messageType = MessageType::FATAL_ERROR;
  5399. std::ostringstream e;
  5400. switch (this->GetPolicyStatusCMP0038()) {
  5401. case cmPolicies::WARN: {
  5402. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  5403. messageType = MessageType::AUTHOR_WARNING;
  5404. } break;
  5405. case cmPolicies::OLD:
  5406. noMessage = true;
  5407. case cmPolicies::REQUIRED_IF_USED:
  5408. case cmPolicies::REQUIRED_ALWAYS:
  5409. case cmPolicies::NEW:
  5410. // Issue the fatal message.
  5411. break;
  5412. }
  5413. if (!noMessage) {
  5414. e << "Target \"" << this->GetName() << "\" links to itself.";
  5415. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  5416. messageType, e.str(), this->GetBacktrace());
  5417. if (messageType == MessageType::FATAL_ERROR) {
  5418. return;
  5419. }
  5420. }
  5421. }
  5422. continue;
  5423. }
  5424. // The entry is meant for this configuration.
  5425. impl.Libraries.emplace_back(this->ResolveLinkItem(name, *btIt),
  5426. evaluated != *le);
  5427. }
  5428. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  5429. for (std::string const& sp : seenProps) {
  5430. if (!this->GetProperty(sp)) {
  5431. this->LinkImplicitNullProperties.insert(sp);
  5432. }
  5433. }
  5434. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  5435. }
  5436. // Get the list of configurations considered to be DEBUG.
  5437. std::vector<std::string> debugConfigs =
  5438. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  5439. cmTargetLinkLibraryType linkType =
  5440. CMP0003_ComputeLinkType(config, debugConfigs);
  5441. cmTarget::LinkLibraryVectorType const& oldllibs =
  5442. this->Target->GetOriginalLinkLibraries();
  5443. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  5444. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  5445. std::string name = this->CheckCMP0004(oldllib.first);
  5446. if (name == this->GetName() || name.empty()) {
  5447. continue;
  5448. }
  5449. // Support OLD behavior for CMP0003.
  5450. impl.WrongConfigLibraries.push_back(
  5451. this->ResolveLinkItem(name, cmListFileBacktrace()));
  5452. }
  5453. }
  5454. }
  5455. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  5456. std::string const& name) const
  5457. {
  5458. cmLocalGenerator const* lg = this->LocalGenerator;
  5459. std::string const* lookupName = &name;
  5460. // When target_link_libraries() is called with a LHS target that is
  5461. // not created in the calling directory it adds a directory id suffix
  5462. // that we can use to look up the calling directory. It is that scope
  5463. // in which the item name is meaningful. This case is relatively rare
  5464. // so we allocate a separate string only when the directory id is present.
  5465. std::string::size_type pos = name.find(CMAKE_DIRECTORY_ID_SEP);
  5466. std::string plainName;
  5467. if (pos != std::string::npos) {
  5468. // We will look up the plain name without the directory id suffix.
  5469. plainName = name.substr(0, pos);
  5470. // We will look up in the scope of the directory id.
  5471. // If we do not recognize the id then leave the original
  5472. // syntax in place to produce an indicative error later.
  5473. cmDirectoryId const dirId =
  5474. name.substr(pos + sizeof(CMAKE_DIRECTORY_ID_SEP) - 1);
  5475. if (cmLocalGenerator const* otherLG =
  5476. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  5477. lg = otherLG;
  5478. lookupName = &plainName;
  5479. }
  5480. }
  5481. TargetOrString resolved;
  5482. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(*lookupName)) {
  5483. resolved.Target = tgt;
  5484. } else if (lookupName == &plainName) {
  5485. resolved.String = std::move(plainName);
  5486. } else {
  5487. resolved.String = name;
  5488. }
  5489. return resolved;
  5490. }
  5491. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  5492. std::string const& name, cmListFileBacktrace const& bt) const
  5493. {
  5494. TargetOrString resolved = this->ResolveTargetReference(name);
  5495. if (!resolved.Target) {
  5496. return cmLinkItem(resolved.String, bt);
  5497. }
  5498. // Skip targets that will not really be linked. This is probably a
  5499. // name conflict between an external library and an executable
  5500. // within the project.
  5501. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  5502. !resolved.Target->IsExecutableWithExports()) {
  5503. return cmLinkItem(resolved.Target->GetName(), bt);
  5504. }
  5505. return cmLinkItem(resolved.Target, bt);
  5506. }
  5507. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  5508. {
  5509. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  5510. // Return the directory in which the target will be built.
  5511. return info->PdbDir;
  5512. }
  5513. return "";
  5514. }
  5515. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  5516. {
  5517. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  5518. }
  5519. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  5520. std::string const& gnuName,
  5521. std::string& out,
  5522. const char* newExt) const
  5523. {
  5524. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  5525. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  5526. out = gnuName.substr(0, gnuName.size() - 6);
  5527. out += newExt ? newExt : ".lib";
  5528. return true;
  5529. }
  5530. return false;
  5531. }
  5532. bool cmGeneratorTarget::IsExecutableWithExports() const
  5533. {
  5534. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  5535. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  5536. }
  5537. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  5538. {
  5539. return (this->IsDLLPlatform() &&
  5540. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5541. this->IsExecutableWithExports()) &&
  5542. // Assemblies which have only managed code do not have
  5543. // import libraries.
  5544. this->GetManagedType(config) != ManagedType::Managed);
  5545. }
  5546. std::string cmGeneratorTarget::GetSupportDirectory() const
  5547. {
  5548. std::string dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  5549. dir += "/CMakeFiles";
  5550. dir += "/";
  5551. dir += this->GetName();
  5552. #if defined(__VMS)
  5553. dir += "_dir";
  5554. #else
  5555. dir += ".dir";
  5556. #endif
  5557. return dir;
  5558. }
  5559. bool cmGeneratorTarget::IsLinkable() const
  5560. {
  5561. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5562. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5563. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  5564. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  5565. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  5566. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  5567. this->IsExecutableWithExports());
  5568. }
  5569. bool cmGeneratorTarget::IsFrameworkOnApple() const
  5570. {
  5571. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5572. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  5573. this->Makefile->IsOn("APPLE") &&
  5574. this->GetPropertyAsBool("FRAMEWORK"));
  5575. }
  5576. bool cmGeneratorTarget::IsAppBundleOnApple() const
  5577. {
  5578. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  5579. this->Makefile->IsOn("APPLE") &&
  5580. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  5581. }
  5582. bool cmGeneratorTarget::IsXCTestOnApple() const
  5583. {
  5584. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  5585. }
  5586. bool cmGeneratorTarget::IsCFBundleOnApple() const
  5587. {
  5588. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  5589. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  5590. }
  5591. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  5592. std::string const& propval) const
  5593. {
  5594. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  5595. // or only C++/CLI) does only depend on whether the property is an empty
  5596. // string or contains any value at all. In Visual Studio generators
  5597. // this propval is prepended with /clr[:] which results in:
  5598. //
  5599. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  5600. // lib
  5601. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  5602. // target, has import lib
  5603. // 3. any value (safe,pure): add /clr:[propval] as flag, target with
  5604. // managed code only, no import lib
  5605. return propval.empty() ? ManagedType::Mixed : ManagedType::Managed;
  5606. }
  5607. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  5608. const std::string& config) const
  5609. {
  5610. // Only libraries and executables can be managed targets.
  5611. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  5612. return ManagedType::Undefined;
  5613. }
  5614. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5615. return ManagedType::Native;
  5616. }
  5617. // Check imported target.
  5618. if (this->IsImported()) {
  5619. if (cmGeneratorTarget::ImportInfo const* info =
  5620. this->GetImportInfo(config)) {
  5621. return info->Managed;
  5622. }
  5623. return ManagedType::Undefined;
  5624. }
  5625. // Check for explicitly set clr target property.
  5626. if (auto* clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  5627. return this->CheckManagedType(clr);
  5628. }
  5629. // C# targets are always managed. This language specific check
  5630. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  5631. // has to be set manually for C# targets.
  5632. return this->IsCSharpOnly() ? ManagedType::Managed : ManagedType::Native;
  5633. }